From patchwork Mon Dec 28 12:50:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 352647 Delivered-To: patch@linaro.org Received: by 2002:a02:85a7:0:0:0:0:0 with SMTP id d36csp9752369jai; Mon, 28 Dec 2020 05:37:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJyyD9uejeQMtwz/wfEQA/ZU4EjsKPmYcAr5oY0Nb+at67UQWWL7O0RY+GmqJq3utcQEhpDf X-Received: by 2002:a05:6402:8da:: with SMTP id d26mr40831525edz.157.1609162658196; Mon, 28 Dec 2020 05:37:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609162658; cv=none; d=google.com; s=arc-20160816; b=GlX7a8/w3qe7uJQappS2609z7YW6CAsu1MPz7FkEwckWWmBYV/sXbdTDJlzT+1AZbs IS4dW1BfIerHfvZYqsEGQluZaCV2T7tfsqhkTsG31xsjAYnSEGGEO/NdFxxO/5EVJWwz tUWowMBcWe9D2sO/Xgzjx8+Sb9Vxkk+TQhRs6Y0ODODZdGWnSoAyjAhIp8Zt9otpPVsq uGGiSqtqRTHsHcZBUVjJTrPG7OdzgGk316ro5enxtP1lWnGoPVohY6oT88C8Va6cN5wk nXpm1tCqP7hsGzqTZfNG6m/jBW1Ld3WNwONiUxRGZ2PNR8lvshec4E0TlfT79Hb7nDr7 4NqA== 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=jGw8klfqWwnHuqpPZjTlUgFZnagSEmkpeSQFNAxILJpficbBkOvJA0dyqrWLftKqIn pkzIwACUMZVXiswN12GYJdaFOXnjebeLn+QKcnudDNdCVOl9fRDPw7GNNzGbcbf+LZ1J Y0y7ilwvdm609w4ejsIfRQ57gWpVhLHqz3a2lTHucxyLz1uqMzv471k9g7sEVwg0kaJh y7rw48FQinPwq3Vi2uu3IASYS5FE0JCKIBD4yRhLz0YfOtksjLEE3H6ROKUZpsp+3lnk N9rix8H0Jzfw5ciQA7fpzXJdlQdybNhVsSXkRToXUCzU5pi3odGcFGxyCJV15kX5BaNi d/Ng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=AQYE9Ai3; 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 w1si7294602ejn.628.2020.12.28.05.37.38; Mon, 28 Dec 2020 05:37:38 -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=AQYE9Ai3; 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 S2390226AbgL1NgQ (ORCPT + 14 others); Mon, 28 Dec 2020 08:36:16 -0500 Received: from mail.kernel.org ([198.145.29.99]:36392 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391342AbgL1NgH (ORCPT ); Mon, 28 Dec 2020 08:36:07 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id F0B76206ED; Mon, 28 Dec 2020 13:35:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1609162526; bh=A62BzNAEG3QD8Ns32oFi/Og+zphtgr6XcCjfSz6IDV4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AQYE9Ai35f4rOdkk/S6cK2ynW5nSOmM1RWp0AYJLfFrig5o53zdv/nat80dcINPbE RSCAVqeRK1pSppLqehwUNhZddPHO1o3Z2YMa9UWR3djfyzmPbYTkpAH6qMCYs9Hrhz xJZhgstGSae0F5iszK1gFwnT685eLl/NoBPHbfEc= 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.19 330/346] iio:light:rpr0521: Fix timestamp alignment and prevent data leak. Date: Mon, 28 Dec 2020 13:50:49 +0100 Message-Id: <20201228124935.728053862@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201228124919.745526410@linuxfoundation.org> References: <20201228124919.745526410@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");