net: sched: Annotate struct tc_pedit with __counted_by
authorKees Cook <keescook@chromium.org>
Fri, 16 Feb 2024 23:27:44 +0000 (15:27 -0800)
committerDavid S. Miller <davem@davemloft.net>
Mon, 19 Feb 2024 10:58:24 +0000 (10:58 +0000)
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 checking 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 tc_pedit.
Additionally, since the element count member must be set before accessing
the annotated flexible array member, move its initialization earlier.

Link: https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci
Signed-off-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Acked-by: Jamal Hadi Salim <jhs@mojatatu.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/uapi/linux/tc_act/tc_pedit.h
net/sched/act_pedit.c

index f3e61b04fa01b4f728c74a8ef0589040f8e62349..f5cab7fc96ab1ea1516beeedff20b279c3cfa4d7 100644 (file)
@@ -62,7 +62,7 @@ struct tc_pedit_sel {
        tc_gen;
        unsigned char           nkeys;
        unsigned char           flags;
-       struct tc_pedit_key     keys[0];
+       struct tc_pedit_key     keys[] __counted_by(nkeys);
 };
 
 #define tc_pedit tc_pedit_sel
index df5a02d5f919c3e8b26308f5128ee35f63e5401a..fc0a35a7b62ac7a550f8f03d4a424f7b5ce5b51c 100644 (file)
@@ -515,11 +515,11 @@ static int tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,
                spin_unlock_bh(&p->tcf_lock);
                return -ENOBUFS;
        }
+       opt->nkeys = parms->tcfp_nkeys;
 
        memcpy(opt->keys, parms->tcfp_keys,
               flex_array_size(opt, keys, parms->tcfp_nkeys));
        opt->index = p->tcf_index;
-       opt->nkeys = parms->tcfp_nkeys;
        opt->flags = parms->tcfp_flags;
        opt->action = p->tcf_action;
        opt->refcnt = refcount_read(&p->tcf_refcnt) - ref;