Message ID | 20240730205509.323320-3-thorsten.blum@toblux.com (mailing list archive) |
---|---|
State | Superseded |
Headers | show |
Series | [v3] ext4: Annotate struct ext4_xattr_inode_array with __counted_by() | expand |
On 30/07/24 14:55, Thorsten Blum wrote: > Add the __counted_by compiler attribute to the flexible array member > inodes to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and > CONFIG_FORTIFY_SOURCE. > > Remove the now obsolete comment on the count field. > > Refactor ext4_expand_inode_array() by assigning count before copying any > data using memcpy(). > > Use struct_size() instead of offsetof() and remove the local variable > count. > > Increment count before adding a new inode to the inodes array. > > Compile-tested only. > > Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com> > --- > Changes in v2: > - Adjust ext4_expand_inode_array() as suggested by Gustavo A. R. Silva > - Use struct_size() and struct_size_t() instead of offsetof() > - Link to v1: https://lore.kernel.org/linux-kernel/20240729110454.346918-3-thorsten.blum@toblux.com/ > > Changes in v3: > - Use struct_size() instead of struct_size_t() as suggested by Kees Cook > - Remove the local variable count as suggested by Gustavo A. R. Silva > - Increment count before adding a new inode as suggested by Gustavo > - Link to v2: https://lore.kernel.org/linux-kernel/20240730172301.231867-4-thorsten.blum@toblux.com/ > --- > fs/ext4/xattr.c | 24 +++++++++++------------- > fs/ext4/xattr.h | 4 ++-- > 2 files changed, 13 insertions(+), 15 deletions(-) > > diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c > index 46ce2f21fef9..4e20ef7cc502 100644 > --- a/fs/ext4/xattr.c > +++ b/fs/ext4/xattr.c > @@ -2879,33 +2879,31 @@ ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array, > if (*ea_inode_array == NULL) { > /* > * Start with 15 inodes, so it fits into a power-of-two size. > - * If *ea_inode_array is NULL, this is essentially offsetof() > */ > - (*ea_inode_array) = > - kmalloc(offsetof(struct ext4_xattr_inode_array, > - inodes[EIA_MASK]), > - GFP_NOFS); > + (*ea_inode_array) = kmalloc( > + struct_size(*ea_inode_array, inodes, EIA_MASK), > + GFP_NOFS); > if (*ea_inode_array == NULL) > return -ENOMEM; > (*ea_inode_array)->count = 0; > } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) { > /* expand the array once all 15 + n * 16 slots are full */ > struct ext4_xattr_inode_array *new_array = NULL; > - int count = (*ea_inode_array)->count; > > - /* if new_array is NULL, this is essentially offsetof() */ > new_array = kmalloc( > - offsetof(struct ext4_xattr_inode_array, > - inodes[count + EIA_INCR]), > - GFP_NOFS); > + struct_size(*ea_inode_array, inodes, > + (*ea_inode_array)->count + EIA_INCR), > + GFP_NOFS); > if (new_array == NULL) > return -ENOMEM; > - memcpy(new_array, *ea_inode_array, > - offsetof(struct ext4_xattr_inode_array, inodes[count])); > + new_array->count = (*ea_inode_array)->count; The line above is not really necessary if you copy `*ea_inode_array` at once like memcpy(... , struct_size(new_array, inodes, (*ea_inode_array)->count)); or like memcpy(... , struct_size(*ea_inode_array, inodes, (*ea_inode_array)->count)); whichever works. > + memcpy(new_array, *ea_inode_array, > + struct_size(new_array, inodes, new_array->count)); > kfree(*ea_inode_array); > *ea_inode_array = new_array; > } > - (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode; > + (*ea_inode_array)->count++; > + (*ea_inode_array)->inodes[(*ea_inode_array)->count - 1] = inode; This looks good, thanks! > return 0; > } > > diff --git a/fs/ext4/xattr.h b/fs/ext4/xattr.h > index bd97c4aa8177..e14fb19dc912 100644 > --- a/fs/ext4/xattr.h > +++ b/fs/ext4/xattr.h > @@ -130,8 +130,8 @@ struct ext4_xattr_ibody_find { > }; > > struct ext4_xattr_inode_array { > - unsigned int count; /* # of used items in the array */ > - struct inode *inodes[]; > + unsigned int count; > + struct inode *inodes[] __counted_by(count); > }; > > extern const struct xattr_handler ext4_xattr_user_handler; -- Gustavo
diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c index 46ce2f21fef9..4e20ef7cc502 100644 --- a/fs/ext4/xattr.c +++ b/fs/ext4/xattr.c @@ -2879,33 +2879,31 @@ ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array, if (*ea_inode_array == NULL) { /* * Start with 15 inodes, so it fits into a power-of-two size. - * If *ea_inode_array is NULL, this is essentially offsetof() */ - (*ea_inode_array) = - kmalloc(offsetof(struct ext4_xattr_inode_array, - inodes[EIA_MASK]), - GFP_NOFS); + (*ea_inode_array) = kmalloc( + struct_size(*ea_inode_array, inodes, EIA_MASK), + GFP_NOFS); if (*ea_inode_array == NULL) return -ENOMEM; (*ea_inode_array)->count = 0; } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) { /* expand the array once all 15 + n * 16 slots are full */ struct ext4_xattr_inode_array *new_array = NULL; - int count = (*ea_inode_array)->count; - /* if new_array is NULL, this is essentially offsetof() */ new_array = kmalloc( - offsetof(struct ext4_xattr_inode_array, - inodes[count + EIA_INCR]), - GFP_NOFS); + struct_size(*ea_inode_array, inodes, + (*ea_inode_array)->count + EIA_INCR), + GFP_NOFS); if (new_array == NULL) return -ENOMEM; - memcpy(new_array, *ea_inode_array, - offsetof(struct ext4_xattr_inode_array, inodes[count])); + new_array->count = (*ea_inode_array)->count; + memcpy(new_array, *ea_inode_array, + struct_size(new_array, inodes, new_array->count)); kfree(*ea_inode_array); *ea_inode_array = new_array; } - (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode; + (*ea_inode_array)->count++; + (*ea_inode_array)->inodes[(*ea_inode_array)->count - 1] = inode; return 0; } diff --git a/fs/ext4/xattr.h b/fs/ext4/xattr.h index bd97c4aa8177..e14fb19dc912 100644 --- a/fs/ext4/xattr.h +++ b/fs/ext4/xattr.h @@ -130,8 +130,8 @@ struct ext4_xattr_ibody_find { }; struct ext4_xattr_inode_array { - unsigned int count; /* # of used items in the array */ - struct inode *inodes[]; + unsigned int count; + struct inode *inodes[] __counted_by(count); }; extern const struct xattr_handler ext4_xattr_user_handler;
Add the __counted_by compiler attribute to the flexible array member inodes to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE. Remove the now obsolete comment on the count field. Refactor ext4_expand_inode_array() by assigning count before copying any data using memcpy(). Use struct_size() instead of offsetof() and remove the local variable count. Increment count before adding a new inode to the inodes array. Compile-tested only. Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com> --- Changes in v2: - Adjust ext4_expand_inode_array() as suggested by Gustavo A. R. Silva - Use struct_size() and struct_size_t() instead of offsetof() - Link to v1: https://lore.kernel.org/linux-kernel/20240729110454.346918-3-thorsten.blum@toblux.com/ Changes in v3: - Use struct_size() instead of struct_size_t() as suggested by Kees Cook - Remove the local variable count as suggested by Gustavo A. R. Silva - Increment count before adding a new inode as suggested by Gustavo - Link to v2: https://lore.kernel.org/linux-kernel/20240730172301.231867-4-thorsten.blum@toblux.com/ --- fs/ext4/xattr.c | 24 +++++++++++------------- fs/ext4/xattr.h | 4 ++-- 2 files changed, 13 insertions(+), 15 deletions(-)