2022-09-27 07:09:12

by Yu Zhe

[permalink] [raw]
Subject: [PATCH] cxl/pmem: Use size_add() against integer overflow

"struct_size() + n" may cause a integer overflow,
use size_add() to handle it.

Signed-off-by: Yu Zhe <[email protected]>
---
drivers/cxl/pmem.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/cxl/pmem.c b/drivers/cxl/pmem.c
index 7dc0a2fa1a6b..8c08aa009a56 100644
--- a/drivers/cxl/pmem.c
+++ b/drivers/cxl/pmem.c
@@ -148,7 +148,7 @@ static int cxl_pmem_set_config_data(struct cxl_dev_state *cxlds,
return -EINVAL;

/* 4-byte status follows the input data in the payload */
- if (struct_size(cmd, in_buf, cmd->in_length) + 4 > buf_len)
+ if (size_add(struct_size(cmd, in_buf, cmd->in_length), 4) > buf_len)
return -EINVAL;

set_lsa =
--
2.11.0


2022-09-27 16:33:56

by Ira Weiny

[permalink] [raw]
Subject: Re: [PATCH] cxl/pmem: Use size_add() against integer overflow

On Tue, Sep 27, 2022 at 12:02:47AM -0700, Yu Zhe wrote:
> "struct_size() + n" may cause a integer overflow,
> use size_add() to handle it.
>
> Signed-off-by: Yu Zhe <[email protected]>
> ---
> drivers/cxl/pmem.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/cxl/pmem.c b/drivers/cxl/pmem.c
> index 7dc0a2fa1a6b..8c08aa009a56 100644
> --- a/drivers/cxl/pmem.c
> +++ b/drivers/cxl/pmem.c
> @@ -148,7 +148,7 @@ static int cxl_pmem_set_config_data(struct cxl_dev_state *cxlds,
> return -EINVAL;
>
> /* 4-byte status follows the input data in the payload */
> - if (struct_size(cmd, in_buf, cmd->in_length) + 4 > buf_len)
> + if (size_add(struct_size(cmd, in_buf, cmd->in_length), 4) > buf_len)

I don't see any benefit here.

struct_size() calls __ab_c_size() which already calls check_add_overflow()? So
why wrap that in another check?

Were you able to get this to fail with some user input?

Ira

> return -EINVAL;
>
> set_lsa =
> --
> 2.11.0
>

2022-09-29 07:48:31

by Yu Zhe

[permalink] [raw]
Subject: Re: [PATCH] cxl/pmem: Use size_add() against integer overflow

在 2022年09月28日 00:23, Ira Weiny 写道:

> On Tue, Sep 27, 2022 at 12:02:47AM -0700, Yu Zhe wrote:
>> "struct_size() + n" may cause a integer overflow,
>> use size_add() to handle it.
>>
>> Signed-off-by: Yu Zhe <[email protected]>
>> ---
>> drivers/cxl/pmem.c | 2 +-
>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/cxl/pmem.c b/drivers/cxl/pmem.c
>> index 7dc0a2fa1a6b..8c08aa009a56 100644
>> --- a/drivers/cxl/pmem.c
>> +++ b/drivers/cxl/pmem.c
>> @@ -148,7 +148,7 @@ static int cxl_pmem_set_config_data(struct cxl_dev_state *cxlds,
>> return -EINVAL;
>>
>> /* 4-byte status follows the input data in the payload */
>> - if (struct_size(cmd, in_buf, cmd->in_length) + 4 > buf_len)
>> + if (size_add(struct_size(cmd, in_buf, cmd->in_length), 4) > buf_len)
> I don't see any benefit here.
>
> struct_size() calls __ab_c_size() which already calls check_add_overflow()? So
> why wrap that in another check?
"struct_size() + 4" still might cause overflow, so there need to use
"size_add" to check it.
> Were you able to get this to fail with some user input?
>
> Ira
>
>> return -EINVAL;
>>
>> set_lsa =
>> --
>> 2.11.0
>>
>>

2022-10-21 23:34:06

by Dan Williams

[permalink] [raw]
Subject: RE: [PATCH] cxl/pmem: Use size_add() against integer overflow

Yu Zhe wrote:
> "struct_size() + n" may cause a integer overflow,
> use size_add() to handle it.
>
> Signed-off-by: Yu Zhe <[email protected]>
> ---
> drivers/cxl/pmem.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/cxl/pmem.c b/drivers/cxl/pmem.c
> index 7dc0a2fa1a6b..8c08aa009a56 100644
> --- a/drivers/cxl/pmem.c
> +++ b/drivers/cxl/pmem.c
> @@ -148,7 +148,7 @@ static int cxl_pmem_set_config_data(struct cxl_dev_state *cxlds,
> return -EINVAL;
>
> /* 4-byte status follows the input data in the payload */
> - if (struct_size(cmd, in_buf, cmd->in_length) + 4 > buf_len)
> + if (size_add(struct_size(cmd, in_buf, cmd->in_length), 4) > buf_len)
> return -EINVAL;

Looks good, applied for v6.1-rc fixes.