iio: chemical: scd30: avoid potential NULL deref in scd30_i2c_command()
Subsystems (2)
PATCH 1/1 iio: chemical: scd30: avoid potential NULL deref in scd30_i2c_command()
scd30_i2c_command() takes an opaque "response" buffer plus its size. At the start of the function the code already checks if response is NULL (via the rsp local), but the response-decoding loop after the i2c transfer always dereferences rsp without re-checking.
With the current callers in scd30_core.c this is harmless, since write commands pass response=NULL together with size=0 (so the loop body is never entered). However, the inconsistency is an accident waiting to happen if a future caller passes response=NULL together with size > 0 -- the loop would then write through a NULL pointer.
smatch flags this:
drivers/iio/chemical/scd30_i2c.c:104 scd30_i2c_command() error: we
previously assumed rsp could be null (see line 77)
Bail out early when rsp is NULL so the function is robust regardless of the (cmd, size) combination chosen by the caller.
No functional change for the existing callers.
--- drivers/iio/chemical/scd30_i2c.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/iio/chemical/scd30_i2c.c +3 -0
@@ -93,6 +93,9 @@ static int scd30_i2c_command(struct scd30_state *state, enum scd30_cmd cmd, u16if (ret)return ret; +if (!rsp) +return 0 ; +/* validate received data and strip off crc bytes */ for (i =0 ; i < size; i +=3 ) { crc =crc8 (scd30_i2c_crc8_tbl, buf + i,2 ,CRC8_INIT_VALUE );
Signature · 1 line
2.43.0
Jonathan Cameron
maintainerQuoted 16 lines · On Wed, 6 May 2026 23:15:33 +0500 Stepan Ionichev <[email protected]> wrote:
scd30_i2c_command()takes an opaque "response" buffer plus its size. At the start of the function the code already checks if response is NULL (via the rsp local), but the response-decoding loop after the i2c transfer always dereferences rsp without re-checking.With the current callers in
scd30_core.cthis is harmless, since write commands pass response=NULL together with size=0 (so the loop body is never entered). However, the inconsistency is an accident waiting to happen if a future caller passes response=NULL together with size > 0 -- the loop would then write through a NULL pointer.smatch flags this:
drivers/iio/chemical/scd30_i2c.c:104scd30_i2c_command()error: we
previously assumed rsp could be null (see line 77)
Quoted 6 lines
Bail out early when rsp is NULL so the function is robust regardless of the (cmd, size) combination chosen by the caller.
No functional change for the existing callers.
Signed-off-by Stepan Ionichev <[email protected]>
Hi Stephan
Thanks for the analysis - as you say no actual bug here but good to make that more obvious to static analzers.
If we ever did hit this I think it would be better to return an error code. I'd also do it at the top given such a combination doesn't make sense so we should exclude it early.
if (!response && size != 0)
return -EINVAL;
Quoted 18 lines
--- drivers/iio/chemical/scd30_i2c.c | 3 +++ 1 file changed, 3 insertions(+)drivers/iio/chemical/scd30_i2c.c +3
-0@@ -93,6 +93,9 @@ static int scd30_i2c_command(struct scd30_state *state, enum scd30_cmd cmd, u16if (ret)return ret; +if (!rsp) +return 0 ; +/* validate received data and strip off crc bytes */ for (i =0 ; i < size; i +=3 ) { crc =crc8 (scd30_i2c_crc8_tbl, buf + i,2 ,CRC8_INIT_VALUE );
PATCH v2 1/1 iio: chemical: scd30: reject (response=NULL, size>0) in scd30_i2c_command()
In reply to Stepan Ionichevscd30_i2c_command() takes an opaque "response" buffer plus its size. At the start of the function the code already checks if response is NULL (via the rsp local), but the response-decoding loop after the i2c transfer always dereferences rsp without re-checking. With the current callers in scd30_core.c this is harmless, since write commands pass response=NULL together with size=0 (so the loop body is never entered).
The (response=NULL, size>0) combination has no useful meaning: there is nowhere to put the bytes that come back from the chip. Treat it as an invalid argument and bail out at the top of the function with -EINVAL, instead of silently doing the i2c transfer and dereferencing a NULL pointer in the decode loop.
smatch flagged the inconsistency:
drivers/iio/chemical/scd30_i2c.c:104 scd30_i2c_command() error: we
previously assumed rsp could be null (see line 77)
No functional change for the existing callers, which only ever use (response=NULL, size=0) for writes and (response!=NULL, size>0) for reads.
---
v2:
- Move the check to the top of the function and return -EINVAL on the (response=NULL, size>0) combination, as suggested by Jonathan Cameron. Drop the v1 "if (!rsp) return 0" deeper in the function.
drivers/iio/chemical/scd30_i2c.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/iio/chemical/scd30_i2c.c +3 -0
@@ -71,6 +71,9 @@ static int scd30_i2c_command(struct scd30_state *state, enum scd30_cmd cmd, u16int i, ret;char crc; +if (!response && size !=0 ) +return -EINVAL ; +put_unaligned_be16 (scd30_i2c_cmd_lookup_tbl[cmd], buf); i =2 ;
--
2.43.0
Re: [PATCH v2] iio: chemical: scd30: reject (response=NULL, size>0) in scd30_i2c_command()
Quoted 14 lines · Stepan Ionichev
scd30_i2c_command()takes an opaque "response" buffer plus its size. At the start of the function the code already checks if response is NULL (via the rsp local), but the response-decoding loop after the i2c transfer always dereferences rsp without re-checking. With the current callers inscd30_core.cthis is harmless, since write commands pass response=NULL together with size=0 (so the loop body is never entered).The (response=NULL, size>0) combination has no useful meaning: there is nowhere to put the bytes that come back from the chip. Treat it as an invalid argument and bail out at the top of the function with
-EINVAL, instead of silently doing the i2c transfer and dereferencing a NULL pointer in the decode loop.
Quoted 8 lines
smatch flagged the inconsistency:
drivers/iio/chemical/scd30_i2c.c:104scd30_i2c_command()error: we
previously assumed rsp could be null (see line 77)No functional change for the existing callers, which only ever use (response=NULL, size=0) for writes and (response!=NULL, size>0) for reads.
Is this analysis AI assisted?
Quoted 6 lines
Signed-off-by Stepan Ionichev <[email protected]>---
v2:
- Move the check to the top of the function and return-EINVALon the (response=NULL, size>0) combination, as suggested by Jonathan Cameron. Drop the v1 "if (!rsp) return 0" deeper in the function.
Do not reply to the same email thread with a new patch version.
...
Code wise LGTM, thanks.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2] iio: chemical: scd30: reject (response=NULL, size>0) in scd30_i2c_command()
Quoted 1 line · Andy Shevchenko
Is this analysis AI assisted?
The bug itself was found by smatch and analysed manually; AI was used to help draft the commit message wording.
Quoted 1 line
Do not reply to the same email thread with a new patch version.
Got it -- will send future v-N in separate threads.
Thanks for the review!
Stepan
Re: [PATCH v2] iio: chemical: scd30: reject (response=NULL, size>0) in scd30_i2c_command()
In reply to Stepan IonichevQuoted 8 lines · Stepan Ionichev
scd30_i2c_command()takes an opaque "response" buffer plus its size. At the start of the function the code already checks if response is NULL (via the rsp local), but the response-decoding loop after the i2c transfer always dereferences rsp without re-checking. With the current callers inscd30_core.cthis is harmless, since write commands pass response=NULL together with size=0 (so the loop body is never entered).
[snip]
drivers/iio/chemical/scd30_i2c.c static int scd30_i2c_command(struct scd30_state *state, enum scd30_cmd cmd, u16 · line 79
Quoted 10 lines
drivers/iio/chemical/scd30_i2c.c +3
-0@@ -71,6 +71,9 @@ static int scd30_i2c_command(struct scd30_state *state, enum scd30_cmd cmd, u16int i, ret;char crc; +if (!response && size !=0 ) +return -EINVAL ; +put_unaligned_be16 (scd30_i2c_cmd_lookup_tbl[cmd], buf); i =2 ;
I guess we're still handling the cases where both response/rsp and size are zero here below?
if (rsp) {
/* each two bytes are followed by a crc8 */
size += size / 2;
} else {
put_unaligned_be16(arg, buf + i);
crc = crc8(scd30_i2c_crc8_tbl, buf + i, 2, CRC8_INIT_VALUE);
i += 2;
buf[i] = crc;
i += 1;
/* commands below don't take an argument */
if ((cmd == CMD_STOP_MEAS) || (cmd == CMD_RESET))
i -= 3;
}Should add a comment showing that this handles this case but that's just my personal nit. Not worth forcing a v3 so
best regards,
max
Re: [PATCH v2] iio: chemical: scd30: reject (response=NULL, size>0) in scd30_i2c_command()
Quoted 7 lines · Maxwell Doose
I guess we're still handling the cases where both response/rsp and size are zero here below?
Should add a comment showing that this handles this case but that's just my personal nit. Not worth forcing a v3 so
Acked-by Maxwell Doose <[email protected]>
Thanks for the ack and the review!
You're right -- the (NULL, 0) write-command path is still handled correctly by the existing rsp-NULL branch below; the new check only rejects the genuinely buggy (NULL, size>0) combination. Happy to add a short comment making that intent explicit if Jonathan would prefer a v3, otherwise I'll keep it noted for a future cleanup.
Stepan
Re: [PATCH v2] iio: chemical: scd30: reject (response=NULL, size>0) in scd30_i2c_command()
Quoted 10 lines · Stepan Ionichev
Quoted 7 lines · Maxwell Doose
I guess we're still handling the cases where both response/rsp and size are zero here below?
Should add a comment showing that this handles this case but that's just my personal nit. Not worth forcing a v3 so
Acked-by Maxwell Doose <[email protected]>
Quoted 8 lines
Thanks for the ack and the review!
You're right -- the (NULL, 0) write-command path is still handled correctly by the existing rsp-NULL branch below; the new check only rejects the genuinely buggy (NULL, size>0) combination. Happy to add a short comment making that intent explicit if Jonathan would prefer a v3, otherwise I'll keep it noted for a future cleanup.
BTW, if Jonathan asks you to resend for some other technical issue, please add that comment. Otherwise if he doesn't ask for v3 or doesn't include it himself, don't bother. Also, keep in mind that "Acked-by != Reviewed-by". Just means the patch is appropriate for inclusion.
best regards,
max
Jonathan Cameron
maintainerRe: [PATCH v2] iio: chemical: scd30: reject (response=NULL, size>0) in scd30_i2c_command()
Quoted 20 lines · On Fri, 8 May 2026 14:50:24 -0500 Maxwell Doose <[email protected]> wrote:
Quoted 10 lines · Stepan Ionichev
Quoted 7 lines · Maxwell Doose
I guess we're still handling the cases where both response/rsp and size are zero here below?
Should add a comment showing that this handles this case but that's just my personal nit. Not worth forcing a v3 so
Acked-by Maxwell Doose <[email protected]>Quoted 8 lines
Thanks for the ack and the review!
You're right -- the (NULL, 0) write-command path is still handled correctly by the existing rsp-NULL branch below; the new check only rejects the genuinely buggy (NULL, size>0) combination. Happy to add a short comment making that intent explicit if Jonathan would prefer a v3, otherwise I'll keep it noted for a future cleanup.
Quoted 4 lines
BTW, if Jonathan asks you to resend for some other technical issue, please add that comment. Otherwise if he doesn't ask for v3 or doesn't include it himself, don't bother. Also, keep in mind that "Acked-by != Reviewed-by". Just means the patch is appropriate for inclusion.
In my view it is a little borderline on whether such a comment is useful given the fairly obvious && rather than || in the check added. Anyhow, no other reason to respin to I've applied this as is.
Thanks,
Jonathan
Quoted 3 lines
best regards,
max