diff mbox series

ext4: Annotate struct fname with __counted_by()

Message ID 20240913110013.151331-2-thorsten.blum@toblux.com (mailing list archive)
State Superseded
Headers show
Series ext4: Annotate struct fname with __counted_by() | expand

Commit Message

Thorsten Blum Sept. 13, 2024, 11 a.m. UTC
Add the __counted_by compiler attribute to the flexible array member
name to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and
CONFIG_FORTIFY_SOURCE.

Inline and use struct_size() to calculate the number of bytes to
allocate for new_fn and remove the local variable len.

Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
---
 fs/ext4/dir.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

Comments

Jan Kara Sept. 17, 2024, 12:25 p.m. UTC | #1
On Fri 13-09-24 13:00:14, Thorsten Blum wrote:
> Add the __counted_by compiler attribute to the flexible array member
> name to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and
> CONFIG_FORTIFY_SOURCE.
> 
> Inline and use struct_size() to calculate the number of bytes to
> allocate for new_fn and remove the local variable len.
> 
> Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>

Looks good. Feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

								Honza

> ---
>  fs/ext4/dir.c | 7 +++----
>  1 file changed, 3 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/ext4/dir.c b/fs/ext4/dir.c
> index ff4514e4626b..8e7df15bb971 100644
> --- a/fs/ext4/dir.c
> +++ b/fs/ext4/dir.c
> @@ -408,7 +408,7 @@ struct fname {
>  	__u32		inode;
>  	__u8		name_len;
>  	__u8		file_type;
> -	char		name[];
> +	char		name[] __counted_by(name_len);
>  };
>  
>  /*
> @@ -464,14 +464,13 @@ int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
>  	struct rb_node **p, *parent = NULL;
>  	struct fname *fname, *new_fn;
>  	struct dir_private_info *info;
> -	int len;
>  
>  	info = dir_file->private_data;
>  	p = &info->root.rb_node;
>  
>  	/* Create and allocate the fname structure */
> -	len = sizeof(struct fname) + ent_name->len + 1;
> -	new_fn = kzalloc(len, GFP_KERNEL);
> +	new_fn = kzalloc(struct_size(new_fn, name, ent_name->len + 1),
> +			 GFP_KERNEL);
>  	if (!new_fn)
>  		return -ENOMEM;
>  	new_fn->hash = hash;
> -- 
> 2.46.0
> 
>
diff mbox series

Patch

diff --git a/fs/ext4/dir.c b/fs/ext4/dir.c
index ff4514e4626b..8e7df15bb971 100644
--- a/fs/ext4/dir.c
+++ b/fs/ext4/dir.c
@@ -408,7 +408,7 @@  struct fname {
 	__u32		inode;
 	__u8		name_len;
 	__u8		file_type;
-	char		name[];
+	char		name[] __counted_by(name_len);
 };
 
 /*
@@ -464,14 +464,13 @@  int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
 	struct rb_node **p, *parent = NULL;
 	struct fname *fname, *new_fn;
 	struct dir_private_info *info;
-	int len;
 
 	info = dir_file->private_data;
 	p = &info->root.rb_node;
 
 	/* Create and allocate the fname structure */
-	len = sizeof(struct fname) + ent_name->len + 1;
-	new_fn = kzalloc(len, GFP_KERNEL);
+	new_fn = kzalloc(struct_size(new_fn, name, ent_name->len + 1),
+			 GFP_KERNEL);
 	if (!new_fn)
 		return -ENOMEM;
 	new_fn->hash = hash;