nsfs: handle inode number mismatches gracefully in file handles
PATCH v2 1/1 nsfs: handle inode number mismatches gracefully in file handles
Replace VFS_WARN_ON_ONCE() with graceful error handling when file handles contain inode numbers that don't match the actual namespace inode. This prevents userspace from triggering kernel warnings by providing malformed file handles to open_by_handle_at().
The issue occurs when userspace provides a file handle with valid namespace type and ID that successfully locates a namespace, but specifies an incorrect inode number. Previously, this would trigger VFS_WARN_ON_ONCE() when comparing the real inode number against the provided value.
Since file handle data is user-controllable, inode number mismatches should be treated as invalid input rather than kernel consistency errors. Handle this case by returning NULL to indicate the file handle is invalid, rather than warning about what is essentially user input validation.
--- fs/nsfs.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
Changes in v2:
- Handle all inode number mismatches, not just zero, as suggested by Jan Kara
- Replace warning with graceful error handling for better architecture
fs/nsfs.c +3 -2
@@ -490,8 +490,9 @@ static struct dentry *nsfs_fh_to_dentry(struct super_block *sb, struct fid *fh,VFS_WARN_ON_ONCE (ns->ns_id != fid->ns_id);VFS_WARN_ON_ONCE (ns->ops->type != fid->ns_type); -VFS_WARN_ON_ONCE (ns->inum != fid->ns_inum); - +/* Someone is playing games and passing invalid file handles? */ +if (ns->inum != fid->ns_inum) +return NULL ;if (!refcount_inc_not_zero (&ns->count))return NULL ; }
Signature · 1 line
2.43.0
Christian Brauner
maintainerQuoted 48 lines · Deepanshu Kartikey
Replace
VFS_WARN_ON_ONCE()with graceful error handling when file handles contain inode numbers that don't match the actual namespace inode. This prevents userspace from triggering kernel warnings by providing malformed file handles toopen_by_handle_at().The issue occurs when userspace provides a file handle with valid namespace type and ID that successfully locates a namespace, but specifies an incorrect inode number. Previously, this would trigger
VFS_WARN_ON_ONCE()when comparing the real inode number against the provided value.Since file handle data is user-controllable, inode number mismatches should be treated as invalid input rather than kernel consistency errors. Handle this case by returning NULL to indicate the file handle is invalid, rather than warning about what is essentially user input validation.
Reported-by [email protected]Suggested-by Jan Kara <[email protected]>Reviewed-by Jan Kara <[email protected]>Signed-off-by Deepanshu Kartikey <[email protected]>--- fs/nsfs.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)Changes in v2:
- Handle all inode number mismatches, not just zero, as suggested by Jan Kara
- Replace warning with graceful error handling for better architecturefs/nsfs.c +3
-2@@ -490,8 +490,9 @@ static struct dentry *nsfs_fh_to_dentry(struct super_block *sb, struct fid *fh,VFS_WARN_ON_ONCE (ns->ns_id != fid->ns_id);VFS_WARN_ON_ONCE (ns->ops->type != fid->ns_type); -VFS_WARN_ON_ONCE (ns->inum != fid->ns_inum); - +/* Someone is playing games and passing invalid file handles? */ +if (ns->inum != fid->ns_inum) +return NULL ;if (!refcount_inc_not_zero (&ns->count))return NULL ; }Signature · 1 line
2.43.0
That seems sane although I have considered to relax the decoding part in the future. IOW, the kernel must always return the file handle with all fields filled in. But userspace may be allowed to specify just the ->ns_id and leave both ->inum and ->ns_type zero. This is based on a patch for next cycled "unified nstree". Anyway, thanks for the fix.