From patchwork Sun Jun 7 15:53:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Cameron X-Patchwork-Id: 11591701 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 69C78913 for ; Sun, 7 Jun 2020 15:56:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5279520748 for ; Sun, 7 Jun 2020 15:56:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1591545404; bh=hZxyQtventa4kfzAWVTFXjuBvdlNLCQy46mZYQxIFfw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=RaPKRNPAO/de6Zkw2Xg1A2ye3cmYG+1vM95y7L0lxCWCA65MkutIZ3VsC8orgzCqj 2pjwMO6Yi1dKK9aN98kxxRS4fKL66r6BvF5QvjL9N0sLgw4i695Lc3BO3oVPZnrX+K qL+PGXYunefBIBhgUKAzg1pN26O3/gHe7+e3RyE8= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726764AbgFGP4n (ORCPT ); Sun, 7 Jun 2020 11:56:43 -0400 Received: from mail.kernel.org ([198.145.29.99]:57542 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726703AbgFGP4n (ORCPT ); Sun, 7 Jun 2020 11:56:43 -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 4620E2076A; Sun, 7 Jun 2020 15:56:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1591545403; bh=hZxyQtventa4kfzAWVTFXjuBvdlNLCQy46mZYQxIFfw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LGfJLMyoQaQfPmugJt9jaBojN4q98gikSRTwO8204vzXbpOkx7hVTFzEkVyclUZeB dHVxUwq2Dv+F4SUUUG+Qu/zAwzzuikdGl7mdMSGs6nbEZpRAKZN66+iOsNNObKxkHi Evi/tAo7H7UGX9CuMkkRhUiYH5JiDrw7dF4ILSYw= From: Jonathan Cameron To: linux-iio@vger.kernel.org Cc: Andy Shevchenko , Jonathan Cameron , Lars-Peter Clausen Subject: [PATCH 21/32] iio:pressure:ms5611 Fix buffer element alignment Date: Sun, 7 Jun 2020 16:53:57 +0100 Message-Id: <20200607155408.958437-22-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 an array of smaller elements on the stack. Here there is no data leak possibility so use an explicit structure on the stack to ensure alignment and nice readable fashion. The forced alignment of ts isn't strictly necessary in this driver as the padding will be correct anyway (there isn't any). However it is probably less fragile to have it there and it acts as documentation of the requirement. Fixes: 713bbb4efb9dc ("iio: pressure: ms5611: Add triggered buffer support") Reported-by: Lars-Peter Clausen Signed-off-by: Jonathan Cameron Acked-by: Tomasz Duszynski --- drivers/iio/pressure/ms5611_core.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/drivers/iio/pressure/ms5611_core.c b/drivers/iio/pressure/ms5611_core.c index d451bb9dffc8..214b0d25f598 100644 --- a/drivers/iio/pressure/ms5611_core.c +++ b/drivers/iio/pressure/ms5611_core.c @@ -212,16 +212,21 @@ static irqreturn_t ms5611_trigger_handler(int irq, void *p) struct iio_poll_func *pf = p; struct iio_dev *indio_dev = pf->indio_dev; struct ms5611_state *st = iio_priv(indio_dev); - s32 buf[4]; /* s32 (pressure) + s32 (temp) + 2 * s32 (timestamp) */ + /* Ensure buffer elements are naturally aligned */ + struct { + s32 channels[2]; + s64 ts __aligned(8); + } scan; int ret; mutex_lock(&st->lock); - ret = ms5611_read_temp_and_pressure(indio_dev, &buf[1], &buf[0]); + ret = ms5611_read_temp_and_pressure(indio_dev, &scan.channels[1], + &scan.channels[0]); mutex_unlock(&st->lock); if (ret < 0) goto err; - iio_push_to_buffers_with_timestamp(indio_dev, buf, + iio_push_to_buffers_with_timestamp(indio_dev, &scan, iio_get_time_ns(indio_dev)); err: