Skip to content

Commit 2684d50

Browse files
committed
iio:light:ltr501 Fix timestamp alignment issue.
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. Here we use a structure on the stack. The driver already did an explicit memset so no data leak was possible. Forced alignment of ts is not strictly necessary but probably makes the code slightly less fragile. Note there has been some rework in this driver of the years, so no way this will apply cleanly all the way back. Fixes: 2690be9 ("iio: Add Lite-On ltr501 ambient light / proximity sensor driver") 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 5236288 commit 2684d50

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

drivers/iio/light/ltr501.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,13 +1243,16 @@ static irqreturn_t ltr501_trigger_handler(int irq, void *p)
12431243
struct iio_poll_func *pf = p;
12441244
struct iio_dev *indio_dev = pf->indio_dev;
12451245
struct ltr501_data *data = iio_priv(indio_dev);
1246-
u16 buf[8];
1246+
struct {
1247+
u16 channels[3];
1248+
s64 ts __aligned(8);
1249+
} scan;
12471250
__le16 als_buf[2];
12481251
u8 mask = 0;
12491252
int j = 0;
12501253
int ret, psdata;
12511254

1252-
memset(buf, 0, sizeof(buf));
1255+
memset(&scan, 0, sizeof(scan));
12531256

12541257
/* figure out which data needs to be ready */
12551258
if (test_bit(0, indio_dev->active_scan_mask) ||
@@ -1268,20 +1271,20 @@ static irqreturn_t ltr501_trigger_handler(int irq, void *p)
12681271
if (ret < 0)
12691272
return ret;
12701273
if (test_bit(0, indio_dev->active_scan_mask))
1271-
buf[j++] = le16_to_cpu(als_buf[1]);
1274+
scan.channels[j++] = le16_to_cpu(als_buf[1]);
12721275
if (test_bit(1, indio_dev->active_scan_mask))
1273-
buf[j++] = le16_to_cpu(als_buf[0]);
1276+
scan.channels[j++] = le16_to_cpu(als_buf[0]);
12741277
}
12751278

12761279
if (mask & LTR501_STATUS_PS_RDY) {
12771280
ret = regmap_bulk_read(data->regmap, LTR501_PS_DATA,
12781281
&psdata, 2);
12791282
if (ret < 0)
12801283
goto done;
1281-
buf[j++] = psdata & LTR501_PS_DATA_MASK;
1284+
scan.channels[j++] = psdata & LTR501_PS_DATA_MASK;
12821285
}
12831286

1284-
iio_push_to_buffers_with_timestamp(indio_dev, buf,
1287+
iio_push_to_buffers_with_timestamp(indio_dev, &scan,
12851288
iio_get_time_ns(indio_dev));
12861289

12871290
done:

0 commit comments

Comments
 (0)