Skip to contentSkip to conversation

HID: tighten ioctl command parsing

PATCH In review Arnd Bergmann 3 participants 7 messages Jul 11, 2025 – Aug 26, 2025

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

PATCH 1/1 HID: tighten ioctl command parsing

From: Arnd Bergmann <[email protected]>

The handling for variable-length ioctl commands in hidraw_ioctl() is rather complex and the check for the data direction is incomplete.

Simplify this code using a switch() statement with the size masked out, to ensure the rest of the command is correctly matched.

Fixes 9188e79ec3fd ("HID: add phys and name ioctls to hidraw")
---
 drivers/hid/hidraw.c | 123 ++++++++++++++++++-------------------------
 1 file changed, 50 insertions(+), 73 deletions(-)
drivers/hid/hidraw.c +50 -73
index c887f48756f4..cc657d60d689 100644
@@ -403,6 +403,8 @@ static long hidraw_ioctl(struct file *file, unsigned int cmd,
 	struct hidraw *dev;
 	struct hidraw_list *list = file->private_data;
 	void __user *user_arg = (void __user*) arg;
+	struct hid_device *hid;
+	int len;
 
 	down_read(&minors_rwsem);
 	dev = hidraw_table[minor];
@@ -453,81 +455,56 @@ static long hidraw_ioctl(struct file *file, unsigned int cmd,
 				break;
 			}
 		default:
-			{
-				struct hid_device *hid = dev->hid;
-				if (_IOC_TYPE(cmd) != 'H') {
-					ret = -EINVAL;
-					break;
-				}
-
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
-					int len = _IOC_SIZE(cmd);
-					ret = hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
-					break;
-				}
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
-					int len = _IOC_SIZE(cmd);
-					ret = hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
-					break;
-				}
-
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSINPUT(0))) {
-					int len = _IOC_SIZE(cmd);
-					ret = hidraw_send_report(file, user_arg, len, HID_INPUT_REPORT);
-					break;
-				}
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGINPUT(0))) {
-					int len = _IOC_SIZE(cmd);
-					ret = hidraw_get_report(file, user_arg, len, HID_INPUT_REPORT);
-					break;
-				}
-
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSOUTPUT(0))) {
-					int len = _IOC_SIZE(cmd);
-					ret = hidraw_send_report(file, user_arg, len, HID_OUTPUT_REPORT);
-					break;
-				}
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGOUTPUT(0))) {
-					int len = _IOC_SIZE(cmd);
-					ret = hidraw_get_report(file, user_arg, len, HID_OUTPUT_REPORT);
-					break;
-				}
-
-				/* Begin Read-only ioctls. */
-				if (_IOC_DIR(cmd) != _IOC_READ) {
-					ret = -EINVAL;
-					break;
-				}
-
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
-					int len = strlen(hid->name) + 1;
-					if (len > _IOC_SIZE(cmd))
-						len = _IOC_SIZE(cmd);
-					ret = copy_to_user(user_arg, hid->name, len) ?
-						-EFAULT : len;
-					break;
-				}
-
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
-					int len = strlen(hid->phys) + 1;
-					if (len > _IOC_SIZE(cmd))
-						len = _IOC_SIZE(cmd);
-					ret = copy_to_user(user_arg, hid->phys, len) ?
-						-EFAULT : len;
-					break;
-				}
-
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWUNIQ(0))) {
-					int len = strlen(hid->uniq) + 1;
-					if (len > _IOC_SIZE(cmd))
-						len = _IOC_SIZE(cmd);
-					ret = copy_to_user(user_arg, hid->uniq, len) ?
-						-EFAULT : len;
-					break;
-				}
-			}
+			break;
+	}
 
+	hid = dev->hid;
+	switch (cmd & ~IOCSIZE_MASK) {
+	case HIDIOCSFEATURE(0):
+		len = _IOC_SIZE(cmd);
+		ret = hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
+		break;
+	case HIDIOCGFEATURE(0):
+		len = _IOC_SIZE(cmd);
+		ret = hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
+		break;
+	case HIDIOCSINPUT(0):
+		len = _IOC_SIZE(cmd);
+		ret = hidraw_send_report(file, user_arg, len, HID_INPUT_REPORT);
+		break;
+	case HIDIOCGINPUT(0):
+		len = _IOC_SIZE(cmd);
+		ret = hidraw_get_report(file, user_arg, len, HID_INPUT_REPORT);
+		break;
+	case HIDIOCSOUTPUT(0):
+		len = _IOC_SIZE(cmd);
+		ret = hidraw_send_report(file, user_arg, len, HID_OUTPUT_REPORT);
+		break;
+	case HIDIOCGOUTPUT(0):
+		len = _IOC_SIZE(cmd);
+		ret = hidraw_get_report(file, user_arg, len, HID_OUTPUT_REPORT);
+		break;
+	case HIDIOCGRAWNAME(0):
+		len = strlen(hid->name) + 1;
+		if (len > _IOC_SIZE(cmd))
+			len = _IOC_SIZE(cmd);
+		ret = copy_to_user(user_arg, hid->name, len) ?  -EFAULT : len;
+		break;
+	case HIDIOCGRAWPHYS(0):
+		len = strlen(hid->phys) + 1;
+		if (len > _IOC_SIZE(cmd))
+			len = _IOC_SIZE(cmd);
+		ret = copy_to_user(user_arg, hid->phys, len) ?  -EFAULT : len;
+		break;
+	case HIDIOCGRAWUNIQ(0):
+		len = strlen(hid->uniq) + 1;
+		if (len > _IOC_SIZE(cmd))
+			len = _IOC_SIZE(cmd);
+		ret = copy_to_user(user_arg, hid->uniq, len) ?  -EFAULT : len;
+		break;
+	default:
 		ret = -ENOTTY;
+		break;
 	}
 out:
 	up_read(&minors_rwsem);
Signature · 1 line
2.39.5
#1 lore raw

Hi Arnd,

Quoted 7 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The handling for variable-length ioctl commands in hidraw_ioctl() is rather complex and the check for the data direction is incomplete.

Simplify this code using a switch() statement with the size masked out, to ensure the rest of the command is correctly matched.

How much "urgent" you believe this patch is. I would say 6.17 material, but I'm not sure if your analysis regarding "the check for the data direction is incomplete." would justify a 6.16 late fix.

Also, lately I added a new tools/testing/selftests/hid/hidraw.c to test the latest HIDIOCREVOKE addition. I wonder if we should not add a couple of checks there to ensure we get the different kind of other ioctls tested before and after this patch.

Cheers,
Benjamin

Quoted 153 lines
Fixes 9188e79ec3fd ("HID: add phys and name ioctls to hidraw")
---
 drivers/hid/hidraw.c | 123 ++++++++++++++++++-------------------------
 1 file changed, 50 insertions(+), 73 deletions(-)
drivers/hid/hidraw.c +50 -73
index c887f48756f4..cc657d60d689 100644
@@ -403,6 +403,8 @@ static long hidraw_ioctl(struct file *file, unsigned int cmd,
 	struct hidraw *dev;
 	struct hidraw_list *list = file->private_data;
 	void __user *user_arg = (void __user*) arg;
+	struct hid_device *hid;
+	int len;
 
 	down_read(&minors_rwsem);
 	dev = hidraw_table[minor];
@@ -453,81 +455,56 @@ static long hidraw_ioctl(struct file *file, unsigned int cmd,
 				break;
 			}
 		default:
-			{
-				struct hid_device *hid = dev->hid;
-				if (_IOC_TYPE(cmd) != 'H') {
-					ret = -EINVAL;
-					break;
-				}
-
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
-					int len = _IOC_SIZE(cmd);
-					ret = hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
-					break;
-				}
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
-					int len = _IOC_SIZE(cmd);
-					ret = hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
-					break;
-				}
-
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSINPUT(0))) {
-					int len = _IOC_SIZE(cmd);
-					ret = hidraw_send_report(file, user_arg, len, HID_INPUT_REPORT);
-					break;
-				}
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGINPUT(0))) {
-					int len = _IOC_SIZE(cmd);
-					ret = hidraw_get_report(file, user_arg, len, HID_INPUT_REPORT);
-					break;
-				}
-
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSOUTPUT(0))) {
-					int len = _IOC_SIZE(cmd);
-					ret = hidraw_send_report(file, user_arg, len, HID_OUTPUT_REPORT);
-					break;
-				}
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGOUTPUT(0))) {
-					int len = _IOC_SIZE(cmd);
-					ret = hidraw_get_report(file, user_arg, len, HID_OUTPUT_REPORT);
-					break;
-				}
-
-				/* Begin Read-only ioctls. */
-				if (_IOC_DIR(cmd) != _IOC_READ) {
-					ret = -EINVAL;
-					break;
-				}
-
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
-					int len = strlen(hid->name) + 1;
-					if (len > _IOC_SIZE(cmd))
-						len = _IOC_SIZE(cmd);
-					ret = copy_to_user(user_arg, hid->name, len) ?
-						-EFAULT : len;
-					break;
-				}
-
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
-					int len = strlen(hid->phys) + 1;
-					if (len > _IOC_SIZE(cmd))
-						len = _IOC_SIZE(cmd);
-					ret = copy_to_user(user_arg, hid->phys, len) ?
-						-EFAULT : len;
-					break;
-				}
-
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWUNIQ(0))) {
-					int len = strlen(hid->uniq) + 1;
-					if (len > _IOC_SIZE(cmd))
-						len = _IOC_SIZE(cmd);
-					ret = copy_to_user(user_arg, hid->uniq, len) ?
-						-EFAULT : len;
-					break;
-				}
-			}
+			break;
+	}
 
+	hid = dev->hid;
+	switch (cmd & ~IOCSIZE_MASK) {
+	case HIDIOCSFEATURE(0):
+		len = _IOC_SIZE(cmd);
+		ret = hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
+		break;
+	case HIDIOCGFEATURE(0):
+		len = _IOC_SIZE(cmd);
+		ret = hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
+		break;
+	case HIDIOCSINPUT(0):
+		len = _IOC_SIZE(cmd);
+		ret = hidraw_send_report(file, user_arg, len, HID_INPUT_REPORT);
+		break;
+	case HIDIOCGINPUT(0):
+		len = _IOC_SIZE(cmd);
+		ret = hidraw_get_report(file, user_arg, len, HID_INPUT_REPORT);
+		break;
+	case HIDIOCSOUTPUT(0):
+		len = _IOC_SIZE(cmd);
+		ret = hidraw_send_report(file, user_arg, len, HID_OUTPUT_REPORT);
+		break;
+	case HIDIOCGOUTPUT(0):
+		len = _IOC_SIZE(cmd);
+		ret = hidraw_get_report(file, user_arg, len, HID_OUTPUT_REPORT);
+		break;
+	case HIDIOCGRAWNAME(0):
+		len = strlen(hid->name) + 1;
+		if (len > _IOC_SIZE(cmd))
+			len = _IOC_SIZE(cmd);
+		ret = copy_to_user(user_arg, hid->name, len) ?  -EFAULT : len;
+		break;
+	case HIDIOCGRAWPHYS(0):
+		len = strlen(hid->phys) + 1;
+		if (len > _IOC_SIZE(cmd))
+			len = _IOC_SIZE(cmd);
+		ret = copy_to_user(user_arg, hid->phys, len) ?  -EFAULT : len;
+		break;
+	case HIDIOCGRAWUNIQ(0):
+		len = strlen(hid->uniq) + 1;
+		if (len > _IOC_SIZE(cmd))
+			len = _IOC_SIZE(cmd);
+		ret = copy_to_user(user_arg, hid->uniq, len) ?  -EFAULT : len;
+		break;
+	default:
 		ret = -ENOTTY;
+		break;
 	}
 out:
 	up_read(&minors_rwsem);
Signature · 1 line
2.39.5
#2 lore raw
Quoted 7 lines · Benjamin Tissoires
Quoted 5 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The handling for variable-length ioctl commands in hidraw_ioctl() is rather complex and the check for the data direction is incomplete.

Quoted 6 lines
Quoted 2 lines

Simplify this code using a switch() statement with the size masked out, to ensure the rest of the command is correctly matched.

How much "urgent" you believe this patch is. I would say 6.17 material, but I'm not sure if your analysis regarding "the check for the data direction is incomplete." would justify a 6.16 late fix.

I'm not aware of anything being actively broken without my patch, the driver just accepts extra commands that it should reject instead.

My feeling is that we still want the change backported to stable kernels as it does address incorrect behavior, and based on that it should be in a fixes branch for the current release rather than wait for the merge window.

On the other hand, it needs to be properly tested to ensure I'm not breaking things.

Quoted 4 lines

Also, lately I added a new tools/testing/selftests/hid/hidraw.c to test the latest HIDIOCREVOKE addition. I wonder if we should not add a couple of checks there to ensure we get the different kind of other ioctls tested before and after this patch.

Yes, makes sense, e.g. testing HIDIOCGFEATURE with incorrect _IOC_DIR bits would be a useful addition.

Arnd

#4 lore raw
In reply to Arnd Bergmann
Quoted 99 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The handling for variable-length ioctl commands in hidraw_ioctl() is rather complex and the check for the data direction is incomplete.

Simplify this code using a switch() statement with the size masked out, to ensure the rest of the command is correctly matched.

Fixes 9188e79ec3fd ("HID: add phys and name ioctls to hidraw")
---
 drivers/hid/hidraw.c | 123 ++++++++++++++++++-------------------------
 1 file changed, 50 insertions(+), 73 deletions(-)
drivers/hid/hidraw.c +2 -68
index c887f48756f4..cc657d60d689 100644
@@ -403,6 +403,8 @@ static long hidraw_ioctl(struct file *file, unsigned int cmd,
 	struct hidraw *dev;
 	struct hidraw_list *list = file->private_data;
 	void __user *user_arg = (void __user*) arg;
+	struct hid_device *hid;
+	int len;
 
 	down_read(&minors_rwsem);
 	dev = hidraw_table[minor];
@@ -453,81 +455,56 @@ static long hidraw_ioctl(struct file *file, unsigned int cmd,
 				break;
 			}
 		default:
-			{
-				struct hid_device *hid = dev->hid;
-				if (_IOC_TYPE(cmd) != 'H') {
-					ret = -EINVAL;
-					break;
-				}
-
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
-					int len = _IOC_SIZE(cmd);
-					ret = hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
-					break;
-				}
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
-					int len = _IOC_SIZE(cmd);
-					ret = hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
-					break;
-				}
-
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSINPUT(0))) {
-					int len = _IOC_SIZE(cmd);
-					ret = hidraw_send_report(file, user_arg, len, HID_INPUT_REPORT);
-					break;
-				}
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGINPUT(0))) {
-					int len = _IOC_SIZE(cmd);
-					ret = hidraw_get_report(file, user_arg, len, HID_INPUT_REPORT);
-					break;
-				}
-
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSOUTPUT(0))) {
-					int len = _IOC_SIZE(cmd);
-					ret = hidraw_send_report(file, user_arg, len, HID_OUTPUT_REPORT);
-					break;
-				}
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGOUTPUT(0))) {
-					int len = _IOC_SIZE(cmd);
-					ret = hidraw_get_report(file, user_arg, len, HID_OUTPUT_REPORT);
-					break;
-				}
-
-				/* Begin Read-only ioctls. */
-				if (_IOC_DIR(cmd) != _IOC_READ) {
-					ret = -EINVAL;
-					break;
-				}
-
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
-					int len = strlen(hid->name) + 1;
-					if (len > _IOC_SIZE(cmd))
-						len = _IOC_SIZE(cmd);
-					ret = copy_to_user(user_arg, hid->name, len) ?
-						-EFAULT : len;
-					break;
-				}
-
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
-					int len = strlen(hid->phys) + 1;
-					if (len > _IOC_SIZE(cmd))
-						len = _IOC_SIZE(cmd);
-					ret = copy_to_user(user_arg, hid->phys, len) ?
-						-EFAULT : len;
-					break;
-				}
-
-				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWUNIQ(0))) {
-					int len = strlen(hid->uniq) + 1;
-					if (len > _IOC_SIZE(cmd))
-						len = _IOC_SIZE(cmd);

drivers/hid/hidraw.c static long hidraw_ioctl(struct file *file, unsigned int cmd, · line 462

Quoted 10 lines
drivers/hid/hidraw.c +4 -5
-					ret = copy_to_user(user_arg, hid->uniq, len) ?
-						-EFAULT : len;
-					break;
-				}
-			}
+			break;
+	}
 
+	hid = dev->hid;
+	switch (cmd & ~IOCSIZE_MASK) {

Jiri pinged me about this one, and I gave it a go with the existing tests I have in selftests... and turns out that this changes the logic of the ioctl processing.

The removed block was in the default section of the switch/case statement. Now it's added *after*, meaning that any ioctl that was normally processed before are now caught in the default of the switch statement below and return -ENOTTY.

Running tools/testing/selftests/hid/hid_bpf showed that.

Cheers,
Benjamin

Quoted 51 lines
drivers/hid/hidraw.c +44 -0
+	case HIDIOCSFEATURE(0):
+		len = _IOC_SIZE(cmd);
+		ret = hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
+		break;
+	case HIDIOCGFEATURE(0):
+		len = _IOC_SIZE(cmd);
+		ret = hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
+		break;
+	case HIDIOCSINPUT(0):
+		len = _IOC_SIZE(cmd);
+		ret = hidraw_send_report(file, user_arg, len, HID_INPUT_REPORT);
+		break;
+	case HIDIOCGINPUT(0):
+		len = _IOC_SIZE(cmd);
+		ret = hidraw_get_report(file, user_arg, len, HID_INPUT_REPORT);
+		break;
+	case HIDIOCSOUTPUT(0):
+		len = _IOC_SIZE(cmd);
+		ret = hidraw_send_report(file, user_arg, len, HID_OUTPUT_REPORT);
+		break;
+	case HIDIOCGOUTPUT(0):
+		len = _IOC_SIZE(cmd);
+		ret = hidraw_get_report(file, user_arg, len, HID_OUTPUT_REPORT);
+		break;
+	case HIDIOCGRAWNAME(0):
+		len = strlen(hid->name) + 1;
+		if (len > _IOC_SIZE(cmd))
+			len = _IOC_SIZE(cmd);
+		ret = copy_to_user(user_arg, hid->name, len) ?  -EFAULT : len;
+		break;
+	case HIDIOCGRAWPHYS(0):
+		len = strlen(hid->phys) + 1;
+		if (len > _IOC_SIZE(cmd))
+			len = _IOC_SIZE(cmd);
+		ret = copy_to_user(user_arg, hid->phys, len) ?  -EFAULT : len;
+		break;
+	case HIDIOCGRAWUNIQ(0):
+		len = strlen(hid->uniq) + 1;
+		if (len > _IOC_SIZE(cmd))
+			len = _IOC_SIZE(cmd);
+		ret = copy_to_user(user_arg, hid->uniq, len) ?  -EFAULT : len;
+		break;
+	default:
 		ret = -ENOTTY;
+		break;
 	}
 out:
 	up_read(&minors_rwsem);
Signature · 1 line
2.39.5
#3 lore raw
In reply to Arnd Bergmann
Quoted 18 lines · Arnd Bergmann
Quoted 7 lines · Benjamin Tissoires
Quoted 5 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The handling for variable-length ioctl commands in hidraw_ioctl() is rather complex and the check for the data direction is incomplete.

Quoted 6 lines
Quoted 2 lines

Simplify this code using a switch() statement with the size masked out, to ensure the rest of the command is correctly matched.

How much "urgent" you believe this patch is. I would say 6.17 material, but I'm not sure if your analysis regarding "the check for the data direction is incomplete." would justify a 6.16 late fix.

I'm not aware of anything being actively broken without my patch, the driver just accepts extra commands that it should reject instead.

Quoted 12 lines

My feeling is that we still want the change backported to stable kernels as it does address incorrect behavior, and based on that it should be in a fixes branch for the current release rather than wait for the merge window.

On the other hand, it needs to be properly tested to ensure I'm not breaking things.

Quoted 4 lines

Also, lately I added a new tools/testing/selftests/hid/hidraw.c to test the latest HIDIOCREVOKE addition. I wonder if we should not add a couple of checks there to ensure we get the different kind of other ioctls tested before and after this patch.

FWIW, I was told to use more AI in my company, and I was too lazy to write a full coverage. So I asked claude to help me out on that and the result is pasted down below. Feel free to include them in v2 (note:
checkpatch complains about the co-developed-by tag, but not sure we have a concensus for that ATM).

Quoted 3 lines

Yes, makes sense, e.g. testing HIDIOCGFEATURE with incorrect _IOC_DIR bits would be a useful addition.

The current behaviour for HIDIOCGRAWNAME with a wrong _IOC_DIR bit is to return -EINVAL, while with your patch applied it's changed to -ENOTTY. Is that expected?

---

From 71110ae5e5f30e0bfe529faf8987d676410f053f Mon Sep 17 00:00:00 2001
From: Benjamin Tissoires <[email protected]>
Date: Thu, 21 Aug 2025 09:42:34 +0200
Subject: [PATCH 2/3] selftests/hid: hidraw: add more coverage for hidraw
ioctls

Try to ensure all ioctls are having at least one test.

Co-developed-by claude-4-sonnet
---
 tools/testing/selftests/hid/hid_common.h |   6 +
 tools/testing/selftests/hid/hidraw.c     | 346 +++++++++++++++++++++++
 2 files changed, 352 insertions(+)
tools/testing/selftests/hid/hid_common.h +6 -0
index f77f69c6657d..8085519c47cb 100644
@@ -230,6 +230,12 @@ static int uhid_event(struct __test_metadata *_metadata, int fd)
 		break;
 	case UHID_SET_REPORT:
 		UHID_LOG("UHID_SET_REPORT from uhid-dev");
+
+		answer.type = UHID_SET_REPORT_REPLY;
+		answer.u.set_report_reply.id = ev.u.set_report.id;
+		answer.u.set_report_reply.err = 0; /* success */
+
+		uhid_write(_metadata, fd, &answer);
 		break;
 	default:
 		TH_LOG("Invalid event from uhid-dev: %u", ev.type);
tools/testing/selftests/hid/hidraw.c +346 -0

358 lines not on this page · raw message

Signature · 17 lines
2.50.1


From 62cbd33ca66b578c3e1ec715cfa308ea48b4dc15 Mon Sep 17 00:00:00 2001
From: Benjamin Tissoires <[email protected]>
Date: Thu, 21 Aug 2025 09:44:11 +0200
Subject: [PATCH 3/3] selftests/hid: hidraw: forge wrong ioctls and tests them

We also need coverage for when the malicious user is not using the
proper ioctls definitions and tries to work around the driver.

Suggested-by: Arnd Bergmann <[email protected]>
Co-developed-by: claude-4-sonnet
Signed-off-by: Benjamin Tissoires <[email protected]>
---
 tools/testing/selftests/hid/hidraw.c | 61 ++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)
tools/testing/selftests/hid/hidraw.c +61 -0

67 lines not on this page · raw message

Signature · 6 lines
2.50.1

---

Cheers,
Benjamin
#5 lore raw
In reply to Benjamin Tissoires
Quoted 12 lines · Benjamin Tissoires

drivers/hid/hidraw.c static long hidraw_ioctl(struct file *file, unsigned int cmd, · line 462

Quoted 6 lines · Arnd Bergmann
drivers/hid/hidraw.c +4 -0
+			break;
+	}
 
+	hid = dev->hid;
+	switch (cmd & ~IOCSIZE_MASK) {

Jiri pinged me about this one, and I gave it a go with the existing tests I have in selftests... and turns out that this changes the logic of the ioctl processing.

Quoted 6 lines

The removed block was in the default section of the switch/case statement. Now it's added *after*, meaning that any ioctl that was normally processed before are now caught in the default of the switch statement below and return -ENOTTY.

Running tools/testing/selftests/hid/hid_bpf showed that.

Ah, of course, thanks for checking and describing the issue.

Did you already come up with a fixed patch? I'm currently travelling and won't be able to send a v2 quickly, so if you have a version that works for you, let's use that instead.

Arnd

#6 lore raw
Quoted 18 lines · Arnd Bergmann
Quoted 17 lines · Benjamin Tissoires

drivers/hid/hidraw.c static long hidraw_ioctl(struct file *file, unsigned int cmd, · line 462

Quoted 6 lines · Arnd Bergmann
drivers/hid/hidraw.c +4 -0
+			break;
+	}
 
+	hid = dev->hid;
+	switch (cmd & ~IOCSIZE_MASK) {

Jiri pinged me about this one, and I gave it a go with the existing tests I have in selftests... and turns out that this changes the logic of the ioctl processing.

The removed block was in the default section of the switch/case statement. Now it's added *after*, meaning that any ioctl that was normally processed before are now caught in the default of the switch statement below and return -ENOTTY.

Quoted 8 lines
Quoted 1 line

Running tools/testing/selftests/hid/hid_bpf showed that.

Ah, of course, thanks for checking and describing the issue.

Did you already come up with a fixed patch? I'm currently travelling and won't be able to send a v2 quickly, so if you have a version that works for you, let's use that instead.

I spent yesterday trying to find a way to make the old behavior looks the same than the new, and got a rather big rewrite of your patch. I'll send this out as a v2, keeping your From and SoB, but please shout if you are unhappy with those 2 tags.

Cheers,
Benjamin