Skip to content

Commit 54f82df

Browse files
committed
iio:adc:ti-adc081c 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 eplicit alignment of ts is necessary to ensure correct padding on x86_32 where s64 is only aligned to 4 bytes. Fixes: 08e05d1 ("ti-adc081c: Initial triggered buffer support") Reported-by: Lars-Peter Clausen <lars@metafoo.de> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com> Cc: <Stable@vger.kernel.org>
1 parent 02ad21c commit 54f82df

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

drivers/iio/adc/ti-adc081c.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ struct adc081c {
3333

3434
/* 8, 10 or 12 */
3535
int bits;
36+
37+
/* Ensure natural alignment of buffer elements */
38+
struct {
39+
u16 channel;
40+
s64 ts __aligned(8);
41+
} scan;
3642
};
3743

3844
#define REG_CONV_RES 0x00
@@ -128,14 +134,13 @@ static irqreturn_t adc081c_trigger_handler(int irq, void *p)
128134
struct iio_poll_func *pf = p;
129135
struct iio_dev *indio_dev = pf->indio_dev;
130136
struct adc081c *data = iio_priv(indio_dev);
131-
u16 buf[8]; /* 2 bytes data + 6 bytes padding + 8 bytes timestamp */
132137
int ret;
133138

134139
ret = i2c_smbus_read_word_swapped(data->i2c, REG_CONV_RES);
135140
if (ret < 0)
136141
goto out;
137-
buf[0] = ret;
138-
iio_push_to_buffers_with_timestamp(indio_dev, buf,
142+
data->scan.channel = ret;
143+
iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
139144
iio_get_time_ns(indio_dev));
140145
out:
141146
iio_trigger_notify_done(indio_dev->trig);

0 commit comments

Comments
 (0)