]> git.ipfire.org Git - people/arne_f/kernel.git/commitdiff
iio:magnetometer:ak8975 Fix alignment and data leak issues.
authorJonathan Cameron <Jonathan.Cameron@huawei.com>
Wed, 22 Jul 2020 15:50:49 +0000 (16:50 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 23 Sep 2020 08:46:29 +0000 (10:46 +0200)
commit 02ad21cefbac4d89ac443866f25b90449527737b 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() data.

This data is allocated with kzalloc so no data can leak apart from
previous readings.

The explicit alignment of ts is not necessary in this case as by
coincidence the padding will end up the same, however I consider
it to make the code less fragile and have included it.

Fixes: bc11ca4a0b84 ("iio:magnetometer:ak8975: triggered buffer support")
Reported-by: Lars-Peter Clausen <lars@metafoo.de>
Cc: Gregor Boirie <gregor.boirie@parrot.com>
Cc: Linus Walleij <linus.walleij@linaro.org>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: <Stable@vger.kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/iio/magnetometer/ak8975.c

index 4ff883942f7b7581dc686ac6d600fe6e4eb91539..b08bd99295699c636c6496d7954a5a3efcaf5340 100644 (file)
@@ -381,6 +381,12 @@ struct ak8975_data {
        struct iio_mount_matrix orientation;
        struct regulator        *vdd;
        struct regulator        *vid;
+
+       /* Ensure natural alignment of timestamp */
+       struct {
+               s16 channels[3];
+               s64 ts __aligned(8);
+       } scan;
 };
 
 /* Enable attached power regulator if any. */
@@ -815,7 +821,6 @@ static void ak8975_fill_buffer(struct iio_dev *indio_dev)
        const struct i2c_client *client = data->client;
        const struct ak_def *def = data->def;
        int ret;
-       s16 buff[8]; /* 3 x 16 bits axis values + 1 aligned 64 bits timestamp */
        __le16 fval[3];
 
        mutex_lock(&data->lock);
@@ -838,12 +843,13 @@ static void ak8975_fill_buffer(struct iio_dev *indio_dev)
        mutex_unlock(&data->lock);
 
        /* Clamp to valid range. */
-       buff[0] = clamp_t(s16, le16_to_cpu(fval[0]), -def->range, def->range);
-       buff[1] = clamp_t(s16, le16_to_cpu(fval[1]), -def->range, def->range);
-       buff[2] = clamp_t(s16, le16_to_cpu(fval[2]), -def->range, def->range);
+       data->scan.channels[0] = clamp_t(s16, le16_to_cpu(fval[0]), -def->range, def->range);
+       data->scan.channels[1] = clamp_t(s16, le16_to_cpu(fval[1]), -def->range, def->range);
+       data->scan.channels[2] = clamp_t(s16, le16_to_cpu(fval[2]), -def->range, def->range);
 
-       iio_push_to_buffers_with_timestamp(indio_dev, buff,
+       iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
                                           iio_get_time_ns(indio_dev));
+
        return;
 
 unlock: