Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp9416842pxu; Mon, 28 Dec 2020 15:48:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJzfC47Uxjuxm0/HKbWXS2ONGdqVPIAe9L+F5lJZgxs+lCdBDaMJFlUt26SfWvySyI+YAcLq X-Received: by 2002:a17:906:3999:: with SMTP id h25mr44012608eje.146.1609199314890; Mon, 28 Dec 2020 15:48:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609199314; cv=none; d=google.com; s=arc-20160816; b=z2WZCB0YB6cY4gvpvHU/qpHcJ2B86IQnwRxdYsnbzmPfSG5G4Da+UozgJgOw13mIzQ mlROwf2rINdM1wdyHVXpgZ5hRO3BRKK0aVtcSzZrT0XqmYNQUR47g3cwPwEHNDp3r107 szkoccAaGa33iE4Baw0XUiM9+8x0ytlTl7SOwWKcl+XeHTky4SkYabRAsHfgHzVaJ9jE jsaP3e/17nWGiy4YZbN5mN1NqZK9w87IvXhTaEp4C6K2JTxNHS/E4nq/n2e+mNVz+RGT lRfioIWk9K8y8TtJQxQmyQFAlQjz1/SyN8uc66DN4Co3cPHEsh89sgIPktQYX0lQ2Nw0 C0vw== 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=QxeGXfSa3ROs7IgxV+Szpadc7WtsitljnTzDdkWTLs64Nqi0jnvJUZR/OPYJitW6En i34K37N9mqKy6C5AOevS4iibbyA+NC0PkiLkTmSxfeSovkPjuAJr6k/6yKd5D9nCc6H2 9OkS/b9bt8Kse4mZNa1VwI20T9DOL6KoZhuuSqOwLD+pMiMsIMFeB3PNMQORhXAiGlp1 rGEu+pVhiyenNEYriJ3gPmYr5Ev8Je7cflZ4IoCBPY9REzD6vOSAoJ7hQgCQ8HD32K8A jxnZuX+si/3k4vjCSEegzicpvJqg+PS7RHUv/fTHUAT33ynVvVb1nV38RsLRyGoQEKzr odgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=NsjTfYgP; 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 o12si18896399ejg.407.2020.12.28.15.48.13; Mon, 28 Dec 2020 15:48:34 -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=NsjTfYgP; 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 S2505631AbgL1OhB (ORCPT + 99 others); Mon, 28 Dec 2020 09:37:01 -0500 Received: from mail.kernel.org ([198.145.29.99]:41004 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2504872AbgL1OdC (ORCPT ); Mon, 28 Dec 2020 09:33:02 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 3AE192245C; Mon, 28 Dec 2020 14:32:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1609165941; bh=asRQwO+15DufR5dDPW0ZkNYMUe2EfFUbF3M/AzTsmD4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NsjTfYgP+5n1ncG30ob8RhbQ5Pf5J7pm0euOh8BCgYTU2A1ee+AY3wqzRPePzBL0p eOKgxuhBX0kCXNy8WrkUx/ViRcMBJUJqrjqTATCvNCEvVUEAquwIpe/AzMnPqtpepY l38XktyR0QIiIow/BiL9MLkofdcjHep0Ua2D1s4k= 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.10 679/717] iio:magnetometer:mag3110: Fix alignment and data leak issues. Date: Mon, 28 Dec 2020 13:51:17 +0100 Message-Id: <20201228125053.517182768@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201228125020.963311703@linuxfoundation.org> References: <20201228125020.963311703@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 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: