From patchwork Tue Nov 3 20:37:44 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: 320465 Delivered-To: patch@linaro.org Received: by 2002:a92:7b12:0:0:0:0:0 with SMTP id w18csp4915164ilc; Tue, 3 Nov 2020 13:12:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJx38TcQg1Op3ez58LOfA+UjqvzxYT6zwi5wxfisF5OUBKAp4xcdCFlxff/OWy/v6zi4RKUW X-Received: by 2002:a05:6402:1c84:: with SMTP id cy4mr11017497edb.382.1604437920322; Tue, 03 Nov 2020 13:12:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604437920; cv=none; d=google.com; s=arc-20160816; b=YQjKj+8lyN+/3b3C7nysx9iQk5WgNRjrZC1JnZqlnn0vUI81+TbE4gEGVXFwLfRUzO 7I8hi1DPXKcD3EALmqKe7beOTghWOVa3QAmVDTZ9v/wfEsEw4fyrZYByyYuWLEW69ZTk cS6TJ13DlHAJ4X6BftQ7awBcD8Q30ZuAWmzBpXO46Lb+O8EGP8b+XcXbww7ThflFunn2 eyDOx7dHnRQGvWcc3AUzC4vQwTkq5i1mapeeCoj681kJIYXtkS8oo+RR23iOgqA6GXhS OdIddqixbYKVObsB14F+jGSS5WMkzPjvMmTTllxUOchf4Aid1kA8fxEDFTfj8571jHK9 LP6A== 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=ZzyeUjbb3sYDZBDpyNxEdxlROU4BsXbrXZS9X6py8/AEIJzvVN4Su/eeEHBz9FAVty f4+Xrcha6bVeKYz3HwZR7O22szvekv4m8RW3mREvqF4sGvJCvNOuJADxE+HWwX16X2Nz tNEXTXK1k2xtmWi4umDG43MIlGzattcCjw2iD3g2eluxPpnQ6GOBnJHTFG7gx+VpddZX MZMpLyPXx/UvzNfMxVIrNq2rpBluCTnIsBNGW3iUBOMWrVUSPT3oEvUYGpUEJ1/hiDRM HpqkalcFiBdF6fbio+yx4RAov29t0xYCRFqy3Wied7z4F98y0gt40/E8Pc9ghcyEmlsm GEXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=yNxYQBuG; 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 bs11si13945202edb.24.2020.11.03.13.12.00; Tue, 03 Nov 2020 13:12:00 -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=yNxYQBuG; 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 S2388950AbgKCVL7 (ORCPT + 15 others); Tue, 3 Nov 2020 16:11:59 -0500 Received: from mail.kernel.org ([198.145.29.99]:54024 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388945AbgKCVL6 (ORCPT ); Tue, 3 Nov 2020 16:11:58 -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 07F16206B5; Tue, 3 Nov 2020 21:11:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604437917; bh=RFBUn9nQ9UEvmhsRqgxmIbzZdl7faCjeCRUgBX4r9YA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yNxYQBuGi+jpJM3WDu8sLlcLyUUyIpCPn5/hcde/z01RnFjxcJEw2LMV2BHEqRgSc rgp4H/6AXqNXCUvulBacpLRzIwQfjLsD7TEKtEtSlY+o539ava5syu/XtnpDV4t1uo PRyXNnzVFgxMx1lMW1qMscz1Y+Z8X65MklgKnrL0= 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.14 087/125] iio:light:si1145: Fix timestamp alignment and prevent data leak. Date: Tue, 3 Nov 2020 21:37:44 +0100 Message-Id: <20201103203209.479553219@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201103203156.372184213@linuxfoundation.org> References: <20201103203156.372184213@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: