Skip to contentSkip to conversation

fs: Set file_handle::handle_bytes before referencing file_handle::f_handle

PATCH Superseded Kees Cook 5 participants 5 messages Apr 3, 2024 – Apr 5, 2024

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 fs: Set file_handle::handle_bytes before referencing file_handle::f_handle

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()")

---
Cc: Christian Brauner <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Alexander Viro <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Chuck Lever <[email protected]>
Cc: Jeff Layton <[email protected]>
Cc: Amir Goldstein <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 fs/fhandle.c | 1 +
 1 file changed, 1 insertion(+)
fs/fhandle.c +1 -0
index 53ed54711cd2..08ec2340dd22 100644
@@ -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;
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 3 lines · Kees Cook

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);

Quoted 3 lines
Fixes 1b43c4629756 ("fs: Annotate struct file_handle with __counted_by() and use struct_size()")

Thanks for catching this!
--
Gustavo

Quoted 27 lines

---
Cc: Christian Brauner <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Alexander Viro <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Chuck Lever <[email protected]>
Cc: Jeff Layton <[email protected]>
Cc: Amir Goldstein <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
  fs/fhandle.c | 1 +
  1 file changed, 1 insertion(+)
fs/fhandle.c +1 -0
index 53ed54711cd2..08ec2340dd22 100644
@@ -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;
reviewer Reviewed-by on v1 1/1
#2 lore raw
In reply to Kees Cook
Quoted 6 lines · Kees Cook

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()")

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:

Honza

Quoted 30 lines

---
Cc: Christian Brauner <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Alexander Viro <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Chuck Lever <[email protected]>
Cc: Jeff Layton <[email protected]>
Cc: Amir Goldstein <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 fs/fhandle.c | 1 +
 1 file changed, 1 insertion(+)
fs/fhandle.c +1 -0
index 53ed54711cd2..08ec2340dd22 100644
@@ -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;
Signature · 1 line
2.34.1
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
Acked-by
#3 lore raw
Quoted 15 lines · Jan Kara
Quoted 6 lines · Kees Cook

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()")

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:

Quoted 3 lines

Honza

I agree, an in-code comment is needed.

Quoted 33 lines
Quoted 30 lines

---
Cc: Christian Brauner <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Alexander Viro <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Chuck Lever <[email protected]>
Cc: Jeff Layton <[email protected]>
Cc: Amir Goldstein <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 fs/fhandle.c | 1 +
 1 file changed, 1 insertion(+)
fs/fhandle.c +1 -0
index 53ed54711cd2..08ec2340dd22 100644
@@ -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;
Signature · 1 line
2.34.1
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
Signature · 1 line
Chuck Lever
#4 lore raw
In reply to Jan Kara
Quoted 11 lines · Jan Kara
Quoted 6 lines · Kees Cook

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()")

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.