From patchwork Sun Jun 7 15:53:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Cameron X-Patchwork-Id: 11591669 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 F03A1913 for ; Sun, 7 Jun 2020 15:56:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CB0D320774 for ; Sun, 7 Jun 2020 15:56:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1591545382; bh=f81nAf9BqkdQk8vYQ1vXUOwvadJk4lSnv1mV+jpZpas=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=PNCfr903lJsRXTTkUXQPGq/djUHalqWd0uuJSEs3WvciaCXNGbJIeKJdinwV7z0Cm NhozrDT/5BBANBRybErd+/uytl3lTTuf1l0oi2jDG2CPQv2Y/b+NdV5zgeotB7YJAw nmoZ6OiW8FYih3S4JYtcu0ZJRz6f+zCMJMDhTE6Y= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726679AbgFGP4W (ORCPT ); Sun, 7 Jun 2020 11:56:22 -0400 Received: from mail.kernel.org ([198.145.29.99]:57256 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726631AbgFGP4V (ORCPT ); Sun, 7 Jun 2020 11:56:21 -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 84C622075A; Sun, 7 Jun 2020 15:56:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1591545381; bh=f81nAf9BqkdQk8vYQ1vXUOwvadJk4lSnv1mV+jpZpas=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sNGiMvjCvrq+cpbZmUyOpfpKULeZUczZ6jqElT9ppqzK1+weEfNh/8cRaQyEfZeGx pNjMwkFrmCu+o8tADLzkDB6FC2kmaHYoD6KMzoFXZRlmCKcuHp2drYYKaCd/Vrj4oU CsqGfgxpIqihEMtoUYubiu/jHuqbTYwMPBntLw40= From: Jonathan Cameron To: linux-iio@vger.kernel.org Cc: Andy Shevchenko , Jonathan Cameron , Lars-Peter Clausen Subject: [PATCH 05/32] iio:gyro:itg3200: Fix timestamp alignment and prevent data leak. Date: Sun, 7 Jun 2020 16:53:41 +0100 Message-Id: <20200607155408.958437-6-jic23@kernel.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200607155408.958437-1-jic23@kernel.org> References: <20200607155408.958437-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 16 byte array of smaller elements on the stack. This is fixed by using an explicit c structure. As there are no holes in the structure, there is no possiblity of data leakage in this case. The explicit alignment of ts is not strictly necessary but potentially makes the code slightly less fragile. Fixes: 36e0371e7764 ("iio:itg3200: Use iio_push_to_buffers_with_timestamp()") Reported-by: Lars-Peter Clausen Signed-off-by: Jonathan Cameron --- drivers/iio/gyro/itg3200_buffer.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/drivers/iio/gyro/itg3200_buffer.c b/drivers/iio/gyro/itg3200_buffer.c index d3fbe9d86467..1c3c1bd53374 100644 --- a/drivers/iio/gyro/itg3200_buffer.c +++ b/drivers/iio/gyro/itg3200_buffer.c @@ -46,13 +46,20 @@ static irqreturn_t itg3200_trigger_handler(int irq, void *p) struct iio_poll_func *pf = p; struct iio_dev *indio_dev = pf->indio_dev; struct itg3200 *st = iio_priv(indio_dev); - __be16 buf[ITG3200_SCAN_ELEMENTS + sizeof(s64)/sizeof(u16)]; - - int ret = itg3200_read_all_channels(st->i2c, buf); + /* + * Ensure correct alignment and padding including for the + * timestamp that may be inserted. + */ + struct { + __be16 buf[ITG3200_SCAN_ELEMENTS]; + s64 ts __aligned(8); + } scan; + + int ret = itg3200_read_all_channels(st->i2c, scan.buf); if (ret < 0) goto error_ret; - iio_push_to_buffers_with_timestamp(indio_dev, buf, pf->timestamp); + iio_push_to_buffers_with_timestamp(indio_dev, &scan, pf->timestamp); iio_trigger_notify_done(indio_dev->trig);