Commit 533aae7c authored by Bartosz Golaszewski's avatar Bartosz Golaszewski

gpiolib: cdev: fix NULL-pointer dereferences

There are several places where we can crash the kernel by requesting
lines, unbinding the GPIO device, then calling any of the system calls
relevant to the GPIO character device's annonymous file descriptors:
ioctl(), read(), poll().

While I observed it with the GPIO simulator, it will also happen for any
of the GPIO devices that can be hot-unplugged - for instance any HID GPIO
expander (e.g. CP2112).

This affects both v1 and v2 uAPI.

This fixes it partially by checking if gdev->chip is not NULL but it
doesn't entirely remedy the situation as we still have a race condition
in which another thread can remove the device after the check.

Fixes: d7c51b47 ("gpio: userspace ABI for reading/writing GPIO lines")
Fixes: 3c0d9c63 ("gpiolib: cdev: support GPIO_V2_GET_LINE_IOCTL and GPIO_V2_LINE_GET_VALUES_IOCTL")
Fixes: aad95584 ("gpiolib: cdev: support GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL")
Fixes: a54756cb ("gpiolib: cdev: support GPIO_V2_LINE_SET_CONFIG_IOCTL")
Fixes: 7b8e00d9 ("gpiolib: cdev: support GPIO_V2_LINE_SET_VALUES_IOCTL")
Signed-off-by: default avatarBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: default avatarAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: default avatarLinus Walleij <linus.walleij@linaro.org>
parent 3b7c7478
...@@ -201,6 +201,9 @@ static long linehandle_ioctl(struct file *file, unsigned int cmd, ...@@ -201,6 +201,9 @@ static long linehandle_ioctl(struct file *file, unsigned int cmd,
unsigned int i; unsigned int i;
int ret; int ret;
if (!lh->gdev->chip)
return -ENODEV;
switch (cmd) { switch (cmd) {
case GPIOHANDLE_GET_LINE_VALUES_IOCTL: case GPIOHANDLE_GET_LINE_VALUES_IOCTL:
/* NOTE: It's okay to read values of output lines */ /* NOTE: It's okay to read values of output lines */
...@@ -1384,6 +1387,9 @@ static long linereq_ioctl(struct file *file, unsigned int cmd, ...@@ -1384,6 +1387,9 @@ static long linereq_ioctl(struct file *file, unsigned int cmd,
struct linereq *lr = file->private_data; struct linereq *lr = file->private_data;
void __user *ip = (void __user *)arg; void __user *ip = (void __user *)arg;
if (!lr->gdev->chip)
return -ENODEV;
switch (cmd) { switch (cmd) {
case GPIO_V2_LINE_GET_VALUES_IOCTL: case GPIO_V2_LINE_GET_VALUES_IOCTL:
return linereq_get_values(lr, ip); return linereq_get_values(lr, ip);
...@@ -1410,6 +1416,9 @@ static __poll_t linereq_poll(struct file *file, ...@@ -1410,6 +1416,9 @@ static __poll_t linereq_poll(struct file *file,
struct linereq *lr = file->private_data; struct linereq *lr = file->private_data;
__poll_t events = 0; __poll_t events = 0;
if (!lr->gdev->chip)
return EPOLLHUP | EPOLLERR;
poll_wait(file, &lr->wait, wait); poll_wait(file, &lr->wait, wait);
if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events, if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events,
...@@ -1429,6 +1438,9 @@ static ssize_t linereq_read(struct file *file, ...@@ -1429,6 +1438,9 @@ static ssize_t linereq_read(struct file *file,
ssize_t bytes_read = 0; ssize_t bytes_read = 0;
int ret; int ret;
if (!lr->gdev->chip)
return -ENODEV;
if (count < sizeof(le)) if (count < sizeof(le))
return -EINVAL; return -EINVAL;
...@@ -1716,6 +1728,9 @@ static __poll_t lineevent_poll(struct file *file, ...@@ -1716,6 +1728,9 @@ static __poll_t lineevent_poll(struct file *file,
struct lineevent_state *le = file->private_data; struct lineevent_state *le = file->private_data;
__poll_t events = 0; __poll_t events = 0;
if (!le->gdev->chip)
return EPOLLHUP | EPOLLERR;
poll_wait(file, &le->wait, wait); poll_wait(file, &le->wait, wait);
if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock)) if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
...@@ -1740,6 +1755,9 @@ static ssize_t lineevent_read(struct file *file, ...@@ -1740,6 +1755,9 @@ static ssize_t lineevent_read(struct file *file,
ssize_t ge_size; ssize_t ge_size;
int ret; int ret;
if (!le->gdev->chip)
return -ENODEV;
/* /*
* When compatible system call is being used the struct gpioevent_data, * When compatible system call is being used the struct gpioevent_data,
* in case of at least ia32, has different size due to the alignment * in case of at least ia32, has different size due to the alignment
...@@ -1821,6 +1839,9 @@ static long lineevent_ioctl(struct file *file, unsigned int cmd, ...@@ -1821,6 +1839,9 @@ static long lineevent_ioctl(struct file *file, unsigned int cmd,
void __user *ip = (void __user *)arg; void __user *ip = (void __user *)arg;
struct gpiohandle_data ghd; struct gpiohandle_data ghd;
if (!le->gdev->chip)
return -ENODEV;
/* /*
* We can get the value for an event line but not set it, * We can get the value for an event line but not set it,
* because it is input by definition. * because it is input by definition.
...@@ -2407,6 +2428,9 @@ static __poll_t lineinfo_watch_poll(struct file *file, ...@@ -2407,6 +2428,9 @@ static __poll_t lineinfo_watch_poll(struct file *file,
struct gpio_chardev_data *cdev = file->private_data; struct gpio_chardev_data *cdev = file->private_data;
__poll_t events = 0; __poll_t events = 0;
if (!cdev->gdev->chip)
return EPOLLHUP | EPOLLERR;
poll_wait(file, &cdev->wait, pollt); poll_wait(file, &cdev->wait, pollt);
if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events, if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
...@@ -2425,6 +2449,9 @@ static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, ...@@ -2425,6 +2449,9 @@ static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
int ret; int ret;
size_t event_size; size_t event_size;
if (!cdev->gdev->chip)
return -ENODEV;
#ifndef CONFIG_GPIO_CDEV_V1 #ifndef CONFIG_GPIO_CDEV_V1
event_size = sizeof(struct gpio_v2_line_info_changed); event_size = sizeof(struct gpio_v2_line_info_changed);
if (count < event_size) if (count < event_size)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment