FAILED: patch "[PATCH] HID: ft260: fix stack-use-after-return write in I2C read race" failed to apply to 6.1-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 6.1-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.1.y
git checkout FETCH_HEAD
git cherry-pick -x bf3e39df3a397fd82967a31d17c4e02c7feab221
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026082538-overhaul-surging-7a4a@gregkh' --subject-prefix 'PATCH 6.1.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From bf3e39df3a397fd82967a31d17c4e02c7feab221 Mon Sep 17 00:00:00 2001
From: Raman Varabets <[email protected]>
Date: Wed, 10 Jun 2026 22:29:52 +0800
Subject: [PATCH] HID: ft260: fix stack-use-after-return write in I2C read race
ft260_i2c_read() points dev->read_buf at a caller-supplied buffer (often an on-stack variable), arms a completion and waits up to five seconds for the device to return the data. The HID input callback ft260_raw_event() runs in the input/IRQ path, independent of the dev->lock mutex held by the read path, and copies the device-supplied payload into dev->read_buf after a plain NULL check.
These two paths share read_buf, read_idx and read_len with no serialization. If the device delays its response until the read times out, ft260_i2c_read() resets the controller, clears read_buf and returns, unwinding the stack frame the buffer lived in. A response that arrives at that moment lets ft260_raw_event() pass the NULL check and then memcpy() the device-controlled payload into the now-freed stack location, a bounded but attacker-influenced stack-use-after-return write triggerable by malicious or malfunctioning hardware.
Add a dedicated spinlock that serializes every access to read_buf, read_idx and read_len. ft260_raw_event() now holds it across the NULL check, the memcpy and the index update, while the read path takes it when arming and when clearing the buffer, so the teardown can no longer slip between the check and the copy.
drivers/hid/hid-ft260.c +24 -1
@@ -240,6 +240,8 @@ struct ft260_device {struct mutex lock;u8 write_buf[FT260_REPORT_MAX_LENGTH ];unsigned long need_wakeup_at; +/* Protects read_buf, read_idx and read_len against ft260_raw_event() */ +spinlock_t read_lock;u8 *read_buf;u16 read_idx;u16 read_len;
@@ -501,6 +503,7 @@ static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data,int timeout, ret =0 ;struct ft260_i2c_read_request_report rep;struct hid_device *hdev = dev->hdev; +unsigned long irqflags;u8 bus_busy =0 ;/* * STOP terminates the last chunk; clear means hold the bus so a
@@ -534,9 +537,11 @@ static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data,reinit_completion (&dev->wait); +spin_lock_irqsave (&dev->read_lock, irqflags); dev->read_idx =0 ; dev->read_buf = data; dev->read_len = rd_len; +spin_unlock_irqrestore (&dev->read_lock, irqflags); ret =ft260_hid_output_report (hdev, (u8 *)&rep,sizeof (rep));if (ret <0 ) {
@@ -551,7 +556,9 @@ static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data, goto ft260_i2c_read_exit; } +spin_lock_irqsave (&dev->read_lock, irqflags); dev->read_buf =NULL ; +spin_unlock_irqrestore (&dev->read_lock, irqflags);if (flag &FT260_FLAG_STOP ) bus_busy =FT260_I2C_STATUS_BUS_BUSY ;
@@ -570,7 +577,9 @@ static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data, }while (len >0 ); ft260_i2c_read_exit: +spin_lock_irqsave (&dev->read_lock, irqflags); dev->read_buf =NULL ; +spin_unlock_irqrestore (&dev->read_lock, irqflags);return ret; }
@@ -1053,6 +1062,7 @@ static int ft260_probe(struct hid_device *hdev, const struct hid_device_id *id) "FT260 usb-i2c bridge");mutex_init (&dev->lock); +spin_lock_init (&dev->read_lock);init_completion (&dev->wait); ret =ft260_xfer_status (dev,FT260_I2C_STATUS_BUS_BUSY );
@@ -1102,6 +1112,7 @@ static int ft260_raw_event(struct hid_device *hdev, struct hid_report *report, {struct ft260_device *dev =hid_get_drvdata (hdev);struct ft260_i2c_input_report *xfer = (void *)data; +unsigned long irqflags;if (size < offsetof(struct ft260_i2c_input_report , data)) {hid_err (hdev,"short report %d\n" , size);
@@ -1110,6 +1121,8 @@ static int ft260_raw_event(struct hid_device *hdev, struct hid_report *report,if (xfer->report >=FT260_I2C_REPORT_MIN && xfer->report <=FT260_I2C_REPORT_MAX ) { +bool complete_read; +ft260_dbg ("i2c resp: rep %#02x len %d size %d\n" , xfer->report, xfer->length, size);
@@ -1120,8 +1133,15 @@ static int ft260_raw_event(struct hid_device *hdev, struct hid_report *report,return -1 ; } +/* +* Hold read_lock so a timed-out ft260_i2c_read() cannot +* clear read_buf between the NULL check and the memcpy. +*/ +spin_lock_irqsave (&dev->read_lock, irqflags); +if ((dev->read_buf ==NULL ) || (xfer->length > dev->read_len - dev->read_idx)) { +spin_unlock_irqrestore (&dev->read_lock, irqflags);hid_err (hdev,"unexpected report %#02x, length %d\n" , xfer->report, xfer->length);return -1 ;
@@ -1130,8 +1150,11 @@ static int ft260_raw_event(struct hid_device *hdev, struct hid_report *report,memcpy (&dev->read_buf[dev->read_idx], &xfer->data, xfer->length); dev->read_idx += xfer->length; + complete_read = dev->read_idx == dev->read_len; -if (dev->read_idx == dev->read_len) +spin_unlock_irqrestore (&dev->read_lock, irqflags); + +if (complete_read)complete (&dev->wait); }else {
Sasha Levin
maintainerPATCH 1/7 HID: ft260: improve i2c write performance
From: Michael Zaidman <[email protected]>
[ Upstream commit 6fca5e3f5574ca1bd5bade5737848c816f924c6a ]
The patch improves the I2C write performance by 20 - 30 percent by revising the sleep time in the ft260_hid_output_report_check_status() in the following ways:
1. Reduce the wait time and start to poll earlier.
Sending a large amount of data at a low I2C clock rate saturates the internal FT260 buffer and causes hiccups in status readiness, as shown below in the log fragment. Aligning the status check wait time to the worst case significantly reduces the write performance.
[Oct22 10:28] ft260_i2c_write: rep 0xd8 addr 0x51 off 0 len 34 d[0] 0x0
[ +0.005296] ft260_xfer_status: bus_status 0x20, clock 100 [ +0.013460] ft260_i2c_write: rep 0xd8 addr 0x51 off 0 len 34 d[0] 0x0 [ +0.003244] ft260_hid_output_report_check_status: wait 1920 usec, len 38 [ +0.000190] ft260_xfer_status: bus_status 0x40, clock 100 [ +0.015324] ft260_i2c_write: rep 0xd8 addr 0x51 off 0 len 34 d[0] 0x0 [ +0.003491] ft260_hid_output_report_check_status: wait 1920 usec, len 38 [ +0.000202] ft260_xfer_status: bus_status 0x40, clock 100 [ +0.016047] ft260_i2c_write: rep 0xd8 addr 0x51 off 0 len 34 d[0] 0x0 [ +0.002768] ft260_hid_output_report_check_status: wait 1920 usec, len 38 [ +0.000150] ft260_xfer_status: bus_status 0x40, clock 100 [ +0.011389] ft260_i2c_write: rep 0xd8 addr 0x51 off 0 len 34 d[0] 0x0 [ +0.003467] ft260_hid_output_report_check_status: wait 1920 usec, len 38 [ +0.000191] ft260_xfer_status: bus_status 0x41, clock 100 [ +0.000172] ft260_xfer_status: bus_status 0x41, clock 100 [ +0.000131] ft260_xfer_status: bus_status 0x41, clock 100 [ +0.000241] ft260_xfer_status: bus_status 0x41, clock 100 [ +0.000233] ft260_xfer_status: bus_status 0x41, clock 100 [ +0.000190] ft260_xfer_status: bus_status 0x41, clock 100 [ +0.000196] ft260_xfer_status: bus_status 0x40, clock 100 [ +0.011314] ft260_i2c_write: rep 0xd8 addr 0x51 off 0 len 34 d[0] 0x0 [ +0.003334] ft260_hid_output_report_check_status: wait 1920 usec, len 38 [ +0.000227] ft260_xfer_status: bus_status 0x41, clock 100 [ +0.000204] ft260_xfer_status: bus_status 0x41, clock 100 [ +0.000198] ft260_xfer_status: bus_status 0x41, clock 100 [ +0.000147] ft260_xfer_status: bus_status 0x40, clock 100 [ +0.011060] ft260_i2c_write: rep 0xd8 addr 0x51 off 0 len 34 d[0] 0x0
Before:
$ sudo ./i2cperf -f 2 -o 2 -s 32 -r 0-0xff 13 0x51 -S
Fill block with increment via i2ctransfer by chunks
-------------------------------------------------------------------
data rate(bps) efficiency(%) data size(B) total IOs IO size(B)
-------------------------------------------------------------------
40510 80 256 8 32
After:
$ sudo ./i2cperf -f 2 -o 2 -s 32 -r 0-0xff 13 0x51 -S
Fill block with increment via i2ctransfer by chunks
-------------------------------------------------------------------
data rate(bps) efficiency(%) data size(B) total IOs IO size(B)
-------------------------------------------------------------------
52584 80 256 8 32
2. Do not sleep if the estimated I2C transfer time is below 2 ms since the first xfer status query frequently takes around 1.5 ms, and the following status queries take about 200us on average. So we usually return from the routine after the first 1 - 3 status checks.
[Oct22 11:14] ft260_i2c_write: rep 0xd4 addr 0x51 off 0 len 18 d[0] 0x0
[ +0.004270] ft260_xfer_status: bus_status 0x20, clock 100 [ +0.013889] ft260_i2c_write: rep 0xd4 addr 0x51 off 0 len 18 d[0] 0x0 [ +0.000856] ft260_xfer_status: bus_status 0x41, clock 100 [ +0.000138] ft260_xfer_status: bus_status 0x40, clock 100 [ +0.013352] ft260_i2c_write: rep 0xd4 addr 0x51 off 0 len 18 d[0] 0x0 [ +0.001501] ft260_xfer_status: bus_status 0x41, clock 100 [ +0.000177] ft260_xfer_status: bus_status 0x40, clock 100 [ +0.014477] ft260_i2c_write: rep 0xd4 addr 0x51 off 0 len 18 d[0] 0x0 [ +0.001377] ft260_xfer_status: bus_status 0x41, clock 100 [ +0.000233] ft260_xfer_status: bus_status 0x41, clock 100 [ +0.000191] ft260_xfer_status: bus_status 0x40, clock 100 [ +0.013197] ft260_i2c_write: rep 0xd4 addr 0x51 off 0 len 18 d[0] 0x0
Before:
$ sudo ./i2cperf -f 2 -o 2 -s 16 -r 0-0xff 13 0x51 -S
Fill block with increment via i2ctransfer by chunks
-------------------------------------------------------------------
data rate(bps) efficiency(%) data size(B) total IOs IO size(B)
-------------------------------------------------------------------
28826 73 256 16 16
After:
$ sudo ./i2cperf -f 2 -o 2 -s 16 -r 0-0xff 13 0x51 -S
Fill block with increment via i2ctransfer by chunks
-------------------------------------------------------------------
data rate(bps) efficiency(%) data size(B) total IOs IO size(B)
-------------------------------------------------------------------
45138 73 256 16 16
Stable-dep-of: bf3e39df3a39 ("HID: ft260: fix stack-use-after-return write in I2C read race")
--- drivers/hid/hid-ft260.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/hid/hid-ft260.c +9 -5
@@ -355,7 +355,7 @@ static int ft260_hid_output_report(struct hid_device *hdev, u8 *data,static int ft260_hid_output_report_check_status (struct ft260_device *dev,u8 *data,int len) { -int ret, usec, try =3 ; +int ret, usec, try =100 ;struct hid_device *hdev = dev->hdev; ret =ft260_hid_output_report (hdev, data, len);
@@ -366,10 +366,14 @@ static int ft260_hid_output_report_check_status(struct ft260_device *dev,return ret; } -/* transfer time = 1 / clock(KHz) * 10 bits * bytes */ - usec =10000 / dev->clock * len; -usleep_range (usec, usec +100 ); -ft260_dbg ("wait %d usec, len %d\n" , usec, len); +/* transfer time = 1 / clock(KHz) * 9 bits * bytes */ + usec = len *9000 / dev->clock; +if (usec >2000 ) { + usec -=1500 ; +usleep_range (usec, usec +100 ); +ft260_dbg ("wait %d usec, len %d\n" , usec, len); + } + do { ret = ft260_xfer_status(dev); if (ret != -EAGAIN)
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/7 HID: ft260: improve i2c large reads performance
From: Michael Zaidman <[email protected]>
[ Upstream commit 54410c14800ad652c77e5c6fc5c17baad6e42cb6 ]
The patch increases the read buffer size to 180 bytes. It reduces the number of ft260_i2c_read() calls by three, improving the big reads performance.
$ sudo i2ctransfer -y -f 13 w2@0x51 0x0 0x0 r180
Before:
[ +4.071878] ft260_i2c_write_read: off 0x0 rlen 180 wlen 2 [ +0.000005] ft260_i2c_write: rep 0xd0 addr 0x51 off 0 len 2 wlen 2 flag 0x2 d[0] 0x0 [ +0.001097] ft260_xfer_status: bus_status 0x41, clock 100 [ +0.000175] ft260_xfer_status: bus_status 0x40, clock 100 [ +0.000004] ft260_i2c_read: rep 0xc2 addr 0x51 len 180 rlen 60 flag 0x3 [ +0.008579] ft260_raw_event: i2c resp: rep 0xde len 60 [ +0.000208] ft260_xfer_status: bus_status 0x40, clock 100 [ +0.000001] ft260_i2c_read: rep 0xc2 addr 0x51 len 120 rlen 60 flag 0x0 [ +0.008794] ft260_raw_event: i2c resp: rep 0xde len 60 [ +0.000181] ft260_xfer_status: bus_status 0x40, clock 100 [ +0.000002] ft260_i2c_read: rep 0xc2 addr 0x51 len 60 rlen 60 flag 0x4 [ +0.008817] ft260_raw_event: i2c resp: rep 0xde len 60 [ +0.000223] ft260_xfer_status: bus_status 0x20, clock 100
After:
[ +11.611642] ft260_i2c_write_read: off 0x0 rlen 180 wlen 2
[ +0.000005] ft260_i2c_write: rep 0xd0 addr 0x51 off 0 len 2 wlen 2 flag 0x2 d[0] 0x0 [ +0.008001] ft260_xfer_status: bus_status 0x20, clock 100 [ +0.000001] ft260_i2c_read: rep 0xc2 addr 0x51 len 180 rlen 180 flag 0x7 [ +0.008994] ft260_raw_event: i2c resp: rep 0xde len 60 [ +0.007987] ft260_raw_event: i2c resp: rep 0xde len 60 [ +0.007992] ft260_raw_event: i2c resp: rep 0xde len 60 [ +0.000206] ft260_xfer_status: bus_status 0x20, clock 100
Stable-dep-of: bf3e39df3a39 ("HID: ft260: fix stack-use-after-return write in I2C read race")
--- drivers/hid/hid-ft260.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
drivers/hid/hid-ft260.c +12 -5
@@ -30,12 +30,19 @@ MODULE_PARM_DESC(debug, "Toggle FT260 debugging messages");#define FT260_REPORT_MAX_LENGTH (64)#define FT260_I2C_DATA_REPORT_ID (len) (FT260_I2C_REPORT_MIN + (len - 1) / 4) +/* -* The input report format assigns 62 bytes for the data payload, but ft260 -* returns 60 and 2 in two separate transactions. To minimize transfer time -* in reading chunks mode, set the maximum read payload length to 60 bytes. -*/ -#define FT260_RD_DATA_MAX (60) +* The ft260 input report format defines 62 bytes for the data payload, but +* when requested 62 bytes, the controller returns 60 and 2 in separate input +* reports. To achieve better performance with the multi-report read data +* transfers, we set the maximum read payload length to a multiple of 60. +* With a 100 kHz I2C clock, one 240 bytes read takes about 1/27 second, +* which is excessive; On the other hand, some higher layer drivers like at24 +* or optoe limit the i2c reads to 128 bytes. To not block other drivers out +* of I2C for potentially troublesome amounts of time, we select the maximum +* read payload length to be 180 bytes. +*/ +#define FT260_RD_DATA_MAX (180)#define FT260_WR_DATA_MAX (60)/*
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/7 HID: ft260: skip unexpected HID input reports
In reply to Sasha LevinFrom: Michael Zaidman <[email protected]>
[ Upstream commit b7121e3c04440cc2af9cabbabb24efd23741294a ]
The FT260 is not supposed to generate unexpected HID reports. However, in theory, the unsolicited HID Input reports can be issued by a specially crafted malicious USB device masquerading as FT260 when the attacker has physical access to the USB port. In this case, the read_buf pointer points to the final data portion of the previous I2C Read transfer, and the memcpy invoked in the ft260_raw_event() will try copying the content of the unexpected report into the wrong location.
This commit sets the Read buffer pointer to NULL on the I2C Read transaction completion and checks it in the ft260_raw_event() to detect and skip the unsolicited Input report.
Stable-dep-of: bf3e39df3a39 ("HID: ft260: fix stack-use-after-return write in I2C read race")
--- drivers/hid/hid-ft260.c | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-)
drivers/hid/hid-ft260.c +27 -14
@@ -471,17 +471,13 @@ static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data,struct ft260_i2c_read_request_report rep;struct hid_device *hdev = dev->hdev;int timeout; -int ret; +int ret =0 ;if (len >FT260_RD_DATA_MAX ) {hid_err (hdev,"%s: unsupported rd len: %d\n" , __func__, len);return -EINVAL ; } - dev->read_idx =0 ; - dev->read_buf = data; - dev->read_len = len; - rep.report =FT260_I2C_READ_REQ ; rep.length =cpu_to_le16 (len); rep.address = addr;
@@ -492,25 +488,36 @@ static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data,reinit_completion (&dev->wait); + dev->read_idx =0 ; + dev->read_buf = data; + dev->read_len = len; + ret =ft260_hid_output_report (hdev, (u8 *)&rep,sizeof (rep));if (ret <0 ) {hid_err (hdev,"%s: failed to start transaction, ret %d\n" , __func__, ret); -return ret; + goto ft260_i2c_read_exit; } timeout =msecs_to_jiffies (5000 );if (!wait_for_completion_timeout (&dev->wait, timeout)) { + ret = -ETIMEDOUT ;ft260_i2c_reset (hdev); -return -ETIMEDOUT ; + goto ft260_i2c_read_exit; } + dev->read_buf =NULL ; + ret =ft260_xfer_status (dev); -if (ret ==0 ) -return 0 ; +if (ret <0 ) { + ret = -EIO ; +ft260_i2c_reset (hdev); + goto ft260_i2c_read_exit; + } -ft260_i2c_reset (hdev); -return -EIO ; +ft260_i2c_read_exit: + dev->read_buf =NULL ; +return ret; }/*
@@ -1033,6 +1040,13 @@ static int ft260_raw_event(struct hid_device *hdev, struct hid_report *report,ft260_dbg ("i2c resp: rep %#02x len %d\n" , xfer->report, xfer->length); +if ((dev->read_buf ==NULL ) || + (xfer->length > dev->read_len - dev->read_idx)) { +hid_err (hdev,"unexpected report %#02x, length %d\n" , + xfer->report, xfer->length); +return -1 ; + } +memcpy (&dev->read_buf[dev->read_idx], &xfer->data, xfer->length); dev->read_idx += xfer->length;
@@ -1041,10 +1055,9 @@ static int ft260_raw_event(struct hid_device *hdev, struct hid_report *report,complete (&dev->wait); }else { -hid_err (hdev,"unknown report: %#02x\n" , xfer->report); -return 0 ; +hid_err (hdev,"unhandled report %#02x\n" , xfer->report); } -return 1 ; +return 0 ; }static struct hid_driver ft260_driver = {
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 4/7 HID: ft260: wake up device from power saving mode
In reply to Sasha LevinFrom: Michael Zaidman <[email protected]>
[ Upstream commit 4b3da6853a619a952e8caf2e8393264dd42ffa27 ]
The FT260 can enter a power saving mode after being idle for longer than 5 seconds.
When being woken up from power saving mode by an I2C write request, a possible NACK is not correctly reported by the controller. As a workaround, the driver will issue an I2C status report two times in ft260_xfer_status() after the chip has been idle for more than 5s.
Stable-dep-of: bf3e39df3a39 ("HID: ft260: fix stack-use-after-return write in I2C read race")
--- drivers/hid/hid-ft260.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
drivers/hid/hid-ft260.c +17 -0
@@ -31,6 +31,8 @@ MODULE_PARM_DESC(debug, "Toggle FT260 debugging messages");#define FT260_REPORT_MAX_LENGTH (64)#define FT260_I2C_DATA_REPORT_ID (len) (FT260_I2C_REPORT_MIN + (len - 1) / 4) +#define FT260_WAKEUP_NEEDED_AFTER_MS (4800)/* 5s minus 200ms margin */ +/* * The ft260 input report format defines 62 bytes for the data payload, but * when requested 62 bytes, the controller returns 60 and 2 in separate input
@@ -237,6 +239,7 @@ struct ft260_device {struct completion wait;struct mutex lock;u8 write_buf[FT260_REPORT_MAX_LENGTH ]; +unsigned long need_wakeup_at;u8 *read_buf;u16 read_idx;u16 read_len;
@@ -306,6 +309,20 @@ static int ft260_xfer_status(struct ft260_device *dev)struct ft260_get_i2c_status_report report;int ret; +if (time_is_before_jiffies (dev->need_wakeup_at)) { + ret =ft260_hid_feature_report_get (hdev,FT260_I2C_STATUS , + (u8 *)&report,sizeof (report)); +if (unlikely (ret <0 )) { +hid_err (hdev,"failed to retrieve status: %d, no wakeup\n" , + ret); + }else { + dev->need_wakeup_at = jiffies + +msecs_to_jiffies (FT260_WAKEUP_NEEDED_AFTER_MS ); +ft260_dbg ("bus_status %#02x, wakeup\n" , + report.bus_status); + } + } + ret =ft260_hid_feature_report_get (hdev,FT260_I2C_STATUS , (u8 *)&report,sizeof (report));if (unlikely (ret <0 )) {
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 5/7 HID: ft260: missed NACK from busy device
In reply to Sasha LevinFrom: Michael Zaidman <[email protected]>
[ Upstream commit 5afac727defa0b2a3dffb2abd5fb5f594b98d217 ]
When writing into a slow device like an EEPROM chip, the controller may exit the busy state before the device releases the bus. In this case, the ft260_xfer_status returns success before the data transfer completion.
The patch fixes it by returning from the ft260_xfer_status() with the "-EAGAIN" on both controller and bus busy status when appropriate.
It does not apply to the i2c combined transactions when after the write IO, the controller keeps the bus busy until the read IO and then between reading IOs to ensure an atomic operation.
Stable-dep-of: bf3e39df3a39 ("HID: ft260: fix stack-use-after-return write in I2C read race")
--- drivers/hid/hid-ft260.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-)
drivers/hid/hid-ft260.c +22 -5
@@ -303,7 +303,7 @@ static int ft260_i2c_reset(struct hid_device *hdev)return ret; } -static int ft260_xfer_status (struct ft260_device *dev) +static int ft260_xfer_status (struct ft260_device *dev,u8 bus_busy) {struct hid_device *hdev = dev->hdev;struct ft260_get_i2c_status_report report;
@@ -334,7 +334,7 @@ static int ft260_xfer_status(struct ft260_device *dev)ft260_dbg ("bus_status %#02x, clock %u\n" , report.bus_status, dev->clock); -if (report.bus_status &FT260_I2C_STATUS_CTRL_BUSY ) +if (report.bus_status & (FT260_I2C_STATUS_CTRL_BUSY | bus_busy))return -EAGAIN ;if (report.bus_status &FT260_I2C_STATUS_BUS_BUSY )
@@ -379,8 +379,11 @@ static int ft260_hid_output_report(struct hid_device *hdev, u8 *data,static int ft260_hid_output_report_check_status (struct ft260_device *dev,u8 *data,int len) { +u8 bus_busy;int ret, usec, try =100 ;struct hid_device *hdev = dev->hdev; +struct ft260_i2c_write_request_report *rep = + (struct ft260_i2c_write_request_report *)data; ret =ft260_hid_output_report (hdev, data, len);if (ret <0 ) {
@@ -398,8 +401,18 @@ static int ft260_hid_output_report_check_status(struct ft260_device *dev,ft260_dbg ("wait %d usec, len %d\n" , usec, len); } +/* +* Do not check the busy bit for combined transactions +* since the controller keeps the bus busy between writing +* and reading IOs to ensure an atomic operation. +*/ +if (rep->flag ==FT260_FLAG_START ) + bus_busy =0 ; +else + bus_busy =FT260_I2C_STATUS_BUS_BUSY ; +do { - ret =ft260_xfer_status (dev); + ret =ft260_xfer_status (dev, bus_busy);if (ret != -EAGAIN )break ; }while (--try);
@@ -487,6 +500,7 @@ static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data, {struct ft260_i2c_read_request_report rep;struct hid_device *hdev = dev->hdev; +u8 bus_busy =0 ;int timeout;int ret =0 ;
@@ -525,7 +539,10 @@ static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data, dev->read_buf =NULL ; - ret =ft260_xfer_status (dev); +if (flag &FT260_FLAG_STOP ) + bus_busy =FT260_I2C_STATUS_BUS_BUSY ; + + ret =ft260_xfer_status (dev, bus_busy);if (ret <0 ) { ret = -EIO ;ft260_i2c_reset (hdev);
@@ -1004,7 +1021,7 @@ static int ft260_probe(struct hid_device *hdev, const struct hid_device_id *id)mutex_init (&dev->lock);init_completion (&dev->wait); - ret =ft260_xfer_status (dev); + ret =ft260_xfer_status (dev,FT260_I2C_STATUS_BUS_BUSY );if (ret)ft260_i2c_reset (hdev);
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 6/7 HID: ft260: validate i2c input report length
In reply to Sasha LevinFrom: Michael Zaidman <[email protected]>
[ Upstream commit 80c4bbb2b38513e9c3d84805fa61a0ee16d79c45 ]
Add two checks to ft260_raw_event() to prevent out-of-bounds reads from malicious or malfunctioning devices:
First, reject reports shorter than the 2-byte header (report ID + length fields). Without this, even accessing xfer->length on a 1-byte report is an OOB read.
Second, validate xfer->length against the actual data capacity of the received HID report. Each I2C data report ID (0xD0 through 0xDE) defines a different report size in the HID descriptor, so the available payload varies per report. A corrupted length field could cause memcpy to read beyond the report buffer.
Stable-dep-of: bf3e39df3a39 ("HID: ft260: fix stack-use-after-return write in I2C read race")
--- drivers/hid/hid-ft260.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
drivers/hid/hid-ft260.c +14 -2
@@ -1069,10 +1069,22 @@ static int ft260_raw_event(struct hid_device *hdev, struct hid_report *report,struct ft260_device *dev =hid_get_drvdata (hdev);struct ft260_i2c_input_report *xfer = (void *)data; +if (size < offsetof(struct ft260_i2c_input_report , data)) { +hid_err (hdev,"short report %d\n" , size); +return -1 ; + } +if (xfer->report >=FT260_I2C_REPORT_MIN && xfer->report <=FT260_I2C_REPORT_MAX ) { -ft260_dbg ("i2c resp: rep %#02x len %d\n" , xfer->report, - xfer->length); +ft260_dbg ("i2c resp: rep %#02x len %d size %d\n" , + xfer->report, xfer->length, size); + +if (xfer->length > size - + offsetof(struct ft260_i2c_input_report , data)) { +hid_err (hdev,"report %#02x: length %d exceeds HID report size\n" , + xfer->report, xfer->length); +return -1 ; + }if ((dev->read_buf ==NULL ) || (xfer->length > dev->read_len - dev->read_idx)) {
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 7/7 HID: ft260: fix stack-use-after-return write in I2C read race
In reply to Sasha LevinFrom: Raman Varabets <[email protected]>
[ Upstream commit bf3e39df3a397fd82967a31d17c4e02c7feab221 ]
ft260_i2c_read() points dev->read_buf at a caller-supplied buffer (often an on-stack variable), arms a completion and waits up to five seconds for the device to return the data. The HID input callback ft260_raw_event() runs in the input/IRQ path, independent of the dev->lock mutex held by the read path, and copies the device-supplied payload into dev->read_buf after a plain NULL check.
These two paths share read_buf, read_idx and read_len with no serialization. If the device delays its response until the read times out, ft260_i2c_read() resets the controller, clears read_buf and returns, unwinding the stack frame the buffer lived in. A response that arrives at that moment lets ft260_raw_event() pass the NULL check and then memcpy() the device-controlled payload into the now-freed stack location, a bounded but attacker-influenced stack-use-after-return write triggerable by malicious or malfunctioning hardware.
Add a dedicated spinlock that serializes every access to read_buf, read_idx and read_len. ft260_raw_event() now holds it across the NULL check, the memcpy and the index update, while the read path takes it when arming and when clearing the buffer, so the teardown can no longer slip between the check and the copy.
--- drivers/hid/hid-ft260.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-)
drivers/hid/hid-ft260.c +24 -1
@@ -240,6 +240,8 @@ struct ft260_device {struct mutex lock;u8 write_buf[FT260_REPORT_MAX_LENGTH ];unsigned long need_wakeup_at; +/* Protects read_buf, read_idx and read_len against ft260_raw_event() */ +spinlock_t read_lock;u8 *read_buf;u16 read_idx;u16 read_len;
@@ -500,6 +502,7 @@ static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data, {struct ft260_i2c_read_request_report rep;struct hid_device *hdev = dev->hdev; +unsigned long irqflags;u8 bus_busy =0 ;int timeout;int ret =0 ;
@@ -519,9 +522,11 @@ static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data,reinit_completion (&dev->wait); +spin_lock_irqsave (&dev->read_lock, irqflags); dev->read_idx =0 ; dev->read_buf = data; dev->read_len = len; +spin_unlock_irqrestore (&dev->read_lock, irqflags); ret =ft260_hid_output_report (hdev, (u8 *)&rep,sizeof (rep));if (ret <0 ) {
@@ -537,7 +542,9 @@ static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data, goto ft260_i2c_read_exit; } +spin_lock_irqsave (&dev->read_lock, irqflags); dev->read_buf =NULL ; +spin_unlock_irqrestore (&dev->read_lock, irqflags);if (flag &FT260_FLAG_STOP ) bus_busy =FT260_I2C_STATUS_BUS_BUSY ;
@@ -550,7 +557,9 @@ static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data, } ft260_i2c_read_exit: +spin_lock_irqsave (&dev->read_lock, irqflags); dev->read_buf =NULL ; +spin_unlock_irqrestore (&dev->read_lock, irqflags);return ret; }
@@ -1019,6 +1028,7 @@ static int ft260_probe(struct hid_device *hdev, const struct hid_device_id *id) ((struct hidraw *)hdev->hidraw)->minor);mutex_init (&dev->lock); +spin_lock_init (&dev->read_lock);init_completion (&dev->wait); ret =ft260_xfer_status (dev,FT260_I2C_STATUS_BUS_BUSY );
@@ -1068,6 +1078,7 @@ static int ft260_raw_event(struct hid_device *hdev, struct hid_report *report, {struct ft260_device *dev =hid_get_drvdata (hdev);struct ft260_i2c_input_report *xfer = (void *)data; +unsigned long irqflags;if (size < offsetof(struct ft260_i2c_input_report , data)) {hid_err (hdev,"short report %d\n" , size);
@@ -1076,6 +1087,8 @@ static int ft260_raw_event(struct hid_device *hdev, struct hid_report *report,if (xfer->report >=FT260_I2C_REPORT_MIN && xfer->report <=FT260_I2C_REPORT_MAX ) { +bool complete_read; +ft260_dbg ("i2c resp: rep %#02x len %d size %d\n" , xfer->report, xfer->length, size);
@@ -1086,8 +1099,15 @@ static int ft260_raw_event(struct hid_device *hdev, struct hid_report *report,return -1 ; } +/* +* Hold read_lock so a timed-out ft260_i2c_read() cannot +* clear read_buf between the NULL check and the memcpy. +*/ +spin_lock_irqsave (&dev->read_lock, irqflags); +if ((dev->read_buf ==NULL ) || (xfer->length > dev->read_len - dev->read_idx)) { +spin_unlock_irqrestore (&dev->read_lock, irqflags);hid_err (hdev,"unexpected report %#02x, length %d\n" , xfer->report, xfer->length);return -1 ;
@@ -1096,8 +1116,11 @@ static int ft260_raw_event(struct hid_device *hdev, struct hid_report *report,memcpy (&dev->read_buf[dev->read_idx], &xfer->data, xfer->length); dev->read_idx += xfer->length; + complete_read = dev->read_idx == dev->read_len; + +spin_unlock_irqrestore (&dev->read_lock, irqflags); -if (dev->read_idx == dev->read_len) +if (complete_read)complete (&dev->wait); }else {
Signature · 1 line
2.53.0