linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Meng Xu <mengxu.gatech@gmail.com>
To: dh.herrmann@googlemail.com, jikos@kernel.org,
	benjamin.tissoires@redhat.com, linux-input@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: meng.xu@gatech.edu, sanidhya@gatech.edu, taesoo@gatech.edu,
	Meng Xu <mengxu.gatech@gmail.com>
Subject: [PATCH] hid/uhid: fix a double-fetch bug when copying event from user
Date: Tue, 19 Sep 2017 01:21:37 -0400	[thread overview]
Message-ID: <1505798497-22436-1-git-send-email-mengxu.gatech@gmail.com> (raw)

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

             reply	other threads:[~2017-09-19  5:21 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-19  5:21 Meng Xu [this message]
2017-09-19 21:31 ` [PATCH] hid/uhid: fix a double-fetch bug when copying event from user Dmitry Torokhov
2017-09-19 21:54   ` Meng Xu
2017-09-19 22:12     ` Dmitry Torokhov
2017-09-22 12:27       ` David Herrmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1505798497-22436-1-git-send-email-mengxu.gatech@gmail.com \
    --to=mengxu.gatech@gmail.com \
    --cc=benjamin.tissoires@redhat.com \
    --cc=dh.herrmann@googlemail.com \
    --cc=jikos@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=meng.xu@gatech.edu \
    --cc=sanidhya@gatech.edu \
    --cc=taesoo@gatech.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).