Skip to contentSkip to conversation

fs/stat: reject invalid flags for empty paths

PATCH Applied Avi Weiss 3 participants 3 messages Nov 8 – Nov 18 VFS

lore
Share & tools
lore

Avi Weiss

author
#0 lore raw

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

vfs_fstatat() and statx() take the file descriptor fast path for empty paths when AT_EMPTY_PATH is set. These paths bypass the flag validation in vfs_statx(), allowing unsupported flags such as AT_RECURSIVE to succeed.

Return -EINVAL for unsupported flags in the vfs_fstatat() fast path, and validate flags in vfs_statx_fd() as is already done in vfs_statx().

Fixes e896474fe485 ("getname_maybe_null() - the third variant of pathname copy-in")

---
v2:
- return -EINVAL directly for unsupported flags in vfs_fstatat()
- validate flags in vfs_statx_fd() instead of changing statx() dispatch (as suggested by Jan Kara)

 fs/stat.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
fs/stat.c +10 -1
index a2e63c6c45b7..3013e4ece011 100644
@@ -322,6 +322,11 @@ static int vfs_statx_fd(int fd, int flags, struct kstat *stat,
 			  u32 request_mask)
 {
 	CLASS(fd_raw, f)(fd);
+
+	if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
+		      AT_EMPTY_PATH | AT_STATX_SYNC_TYPE))
+		return -EINVAL;
+
 	if (fd_empty(f))
 		return -EBADF;
 	return vfs_statx_path(&fd_file(f)->f_path, flags, stat, request_mask);
@@ -371,8 +376,12 @@ int vfs_fstatat(int dfd, const char __user *filename,
 {
 	CLASS(filename_maybe_null, name)(filename, flags);
 
-	if (!name && dfd >= 0)
+	if (!name && dfd >= 0) {
+		if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
+			      AT_EMPTY_PATH | AT_STATX_SYNC_TYPE))
+			return -EINVAL;
 		return vfs_fstat(dfd, stat);
+	}
 
 	return vfs_statx(dfd, name, flags | AT_NO_AUTOMOUNT,
 			 stat, STATX_BASIC_STATS);

base-commit: fa4ad798f6f681031775266044ba1b0cf3c670bd

Signature · 1 line
2.43.0
reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 10 lines · Avi Weiss

vfs_fstatat() and statx() take the file descriptor fast path for empty paths when AT_EMPTY_PATH is set. These paths bypass the flag validation in vfs_statx(), allowing unsupported flags such as AT_RECURSIVE to succeed.

Return -EINVAL for unsupported flags in the vfs_fstatat() fast path, and validate flags in vfs_statx_fd() as is already done in vfs_statx().

Fixes e896474fe485 ("getname_maybe_null() - the third variant of pathname copy-in")

Looks good. Feel free to add:

Honza

Quoted 44 lines

---
v2:
- return -EINVAL directly for unsupported flags in vfs_fstatat()
- validate flags in vfs_statx_fd() instead of changing statx() dispatch (as suggested by Jan Kara)

 fs/stat.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
fs/stat.c +10 -1
index a2e63c6c45b7..3013e4ece011 100644
@@ -322,6 +322,11 @@ static int vfs_statx_fd(int fd, int flags, struct kstat *stat,
 			  u32 request_mask)
 {
 	CLASS(fd_raw, f)(fd);
+
+	if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
+		      AT_EMPTY_PATH | AT_STATX_SYNC_TYPE))
+		return -EINVAL;
+
 	if (fd_empty(f))
 		return -EBADF;
 	return vfs_statx_path(&fd_file(f)->f_path, flags, stat, request_mask);
@@ -371,8 +376,12 @@ int vfs_fstatat(int dfd, const char __user *filename,
 {
 	CLASS(filename_maybe_null, name)(filename, flags);
 
-	if (!name && dfd >= 0)
+	if (!name && dfd >= 0) {
+		if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
+			      AT_EMPTY_PATH | AT_STATX_SYNC_TYPE))
+			return -EINVAL;
 		return vfs_fstat(dfd, stat);
+	}
 
 	return vfs_statx(dfd, name, flags | AT_NO_AUTOMOUNT,
 			 stat, STATX_BASIC_STATS);

base-commit: fa4ad798f6f681031775266044ba1b0cf3c670bd

Signature · 1 line
2.43.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer Applied
#2 lore raw
In reply to Avi Weiss
Quoted 9 lines · Avi Weiss

vfs_fstatat() and statx() take the file descriptor fast path for empty paths when AT_EMPTY_PATH is set. These paths bypass the flag validation in vfs_statx(), allowing unsupported flags such as AT_RECURSIVE to succeed.

Return -EINVAL for unsupported flags in the vfs_fstatat() fast path, and validate flags in vfs_statx_fd() as is already done in vfs_statx().

[...]

Applied to the vfs.fixes branch of the vfs/vfs.git tree. Patches in the vfs.fixes branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs.fixes

[1/1] fs/stat: reject invalid flags for empty paths

      https://git.kernel.org/vfs/vfs/c/9a74699d0073