Skip to content

Commit 9f92055

Browse files
cjd8gregkh
authored andcommitted
iio: magnetometer: ak8975: fix potential kernel stack memory leak
[ Upstream commit a9a00d7 ] 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> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 949b970 commit 9f92055

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
@@ -464,6 +464,10 @@ static int ak8975_who_i_am(struct i2c_client *client,
464464
dev_err(&client->dev, "Error reading WIA\n");
465465
return ret;
466466
}
467+
if (ret != sizeof(wia_val)) {
468+
dev_err(&client->dev, "Error reading WIA\n");
469+
return -EIO;
470+
}
467471

468472
if (wia_val[0] != AK8975_DEVICE_ID)
469473
return -ENODEV;
@@ -580,6 +584,10 @@ static int ak8975_setup(struct i2c_client *client)
580584
dev_err(&client->dev, "Not able to read asa data\n");
581585
return ret;
582586
}
587+
if (ret != sizeof(data->asa)) {
588+
dev_err(&client->dev, "Error reading asa data\n");
589+
return -EIO;
590+
}
583591

584592
/* After reading fuse ROM data set power-down mode */
585593
ret = ak8975_set_mode(data, POWER_DOWN);
@@ -719,6 +727,10 @@ static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
719727
sizeof(rval), (u8*)&rval);
720728
if (ret < 0)
721729
goto exit;
730+
if (ret != sizeof(rval)) {
731+
ret = -EIO;
732+
goto exit;
733+
}
722734

723735
/* Read out ST2 for release lock on measurment data. */
724736
ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST2]);
@@ -848,6 +860,8 @@ static void ak8975_fill_buffer(struct iio_dev *indio_dev)
848860
(u8 *)fval);
849861
if (ret < 0)
850862
goto unlock;
863+
if (ret != sizeof(fval))
864+
goto unlock;
851865

852866
mutex_unlock(&data->lock);
853867

0 commit comments

Comments
 (0)