ceph: Annotate struct ceph_monmap with __counted_by
authorKees Cook <keescook@chromium.org>
Fri, 15 Sep 2023 20:15:10 +0000 (13:15 -0700)
committerDavid S. Miller <davem@davemloft.net>
Mon, 18 Sep 2023 09:39:29 +0000 (10:39 +0100)
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 ceph_monmap.
Additionally, since the element count member must be set before accessing
the annotated flexible array member, move its initialization earlier.

[1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci

Cc: Ilya Dryomov <idryomov@gmail.com>
Cc: Xiubo Li <xiubli@redhat.com>
Cc: Jeff Layton <jlayton@kernel.org>
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: ceph-devel@vger.kernel.org
Cc: netdev@vger.kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Reviewed-by: Xiubo Li <xiubli@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/linux/ceph/mon_client.h
net/ceph/mon_client.c

index b658961156a036d88950d8d7ebc96f146ff4c2b6..7a9a40163c0f7dd1bda00faa2e68651aa8d2e286 100644 (file)
@@ -19,7 +19,7 @@ struct ceph_monmap {
        struct ceph_fsid fsid;
        u32 epoch;
        u32 num_mon;
-       struct ceph_entity_inst mon_inst[];
+       struct ceph_entity_inst mon_inst[] __counted_by(num_mon);
 };
 
 struct ceph_mon_client;
index faabad6603db29263d11682afbf3b1dad2ca9e69..f263f7e91a219bbfa917c782ea1c8d9c4e236d9e 100644 (file)
@@ -1136,6 +1136,7 @@ static int build_initial_monmap(struct ceph_mon_client *monc)
                               GFP_KERNEL);
        if (!monc->monmap)
                return -ENOMEM;
+       monc->monmap->num_mon = num_mon;
 
        for (i = 0; i < num_mon; i++) {
                struct ceph_entity_inst *inst = &monc->monmap->mon_inst[i];
@@ -1147,7 +1148,6 @@ static int build_initial_monmap(struct ceph_mon_client *monc)
                inst->name.type = CEPH_ENTITY_TYPE_MON;
                inst->name.num = cpu_to_le64(i);
        }
-       monc->monmap->num_mon = num_mon;
        return 0;
 }