From patchwork Thu Jan 7 14:31:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 358246 Delivered-To: patch@linaro.org Received: by 2002:a02:85a7:0:0:0:0:0 with SMTP id d36csp540288jai; Thu, 7 Jan 2021 06:40:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJwoS3WMCTUMfZvHgfKdgqArMf1tqUKZcT7r1yRQ+zeFysRjjnztDBTGrXk8yUBnePmR3okN X-Received: by 2002:a05:6402:1c04:: with SMTP id ck4mr1876188edb.320.1610030399834; Thu, 07 Jan 2021 06:39:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610030399; cv=none; d=google.com; s=arc-20160816; b=Ryqq2j5cNjh11nMppyqMaw1C8uF6YKyY7rU7TvQlgrNFLEWBaAD35Rd5xZ7S8HMHPi fziKtZstn/LFOHO8PcD1tkxlIwFwliBd3g3s3CN+MJ/UrWOKUIuSVv2rRC9qMGs4Rreu 842uFNauMdkhn7zqc9hNn3ttwRFLN0UWrrAhRhRYM9DbQoKaVirEbwPM0deXx+nyK8/F 9TAKyIi2P/2de7iZatrZkRzbkPqxlgArCgtKRRw9v9MNuspgIDL2wi4DdFb9SVdFsTBL 9YQi1ouogVNYyR5ydhLzdIaXPk4pn4FTxYfh7FQIx+YJMUOQswvZcf5Q0yXc9jw0ds23 Jc1Q== 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=+hKnBzlt9C+W23mvZ2ESuz0cNLD/8EpETppQ8dB+A3E=; b=zqpIWRQg/8/wCvXENOzw9L+jwdCO8m1iW2jIIkMs9IEKRGmFEaUBJQPiu5+VP3gRGh xNxOpjJhquT9FjxIj4if38cqV1K2wcnC/Q2EBKAVPmb8U3nLNBRfCTglkoGeSPTf81dO lFnOagSD2JSKi6y1z6dIyZzRN3DaaZEK8gW1O7zmjhZG9Stov+4zA5lqulsVX0/msk9r kHIFKoXk8UQIQb2s2fCQyrOyRrqXUaUdqG4soiNQ5Ibcr+5GJX3tToEMYFM2JeL1dzxo 9XCcdOIm+BOpJeYcZ4LWDrFefr/jZfr0Dezw1Ot+lMVVsaURZgID0jDWdQvB0mw/rnzY VAjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=bnBCTacy; 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 cx27si2215837edb.264.2021.01.07.06.39.59; Thu, 07 Jan 2021 06:39:59 -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=bnBCTacy; 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 S1729018AbhAGOby (ORCPT + 14 others); Thu, 7 Jan 2021 09:31:54 -0500 Received: from mail.kernel.org ([198.145.29.99]:45346 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729084AbhAGOby (ORCPT ); Thu, 7 Jan 2021 09:31:54 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7BBE2233EE; Thu, 7 Jan 2021 14:31:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1610029877; bh=Z8u03H/lO7PXoq3GbVsBabhrrdJ39M3QWKoqX0iReT0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bnBCTacygxwH7bwyu794V8BIvDlBOWC5wSQVctk2OW0O5rmJm+UQEeSBMdV25Jvhr RW/7iTlNxu2Cqi/XCzIkcwY25oBsWKgxyW1pWs+Mtpz6hRGC+RiuEqYnC3tnhENs7F qC+p29J7tPtoviAxv3OAr9EHdDYnJGd2bAsFfwUE= 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, Sudip Mukherjee Subject: [PATCH 4.14 28/29] iio:magnetometer:mag3110: Fix alignment and data leak issues. Date: Thu, 7 Jan 2021 15:31:43 +0100 Message-Id: <20210107143056.969406570@linuxfoundation.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210107143052.973437064@linuxfoundation.org> References: <20210107143052.973437064@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 [sudip: adjust context] Signed-off-by: Sudip Mukherjee 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 @@ -52,6 +52,12 @@ struct mag3110_data { struct i2c_client *client; struct mutex lock; u8 ctrl_reg1; + /* Ensure natural alignment of timestamp */ + struct { + __be16 channels[3]; + u8 temperature; + s64 ts __aligned(8); + } scan; }; static int mag3110_request(struct mag3110_data *data) @@ -262,10 +268,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; @@ -274,10 +279,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: