Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3285134pxk; Mon, 21 Sep 2020 09:41:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxW79Zl2Y4Dp+K0IOtNmAloCHr9g0bQ9zrGMKnnnfVRR1m9hpcA2xhKXuRStxY5zP8kSwj9 X-Received: by 2002:a17:906:3c47:: with SMTP id i7mr305386ejg.554.1600706474174; Mon, 21 Sep 2020 09:41:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600706474; cv=none; d=google.com; s=arc-20160816; b=qHOS3LqE9UE99tTAP4ka09ev5wW8AC2gB7t3Hpjul9hPjeZg9tje/EY3YVG7kvLukY Cc6Lw/HFC2fu7LAtWA2ir+33g6FPU+xAZDFNeG2uMorbEK72KcW8bJZcZFC91CKgfOZv 5tOVM/HryuEgZwj3GrpDlryjxG82J208MIQuyp+/mDpfDeavMaGoP4bbEtqbuJ/a2dbu jU292O13av5ewpacRrpnjfZBhVBQ0q4JZs/KpbI3FA9w4OFImJeResJEYxLpdqG6tzcc eWvq+HsYXI8bYwfLTHI1UustQ1+HneofEY1jgUhvWKr7b6VdrWNOUa3bKl5aVnHku/6c eJnA== 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=/wImNIW4S92LY8fDcnrxZ6celmL6RcvBzJ3/lDZY6zI=; b=VqxsAipV6tMdGNvpVEQgA2BMphP1I59SYlrae9nEwsIWnns/x9pSPLn9JFiJvZsHH8 grie9bRmAR5MvCGlQL9gOUeldz/euPyyoLQqNR/OOYIeZu3qrOC8KUwyERrmHdjAdpfy q4rcnuWs26LwFOjUY9YosP5GY0p88k3Q8o3YiRKSlVSvV/FzUsypFtFuttGso1Wx2Ydd LimPjvAYv3uSQc0Ls+RKqB3tRg2ynSgAhJxIpQWGefugY9JOCBnTr3RgF1L4lhB74Hfe 9l99sl60RpwkCx2uxSP9LyEhE1iw5uVoy8mb66KRY+lbia1dUEswwj+l06maDnhnLuy6 SMwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Co01a6iQ; 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=fail (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 f6si8728446edj.365.2020.09.21.09.40.50; Mon, 21 Sep 2020 09:41:14 -0700 (PDT) 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=@kernel.org header.s=default header.b=Co01a6iQ; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729212AbgIUQi7 (ORCPT + 99 others); Mon, 21 Sep 2020 12:38:59 -0400 Received: from mail.kernel.org ([198.145.29.99]:40382 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729203AbgIUQi4 (ORCPT ); Mon, 21 Sep 2020 12:38:56 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 4E423206DC; Mon, 21 Sep 2020 16:38:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600706331; bh=V+hMxe2Lwr/FAnvCw25sVauwGTJ4DqBLe1JJIdJzaaU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Co01a6iQ6mOl9MgewHfDepY12GW0AAOByvQbjHCh/55Tn2p+NThzOrkCMU9h2HEtn +2iZZviS5lgAsRVNozShQXhwPDb4hY4wCfTFG+XgDpNgQdieU8/lXtqwbF0721N7S9 9uL+Sqp/SvgCRG4BjZvv/JEfEMjO/cTC9kpW7o/I= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Lars-Peter Clausen , Jonathan Cameron , Srinivas Pandruvada , Andy Shevchenko , Stable@vger.kernel.org Subject: [PATCH 4.14 27/94] iio:accel:bmc150-accel: Fix timestamp alignment and prevent data leak. Date: Mon, 21 Sep 2020 18:27:14 +0200 Message-Id: <20200921162036.806621492@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200921162035.541285330@linuxfoundation.org> References: <20200921162035.541285330@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 a6f86f724394de3629da63fe5e1b7a4ab3396efe 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 a 16 byte 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 with alignment ensured by use of an explicit c structure. This data is allocated with kzalloc so no data can leak appart from previous readings. Fixes tag is beyond some major refactoring so likely manual backporting would be needed to get that far back. Whilst the force alignment of the ts is not strictly necessary, it does make the code less fragile. Fixes: 3bbec9773389 ("iio: bmc150_accel: add support for hardware fifo") Reported-by: Lars-Peter Clausen Signed-off-by: Jonathan Cameron Acked-by: Srinivas Pandruvada Reviewed-by: Andy Shevchenko Cc: Signed-off-by: Greg Kroah-Hartman --- drivers/iio/accel/bmc150-accel-core.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) --- a/drivers/iio/accel/bmc150-accel-core.c +++ b/drivers/iio/accel/bmc150-accel-core.c @@ -197,6 +197,14 @@ struct bmc150_accel_data { struct mutex mutex; u8 fifo_mode, watermark; s16 buffer[8]; + /* + * Ensure there is sufficient space and correct alignment for + * the timestamp if enabled + */ + struct { + __le16 channels[3]; + s64 ts __aligned(8); + } scan; u8 bw_bits; u32 slope_dur; u32 slope_thres; @@ -933,15 +941,16 @@ static int __bmc150_accel_fifo_flush(str * now. */ for (i = 0; i < count; i++) { - u16 sample[8]; int j, bit; j = 0; for_each_set_bit(bit, indio_dev->active_scan_mask, indio_dev->masklength) - memcpy(&sample[j++], &buffer[i * 3 + bit], 2); + memcpy(&data->scan.channels[j++], &buffer[i * 3 + bit], + sizeof(data->scan.channels[0])); - iio_push_to_buffers_with_timestamp(indio_dev, sample, tstamp); + iio_push_to_buffers_with_timestamp(indio_dev, &data->scan, + tstamp); tstamp += sample_period; }