From patchwork Tue Nov 3 20:37:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Kroah-Hartman X-Patchwork-Id: 320461 Delivered-To: patch@linaro.org Received: by 2002:a92:7b12:0:0:0:0:0 with SMTP id w18csp4911854ilc; Tue, 3 Nov 2020 13:06:51 -0800 (PST) X-Google-Smtp-Source: ABdhPJwQ1MQaJRjemXseQkQyRR5Vi0vm5H9mP3toaaQ0xIcz3nB84N+v+p+8nlpTADdiuIaVJh1P X-Received: by 2002:a17:906:d1ce:: with SMTP id bs14mr21328796ejb.548.1604437611635; Tue, 03 Nov 2020 13:06:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604437611; cv=none; d=google.com; s=arc-20160816; b=YVeM+ygcydq4Gn3qwd1hbyg1huJZJiFw4CCyMOu0Heg3+kgQZfxjUp2oxww/2dFjCk /zrQgQWmSeOTj5LPTQ40LOJepMSrrO7riVUiOxaUhyIhquYjWDnNxFClZXYW2UH1DL2W 8CX6aDe/3gPT+FnwMpLSyKnc2br/UFp4nDUxdrwm0r1IYP+MYfPNX54cRcOcMO+ykQzN kzbsLQEbRjpfCjAqgs/TZYAhldoBkzJKsvL0kmqkPHzBiK3AOK78O6c1hCffRmF5JZtW 2l2s9O2jtrQpKGD6LUCiLJWxvoL22KnB9Bx0qsDFcQ1Dkpetnpi2mZv8LWcr71jNeKgW lYgw== 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=/aKrRcM50Nxah9v16YeTOLbFDzb9FYk5xELUWs3QdX4=; b=qmYM3sw/GC56nKOVekmu0LNf14A0sxOR7o7am4uulQRlNUlJPqsMy0pT23g8rAGum0 ba6V2LHNjTEOhCp+v9RxHwevk+UDb4p2gzqcc4BwvoJ2yVW/mUHSWXuQPWod0UrWgCVh 1IuMgLTbm2oLM55P5DUKXAiMLDdB4xTg0jk5CK1YEnAY6bvrHAHgifrDUKklZ3jpl8J+ KpchWSnjW9lzsFrtHGHiEbh+JFGR3u2DMj59T17mxXKek5K6AM0XtcmynvRxF+fKMZr1 Oe6cAP3otp8Z6/tIsAXD3kYv7gvqKHGsA274nR0PoRPvHVuNRxlwdZhMH/wuFWZUjLAY Sziw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=RrCw9LQM; 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=fail (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 i59si14031103edd.308.2020.11.03.13.06.51; Tue, 03 Nov 2020 13:06:51 -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=@kernel.org header.s=default header.b=RrCw9LQM; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388017AbgKCVGu (ORCPT + 15 others); Tue, 3 Nov 2020 16:06:50 -0500 Received: from mail.kernel.org ([198.145.29.99]:45856 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388218AbgKCVGt (ORCPT ); Tue, 3 Nov 2020 16:06:49 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C0EC9205ED; Tue, 3 Nov 2020 21:06:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604437608; bh=RFBUn9nQ9UEvmhsRqgxmIbzZdl7faCjeCRUgBX4r9YA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RrCw9LQMQLAyixm1zvegrngdfwTV7bjGy7wzz96MuqT0kZjYzRKjIlff4wbrW+Mm+ /dcymLTJ5U03tVRevGOtmmDCgS0x66OS+1Drj7XWvg66rkSVvikGqSKjAc0Rl6aK55 cBdhOLP8C1h2avB+cH7VixM+Wt+t2iKqfj2CDUM0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Lars-Peter Clausen , Jonathan Cameron , Andy Shevchenko , Peter Meerwald-Stadler , Stable@vger.kernel.org Subject: [PATCH 4.19 149/191] iio:light:si1145: Fix timestamp alignment and prevent data leak. Date: Tue, 3 Nov 2020 21:37:21 +0100 Message-Id: <20201103203246.658966400@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201103203232.656475008@linuxfoundation.org> References: <20201103203232.656475008@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 0456ecf34d466261970e0ff92b2b9c78a4908637 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 a 24 byte 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 appart from previous readings. Depending on the enabled channels, the location of the timestamp can be at various aligned offsets through the buffer. As such we any use of a structure to enforce this alignment would incorrectly suggest a single location for the timestamp. Comments adjusted to express this clearly in the code. Fixes: ac45e57f1590 ("iio: light: Add driver for Silabs si1132, si1141/2/3 and si1145/6/7 ambient light, uv index and proximity sensors") Reported-by: Lars-Peter Clausen Signed-off-by: Jonathan Cameron Reviewed-by: Andy Shevchenko Cc: Peter Meerwald-Stadler Cc: Link: https://lore.kernel.org/r/20200722155103.979802-9-jic23@kernel.org Signed-off-by: Greg Kroah-Hartman --- drivers/iio/light/si1145.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) --- a/drivers/iio/light/si1145.c +++ b/drivers/iio/light/si1145.c @@ -172,6 +172,7 @@ struct si1145_part_info { * @part_info: Part information * @trig: Pointer to iio trigger * @meas_rate: Value of MEAS_RATE register. Only set in HW in auto mode + * @buffer: Used to pack data read from sensor. */ struct si1145_data { struct i2c_client *client; @@ -183,6 +184,14 @@ struct si1145_data { bool autonomous; struct iio_trigger *trig; int meas_rate; + /* + * Ensure timestamp will be naturally aligned if present. + * Maximum buffer size (may be only partly used if not all + * channels are enabled): + * 6*2 bytes channels data + 4 bytes alignment + + * 8 bytes timestamp + */ + u8 buffer[24] __aligned(8); }; /** @@ -444,12 +453,6 @@ static irqreturn_t si1145_trigger_handle struct iio_poll_func *pf = private; struct iio_dev *indio_dev = pf->indio_dev; struct si1145_data *data = iio_priv(indio_dev); - /* - * Maximum buffer size: - * 6*2 bytes channels data + 4 bytes alignment + - * 8 bytes timestamp - */ - u8 buffer[24]; int i, j = 0; int ret; u8 irq_status = 0; @@ -482,7 +485,7 @@ static irqreturn_t si1145_trigger_handle ret = i2c_smbus_read_i2c_block_data_or_emulated( data->client, indio_dev->channels[i].address, - sizeof(u16) * run, &buffer[j]); + sizeof(u16) * run, &data->buffer[j]); if (ret < 0) goto done; j += run * sizeof(u16); @@ -497,7 +500,7 @@ static irqreturn_t si1145_trigger_handle goto done; } - iio_push_to_buffers_with_timestamp(indio_dev, buffer, + iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, iio_get_time_ns(indio_dev)); done: