From patchwork Tue Sep 15 14:13:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "gregkh@linuxfoundation.org" X-Patchwork-Id: 305242 Delivered-To: patch@linaro.org Received: by 2002:a92:5ad1:0:0:0:0:0 with SMTP id b78csp2593764ilg; Tue, 15 Sep 2020 07:20:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxWL8UUHRYGpUiKqennB8b18pcaZf8z+SAHLlGYyOHB253Skm9vrA+g/deTx5vibh3Ioc17 X-Received: by 2002:a17:906:7f0b:: with SMTP id d11mr21167885ejr.439.1600179658858; Tue, 15 Sep 2020 07:20:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600179658; cv=none; d=google.com; s=arc-20160816; b=KFKAW8h6oulrGRw/hqDIx8J88fajLZcnnqghKPzh7wPW/w85q0idEX6ZtlL8/s7f5k H/DH46KW08T1Jv1pK1Pfa9PSnSPyy80UF9fgDNbRuufFeY/lI9AOpox+yEx7/8MH4etK MDcukf+Iq+Wm5pknzurQGbUWAjJe2/JkJ7N0HfrLVgVqF5SsruXfWwxrDQPRYi3wpzCr 6k4rMSWuiISQFY0rDSgppoMX9ynf2mMdTa5v31JlfQhZCpIvPV9xxkzJ33TZocX8qoUh 4boKzlo3BCPc5vljkT1ImMH6JjJbF7z3Pw+zZi3bWQulSmDb5jbfqVcgx3FKzbQ1W/HN bF8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=8dfCaXGcTDs31k+8imVb7+p50WW0j4OYQ4urWRF4T9E=; b=GJQL0xEe22xJgOBGU1HeDNm1ZjO65Elp8iew6uqYuqZERq8RO3pjML3SBn/lATkG/q b5Vs47+X3h0ObAetszicBmgNV/AbGiGGSZXF+gxteXBI7IpOXq+Fu/s300nX/Wbauf2q 5fio2P6GJlvs8drU5rHyxWkgCyhTZTF+1yQOb0jAULpdA6B/nOx2eXMDIl/sOs+lajHe s7agpPZCod2RsHmFYn9RuGv0x2ecdAV7P+ZhA3d6/8PJ8V/Loy972YyW2w0OUziextOz QQSsiz1hXe5w9yDfLBGGJOxA+tdbh+fKdSE92TPDJEXvlsYeFOXR0J7VXDk76KKAamiv zsXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=SO3+IZLf; 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=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 v6si9503867edt.456.2020.09.15.07.20.58; Tue, 15 Sep 2020 07:20:58 -0700 (PDT) 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=@kernel.org header.s=default header.b=SO3+IZLf; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726404AbgIOOU4 (ORCPT + 15 others); Tue, 15 Sep 2020 10:20:56 -0400 Received: from mail.kernel.org ([198.145.29.99]:60526 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726806AbgIOOTx (ORCPT ); Tue, 15 Sep 2020 10:19:53 -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 DF7872222E; Tue, 15 Sep 2020 14:16:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600179394; bh=0UWIwUObDoXc88RA1kBQm3/Ttdtb0YaBcDx5KmFlTCE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SO3+IZLfRjCPzmHNWtNAOGF+GhW0MYQAbGrUU8HQ9JJ75JQjhOJscbS3z/CPbgkTm LE2z+8vMhNDhqLNHFVGvxIyzBIsPDa3OqJtpqYG/EOBy6fxWGkIkGXoro+zVWdH61g LERAUR5I/sNy5TBQDPhnLndxpssBEG2gmQEFAa0g= 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.19 43/78] iio:accel:bmc150-accel: Fix timestamp alignment and prevent data leak. Date: Tue, 15 Sep 2020 16:13:08 +0200 Message-Id: <20200915140635.739533351@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200915140633.552502750@linuxfoundation.org> References: <20200915140633.552502750@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@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; @@ -915,15 +923,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; }