leds: uleds: fix unchecked copy_to_user() in uleds_read()
PATCH 1/1 leds: uleds: fix unchecked copy_to_user() in uleds_read()
The copy_to_user() is annotated with __must_check, indicating that its return value must be checked by the caller. Currently, uleds_read() ignores it. If the userspace buffer is invalid and copy_to_user() fails, the userspace application may assume it has received fresh data, while in fact copying has failed. This can leave applications out of sync with the actual device state.
Found by Linux Verification Center (linuxtesting.org) with SVACE.
--- drivers/leds/uleds.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/leds/uleds.c +7 -4
@@ -147,10 +147,13 @@ static ssize_t uleds_read(struct file *file, char __user *buffer, size_t count, }else if (!udev->new_data && (file->f_flags &O_NONBLOCK )) { retval = -EAGAIN ; }else if (udev->new_data) { - retval =copy_to_user (buffer, &udev->brightness, -sizeof (udev->brightness)); - udev->new_data = false; - retval =sizeof (udev->brightness); +if (copy_to_user (buffer, &udev->brightness, +sizeof (udev->brightness))) { + retval = -EFAULT ; + }else { + udev->new_data = false; + retval =sizeof (udev->brightness); + } }mutex_unlock (&udev->mutex);
Signature · 1 line
2.39.5
Lee Jones
maintainerQuoted 20 lines · Ivan Stepchenko
The
copy_to_user()is annotated with__must_check, indicating that its return value must be checked by the caller. Currently,uleds_read()ignores it. If the userspace buffer is invalid andcopy_to_user()fails, the userspace application may assume it has received fresh data, while in fact copying has failed. This can leave applications out of sync with the actual device state.Found by Linux Verification Center (linuxtesting.org) with SVACE.
Fixes e381322b0190 ("leds: Introduce userspace LED class driver")Signed-off-by Ivan Stepchenko <[email protected]>--- drivers/leds/uleds.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)drivers/leds/uleds.c +0
-0
drivers/leds/uleds.c static ssize_t uleds_read(struct file *file, char __user *buffer, size_t count, · line 151
Quoted 9 lines
drivers/leds/uleds.c +2
-4@@ -147,10 +147,13 @@ static ssize_t uleds_read(struct file *file, char __user *buffer, size_t count, }else if (!udev->new_data && (file->f_flags &O_NONBLOCK )) { retval = -EAGAIN ; }else if (udev->new_data) { - retval =copy_to_user (buffer, &udev->brightness, -sizeof (udev->brightness)); - udev->new_data = false; - retval =sizeof (udev->brightness); +if (copy_to_user (buffer, &udev->brightness, +sizeof (udev->brightness))) {
This is not good.
Please store the result into a variable and return that instead.
Quoted 11 lines
drivers/leds/uleds.c +5
-0+ retval = -EFAULT ; + }else { + udev->new_data = false; + retval =sizeof (udev->brightness); + } }mutex_unlock (&udev->mutex);Signature · 1 line
2.39.5
Signature · 1 line
Lee Jones [李琼斯]
Quoted 34 lines · Lee Jones
Quoted 20 lines · Ivan Stepchenko
The
copy_to_user()is annotated with__must_check, indicating that its return value must be checked by the caller. Currently,uleds_read()ignores it. If the userspace buffer is invalid andcopy_to_user()fails, the userspace application may assume it has received fresh data, while in fact copying has failed. This can leave applications out of sync with the actual device state.Found by Linux Verification Center (linuxtesting.org) with SVACE.
Fixes e381322b0190 ("leds: Introduce userspace LED class driver")Signed-off-by Ivan Stepchenko <[email protected]>--- drivers/leds/uleds.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)drivers/leds/uleds.c +0
-0drivers/leds/uleds.c static ssize_t uleds_read(struct file *file, char __user *buffer, size_t count, · line 151
Quoted 9 lines
drivers/leds/uleds.c +2
-4@@ -147,10 +147,13 @@ static ssize_t uleds_read(struct file *file, char __user *buffer, size_t count, }else if (!udev->new_data && (file->f_flags &O_NONBLOCK )) { retval = -EAGAIN ; }else if (udev->new_data) { - retval =copy_to_user (buffer, &udev->brightness, -sizeof (udev->brightness)); - udev->new_data = false; - retval =sizeof (udev->brightness); +if (copy_to_user (buffer, &udev->brightness, +sizeof (udev->brightness))) {This is not good.
Quoted 1 line
Please store the result into a variable and return that instead.
Every other caller of copy_to_user() in the kernel I've seen ignores the actual return value and returns -EFAULT, so I thought this looked correct and it was just a quirk of copy_to_user().
Quoted 13 lines
Quoted 12 lines
drivers/leds/uleds.c +5
-0+ retval = -EFAULT ; + }else { + udev->new_data = false; + retval =sizeof (udev->brightness); + } }mutex_unlock (&udev->mutex);Signature · 1 line
2.39.5
Lee Jones
maintainerQuoted 35 lines · David Lechner
Quoted 34 lines · Lee Jones
Quoted 20 lines · Ivan Stepchenko
The
copy_to_user()is annotated with__must_check, indicating that its return value must be checked by the caller. Currently,uleds_read()ignores it. If the userspace buffer is invalid andcopy_to_user()fails, the userspace application may assume it has received fresh data, while in fact copying has failed. This can leave applications out of sync with the actual device state.Found by Linux Verification Center (linuxtesting.org) with SVACE.
Fixes e381322b0190 ("leds: Introduce userspace LED class driver")Signed-off-by Ivan Stepchenko <[email protected]>--- drivers/leds/uleds.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)drivers/leds/uleds.c +0
-0drivers/leds/uleds.c static ssize_t uleds_read(struct file *file, char __user *buffer, size_t count, · line 151
Quoted 9 lines
drivers/leds/uleds.c +2
-4@@ -147,10 +147,13 @@ static ssize_t uleds_read(struct file *file, char __user *buffer, size_t count, }else if (!udev->new_data && (file->f_flags &O_NONBLOCK )) { retval = -EAGAIN ; }else if (udev->new_data) { - retval =copy_to_user (buffer, &udev->brightness, -sizeof (udev->brightness)); - udev->new_data = false; - retval =sizeof (udev->brightness); +if (copy_to_user (buffer, &udev->brightness, +sizeof (udev->brightness))) {This is not good.
Quoted 5 lines
Quoted 1 line
Please store the result into a variable and return that instead.
Every other caller of
copy_to_user()in the kernel I've seen ignores the actual return value and returns-EFAULT, so I thought this looked correct and it was just a quirk ofcopy_to_user().
Yes, I think you're right. Interesting.
So my counterproposal is as follows:
drivers/leds/uleds.c +5 -4
@@ -147,10 +147,11 @@ static ssize_t uleds_read(struct file *file, char __user *buffer, size_t count, }else if (!udev->new_data && (file->f_flags &O_NONBLOCK )) { retval = -EAGAIN ; }else if (udev->new_data) { - retval =copy_to_user (buffer, &udev->brightness, -sizeof (udev->brightness)); - udev->new_data = false; - retval =sizeof (udev->brightness); +ssize_t size = retval =sizeof (udev->brightness); +if (copy_to_user (buffer, &udev->brightness, size)) + retval = -EFAULT ; +else + udev->new_data = false; }mutex_unlock (&udev->mutex);
Quoted 13 lines
Quoted 12 lines
Quoted 11 lines
drivers/leds/uleds.c +5
-0+ retval = -EFAULT ; + }else { + udev->new_data = false; + retval =sizeof (udev->brightness); + } }mutex_unlock (&udev->mutex);Signature · 1 line
2.39.5
Signature · 1 line
Lee Jones [李琼斯]