uhid: Pad short UHID_CREATE writes from compat tasks
Subsystems (2)
PATCH 1/1 uhid: Pad short UHID_CREATE writes from compat tasks
Short event writes are normally padded with zeroes, but the compat fixup for UHID_CREATE didn't ensure this. This appears to allow an information leak.
Compile-tested only.
---
I have no familiarity with uhid so I haven't written a test for this. It looks like it would be possible to write a UHID_CREATE event that only covers fields up to rd_size, and the following data on the heap would be copied to the HID device metadata and be readable that way.
Ben.
drivers/hid/uhid.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/hid/uhid.c +3 -0
@@ -298,6 +298,9 @@ static int uhid_event_from_user(const char __user *buffer, size_t len,kfree (compat);return -EFAULT ; } +if (len <sizeof (*compat)) +memset ((char *)buffer + len,0 , +sizeof (*compat) - len);/* Shuffle the data over to proper structure */ event->type = type;
Signature · 3 lines
Ben Hutchings
Usenet is essentially a HUGE group of people passing notes in class.
- Rachel Kadel, `A Quick Guide to Newsgroup Etiquette'Hi
Quoted 25 lines · Ben Hutchings
Short event writes are normally padded with zeroes, but the compat fixup for
UHID_CREATEdidn't ensure this. This appears to allow an information leak.Compile-tested only.
Fixes befde0226a59 ('HID: uhid: make creating devices work on 64/32 systems')Signed-off-by Ben Hutchings <[email protected]>---
I have no familiarity with uhid so I haven't written a test for this. It looks like it would be possible to write aUHID_CREATEevent that only covers fields up tord_size, and the following data on the heap would be copied to the HID device metadata and be readable that way.Ben.
drivers/hid/uhid.c | 3 +++ 1 file changed, 3 insertions(+)drivers/hid/uhid.c +0
-0
drivers/hid/uhid.c static int uhid_event_from_user(const char __user *buffer, size_t len, · line 303
Quoted 6 lines
drivers/hid/uhid.c +3
-0@@ -298,6 +298,9 @@ static int uhid_event_from_user(const char __user *buffer, size_t len,kfree (compat);return -EFAULT ; } +if (len <sizeof (*compat)) +memset ((char *)buffer + len,0 , +sizeof (*compat) - len);
This should be "compat", not "buffer".
Anyhow, nice catch! But the better fix imho is to use kzalloc() for the "compat" object. This isn't performance-critical and we can avoid any other off-by-one bug or future conversion errors. And besides, it's far easier to read than this memset().
Thanks
David
Quoted 7 lines
drivers/hid/uhid.c +0
-1/* Shuffle the data over to proper structure */ event->type = type; --Ben Hutchings
Usenet is essentially a HUGE group of people passing notes in class.
- Rachel Kadel, `A Quick Guide to Newsgroup Etiquette'
PATCH v2 1/1 HID: uhid: fix leak for 64/32 UHID_CREATE
In reply to Ben HutchingsUHID allows short writes so user-space can omit unused fields. We automatically set them to 0 in the kernel. However, the 64/32 bit compat-handler didn't do that in the UHID_CREATE fallback. This will reveal random kernel heap data (of random size, even) to user-space.
--- drivers/hid/uhid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hid/uhid.c +1 -1
@@ -287,7 +287,7 @@ static int uhid_event_from_user(const char __user *buffer, size_t len,*/ struct uhid_create_req_compat *compat; - compat =kmalloc (sizeof (*compat),GFP_KERNEL ); + compat =kzalloc (sizeof (*compat),GFP_KERNEL );if (!compat)return -ENOMEM ;
Signature · 1 line
1.8.4.2
Re: [PATCH v2] HID: uhid: fix leak for 64/32 UHID_CREATE
Quoted 8 lines · David Herrmann
UHID allows short writes so user-space can omit unused fields. We automatically set them to 0 in the kernel. However, the 64/32 bit compat-handler didn't do that in the
UHID_CREATEfallback. This will reveal random kernel heap data (of random size, even) to user-space.Reported-by Ben Hutchings <[email protected]>Signed-off-by David Herrmann <[email protected]>
(that should make it clear which versions need the fix)
Quoted 17 lines
--- drivers/hid/uhid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/hid/uhid.c +1
-1@@ -287,7 +287,7 @@ static int uhid_event_from_user(const char __user *buffer, size_t len,*/ struct uhid_create_req_compat *compat; - compat =kmalloc (sizeof (*compat),GFP_KERNEL ); + compat =kzalloc (sizeof (*compat),GFP_KERNEL );if (!compat)return -ENOMEM ;
Signature · 3 lines
Ben Hutchings
Usenet is essentially a HUGE group of people passing notes in class.
- Rachel Kadel, `A Quick Guide to Newsgroup Etiquette'Re: [PATCH v2] HID: uhid: fix leak for 64/32 UHID_CREATE
Quoted 6 lines · Ben Hutchings
Quoted 5 lines · David Herrmann
UHID allows short writes so user-space can omit unused fields. We automatically set them to 0 in the kernel. However, the 64/32 bit compat-handler didn't do that in the
UHID_CREATEfallback. This will reveal random kernel heap data (of random size, even) to user-space.
Quoted 7 lines
Quoted 3 lines
Reported-by Ben Hutchings <[email protected]>Signed-off-by David Herrmann <[email protected]>Fixes befde0226a59 ('HID: uhid: make creating devices work on 64/32 systems')(that should make it clear which versions need the fix)
Thanks a lot, applied with the Fixes: annotation added.
Signature · 2 lines
Jiri Kosina SUSE Labs