From patchwork Sun Sep 20 11:27:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Cameron X-Patchwork-Id: 11787469 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 922ED6CB for ; Sun, 20 Sep 2020 11:28:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7992A235F9 for ; Sun, 20 Sep 2020 11:28:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600601308; bh=k2jtqCM5EMsFrg6mZaM/3bhv5y3IDsm43+pl2qUen6c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=KDwOzXq9aLwDug4d5WPB60tSmvdwLu0exG8FJMo4HbZvwpuObzhZpgJpTDu+J+P12 3yJmsPBKY/oDXPvo601MzRA55CHyCB78kCpEUBixlydBekPT+FHm2Iq4rhmYF/k7Nf i61HZF11z5VINrJhnmcQ59kDOa4+iXF0dCoVC890= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726290AbgITL22 (ORCPT ); Sun, 20 Sep 2020 07:28:28 -0400 Received: from mail.kernel.org ([198.145.29.99]:39466 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726250AbgITL21 (ORCPT ); Sun, 20 Sep 2020 07:28:27 -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 56565222BB; Sun, 20 Sep 2020 11:28:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600601307; bh=k2jtqCM5EMsFrg6mZaM/3bhv5y3IDsm43+pl2qUen6c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=l8iS+Eciq0eKBceh2kz40I4fV1SyG41f/B0l193XWgXnuILAbicScUttn70vizU/A ivov9zkbyG1FljvOIs3jCqe6fnp/4UrG7cLBKFRXkLo9mDFkyHkocg8S4db9iBruEH yicBoCfvRgPwmYmWvsDMAkxzGcGICl8FiczveSq0= From: Jonathan Cameron To: linux-iio@vger.kernel.org Cc: Andy Shevchenko , Jonathan Cameron , Lars-Peter Clausen Subject: [PATCH v4 3/8] iio:magnetometer:mag3110: Fix alignment and data leak issues. Date: Sun, 20 Sep 2020 12:27:37 +0100 Message-Id: <20200920112742.170751-4-jic23@kernel.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200920112742.170751-1-jic23@kernel.org> References: <20200920112742.170751-1-jic23@kernel.org> MIME-Version: 1.0 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 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() data. This data is allocated with kzalloc() so no data can leak apart from previous readings. The explicit alignment of ts is not necessary in this case but does make the code slightly less fragile so I have included it. Fixes: 39631b5f9584 ("iio: Add Freescale mag3110 magnetometer driver") Reported-by: Lars-Peter Clausen Signed-off-by: Jonathan Cameron --- v4: Rename element to temperature to avoid confusion. drivers/iio/magnetometer/mag3110.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/drivers/iio/magnetometer/mag3110.c b/drivers/iio/magnetometer/mag3110.c index 838b13c8bb3d..c96415a1aead 100644 --- a/drivers/iio/magnetometer/mag3110.c +++ b/drivers/iio/magnetometer/mag3110.c @@ -56,6 +56,12 @@ struct mag3110_data { int sleep_val; struct regulator *vdd_reg; struct regulator *vddio_reg; + /* Ensure natural alignment of timestamp */ + struct { + __be16 channels[3]; + u8 temperature; + s64 ts __aligned(8); + } scan; }; static int mag3110_request(struct mag3110_data *data) @@ -387,10 +393,9 @@ static irqreturn_t mag3110_trigger_handler(int irq, void *p) struct iio_poll_func *pf = p; struct iio_dev *indio_dev = pf->indio_dev; struct mag3110_data *data = iio_priv(indio_dev); - u8 buffer[16]; /* 3 16-bit channels + 1 byte temp + padding + ts */ int ret; - ret = mag3110_read(data, (__be16 *) buffer); + ret = mag3110_read(data, data->scan.channels); if (ret < 0) goto done; @@ -399,10 +404,10 @@ static irqreturn_t mag3110_trigger_handler(int irq, void *p) MAG3110_DIE_TEMP); if (ret < 0) goto done; - buffer[6] = ret; + data->scan.temperature = ret; } - iio_push_to_buffers_with_timestamp(indio_dev, buffer, + iio_push_to_buffers_with_timestamp(indio_dev, &data->scan, iio_get_time_ns(indio_dev)); done: