[PATCH] media: flexcop-i2c: validate the register address message length
From: Zou Jianqiang
Date: Sat Nov 14 2026 - 02:29:18 EST
From: Jianqiang Zou <jq.zou@xxxxxxxxxxx>
Both the write and combined-read paths in flexcop_master_xfer()
use the first byte of the current message as the register address.
A zero-length message has no such byte. A syzkaller run on Linux
7.3 reported a KASAN null-ptr-deref when the write path tried to
read it.
Check the current message length before selecting either path.
This protects both register address accesses while preserving the
existing single-message read probe and register-address-only writes.
Advertise I2C_AQ_NO_ZERO_LEN_WRITE on all three FlexCop adapters so
the I2C core rejects zero-length writes before calling the driver.
Guo Zihao previously proposed a check in the write path [1].
[1] https://lore.kernel.org/r/20260918032427.803874-1-guozh23@xxxxxxxxxxxx
Signed-off-by: Jianqiang Zou <jq.zou@xxxxxxxxxxx>
---
drivers/media/common/b2c2/flexcop-i2c.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/drivers/media/common/b2c2/flexcop-i2c.c b/drivers/media/common/b2c2/flexcop-i2c.c
index 21edf870d..3a13d73ff 100644
--- a/drivers/media/common/b2c2/flexcop-i2c.c
+++ b/drivers/media/common/b2c2/flexcop-i2c.c
@@ -181,6 +181,12 @@ static int flexcop_master_xfer(struct i2c_adapter *i2c_adap,
return -ERESTARTSYS;
for (i = 0; i < num; i++) {
+ /* Both branches use the first byte as the register address. */
+ if (msgs[i].len == 0) {
+ ret = -EINVAL;
+ break;
+ }
+
/* reading */
if (i+1 < num && (msgs[i+1].flags == I2C_M_RD)) {
ret = i2c->fc->i2c_request(i2c, FC_READ, msgs[i].addr,
@@ -209,6 +215,11 @@ static u32 flexcop_i2c_func(struct i2c_adapter *adapter)
return I2C_FUNC_I2C;
}
+/* A zero-length write message cannot be represented by this hardware. */
+static const struct i2c_adapter_quirks flexcop_i2c_quirks = {
+ .flags = I2C_AQ_NO_ZERO_LEN_WRITE,
+};
+
static const struct i2c_algorithm flexcop_algo = {
.master_xfer = flexcop_master_xfer,
.functionality = flexcop_i2c_func,
@@ -246,6 +257,9 @@ int flexcop_i2c_init(struct flexcop_device *fc)
fc->fc_i2c_adap[0].i2c_adap.dev.parent =
fc->fc_i2c_adap[1].i2c_adap.dev.parent =
fc->fc_i2c_adap[2].i2c_adap.dev.parent = fc->dev;
+ fc->fc_i2c_adap[0].i2c_adap.quirks =
+ fc->fc_i2c_adap[1].i2c_adap.quirks =
+ fc->fc_i2c_adap[2].i2c_adap.quirks = &flexcop_i2c_quirks;
ret = i2c_add_adapter(&fc->fc_i2c_adap[0].i2c_adap);
if (ret < 0)
--
2.43.0