linux/drivers/i2c
Weiming Shi 3051cd060f i2c: smbus: reject oversized block transfers in the common path
The SMBus block transfer length data->block[0] is validated in
i2c_smbus_xfer_emulated() but that check runs too late for tracepoints
and is skipped entirely when the adapter provides a native smbus_xfer
implementation. This allows user-controlled oversized block lengths to
reach tracepoint memcpy calls and driver callbacks unchecked.

Add an early validation in __i2c_smbus_xfer() that rejects block
transfers whose caller-supplied length is zero or exceeds
I2C_SMBUS_BLOCK_MAX before any tracepoint fires or driver callback
runs. data->block[0] is filled in by the device on SMBus block reads,
so the check is scoped to operations where the length is actually
supplied by the caller. This is consistent with the existing -EINVAL
convention in the emulated path and protects all downstream consumers
at once: the smbus_write tracepoint, all native smbus_xfer driver
implementations, and the emulated path.

Two distinct bugs are fixed by this change:

Bug 1: smbus_write tracepoint OOB (include/trace/events/smbus.h)
  trace_smbus_write() fires before any validation and copies
  data->block[0]+1 bytes into a 34-byte event buffer. With
  block[0]=0xfe the tracepoint copies 255 bytes, overflowing by 221.

 BUG: KASAN: stack-out-of-bounds in trace_event_raw_event_smbus_write+0x27c/0x530
 Read of size 255 at addr ffff88800d98fcf8 by task poc_smbus/91
 Call Trace:
  <TASK>
  __asan_memcpy+0x23/0x80
  trace_event_raw_event_smbus_write+0x27c/0x530
  __i2c_smbus_xfer+0x43a/0xa40
  i2c_smbus_xfer+0x19e/0x340
  i2cdev_ioctl_smbus+0x38f/0x7f0
  i2cdev_ioctl+0x35e/0x680
  __x64_sys_ioctl+0x147/0x1e0
  do_syscall_64+0xcf/0x15a0
  entry_SYSCALL_64_after_hwframe+0x76/0x7e
  </TASK>

Bug 2: i2c-stub I2C_SMBUS_I2C_BLOCK_DATA OOB (drivers/i2c/i2c-stub.c)
  stub_xfer() implements .smbus_xfer directly and only clamps
  block[0] against 256-command, not I2C_SMBUS_BLOCK_MAX. With
  block[0]=0xff and command=0 the loop accesses block[1+i] for
  i up to 254, far past the 34-byte union.

 UBSAN: array-index-out-of-bounds in drivers/i2c/i2c-stub.c:223:44
 index 34 is out of range for type '__u8 [34]'
 Call Trace:
  <TASK>
  __ubsan_handle_out_of_bounds+0xd7/0x120
  stub_xfer+0x1971/0x198f [i2c_stub]
  __i2c_smbus_xfer+0x306/0xa40
  i2c_smbus_xfer+0x19e/0x340
  i2cdev_ioctl_smbus+0x38f/0x7f0
  i2cdev_ioctl+0x35e/0x680
  __x64_sys_ioctl+0x147/0x1e0
  do_syscall_64+0xcf/0x15a0
  entry_SYSCALL_64_after_hwframe+0x76/0x7e
  </TASK>

Both traces reproduced on v7.0-rc6+i2c/for-current with KASAN+UBSAN.

Fixes: 8a325997d9 ("i2c: Add message transfer tracepoints for SMBUS [ver #2]")
Fixes: 4710317891 ("i2c-stub: Implement I2C block support")
Reported-by: Xiang Mei <xmei5@asu.edu>
Signed-off-by: Weiming Shi <bestswngs@gmail.com>
Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
2026-05-07 10:59:07 +02:00
..
algos i2c: pcf8584: Change pcf_doAdress() to pcf_send_address() 2025-10-28 17:05:39 +01:00
busses i2c: stm32f7: reinit_completion() per transfer not per msg 2026-05-04 10:22:00 +02:00
muxes i2c: muxes: pca954x: Fix broken reset-gpio usage 2025-11-03 18:04:15 +01:00
i2c-atr.c i2c: atr: use kzalloc_flex 2026-04-10 01:16:59 +02:00
i2c-boardinfo.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
i2c-core-acpi.c i2c: acpi: Add ELAN0678 to i2c_acpi_force_100khz_device_ids 2026-05-04 11:17:46 +02:00
i2c-core-base.c i2c: Compare the return value of gpiod_get_direction against GPIO_LINE_DIRECTION_OUT 2026-05-04 12:19:25 +02:00
i2c-core-of-prober.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
i2c-core-of.c device property: Make modifications of fwnode "flags" thread safe 2026-03-26 22:00:59 +01:00
i2c-core-slave.c i2c: Use trace_call__##name() at guarded tracepoint call sites 2026-03-26 10:24:39 -04:00
i2c-core-smbus.c i2c: smbus: reject oversized block transfers in the common path 2026-05-07 10:59:07 +02:00
i2c-core.h i2c: Unexport i2c_of_match_device() 2025-02-13 16:47:52 +01:00
i2c-dev.c i2c: dev: prevent integer overflow in I2C_TIMEOUT ioctl 2026-05-04 11:31:35 +02:00
i2c-mux.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
i2c-slave-eeprom.c sysfs: treewide: switch back to bin_attribute::read()/write() 2025-06-17 10:44:13 +02:00
i2c-slave-testunit.c i2c: testunit: Replace system_long_wq with system_dfl_long_wq 2026-05-04 10:07:46 +02:00
i2c-smbus.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
i2c-stub.c i2c: stub: Reject I2C block transfers with invalid length 2026-05-04 13:23:53 +02:00
Kconfig i2c: Remove I2C_COMPAT config symbol and related code 2024-09-07 18:31:28 +02:00
Makefile i2c: Introduce OF component probe function 2024-11-27 12:04:10 +01:00