mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] hid/uhid: fix a double-fetch bug when copying event from user
@ 2017-09-19  5:21 Meng Xu
  2017-09-19 21:31 ` Dmitry Torokhov
  0 siblings, 1 reply; 5+ messages in thread
From: Meng Xu @ 2017-09-19  5:21 UTC (permalink / raw)
  To: dh.herrmann, jikos, benjamin.tissoires, linux-input, linux-kernel
  Cc: meng.xu, sanidhya, taesoo, Meng Xu

When in_compat_syscall(), a user could make type != UHID_CREATE when
get_user(type, buffer) [first fetch] and later make event->type ==
UHID_CREATE in copy_from_user(event, buffer, ...) [second fetch].

By doing so, an attacker might circumvent the specific logic to handle
the type == UHID_CREATE case and later cause undefined behaviors.

This patch enforces that event->type is overriden to the type value
copied in the first fetch and thus, mitigate this race condition attack.

Signed-off-by: Meng Xu <mengxu.gatech@gmail.com>
---
 drivers/hid/uhid.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/drivers/hid/uhid.c b/drivers/hid/uhid.c
index 7f8ff39..4bbfd8a 100644
--- a/drivers/hid/uhid.c
+++ b/drivers/hid/uhid.c
@@ -448,11 +448,20 @@ static int uhid_event_from_user(const char __user *buffer, size_t len,
 			kfree(compat);
 			return 0;
 		}
+
 		/* All others can be copied directly */
-	}
+		if (copy_from_user(event, buffer, min(len, sizeof(*event))))
+			return -EFAULT;
 
-	if (copy_from_user(event, buffer, min(len, sizeof(*event))))
-		return -EFAULT;
+		/* 
+		 * Override type in case the user process rushes to change it 
+		 * between two fetches 
+		 * */
+		event->type = type;
+	} else {
+		if (copy_from_user(event, buffer, min(len, sizeof(*event))))
+			return -EFAULT;
+	}
 
 	return 0;
 }
-- 
2.7.4

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-09-22 12:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-19  5:21 [PATCH] hid/uhid: fix a double-fetch bug when copying event from user Meng Xu
2017-09-19 21:31 ` Dmitry Torokhov
2017-09-19 21:54   ` Meng Xu
2017-09-19 22:12     ` Dmitry Torokhov
2017-09-22 12:27       ` David Herrmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®