Skip to contentSkip to conversation

uhid: Pad short UHID_CREATE writes from compat tasks

PATCH In review Ben Hutchings 3 participants 5 messages Nov 26, 2013 – Nov 27, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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.

Fixes befde0226a59 ('HID: uhid: make creating devices work on 64/32 systems')

---
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
index 5bf2fb7..579a7115 100644
@@ -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'
#1 lore raw

Hi

Quoted 25 lines · Ben Hutchings

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.

Fixes befde0226a59 ('HID: uhid: make creating devices work on 64/32 systems')

---
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 +0 -0
index 5bf2fb7..579a7115 100644

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'

#2 lore raw

PATCH v2 1/1 HID: uhid: fix leak for 64/32 UHID_CREATE

In reply to Ben Hutchings

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_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
index 93b00d7..cedc6da 100644
@@ -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
#3 lore raw

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_CREATE fallback. This will reveal random kernel heap data (of random size, even) to user-space.

Fixes befde0226a59 ('HID: uhid: make creating devices work on 64/32 systems')

(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
index 93b00d7..cedc6da 100644
@@ -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'
#4 lore raw

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_CREATE fallback. This will reveal random kernel heap data (of random size, even) to user-space.

Quoted 7 lines
Quoted 3 lines
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