Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp3210566pxb; Mon, 9 Nov 2020 05:41:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJyxdBdT9Yx3tVCQgwJnk3yzS6oSOc69TWvg/e7prcIvaQmtE7V2zTjiqqbMrtwHCTVP6wIM X-Received: by 2002:aa7:da01:: with SMTP id r1mr7889805eds.45.1604929275242; Mon, 09 Nov 2020 05:41:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604929275; cv=none; d=google.com; s=arc-20160816; b=WbnQoL5aLOrjhhcprVNr6EpMgOcOMmgsdCGpRm3NNUtt/bA5MuixE866qi2D+9yfCu YgJBhsRU2k+jKnAzwKCCfAgaPUYu/c+fQDrobpDjxpUScJtrRipPLnj7dZDKsRyndlJf udvGJ4I6eIdXeKMRw8s8vBJiG78+2RfGsRkbddsRLahitslDYggn4xb8GyrYqkowHGM1 Ws8hQgX4ikwl75X9iJVQBs7UswGfBv4Wabt5g+bCzwvdtKsUy/AAbRNCSvwyf68DHic1 4CtallRGA7dyb4Wv8NPQQHOsTbA2QiZmdB5lUwuUMQAZ6IAsvBklMmJj6S0w+Yimgc3Q sbpQ== 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=/aKrRcM50Nxah9v16YeTOLbFDzb9FYk5xELUWs3QdX4=; b=gq9STrTesNef/Jkaz+HGkJY8iz7dn9d2q4if0pFEc1Ju2DRtsBLW/jtsHu+anVX9XS NgWncgIrBTZkdrcQ5ys6RHLeIgonKFqIhdGNFO6SgY4KJo2XQGGWBCy1k+JYSNHv5Csv YphlFfQo5V+RD4XZJu4yOc27hLMeA68sgxDgGkuBa3EuthgydiXJnLtOTbdhtebdlQ6i xL6UZzGvb3pEbtxXEBH/TZkbY7cnks6nIebZwny1kLYATxwXxB4UGxkqw3L0gUt0Z+Tx t4lJUk+KZQC/o0kLV2QihdarUZyWvf+EbUsHTjnQq+m76Bbe/YjyQUDpup6bvlmk5oTH DKtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=TEMldiOI; 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=fail (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 w10si7170089ejn.255.2020.11.09.05.40.51; Mon, 09 Nov 2020 05:41:15 -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=@kernel.org header.s=default header.b=TEMldiOI; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730755AbgKINi2 (ORCPT + 99 others); Mon, 9 Nov 2020 08:38:28 -0500 Received: from mail.kernel.org ([198.145.29.99]:56462 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730657AbgKINCv (ORCPT ); Mon, 9 Nov 2020 08:02:51 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 8F7B2221F9; Mon, 9 Nov 2020 13:02:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604926971; bh=RFBUn9nQ9UEvmhsRqgxmIbzZdl7faCjeCRUgBX4r9YA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TEMldiOIyUudBU8AdYiu6NXTjaQmYjlZt5WOJpcw992YPqZbazouJWGj7Dqpm8cCp pNXkhvJcEgumarzP2WHgycK2MC2nunSI3kslY+iQGlQVWOWcl2Z9ktuKAe0S5aDp+1 EuXn2UhZfg/l+dXEhUM3dNkzOO1wL1vp5F7LxQEU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Lars-Peter Clausen , Jonathan Cameron , Andy Shevchenko , Peter Meerwald-Stadler , Stable@vger.kernel.org Subject: [PATCH 4.9 066/117] iio:light:si1145: Fix timestamp alignment and prevent data leak. Date: Mon, 9 Nov 2020 13:54:52 +0100 Message-Id: <20201109125028.803240098@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201109125025.630721781@linuxfoundation.org> References: <20201109125025.630721781@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 0456ecf34d466261970e0ff92b2b9c78a4908637 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 a 24 byte 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 array in the iio_priv() data with alignment explicitly requested. This data is allocated with kzalloc so no data can leak appart from previous readings. Depending on the enabled channels, the location of the timestamp can be at various aligned offsets through the buffer. As such we any use of a structure to enforce this alignment would incorrectly suggest a single location for the timestamp. Comments adjusted to express this clearly in the code. Fixes: ac45e57f1590 ("iio: light: Add driver for Silabs si1132, si1141/2/3 and si1145/6/7 ambient light, uv index and proximity sensors") Reported-by: Lars-Peter Clausen Signed-off-by: Jonathan Cameron Reviewed-by: Andy Shevchenko Cc: Peter Meerwald-Stadler Cc: Link: https://lore.kernel.org/r/20200722155103.979802-9-jic23@kernel.org Signed-off-by: Greg Kroah-Hartman --- drivers/iio/light/si1145.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) --- a/drivers/iio/light/si1145.c +++ b/drivers/iio/light/si1145.c @@ -172,6 +172,7 @@ struct si1145_part_info { * @part_info: Part information * @trig: Pointer to iio trigger * @meas_rate: Value of MEAS_RATE register. Only set in HW in auto mode + * @buffer: Used to pack data read from sensor. */ struct si1145_data { struct i2c_client *client; @@ -183,6 +184,14 @@ struct si1145_data { bool autonomous; struct iio_trigger *trig; int meas_rate; + /* + * Ensure timestamp will be naturally aligned if present. + * Maximum buffer size (may be only partly used if not all + * channels are enabled): + * 6*2 bytes channels data + 4 bytes alignment + + * 8 bytes timestamp + */ + u8 buffer[24] __aligned(8); }; /** @@ -444,12 +453,6 @@ static irqreturn_t si1145_trigger_handle struct iio_poll_func *pf = private; struct iio_dev *indio_dev = pf->indio_dev; struct si1145_data *data = iio_priv(indio_dev); - /* - * Maximum buffer size: - * 6*2 bytes channels data + 4 bytes alignment + - * 8 bytes timestamp - */ - u8 buffer[24]; int i, j = 0; int ret; u8 irq_status = 0; @@ -482,7 +485,7 @@ static irqreturn_t si1145_trigger_handle ret = i2c_smbus_read_i2c_block_data_or_emulated( data->client, indio_dev->channels[i].address, - sizeof(u16) * run, &buffer[j]); + sizeof(u16) * run, &data->buffer[j]); if (ret < 0) goto done; j += run * sizeof(u16); @@ -497,7 +500,7 @@ static irqreturn_t si1145_trigger_handle goto done; } - iio_push_to_buffers_with_timestamp(indio_dev, buffer, + iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, iio_get_time_ns(indio_dev)); done: