Re: [PATCH 18/32] iio:imu:bmi160 Fix alignment and data leak issues

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, 8 Jun 2020 16:17:12 +0300
Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx> wrote:

> On Sun, Jun 07, 2020 at 04:53:54PM +0100, Jonathan Cameron wrote:
> > From: Jonathan Cameron <Jonathan.Cameron@xxxxxxxxxx>
> > 
> > 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 array in the iio_priv() data with alignment
> > explicitly requested.  This data is allocated with kzalloc so no
> > data can leak apart from previous readings.  
> 
> > +	/* Ensure natural alignment for timestamp if present */
> > +	__le16 buf[16] __aligned(8);  
> 
> Perhaps struct in all such cases, like
> 
>  struct scan {
> 	 __le16 buf[3 * 3]; // 3 axis per 3 sensors
> 	 s64 ts; __aligned(8);
>  };
> 
> ?
> 

I did that for all the cases where the timestamp could only appear in
one location, which are those with 8 bytes or less of channels + any
which don't have flexible channel configs.

Unfortunately that's not always the case in this driver.  It depends on how
many channels are turned on.   As they are 2 bytes each, if you have
only 1-4 channels enabled, then the timestamp will start at 8 bytes in.
If you have 5-8 channels enabled it'll be 8 bytes in, if you have 9 channels
it will be 24 bytes in.

I thought about just ignoring that and pretending the timestamp was always
in the same place but my thought was that we'd be implying something
false and weird bugs lie down that sort of route.

We could in theory do

union {
	struct {
		__le16 buf[3 * 3];
		s64 ts __aligned(8) 
	};
	struct {
		__le16 buf[4];
		s64 ts __aligned(8);
	} short1;
	struct {
		__le16 buf[8];
		s64 ts __aligned(8);
	} short2;	
} scan;

But I guess you can see why I didn't go that route.

Jonathan






[Index of Archives]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Input]     [Linux Kernel]     [Linux SCSI]     [X.org]

  Powered by Linux