Skip to content

Commit 8c6e03f

Browse files
tobluxbrauner
authored andcommitted
acl: Annotate struct posix_acl with __counted_by()
Add the __counted_by compiler attribute to the flexible array member a_entries to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE. Use struct_size() to calculate the number of bytes to allocate for new and cloned acls and remove the local size variables. Change the posix_acl_alloc() function parameter count from int to unsigned int to match posix_acl's a_count data type. Add identifier names to the function definition to silence two checkpatch warnings. Reviewed-by: Jan Kara <[email protected]> Signed-off-by: Thorsten Blum <[email protected]> Link: https://lore.kernel.org/r/[email protected] Cc: Nathan Chancellor <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent 99bdadb commit 8c6e03f

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

fs/posix_acl.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,11 @@ EXPORT_SYMBOL(posix_acl_init);
200200
* Allocate a new ACL with the specified number of entries.
201201
*/
202202
struct posix_acl *
203-
posix_acl_alloc(int count, gfp_t flags)
203+
posix_acl_alloc(unsigned int count, gfp_t flags)
204204
{
205-
const size_t size = sizeof(struct posix_acl) +
206-
count * sizeof(struct posix_acl_entry);
207-
struct posix_acl *acl = kmalloc(size, flags);
205+
struct posix_acl *acl;
206+
207+
acl = kmalloc(struct_size(acl, a_entries, count), flags);
208208
if (acl)
209209
posix_acl_init(acl, count);
210210
return acl;
@@ -220,9 +220,8 @@ posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
220220
struct posix_acl *clone = NULL;
221221

222222
if (acl) {
223-
int size = sizeof(struct posix_acl) + acl->a_count *
224-
sizeof(struct posix_acl_entry);
225-
clone = kmemdup(acl, size, flags);
223+
clone = kmemdup(acl, struct_size(acl, a_entries, acl->a_count),
224+
flags);
226225
if (clone)
227226
refcount_set(&clone->a_refcount, 1);
228227
}

include/linux/posix_acl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct posix_acl {
3030
refcount_t a_refcount;
3131
unsigned int a_count;
3232
struct rcu_head a_rcu;
33-
struct posix_acl_entry a_entries[];
33+
struct posix_acl_entry a_entries[] __counted_by(a_count);
3434
};
3535

3636
#define FOREACH_ACL_ENTRY(pa, acl, pe) \
@@ -62,7 +62,7 @@ posix_acl_release(struct posix_acl *acl)
6262
/* posix_acl.c */
6363

6464
extern void posix_acl_init(struct posix_acl *, int);
65-
extern struct posix_acl *posix_acl_alloc(int, gfp_t);
65+
extern struct posix_acl *posix_acl_alloc(unsigned int count, gfp_t flags);
6666
extern struct posix_acl *posix_acl_from_mode(umode_t, gfp_t);
6767
extern int posix_acl_equiv_mode(const struct posix_acl *, umode_t *);
6868
extern int __posix_acl_create(struct posix_acl **, gfp_t, umode_t *);

0 commit comments

Comments
 (0)