fs: Set file_handle::handle_bytes before referencing file_handle::f_handle
Subsystems (4)
PATCH v2 1/1 fs: Set file_handle::handle_bytes before referencing file_handle::f_handle
Since __counted_by(handle_bytes) was added 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". For robustness also check for a negative value for handle_bytes, which is possible for an "int", but nothing appears to set.
---
Cc: Jan Kara <[email protected]>
Cc: Chuck Lever <[email protected]>
Cc: Gustavo A. R. Silva <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Alexander Viro <[email protected]>
Cc: Jeff Layton <[email protected]>
Cc: Amir Goldstein <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
v2: more bounds checking, add comments, dropped reviews since logic changed
v1: https://lore.kernel.org/all/[email protected]/
--- fs/fhandle.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
fs/fhandle.c +9 -2
@@ -40,6 +40,11 @@ static long do_sys_name_to_handle(const struct path *path, GFP_KERNEL);if (!handle)return -ENOMEM ; +/* +* Since handle->f_handle is about to be written, make sure the +* associated __counted_by(handle_bytes) variable is correct. +*/ + handle->handle_bytes = f_handle.handle_bytes;/* convert handle size to multiple of sizeof(u32) */ handle_dwords = f_handle.handle_bytes >>2 ;
@@ -51,8 +56,8 @@ static long do_sys_name_to_handle(const struct path *path, handle->handle_type = retval;/* convert handle size to bytes */ handle_bytes = handle_dwords *sizeof (u32); - handle->handle_bytes = handle_bytes; -if ((handle->handle_bytes > f_handle.handle_bytes) || +/* check if handle_bytes would have exceeded the allocation */ +if ((handle_bytes <0 ) || (handle_bytes > f_handle.handle_bytes) || (retval ==FILEID_INVALID ) || (retval <0 )) {/* As per old exportfs_encode_fh documentation * we could return ENOSPC to indicate overflow
@@ -68,6 +73,8 @@ static long do_sys_name_to_handle(const struct path *path, handle_bytes =0 ; } else retval =0 ; +/* the "valid" number of bytes may fewer than originally allocated */ + handle->handle_bytes = handle_bytes;/* copy the mount id */ if (put_user (real_mount (path->mnt)->mnt_id, mnt_id) || copy_to_user(ufh, handle,
Signature · 1 line
2.34.1
Quoted 5 lines · Kees Cook
Since
__counted_by(handle_bytes)was added to structfile_handle, we need to explicitly set it in the one place it wasn't yet happening prior to accessing the flex array "f_handle". For robustness also check for a negative value forhandle_bytes, which is possible for an "int", but nothing appears to set.
Why not change handle_bytes from an int to a u32?
Also, what a grotty function.
handle_dwords = f_handle.handle_bytes >> 2;
...
handle_bytes = handle_dwords * sizeof(u32);Quoted 59 lines
Fixes 1b43c4629756 ("fs: Annotate struct file_handle with __counted_by() and use struct_size()")Signed-off-by Kees Cook <[email protected]>---
Cc: Jan Kara <[email protected]>
Cc: Chuck Lever <[email protected]>
Cc: Gustavo A. R. Silva <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Alexander Viro <[email protected]>
Cc: Jeff Layton <[email protected]>
Cc: Amir Goldstein <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
v2: more bounds checking, add comments, dropped reviews since logic changed
v1: https://lore.kernel.org/all/[email protected]/--- fs/fhandle.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)fs/fhandle.c +9
-2@@ -40,6 +40,11 @@ static long do_sys_name_to_handle(const struct path *path, GFP_KERNEL);if (!handle)return -ENOMEM ; +/* +* Since handle->f_handle is about to be written, make sure the +* associated __counted_by(handle_bytes) variable is correct. +*/ + handle->handle_bytes = f_handle.handle_bytes;/* convert handle size to multiple of sizeof(u32) */ handle_dwords = f_handle.handle_bytes >>2 ;@@ -51,8 +56,8 @@ static long do_sys_name_to_handle(const struct path *path, handle->handle_type = retval;/* convert handle size to bytes */ handle_bytes = handle_dwords *sizeof (u32); - handle->handle_bytes = handle_bytes; -if ((handle->handle_bytes > f_handle.handle_bytes) || +/* check if handle_bytes would have exceeded the allocation */ +if ((handle_bytes <0 ) || (handle_bytes > f_handle.handle_bytes) || (retval ==FILEID_INVALID ) || (retval <0 )) {/* As per old exportfs_encode_fh documentation * we could return ENOSPC to indicate overflow @@ -68,6 +73,8 @@ static long do_sys_name_to_handle(const struct path *path, handle_bytes =0 ; } else retval =0 ; +/* the "valid" number of bytes may fewer than originally allocated */ + handle->handle_bytes = handle_bytes;/* copy the mount id */ if (put_user (real_mount (path->mnt)->mnt_id, mnt_id) || copy_to_user(ufh, handle,Signature · 1 line
2.34.1
Jan Kara
reviewerQuoted 42 lines · Kees Cook
Since
__counted_by(handle_bytes)was added to structfile_handle, we need to explicitly set it in the one place it wasn't yet happening prior to accessing the flex array "f_handle". For robustness also check for a negative value forhandle_bytes, which is possible for an "int", but nothing appears to set.Fixes 1b43c4629756 ("fs: Annotate struct file_handle with __counted_by() and use struct_size()")Signed-off-by Kees Cook <[email protected]>---
Cc: Jan Kara <[email protected]>
Cc: Chuck Lever <[email protected]>
Cc: Gustavo A. R. Silva <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Alexander Viro <[email protected]>
Cc: Jeff Layton <[email protected]>
Cc: Amir Goldstein <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
v2: more bounds checking, add comments, dropped reviews since logic changed
v1: https://lore.kernel.org/all/[email protected]/--- fs/fhandle.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)fs/fhandle.c +5
-0@@ -40,6 +40,11 @@ static long do_sys_name_to_handle(const struct path *path, GFP_KERNEL);if (!handle)return -ENOMEM ; +/* +* Since handle->f_handle is about to be written, make sure the +* associated __counted_by(handle_bytes) variable is correct. +*/ + handle->handle_bytes = f_handle.handle_bytes;/* convert handle size to multiple of sizeof(u32) */ handle_dwords = f_handle.handle_bytes >>2 ;
fs/fhandle.c static long do_sys_name_to_handle(const struct path *path, · line 60
Quoted 7 lines
fs/fhandle.c +2
-2@@ -51,8 +56,8 @@ static long do_sys_name_to_handle(const struct path *path, handle->handle_type = retval;/* convert handle size to bytes */ handle_bytes = handle_dwords *sizeof (u32); - handle->handle_bytes = handle_bytes; - if ((handle->handle_bytes > f_handle.handle_bytes) || +/* check if handle_bytes would have exceeded the allocation */ + if ((handle_bytes < 0) || (handle_bytes > f_handle.handle_bytes) ||
This is broken. Let me explain: Userspace passes in struct file_handle (ufh) and says how many bytes it has reserved for the variable length contents in ufh->handle_bytes. We call exportfs_encode_fh() to create the file handle. If it fits into the provided space, the function returns in handle_dwords how many uints it has actually stored. If the handle didn't fit, handle_dword contains number of uints we'd need in the variable length part to be able to fit the handle in.
Now your patch destroys this behavior by storing 0 to handle_bytes in case the handle didn't fit *before* the returned value is actually stored to a struct copied to userspace.
Also the handle_bytes < 0 check is IMHO pointless and confusing. If some filesystem is returning bogus size of the file handle, we have a big problem anyway so I don't think checking like this brings much. If you want to add some paranoia what would make some sense is: Make handle_bytes uint and do:
if (WARN_ON_ONCE(handle_bytes > MAX_HANDLE_SZ)) {
handle_bytes = 0;
retval = -EINVAL;
}But as a separate patch please, because it is unrelated to this __counted_by fixup.
Honza
Quoted 15 lines
fs/fhandle.c +2
-0(retval == FILEID_INVALID) || (retval < 0)) {/* As per old exportfs_encode_fh documentation * we could return ENOSPC to indicate overflow @@ -68,6 +73,8 @@ static long do_sys_name_to_handle(const struct path *path, handle_bytes =0 ; } else retval =0 ; +/* the "valid" number of bytes may fewer than originally allocated */ + handle->handle_bytes = handle_bytes;/* copy the mount id */ if (put_user (real_mount (path->mnt)->mnt_id, mnt_id) || copy_to_user(ufh, handle,Signature · 1 line
2.34.1
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 59 lines · Jan Kara
Quoted 42 lines · Kees Cook
Since
__counted_by(handle_bytes)was added to structfile_handle, we need to explicitly set it in the one place it wasn't yet happening prior to accessing the flex array "f_handle". For robustness also check for a negative value forhandle_bytes, which is possible for an "int", but nothing appears to set.Fixes 1b43c4629756 ("fs: Annotate struct file_handle with __counted_by() and use struct_size()")Signed-off-by Kees Cook <[email protected]>---
Cc: Jan Kara <[email protected]>
Cc: Chuck Lever <[email protected]>
Cc: Gustavo A. R. Silva <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Alexander Viro <[email protected]>
Cc: Jeff Layton <[email protected]>
Cc: Amir Goldstein <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
v2: more bounds checking, add comments, dropped reviews since logic changed
v1: https://lore.kernel.org/all/[email protected]/--- fs/fhandle.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)fs/fhandle.c +5
-0@@ -40,6 +40,11 @@ static long do_sys_name_to_handle(const struct path *path, GFP_KERNEL);if (!handle)return -ENOMEM ; +/* +* Since handle->f_handle is about to be written, make sure the +* associated __counted_by(handle_bytes) variable is correct. +*/ + handle->handle_bytes = f_handle.handle_bytes;/* convert handle size to multiple of sizeof(u32) */ handle_dwords = f_handle.handle_bytes >>2 ;fs/fhandle.c static long do_sys_name_to_handle(const struct path *path, · line 60
Quoted 7 lines
fs/fhandle.c +2
-2@@ -51,8 +56,8 @@ static long do_sys_name_to_handle(const struct path *path, handle->handle_type = retval;/* convert handle size to bytes */ handle_bytes = handle_dwords *sizeof (u32); - handle->handle_bytes = handle_bytes; - if ((handle->handle_bytes > f_handle.handle_bytes) || +/* check if handle_bytes would have exceeded the allocation */ + if ((handle_bytes < 0) || (handle_bytes > f_handle.handle_bytes) ||This is broken. Let me explain: Userspace passes in struct
file_handle(ufh) and says how many bytes it has reserved for the variable length contents inufh->handle_bytes. We callexportfs_encode_fh()to create the file handle. If it fits into the provided space, the function returns inhandle_dwordshow many uints it has actually stored. If the handle didn't fit,handle_dwordcontains number of uints we'd need in the variable length part to be able to fit the handle in.
Quoted 3 lines
Now your patch destroys this behavior by storing 0 to
handle_bytesin case the handle didn't fit *before* the returned value is actually stored to a struct copied to userspace.
Ah yes, I see now how they're used for separate things. I will respin again and possibly fix up the "int" to "u32" as suggested by willy.
Signature · 1 line
Kees Cook