From patchwork Mon Dec 28 12:51:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 352716 Delivered-To: patch@linaro.org Received: by 2002:a02:85a7:0:0:0:0:0 with SMTP id d36csp9829209jai; Mon, 28 Dec 2020 07:27:07 -0800 (PST) X-Google-Smtp-Source: ABdhPJxX6ZjoNY0thgaAZZr/2kELXvxBEIJ7nBios/M1y/9rCeNqmWTOu97NKtnupgvMEUJbBaxd X-Received: by 2002:a17:906:4e53:: with SMTP id g19mr41416738ejw.454.1609169227514; Mon, 28 Dec 2020 07:27:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609169227; cv=none; d=google.com; s=arc-20160816; b=uOYZPoIy1PHsVnLTJpWexjG6gNO2xWkjOKnojEfbvSnH44k63pGepJuO/f630gaeJd jAHtMaDAWVyJZhUsSVvyd8TplMggXczJSKDnkFhaqcsNpqdtv2nH8xoOLpUp81hJypdq 37e2IyBYHH3SE2cn8Z6N8wvpU/hC8pHznC/ksDsdVaIEKxyoSV8I+UOd+Ae5v0MwL/fr 3b4NAeU8MEFExISd13MvJKtRPFaYLId2CrJNc1AK1eov66rLGuPrcms3QvYAjzCNgb6n rT4rL69jwL/F3DZWfO49u+xkWynJK6yHbNwm7RpfLpcprimQkepd/rhzhPF8H2licNOh MOYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=dNbK44PWuBGcjDrju/H9bRfJPsshscZILy7igJWyqH4=; b=cepVprhnA+Nu39eh4LXepuyBsyW/Hb2Q76PDaeDWf0TIvTGCH1omgnbUDc16vMu9BJ P6W8PA9ottNrzA47/SMwjgQQ0ucjyLEm9TIhhFwz4tWqTsRvOLDQiq5RzXMet5dg05ob etp1oDIo668X9EDtJwp7NsX26tmscZbF2wFcT3dcugmXwi5KfE5KN9N4a4JZXViKrEt4 57mOgkjN8EZn5a/rk7OIJGqyMjYaAGXfqNJPtRfo2mQN7gm+/JnNCudp/F7lGv0owujD 69eXKftTM2iK8v7SyK8UdvmLTBacm2enFVWMfuzqNmbK8uUSjktxPLTuHNpRmMPtiijW Pm3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=rUzUaVkF; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q11si20313189edn.225.2020.12.28.07.27.07; Mon, 28 Dec 2020 07:27:07 -0800 (PST) Received-SPF: pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=rUzUaVkF; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2407491AbgL1N6F (ORCPT + 14 others); Mon, 28 Dec 2020 08:58:05 -0500 Received: from mail.kernel.org ([198.145.29.99]:59828 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2407502AbgL1N6D (ORCPT ); Mon, 28 Dec 2020 08:58:03 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 61DC1205CB; Mon, 28 Dec 2020 13:57:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1609163843; bh=MqYK4bByvyed3WeX+W3EJNVYkbjV7CdwuADB16FkQpw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rUzUaVkFPosAx8Fi6gE3DrHFDwfa1Pwn2QsAelAFO2+wPh+nV5LFz9YRprzDIAWS8 hpvxKaZOWZ5s6W4Muo77N/c+7AJOvktbjrmwArEI+/3sLo/tG/4pHdCkQjaMCSjc8t Q/Egpvea00zqbmL78GsbY7c4BBcAlRknHhuM4iqE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jonathan Cameron , Alexandru Ardelean , Mikko Koivunen , Stable@vger.kernel.org Subject: [PATCH 5.4 427/453] iio:light:rpr0521: Fix timestamp alignment and prevent data leak. Date: Mon, 28 Dec 2020 13:51:03 +0100 Message-Id: <20201228124957.772334772@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201228124937.240114599@linuxfoundation.org> References: <20201228124937.240114599@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Jonathan Cameron commit a61817216bcc755eabbcb1cf281d84ccad267ed1 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(). This data is allocated with kzalloc() so no data can leak apart from previous readings and in this case the status byte from the device. The forced alignment of ts is not necessary in this case but it potentially makes the code less fragile. >From personal communications with Mikko: We could probably split the reading of the int register, but it would mean a significant performance cost of 20 i2c clock cycles. Fixes: e12ffd241c00 ("iio: light: rpr0521 triggered buffer") Signed-off-by: Jonathan Cameron Reviewed-by: Alexandru Ardelean Cc: Mikko Koivunen Cc: Link: https://lore.kernel.org/r/20200920112742.170751-2-jic23@kernel.org Signed-off-by: Greg Kroah-Hartman --- drivers/iio/light/rpr0521.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) --- a/drivers/iio/light/rpr0521.c +++ b/drivers/iio/light/rpr0521.c @@ -194,6 +194,17 @@ struct rpr0521_data { bool pxs_need_dis; struct regmap *regmap; + + /* + * Ensure correct naturally aligned timestamp. + * Note that the read will put garbage data into + * the padding but this should not be a problem + */ + struct { + __le16 channels[3]; + u8 garbage; + s64 ts __aligned(8); + } scan; }; static IIO_CONST_ATTR(in_intensity_scale_available, RPR0521_ALS_SCALE_AVAIL); @@ -449,8 +460,6 @@ static irqreturn_t rpr0521_trigger_consu struct rpr0521_data *data = iio_priv(indio_dev); int err; - u8 buffer[16]; /* 3 16-bit channels + padding + ts */ - /* Use irq timestamp when reasonable. */ if (iio_trigger_using_own(indio_dev) && data->irq_timestamp) { pf->timestamp = data->irq_timestamp; @@ -461,11 +470,11 @@ static irqreturn_t rpr0521_trigger_consu pf->timestamp = iio_get_time_ns(indio_dev); err = regmap_bulk_read(data->regmap, RPR0521_REG_PXS_DATA, - &buffer, + data->scan.channels, (3 * 2) + 1); /* 3 * 16-bit + (discarded) int clear reg. */ if (!err) iio_push_to_buffers_with_timestamp(indio_dev, - buffer, pf->timestamp); + &data->scan, pf->timestamp); else dev_err(&data->client->dev, "Trigger consumer can't read from sensor.\n");