Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp345696pxu; Thu, 7 Jan 2021 06:36:18 -0800 (PST) X-Google-Smtp-Source: ABdhPJz8+vjfm96Wt1kxpR8irZa0wnWpMZTkwH8qAJk+m9XdaG19Yku6jjJ+IB+GHrnuhiCjcL2v X-Received: by 2002:aa7:c558:: with SMTP id s24mr1923492edr.257.1610030178731; Thu, 07 Jan 2021 06:36:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610030178; cv=none; d=google.com; s=arc-20160816; b=oX3DKqjMHMMveSG37tOACZhViDkmkTW0W4TWCkMyKh9UZ1SdSKoNjxYGSRetT9jjvA JcjoYWaqqcKxyAXWeeO3OW2HvwIxy4zG2Z+33E9dhflwgMpcjKeead8trUKqzzA9FlHI XtJCpYnT3QxUG7oYiMIaPrDm5HFjZbSQZCl7bobO+EE35Aznohdf3sePtCwmNPWhfl0+ 4WZr5Y8aHYLZ1NC6Vt/c/Ves6r25n7kvTLOz2cLkl5DyqZwY9GezrtQD2AdkPJdqan6q WWh2HmIGdMEWi5hx7E+sjdUo/Os9FNl3FANNrvO+XqRYiIgPytNhciYQP9AFsXkzFazV mQ2Q== 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=ESJUeCuaLNakKNjds4OJcsDzsv9/b8EqrXD5jUFDumJocua/vt1U45bUGQ45WevJoH EeFLtXc+fkcDoqsJU3hiOuzgI/P/pj7aA2kpXbSbgUF7NzpAyaIT8hZppv7xWXVdo3O8 cXqcJDeQ2Fm2GG8TC674eoH9xvmlI/4/cRf4WqKQV2fiP5Wu2OAZVEZ4c+dGZwHX9ln5 qhHPHfLple8V+eoerXPsJ3+nIx6oOjC0JNx2LTYlgw5xIO4DPIERSLxvI61mq8ieAqCO UY+VO/OoICanNNl+4104HAlCIuSSTp+p/3peNXo5YAtGDaP28hjxZFLvioZoaTycwjZR LWlA== 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 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 r3si2372087edy.595.2021.01.07.06.35.55; Thu, 07 Jan 2021 06:36:18 -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=bnBCTacy; 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 S1729106AbhAGOb4 (ORCPT + 99 others); Thu, 7 Jan 2021 09:31:56 -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 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 @@ -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: