Skip to content

Commit a9a00d7

Browse files
cjd8jic23
authored andcommitted
iio: magnetometer: ak8975: fix potential kernel stack memory leak
Currently in the AK8975 driver there are four instances where potential uninitialized kernel stack memory leaks can occur. If i2c_smbus_read_i2c_block_data_or_emulated() returns a value less than the size of the buffer, uninitialized bytes are retained in the buffer and later the buffer is passed on to IIO buffers, potentially leaking memory to userspace. Fix this by adding checks whether the return value of the function is equal to the size of the buffer and subsequently if the value is lesser than zero to distinguish from a returned error code. Fixes: bc11ca4 ("iio:magnetometer:ak8975: triggered buffer support") Reported-by: Sashiko <sashiko-bot@kernel.org> Closes: https://sashiko.dev/#/patchset/20260513-ak8975-fix-v1-1-104ea605dd54%40gmail.com Signed-off-by: Joshua Crofts <joshua.crofts1@gmail.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent eaead58 commit a9a00d7

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

drivers/iio/magnetometer/ak8975.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,10 @@ static int ak8975_who_i_am(const struct ak8975_data *data,
499499
dev_err(&client->dev, "Error reading WIA\n");
500500
return ret;
501501
}
502+
if (ret != sizeof(wia_val)) {
503+
dev_err(&client->dev, "Error reading WIA\n");
504+
return -EIO;
505+
}
502506

503507
if (wia_val[0] != AK8975_DEVICE_ID)
504508
return -ENODEV;
@@ -620,6 +624,10 @@ static int ak8975_setup(struct ak8975_data *data)
620624
dev_err(&client->dev, "Not able to read asa data\n");
621625
return ret;
622626
}
627+
if (ret != sizeof(data->asa)) {
628+
dev_err(&client->dev, "Error reading asa data\n");
629+
return -EIO;
630+
}
623631

624632
/* After reading fuse ROM data set power-down mode */
625633
ret = ak8975_set_mode(data, POWER_DOWN);
@@ -755,6 +763,10 @@ static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
755763
(u8 *)&rval);
756764
if (ret < 0)
757765
goto exit;
766+
if (ret != sizeof(rval)) {
767+
ret = -EIO;
768+
goto exit;
769+
}
758770

759771
/* Read out ST2 for release lock on measurement data. */
760772
ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST2]);
@@ -871,6 +883,8 @@ static void ak8975_fill_buffer(struct iio_dev *indio_dev)
871883
(u8 *)fval);
872884
if (ret < 0)
873885
goto unlock;
886+
if (ret != sizeof(fval))
887+
goto unlock;
874888

875889
mutex_unlock(&data->lock);
876890

0 commit comments

Comments
 (0)