Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp9098575pxu; Mon, 28 Dec 2020 06:36:14 -0800 (PST) X-Google-Smtp-Source: ABdhPJz3ytFntN3YeETWrb9yqqFK6vEZbuceTgO8i+JkHPXdchz7Wif10NfuLcAA4tO4EaJXbNcE X-Received: by 2002:a17:907:2111:: with SMTP id qn17mr40430874ejb.525.1609166173791; Mon, 28 Dec 2020 06:36:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609166173; cv=none; d=google.com; s=arc-20160816; b=jPJfe70OqfdevNfjhUabKT+quSCrUt/4NwEFUSo7PBxCd0tFmZ9aSPLOyM64dLD8Fk 2J21TtKvIn5CXOnysHxY8o9BnC+A7z4xbAru5ZKeQQkQPq9sl+YSFDtcPMiaBkOYVAG6 veKf9aZj0QCjiEC1dmYHJ1idaoILnxHmcU1cZ3EuE5dWKdlSPm8paAy4AJkXmHR5TJv6 E/xPbiVGnaIPFP0JV3AohKIk6VtVpbur/6eKCrHNVb9/UVRxe7KYC0/ir4xKgZwQveWR rymBTltQYueDGXhyhLp7N6O/NkLXjrd9g9hODaBVPctznJL5Ubv13oWBdjjErnkAzd1z lLMQ== 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=AvZaFYcNDziWEhHzDQnEylENbto4h5vNhWXr1x5ErAE=; b=uSJl/CJ7AFdnMlgPn/NgCDYtGWlvDmxHCjmR+3muDlmwXysHAHjfBX+LdCXMtz06/B F2BHYb/2NfptyEL10KsaoUL88koWSGFKWpdYvEXOxsOpEOYW7uAMBsAMoUuSJzj+JEzl hhEwcPtLtGsIDBVoN5WUDgJN60A98r1EGR3hn1cYFoQM+Z31zWJzOsN9QYilA5StDuHA p1w7iSUzDiDbRxG4LGnFDdNI/3+uovq+NWbzYwOrAJbiiNrxf7BubGp1EOlXVlntrfoA rCsQtyQKwtUVa4Xl1RXjQim+waG/9Vy6SrOpn19U3ZUJuo1Csp+l9TaEfTlS/auVp1rZ VoAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=BqoZdjM6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (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 mj22si20076772ejb.215.2020.12.28.06.35.51; Mon, 28 Dec 2020 06:36:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-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=@linuxfoundation.org header.s=korg header.b=BqoZdjM6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2504912AbgL1OdK (ORCPT + 99 others); Mon, 28 Dec 2020 09:33:10 -0500 Received: from mail.kernel.org ([198.145.29.99]:40238 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2504795AbgL1Ocp (ORCPT ); Mon, 28 Dec 2020 09:32:45 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 99AB02063A; Mon, 28 Dec 2020 14:32:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1609165950; bh=B88tFzYB7Wcia0t530oTc625taDcTdsUFlGMrJnGv3A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BqoZdjM6u2Sxws9zvgkdlr5N5YX5pzuNG0nOAfu8Kfyug6zT9mqedDZaG2l+APjdO cQSuzSojmkTD4kqpAj10c0jw2jwU9fTftvHidhfQ8vz83i7y2pbeQUREQDHnLsT/NF Fbu7rNqSp9bPLlvfIRhmJX1VDsvA36du7/uJ/SFw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Lars-Peter Clausen , Jonathan Cameron , Alexandru Ardelean , Daniel Baluta , Daniel Baluta , Stable@vger.kernel.org Subject: [PATCH 5.10 682/717] iio:imu:bmi160: Fix alignment and data leak issues Date: Mon, 28 Dec 2020 13:51:20 +0100 Message-Id: <20201228125053.656836534@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201228125020.963311703@linuxfoundation.org> References: <20201228125020.963311703@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jonathan Cameron commit 7b6b51234df6cd8b04fe736b0b89c25612d896b8 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 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 array in the iio_priv() data with alignment explicitly requested. This data is allocated with kzalloc() so no data can leak apart from previous readings. In this driver, depending on which channels are enabled, the timestamp can be in a number of locations. Hence we cannot use a structure to specify the data layout without it being misleading. Fixes: 77c4ad2d6a9b ("iio: imu: Add initial support for Bosch BMI160") Reported-by: Lars-Peter Clausen Signed-off-by: Jonathan Cameron Reviewed-by: Alexandru Ardelean Cc: Daniel Baluta Cc: Daniel Baluta Cc: Link: https://lore.kernel.org/r/20200920112742.170751-6-jic23@kernel.org Signed-off-by: Greg Kroah-Hartman --- drivers/iio/imu/bmi160/bmi160.h | 7 +++++++ drivers/iio/imu/bmi160/bmi160_core.c | 6 ++---- 2 files changed, 9 insertions(+), 4 deletions(-) --- a/drivers/iio/imu/bmi160/bmi160.h +++ b/drivers/iio/imu/bmi160/bmi160.h @@ -10,6 +10,13 @@ struct bmi160_data { struct iio_trigger *trig; struct regulator_bulk_data supplies[2]; struct iio_mount_matrix orientation; + /* + * Ensure natural alignment for timestamp if present. + * Max length needed: 2 * 3 channels + 4 bytes padding + 8 byte ts. + * If fewer channels are enabled, less space may be needed, as + * long as the timestamp is still aligned to 8 bytes. + */ + __le16 buf[12] __aligned(8); }; extern const struct regmap_config bmi160_regmap_config; --- a/drivers/iio/imu/bmi160/bmi160_core.c +++ b/drivers/iio/imu/bmi160/bmi160_core.c @@ -427,8 +427,6 @@ static irqreturn_t bmi160_trigger_handle struct iio_poll_func *pf = p; struct iio_dev *indio_dev = pf->indio_dev; struct bmi160_data *data = iio_priv(indio_dev); - __le16 buf[12]; - /* 2 sens x 3 axis x __le16 + 2 x __le16 pad + 4 x __le16 tstamp */ int i, ret, j = 0, base = BMI160_REG_DATA_MAGN_XOUT_L; __le16 sample; @@ -438,10 +436,10 @@ static irqreturn_t bmi160_trigger_handle &sample, sizeof(sample)); if (ret) goto done; - buf[j++] = sample; + data->buf[j++] = sample; } - iio_push_to_buffers_with_timestamp(indio_dev, buf, pf->timestamp); + iio_push_to_buffers_with_timestamp(indio_dev, data->buf, pf->timestamp); done: iio_trigger_notify_done(indio_dev->trig); return IRQ_HANDLED;