public inbox for linux-trace-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] i2c: Fix OOB access in trace_event_raw_event_smbus_write
@ 2025-08-21  1:23 Xiaomeng Zhang
  2025-09-02 19:49 ` Steven Rostedt
  0 siblings, 1 reply; 3+ messages in thread
From: Xiaomeng Zhang @ 2025-08-21  1:23 UTC (permalink / raw)
  To: rostedt, mhiramat, dhowells, wsa; +Cc: linux-kernel, linux-trace-kernel

The smbus_write tracepoint copies __entry->len bytes into a fixed
I2C_SMBUS_BLOCK_MAX + 2 buffer. Oversized lengths (e.g., 46)
exceed the destination and over-read the source buffer, triggering
OOB warning:

memcpy: detected field-spanning write (size 48) of single field
"entry->buf" at include/trace/events/smbus.h:60 (size 34)

Clamp the copy size to I2C_SMBUS_BLOCK_MAX + 2 before memcpy().
This only affects tracing and does not change I2C transfer behavior.

Fixes: 8a325997d95d ("i2c: Add message transfer tracepoints for SMBUS [ver #2]")
Signed-off-by: Xiaomeng Zhang <zhangxiaomeng13@huawei.com>
---
 include/trace/events/smbus.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/include/trace/events/smbus.h b/include/trace/events/smbus.h
index 71a87edfc46d..e306d8b928c3 100644
--- a/include/trace/events/smbus.h
+++ b/include/trace/events/smbus.h
@@ -57,6 +57,8 @@ TRACE_EVENT_CONDITION(smbus_write,
 		case I2C_SMBUS_I2C_BLOCK_DATA:
 			__entry->len = data->block[0] + 1;
 		copy:
+			if (__entry->len > I2C_SMBUS_BLOCK_MAX + 2)
+				__entry->len = I2C_SMBUS_BLOCK_MAX + 2;
 			memcpy(__entry->buf, data->block, __entry->len);
 			break;
 		case I2C_SMBUS_QUICK:
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] i2c: Fix OOB access in trace_event_raw_event_smbus_write
  2025-08-21  1:23 [PATCH] i2c: Fix OOB access in trace_event_raw_event_smbus_write Xiaomeng Zhang
@ 2025-09-02 19:49 ` Steven Rostedt
  2025-09-09  9:30   ` 回复: " zhangxiaomeng (A)
  0 siblings, 1 reply; 3+ messages in thread
From: Steven Rostedt @ 2025-09-02 19:49 UTC (permalink / raw)
  To: Xiaomeng Zhang; +Cc: mhiramat, dhowells, wsa, linux-kernel, linux-trace-kernel

On Thu, 21 Aug 2025 01:23:12 +0000
Xiaomeng Zhang <zhangxiaomeng13@huawei.com> wrote:

> The smbus_write tracepoint copies __entry->len bytes into a fixed
> I2C_SMBUS_BLOCK_MAX + 2 buffer. Oversized lengths (e.g., 46)
> exceed the destination and over-read the source buffer, triggering
> OOB warning:
> 
> memcpy: detected field-spanning write (size 48) of single field
> "entry->buf" at include/trace/events/smbus.h:60 (size 34)
> 
> Clamp the copy size to I2C_SMBUS_BLOCK_MAX + 2 before memcpy().
> This only affects tracing and does not change I2C transfer behavior.
> 
> Fixes: 8a325997d95d ("i2c: Add message transfer tracepoints for SMBUS [ver #2]")
> Signed-off-by: Xiaomeng Zhang <zhangxiaomeng13@huawei.com>
> ---
>  include/trace/events/smbus.h | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/include/trace/events/smbus.h b/include/trace/events/smbus.h
> index 71a87edfc46d..e306d8b928c3 100644
> --- a/include/trace/events/smbus.h
> +++ b/include/trace/events/smbus.h
> @@ -57,6 +57,8 @@ TRACE_EVENT_CONDITION(smbus_write,
>  		case I2C_SMBUS_I2C_BLOCK_DATA:
>  			__entry->len = data->block[0] + 1;
>  		copy:
> +			if (__entry->len > I2C_SMBUS_BLOCK_MAX + 2)
> +				__entry->len = I2C_SMBUS_BLOCK_MAX + 2;
>  			memcpy(__entry->buf, data->block, __entry->len);
>  			break;
>  		case I2C_SMBUS_QUICK:

The code has:

                switch (protocol) {
                case I2C_SMBUS_BYTE_DATA:
                        __entry->len = 1;
                        goto copy;
                case I2C_SMBUS_WORD_DATA:
                case I2C_SMBUS_PROC_CALL:
                        __entry->len = 2;
                        goto copy;
                case I2C_SMBUS_BLOCK_DATA:
                case I2C_SMBUS_BLOCK_PROC_CALL:
                case I2C_SMBUS_I2C_BLOCK_DATA:
                        __entry->len = data->block[0] + 1;
                copy:   
                        memcpy(__entry->buf, data->block, __entry->len);
                        break;
                case I2C_SMBUS_QUICK:
                case I2C_SMBUS_BYTE:
                case I2C_SMBUS_I2C_BLOCK_BROKEN:
                default:
                        __entry->len = 0;
                }

I only see two calls to the copy where one is len = 1 and the other is
len = 2. Why not put the check before the copy label?

-- Steve

^ permalink raw reply	[flat|nested] 3+ messages in thread

* 回复: [PATCH] i2c: Fix OOB access in trace_event_raw_event_smbus_write
  2025-09-02 19:49 ` Steven Rostedt
@ 2025-09-09  9:30   ` zhangxiaomeng (A)
  0 siblings, 0 replies; 3+ messages in thread
From: zhangxiaomeng (A) @ 2025-09-09  9:30 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: mhiramat@kernel.org, dhowells@redhat.com, wsa@kernel.org,
	linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org

I previously attempted to apply a fix within the i2cdev_ioctl_smbus function. While this approach was successful in preventing the warning, I found that the required changes were quite extensive. The WARN is triggered by the trace_smbus_write tracepoint, which performs a memcpy(__entry->buf, data->block, len) for write operations on three specific block protocols: I2C_SMBUS_BLOCK_DATA, I2C_SMBUS_I2C_BLOCK_DATA, and I2C_SMBUS_BLOCK_PROC_CALL. To fix this in i2cdev_ioctl_smbus, it would be necessary to add checks for all three of these cases, which makes the solution rather complex.

--xiaomeng
-----邮件原件-----
发件人: Steven Rostedt <rostedt@goodmis.org> 
发送时间: 2025年9月3日 3:50
收件人: zhangxiaomeng (A) <zhangxiaomeng13@huawei.com>
抄送: mhiramat@kernel.org; dhowells@redhat.com; wsa@kernel.org; linux-kernel@vger.kernel.org; linux-trace-kernel@vger.kernel.org
主题: Re: [PATCH] i2c: Fix OOB access in trace_event_raw_event_smbus_write

On Thu, 21 Aug 2025 01:23:12 +0000
Xiaomeng Zhang <zhangxiaomeng13@huawei.com> wrote:

> The smbus_write tracepoint copies __entry->len bytes into a fixed 
> I2C_SMBUS_BLOCK_MAX + 2 buffer. Oversized lengths (e.g., 46) exceed 
> the destination and over-read the source buffer, triggering OOB 
> warning:
> 
> memcpy: detected field-spanning write (size 48) of single field 
> "entry->buf" at include/trace/events/smbus.h:60 (size 34)
> 
> Clamp the copy size to I2C_SMBUS_BLOCK_MAX + 2 before memcpy().
> This only affects tracing and does not change I2C transfer behavior.
> 
> Fixes: 8a325997d95d ("i2c: Add message transfer tracepoints for SMBUS 
> [ver #2]")
> Signed-off-by: Xiaomeng Zhang <zhangxiaomeng13@huawei.com>
> ---
>  include/trace/events/smbus.h | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/include/trace/events/smbus.h 
> b/include/trace/events/smbus.h index 71a87edfc46d..e306d8b928c3 100644
> --- a/include/trace/events/smbus.h
> +++ b/include/trace/events/smbus.h
> @@ -57,6 +57,8 @@ TRACE_EVENT_CONDITION(smbus_write,
>  		case I2C_SMBUS_I2C_BLOCK_DATA:
>  			__entry->len = data->block[0] + 1;
>  		copy:
> +			if (__entry->len > I2C_SMBUS_BLOCK_MAX + 2)
> +				__entry->len = I2C_SMBUS_BLOCK_MAX + 2;
>  			memcpy(__entry->buf, data->block, __entry->len);
>  			break;
>  		case I2C_SMBUS_QUICK:

The code has:

                switch (protocol) {
                case I2C_SMBUS_BYTE_DATA:
                        __entry->len = 1;
                        goto copy;
                case I2C_SMBUS_WORD_DATA:
                case I2C_SMBUS_PROC_CALL:
                        __entry->len = 2;
                        goto copy;
                case I2C_SMBUS_BLOCK_DATA:
                case I2C_SMBUS_BLOCK_PROC_CALL:
                case I2C_SMBUS_I2C_BLOCK_DATA:
                        __entry->len = data->block[0] + 1;
                copy:   
                        memcpy(__entry->buf, data->block, __entry->len);
                        break;
                case I2C_SMBUS_QUICK:
                case I2C_SMBUS_BYTE:
                case I2C_SMBUS_I2C_BLOCK_BROKEN:
                default:
                        __entry->len = 0;
                }

I only see two calls to the copy where one is len = 1 and the other is len = 2. Why not put the check before the copy label?

-- Steve


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-09-09  9:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-21  1:23 [PATCH] i2c: Fix OOB access in trace_event_raw_event_smbus_write Xiaomeng Zhang
2025-09-02 19:49 ` Steven Rostedt
2025-09-09  9:30   ` 回复: " zhangxiaomeng (A)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox