Skip to contentSkip to conversation

HID: debug: check length before copy_to_user()

PATCH In review Daniel Rosenberg 4 participants 4 messages Jul 2, 2018 – Jul 5, 2018

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

PATCH 1/1 HID: debug: check length before copy_to_user()

If our length is greater than the size of the buffer, we overflow the buffer

---
 drivers/hid/hid-debug.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/hid/hid-debug.c +7 -1
index 8469b6964ff64..b48100236df89 100644
@@ -1154,6 +1154,8 @@ static ssize_t hid_debug_events_read(struct file *file, char __user *buffer,
 			goto out;
 		if (list->tail > list->head) {
 			len = list->tail - list->head;
+			if (len > count)
+				len = count;
 
 			if (copy_to_user(buffer + ret, &list->hid_debug_buf[list->head], len)) {
 				ret = -EFAULT;
@@ -1163,6 +1165,8 @@ static ssize_t hid_debug_events_read(struct file *file, char __user *buffer,
 			list->head += len;
 		} else {
 			len = HID_DEBUG_BUFSIZE - list->head;
+			if (len > count)
+				len = count;
 
 			if (copy_to_user(buffer, &list->hid_debug_buf[list->head], len)) {
 				ret = -EFAULT;
@@ -1170,7 +1174,9 @@ static ssize_t hid_debug_events_read(struct file *file, char __user *buffer,
 			}
 			list->head = 0;
 			ret += len;
-			goto copy_rest;
+			count -= len;
+			if (count > 0)
+				goto copy_rest;
 		}
 
 	}
Signature · 1 line
2.18.0.399.gad0ab374a1-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Daniel Rosenberg

If our length is greater than the size of the buffer, we overflow the buffer

---

Looks good:

Cheers,
Benjamin

Quoted 39 lines
 drivers/hid/hid-debug.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/hid/hid-debug.c +7 -2
index 8469b6964ff64..b48100236df89 100644
@@ -1154,6 +1154,8 @@ static ssize_t hid_debug_events_read(struct file *file, char __user *buffer,
                        goto out;
                if (list->tail > list->head) {
                        len = list->tail - list->head;
+                       if (len > count)
+                               len = count;
 
                        if (copy_to_user(buffer + ret, &list->hid_debug_buf[list->head], len)) {
                                ret = -EFAULT;
@@ -1163,6 +1165,8 @@ static ssize_t hid_debug_events_read(struct file *file, char __user *buffer,
                        list->head += len;
                } else {
                        len = HID_DEBUG_BUFSIZE - list->head;
+                       if (len > count)
+                               len = count;
 
                        if (copy_to_user(buffer, &list->hid_debug_buf[list->head], len)) {
                                ret = -EFAULT;
@@ -1170,7 +1174,9 @@ static ssize_t hid_debug_events_read(struct file *file, char __user *buffer,
                        }
                        list->head = 0;
                        ret += len;
-                       goto copy_rest;
+                       count -= len;
+                       if (count > 0)
+                               goto copy_rest;
                }
 
        }
--

2.18.0.399.gad0ab374a1-goog

#3 lore raw
In reply to Daniel Rosenberg
Quoted 2 lines · Daniel Rosenberg

If our length is greater than the size of the buffer, we overflow the buffer

Hmm...

How about this:

	buf = list->hid_debug_buf;
	if (list->tail < list->head) {
		ret = simple_read_from_buffer(buffer, count, &list->head,
					buf, HID_DEBUG_BUFSIZE);
		if (ret < 0)
			break;
		if (list->head != buf + HID_DEBUG_BUFSIZE)
			break;
		list->head = 0;
	}
	n = simple_read_from_buffer(buffer + ret, count - ret, &list->head,
					buf, list->tail);
	if (n >= 0)
		ret += n;
	if (list->head == buf + HID_DEBUG_BUFSIZE)
		list->head = 0;

instead?