linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fs: Set file_handle::handle_bytes before referencing file_handle::f_handle
@ 2024-04-03 21:54 Kees Cook
  2024-04-03 22:11 ` Gustavo A. R. Silva
  2024-04-04  9:19 ` Jan Kara
  0 siblings, 2 replies; 5+ messages in thread
From: Kees Cook @ 2024-04-03 21:54 UTC (permalink / raw)
  To: Christian Brauner
  Cc: Kees Cook, Gustavo A. R. Silva, Alexander Viro, Jan Kara,
	Chuck Lever, Jeff Layton, Amir Goldstein, linux-fsdevel,
	linux-nfs, linux-hardening, linux-kernel

With adding __counted_by(handle_bytes) to struct file_handle, we need
to explicitly set it in the one place it wasn't yet happening prior to
accessing the flex array "f_handle".

Fixes: 1b43c4629756 ("fs: Annotate struct file_handle with __counted_by() and use struct_size()")
Signed-off-by: Kees Cook <keescook@chromium.org>
---
Cc: Christian Brauner <brauner@kernel.org>
Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Jan Kara <jack@suse.cz>
Cc: Chuck Lever <chuck.lever@oracle.com>
Cc: Jeff Layton <jlayton@kernel.org>
Cc: Amir Goldstein <amir73il@gmail.com>
Cc: linux-fsdevel@vger.kernel.org
Cc: linux-nfs@vger.kernel.org
Cc: linux-hardening@vger.kernel.org
---
 fs/fhandle.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/fs/fhandle.c b/fs/fhandle.c
index 53ed54711cd2..08ec2340dd22 100644
--- a/fs/fhandle.c
+++ b/fs/fhandle.c
@@ -40,6 +40,7 @@ static long do_sys_name_to_handle(const struct path *path,
 			 GFP_KERNEL);
 	if (!handle)
 		return -ENOMEM;
+	handle->handle_bytes = f_handle.handle_bytes;
 
 	/* convert handle size to multiple of sizeof(u32) */
 	handle_dwords = f_handle.handle_bytes >> 2;
-- 
2.34.1


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

* Re: [PATCH] fs: Set file_handle::handle_bytes before referencing file_handle::f_handle
  2024-04-03 21:54 [PATCH] fs: Set file_handle::handle_bytes before referencing file_handle::f_handle Kees Cook
@ 2024-04-03 22:11 ` Gustavo A. R. Silva
  2024-04-04  9:19 ` Jan Kara
  1 sibling, 0 replies; 5+ messages in thread
From: Gustavo A. R. Silva @ 2024-04-03 22:11 UTC (permalink / raw)
  To: Kees Cook, Christian Brauner
  Cc: Gustavo A. R. Silva, Alexander Viro, Jan Kara, Chuck Lever,
	Jeff Layton, Amir Goldstein, linux-fsdevel, linux-nfs,
	linux-hardening, linux-kernel



On 03/04/24 15:54, Kees Cook wrote:
> With adding __counted_by(handle_bytes) to struct file_handle, we need
> to explicitly set it in the one place it wasn't yet happening prior to
> accessing the flex array "f_handle".

Yes, which (access to `f_handle`) happens here:

  48         retval = exportfs_encode_fh(path->dentry,
  49                                     (struct fid *)handle->f_handle,
  50                                     &handle_dwords, fh_flags);

> 
> Fixes: 1b43c4629756 ("fs: Annotate struct file_handle with __counted_by() and use struct_size()")
> Signed-off-by: Kees Cook <keescook@chromium.org>

Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>

Thanks for catching this!
--
Gustavo

> ---
> Cc: Christian Brauner <brauner@kernel.org>
> Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org>
> Cc: Alexander Viro <viro@zeniv.linux.org.uk>
> Cc: Jan Kara <jack@suse.cz>
> Cc: Chuck Lever <chuck.lever@oracle.com>
> Cc: Jeff Layton <jlayton@kernel.org>
> Cc: Amir Goldstein <amir73il@gmail.com>
> Cc: linux-fsdevel@vger.kernel.org
> Cc: linux-nfs@vger.kernel.org
> Cc: linux-hardening@vger.kernel.org
> ---
>   fs/fhandle.c | 1 +
>   1 file changed, 1 insertion(+)
> 
> diff --git a/fs/fhandle.c b/fs/fhandle.c
> index 53ed54711cd2..08ec2340dd22 100644
> --- a/fs/fhandle.c
> +++ b/fs/fhandle.c
> @@ -40,6 +40,7 @@ static long do_sys_name_to_handle(const struct path *path,
>   			 GFP_KERNEL);
>   	if (!handle)
>   		return -ENOMEM;
> +	handle->handle_bytes = f_handle.handle_bytes;
>   
>   	/* convert handle size to multiple of sizeof(u32) */
>   	handle_dwords = f_handle.handle_bytes >> 2;

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

* Re: [PATCH] fs: Set file_handle::handle_bytes before referencing file_handle::f_handle
  2024-04-03 21:54 [PATCH] fs: Set file_handle::handle_bytes before referencing file_handle::f_handle Kees Cook
  2024-04-03 22:11 ` Gustavo A. R. Silva
@ 2024-04-04  9:19 ` Jan Kara
  2024-04-04 15:25   ` Chuck Lever
  2024-04-05 11:00   ` Christian Brauner
  1 sibling, 2 replies; 5+ messages in thread
From: Jan Kara @ 2024-04-04  9:19 UTC (permalink / raw)
  To: Kees Cook
  Cc: Christian Brauner, Gustavo A. R. Silva, Alexander Viro, Jan Kara,
	Chuck Lever, Jeff Layton, Amir Goldstein, linux-fsdevel,
	linux-nfs, linux-hardening, linux-kernel

On Wed 03-04-24 14:54:03, Kees Cook wrote:
> With adding __counted_by(handle_bytes) to struct file_handle, we need
> to explicitly set it in the one place it wasn't yet happening prior to
> accessing the flex array "f_handle".
> 
> Fixes: 1b43c4629756 ("fs: Annotate struct file_handle with __counted_by() and use struct_size()")
> Signed-off-by: Kees Cook <keescook@chromium.org>

OK, so this isn't really a functional bug AFAIU but the compiler will
wrongly complain we are accessing handle->f_handle beyond claimed array
size (because handle->handle_bytes == 0 at that point). Am I right? If
that's the case, please add a short comment explaining this (because it
looks odd we set handle->handle_bytes and then reset it a few lines later).
With the comment feel free to add:

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

								Honza

> ---
> Cc: Christian Brauner <brauner@kernel.org>
> Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org>
> Cc: Alexander Viro <viro@zeniv.linux.org.uk>
> Cc: Jan Kara <jack@suse.cz>
> Cc: Chuck Lever <chuck.lever@oracle.com>
> Cc: Jeff Layton <jlayton@kernel.org>
> Cc: Amir Goldstein <amir73il@gmail.com>
> Cc: linux-fsdevel@vger.kernel.org
> Cc: linux-nfs@vger.kernel.org
> Cc: linux-hardening@vger.kernel.org
> ---
>  fs/fhandle.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/fs/fhandle.c b/fs/fhandle.c
> index 53ed54711cd2..08ec2340dd22 100644
> --- a/fs/fhandle.c
> +++ b/fs/fhandle.c
> @@ -40,6 +40,7 @@ static long do_sys_name_to_handle(const struct path *path,
>  			 GFP_KERNEL);
>  	if (!handle)
>  		return -ENOMEM;
> +	handle->handle_bytes = f_handle.handle_bytes;
>  
>  	/* convert handle size to multiple of sizeof(u32) */
>  	handle_dwords = f_handle.handle_bytes >> 2;
> -- 
> 2.34.1
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

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

* Re: [PATCH] fs: Set file_handle::handle_bytes before referencing file_handle::f_handle
  2024-04-04  9:19 ` Jan Kara
@ 2024-04-04 15:25   ` Chuck Lever
  2024-04-05 11:00   ` Christian Brauner
  1 sibling, 0 replies; 5+ messages in thread
From: Chuck Lever @ 2024-04-04 15:25 UTC (permalink / raw)
  To: Jan Kara
  Cc: Kees Cook, Christian Brauner, Gustavo A. R. Silva,
	Alexander Viro, Jeff Layton, Amir Goldstein, linux-fsdevel,
	linux-nfs, linux-hardening, linux-kernel

On Thu, Apr 04, 2024 at 11:19:00AM +0200, Jan Kara wrote:
> On Wed 03-04-24 14:54:03, Kees Cook wrote:
> > With adding __counted_by(handle_bytes) to struct file_handle, we need
> > to explicitly set it in the one place it wasn't yet happening prior to
> > accessing the flex array "f_handle".
> > 
> > Fixes: 1b43c4629756 ("fs: Annotate struct file_handle with __counted_by() and use struct_size()")
> > Signed-off-by: Kees Cook <keescook@chromium.org>
> 
> OK, so this isn't really a functional bug AFAIU but the compiler will
> wrongly complain we are accessing handle->f_handle beyond claimed array
> size (because handle->handle_bytes == 0 at that point). Am I right? If
> that's the case, please add a short comment explaining this (because it
> looks odd we set handle->handle_bytes and then reset it a few lines later).
> With the comment feel free to add:
> 
> Reviewed-by: Jan Kara <jack@suse.cz>
> 
> 								Honza

I agree, an in-code comment is needed.

Acked-by: Chuck Lever <chuck.lever@oracle.com>


> > ---
> > Cc: Christian Brauner <brauner@kernel.org>
> > Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org>
> > Cc: Alexander Viro <viro@zeniv.linux.org.uk>
> > Cc: Jan Kara <jack@suse.cz>
> > Cc: Chuck Lever <chuck.lever@oracle.com>
> > Cc: Jeff Layton <jlayton@kernel.org>
> > Cc: Amir Goldstein <amir73il@gmail.com>
> > Cc: linux-fsdevel@vger.kernel.org
> > Cc: linux-nfs@vger.kernel.org
> > Cc: linux-hardening@vger.kernel.org
> > ---
> >  fs/fhandle.c | 1 +
> >  1 file changed, 1 insertion(+)
> > 
> > diff --git a/fs/fhandle.c b/fs/fhandle.c
> > index 53ed54711cd2..08ec2340dd22 100644
> > --- a/fs/fhandle.c
> > +++ b/fs/fhandle.c
> > @@ -40,6 +40,7 @@ static long do_sys_name_to_handle(const struct path *path,
> >  			 GFP_KERNEL);
> >  	if (!handle)
> >  		return -ENOMEM;
> > +	handle->handle_bytes = f_handle.handle_bytes;
> >  
> >  	/* convert handle size to multiple of sizeof(u32) */
> >  	handle_dwords = f_handle.handle_bytes >> 2;
> > -- 
> > 2.34.1
> > 
> -- 
> Jan Kara <jack@suse.com>
> SUSE Labs, CR

-- 
Chuck Lever

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

* Re: [PATCH] fs: Set file_handle::handle_bytes before referencing file_handle::f_handle
  2024-04-04  9:19 ` Jan Kara
  2024-04-04 15:25   ` Chuck Lever
@ 2024-04-05 11:00   ` Christian Brauner
  1 sibling, 0 replies; 5+ messages in thread
From: Christian Brauner @ 2024-04-05 11:00 UTC (permalink / raw)
  To: Jan Kara
  Cc: Kees Cook, Gustavo A. R. Silva, Alexander Viro, Chuck Lever,
	Jeff Layton, Amir Goldstein, linux-fsdevel, linux-nfs,
	linux-hardening, linux-kernel

On Thu, Apr 04, 2024 at 11:19:00AM +0200, Jan Kara wrote:
> On Wed 03-04-24 14:54:03, Kees Cook wrote:
> > With adding __counted_by(handle_bytes) to struct file_handle, we need
> > to explicitly set it in the one place it wasn't yet happening prior to
> > accessing the flex array "f_handle".
> > 
> > Fixes: 1b43c4629756 ("fs: Annotate struct file_handle with __counted_by() and use struct_size()")
> > Signed-off-by: Kees Cook <keescook@chromium.org>
> 
> OK, so this isn't really a functional bug AFAIU but the compiler will
> wrongly complain we are accessing handle->f_handle beyond claimed array
> size (because handle->handle_bytes == 0 at that point). Am I right? If

And really, this also needs to please be mentioned in the commit message
because from reading the commit message I'm not even sure what this
patch is trying to fix.

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

end of thread, other threads:[~2024-04-05 11:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-03 21:54 [PATCH] fs: Set file_handle::handle_bytes before referencing file_handle::f_handle Kees Cook
2024-04-03 22:11 ` Gustavo A. R. Silva
2024-04-04  9:19 ` Jan Kara
2024-04-04 15:25   ` Chuck Lever
2024-04-05 11:00   ` Christian Brauner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).