From patchwork Thu Jan 7 14:16:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 358238 Delivered-To: patch@linaro.org Received: by 2002:a17:906:4755:0:0:0:0 with SMTP id j21csp338307ejs; Thu, 7 Jan 2021 06:21:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJxmlK+/JDbAqiyh/8XwC6YeytCq593ZMGXIADooAEkK8m5L87Un1X4pIlivmJ8gCQnmdpul X-Received: by 2002:a50:955b:: with SMTP id v27mr1839546eda.324.1610029277062; Thu, 07 Jan 2021 06:21:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610029277; cv=none; d=google.com; s=arc-20160816; b=PPMT74lD++McmYV4aloqqJJtbt+aiN53JF+eu1vVjUnD7wZ55zyOqUo0upNmLuMAS1 kxw1AHr/vFKGQEJAD5EHnIZGiJCbG5jK6oebJVt7ilcwY04lijmfuBaNApAgMrFBx6BY AIvmieuFooyC77FR3n3hnvCEN7WFCCu89hIuyMEIbAGfyRpPVf06LEnsh5mr71vQdo4O YcplxmJvjmhcPPGPGuHuErndPuQEm7glPc0fF96BNZ3dcvXmAqNoqLWO6y4wKywy+fGb q8FRil4lhaxl40REGHtfF6y481/8T4jFJJ/LE3cwiZHqCLg4qAPtieB8sUtkSAsSjT94 auWQ== 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=rU8qNteCKiiSgIuOe7hvl0Evzam/SaBV5+HA12FjuAU=; b=QiLKFN7LgUQhZQky9+4ZMNXZ+p7r3hlslF421riCwq2bhYGQF5VgscDsizC8qG18jy TtOvWzFuBdYcFEHoY7XVuq7tnlQstHAkZFeZNtAZJsICgVTM9J1pn2vP4YbzjBTDcqZ+ Pwj1q9uVLrnFRJystd+CX5J5CyrzbZGdB7hd5r/ObqA2rRa/5GAmrYI825XLWJ7Casmd psJ+Dwx7uHT8KbUmsnhwZsWKZO/qvbYnBqXcRIpkp7/ciVMMzaxRQMEZzEtlS9+fHYsz +ghIESj/7U5lqUBhh06c/zMxv0d3D7zVLkdhmuPLCZWittqkUZG1CoY1Yjk6EMpYxZKC r1OQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="f8zUTJ/U"; 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 gu26si2212467ejb.461.2021.01.07.06.21.16; Thu, 07 Jan 2021 06:21:17 -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="f8zUTJ/U"; 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 S1728880AbhAGOQy (ORCPT + 14 others); Thu, 7 Jan 2021 09:16:54 -0500 Received: from mail.kernel.org ([198.145.29.99]:39050 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728817AbhAGOQy (ORCPT ); Thu, 7 Jan 2021 09:16:54 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 47E9E23358; Thu, 7 Jan 2021 14:15:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1610028950; bh=CmBtylNqzT62dUOb5GXnCh+v4oVfy5Rsr1/aDUhSRLw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f8zUTJ/U1uSeGE35seBSHu9Xn65EP+CQIvhzK+0t1hqy2skeyjHRCmGzobsvg5DOe haxEgooxPzyFje31Ao3dqf4KjT2UcwcbIbkhMVmDZxRwt1W5vekUlnU0D4kkmigO/k aYOEL65JwJoAeg2ElArAZGYF45umjLhDM573Kyf0= 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.4 19/19] iio:magnetometer:mag3110: Fix alignment and data leak issues. Date: Thu, 7 Jan 2021 15:16:44 +0100 Message-Id: <20210107140828.467869494@linuxfoundation.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210107140827.584658199@linuxfoundation.org> References: <20210107140827.584658199@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) @@ -245,10 +251,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; @@ -257,10 +262,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()); done: