From patchwork Mon Dec 28 12:50:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 352746 Delivered-To: patch@linaro.org Received: by 2002:a17:906:4755:0:0:0:0 with SMTP id j21csp10020577ejs; Mon, 28 Dec 2020 08:16:53 -0800 (PST) X-Google-Smtp-Source: ABdhPJxn6kwbr8c3RzVuiJQFhYzlJUimpYCOL7eXUG2A5SD9IMf0KHDLdBgR4oBUeze3szFviZzt X-Received: by 2002:a17:906:68d1:: with SMTP id y17mr42478564ejr.447.1609172126138; Mon, 28 Dec 2020 08:15:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609172126; cv=none; d=google.com; s=arc-20160816; b=CJ6YkVaLXmibsiQbPQAGwswDaKN4eNiPwODm6z5tkaqsP7Fkf2QDkLlhwA5N9UyKME Vcztv6zRrEje8ABOy4XZZpksJ7ggVsYcujdzzk5V7fy6NAqebqZXvvbvmmCkF6UmONOU YY2ZXb149q7dBkCYETPxpkBXugcnCjm2nbZNwll5HvcT7KaLrf1Ib4jDL9+7aD+PJRK2 xQLGxHTEpJdns0fPbeK1MnxTabz3w51Lh/dpcAGPHAGe5KehCmjwCZpB7wkWRnc3rip+ Qk+kSNwgGekuqkK7gEkDA8sYq/LSCoNNSM/zaaF8IZkPUGA7luzkx8Zdpnv3+CQ25xXN Ho2Q== 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=ILeJfyaUkRT4alEQwcsh+JQFWGAtieioZZnzAW8+FS0=; b=ptTwiXUNjvHLAZAUcegOpgEGVart9fJQoohosFMQo5e7/KsIbeY432y8TtozgVlE6a UjkW+lsrunPGzrTMNkE6RArnRKW97JdOqAlg5NsRO9h9XQ3r1EYF7G53jwv7/FrBpt57 jMm1WcQpTz056KuhbsGeigJGrFxVsMw4AqIWRvhqL3dLPvsXh5e0teE02zeGHw8LgrOv jcSZ8lRVt7iH7iEPj+FFBlPSjuxQUxQU0Z+H+eRU0SEBC3wG2Srv6J97vOpLRgPBzUGA GAwieNNetFJ7AJeYsY0xD1bEhZqLXNLgaE3bUGv0dLQNT6OyZ4cyQSKSZpL/DDbEp71V MYQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=kUGbRhVr; 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 jg21si6504222ejc.418.2020.12.28.08.15.25; Mon, 28 Dec 2020 08:15:26 -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=kUGbRhVr; 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 S1732481AbgL1NS0 (ORCPT + 14 others); Mon, 28 Dec 2020 08:18:26 -0500 Received: from mail.kernel.org ([198.145.29.99]:46666 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387499AbgL1NS0 (ORCPT ); Mon, 28 Dec 2020 08:18:26 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id F17392076D; Mon, 28 Dec 2020 13:18:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1609161490; bh=A62BzNAEG3QD8Ns32oFi/Og+zphtgr6XcCjfSz6IDV4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kUGbRhVrY5tgtpuPbBuFmkAqi04TOEcbGzXLXZVBXVjNqCu+MsYlm8aGUSeRdP6jU kzs+RD/jzc7jfPLPqV1OBa6FZ9OMKW9iuwA1nln1Or6aeZNmGbZe7WE4Uh9M7QLPOu AAWnlj/kTLxL9g4jT1Ao/3wgWdBHiYxyNTzn5mnY= 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 4.14 230/242] iio:light:rpr0521: Fix timestamp alignment and prevent data leak. Date: Mon, 28 Dec 2020 13:50:35 +0100 Message-Id: <20201228124916.000319871@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201228124904.654293249@linuxfoundation.org> References: <20201228124904.654293249@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 @@ -197,6 +197,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); @@ -452,8 +463,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; @@ -464,11 +473,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");