linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Lars-Peter Clausen <lars@metafoo.de>
Cc: "Nuno Sá" <noname.nuno@gmail.com>, "Nuno Sá" <nuno.sa@analog.com>,
	linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
	"Hartmut Knaack" <knaack.h@gmx.de>,
	"Peter Meerwald-Stadler" <pmeerw@pmeerw.net>,
	"Rob Herring" <robh+dt@kernel.org>,
	"Mark Rutland" <mark.rutland@arm.com>,
	"Alexandru Ardelean" <alexandru.Ardelean@analog.com>,
	"Michael Hennerich" <Michael.Hennerich@analog.com>
Subject: Re: [PATCH v5 5/6] iio: imu: Add support for adis16475
Date: Sun, 17 May 2020 17:15:23 +0100	[thread overview]
Message-ID: <20200517171523.54fa6deb@archlinux> (raw)
In-Reply-To: <b7289aca-f393-faca-f512-a952a77c1e68@metafoo.de>

On Sun, 3 May 2020 11:12:34 +0200
Lars-Peter Clausen <lars@metafoo.de> wrote:

> On 5/3/20 11:07 AM, Lars-Peter Clausen wrote:
> > On 5/3/20 10:47 AM, Jonathan Cameron wrote:  
> >> On Sat, 02 May 2020 21:52:18 +0200
> >> Nuno Sá <noname.nuno@gmail.com> wrote:
> >>  
> >>> On Sat, 2020-05-02 at 20:01 +0200, Lars-Peter Clausen wrote:  
> >>>> On 5/2/20 7:40 PM, Jonathan Cameron wrote:  
> >>>>> On Mon, 27 Apr 2020 20:06:07 +0200
> >>>>> Lars-Peter Clausen <lars@metafoo.de> wrote:  
> >>>>>> On 4/13/20 10:24 AM, Nuno Sá wrote:  
> >>>>>>> [...]
> >>>>>>> +static irqreturn_t adis16475_trigger_handler(int irq, void *p)
> >>>>>>> +{
> >>>>>>> [...]
> >>>>>>> +    __be16 data[ADIS16475_MAX_SCAN_DATA], *buffer;
> >>>>>>> [...]
> >>>>>>> +
> >>>>>>> +    iio_push_to_buffers_with_timestamp(indio_dev, data, pf-  
> >>>>>>>> timestamp);  
> >>>>>> If the timestamp is enabled the IIO core might insert padding
> >>>>>> between
> >>>>>> the data channels and the timestamp. If that happens this will
> >>>>>> disclose
> >>>>>> kernel stack memory to userspace.
> >>>>>>
> >>>>>> This needs either a memset(data, 0x00, sizeof(data)) or maybe put
> >>>>>> data
> >>>>>> into the state struct and kzalloc it.  
> >>>>> Good spot. Could simply do __be16 data[ADI..] = {0}; rather than
> >>>>> explicit
> >>>>> memset, but some form of zeroization is needed.
> >>>>>
> >>>>> I've fixed up the applied patch with the above approach.  
> >>>> There is actually another issue. The stack data is not necessarily
> >>>> aligned to 64 bit, which causes issues if we try to put the 64-bit  
> >>> Oh, this is actually more problematic. Yes, since we have an array of
> >>> u16, that is not guaranteed to be 64bit aligned. Doing a quick search
> >>> of `iio_push_to_buffers_with_timestamp()` users and I could quickly
> >>> find 4/5 drivers with the same problem. I guess the API should clearly
> >>> state that `data` needs to be __at least__ 64 bits aligned (maybe a
> >>> future patch). Or we could even check the address and guarantee that it
> >>> is properly aligned before continuing (though Im guessing this will
> >>> break a lot of users...)  
> >>>> timestamp in it. I think data should really be in the state struct.  
> >>> Yes, with a proper __aligned(8) attribute... Or couldn't we just use
> >>> __aligned(8) on the stack variable?  
> >> Forcing alignment on the stack isn't terribly reliable, which is why
> >> we never do that for dma safe buffers.
> >>
> >> Probably better to just move it to the state structure.
> >> I'll fix it up to do that. Please sanity check what will shortly
> >> be in the testing branch.
> >>
> >> The moment Lars mentioned this I groaned. As you've noted a few other
> >> drivers have the same problem + the ABI doesn't clearly state
> >> or check this.
> >>
> >> We should certainly fix all the drivers that suffer this problem
> >> first then we can think about adding a runtime check.  
> >
> > It looks like it is actually quite a few drivers, maybe we should 
> > switch to put_unaligned(). We probably got lucky in most cases and the 
> > buffer is naturally aligned to 64 bit.

Just a quick update on this.  I've been taking a deeper look and there
are some 'interesting' cases in here so the put_unaligned is attractive
unfortunately I don't think we can go that way because it would be
reasonable for consumers of the buffer to expect it to be appropriately
aligned.   We need to rework many of these anyway to fix the related
data leak.

I've done some of below and will post shortly - a few will take more
effort and probably need testing rather than just relying on review.

So far the 'interesting ones' are mpu3050 and isl29501.

J

> >
> > But the reason I noticed this is because I ran into the issue in the 
> > wild where the timestamp ended up at the wrong offset in the buffer, 
> > so it does happen.
> >
> > The following semantic patch finds affected drivers.
> >
> > @@
> > type T;
> > identifier buf;
> > expression N;
> > expression ts;
> > expression indio_dev;
> > @@
> > *T buf[N];
> > ...
> > *iio_push_to_buffers_with_timestamp(indio_dev, buf, ts)
> >
> > Matched files:
> >
> > --- drivers/iio/health/afe4403.c
> > --- drivers/iio/health/afe4404.c
> > --- drivers/iio/gyro/mpu3050-core.c
> > --- drivers/iio/gyro/itg3200_buffer.c
> > --- drivers/iio/chemical/ccs811.c
> > --- drivers/iio/chemical/sps30.c
> > --- drivers/iio/chemical/pms7003.c
> > --- drivers/iio/proximity/isl29501.c
> > --- drivers/iio/proximity/mb1232.c
> > --- drivers/iio/accel/kxsd9.c
> > --- drivers/iio/accel/mma8452.c
> > --- drivers/iio/accel/bmc150-accel-core.c
> > --- drivers/iio/accel/mma7455_core.c
> > --- drivers/iio/adc/ti-adc081c.c
> > --- drivers/iio/adc/ti-adc084s021.c
> > --- drivers/iio/adc/ti-ads1015.c
> > --- drivers/iio/adc/ti-ads124s08.c
> > --- drivers/iio/adc/ina2xx-adc.c
> > --- drivers/iio/adc/ti-ads8688.c
> > --- drivers/iio/adc/ti-adc0832.c
> > --- drivers/iio/adc/ti-adc12138.c
> > --- drivers/iio/adc/max1118.c
> > --- drivers/iio/adc/ad_sigma_delta.c
> > --- drivers/iio/light/si1145.c
> > --- drivers/iio/light/vcnl4035.c
> > --- drivers/iio/light/max44000.c
> > --- drivers/iio/light/rpr0521.c
> > --- drivers/iio/light/st_uvis25_core.c
> > --- drivers/iio/light/ltr501.c
> > --- drivers/iio/magnetometer/ak8974.c
> > --- drivers/iio/magnetometer/mag3110.c
> > --- drivers/iio/magnetometer/ak8975.c
> > --- drivers/iio/humidity/hdc100x.c
> > --- drivers/iio/humidity/hts221_buffer.c
> > --- drivers/iio/imu/bmi160/bmi160_core.c
> > --- drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c
> > --- drivers/iio/imu/inv_mpu6050/inv_mpu_ring.c
> > --- drivers/iio/pressure/ms5611_core.c
> > --- drivers/iio/pressure/mpl3115.c
> >  
> Most of these drivers also seem to have the issue that they leak stack 
> memory. Only 11 of them clear the buffer.
> 


  reply	other threads:[~2020-05-17 16:15 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-13  8:24 [PATCH v5 0/6] Support ADIS16475 and similar IMUs Nuno Sá
2020-04-13  8:24 ` [PATCH v5 1/6] iio: imu: adis: Add Managed device functions Nuno Sá
2020-04-13  8:24 ` [PATCH v5 2/6] iio: imu: adis: Add irq flag variable Nuno Sá
2020-04-13  8:24 ` [PATCH v5 3/6] iio: adis: Add adis_update_bits() APIs Nuno Sá
2020-04-13  8:24 ` [PATCH v5 4/6] iio: adis: Support different burst sizes Nuno Sá
2020-04-13  8:24 ` [PATCH v5 5/6] iio: imu: Add support for adis16475 Nuno Sá
2020-04-27 18:06   ` Lars-Peter Clausen
2020-05-02 17:40     ` Jonathan Cameron
2020-05-02 18:01       ` Lars-Peter Clausen
2020-05-02 19:52         ` Nuno Sá
2020-05-03  8:47           ` Jonathan Cameron
2020-05-03  9:07             ` Lars-Peter Clausen
2020-05-03  9:12               ` Lars-Peter Clausen
2020-05-17 16:15                 ` Jonathan Cameron [this message]
2020-05-17 19:07                   ` Lars-Peter Clausen
2020-05-18 16:29                     ` Jonathan Cameron
2020-05-03 11:07               ` Nuno Sá
2020-05-03 11:29                 ` Jonathan Cameron
2020-05-03 17:19                   ` Jonathan Cameron
2020-05-03 17:55                     ` Nuno Sá
2020-04-13  8:24 ` [PATCH v5 6/6] dt-bindings: iio: Add adis16475 documentation Nuno Sá
2020-04-20 19:17   ` Rob Herring
2020-04-13 16:27 ` [PATCH v5 0/6] Support ADIS16475 and similar IMUs Jonathan Cameron
2020-04-25 15:18   ` Jonathan Cameron

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200517171523.54fa6deb@archlinux \
    --to=jic23@kernel.org \
    --cc=Michael.Hennerich@analog.com \
    --cc=alexandru.Ardelean@analog.com \
    --cc=devicetree@vger.kernel.org \
    --cc=knaack.h@gmx.de \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=noname.nuno@gmail.com \
    --cc=nuno.sa@analog.com \
    --cc=pmeerw@pmeerw.net \
    --cc=robh+dt@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).