From patchwork Mon Dec 28 12:51:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 352658 Delivered-To: patch@linaro.org Received: by 2002:a02:85a7:0:0:0:0:0 with SMTP id d36csp9765122jai; Mon, 28 Dec 2020 05:59:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJwS4VCbeJhtNUVOjc3O8p+ww5Typ19JIkP/NtiP/7fpANvK1MJZdVktFLoWB27aG4v2vvow X-Received: by 2002:a05:6402:1646:: with SMTP id s6mr42043522edx.319.1609163976682; Mon, 28 Dec 2020 05:59:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609163976; cv=none; d=google.com; s=arc-20160816; b=WtR59CqzomFJuZtTnH0dqXFi7X7u8XhLDIBYRFsm8pcX9bR+hguvH+/ClJBnckWTt/ sc5nq+Wi14iKzmcFN/NGM+4CX1k1eboVAB+TnOrWtXxliUnHZqCFfXqgCj2UIGcjqcpO iAx37tfzTweIi/X6SrCxJkmWd0iOq6q7TwIeFogeIJGzvMhBC4WbFmd7e/IU9sJ8w70I B6p2eguVFINUIBlNqyZNPSNDxHTnbODdzvrHP1CTqNUJr1O/BWUETbFjgtdWxUQby8nj kbhJTH8EW6VbKWbmf9sNfrbebn0xIrBefWtjbx1q3csG+Wv0z9y17l3Dlvq5LJueOA6M B7ng== 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=HQjdmcOkDEyYB65v0BvtXIJgpWWp6x4VMmOZErbcClo=; b=dZkSvawiQVIc5atrvgpTbhQtESUA07DqBKWamymdhnbD23YIgXsO9mVJaRXhO8Qgy5 OydNNaSvwCiJ9+x28G+6WyuRAB8JtJiWPnEF0VP8ucCq2BS3LByI+lfkdESTGV+R4IXy KngDrQBMWlEVxGYm319mQPQOzF58M98nXAyeFc8lf9IhSAPBx3ok9436N+EG43Y+ztYk AOWRhorMilBOfETkwsJA72JeRe7736XY7Anhs4KN4nEsXXUw93+DZVzWq9b3j0/PRQ2M WTkk8AAgT9mxlGgZoWBnBci4f0kSzHII2L49adF8pnA1D6VPEU2O8xjqSwkEPNC02qC9 ldEQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=L5Kk8A1x; 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 e14si19256777ejj.571.2020.12.28.05.59.36; Mon, 28 Dec 2020 05:59:36 -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=L5Kk8A1x; 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 S2391587AbgL1N6K (ORCPT + 14 others); Mon, 28 Dec 2020 08:58:10 -0500 Received: from mail.kernel.org ([198.145.29.99]:59930 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391579AbgL1N6J (ORCPT ); Mon, 28 Dec 2020 08:58:09 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7410C208B3; Mon, 28 Dec 2020 13:57:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1609163849; bh=asRQwO+15DufR5dDPW0ZkNYMUe2EfFUbF3M/AzTsmD4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=L5Kk8A1xCeO1/DFqKU+7b0QWbNLYyaOYvYGCc3n7euY7oNnJBm+waSiDdO+h1v/it uhYltuokw5yoLTYqwjZQFYeb0WSv/J4lqmA5yfsAa/XYASyyS9hxZn4tBwX3GajXJf 4ze/8bvXBSL653GrrNPJANu8/TudCNFgDXSYyirM= 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 , Stable@vger.kernel.org Subject: [PATCH 5.4 429/453] iio:magnetometer:mag3110: Fix alignment and data leak issues. Date: Mon, 28 Dec 2020 13:51:05 +0100 Message-Id: <20201228124957.867387466@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201228124937.240114599@linuxfoundation.org> References: <20201228124937.240114599@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 89deb1334252ea4a8491d47654811e28b0790364 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 structure in the iio_priv() data. This data is allocated with kzalloc() so no data can leak apart from previous readings. The explicit alignment of ts is not necessary in this case but does make the code slightly less fragile so I have included it. Fixes: 39631b5f9584 ("iio: Add Freescale mag3110 magnetometer driver") Reported-by: Lars-Peter Clausen Signed-off-by: Jonathan Cameron Reviewed-by: Alexandru Ardelean Cc: Link: https://lore.kernel.org/r/20200920112742.170751-4-jic23@kernel.org Signed-off-by: Greg Kroah-Hartman --- drivers/iio/magnetometer/mag3110.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) --- a/drivers/iio/magnetometer/mag3110.c +++ b/drivers/iio/magnetometer/mag3110.c @@ -56,6 +56,12 @@ struct mag3110_data { int sleep_val; struct regulator *vdd_reg; struct regulator *vddio_reg; + /* Ensure natural alignment of timestamp */ + struct { + __be16 channels[3]; + u8 temperature; + s64 ts __aligned(8); + } scan; }; static int mag3110_request(struct mag3110_data *data) @@ -387,10 +393,9 @@ static irqreturn_t mag3110_trigger_handl struct iio_poll_func *pf = p; struct iio_dev *indio_dev = pf->indio_dev; struct mag3110_data *data = iio_priv(indio_dev); - u8 buffer[16]; /* 3 16-bit channels + 1 byte temp + padding + ts */ int ret; - ret = mag3110_read(data, (__be16 *) buffer); + ret = mag3110_read(data, data->scan.channels); if (ret < 0) goto done; @@ -399,10 +404,10 @@ static irqreturn_t mag3110_trigger_handl MAG3110_DIE_TEMP); if (ret < 0) goto done; - buffer[6] = ret; + data->scan.temperature = ret; } - iio_push_to_buffers_with_timestamp(indio_dev, buffer, + iio_push_to_buffers_with_timestamp(indio_dev, &data->scan, iio_get_time_ns(indio_dev)); done: