From patchwork Mon Dec 28 12:51:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 352678 Delivered-To: patch@linaro.org Received: by 2002:a02:85a7:0:0:0:0:0 with SMTP id d36csp9796434jai; Mon, 28 Dec 2020 06:41:33 -0800 (PST) X-Google-Smtp-Source: ABdhPJxMjAK5HQepts4vPhZZaPJLFYxoyiMeOV38NDjqAGUdFz6q8s5vbmoynco0IGz7Pb95WCt/ X-Received: by 2002:a17:906:4756:: with SMTP id j22mr2123418ejs.353.1609166493477; Mon, 28 Dec 2020 06:41:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609166493; cv=none; d=google.com; s=arc-20160816; b=HA5fb50BuBsqMJOzlSrINIgBdiHufCf6gPjvieeAIJoiX/1Kj7dXZtORhhxWKhOSN0 LB/9g5aUvNJFT8V9iaCOErO51OUo5UsRIj0R9mL471hhnMKeKUm4OsFGkbtnaJSvLjGi Bw3tcwtK5Vu/K6ycKBpm3K/4ubrJBoHDtPrT/T4Cy7Zq8SlD9yh/PgSbP/vWJhLzTgiR k6gv+cGq8jUKE1xMbJEyhX7SAvMHvzOjYYFkz5taKtBm+5LFfBRVGZ+4XJmd8DPOETQJ BJrQuirr9sfyOcAqSnozf7jdbOt/lHDNjLe1XPT6CwbDBMetnD7XBnJOQNBvM9cN84Be ZGVA== 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=TgJmtXb6e5XYGtz+nv/E0PvtgCDL5mSThzvdj68WZH8l/zZNCHoJWQnI7HDjMcl0H3 8aPfKMInWUput6MNuseYNSBZmt57W5JQn2TC3vvg39A8g2Ak+73v7LnVAZ4Lufp6X5kV 663KsjW1FNqNzPkXP1/lxcaDHh9gk21uoq5Dqf5kTiG0fAZvJmyY3HXWE7aLGewGzfUX O5CnogChtLGDP67qK9FhsX7PKCpvM+eC4PS61VIs0KRSQmwyljgbmU6cQx/YdGfdFLb6 b+gHV5Lh2hVDInF51h2cInund7jWB6zUja4k+ezVXLT/iUv/n7JzpM3Vk5jWRn/5upgh M2pQ== 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 stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-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 os24si19005124ejb.680.2020.12.28.06.41.33; Mon, 28 Dec 2020 06:41:33 -0800 (PST) Received-SPF: pass (google.com: domain of stable-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 stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-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 S2504801AbgL1Ocq (ORCPT + 14 others); Mon, 28 Dec 2020 09:32:46 -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 Precedence: bulk List-ID: X-Mailing-List: stable@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;