Skip to content

Commit 68d6f4f

Browse files
GustavoARSilvabrauner
authored andcommitted
fs: Annotate struct file_handle with __counted_by() and use struct_size()
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. [brauner@kernel.org: contains a fix by Edward for an OOB access] Reported-by: syzbot+4139435cb1b34cf759c2@syzkaller.appspotmail.com Signed-off-by: Edward Adam Davis <eadavis@qq.com> Link: https://lore.kernel.org/r/tencent_A7845DD769577306D813742365E976E3A205@qq.com Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> Link: https://lore.kernel.org/r/ZgImCXTdGDTeBvSS@neat Reviewed-by: Jan Kara <jack@suse.cz> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 61db088 commit 68d6f4f

2 files changed

Lines changed: 4 additions & 4 deletions

File tree

fs/fhandle.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static long do_sys_name_to_handle(const struct path *path,
3636
if (f_handle.handle_bytes > MAX_HANDLE_SZ)
3737
return -EINVAL;
3838

39-
handle = kzalloc(sizeof(struct file_handle) + f_handle.handle_bytes,
39+
handle = kzalloc(struct_size(handle, f_handle, f_handle.handle_bytes),
4040
GFP_KERNEL);
4141
if (!handle)
4242
return -ENOMEM;
@@ -71,7 +71,7 @@ static long do_sys_name_to_handle(const struct path *path,
7171
/* copy the mount id */
7272
if (put_user(real_mount(path->mnt)->mnt_id, mnt_id) ||
7373
copy_to_user(ufh, handle,
74-
sizeof(struct file_handle) + handle_bytes))
74+
struct_size(handle, f_handle, handle_bytes)))
7575
retval = -EFAULT;
7676
kfree(handle);
7777
return retval;
@@ -192,7 +192,7 @@ static int handle_to_path(int mountdirfd, struct file_handle __user *ufh,
192192
retval = -EINVAL;
193193
goto out_err;
194194
}
195-
handle = kmalloc(sizeof(struct file_handle) + f_handle.handle_bytes,
195+
handle = kmalloc(struct_size(handle, f_handle, f_handle.handle_bytes),
196196
GFP_KERNEL);
197197
if (!handle) {
198198
retval = -ENOMEM;

include/linux/fs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ struct file_handle {
10331033
__u32 handle_bytes;
10341034
int handle_type;
10351035
/* file identifier */
1036-
unsigned char f_handle[];
1036+
unsigned char f_handle[] __counted_by(handle_bytes);
10371037
};
10381038

10391039
static inline struct file *get_file(struct file *f)

0 commit comments

Comments
 (0)