Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

reset: Annotate struct reset_control_array 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 reset_control_array.
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: Philipp Zabel <p.zabel@pengutronix.de>
Reviewed-by: "Gustavo A. R. Silva" <gustavoars@kernel.org>
Link: https://lore.kernel.org/r/20230922175229.work.838-kees@kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>

+2 -2
+2 -2
drivers/reset/core.c
··· 60 60 struct reset_control_array { 61 61 struct reset_control base; 62 62 unsigned int num_rstcs; 63 - struct reset_control *rstc[]; 63 + struct reset_control *rstc[] __counted_by(num_rstcs); 64 64 }; 65 65 66 66 static const char *rcdev_name(struct reset_controller_dev *rcdev) ··· 1185 1185 resets = kzalloc(struct_size(resets, rstc, num), GFP_KERNEL); 1186 1186 if (!resets) 1187 1187 return ERR_PTR(-ENOMEM); 1188 + resets->num_rstcs = num; 1188 1189 1189 1190 for (i = 0; i < num; i++) { 1190 1191 rstc = __of_reset_control_get(np, NULL, i, shared, optional, ··· 1194 1193 goto err_rst; 1195 1194 resets->rstc[i] = rstc; 1196 1195 } 1197 - resets->num_rstcs = num; 1198 1196 resets->base.array = true; 1199 1197 1200 1198 return &resets->base;