From patchwork Mon May 25 17:06:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jonathan Cameron X-Patchwork-Id: 11569123 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 598881392 for ; Mon, 25 May 2020 17:09:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4338920870 for ; Mon, 25 May 2020 17:09:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1590426571; bh=Wu6mvDbeHBOOgIe4UaMtENrclvA24EsKP2BStbE5TWg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=Y6Am+jXvX7MnrgVZax2zXqhyrB+Lx5KA5KfFSg/uuNvUQs3pPtWW0H2nXNVBEicfJ 0s6h+4rwMjH32GMuYD8jN++8YqoDybvJh3uL1oobC/VW6IA5StTom4DtgmEEtE/Vjj BK5JDHjyRulHh3EG7vTuuKukM+1J4RE38Tu3UmUY= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391309AbgEYRJa (ORCPT ); Mon, 25 May 2020 13:09:30 -0400 Received: from mail.kernel.org ([198.145.29.99]:42918 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391301AbgEYRJa (ORCPT ); Mon, 25 May 2020 13:09:30 -0400 Received: from localhost.localdomain (cpc149474-cmbg20-2-0-cust94.5-4.cable.virginm.net [82.4.196.95]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C6F6D2084C; Mon, 25 May 2020 17:09:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1590426569; bh=Wu6mvDbeHBOOgIe4UaMtENrclvA24EsKP2BStbE5TWg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mcZjXG3GarrUmMruwaGFLISccDimDCzXgmB3K60G43wIfa+WPnFziq0VtYTgS/R6s 1EfJpKfTjm59UPdOLMpxyFeYrr134JuxYPXxQTEjfDlXPWL9lrh7bkK0ME5MFyZVF3 iNST4mBaY1g+YXMu0RrhzcMsKYxb2SAUGIXxPZYI= From: Jonathan Cameron To: linux-iio@vger.kernel.org Cc: Jonathan Cameron , Lars-Peter Clausen , =?utf-8?q?Stefan_Br=C3=BCns?= , Marc Titinger Subject: [PATCH 24/25] iio:adc:ina2xx Fix timestamp alignment issue. Date: Mon, 25 May 2020 18:06:27 +0100 Message-Id: <20200525170628.503283-25-jic23@kernel.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200525170628.503283-1-jic23@kernel.org> References: <20200525170628.503283-1-jic23@kernel.org> MIME-Version: 1.0 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Jonathan Cameron 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 32 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 structure in the iio_priv() data with alignment explicitly requested. This data is allocated with kzalloc so no data can leak apart from previous readings. If we want this in older stables will need manual backport due to driver reworks. Fixes: c43a102e67db ("iio: ina2xx: add support for TI INA2xx Power Monitors") Reported-by: Lars-Peter Clausen Signed-off-by: Jonathan Cameron Cc: Stefan BrĂ¼ns Cc: Marc Titinger --- drivers/iio/adc/ina2xx-adc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/iio/adc/ina2xx-adc.c b/drivers/iio/adc/ina2xx-adc.c index bdd7cba6f6b0..ed2d069b791a 100644 --- a/drivers/iio/adc/ina2xx-adc.c +++ b/drivers/iio/adc/ina2xx-adc.c @@ -146,6 +146,8 @@ struct ina2xx_chip_info { int range_vbus; /* Bus voltage maximum in V */ int pga_gain_vshunt; /* Shunt voltage PGA gain */ bool allow_async_readout; + /* data buffer needs space for channel data and timestap */ + unsigned short data[4 + sizeof(s64)/sizeof(short)] __aligned(8); }; static const struct ina2xx_config ina2xx_config[] = { @@ -738,8 +740,6 @@ static int ina2xx_conversion_ready(struct iio_dev *indio_dev) static int ina2xx_work_buffer(struct iio_dev *indio_dev) { struct ina2xx_chip_info *chip = iio_priv(indio_dev); - /* data buffer needs space for channel data and timestap */ - unsigned short data[4 + sizeof(s64)/sizeof(short)]; int bit, ret, i = 0; s64 time; @@ -758,10 +758,10 @@ static int ina2xx_work_buffer(struct iio_dev *indio_dev) if (ret < 0) return ret; - data[i++] = val; + chip->data[i++] = val; } - iio_push_to_buffers_with_timestamp(indio_dev, data, time); + iio_push_to_buffers_with_timestamp(indio_dev, chip->data, time); return 0; };