Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp344769pxu; Thu, 7 Jan 2021 06:35:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJxLMeRvlLd3OcwOQZDEKUxAPmTIJQfpSubDwI3IRI+TX/cPRvEkgsUHmE9Q0C/ajH+ZRa2x X-Received: by 2002:a17:906:2c54:: with SMTP id f20mr6662754ejh.318.1610030108859; Thu, 07 Jan 2021 06:35:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610030108; cv=none; d=google.com; s=arc-20160816; b=u5wkppADCuxX9ko96jrKD16v214YlbF8wfeY4MGwetXPnmS5WyZZoF3cjUD5UAq0Sr 7vLxG9yQT3xBnMFV4h1iqwuGhnioIuWLWSBCy8dblJgw+G3mRC+5SMkeOcLAQGb8mroC ciGeBoLitBVGcXvPQput+uVCPmNaDTmK+Z/nrk9r9JIoadpreFL7htmKycrIBPst0wVE 6idMeZtVjYbKAaW1NJNRkbkSVj16Cc/207x5rJ7VIKTxIzxBJxlk5BVGh8ds/1ig7wJB xibSbk5Dr52iaCJj9ZgbTXjhiR1h4EUjewg/vyIx+jTFQ149MklNZCDbqYkNns//xC5+ SIpw== 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=31Xf6FG0zHlnk5nqNOc7d6PYM03s0iGdUQJRSgB0378=; b=dI/3QKN9vSMlQy2e05RJoiNNQ4Z2C/dMWIO499/s4jaK9VE306HoFYkkDavpioC8us ZHdZD499z39On16aRu5W6Yl+BlWoTkfZnsDLbe9c01dHXyuJLSVYAUBbeaRlUL6uFuWA 9bLgEwu6alSeGjif7A1gqVMCB+5E+BqMYTkq03CAPfh9p+JIU3V44umrKaHKKFhxFfc2 gLso4/ra8HPrJlkIrXWvTaEhLBk7LKYSFqHCAw2nNiXYP23UlDZ7ZJMNeDD0V1lBHKSW V5kg5G6FrsFi2qh7c1hx6mY/kkByAhY9LTzUiom0WdyCVqXv0+wwRy2tUJfgvRYtBefZ zbYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Y5YQcor0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-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 v4si2276429ejx.279.2021.01.07.06.34.45; Thu, 07 Jan 2021 06:35:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-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=Y5YQcor0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-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 S1729253AbhAGOcN (ORCPT + 99 others); Thu, 7 Jan 2021 09:32:13 -0500 Received: from mail.kernel.org ([198.145.29.99]:45976 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729236AbhAGOcL (ORCPT ); Thu, 7 Jan 2021 09:32:11 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 46F5D23380; Thu, 7 Jan 2021 14:31:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1610029911; bh=NXiBODZDOGHHsvjogMI2/69pXg+hQavbaRF2tkC24qE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Y5YQcor0YL9REdcGWmiFjupDNQMHxW8BqaZ+EDKPQejbl2mdIbsL7gZqKqXvsv7PY AFNaetW+2gpcbUnEjW9yQg4NE8YnnGfTF+Id1exhc0yCkO/cNc+SyRkyA8bmWIV3iV rRoA3RRsvvtvJ2LOkr4tfdFzB/kmcahLt3AI47fU= 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.19 7/8] iio:magnetometer:mag3110: Fix alignment and data leak issues. Date: Thu, 7 Jan 2021 15:32:07 +0100 Message-Id: <20210107143048.590038459@linuxfoundation.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210107143047.586006010@linuxfoundation.org> References: <20210107143047.586006010@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 @@ -56,6 +56,12 @@ struct mag3110_data { struct mutex lock; u8 ctrl_reg1; int sleep_val; + /* 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: