Message ID | 20231003232102.work.430-kees@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2a8e:b0:403:3b70:6f57 with SMTP id in14csp2410613vqb; Tue, 3 Oct 2023 16:21:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEz3oPH4uNGB/i8wFHzd915M4LUc/0YcVF1leA2th1gBkcLrHR2TBEjuxCc+22+aAYSjPcH X-Received: by 2002:a05:6808:2a76:b0:3a1:df16:2eed with SMTP id fu22-20020a0568082a7600b003a1df162eedmr868403oib.30.1696375289072; Tue, 03 Oct 2023 16:21:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696375289; cv=none; d=google.com; s=arc-20160816; b=s3b60yfv2wfM5+vUGVAQnp4YtdWmCk3y6hppoBiA0Q41GsVMWyHrmloYm8tFa/dKm3 8jP9wTsG0RmbFPwgom75nNBlhMb0ZpyR4TtUohODOpEiJ2iV49xb/fmg49Hz6UZLnngc 7p9TwoRLFBJjHVTqPC812laXjLFajzjUUdbwTU+YiZ3YArfcdLpXWdkwlRRq5RMZ5vCN 1O1tqigiR20DQSbOhgrKqkxA5IYTAdosmwmtzJLL4x48OjW2hH1COKmd88OkgM7UhNRJ xcqc8RETD9iq9sWBXiB+ly60P3bkavVWqyf/k7AlAwNXsjyGsUf5JU1aNbR2HOtc6DF0 HceA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=zBFKpyW9pXC/chljQ3RVPO1a7/I5zD11PHOJtNgxGKI=; fh=Hvxf45imcS5IKUHLDUPhqSrfoJinpT/+3sH0Fmt3iLA=; b=ZpRTdP73Qt+oAYBtPZkgtUuk5zup3cGWO1rb6qrXDG9+TuSproqgp8XxFCvJHIQczp G5DtOhS7OOcbIuTwIAreIDmv9LzjhRaRPzs15kgwJiZGI4C+ANCvUv2RVm5p0NhDO5OW 3hMPeD3jhUn72PZpGkmvJ0GhASnirbtIojakOEQIXsfZynANakgiDSNdN2xSWqnU7Dfz qD3WfS61R+pdUnLkGXZzTxRB89y/THsPCBDlRm9LF3J2Ih7B5naNrogiDqUzN9qCFkAu vIP8DTpALEKh02jBCNl7KuByUBkJh4sH6dgIqCdbEuEb6cR5H6PHJl9ipI2Y9vOyLrBX inKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=MiCdz5CO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id 129-20020a630287000000b00563deb65f93si2315190pgc.200.2023.10.03.16.21.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 16:21:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=MiCdz5CO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 3436D804D7D2; Tue, 3 Oct 2023 16:21:26 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235333AbjJCXVO (ORCPT <rfc822;chrisfriedt@gmail.com> + 17 others); Tue, 3 Oct 2023 19:21:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41190 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229805AbjJCXVM (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 3 Oct 2023 19:21:12 -0400 Received: from mail-oi1-x22c.google.com (mail-oi1-x22c.google.com [IPv6:2607:f8b0:4864:20::22c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2CBD0B8 for <linux-kernel@vger.kernel.org>; Tue, 3 Oct 2023 16:21:07 -0700 (PDT) Received: by mail-oi1-x22c.google.com with SMTP id 5614622812f47-3ae2f8bf865so856031b6e.2 for <linux-kernel@vger.kernel.org>; Tue, 03 Oct 2023 16:21:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1696375266; x=1696980066; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=zBFKpyW9pXC/chljQ3RVPO1a7/I5zD11PHOJtNgxGKI=; b=MiCdz5COYwWf9hL/xnHXtxqCz4yQ/SKt5Qf/jcyVDi0wwrLMkHafzR3e8ycB0eH3Lk XNmdCYFjbSYG56n9KoLGqMHDBsR+fip+Ra70CPWpozjJWZf6/kuYJOf0gj7A1LodLT6y IcCxZ4v0Qgp2aV2tJVS539EvZJqChNWtlpzj4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696375266; x=1696980066; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=zBFKpyW9pXC/chljQ3RVPO1a7/I5zD11PHOJtNgxGKI=; b=TVHV8vUao2lXq/7eephZrRmwvJBIqGGlalSsqLamsWJ6n2gL1rSKwVAINOpLcGn4T7 u9WcbaGUPychqOkOJsFweE+kGvyamjXoUANcYJUqQF9BXfBSBQRsOhZPPLO2WY5UggoQ EQiTqMb+P7/HGGjBFjshrXN8XktuYupfevvKUTGtiDvNytUe/4lB0uZMV1qEWeI/FAcm SvOrJDBPInxhTXTIZquiZD3SEfqgZgF+/ouQI4k0EoHrxZk/BZCN7fYwya+L2IheTVl9 Mf9fDmh5wdMgDyOBBYN/GGa4aruUzWnioDr4eyVCRRo/oD13pa+R4fQSjM28cZbZ01Ds bc7Q== X-Gm-Message-State: AOJu0Yzfzz7JVLtZz3po0NUtd5a38u5g72IsXDoNxX6oT3cML2pCWg2+ Kg4ke3+/m1W3kxu4KdDTQNoFsQ== X-Received: by 2002:a05:6358:279c:b0:143:2f7e:aa with SMTP id l28-20020a056358279c00b001432f7e00aamr833720rwb.28.1696375266337; Tue, 03 Oct 2023 16:21:06 -0700 (PDT) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id i7-20020a17090a65c700b00268b9862343sm129724pjs.24.2023.10.03.16.21.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 16:21:05 -0700 (PDT) From: Kees Cook <keescook@chromium.org> To: "David S. Miller" <davem@davemloft.net> Cc: Kees Cook <keescook@chromium.org>, Eric Dumazet <edumazet@google.com>, Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>, Johannes Berg <johannes.berg@intel.com>, netdev@vger.kernel.org, "Gustavo A. R. Silva" <gustavoars@kernel.org>, Nathan Chancellor <nathan@kernel.org>, Nick Desaulniers <ndesaulniers@google.com>, Tom Rix <trix@redhat.com>, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH] netlink: Annotate struct netlink_policy_dump_state with __counted_by Date: Tue, 3 Oct 2023 16:21:02 -0700 Message-Id: <20231003232102.work.430-kees@kernel.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2411; i=keescook@chromium.org; h=from:subject:message-id; bh=AOFOpjhFJ4cNtC6OCXwo5LXfLCQZ4I9UO9j+h4yyfUs=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlHKHeX2lvqJ/DWVLVsK0XgYQdAiBj6vxjhY/Yt h5lBPgBr8iJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZRyh3gAKCRCJcvTf3G3A JpbFD/90/nKxGOgPkTr6m5FPWz2QDZiqlWSseFGtlBSmRfwd74/AKir/9n+h2SPdpiYHhSm/ODj 51C82bHdtlFqYyi2OBiblShtD+quu3qiiSrWWZIT+5K96uTq3AcXZZbi/ovSimsx8/2Obpax6fz 80F/rONc9qVKXV9W/EEnYuNJF3sr79igF094ZYZYM7O69/7xHKfzFnK4pkbwOtVkSPvL2uHfELN 3bcaSwyUuRownQ2+/2nhM5BRhxDIWkQkqyWDmxCCx7mijOa8r6Wd3QjtjoJY/MuQaPrAPz1AHJy 3twQgCD5PnlE5UC4SQs78etQpqHTWwAUp1y4BhbVSA8jIkEBzJZF6DXcnqI52Fk2AYi1thlj1qt tPKeT4/aWr88J8MRfMSCZActXpIt8Yz4Cy8My6kyFZxlRrupFk42VxFZ01yS6td9FrQWj53VKdv qd5MkDrRBBpvDugLxBPLBWoIchJzKvGu0/zMvRBvD3HhqLUL0C6wC4/Iw5z7r6CW5ueDQeOB7/w W4ymWTWy124oUuEo4VWi9MJeZhyNkKN2BW3ryIoxvfaD4XOgt7yNfxbloUafYLZYY+GOLAppurQ UHjRxahSEzRMOZUYDp19l9tcD+ZRz8Gar+GHZUqnjHYm+yQLqroHP49nsGckTdqEpSEOp84biMe B/3psD5 QDKIWtDw== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email 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 (groat.vger.email [0.0.0.0]); Tue, 03 Oct 2023 16:21:26 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778778414987133187 X-GMAIL-MSGID: 1778778414987133187 |
Series |
netlink: Annotate struct netlink_policy_dump_state with __counted_by
|
|
Commit Message
Kees Cook
Oct. 3, 2023, 11:21 p.m. UTC
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).
As found with Coccinelle[1], add __counted_by for struct netlink_policy_dump_state.
Additionally update the size of the usage array length before accessing
it. This requires remembering the old size for the memset() and later
assignments.
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Eric Dumazet <edumazet@google.com>
Cc: Jakub Kicinski <kuba@kernel.org>
Cc: Paolo Abeni <pabeni@redhat.com>
Cc: Johannes Berg <johannes.berg@intel.com>
Cc: netdev@vger.kernel.org
Link: https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci [1]
Signed-off-by: Kees Cook <keescook@chromium.org>
---
net/netlink/policy.c | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)
Comments
On 10/4/23 01:21, Kees Cook wrote: > 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). > > As found with Coccinelle[1], add __counted_by for struct netlink_policy_dump_state. > > Additionally update the size of the usage array length before accessing > it. This requires remembering the old size for the memset() and later > assignments. > > Cc: "David S. Miller" <davem@davemloft.net> > Cc: Eric Dumazet <edumazet@google.com> > Cc: Jakub Kicinski <kuba@kernel.org> > Cc: Paolo Abeni <pabeni@redhat.com> > Cc: Johannes Berg <johannes.berg@intel.com> > Cc: netdev@vger.kernel.org > Link: https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci [1] > Signed-off-by: Kees Cook <keescook@chromium.org> Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org> Thanks -- Gustavo > --- > net/netlink/policy.c | 15 ++++++++------- > 1 file changed, 8 insertions(+), 7 deletions(-) > > diff --git a/net/netlink/policy.c b/net/netlink/policy.c > index 87e3de0fde89..e2f111edf66c 100644 > --- a/net/netlink/policy.c > +++ b/net/netlink/policy.c > @@ -21,7 +21,7 @@ struct netlink_policy_dump_state { > struct { > const struct nla_policy *policy; > unsigned int maxtype; > - } policies[]; > + } policies[] __counted_by(n_alloc); > }; > > static int add_policy(struct netlink_policy_dump_state **statep, > @@ -29,7 +29,7 @@ static int add_policy(struct netlink_policy_dump_state **statep, > unsigned int maxtype) > { > struct netlink_policy_dump_state *state = *statep; > - unsigned int n_alloc, i; > + unsigned int old_n_alloc, n_alloc, i; > > if (!policy || !maxtype) > return 0; > @@ -52,12 +52,13 @@ static int add_policy(struct netlink_policy_dump_state **statep, > if (!state) > return -ENOMEM; > > - memset(&state->policies[state->n_alloc], 0, > - flex_array_size(state, policies, n_alloc - state->n_alloc)); > - > - state->policies[state->n_alloc].policy = policy; > - state->policies[state->n_alloc].maxtype = maxtype; > + old_n_alloc = state->n_alloc; > state->n_alloc = n_alloc; > + memset(&state->policies[old_n_alloc], 0, > + flex_array_size(state, policies, n_alloc - old_n_alloc)); > + > + state->policies[old_n_alloc].policy = policy; > + state->policies[old_n_alloc].maxtype = maxtype; > *statep = state; > > return 0;
Hello: This patch was applied to netdev/net-next.git (main) by David S. Miller <davem@davemloft.net>: On Tue, 3 Oct 2023 16:21:02 -0700 you wrote: > 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). > > As found with Coccinelle[1], add __counted_by for struct netlink_policy_dump_state. > > [...] Here is the summary with links: - netlink: Annotate struct netlink_policy_dump_state with __counted_by https://git.kernel.org/netdev/net-next/c/eaede99c3aeb You are awesome, thank you!
diff --git a/net/netlink/policy.c b/net/netlink/policy.c index 87e3de0fde89..e2f111edf66c 100644 --- a/net/netlink/policy.c +++ b/net/netlink/policy.c @@ -21,7 +21,7 @@ struct netlink_policy_dump_state { struct { const struct nla_policy *policy; unsigned int maxtype; - } policies[]; + } policies[] __counted_by(n_alloc); }; static int add_policy(struct netlink_policy_dump_state **statep, @@ -29,7 +29,7 @@ static int add_policy(struct netlink_policy_dump_state **statep, unsigned int maxtype) { struct netlink_policy_dump_state *state = *statep; - unsigned int n_alloc, i; + unsigned int old_n_alloc, n_alloc, i; if (!policy || !maxtype) return 0; @@ -52,12 +52,13 @@ static int add_policy(struct netlink_policy_dump_state **statep, if (!state) return -ENOMEM; - memset(&state->policies[state->n_alloc], 0, - flex_array_size(state, policies, n_alloc - state->n_alloc)); - - state->policies[state->n_alloc].policy = policy; - state->policies[state->n_alloc].maxtype = maxtype; + old_n_alloc = state->n_alloc; state->n_alloc = n_alloc; + memset(&state->policies[old_n_alloc], 0, + flex_array_size(state, policies, n_alloc - old_n_alloc)); + + state->policies[old_n_alloc].policy = policy; + state->policies[old_n_alloc].maxtype = maxtype; *statep = state; return 0;