All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH][next] fs: Annotate struct file_handle with __counted_by() and use struct_size()
@ 2024-03-26  1:34 Gustavo A. R. Silva
  2024-03-26  8:57 ` Jan Kara
  2024-03-26 10:48 ` Christian Brauner
  0 siblings, 2 replies; 3+ messages in thread
From: Gustavo A. R. Silva @ 2024-03-26  1:34 UTC (permalink / raw)
  To: Alexander Viro, Christian Brauner, Jan Kara, Chuck Lever,
	Jeff Layton, Amir Goldstein
  Cc: linux-fsdevel, linux-nfs, linux-kernel, Gustavo A. R. Silva,
	linux-hardening

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 via CONFIG_UBSAN_BOUNDS (for
array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family
functions).

While there, use struct_size() helper, instead of the open-coded
version.

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 fs/fhandle.c       | 8 ++++----
 include/linux/fs.h | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/fs/fhandle.c b/fs/fhandle.c
index 57a12614addf..53ed54711cd2 100644
--- a/fs/fhandle.c
+++ b/fs/fhandle.c
@@ -36,7 +36,7 @@ static long do_sys_name_to_handle(const struct path *path,
 	if (f_handle.handle_bytes > MAX_HANDLE_SZ)
 		return -EINVAL;
 
-	handle = kzalloc(sizeof(struct file_handle) + f_handle.handle_bytes,
+	handle = kzalloc(struct_size(handle, f_handle, f_handle.handle_bytes),
 			 GFP_KERNEL);
 	if (!handle)
 		return -ENOMEM;
@@ -71,7 +71,7 @@ static long do_sys_name_to_handle(const struct path *path,
 	/* copy the mount id */
 	if (put_user(real_mount(path->mnt)->mnt_id, mnt_id) ||
 	    copy_to_user(ufh, handle,
-			 sizeof(struct file_handle) + handle_bytes))
+			 struct_size(handle, f_handle, handle_bytes)))
 		retval = -EFAULT;
 	kfree(handle);
 	return retval;
@@ -192,7 +192,7 @@ static int handle_to_path(int mountdirfd, struct file_handle __user *ufh,
 		retval = -EINVAL;
 		goto out_err;
 	}
-	handle = kmalloc(sizeof(struct file_handle) + f_handle.handle_bytes,
+	handle = kmalloc(struct_size(handle, f_handle, f_handle.handle_bytes),
 			 GFP_KERNEL);
 	if (!handle) {
 		retval = -ENOMEM;
@@ -202,7 +202,7 @@ static int handle_to_path(int mountdirfd, struct file_handle __user *ufh,
 	*handle = f_handle;
 	if (copy_from_user(&handle->f_handle,
 			   &ufh->f_handle,
-			   f_handle.handle_bytes)) {
+			   struct_size(ufh, f_handle, f_handle.handle_bytes))) {
 		retval = -EFAULT;
 		goto out_handle;
 	}
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 034f0c918eea..1540e28d10d7 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -1033,7 +1033,7 @@ struct file_handle {
 	__u32 handle_bytes;
 	int handle_type;
 	/* file identifier */
-	unsigned char f_handle[];
+	unsigned char f_handle[] __counted_by(handle_bytes);
 };
 
 static inline struct file *get_file(struct file *f)
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH][next] fs: Annotate struct file_handle with __counted_by() and use struct_size()
  2024-03-26  1:34 [PATCH][next] fs: Annotate struct file_handle with __counted_by() and use struct_size() Gustavo A. R. Silva
@ 2024-03-26  8:57 ` Jan Kara
  2024-03-26 10:48 ` Christian Brauner
  1 sibling, 0 replies; 3+ messages in thread
From: Jan Kara @ 2024-03-26  8:57 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Alexander Viro, Christian Brauner, Jan Kara, Chuck Lever,
	Jeff Layton, Amir Goldstein, linux-fsdevel, linux-nfs,
	linux-kernel, linux-hardening

On Mon 25-03-24 19:34:01, Gustavo A. R. Silva wrote:
> 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 via CONFIG_UBSAN_BOUNDS (for
> array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family
> functions).
> 
> While there, use struct_size() helper, instead of the open-coded
> version.
> 
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>

Looks good. Feel free to add:

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

								Honza

> ---
>  fs/fhandle.c       | 8 ++++----
>  include/linux/fs.h | 2 +-
>  2 files changed, 5 insertions(+), 5 deletions(-)
> 
> diff --git a/fs/fhandle.c b/fs/fhandle.c
> index 57a12614addf..53ed54711cd2 100644
> --- a/fs/fhandle.c
> +++ b/fs/fhandle.c
> @@ -36,7 +36,7 @@ static long do_sys_name_to_handle(const struct path *path,
>  	if (f_handle.handle_bytes > MAX_HANDLE_SZ)
>  		return -EINVAL;
>  
> -	handle = kzalloc(sizeof(struct file_handle) + f_handle.handle_bytes,
> +	handle = kzalloc(struct_size(handle, f_handle, f_handle.handle_bytes),
>  			 GFP_KERNEL);
>  	if (!handle)
>  		return -ENOMEM;
> @@ -71,7 +71,7 @@ static long do_sys_name_to_handle(const struct path *path,
>  	/* copy the mount id */
>  	if (put_user(real_mount(path->mnt)->mnt_id, mnt_id) ||
>  	    copy_to_user(ufh, handle,
> -			 sizeof(struct file_handle) + handle_bytes))
> +			 struct_size(handle, f_handle, handle_bytes)))
>  		retval = -EFAULT;
>  	kfree(handle);
>  	return retval;
> @@ -192,7 +192,7 @@ static int handle_to_path(int mountdirfd, struct file_handle __user *ufh,
>  		retval = -EINVAL;
>  		goto out_err;
>  	}
> -	handle = kmalloc(sizeof(struct file_handle) + f_handle.handle_bytes,
> +	handle = kmalloc(struct_size(handle, f_handle, f_handle.handle_bytes),
>  			 GFP_KERNEL);
>  	if (!handle) {
>  		retval = -ENOMEM;
> @@ -202,7 +202,7 @@ static int handle_to_path(int mountdirfd, struct file_handle __user *ufh,
>  	*handle = f_handle;
>  	if (copy_from_user(&handle->f_handle,
>  			   &ufh->f_handle,
> -			   f_handle.handle_bytes)) {
> +			   struct_size(ufh, f_handle, f_handle.handle_bytes))) {
>  		retval = -EFAULT;
>  		goto out_handle;
>  	}
> diff --git a/include/linux/fs.h b/include/linux/fs.h
> index 034f0c918eea..1540e28d10d7 100644
> --- a/include/linux/fs.h
> +++ b/include/linux/fs.h
> @@ -1033,7 +1033,7 @@ struct file_handle {
>  	__u32 handle_bytes;
>  	int handle_type;
>  	/* file identifier */
> -	unsigned char f_handle[];
> +	unsigned char f_handle[] __counted_by(handle_bytes);
>  };
>  
>  static inline struct file *get_file(struct file *f)
> -- 
> 2.34.1
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH][next] fs: Annotate struct file_handle with __counted_by() and use struct_size()
  2024-03-26  1:34 [PATCH][next] fs: Annotate struct file_handle with __counted_by() and use struct_size() Gustavo A. R. Silva
  2024-03-26  8:57 ` Jan Kara
@ 2024-03-26 10:48 ` Christian Brauner
  1 sibling, 0 replies; 3+ messages in thread
From: Christian Brauner @ 2024-03-26 10:48 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Christian Brauner, linux-fsdevel, linux-nfs, linux-kernel,
	linux-hardening, Alexander Viro, Jan Kara, Chuck Lever,
	Jeff Layton, Amir Goldstein

On Mon, 25 Mar 2024 19:34:01 -0600, Gustavo A. R. Silva wrote:
> 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 via CONFIG_UBSAN_BOUNDS (for
> array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family
> functions).
> 
> While there, use struct_size() helper, instead of the open-coded
> version.
> 
> [...]

Applied to the vfs.misc branch of the vfs/vfs.git tree.
Patches in the vfs.misc branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a
new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the
patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase,
trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs.misc

[1/1] fs: Annotate struct file_handle with __counted_by() and use struct_size()
      https://git.kernel.org/vfs/vfs/c/1b43c4629756

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-03-26 10:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-26  1:34 [PATCH][next] fs: Annotate struct file_handle with __counted_by() and use struct_size() Gustavo A. R. Silva
2024-03-26  8:57 ` Jan Kara
2024-03-26 10:48 ` Christian Brauner

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.