Skip to contentSkip to conversation

HID: hidraw: switch to using memdup_user

PATCH In review Dmitry Torokhov 3 participants 3 messages Mar 16, 2016 – Mar 24, 2016 HID

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 HID: hidraw: switch to using memdup_user

Instead of open-coding memory allocation and copying form user memory sequence let's use memdup_user().

---
 drivers/hid/hidraw.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/hid/hidraw.c +4 -9
index 9c2d7c2..b9a76e3 100644
@@ -34,6 +34,7 @@
 #include <linux/hid.h>
 #include <linux/mutex.h>
 #include <linux/sched.h>
+#include <linux/string.h>
 
 #include <linux/hidraw.h>
 
@@ -123,7 +124,6 @@ static ssize_t hidraw_send_report(struct file *file, const char __user *buffer,
 
 	dev = hidraw_table[minor]->hid;
 
-
 	if (count > HID_MAX_BUFFER_SIZE) {
 		hid_warn(dev, "pid %d passed too large report\n",
 			 task_pid_nr(current));
@@ -138,17 +138,12 @@ static ssize_t hidraw_send_report(struct file *file, const char __user *buffer,
 		goto out;
 	}
 
-	buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
-	if (!buf) {
-		ret = -ENOMEM;
+	buf = memdup_user(buffer, count);
+	if (IS_ERR(buf)) {
+		ret = PTR_ERR(buf);
 		goto out;
 	}
 
-	if (copy_from_user(buf, buffer, count)) {
-		ret = -EFAULT;
-		goto out_free;
-	}
-
 	if ((report_type == HID_OUTPUT_REPORT) &&
 	    !(dev->quirks & HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP)) {
 		ret = hid_hw_output_report(dev, buf, count);
Signature · 5 lines
2.7.0.rc3.207.g0ac5344


-- 
Dmitry
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Dmitry Torokhov

Instead of open-coding memory allocation and copying form user memory sequence let's use memdup_user().

---

Works for me:

Cheers,
Benjamin

Quoted 50 lines
 drivers/hid/hidraw.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/hid/hidraw.c +4 -9
index 9c2d7c2..b9a76e3 100644
@@ -34,6 +34,7 @@
 #include <linux/hid.h>
 #include <linux/mutex.h>
 #include <linux/sched.h>
+#include <linux/string.h>
 
 #include <linux/hidraw.h>
 
@@ -123,7 +124,6 @@ static ssize_t hidraw_send_report(struct file *file, const char __user *buffer,
 
 	dev = hidraw_table[minor]->hid;
 
-
 	if (count > HID_MAX_BUFFER_SIZE) {
 		hid_warn(dev, "pid %d passed too large report\n",
 			 task_pid_nr(current));
@@ -138,17 +138,12 @@ static ssize_t hidraw_send_report(struct file *file, const char __user *buffer,
 		goto out;
 	}
 
-	buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
-	if (!buf) {
-		ret = -ENOMEM;
+	buf = memdup_user(buffer, count);
+	if (IS_ERR(buf)) {
+		ret = PTR_ERR(buf);
 		goto out;
 	}
 
-	if (copy_from_user(buf, buffer, count)) {
-		ret = -EFAULT;
-		goto out_free;
-	}
-
 	if ((report_type == HID_OUTPUT_REPORT) &&
 	    !(dev->quirks & HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP)) {
 		ret = hid_hw_output_report(dev, buf, count);
Signature · 5 lines
2.7.0.rc3.207.g0ac5344


-- 
Dmitry
maintainer
#2 lore raw
In reply to Dmitry Torokhov
Quoted 2 lines · Dmitry Torokhov

Instead of open-coding memory allocation and copying form user memory sequence let's use memdup_user().

Makes sense. Applied, thanks.

Signature · 2 lines
Jiri Kosina
SUSE Labs