Skip to contentSkip to conversation

media: az6007: Fix out-of-bounds access and mutex reinitialization

RFC PATCH syzbot 2 participants 2 messages Oct 19 – Oct 30

Subsystems (2)
lore
Share & tools
lore

syzbot

author syzbot
#0 lore raw

In az6007_i2c_xfer(), user-controlled I2C messages transferred via /dev/i2c-* (through i2c-dev) can specify lengths up to 8192 bytes. The driver uses a fixed 4096-byte bounce buffer, st->data, defined in struct az6007_device_state. The driver does not validate message lengths against sizeof(st->data) before copying data or issuing USB control transfers, leading to out-of-bounds buffer accesses.

When userspace reads from an I2C device (such as via preadv() on /dev/i2c-*), i2cdev_read() calls i2c_master_recv() -> i2c_transfer() -> __i2c_transfer() -> az6007_i2c_xfer() with msgs[0].len matching the requested read size. In the single read branch, length is computed as msgs[i].len + 6. If msgs[i].len exceeds 4090 bytes, length exceeds sizeof(st->data). Furthermore, the return value of __az6007_read() was not validated before copying bytes from st->data[j + 5] into msgs[i].buf[j]. When a large read request fails at the USB level, the copy loop still executes unconditionally. When j reaches 4091, j + 5 equals 4096, reading past the end of st->data and triggering a UBSAN array-index-out-of-bounds warning:

UBSAN: array-index-out-of-bounds in
drivers/media/usb/dvb-usb-v2/az6007.c:821:22
index 4096 is out of range for type 'unsigned char[4096]'
Call Trace:
 <TASK>
 dump_stack_lvl+0xe8/0x150 lib/dump_stack.c:120
 ubsan_epilogue+0xa/0x30 lib/ubsan.c:233
 __ubsan_handle_out_of_bounds+0xe8/0xf0 lib/ubsan.c:455
 az6007_i2c_xfer+0x419/0xaa0 drivers/media/usb/dvb-usb-v2/az6007.c:821
 __i2c_transfer+0x7bf/0x1fe0 drivers/i2c/i2c-core-base.c:-1
 i2c_transfer+0x1cb/0x2d0 drivers/i2c/i2c-core-base.c:2350
 i2c_transfer_buffer_flags+0x10d/0x1a0 drivers/i2c/i2c-core-base.c:2378
 i2c_master_recv include/linux/i2c.h:79 [inline]
 i2cdev_read+0x10d/0x250 drivers/i2c/i2c-dev.c:155
 vfs_readv+0x57c/0x850 fs/read_write.c:1021
 do_preadv fs/read_write.c:1133 [inline]
 </TASK>

Similar buffer overflow issues exist in the other transfer paths: the combined write+read branch lacked checks on msgs[i + 1].len < 1 and length

Quoted 1 line

sizeof(st->data), while the write branch copied msgs[i].len - 1 bytes

into st->data without verifying that the payload fit within st->data. Additionally, in __az6007_write(), the blen > 64 check was performed after dumping the payload to the kernel log under az6007_xfer_debug.

Separately, mutex_init(&state->mutex) was called inside az6007_power_ctrl() when !state->warm. If the initial power-on command failed, state->warm remained false, causing subsequent invocations of az6007_power_ctrl() to reinitialize state->mutex while the device was active. If another thread held the mutex during az6007_i2c_xfer(), reinitializing the mutex cleared the lock owner and triggered a DEBUG_LOCKS_WARN_ON warning on unlock.

Fix these issues by:
- Moving mutex initialization out of az6007_power_ctrl() into a newly introduced probe callback (az6007_probe) that initializes state->mutex once upon device probing.
- Adding upper-bound checks against sizeof(st->data) in the single read, single write, and combined write+read paths of az6007_i2c_xfer(), as well as verifying msgs[i + 1].len >= 1 in the combined path.
- Checking that __az6007_read() succeeded before copying data from st->data to msgs[i].buf in the single read path.
- Moving the blen > 64 check in __az6007_write() before the debug hex dump.

Fixes b19280cf2bee ("[media] az6007: code cleanups and fixes")
Assisted-by Gemini:gemini-4-argon Gemini:gemini-3.8-flash syzbot
Reported-by [email protected]

To: <[email protected]>
To: "Mauro Carvalho Chehab" <[email protected]>
To: "Mauro Carvalho Chehab" <[email protected]>
Cc: <[email protected]>

---

drivers/media/usb/dvb-usb-v2/az6007.c +40 -11
index 65ef045b7..20b2bd944 100644
@@ -143,6 +143,12 @@ static int __az6007_write(struct usb_device *udev, u8 req, u16 value,
 {
 	int ret;
 
+	if (blen > 64) {
+		pr_err("az6007: tried to write %d bytes, but I2C max size is 64 bytes\n",
+		       blen);
+		return -EOPNOTSUPP;
+	}
+
 	if (az6007_xfer_debug) {
 		printk(KERN_DEBUG "az6007: OUT req: %02x, value: %04x, index: %04x\n",
 		       req, value, index);
@@ -150,12 +156,6 @@ static int __az6007_write(struct usb_device *udev, u8 req, u16 value,
 				     DUMP_PREFIX_NONE, b, blen);
 	}
 
-	if (blen > 64) {
-		pr_err("az6007: tried to write %d bytes, but I2C max size is 64 bytes\n",
-		       blen);
-		return -EOPNOTSUPP;
-	}
-
 	ret = usb_control_msg(udev,
 			      usb_sndctrlpipe(udev, 0),
 			      req,
@@ -694,8 +694,6 @@ static int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
 	pr_debug("%s()\n", __func__);
 
 	if (!state->warm) {
-		mutex_init(&state->mutex);
-
 		ret = az6007_write(d, AZ6007_POWER, 0, 2, NULL, 0);
 		if (ret < 0)
 			return ret;
@@ -770,11 +768,19 @@ static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
 			if (az6007_xfer_debug)
 				printk(KERN_DEBUG "az6007: I2C W/R addr=0x%x len=%d/%d\n",
 				       addr, msgs[i].len, msgs[i + 1].len);
+			if (msgs[i + 1].len < 1) {
+				ret = -EIO;
+				goto err;
+			}
 			req = AZ6007_I2C_RD;
 			index = msgs[i].buf[0];
 			value = addr | (1 << 8);
 			length = 6 + msgs[i + 1].len;
 			len = msgs[i + 1].len;
+			if (length > sizeof(st->data)) {
+				ret = -EOPNOTSUPP;
+				goto err;
+			}
 			ret = __az6007_read(d->udev, req, value, index,
 					    st->data, length);
 			if (ret >= len) {
@@ -797,6 +803,10 @@ static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
 			value = addr | (1 << 8);
 			length = msgs[i].len - 1;
 			len = msgs[i].len - 1;
+			if (length > sizeof(st->data)) {
+				ret = -EOPNOTSUPP;
+				goto err;
+			}
 			for (j = 0; j < len; j++)
 				st->data[j] = msgs[i].buf[j + 1];
 			ret =  __az6007_write(d->udev, req, value, index,
@@ -815,10 +825,18 @@ static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
 			value = addr;
 			length = msgs[i].len + 6;
 			len = msgs[i].len;
+			if (length > sizeof(st->data)) {
+				ret = -EOPNOTSUPP;
+				goto err;
+			}
 			ret = __az6007_read(d->udev, req, value, index,
 					    st->data, length);
-			for (j = 0; j < len; j++)
-				msgs[i].buf[j] = st->data[j + 5];
+			if (ret >= len) {
+				for (j = 0; j < len; j++)
+					msgs[i].buf[j] = st->data[j + 5];
+			} else {
+				ret = -EIO;
+			}
 		}
 		if (ret < 0)
 			goto err;
@@ -889,6 +907,15 @@ static int az6007_download_firmware(struct dvb_usb_device *d,
 	return cypress_load_firmware(d->udev, fw, CYPRESS_FX2);
 }
 
+static int az6007_probe(struct dvb_usb_device *d)
+{
+	struct az6007_device_state *state = d_to_priv(d);
+
+	mutex_init(&state->mutex);
+
+	return 0;
+}
+
 /* DVB USB Driver stuff */
 static struct dvb_usb_device_properties az6007_props = {
 	.driver_name         = KBUILD_MODNAME,
@@ -897,6 +924,7 @@ static struct dvb_usb_device_properties az6007_props = {
 
 	.adapter_nr          = adapter_nr,
 	.size_of_priv        = sizeof(struct az6007_device_state),
+	.probe               = az6007_probe,
 	.i2c_algo            = &az6007_i2c_algo,
 	.tuner_attach        = az6007_tuner_attach,
 	.frontend_attach     = az6007_frontend_attach,
@@ -919,11 +947,12 @@ static struct dvb_usb_device_properties az6007_cablestar_hdci_props = {
 
 	.adapter_nr          = adapter_nr,
 	.size_of_priv        = sizeof(struct az6007_device_state),
+	.probe               = az6007_probe,
 	.i2c_algo            = &az6007_i2c_algo,
 	.tuner_attach        = az6007_tuner_attach,
 	.frontend_attach     = az6007_cablestar_hdci_frontend_attach,
 	.streaming_ctrl      = az6007_streaming_ctrl,
-/* ditch get_rc_config as it can't work (TS35 remote, I believe it's rc5) */
+	/* ditch get_rc_config as it can't work (TS35 remote, I believe it's rc5) */
 	.get_rc_config       = NULL,
 	.read_mac_address    = az6007_read_mac_addr,
 	.download_firmware   = az6007_download_firmware,

base-commit: c03c1a14507dfaf8b856c2608f9ac216021fc222

Signature · 9 lines
This is an AI-generated patch subject to moderation.
Reply with '#syz upstream' to Sign-off the patch as a human author
and send it to the upstream kernel mailing lists.
Reply with '#syz reject' to reject it ('#syz unreject' to undo).

See https://goo.gle/syzbot-ai-patches for information about AI-generated patches.
You can comment on the patch as usual, syzbot will try to address
the comments and send a new version of the patch if necessary.
syzbot engineers can be reached at [email protected].
#1 lore raw

The v1 patch puts the buffer bounds in the right place and initializes the mutex once in probe. Both read paths can still accept a short USB reply and copy stale bytes.

Required changes:
- In both read paths, check that the USB reply contains the five skipped bytes plus all bytes copied to the I2C message. Return -EIO if it is too short, or document and enforce a different exact reply length required by the device. The current ret >= len check can report success and copy old data.
- Reject write payloads above the 64-byte USB limit in az6007_i2c_xfer() before copying them to st->data. Keep the __az6007_write() limit before its debug dump. Keep the six-byte read bounds and probe initialization for both device variants.
- Correct the description. The selected report shows damage to the mutex wait list before the I2C transfer starts. Other testing showed a separate read past the buffer. A failed-power mutex reset was observed on the patch's base commit, but the selected report does not show that exact sequence.
- Fix the final commit metadata: keep the Fixes: tag for b19280cf2bee, which introduced the buffer bug. Also cover a2c35d346d9e, which introduced the mutex; use a second Fixes: tag if appropriate, or explain why one is enough. Remove the old duplicate [email protected] address, and keep the current media maintainer and lists. Update the generated tags and recipients at submission.

Syzbot flow request:
Rerun the original reproducer on the historical kernel. Compare unpatched and patched kernels built from the same base commit. Check short USB replies, I2C transfer sizes, a failed-power command with an I2C waiter, and both device variants. State any hardware testing limit.

Context:
On the unpatched kernel at the patch's base commit, two guests showed the driver initializing the mutex again after a failed power command while an I2C transfer waited on it. Initializing the mutex once in probe removes that reset path. The first write context details that damaged the historical list are not confirmed.

Impact assessment:
The oversized read can go past the buffer; v1 blocks it. The short-reply bug
can still return stale bytes from the state buffer to an I2C reader.. The selected warning shows a damaged mutex wait list, but its exact trigger is still unproven.

Kind Regards / Mit freundlichen Grüßen
BART CHRONOWSKI

Quoted 220 lines · syzbot' via syzkaller-upstream-moderation

In az6007_i2c_xfer(), user-controlled I2C messages transferred via /dev/i2c-* (through i2c-dev) can specify lengths up to 8192 bytes. The driver uses a fixed 4096-byte bounce buffer, st->data, defined in struct az6007_device_state. The driver does not validate message lengths against sizeof(st->data) before copying data or issuing USB control transfers, leading to out-of-bounds buffer accesses.

When userspace reads from an I2C device (such as via preadv() on /dev/i2c-*), i2cdev_read() calls i2c_master_recv() -> i2c_transfer() -> __i2c_transfer() -> az6007_i2c_xfer() with msgs[0].len matching the requested read size. In the single read branch, length is computed as msgs[i].len + 6. If msgs[i].len exceeds 4090 bytes, length exceeds sizeof(st->data). Furthermore, the return value of __az6007_read() was not validated before copying bytes from st->data[j + 5] into msgs[i].buf[j]. When a large read request fails at the USB level, the copy loop still executes unconditionally. When j reaches 4091, j + 5 equals 4096, reading past the end of st->data and triggering a UBSAN array-index-out-of-bounds warning:

UBSAN: array-index-out-of-bounds in
drivers/media/usb/dvb-usb-v2/az6007.c:821:22
index 4096 is out of range for type 'unsigned char[4096]'
Call Trace:
 <TASK>
 dump_stack_lvl+0xe8/0x150 lib/dump_stack.c:120
 ubsan_epilogue+0xa/0x30 lib/ubsan.c:233
 __ubsan_handle_out_of_bounds+0xe8/0xf0 lib/ubsan.c:455
 az6007_i2c_xfer+0x419/0xaa0 drivers/media/usb/dvb-usb-v2/az6007.c:821
 __i2c_transfer+0x7bf/0x1fe0 drivers/i2c/i2c-core-base.c:-1
 i2c_transfer+0x1cb/0x2d0 drivers/i2c/i2c-core-base.c:2350
 i2c_transfer_buffer_flags+0x10d/0x1a0 drivers/i2c/i2c-core-base.c:2378
 i2c_master_recv include/linux/i2c.h:79 [inline]
 i2cdev_read+0x10d/0x250 drivers/i2c/i2c-dev.c:155
 vfs_readv+0x57c/0x850 fs/read_write.c:1021
 do_preadv fs/read_write.c:1133 [inline]
 </TASK>

Similar buffer overflow issues exist in the other transfer paths: the combined write+read branch lacked checks on msgs[i + 1].len < 1 and length

Quoted 1 line

sizeof(st->data), while the write branch copied msgs[i].len - 1 bytes

into st->data without verifying that the payload fit within st->data. Additionally, in __az6007_write(), the blen > 64 check was performed after dumping the payload to the kernel log under az6007_xfer_debug.

Separately, mutex_init(&state->mutex) was called inside az6007_power_ctrl() when !state->warm. If the initial power-on command failed, state->warm remained false, causing subsequent invocations of az6007_power_ctrl() to reinitialize state->mutex while the device was active. If another thread held the mutex during az6007_i2c_xfer(), reinitializing the mutex cleared the lock owner and triggered a DEBUG_LOCKS_WARN_ON warning on unlock.

Fix these issues by:
- Moving mutex initialization out of az6007_power_ctrl() into a newly introduced probe callback (az6007_probe) that initializes state->mutex once upon device probing.
- Adding upper-bound checks against sizeof(st->data) in the single read, single write, and combined write+read paths of az6007_i2c_xfer(), as well as verifying msgs[i + 1].len >= 1 in the combined path.
- Checking that __az6007_read() succeeded before copying data from st->data to msgs[i].buf in the single read path.
- Moving the blen > 64 check in __az6007_write() before the debug hex dump.

Fixes b19280cf2bee ("[media] az6007: code cleanups and fixes")
Assisted-by Gemini:gemini-4-argon Gemini:gemini-3.8-flash syzbot
Reported-by [email protected]

To: <[email protected]>
To: "Mauro Carvalho Chehab" <[email protected]>
To: "Mauro Carvalho Chehab" <[email protected]>
Cc: <[email protected]>

---

drivers/media/usb/dvb-usb-v2/az6007.c +40 -11
index 65ef045b7..20b2bd944 100644
@@ -143,6 +143,12 @@ static int __az6007_write(struct usb_device *udev, u8 req, u16 value,
 {
 	int ret;
 
+	if (blen > 64) {
+		pr_err("az6007: tried to write %d bytes, but I2C max size is 64 bytes\n",
+		       blen);
+		return -EOPNOTSUPP;
+	}
+
 	if (az6007_xfer_debug) {
 		printk(KERN_DEBUG "az6007: OUT req: %02x, value: %04x, index: %04x\n",
 		       req, value, index);
@@ -150,12 +156,6 @@ static int __az6007_write(struct usb_device *udev, u8 req, u16 value,
 				     DUMP_PREFIX_NONE, b, blen);
 	}
 
-	if (blen > 64) {
-		pr_err("az6007: tried to write %d bytes, but I2C max size is 64 bytes\n",
-		       blen);
-		return -EOPNOTSUPP;
-	}
-
 	ret = usb_control_msg(udev,
 			      usb_sndctrlpipe(udev, 0),
 			      req,
@@ -694,8 +694,6 @@ static int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
 	pr_debug("%s()\n", __func__);
 
 	if (!state->warm) {
-		mutex_init(&state->mutex);
-
 		ret = az6007_write(d, AZ6007_POWER, 0, 2, NULL, 0);
 		if (ret < 0)
 			return ret;
@@ -770,11 +768,19 @@ static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
 			if (az6007_xfer_debug)
 				printk(KERN_DEBUG "az6007: I2C W/R addr=0x%x len=%d/%d\n",
 				       addr, msgs[i].len, msgs[i + 1].len);
+			if (msgs[i + 1].len < 1) {
+				ret = -EIO;
+				goto err;
+			}
 			req = AZ6007_I2C_RD;
 			index = msgs[i].buf[0];
 			value = addr | (1 << 8);
 			length = 6 + msgs[i + 1].len;
 			len = msgs[i + 1].len;
+			if (length > sizeof(st->data)) {
+				ret = -EOPNOTSUPP;
+				goto err;
+			}
 			ret = __az6007_read(d->udev, req, value, index,
 					    st->data, length);
 			if (ret >= len) {
@@ -797,6 +803,10 @@ static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
 			value = addr | (1 << 8);
 			length = msgs[i].len - 1;
 			len = msgs[i].len - 1;
+			if (length > sizeof(st->data)) {
+				ret = -EOPNOTSUPP;
+				goto err;
+			}
 			for (j = 0; j < len; j++)
 				st->data[j] = msgs[i].buf[j + 1];
 			ret =  __az6007_write(d->udev, req, value, index,
@@ -815,10 +825,18 @@ static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
 			value = addr;
 			length = msgs[i].len + 6;
 			len = msgs[i].len;
+			if (length > sizeof(st->data)) {
+				ret = -EOPNOTSUPP;
+				goto err;
+			}
 			ret = __az6007_read(d->udev, req, value, index,
 					    st->data, length);
-			for (j = 0; j < len; j++)
-				msgs[i].buf[j] = st->data[j + 5];
+			if (ret >= len) {
+				for (j = 0; j < len; j++)
+					msgs[i].buf[j] = st->data[j + 5];
+			} else {
+				ret = -EIO;
+			}
 		}
 		if (ret < 0)
 			goto err;
@@ -889,6 +907,15 @@ static int az6007_download_firmware(struct dvb_usb_device *d,
 	return cypress_load_firmware(d->udev, fw, CYPRESS_FX2);
 }
 
+static int az6007_probe(struct dvb_usb_device *d)
+{
+	struct az6007_device_state *state = d_to_priv(d);
+
+	mutex_init(&state->mutex);
+
+	return 0;
+}
+
 /* DVB USB Driver stuff */
 static struct dvb_usb_device_properties az6007_props = {
 	.driver_name         = KBUILD_MODNAME,
@@ -897,6 +924,7 @@ static struct dvb_usb_device_properties az6007_props = {
 
 	.adapter_nr          = adapter_nr,
 	.size_of_priv        = sizeof(struct az6007_device_state),
+	.probe               = az6007_probe,
 	.i2c_algo            = &az6007_i2c_algo,
 	.tuner_attach        = az6007_tuner_attach,
 	.frontend_attach     = az6007_frontend_attach,
@@ -919,11 +947,12 @@ static struct dvb_usb_device_properties az6007_cablestar_hdci_props = {
 
 	.adapter_nr          = adapter_nr,
 	.size_of_priv        = sizeof(struct az6007_device_state),
+	.probe               = az6007_probe,
 	.i2c_algo            = &az6007_i2c_algo,
 	.tuner_attach        = az6007_tuner_attach,
 	.frontend_attach     = az6007_cablestar_hdci_frontend_attach,
 	.streaming_ctrl      = az6007_streaming_ctrl,
-/* ditch get_rc_config as it can't work (TS35 remote, I believe it's rc5) */
+	/* ditch get_rc_config as it can't work (TS35 remote, I believe it's rc5) */
 	.get_rc_config       = NULL,
 	.read_mac_address    = az6007_read_mac_addr,
 	.download_firmware   = az6007_download_firmware,

base-commit: c03c1a14507dfaf8b856c2608f9ac216021fc222

Signature · 14 lines
This is an AI-generated patch subject to moderation.
Reply with '#syz upstream' to Sign-off the patch as a human author
and send it to the upstream kernel mailing lists.
Reply with '#syz reject' to reject it ('#syz unreject' to undo).

See https://goo.gle/syzbot-ai-patches for information about AI-generated patches.
You can comment on the patch as usual, syzbot will try to address
the comments and send a new version of the patch if necessary.
syzbot engineers can be reached at [email protected].

-- 
You received this message because you are subscribed to the Google Groups "syzkaller-upstream-moderation" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/syzkaller-upstream-moderation/98467e25-6e27-41d8-94d9-5367714ce9b0%40mail.kernel.org.