[next] nouveau/gsp: replace zero-length array with flex-array member and use __counted_by
Message ID | ZVZbX7C5suLMiBf+@work |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6359:6513:b0:164:83eb:24d7 with SMTP id sk19csp44866rwb; Thu, 16 Nov 2023 10:11:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IFhs41g7c5DefdZZZLvrVm7dc2e9kCasegL4qnXyLBWh8XDHjTUhivn0c3Q3OeeRcCeimCQ X-Received: by 2002:a17:903:485:b0:1cc:38e6:f097 with SMTP id jj5-20020a170903048500b001cc38e6f097mr3027742plb.7.1700158316684; Thu, 16 Nov 2023 10:11:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700158316; cv=none; d=google.com; s=arc-20160816; b=xcDbzSQYhAAYEorYzW/aDqi0/YXKb7zWzQ/q+UFakyYKjMvHMYwSfs37AOi+k++XxK QIWku4DO71+zZljqdw0Tp/7MyiWYOZSeKSxMI8ZKjxF8MPfEimj/kS9ClrY9Sy7e2MFp JeL5pOW+5StQfPGNDC4dbZH4KAqDoyGXjFG3SPCushl7ZRM1FqF9s4o+ySlR1k9ciakW ALvK32wbckpy2NT42Kk/GEhkAh+NwZqynA9sPppCZjU84fO4H4AM2bfJoUiqa56e8krP adkA0L9TdM4kvi5B3dJ7KvJBOKUHLFfNEuPTE9Pf//Zr/S7WZspGmeU3Xv7tHOIO7yM2 h+9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=7COaxt3xzH7UjhmVz9OyINuerYqwq8MFg6ZjyrNlOdQ=; fh=/vuI8n/B3WroOhV3WMcnPgTtiSjPxV8MZY09bqvLmmM=; b=urlhTx5xdW52gZel8OqCAc4z4S6U21c/yvLRI64+OBhFneVATUOjAwOxhZkfdZ2VHY hGOUiWzIbOVVJ/+dEKjRdyqZAnLUK3Id/mIkfKXz5xVwJ6Ggvtv0xDA4R8cPjW8QAXcK J9m85B8UYyrDBO7uhVudt7QNtTx7orpODHEJlquF4aNRRGE7ayH23usJsVlxMRc1ZPu4 gfU665ZF7X/p4X6qwLQcUifvTx8T+asXhegbHchygU+NdC21XymqU9/eGTVK7v62lW/y wlrvmU2SDL5LqJ9T8mo1Wra6t8XCQx9UdyfZDX3peXTo+Wp4NAh9BtV+pZatdxco2joI JLgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=la6R0gS4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id z17-20020a170903019100b001c6182d9fdasi13384600plg.326.2023.11.16.10.11.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Nov 2023 10:11:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=la6R0gS4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 8DD7680C245A; Thu, 16 Nov 2023 10:11:54 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229841AbjKPSLw (ORCPT <rfc822;jaysivo@gmail.com> + 30 others); Thu, 16 Nov 2023 13:11:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229464AbjKPSLu (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 16 Nov 2023 13:11:50 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A3B79195 for <linux-kernel@vger.kernel.org>; Thu, 16 Nov 2023 10:11:47 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1E2C7C433C9; Thu, 16 Nov 2023 18:11:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700158307; bh=Ry4mQQ1q/vn/j3+Dj1Ms0K/kq2DMCGX9s61dOISwTRo=; h=Date:From:To:Cc:Subject:From; b=la6R0gS4A9sua9wheZyCxfbRGCUfMXibabY/Ch7ODIveMD52xRHRGABVg9QniPV3L /5Z5+C+0ck49xRi+27ggYHvoNZ3Oq4AxSk6SqMgLOYo+Q56/tu7/awDVwGKwQU3SG3 VEgi/EuHhu7B8nSgTEx7dl4Q7xjz2zJL6LLENK82zgRCCPA5RZYClmRkDBA7TsP1+5 8LkS2PdipAWqokVc9o8iBKQ3B0Z0al6fKdNvMtOHlA3MylPEzhnCFMQhedTPgQd9Bs ka46ygRkuiJqgbpkdHucUNQ2Vj94TTjKwtYlHTz8Wu4/J3Wm9yRZztnQU8cgsHZ9CS Jcg/Mtvj3mylA== Date: Thu, 16 Nov 2023 12:11:43 -0600 From: "Gustavo A. R. Silva" <gustavoars@kernel.org> To: Karol Herbst <kherbst@redhat.com>, Lyude Paul <lyude@redhat.com>, Danilo Krummrich <dakr@redhat.com>, David Airlie <airlied@gmail.com>, Daniel Vetter <daniel@ffwll.ch> Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" <gustavoars@kernel.org>, linux-hardening@vger.kernel.org Subject: [PATCH][next] nouveau/gsp: replace zero-length array with flex-array member and use __counted_by Message-ID: <ZVZbX7C5suLMiBf+@work> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 16 Nov 2023 10:11:54 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782745206969977329 X-GMAIL-MSGID: 1782745206969977329 |
Series |
[next] nouveau/gsp: replace zero-length array with flex-array member and use __counted_by
|
|
Commit Message
Gustavo A. R. Silva
Nov. 16, 2023, 6:11 p.m. UTC
Fake flexible arrays (zero-length and one-element arrays) are deprecated,
and should be replaced by flexible-array members. So, replace
zero-length array with a flexible-array member in `struct
PACKED_REGISTRY_TABLE`.
Also annotate array `entries` with `__counted_by()` to prepare for the
coming implementation by GCC and Clang of the `__counted_by` attribute.
Flexible array members annotated with `__counted_by` can have their
accesses bounds-checked at run-time via `CONFIG_UBSAN_BOUNDS` (for array
indexing) and `CONFIG_FORTIFY_SOURCE` (for strcpy/memcpy-family functions).
This fixes multiple -Warray-bounds warnings:
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c:1069:29: warning: array subscript 0 is outside array bounds of 'PACKED_REGISTRY_ENTRY[0]' [-Warray-bounds=]
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c:1070:29: warning: array subscript 0 is outside array bounds of 'PACKED_REGISTRY_ENTRY[0]' [-Warray-bounds=]
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c:1071:29: warning: array subscript 0 is outside array bounds of 'PACKED_REGISTRY_ENTRY[0]' [-Warray-bounds=]
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c:1072:29: warning: array subscript 0 is outside array bounds of 'PACKED_REGISTRY_ENTRY[0]' [-Warray-bounds=]
While there, also make use of the struct_size() helper, and address
checkpatch.pl warning:
WARNING: please, no spaces at the start of a line
This results in no differences in binary output.
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
.../nvrm/535.113.01/nvidia/generated/g_os_nvoc.h | 14 +++++++-------
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c | 2 +-
2 files changed, 8 insertions(+), 8 deletions(-)
Comments
On Thu, Nov 16, 2023 at 12:11:43PM -0600, Gustavo A. R. Silva wrote: > Fake flexible arrays (zero-length and one-element arrays) are deprecated, > and should be replaced by flexible-array members. So, replace > zero-length array with a flexible-array member in `struct > PACKED_REGISTRY_TABLE`. > > Also annotate array `entries` with `__counted_by()` to prepare for the > coming implementation by GCC and Clang of the `__counted_by` attribute. > Flexible array members annotated with `__counted_by` can have their > accesses bounds-checked at run-time via `CONFIG_UBSAN_BOUNDS` (for array > indexing) and `CONFIG_FORTIFY_SOURCE` (for strcpy/memcpy-family functions). > > This fixes multiple -Warray-bounds warnings: > drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c:1069:29: warning: array subscript 0 is outside array bounds of 'PACKED_REGISTRY_ENTRY[0]' [-Warray-bounds=] > drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c:1070:29: warning: array subscript 0 is outside array bounds of 'PACKED_REGISTRY_ENTRY[0]' [-Warray-bounds=] > drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c:1071:29: warning: array subscript 0 is outside array bounds of 'PACKED_REGISTRY_ENTRY[0]' [-Warray-bounds=] > drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c:1072:29: warning: array subscript 0 is outside array bounds of 'PACKED_REGISTRY_ENTRY[0]' [-Warray-bounds=] > > While there, also make use of the struct_size() helper, and address > checkpatch.pl warning: > WARNING: please, no spaces at the start of a line > > This results in no differences in binary output. > > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> Looks nice to me. Reviewed-by: Kees Cook <keescook@chromium.org>
Hi Gustavo, On Thu, Nov 16, 2023 at 12:11:43PM -0600, Gustavo A. R. Silva wrote: > Fake flexible arrays (zero-length and one-element arrays) are deprecated, > and should be replaced by flexible-array members. So, replace > zero-length array with a flexible-array member in `struct > PACKED_REGISTRY_TABLE`. > > Also annotate array `entries` with `__counted_by()` to prepare for the > coming implementation by GCC and Clang of the `__counted_by` attribute. > Flexible array members annotated with `__counted_by` can have their > accesses bounds-checked at run-time via `CONFIG_UBSAN_BOUNDS` (for array > indexing) and `CONFIG_FORTIFY_SOURCE` (for strcpy/memcpy-family functions). > > This fixes multiple -Warray-bounds warnings: > drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c:1069:29: warning: array subscript 0 is outside array bounds of 'PACKED_REGISTRY_ENTRY[0]' [-Warray-bounds=] > drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c:1070:29: warning: array subscript 0 is outside array bounds of 'PACKED_REGISTRY_ENTRY[0]' [-Warray-bounds=] > drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c:1071:29: warning: array subscript 0 is outside array bounds of 'PACKED_REGISTRY_ENTRY[0]' [-Warray-bounds=] > drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c:1072:29: warning: array subscript 0 is outside array bounds of 'PACKED_REGISTRY_ENTRY[0]' [-Warray-bounds=] > > While there, also make use of the struct_size() helper, and address > checkpatch.pl warning: > WARNING: please, no spaces at the start of a line > > This results in no differences in binary output. > > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> > --- > .../nvrm/535.113.01/nvidia/generated/g_os_nvoc.h | 14 +++++++------- > drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c | 2 +- > 2 files changed, 8 insertions(+), 8 deletions(-) > > diff --git a/drivers/gpu/drm/nouveau/include/nvrm/535.113.01/nvidia/generated/g_os_nvoc.h b/drivers/gpu/drm/nouveau/include/nvrm/535.113.01/nvidia/generated/g_os_nvoc.h > index 754c6af42f30..259b25c2ac6b 100644 > --- a/drivers/gpu/drm/nouveau/include/nvrm/535.113.01/nvidia/generated/g_os_nvoc.h > +++ b/drivers/gpu/drm/nouveau/include/nvrm/535.113.01/nvidia/generated/g_os_nvoc.h > @@ -28,17 +28,17 @@ > > typedef struct PACKED_REGISTRY_ENTRY > { > - NvU32 nameOffset; > - NvU8 type; > - NvU32 data; > - NvU32 length; > + NvU32 nameOffset; > + NvU8 type; > + NvU32 data; > + NvU32 length; > } PACKED_REGISTRY_ENTRY; > > typedef struct PACKED_REGISTRY_TABLE > { > - NvU32 size; > - NvU32 numEntries; > - PACKED_REGISTRY_ENTRY entries[0]; > + NvU32 size; > + NvU32 numEntries; > + PACKED_REGISTRY_ENTRY entries[] __counted_by(numEntries); > } PACKED_REGISTRY_TABLE; Thanks for the fix! However, I have some concerns about changing those header files, since they're just copied over from Nvidia's driver [1]. Once we add the header files for a new firmware revision, we'd potentially run into the same issue, applying the same fix again. As I already mentioned for Timur's patch [2], I'd prefer to get a fix upstream (meaning [1] in this case). Of course, that's probably more up to Timur to tell if this will work out. If we can't get a fix upstream, I'd probably prefer to silence warning elsewhere. [1] https://github.com/NVIDIA/open-gpu-kernel-modules [2] https://lore.kernel.org/all/20231107234726.854248-1-ttabi@nvidia.com/T/ > > #endif > diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c b/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c > index dc44f5c7833f..228335487af5 100644 > --- a/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c > +++ b/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c > @@ -1048,7 +1048,7 @@ r535_gsp_rpc_set_registry(struct nvkm_gsp *gsp) > char *strings; > int str_offset; > int i; > - size_t rpc_size = sizeof(*rpc) + sizeof(rpc->entries[0]) * NV_GSP_REG_NUM_ENTRIES; > + size_t rpc_size = struct_size(rpc, entries, NV_GSP_REG_NUM_ENTRIES); > > /* add strings + null terminator */ > for (i = 0; i < NV_GSP_REG_NUM_ENTRIES; i++) > -- > 2.34.1 >
On Thu, 2023-11-16 at 20:45 +0100, Danilo Krummrich wrote: > As I already mentioned for Timur's patch [2], I'd prefer to get a fix > upstream > (meaning [1] in this case). Of course, that's probably more up to Timur to > tell > if this will work out. Don't count on it. Even if I did change [0] to [], I'm not going to be able to add the "__counted_by(numEntries);" because that's just not something that our build system uses. And even then, I would need to change all [0] to []. You're not going to be able to use RM's header files as-is anyway in the long term. If we changed the layout of PACKED_REGISTRY_TABLE, we're not going to create a PACKED_REGISTRY_TABLE2 and keep both around. We're just going to change PACKED_REGISTRY_TABLE and pretend the previous version never existed. You will then have to manually copy the new struct to your header files and and maintain two versions yourself.
On 11/16/23 20:55, Timur Tabi wrote: > On Thu, 2023-11-16 at 20:45 +0100, Danilo Krummrich wrote: >> As I already mentioned for Timur's patch [2], I'd prefer to get a fix >> upstream >> (meaning [1] in this case). Of course, that's probably more up to Timur to >> tell >> if this will work out. > > Don't count on it. I see. Well, I think it's fine. Once we implement a decent abstraction we likely don't need those header files in the kernel anymore. @Gustavo, if you agree I will discard the indentation change when applying the patch to keep the diff as small as possible. - Danilo > > Even if I did change [0] to [], I'm not going to be able to add the > "__counted_by(numEntries);" because that's just not something that our build > system uses. > > And even then, I would need to change all [0] to []. > > You're not going to be able to use RM's header files as-is anyway in the > long term. If we changed the layout of PACKED_REGISTRY_TABLE, we're not > going to create a PACKED_REGISTRY_TABLE2 and keep both around. We're just > going to change PACKED_REGISTRY_TABLE and pretend the previous version never > existed. You will then have to manually copy the new struct to your header > files and and maintain two versions yourself. > > >
On 11/28/23 19:01, Danilo Krummrich wrote: > On 11/16/23 20:55, Timur Tabi wrote: >> On Thu, 2023-11-16 at 20:45 +0100, Danilo Krummrich wrote: >>> As I already mentioned for Timur's patch [2], I'd prefer to get a fix >>> upstream >>> (meaning [1] in this case). Of course, that's probably more up to Timur to >>> tell >>> if this will work out. >> >> Don't count on it. > > I see. Well, I think it's fine. Once we implement a decent abstraction we likely > don't need those header files in the kernel anymore. > > @Gustavo, if you agree I will discard the indentation change when applying the > patch to keep the diff as small as possible. No problem. Thanks -- Gustavo
On 11/29/23 02:06, Gustavo A. R. Silva wrote: > > > On 11/28/23 19:01, Danilo Krummrich wrote: >> On 11/16/23 20:55, Timur Tabi wrote: >>> On Thu, 2023-11-16 at 20:45 +0100, Danilo Krummrich wrote: >>>> As I already mentioned for Timur's patch [2], I'd prefer to get a fix >>>> upstream >>>> (meaning [1] in this case). Of course, that's probably more up to Timur to >>>> tell >>>> if this will work out. >>> >>> Don't count on it. >> >> I see. Well, I think it's fine. Once we implement a decent abstraction we likely >> don't need those header files in the kernel anymore. >> >> @Gustavo, if you agree I will discard the indentation change when applying the >> patch to keep the diff as small as possible. > > No problem. Applied to drm-misc-fixes. > > Thanks > -- > Gustavo > >
diff --git a/drivers/gpu/drm/nouveau/include/nvrm/535.113.01/nvidia/generated/g_os_nvoc.h b/drivers/gpu/drm/nouveau/include/nvrm/535.113.01/nvidia/generated/g_os_nvoc.h index 754c6af42f30..259b25c2ac6b 100644 --- a/drivers/gpu/drm/nouveau/include/nvrm/535.113.01/nvidia/generated/g_os_nvoc.h +++ b/drivers/gpu/drm/nouveau/include/nvrm/535.113.01/nvidia/generated/g_os_nvoc.h @@ -28,17 +28,17 @@ typedef struct PACKED_REGISTRY_ENTRY { - NvU32 nameOffset; - NvU8 type; - NvU32 data; - NvU32 length; + NvU32 nameOffset; + NvU8 type; + NvU32 data; + NvU32 length; } PACKED_REGISTRY_ENTRY; typedef struct PACKED_REGISTRY_TABLE { - NvU32 size; - NvU32 numEntries; - PACKED_REGISTRY_ENTRY entries[0]; + NvU32 size; + NvU32 numEntries; + PACKED_REGISTRY_ENTRY entries[] __counted_by(numEntries); } PACKED_REGISTRY_TABLE; #endif diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c b/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c index dc44f5c7833f..228335487af5 100644 --- a/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c +++ b/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c @@ -1048,7 +1048,7 @@ r535_gsp_rpc_set_registry(struct nvkm_gsp *gsp) char *strings; int str_offset; int i; - size_t rpc_size = sizeof(*rpc) + sizeof(rpc->entries[0]) * NV_GSP_REG_NUM_ENTRIES; + size_t rpc_size = struct_size(rpc, entries, NV_GSP_REG_NUM_ENTRIES); /* add strings + null terminator */ for (i = 0; i < NV_GSP_REG_NUM_ENTRIES; i++)