Skip to contentSkip to conversation

fs/stat: reject invalid flags for empty paths

PATCH Superseded Avi Weiss 2 participants 2 messages Oct 31 – Nov 5 VFS

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Avi Weiss

author
#0 lore raw

PATCH 1/1 fs/stat: reject invalid flags for empty paths

vfs_fstatat() and statx() take the file descriptor fast path when AT_EMPTY_PATH is set, even if unsupported flag bits are present. This bypasses vfs_statx() flag validation and lets both syscalls succeed with AT_RECURSIVE.

Restrict the fast paths to supported flags so invalid combinations fall through to vfs_statx() and return -EINVAL.

Fixes e896474fe485 ("getname_maybe_null() - the third variant of pathname copy-in")
---
 fs/stat.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
fs/stat.c +6 -2
index a2e63c6c45b7..b75a411b502f 100644
@@ -371,7 +371,9 @@ int vfs_fstatat(int dfd, const char __user *filename,
 {
 	CLASS(filename_maybe_null, name)(filename, flags);
 
-	if (!name && dfd >= 0)
+	if (!name && dfd >= 0 &&
+	    !(flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
+			AT_EMPTY_PATH | AT_STATX_SYNC_TYPE)))
 		return vfs_fstat(dfd, stat);
 
 	return vfs_statx(dfd, name, flags | AT_NO_AUTOMOUNT,
@@ -812,7 +814,9 @@ SYSCALL_DEFINE5(statx,
 {
 	CLASS(filename_maybe_null, name)(filename, flags);
 
-	if (!name && dfd >= 0)
+	if (!name && dfd >= 0 &&
+	    !(flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
+			AT_EMPTY_PATH | AT_STATX_SYNC_TYPE)))
 		return do_statx_fd(dfd, flags & ~AT_NO_AUTOMOUNT, mask, buffer);
 
 	return do_statx(dfd, name, flags, mask, buffer);

base-commit: 36aad21d50d1af3f6954bb0c988257a908202477

Signature · 1 line
2.43.0
reviewer
#1 lore raw
Quoted 19 lines · Avi Weiss

vfs_fstatat() and statx() take the file descriptor fast path when AT_EMPTY_PATH is set, even if unsupported flag bits are present. This bypasses vfs_statx() flag validation and lets both syscalls succeed with AT_RECURSIVE.

Restrict the fast paths to supported flags so invalid combinations fall through to vfs_statx() and return -EINVAL.

Fixes e896474fe485 ("getname_maybe_null() - the third variant of pathname copy-in")
---
 fs/stat.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
fs/stat.c +0 -0
index a2e63c6c45b7..b75a411b502f 100644

fs/stat.c int vfs_fstatat(int dfd, const char __user *filename, · line 377

Quoted 8 lines
fs/stat.c +3 -1
@@ -371,7 +371,9 @@ int vfs_fstatat(int dfd, const char __user *filename,
 {
 	CLASS(filename_maybe_null, name)(filename, flags);
 
-	if (!name && dfd >= 0)
+	if (!name && dfd >= 0 &&
+	    !(flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
+			AT_EMPTY_PATH | AT_STATX_SYNC_TYPE)))
 		return vfs_fstat(dfd, stat);

Shouldn't we just return EINVAL in the case of unsupported flag instead of falling back to vfs_statx() to fail?

fs/stat.c SYSCALL_DEFINE5(statx, · line 820

Quoted 10 lines
fs/stat.c +3 -1
 	return vfs_statx(dfd, name, flags | AT_NO_AUTOMOUNT,
@@ -812,7 +814,9 @@ SYSCALL_DEFINE5(statx,
 {
 	CLASS(filename_maybe_null, name)(filename, flags);
 
-	if (!name && dfd >= 0)
+	if (!name && dfd >= 0 &&
+	    !(flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
+			AT_EMPTY_PATH | AT_STATX_SYNC_TYPE)))
 		return do_statx_fd(dfd, flags & ~AT_NO_AUTOMOUNT, mask, buffer);

For symmetry this belongs to vfs_statx_fd() similarly as in the other path the checks are done in vfs_statx().

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR