The PMS7003 driver accepts any frame data length up to the size of its frame buffer. This lets a device send a frame with a length shorter than the two checksum bytes. When that frame is completed, pms7003_frame_is_okay() subtracts PMS7003_CHECKSUM_LENGTH and reads the checksum from before frame->data. Lengths that are at least two bytes but shorter than the fixed PMS7003 data payload can also pass checksum validation and leave the later PM reads using bytes that were never received for the current frame. Accept only the fixed 28-byte PMS7003 data payload and make the checksum helper reject any other length before deriving the checksum offset. Tooling disclosure: I found this bug. OpenAI Codex helped prepare the fix, and I reviewed the changes. Fixes: a1d642266c14 ("iio: chemical: add support for Plantower PMS7003 sensor") Cc: stable@vger.kernel.org Signed-off-by: Zhiqi Zhang --- drivers/iio/chemical/pms7003.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/drivers/iio/chemical/pms7003.c b/drivers/iio/chemical/pms7003.c index c50edb24a..4f62b5092 100644 --- a/drivers/iio/chemical/pms7003.c +++ b/drivers/iio/chemical/pms7003.c @@ -206,7 +206,12 @@ static u16 pms7003_calc_checksum(struct pms7003_frame *frame) static bool pms7003_frame_is_okay(struct pms7003_frame *frame) { int offset = frame->length - PMS7003_CHECKSUM_LENGTH; - u16 checksum = get_unaligned_be16(frame->data + offset); + u16 checksum; + + if (frame->length != PMS7003_MAX_DATA_LENGTH) + return false; + + checksum = get_unaligned_be16(frame->data + offset); return checksum == pms7003_calc_checksum(frame); } @@ -231,7 +236,7 @@ static size_t pms7003_receive_buf(struct serdev_device *serdev, const u8 *buf, return 2; num = get_unaligned_be16(buf + 2); - if (num <= PMS7003_MAX_DATA_LENGTH) { + if (num == PMS7003_MAX_DATA_LENGTH) { frame->expected_length = num; frame->length = 0; } -- 2.54.0 (Apple Git-157)