Skip to content

Commit a661b57

Browse files
committed
iio:adc:ti-adc084s021 Fix alignment and data leak issues.
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. The force alignment of ts is not strictly necessary in this case but reduces the fragility of the code. Fixes: 3691e5a ("iio: adc: add driver for the ti-adc084s021 chip") Reported-by: Lars-Peter Clausen <lars@metafoo.de> Cc: Mårten Lindahl <martenli@axis.com> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com> Cc: <Stable@vger.kernel.org>
1 parent 54f82df commit a661b57

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

drivers/iio/adc/ti-adc084s021.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ struct adc084s021 {
2626
struct spi_transfer spi_trans;
2727
struct regulator *reg;
2828
struct mutex lock;
29+
/* Buffer used to align data */
30+
struct {
31+
__be16 channels[4];
32+
s64 ts __aligned(8);
33+
} scan;
2934
/*
3035
* DMA (thus cache coherency maintenance) requires the
3136
* transfer buffers to live in their own cache line.
@@ -141,14 +146,13 @@ static irqreturn_t adc084s021_buffer_trigger_handler(int irq, void *pollfunc)
141146
struct iio_poll_func *pf = pollfunc;
142147
struct iio_dev *indio_dev = pf->indio_dev;
143148
struct adc084s021 *adc = iio_priv(indio_dev);
144-
__be16 data[8] = {0}; /* 4 * 16-bit words of data + 8 bytes timestamp */
145149

146150
mutex_lock(&adc->lock);
147151

148-
if (adc084s021_adc_conversion(adc, &data) < 0)
152+
if (adc084s021_adc_conversion(adc, adc->scan.channels) < 0)
149153
dev_err(&adc->spi->dev, "Failed to read data\n");
150154

151-
iio_push_to_buffers_with_timestamp(indio_dev, data,
155+
iio_push_to_buffers_with_timestamp(indio_dev, &adc->scan,
152156
iio_get_time_ns(indio_dev));
153157
mutex_unlock(&adc->lock);
154158
iio_trigger_notify_done(indio_dev->trig);

0 commit comments

Comments
 (0)