Skip to content

Commit 3690f4a

Browse files
tobluxpetrpavlu
authored andcommitted
params: Annotate struct module_param_attrs with __counted_by()
Add the __counted_by compiler attribute to the flexible array member attrs to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE. Increment num before adding a new param_attribute to the attrs array and adjust the array index accordingly. Increment num immediately after the first reallocation such that the reallocation for the NULL terminator only needs to add 1 (instead of 2) to mk->mp->num. Use struct_size() instead of manually calculating the size for the reallocation. Use krealloc_array() for the additional NULL terminator. Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Cc: Luis Chamberlain <mcgrof@kernel.org> Cc: Nathan Chancellor <nathan@kernel.org> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev> Reviewed-by: Luis Chamberlain <mcgrof@kernel.org> Link: https://lore.kernel.org/r/20250213221352.2625-3-thorsten.blum@linux.dev Signed-off-by: Petr Pavlu <petr.pavlu@suse.com>
1 parent f47d2a3 commit 3690f4a

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

kernel/params.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ struct module_param_attrs
551551
{
552552
unsigned int num;
553553
struct attribute_group grp;
554-
struct param_attribute attrs[];
554+
struct param_attribute attrs[] __counted_by(num);
555555
};
556556

557557
#ifdef CONFIG_SYSFS
@@ -651,35 +651,32 @@ static __modinit int add_sysfs_param(struct module_kobject *mk,
651651
}
652652

653653
/* Enlarge allocations. */
654-
new_mp = krealloc(mk->mp,
655-
sizeof(*mk->mp) +
656-
sizeof(mk->mp->attrs[0]) * (mk->mp->num + 1),
654+
new_mp = krealloc(mk->mp, struct_size(mk->mp, attrs, mk->mp->num + 1),
657655
GFP_KERNEL);
658656
if (!new_mp)
659657
return -ENOMEM;
660658
mk->mp = new_mp;
659+
mk->mp->num++;
661660

662661
/* Extra pointer for NULL terminator */
663-
new_attrs = krealloc(mk->mp->grp.attrs,
664-
sizeof(mk->mp->grp.attrs[0]) * (mk->mp->num + 2),
665-
GFP_KERNEL);
662+
new_attrs = krealloc_array(mk->mp->grp.attrs, mk->mp->num + 1,
663+
sizeof(mk->mp->grp.attrs[0]), GFP_KERNEL);
666664
if (!new_attrs)
667665
return -ENOMEM;
668666
mk->mp->grp.attrs = new_attrs;
669667

670668
/* Tack new one on the end. */
671-
memset(&mk->mp->attrs[mk->mp->num], 0, sizeof(mk->mp->attrs[0]));
672-
sysfs_attr_init(&mk->mp->attrs[mk->mp->num].mattr.attr);
673-
mk->mp->attrs[mk->mp->num].param = kp;
674-
mk->mp->attrs[mk->mp->num].mattr.show = param_attr_show;
669+
memset(&mk->mp->attrs[mk->mp->num - 1], 0, sizeof(mk->mp->attrs[0]));
670+
sysfs_attr_init(&mk->mp->attrs[mk->mp->num - 1].mattr.attr);
671+
mk->mp->attrs[mk->mp->num - 1].param = kp;
672+
mk->mp->attrs[mk->mp->num - 1].mattr.show = param_attr_show;
675673
/* Do not allow runtime DAC changes to make param writable. */
676674
if ((kp->perm & (S_IWUSR | S_IWGRP | S_IWOTH)) != 0)
677-
mk->mp->attrs[mk->mp->num].mattr.store = param_attr_store;
675+
mk->mp->attrs[mk->mp->num - 1].mattr.store = param_attr_store;
678676
else
679-
mk->mp->attrs[mk->mp->num].mattr.store = NULL;
680-
mk->mp->attrs[mk->mp->num].mattr.attr.name = (char *)name;
681-
mk->mp->attrs[mk->mp->num].mattr.attr.mode = kp->perm;
682-
mk->mp->num++;
677+
mk->mp->attrs[mk->mp->num - 1].mattr.store = NULL;
678+
mk->mp->attrs[mk->mp->num - 1].mattr.attr.name = (char *)name;
679+
mk->mp->attrs[mk->mp->num - 1].mattr.attr.mode = kp->perm;
683680

684681
/* Fix up all the pointers, since krealloc can move us */
685682
for (i = 0; i < mk->mp->num; i++)

0 commit comments

Comments
 (0)