Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp800119pxb; Tue, 3 Nov 2020 12:54:02 -0800 (PST) X-Google-Smtp-Source: ABdhPJylR9dKBqNewRZ0IxUnlUrf0RLB0LpU2fByhxK9tkwOfLAd+zDomZQ7en16gtnFJh3eQrMT X-Received: by 2002:a17:907:43c6:: with SMTP id i6mr21360069ejs.207.1604436842388; Tue, 03 Nov 2020 12:54:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604436842; cv=none; d=google.com; s=arc-20160816; b=amnhDVKydEV8nHw5DasXxizDUQMfwh0xGjlf3dyKdozZnblkEu7ql4XRLuq1CZ7LAC IqmpUD1Wy7pVd/OocmYwQ3Vd6zyHrqv7ZbZIhz1AIaGyBn6Idd8F53ntPw7zKjnwGYCn AmPya73bTeNNhlX2CBGIhq33qV1rTac02rWDwIAdQ42r0w8JOrNBJ8iwb9GENlvX1vmG 47XO7XKPJ8EeZ2gaFv5JCH1T8I5EWC8zeBNoXx8gbYZ1zWqr6sP6Tny7DaPZF2dcSF78 McJsneJslvh9T1v7tzkwc/Qp/wxup2Rwp0Cn2uXp+yauyWY70eJZSiIcex0tfgE4bGjL y89Q== 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=aqE2PwjCHo6HQGSNGcYYAbyQMrFQC5Y26KaHKIkwI9E=; b=KBUvzOmKF80Uj4K5mKnmqNW+6aIvkFIqrd5I+MVKgtfl+UFYis884D1iKliF1LBWNn Qlqwcv0aDlTocSWp3qiS2+GMBgSExCmf0OLwH9xvk/z1vLEaSZ4ZNXLeT1DFLtEYE4bp JfjjptsDYPrU4VFYboT29G0nqaXha+8AW2EMQYPBr5Nquu0R/ZWG0DiATE+2vAjni1UO J4ByYw10Kn/h3KToDo11Y5lXcfn2B/aJPp1ek2gXGWzcR/KrX6M4iwHGYtT/UGbS96L4 D0E2QxNqwk0TU1waHGV/1ITtQsz+Jb9rlZ3LcZUF/xKJCEj5GZ0zj8RUsckpNtwqPv5J WIqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=iHcat1eo; 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 f20si129480edq.210.2020.11.03.12.53.39; Tue, 03 Nov 2020 12:54:02 -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=iHcat1eo; 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 S1731096AbgKCUtK (ORCPT + 99 others); Tue, 3 Nov 2020 15:49:10 -0500 Received: from mail.kernel.org ([198.145.29.99]:39252 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731531AbgKCUsH (ORCPT ); Tue, 3 Nov 2020 15:48:07 -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 C18BC22409; Tue, 3 Nov 2020 20:48:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604436487; bh=NnvJSJlVMoj2kyRS0C8Hwv9jOxh21h/gjBZ2Hof5NZM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iHcat1eodIV6SZhmnT47E/IexSrIbsycufPaXkcLcLfpJwGRx57ROl+6z4P7HnM2e ofyl/uZe3EJAEmxBOpHLuuKZ31jn9yLFOCwvb1UFuzeYlP/Oy72egkH+TBEAY1jpzH NrLa9KV9rlfITmxlM08Kbm7mZdnKYClTod3FfHZE= 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 5.9 272/391] iio:light:si1145: Fix timestamp alignment and prevent data leak. Date: Tue, 3 Nov 2020 21:35:23 +0100 Message-Id: <20201103203405.386201658@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201103203348.153465465@linuxfoundation.org> References: <20201103203348.153465465@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 @@ -168,6 +168,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; @@ -179,6 +180,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); }; /* @@ -440,12 +449,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; @@ -478,7 +481,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); @@ -493,7 +496,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: