[v3,08/27] iio:light:si1145: Fix timestamp alignment and prevent data leak.
diff mbox series

Message ID 20200722155103.979802-9-jic23@kernel.org
State New
Headers show
Series
  • IIO: Fused set 1 and 2 of timestamp alignment fixes
Related show

Commit Message

Jonathan Cameron July 22, 2020, 3:50 p.m. UTC
From: Jonathan Cameron <Jonathan.Cameron@huawei.com>

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 a 24 byte 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 array in the iio_priv() data with alignment
explicitly requested.  This data is allocated with kzalloc so no
data can leak appart from previous readings.

Depending on the enabled channels, the  location of the timestamp
can be at various aligned offsets through the buffer.  As such we
any use of a structure to enforce this alignment would incorrectly
suggest a single location for the timestamp.

Fixes: ac45e57f1590 ("iio: light: Add driver for Silabs si1132, si1141/2/3 and si1145/6/7 ambient light, uv index and proximity sensors")
Reported-by: Lars-Peter Clausen <lars@metafoo.de>
Cc: Peter Meerwald-Stadler <pmeerw@pmeerw.net>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
---
 drivers/iio/light/si1145.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

Comments

Andy Shevchenko July 22, 2020, 7:43 p.m. UTC | #1
On Wed, Jul 22, 2020 at 6:53 PM Jonathan Cameron <jic23@kernel.org> wrote:
>
> From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
>
> 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 a 24 byte 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 array in the iio_priv() data with alignment
> explicitly requested.  This data is allocated with kzalloc so no
> data can leak appart from previous readings.
>
> Depending on the enabled channels, the  location of the timestamp
> can be at various aligned offsets through the buffer.  As such we
> any use of a structure to enforce this alignment would incorrectly
> suggest a single location for the timestamp.

...

> +       /* Ensure timestamp will be naturally aligned if present */
> +       u8 buffer[24] __aligned(8);

Why can't we use proper structure here?

> @@ -445,7 +447,6 @@ static irqreturn_t si1145_trigger_handler(int irq, void *private)
>          *   6*2 bytes channels data + 4 bytes alignment +
>          *   8 bytes timestamp
>          */
> -       u8 buffer[24];

Seems even the old comment shows how it should look like...
Andy Shevchenko July 22, 2020, 7:45 p.m. UTC | #2
On Wed, Jul 22, 2020 at 10:43 PM Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
>
> On Wed, Jul 22, 2020 at 6:53 PM Jonathan Cameron <jic23@kernel.org> wrote:
> >
> > From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> >
> > 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 a 24 byte 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 array in the iio_priv() data with alignment
> > explicitly requested.  This data is allocated with kzalloc so no
> > data can leak appart from previous readings.
> >
> > Depending on the enabled channels, the  location of the timestamp
> > can be at various aligned offsets through the buffer.  As such we
> > any use of a structure to enforce this alignment would incorrectly
> > suggest a single location for the timestamp.
>
> ...
>
> > +       /* Ensure timestamp will be naturally aligned if present */
> > +       u8 buffer[24] __aligned(8);
>
> Why can't we use proper structure here?
>
> > @@ -445,7 +447,6 @@ static irqreturn_t si1145_trigger_handler(int irq, void *private)
> >          *   6*2 bytes channels data + 4 bytes alignment +
> >          *   8 bytes timestamp
> >          */
> > -       u8 buffer[24];
>
> Seems even the old comment shows how it should look like...

I think I understand now. Basically it's a dynamic amount of channels
(up to 6) before you get a timestamp.
Jonathan Cameron July 23, 2020, 11:25 a.m. UTC | #3
On Wed, 22 Jul 2020 22:45:59 +0300
Andy Shevchenko <andy.shevchenko@gmail.com> wrote:

> On Wed, Jul 22, 2020 at 10:43 PM Andy Shevchenko
> <andy.shevchenko@gmail.com> wrote:
> >
> > On Wed, Jul 22, 2020 at 6:53 PM Jonathan Cameron <jic23@kernel.org> wrote:  
> > >
> > > From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> > >
> > > 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 a 24 byte 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 array in the iio_priv() data with alignment
> > > explicitly requested.  This data is allocated with kzalloc so no
> > > data can leak appart from previous readings.
> > >
> > > Depending on the enabled channels, the  location of the timestamp
> > > can be at various aligned offsets through the buffer.  As such we
> > > any use of a structure to enforce this alignment would incorrectly
> > > suggest a single location for the timestamp.  
> >
> > ...
> >  
> > > +       /* Ensure timestamp will be naturally aligned if present */
> > > +       u8 buffer[24] __aligned(8);  
> >
> > Why can't we use proper structure here?
> >  
> > > @@ -445,7 +447,6 @@ static irqreturn_t si1145_trigger_handler(int irq, void *private)
> > >          *   6*2 bytes channels data + 4 bytes alignment +
> > >          *   8 bytes timestamp
> > >          */
> > > -       u8 buffer[24];  
> >
> > Seems even the old comment shows how it should look like...  
> 
> I think I understand now. Basically it's a dynamic amount of channels
> (up to 6) before you get a timestamp.
> 
Exactly.  Comment is giving the largest it can be, not what is needed for
a given configuration of the device.

Should indeed drop that comment.  Obviously went into automated mode and stopped
actually reading what was in front of me.

Jonathan
Jonathan Cameron Sept. 19, 2020, 3:46 p.m. UTC | #4
On Thu, 23 Jul 2020 12:25:17 +0100
Jonathan Cameron <Jonathan.Cameron@Huawei.com> wrote:

> On Wed, 22 Jul 2020 22:45:59 +0300
> Andy Shevchenko <andy.shevchenko@gmail.com> wrote:
> 
> > On Wed, Jul 22, 2020 at 10:43 PM Andy Shevchenko
> > <andy.shevchenko@gmail.com> wrote:  
> > >
> > > On Wed, Jul 22, 2020 at 6:53 PM Jonathan Cameron <jic23@kernel.org> wrote:    
> > > >
> > > > From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> > > >
> > > > 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 a 24 byte 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 array in the iio_priv() data with alignment
> > > > explicitly requested.  This data is allocated with kzalloc so no
> > > > data can leak appart from previous readings.
> > > >
> > > > Depending on the enabled channels, the  location of the timestamp
> > > > can be at various aligned offsets through the buffer.  As such we
> > > > any use of a structure to enforce this alignment would incorrectly
> > > > suggest a single location for the timestamp.    
> > >
> > > ...
> > >    
> > > > +       /* Ensure timestamp will be naturally aligned if present */
> > > > +       u8 buffer[24] __aligned(8);    
> > >
> > > Why can't we use proper structure here?
> > >    
> > > > @@ -445,7 +447,6 @@ static irqreturn_t si1145_trigger_handler(int irq, void *private)
> > > >          *   6*2 bytes channels data + 4 bytes alignment +
> > > >          *   8 bytes timestamp
> > > >          */
> > > > -       u8 buffer[24];    
> > >
> > > Seems even the old comment shows how it should look like...    
> > 
> > I think I understand now. Basically it's a dynamic amount of channels
> > (up to 6) before you get a timestamp.
> >   
> Exactly.  Comment is giving the largest it can be, not what is needed for
> a given configuration of the device.
> 
> Should indeed drop that comment.  Obviously went into automated mode and stopped
> actually reading what was in front of me.

I've adjusted the comment as requested by Andy (and moved it!).  Fits
under Andy's class 2 so applied to the togreg branch of iio.git and marked
for stable.  No great rush for this, beyond the fact that I'll keep forgetting
to actually sort these out!

Thanks,

Jonathan

> 
> Jonathan
> 
>

Patch
diff mbox series

diff --git a/drivers/iio/light/si1145.c b/drivers/iio/light/si1145.c
index 155faaea8c72..e8bdc221d65b 100644
--- a/drivers/iio/light/si1145.c
+++ b/drivers/iio/light/si1145.c
@@ -179,6 +179,8 @@  struct si1145_data {
 	bool autonomous;
 	struct iio_trigger *trig;
 	int meas_rate;
+	/* Ensure timestamp will be naturally aligned if present */
+	u8 buffer[24] __aligned(8);
 };
 
 /*
@@ -445,7 +447,6 @@  static irqreturn_t si1145_trigger_handler(int irq, void *private)
 	 *   6*2 bytes channels data + 4 bytes alignment +
 	 *   8 bytes timestamp
 	 */
-	u8 buffer[24];
 	int i, j = 0;
 	int ret;
 	u8 irq_status = 0;
@@ -478,7 +479,7 @@  static irqreturn_t si1145_trigger_handler(int irq, void *private)
 
 		ret = i2c_smbus_read_i2c_block_data_or_emulated(
 				data->client, indio_dev->channels[i].address,
-				sizeof(u16) * run, &buffer[j]);
+				sizeof(u16) * run, &data->buffer[j]);
 		if (ret < 0)
 			goto done;
 		j += run * sizeof(u16);
@@ -493,7 +494,7 @@  static irqreturn_t si1145_trigger_handler(int irq, void *private)
 			goto done;
 	}
 
-	iio_push_to_buffers_with_timestamp(indio_dev, buffer,
+	iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
 		iio_get_time_ns(indio_dev));
 
 done: