Received: by 2002:a05:7208:3188:b0:7e:5202:c8b4 with SMTP id r8csp976180rbd; Fri, 23 Feb 2024 09:09:32 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWz+REgKqL0ZJQO1ICEzAXRp5F7jw9KkCy4HDWpKvQRzGm74ppg+p8dV3//pPm1LC4960m6KYD1vEeFekEr7AQopPT3lu3+X365IYKKHQ== X-Google-Smtp-Source: AGHT+IHVZAttTR2gQnm8k1bPjyZhicmLPI/5BSS6qB1JaKlE5zZiNTt/ug5Rp5OoqpbR6h9oannI X-Received: by 2002:a17:903:1111:b0:1dc:7b6:867a with SMTP id n17-20020a170903111100b001dc07b6867amr283631plh.21.1708708172562; Fri, 23 Feb 2024 09:09:32 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708708172; cv=pass; d=google.com; s=arc-20160816; b=wfEGV2+FruhqsVWAhx9WR2F3aThCPDVFcyTETFum1V9a06ufHnx0J93J2dLDeqF1Q4 j0zwov4ffvgeBHyuzFiakNMZZSUsg/9IOoto8YSkNsyCwbSwQ/w5q5vH4E/2OS5Asbvn +ZEvTQDnfG8hRgueCckbsYgPFrN4DjNNPAAviERah1FZeWKLxZWVFfdoXstwwm0TXG7w v5ccPHvDOrU5IJ3lPKpFsIMjhOsKmxUDBFd7xGQzyfa3vf7oMuMAE8D66R0H/qrEVwL2 Nhh2IVPhLI8Do5XlS7L1Fw1ldS+dJtanuPOgQ9aePneoP6jxTz1BuvYGPtwxxzIjx59o TaCA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:organization:references :in-reply-to:message-id:subject:cc:to:from:date; bh=2SlIEBARZsqrC/ab2yhTSxGRM9j6IXouOvz0ZcTgdGk=; fh=ZjUSewBdMSRF0+t57yk0Ed1/9/oN7rAHAn5TRHjp1e4=; b=SY8x+9F2w/pJLH8p8KMwzEVuQC7MLiYlA+EwkiWcPSYaGb8p24WvGXeOrXbtNZfg0c M0DzawssRxPzAWfGgxZcNOOk5w4pHszAltonXKhN6i0wis1kMofLrrLLocMEmroJTTfk /6Ejw6qwYFo8EziWQTL+PQBvWuw4KK3QMTI0Ueupw8krPedkKfIQSRMvAIDKwkyltHR1 xALTQrNtbu0VV6ubt78A00re7PVoKjKxfnhkSv7A9uxFQQpUonODH1Le1o5a81uAP+MN uuaL+Kfr9FKHnaVYx3mtvS3Fryn1g0F0t+QxY5NX+FYV1XCzgxocFwlA77+dHzcTEZxP g+2Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-78792-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-78792-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id mc11-20020a1709032b0b00b001d9e8a8809bsi7799225plb.600.2024.02.23.09.09.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Feb 2024 09:09:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-78792-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-78792-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-78792-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id CC1F02877B3 for ; Fri, 23 Feb 2024 17:09:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CE03612BEBC; Fri, 23 Feb 2024 17:09:26 +0000 (UTC) Received: from frasgout.his.huawei.com (frasgout.his.huawei.com [185.176.79.56]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 06966129A76; Fri, 23 Feb 2024 17:09:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.176.79.56 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708708166; cv=none; b=ZC2vs+kwY6/ofv75IWscQ8G1fGIgWxCpXJ7/pj5rEgT0Y7GNR5i3v97pPrv9GNWVJ8nbqP4hKWDX7ww4K3TQ57HQRpGtKLTGaBD7ZOLVjx87GpEc+yzESapYWYqvcuqd4joek4YOGCenDKr5tCTCMEqW+fLyl0Zefj0SMafkeKM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708708166; c=relaxed/simple; bh=c2F4ATjzYPy88wzTfQiSLZxU8flLRd+N5pfPB2OqJw4=; h=Date:From:To:CC:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ELakZg/os0WcJoX7zC0iYlsvjZAiM2/3cqmjeGSTbeQr0ajyXZ7dkUc+HeQCDINxL0R/dFFQR249cyXduohpdrmAKbGyH0k/A0FYjLIGEpdNIiI0kh9XXRJUYSMci/Fa4qSMPWpONdpvEmzHHbw/rQjtFvKTSz3eYDNWuH+5wpM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=Huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=185.176.79.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=Huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.18.186.216]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4ThGbW00nHz6899K; Sat, 24 Feb 2024 01:05:11 +0800 (CST) Received: from lhrpeml500005.china.huawei.com (unknown [7.191.163.240]) by mail.maildlp.com (Postfix) with ESMTPS id A7FA7140684; Sat, 24 Feb 2024 01:09:20 +0800 (CST) Received: from localhost (10.202.227.76) by lhrpeml500005.china.huawei.com (7.191.163.240) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Fri, 23 Feb 2024 17:09:20 +0000 Date: Fri, 23 Feb 2024 17:09:18 +0000 From: Jonathan Cameron To: Kees Cook CC: Jonathan Cameron , Lars-Peter Clausen , Uwe =?ISO-8859-1?Q?Kleine-K=F6nig?= , Andy Shevchenko , Nuno =?ISO-8859-1?Q?S=E1?= , , Nathan Chancellor , Nick Desaulniers , "Bill Wendling" , Justin Stitt , , , Subject: Re: [PATCH] [RFC] iio: pressure: dlhl60d: Check mask_width for IRQs Message-ID: <20240223170918.00006b16@Huawei.com> In-Reply-To: <20240222222335.work.759-kees@kernel.org> References: <20240222222335.work.759-kees@kernel.org> Organization: Huawei Technologies Research and Development (UK) Ltd. X-Mailer: Claws Mail 4.1.0 (GTK 3.24.33; x86_64-w64-mingw32) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: lhrpeml100006.china.huawei.com (7.191.160.224) To lhrpeml500005.china.huawei.com (7.191.163.240) On Thu, 22 Feb 2024 14:23:39 -0800 Kees Cook wrote: > Clang tripped over a FORTIFY warning in this code, and while it seems it > may be a false positive in Clang due to loop unwinding, the code in > question seems to make a lot of assumptions.=20 Hi Kees, The assumptions are mostly characteristics of how the IIO buffers work with the scan masks defined based on indexes in the driver provided struct iio_chan_spec arrays. This driver is doing more work than it should need to as we long ago moved some of the more fiddly handling into the IIO core. > Comments added, and the > Clang warning[1] has been worked around by growing the array size. > Also there was an uninitialized 4th byte in the __be32 array that was > being sent through to iio_push_to_buffers(). That is indeed not good - the buffer should have been zero initialized. >=20 > Link: https://github.com/ClangBuiltLinux/linux/issues/2000 [1] > Signed-off-by: Kees Cook > --- > Cc: Jonathan Cameron > Cc: Lars-Peter Clausen > Cc: "Uwe Kleine-K=F6nig" > Cc: Andy Shevchenko > Cc: "Nuno S=E1" > Cc: linux-iio@vger.kernel.org > --- > drivers/iio/pressure/dlhl60d.c | 11 +++++++++-- > 1 file changed, 9 insertions(+), 2 deletions(-) >=20 > diff --git a/drivers/iio/pressure/dlhl60d.c b/drivers/iio/pressure/dlhl60= d.c > index 28c8269ba65d..9bbecd0bfe88 100644 > --- a/drivers/iio/pressure/dlhl60d.c > +++ b/drivers/iio/pressure/dlhl60d.c > @@ -250,20 +250,27 @@ static irqreturn_t dlh_trigger_handler(int irq, voi= d *private) > struct dlh_state *st =3D iio_priv(indio_dev); > int ret; > unsigned int chn, i =3D 0; > - __be32 tmp_buf[2]; > + /* This was only an array pair of 4 bytes. */ True, which is the right size as far as I can tell. If we need this to suppress a warning then comment should say that. > + __be32 tmp_buf[4] =3D { }; > =20 > ret =3D dlh_start_capture_and_read(st); > if (ret) > goto out; > =20 > + /* Nothing was checking masklength vs ARRAY_SIZE(tmp_buf)? */ Not needed but no way a compiler could know that. > + if (WARN_ON_ONCE(indio_dev->masklength > ARRAY_SIZE(tmp_buf))) > + goto out; > + > for_each_set_bit(chn, indio_dev->active_scan_mask, This is all a bit pointless if not 'wrong' other than the 4th byte uninitialized part. The limit can be hard coded as 2 as that's a characteristic of this driver. For device that always read a particular set of channels they should provide indio_dev->available_scan_masks =3D { BIT(1) | BIT(0), 0 }; and then always push all the data making this always memcpy(&tmp_buf[0], &st->rx_buf[1], 3); mempcy(&tmp_buf[1], &st->rx_buf[1] + 3, 3); The buffer demux code in the IIO core will deal with repacking the data if only one channel is enabled. > indio_dev->masklength) { > - memcpy(tmp_buf + i, > + /* This is copying 3 bytes. What about the 4th? */ > + memcpy(&tmp_buf[i], > &st->rx_buf[1] + chn * DLH_NUM_DATA_BYTES, > DLH_NUM_DATA_BYTES); > i++; > } > =20 > + /* How do we know the iio buffer_list has only 2 items? */ Can only include items from the channels array at indexes up to the max scan_index in there, so 0 and 1 in this case (1 might not be present if only one channel is enabled). Sizes (and alignment) are given by storagebits so 4 bytes for each. > iio_push_to_buffers(indio_dev, tmp_buf); > =20 > out: