Skip to content

Commit fd86eff

Browse files
keesweiny2
authored andcommitted
libnvdimm: Annotate struct nd_region with __counted_by
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 nd_region. 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: Dan Williams <[email protected]> Cc: Vishal Verma <[email protected]> Cc: Dave Jiang <[email protected]> Cc: Ira Weiny <[email protected]> Cc: [email protected] Signed-off-by: Kees Cook <[email protected]> Reviewed-by: Gustavo A. R. Silva <[email protected]> Reviewed-by: Vishal Verma <[email protected]> Reviewed-by: Dave Jiang <[email protected]> Signed-off-by: Ira Weiny <[email protected]>
1 parent 36c75ce commit fd86eff

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

drivers/nvdimm/nd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ struct nd_region {
422422
struct nd_interleave_set *nd_set;
423423
struct nd_percpu_lane __percpu *lane;
424424
int (*flush)(struct nd_region *nd_region, struct bio *bio);
425-
struct nd_mapping mapping[];
425+
struct nd_mapping mapping[] __counted_by(ndr_mappings);
426426
};
427427

428428
static inline bool nsl_validate_nlabel(struct nd_region *nd_region,

drivers/nvdimm/region_devs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,7 @@ static struct nd_region *nd_region_create(struct nvdimm_bus *nvdimm_bus,
10281028

10291029
if (!nd_region)
10301030
return NULL;
1031+
nd_region->ndr_mappings = ndr_desc->num_mappings;
10311032
/* CXL pre-assigns memregion ids before creating nvdimm regions */
10321033
if (test_bit(ND_REGION_CXL, &ndr_desc->flags)) {
10331034
nd_region->id = ndr_desc->memregion;
@@ -1062,7 +1063,6 @@ static struct nd_region *nd_region_create(struct nvdimm_bus *nvdimm_bus,
10621063

10631064
get_device(&nvdimm->dev);
10641065
}
1065-
nd_region->ndr_mappings = ndr_desc->num_mappings;
10661066
nd_region->provider_data = ndr_desc->provider_data;
10671067
nd_region->nd_set = ndr_desc->nd_set;
10681068
nd_region->num_lanes = ndr_desc->num_lanes;

0 commit comments

Comments
 (0)