Skip to contentSkip to conversation

HID: intel-ish-hid: Reallocate buffers for a later, larger response

PATCH New Maoyi Xie 2 participants 5 messages Nov 23

Subsystems (3)
lore
Share & tools
lore

Maoyi Xie

author
#0 lore raw

cover HID: intel-ish-hid: Reallocate buffers for a later, larger response

process_recv() sizes the descriptor buffers of each device and the hid_devices array by the first firmware response. It then reuses them for later, larger responses. Patch 1 fixes the descriptor buffers and patch 2 fixes hid_devices.

A pending patch [1] limits hid_dev_count to MAX_HID_DEVICES, and it is still needed with this series. Patch 2 touches the lines next to that patch. If that patch goes in first, I can rebase patch 2 on it.

The series is based on hid.git for-7.4/upstream-fixes. In mainline, patch 2 conflicts with the treewide devm_kmalloc_obj() conversion (4dd10fbbeb9c), and taking the lines of patch 2 resolves it. I can send a version for mainline if you prefer.

I used an LLM to help write the harness and these patches.

[1] https://lore.kernel.org/all/[email protected]/

Maoyi Xie (2):
HID: intel-ish-hid: Reallocate the descriptor buffers for a longer
response
HID: intel-ish-hid: Reallocate hid_devices for a larger device count

 drivers/hid/intel-ish-hid/ishtp-hid-client.c | 41 +++++++++++---------
 1 file changed, 23 insertions(+), 18 deletions(-)

base-commit: 10bc1d627c297097cfdc099377f8a13efdfffd86

Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/2 HID: intel-ish-hid: Reallocate the descriptor buffers for a longer response

process_recv() allocates hid_descr[i] and report_descr[i] only when they are NULL. It then copies each response into them with the length of that response. After an ISH reset, a longer descriptor for the same index can overflow the buffer allocated for the first response. A second, longer response to one request does the same. After an empty first response, the next copy writes at ZERO_SIZE_PTR.

Size the buffer to the received length with devm_krealloc() before the copy.

Found with a CodeQL-based static analysis tool that we are developing. Reproduced in a userspace harness under AddressSanitizer, which reports none of these writes with this patch. Not tested on hardware, only compile-tested with W=1 on hid.git for-7.4/upstream-fixes.

Fixes 0b28cb4bcb17 ("HID: intel-ish-hid: ISH HID client driver")

---
I used an LLM to help write the harness and this patch.

 drivers/hid/intel-ish-hid/ishtp-hid-client.c | 21 ++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
drivers/hid/intel-ish-hid/ishtp-hid-client.c +11 -10
index ec5e8afe77bd..24f4032d6dfe 100644
@@ -68,6 +68,7 @@ static void process_recv(struct ishtp_cl *hid_ishtp_cl, void *recv_buf,
 {
 	struct hostif_msg *recv_msg;
 	unsigned char *payload;
+	unsigned char *descr;
 	struct device_info *dev_info;
 	int i, j;
 	size_t	payload_len, total_len, cur_pos, raw_len, msg_len;
@@ -172,11 +173,11 @@ static void process_recv(struct ishtp_cl *hid_ishtp_cl, void *recv_buf,
 				ish_hw_reset(ishtp_get_ishtp_device(hid_ishtp_cl));
 				break;
 			}
-			if (!client_data->hid_descr[curr_hid_dev])
-				client_data->hid_descr[curr_hid_dev] =
-				devm_kmalloc(cl_data_to_dev(client_data),
-					     payload_len, GFP_KERNEL);
-			if (client_data->hid_descr[curr_hid_dev]) {
+			descr = devm_krealloc(cl_data_to_dev(client_data),
+					      client_data->hid_descr[curr_hid_dev],
+					      payload_len, GFP_KERNEL);
+			if (descr) {
+				client_data->hid_descr[curr_hid_dev] = descr;
 				memcpy(client_data->hid_descr[curr_hid_dev],
 				       payload, payload_len);
 				client_data->hid_descr_size[curr_hid_dev] =
@@ -197,11 +198,11 @@ static void process_recv(struct ishtp_cl *hid_ishtp_cl, void *recv_buf,
 				ish_hw_reset(ishtp_get_ishtp_device(hid_ishtp_cl));
 				break;
 			}
-			if (!client_data->report_descr[curr_hid_dev])
-				client_data->report_descr[curr_hid_dev] =
-				devm_kmalloc(cl_data_to_dev(client_data),
-					     payload_len, GFP_KERNEL);
-			if (client_data->report_descr[curr_hid_dev])  {
+			descr = devm_krealloc(cl_data_to_dev(client_data),
+					      client_data->report_descr[curr_hid_dev],
+					      payload_len, GFP_KERNEL);
+			if (descr) {
+				client_data->report_descr[curr_hid_dev] = descr;
 				memcpy(client_data->report_descr[curr_hid_dev],
 				       payload,
 				       payload_len);
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/2] HID: intel-ish-hid: Reallocate the descriptor buffers for a longer response

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] HID: intel-ish-hid: UAF race in process_recv() on duplicate report descriptor --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] HID: intel-ish-hid: UAF race in process_recv() on duplicate report descriptor
--

commit 024e9922cb6a61e316edcda24a29fa99a9eae537
Author: Maoyi Xie <[email protected]>

HID: intel-ish-hid: Reallocate the descriptor buffers for a longer response

This commit replaces the one-time devm_kmalloc() calls for hid_descr[i] and report_descr[i] in process_recv() with devm_krealloc() so each buffer is resized to the received payload length before copying.

Quoted 4 lines
drivers/hid/intel-ish-hid/ishtp-hid-client.c +0 -0
index ec5e8afe77bd4..24f4032d6dfe1 100644

[ ... ]

Quoted 7 lines
drivers/hid/intel-ish-hid/ishtp-hid-client.c +0 -3
@@ -197,11 +198,11 @@ static void process_recv(struct ishtp_cl *hid_ishtp_cl, void *recv_buf,
 				ish_hw_reset(ishtp_get_ishtp_device(hid_ishtp_cl));
 				break;
 			}
-			if (!client_data->report_descr[curr_hid_dev])
-				client_data->report_descr[curr_hid_dev] =
-				devm_kmalloc(cl_data_to_dev(client_data),

drivers/hid/intel-ish-hid/ishtp-hid-client.c static void process_recv(struct ishtp_cl *hid_ishtp_cl, void *recv_buf, · line 208

Quoted 10 lines
drivers/hid/intel-ish-hid/ishtp-hid-client.c +5 -2
-					     payload_len, GFP_KERNEL);
-			if (client_data->report_descr[curr_hid_dev])  {
+			descr = devm_krealloc(cl_data_to_dev(client_data),
+					      client_data->report_descr[curr_hid_dev],
+					      payload_len, GFP_KERNEL);
+			if (descr) {
+				client_data->report_descr[curr_hid_dev] = descr;
 				memcpy(client_data->report_descr[curr_hid_dev],
 				       payload,
 				       payload_len);

[Severity: Medium]
Can a second HOSTIF_GET_REPORT_DESCRIPTOR response cause a use-after-free on client_data->report_descr[curr_hid_dev] in ishtp_hid_parse()?

During initial probe, hid_ishtp_cl_init() wakes up as soon as the first response sets report_descr_done and immediately calls ishtp_hid_probe() while cur_hid_dev is still i and init_done is still false. If a second, longer (or zero-length) response arrives before cur_hid_dev advances, devm_krealloc() frees the old buffer while hid_parse_report() is reading it:

CPU0 (probe) CPU1 (unbound_wq)
hid_ishtp_cl_init()
ishtp_get_report_descriptor()
process_recv()
report_descr[i] = descr report_descr_done = true wake_up_interruptible(&init_wait)
ishtp_hid_probe()

    ...
      ishtp_hid_parse()
        hid_parse_report(...,
          report_descr[i], ...)
                                  process_recv()
                                    devm_krealloc(..., report_descr[i], ...)
                                      free_dr(old_dr)
          kmemdup(start, size, ...)  <-- use-after-free

Ignoring or rejecting duplicate responses when report_descr_done (and hid_descr_done for HOSTIF_GET_HID_DESCRIPTOR) is already true would avoid reallocating the buffer while the waiter is using it.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#3 lore raw

PATCH 2/2 HID: intel-ish-hid: Reallocate hid_devices for a larger device count

In reply to Maoyi Xie

process_recv() allocates hid_devices only when it is NULL, with the count of the first ENUM_DEVICES response. It then copies as many entries as each response announces. After an ISH reset, a larger count can write past the array. A second response with a larger count does the same. After a first response with no devices, the next copy writes at ZERO_SIZE_PTR.

Size the array to the announced count with devm_krealloc_array() before the copy, and update hid_dev_count only when that succeeds. A count of zero keeps the old array as before, since hid_ishtp_cl_init() may still read it.

Found with a CodeQL-based static analysis tool that we are developing. Reproduced in a userspace harness under AddressSanitizer, which reports none of these writes with this patch. Not tested on hardware, only compile-tested with W=1 on hid.git for-7.4/upstream-fixes.

Fixes 0b28cb4bcb17 ("HID: intel-ish-hid: ISH HID client driver")

---
I used an LLM to help write the harness and this patch.

 drivers/hid/intel-ish-hid/ishtp-hid-client.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
drivers/hid/intel-ish-hid/ishtp-hid-client.c +12 -8
index 24f4032d6dfe..864a06a7bd3f 100644
@@ -70,6 +70,8 @@ static void process_recv(struct ishtp_cl *hid_ishtp_cl, void *recv_buf,
 	unsigned char *payload;
 	unsigned char *descr;
 	struct device_info *dev_info;
+	struct device_info *devices;
+	unsigned int count;
 	int i, j;
 	size_t	payload_len, total_len, cur_pos, raw_len, msg_len;
 	int report_type;
@@ -124,19 +126,21 @@ static void process_recv(struct ishtp_cl *hid_ishtp_cl, void *recv_buf,
 			}
 			if (client_data->init_done)
 				break;
-			client_data->hid_dev_count = (unsigned int)*payload;
-			if (!client_data->hid_devices)
-				client_data->hid_devices = devm_kcalloc(
-						cl_data_to_dev(client_data),
-						client_data->hid_dev_count,
-						sizeof(struct device_info),
-						GFP_KERNEL);
-			if (!client_data->hid_devices) {
+			count = *payload;
+			devices = client_data->hid_devices;
+			if (count || !devices)
+				devices = devm_krealloc_array(cl_data_to_dev(client_data),
+							      devices, count,
+							      sizeof(struct device_info),
+							      GFP_KERNEL | __GFP_ZERO);
+			if (!devices) {
 				dev_err(cl_data_to_dev(client_data),
 				"Mem alloc failed for hid device info\n");
 				wake_up_interruptible(&client_data->init_wait);
 				break;
 			}
+			client_data->hid_devices = devices;
+			client_data->hid_dev_count = count;
 			for (i = 0; i < client_data->hid_dev_count; ++i) {
 				if (1 + sizeof(struct device_info) * i >=
 						payload_len) {
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH 2/2] HID: intel-ish-hid: Reallocate hid_devices for a larger device count

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] HID: intel-ish-hid: use-after-free in process_recv() on hid_devices realloc --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] HID: intel-ish-hid: use-after-free in process_recv() on hid_devices realloc
--

commit 354365a28c1b9e56e376d6cd317766efa118e7ec
Author: Maoyi Xie <[email protected]>

HID: intel-ish-hid: Reallocate hid_devices for a larger device count

This commit uses devm_krealloc_array() in process_recv() to resize client_data->hid_devices when a HOSTIF_DM_ENUM_DEVICES response announces more devices than previously allocated.

Quoted 30 lines
drivers/hid/intel-ish-hid/ishtp-hid-client.c +7 -8
index 24f4032d6dfe1..864a06a7bd3ff 100644
@@ -70,6 +70,8 @@ static void process_recv(struct ishtp_cl *hid_ishtp_cl, void *recv_buf,
 	unsigned char *payload;
 	unsigned char *descr;
 	struct device_info *dev_info;
+	struct device_info *devices;
+	unsigned int count;
 	int i, j;
 	size_t	payload_len, total_len, cur_pos, raw_len, msg_len;
 	int report_type;
@@ -124,19 +126,21 @@ static void process_recv(struct ishtp_cl *hid_ishtp_cl, void *recv_buf,
 			}
 			if (client_data->init_done)
 				break;
-			client_data->hid_dev_count = (unsigned int)*payload;
-			if (!client_data->hid_devices)
-				client_data->hid_devices = devm_kcalloc(
-						cl_data_to_dev(client_data),
-						client_data->hid_dev_count,
-						sizeof(struct device_info),
-						GFP_KERNEL);
-			if (!client_data->hid_devices) {
+			count = *payload;
+			devices = client_data->hid_devices;
+			if (count || !devices)
+				devices = devm_krealloc_array(cl_data_to_dev(client_data),
+							      devices, count,

drivers/hid/intel-ish-hid/ishtp-hid-client.c static void process_recv(struct ishtp_cl *hid_ishtp_cl, void *recv_buf, · line 143

Quoted 10 lines
drivers/hid/intel-ish-hid/ishtp-hid-client.c +5 -0
+							      sizeof(struct device_info),
+							      GFP_KERNEL | __GFP_ZERO);
+			if (!devices) {
 				dev_err(cl_data_to_dev(client_data),
 				"Mem alloc failed for hid device info\n");
 				wake_up_interruptible(&client_data->init_wait);
 				break;
 			}
+			client_data->hid_devices = devices;
+			client_data->hid_dev_count = count;

[Severity: Medium]
Can reallocating client_data->hid_devices on a second HOSTIF_DM_ENUM_DEVICES response cause a use-after-free in hid_ishtp_cl_init()?

After the first response sets client_data->enum_devices_done and wakes client_data->init_wait, hid_ishtp_cl_init() proceeds to read client_data->hid_devices[] across descriptor queries and ishtp_hid_probe() while client_data->init_done is still false:

CPU0 CPU1
hid_ishtp_cl_init() process_recv()
ishtp_enum_enum_devices()

                                        case HOSTIF_DM_ENUM_DEVICES:
                                          devm_krealloc_array(...)
                                          enum_devices_done = true
                                          wake_up_interruptible(...)
  for (i = 0; i < num_hid_devices; ++i)
    ishtp_get_hid_descriptor(i)
                                        case HOSTIF_DM_ENUM_DEVICES:
                                          devm_krealloc_array(...)
                                            /* frees old hid_devices */
      client_data->hid_devices[i].dev_id  <-- use-after-free

client_data->init_done = 1

Because HOSTIF_DM_ENUM_DEVICES only checks client_data->init_done and not client_data->enum_devices_done, a second response with a larger count frees the old client_data->hid_devices buffer without synchronization while hid_ishtp_cl_init() is still iterating over it.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2