Skip to content

Commit 2409bfd

Browse files
Jonathan Camerongregkh
authored andcommitted
iio:light:st_uvis25: Fix timestamp alignment and prevent data leak.
commit d837a99 upstream. One of a class of bugs pointed out by Lars in a recent review. iio_push_to_buffers_with_timestamp() assumes the buffer used is aligned to the size of the timestamp (8 bytes). This is not guaranteed in this driver which uses an array of smaller elements on the stack. As Lars also noted this anti pattern can involve a leak of data to userspace and that indeed can happen here. We close both issues by moving to a suitable structure in the iio_priv() This data is allocated with kzalloc() so no data can leak apart from previous readings. A local unsigned int variable is used for the regmap call so it is clear there is no potential issue with writing into the padding of the structure. Fixes: 3025c86 ("iio: light: add support for UVIS25 sensor") Reported-by: Lars-Peter Clausen <lars@metafoo.de> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Reviewed-by: Alexandru Ardelean <alexandru.ardelean@analog.com> Acked-by: Lorenzo Bianconi <lorenzo@kernel.org> Cc: <Stable@vger.kernel.org> Link: https://lore.kernel.org/r/20200920112742.170751-3-jic23@kernel.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 45ebad3 commit 2409bfd

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

‎drivers/iio/light/st_uvis25.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ struct st_uvis25_hw {
2727
struct iio_trigger *trig;
2828
bool enabled;
2929
int irq;
30+
/* Ensure timestamp is naturally aligned */
31+
struct {
32+
u8 chan;
33+
s64 ts __aligned(8);
34+
} scan;
3035
};
3136

3237
extern const struct dev_pm_ops st_uvis25_pm_ops;

‎drivers/iio/light/st_uvis25_core.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,17 +232,19 @@ static const struct iio_buffer_setup_ops st_uvis25_buffer_ops = {
232232

233233
static irqreturn_t st_uvis25_buffer_handler_thread(int irq, void *p)
234234
{
235-
u8 buffer[ALIGN(sizeof(u8), sizeof(s64)) + sizeof(s64)];
236235
struct iio_poll_func *pf = p;
237236
struct iio_dev *iio_dev = pf->indio_dev;
238237
struct st_uvis25_hw *hw = iio_priv(iio_dev);
238+
unsigned int val;
239239
int err;
240240

241-
err = regmap_read(hw->regmap, ST_UVIS25_REG_OUT_ADDR, (int *)buffer);
241+
err = regmap_read(hw->regmap, ST_UVIS25_REG_OUT_ADDR, &val);
242242
if (err < 0)
243243
goto out;
244244

245-
iio_push_to_buffers_with_timestamp(iio_dev, buffer,
245+
hw->scan.chan = val;
246+
247+
iio_push_to_buffers_with_timestamp(iio_dev, &hw->scan,
246248
iio_get_time_ns(iio_dev));
247249

248250
out:

0 commit comments

Comments
 (0)