core: check for positive values returned by calls to ioctl()
authorBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Thu, 25 Jan 2024 08:06:29 +0000 (09:06 +0100)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Mon, 29 Jan 2024 10:12:41 +0000 (11:12 +0100)
commitcfac97acb4392b3be317f1a18bdb12f2b7f0e2ca
tree18792fb81fa21014620ac162ab9b63c945e598bf
parentfc2f2feaa99dce8aab6381168051f2725b537621
core: check for positive values returned by calls to ioctl()

If the kernel GPIO driver (erroneously) returns a positive value from one
of its callbacks, it may end up being propagated to user space as
a positive value returned by the call to ioctl(). Let's treat all
non-zero values as errors as GPIO uAPI ioctl()s are not expected to ever
return positive values.

To that end let's create a wrapper around the libc's ioctl() that checks
the return value and sets errno to EBADE (Invalid exchange) if it's
greater than 0.

This should be addressed in the kernel but will remain a problem on older
or unpatched versions so we need to sanitize it in user-space too.

Reported-by: José Guilherme de Castro Rodrigues <joseguilhermebh@hotmail.com>
Fixes: b7ba732e6a93 ("treewide: libgpiod v2 implementation")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Kent Gibson <warthog618@gmail.com>
lib/chip.c
lib/internal.c
lib/internal.h
lib/line-request.c