From patchwork Mon Dec 28 12:51:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 352681 Delivered-To: patch@linaro.org Received: by 2002:a02:85a7:0:0:0:0:0 with SMTP id d36csp9797391jai; Mon, 28 Dec 2020 06:43:03 -0800 (PST) X-Google-Smtp-Source: ABdhPJz7gxcm5HcPF2PzDB1wN/4p6G2dXLPAfgP5dysrXBmh7wWSt2fsSCwEcocQTe1GmsTjGQov X-Received: by 2002:a17:906:7d09:: with SMTP id u9mr34440610ejo.380.1609166495771; Mon, 28 Dec 2020 06:41:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609166495; cv=none; d=google.com; s=arc-20160816; b=EI1FesOpdD3wbGE6XpJokMUu/Q2MgAbhOWn17m/oyKThYDihUHIFwOmoDsaeeGMfCd DOqSwjbs8Cd0v1PDw+0b6BjvYdtgM8LZG+vCh3sFziovBiuL8USXlY7Ys8/dBMJQZF/y DdAscpWwxp9QidaGwvHLF7M5EhQOSJirFL5gYMv2tBkckiVhsowmNj9ug351NfyBODjG ++7kNx/eqKTOLZjtM11PW/DvjQ08FS7DPeaBMReZFe9WrGYfGpyG0gAcjURMsOSCzj2B YXAcGRj3Wx74xPr0AHUePBes6tbWfBDdlzFkv6HMe0khAQbrF/hIWhJe2FUkBv8ufqC/ +dUg== 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=rZoY0sXE0fetyRg20aVPml13guhyYaa/FjSouO1Q6Hq35eYzPazQTOYEtaT1H1IEFH vJD5PC5lf/ZlgWgaBOTF/cFcw0sho6XtgCeAuxJGWQWAPa35o+tre39fFQ7qrtAtcqN0 QNTfZgkh2BmZCrATblvnpe5FXaGZki1sZEoP1vYPLAEpwu+qBYQ46KDzP9SPzre5UjmX IyIswVgfhc/c/diuYlZ7Y56Krlq04D8/rSY9s3qCkIgnKu1po+qAHv27MreyiKZa28xO /eACdUs1TDO//VSzLA84NRX//e5uaCc39U7aEo9AGeMmvNi8WOXTddFMd7RW0ids6VTc gwMA== 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 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 os24si19005124ejb.680.2020.12.28.06.41.35; Mon, 28 Dec 2020 06:41:35 -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=NsjTfYgP; 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 S2505322AbgL1Og5 (ORCPT + 14 others); Mon, 28 Dec 2020 09:36:57 -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 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 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: