From patchwork Thu Jan 7 14:16:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 358235 Delivered-To: patch@linaro.org Received: by 2002:a17:906:4755:0:0:0:0 with SMTP id j21csp335597ejs; Thu, 7 Jan 2021 06:18:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJyUDoqZDdo4EuaGcRBH4n4fdN6FASH+HrapEEw5lJUJ1MDZ51kY9LcKaZgJKNw8CVFcRlt4 X-Received: by 2002:a17:906:55d0:: with SMTP id z16mr6393314ejp.466.1610029080099; Thu, 07 Jan 2021 06:18:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610029080; cv=none; d=google.com; s=arc-20160816; b=lhG+mlFicUXaoWwFcfP7Mkq2kNnuAXJ8Rw5WkZsyuyp6vPj4FEUW4fZuhBsktyaQXN bnAcMKNcb5t/vsNHyAxMOTjJacDYIrCu1ItB8LMtHwMpEpJSAFm84JySCEDjyRjE1GV5 2C2OSpUlvJJ4ju10rLMfWtKdToIiiTqKT7dBvo09L2DjaIZC4M2r5rT4pgpf9SC8ltCt orRo3KwQYkQRbCvLeXbmCXGH1urUepvtPWKJJ74MDAZa1b4bl9R7lm32KiiRsSO7Jbzt v3vHjY15ztNEBrApD9QyTyJECow1J9czRGJL2iA+eaVeBFYZtjRLUdQL7Ac8s40MjePP WpaQ== 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=WVBEbkkbu1PP3xuLzrwmZ2r9Kk8J6KH08HgsSNc3GuiMLkGEKFTPs/PzSCsrLkQ3kZ eihMcrQsOjvcpWjufAKX0cq5gD6bgL9VjUfj1HS2NHMcItzCf2Qy7Osm+jHGvvKLQujf Dw2Sh18MjhqXKFnTspDDxg+4oWnyhjLx6Q6ebcEXiYPatkjpd+4E2hvzTs0+91jqkSl0 sTKKt/LBm69uxLsvXq7CPkQKMHZt6WhSgPvn2TPOlKEPI5NLNgGuR8XJVQU7YuhQCMAx g2WA84+z2jpAl2hQnwZ26+ok3Fnu6q2JxLIGLVEdD3d7yPnh9c10j30Uv34/o8F/evz4 3hXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=nuyrWM+i; 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 y1si2241480eds.331.2021.01.07.06.17.59; Thu, 07 Jan 2021 06:18:00 -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=nuyrWM+i; 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 S1729239AbhAGORv (ORCPT + 14 others); Thu, 7 Jan 2021 09:17:51 -0500 Received: from mail.kernel.org ([198.145.29.99]:39066 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729238AbhAGORn (ORCPT ); Thu, 7 Jan 2021 09:17:43 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id EF2E223371; Thu, 7 Jan 2021 14:17:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1610029041; bh=Z8u03H/lO7PXoq3GbVsBabhrrdJ39M3QWKoqX0iReT0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nuyrWM+i1exVoQM35FyrKI5lbbysWt5gsKKMV6/C9S90Fd55pKuHsD2hdhHkypwCH Ai++8EIPajY/Kb3V7I/VERmJuEZOCGN8qMT5/Lms5yoW8sQEhl+tvYIF2BVU+n8EqQ gNsHD34p+9jY7835xa0zx93ggB3uA3yErWpcrn3Y= 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.9 32/32] iio:magnetometer:mag3110: Fix alignment and data leak issues. Date: Thu, 7 Jan 2021 15:16:52 +0100 Message-Id: <20210107140829.394192914@linuxfoundation.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210107140827.866214702@linuxfoundation.org> References: <20210107140827.866214702@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: