Skip to contentSkip to conversation

xattrat: accept empty O_PATH file descriptors

PATCH In review Andreas Gruenbacher 2 participants 6 messages Jul 22 – Nov 24 VFS

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xattrat: accept empty O_PATH file descriptors

Christian,

here's an updated patch for making the *xattrat() system calls accept O_PATH file descriptors.

Compared to the previous version (see the discussion at [*]), this version adds a fd_maybe_raw cleanup class that uses either fdget() or fdget_raw() depending on which kinds of file descriptors are acceptable. That's still a but ugly, but all the alternatives I could come up with are much worse.

[*] https://lore.kernel.org/linux-fsdevel/[email protected]/

Thanks,
Andreas

--

Currently, the setxattrat(), getxattrat(), listxattrat(), and removexattrat() system calls fail with -EBADF when dfd is an O_PATH file descriptor, pathname is an empty string or NULL, and the AT_EMPTY_PATH flag is set in at_flags. This is inconsistent with the behavior of other system calls like fstatat() and fchmodat() which do accept O_PATH file descriptors, so change the *xattrat() system calls to accept O_PATH file descriptors as well.

In environments where /proc is available, operations on O_PATH file descriptors can also be carried out by using "/proc/self/fd/<dfd>" as the pathname, so from a security standpoint, this is a relatively insignificant change.

We stick with the existing practice of leaving the behavior of the fsetxattr(), fgetxattr(), flistxattr(), and fremovexattr() system calls unchanged: those will still reject O_PATH file descriptors.

---
 fs/xattr.c           | 55 +++++++++++++++++++++++++-------------------
 include/linux/file.h |  2 ++
 2 files changed, 33 insertions(+), 24 deletions(-)
fs/xattr.c +31 -24
index d58979115200..1db6596a2ec1 100644
@@ -700,7 +700,8 @@ int filename_setxattr(int dfd, struct filename *filename,
 
 static int path_setxattrat(int dfd, const char __user *pathname,
 			   unsigned int at_flags, const char __user *name,
-			   const void __user *value, size_t size, int flags)
+			   const void __user *value, size_t size, int flags,
+			   bool raw)
 {
 	struct xattr_name kname;
 	struct kernel_xattr_ctx ctx = {
@@ -725,7 +726,7 @@ static int path_setxattrat(int dfd, const char __user *pathname,
 
 	CLASS(filename_maybe_null, filename)(pathname, at_flags);
 	if (!filename && dfd >= 0) {
-		CLASS(fd, f)(dfd);
+		CLASS(fd_maybe_raw, f)(dfd, raw);
 		if (fd_empty(f))
 			error = -EBADF;
 		else
@@ -758,14 +759,15 @@ SYSCALL_DEFINE6(setxattrat, int, dfd, const char __user *, pathname, unsigned in
 
 	return path_setxattrat(dfd, pathname, at_flags, name,
 			       u64_to_user_ptr(args.value), args.size,
-			       args.flags);
+			       args.flags, true);
 }
 
 SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
 		const char __user *, name, const void __user *, value,
 		size_t, size, int, flags)
 {
-	return path_setxattrat(AT_FDCWD, pathname, 0, name, value, size, flags);
+	return path_setxattrat(AT_FDCWD, pathname, 0, name, value, size, flags,
+			       false);
 }
 
 SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
@@ -773,14 +775,14 @@ SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
 		size_t, size, int, flags)
 {
 	return path_setxattrat(AT_FDCWD, pathname, AT_SYMLINK_NOFOLLOW, name,
-			       value, size, flags);
+			       value, size, flags, false);
 }
 
 SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
 		const void __user *,value, size_t, size, int, flags)
 {
 	return path_setxattrat(fd, NULL, AT_EMPTY_PATH, name,
-			       value, size, flags);
+			       value, size, flags, false);
 }
 
 /*
@@ -845,7 +847,7 @@ ssize_t filename_getxattr(int dfd, struct filename *filename,
 
 static ssize_t path_getxattrat(int dfd, const char __user *pathname,
 			       unsigned int at_flags, const char __user *name,
-			       void __user *value, size_t size)
+			       void __user *value, size_t size, bool raw)
 {
 	struct xattr_name kname;
 	struct kernel_xattr_ctx ctx = {
@@ -865,7 +867,7 @@ static ssize_t path_getxattrat(int dfd, const char __user *pathname,
 
 	CLASS(filename_maybe_null, filename)(pathname, at_flags);
 	if (!filename && dfd >= 0) {
-		CLASS(fd, f)(dfd);
+		CLASS(fd_maybe_raw, f)(dfd, raw);
 		if (fd_empty(f))
 			return -EBADF;
 		return file_getxattr(fd_file(f), &ctx);
@@ -899,26 +901,28 @@ SYSCALL_DEFINE6(getxattrat, int, dfd, const char __user *, pathname, unsigned in
 		return -EINVAL;
 
 	return path_getxattrat(dfd, pathname, at_flags, name,
-			       u64_to_user_ptr(args.value), args.size);
+			       u64_to_user_ptr(args.value), args.size, true);
 }
 
 SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
 		const char __user *, name, void __user *, value, size_t, size)
 {
-	return path_getxattrat(AT_FDCWD, pathname, 0, name, value, size);
+	return path_getxattrat(AT_FDCWD, pathname, 0, name, value, size,
+			       false);
 }
 
 SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
 		const char __user *, name, void __user *, value, size_t, size)
 {
 	return path_getxattrat(AT_FDCWD, pathname, AT_SYMLINK_NOFOLLOW, name,
-			       value, size);
+			       value, size, false);
 }
 
 SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
 		void __user *, value, size_t, size)
 {
-	return path_getxattrat(fd, NULL, AT_EMPTY_PATH, name, value, size);
+	return path_getxattrat(fd, NULL, AT_EMPTY_PATH, name, value, size,
+			       false);
 }
 
 /*
@@ -982,7 +986,7 @@ ssize_t filename_listxattr(int dfd, struct filename *filename,
 
 static ssize_t path_listxattrat(int dfd, const char __user *pathname,
 				unsigned int at_flags, char __user *list,
-				size_t size)
+				size_t size, bool raw)
 {
 	int lookup_flags;
 
@@ -991,7 +995,7 @@ static ssize_t path_listxattrat(int dfd, const char __user *pathname,
 
 	CLASS(filename_maybe_null, filename)(pathname, at_flags);
 	if (!filename) {
-		CLASS(fd, f)(dfd);
+		CLASS(fd_maybe_raw, f)(dfd, raw);
 		if (fd_empty(f))
 			return -EBADF;
 		return file_listxattr(fd_file(f), list, size);
@@ -1005,24 +1009,25 @@ SYSCALL_DEFINE5(listxattrat, int, dfd, const char __user *, pathname,
 		unsigned int, at_flags,
 		char __user *, list, size_t, size)
 {
-	return path_listxattrat(dfd, pathname, at_flags, list, size);
+	return path_listxattrat(dfd, pathname, at_flags, list, size, true);
 }
 
 SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
 		size_t, size)
 {
-	return path_listxattrat(AT_FDCWD, pathname, 0, list, size);
+	return path_listxattrat(AT_FDCWD, pathname, 0, list, size, false);
 }
 
 SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
 		size_t, size)
 {
-	return path_listxattrat(AT_FDCWD, pathname, AT_SYMLINK_NOFOLLOW, list, size);
+	return path_listxattrat(AT_FDCWD, pathname, AT_SYMLINK_NOFOLLOW, list,
+				size, false);
 }
 
 SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
 {
-	return path_listxattrat(fd, NULL, AT_EMPTY_PATH, list, size);
+	return path_listxattrat(fd, NULL, AT_EMPTY_PATH, list, size, false);
 }
 
 /*
@@ -1073,7 +1078,8 @@ static int filename_removexattr(int dfd, struct filename *filename,
 }
 
 static int path_removexattrat(int dfd, const char __user *pathname,
-			      unsigned int at_flags, const char __user *name)
+			      unsigned int at_flags, const char __user *name,
+			      bool raw)
 {
 	struct xattr_name kname;
 	unsigned int lookup_flags;
@@ -1088,7 +1094,7 @@ static int path_removexattrat(int dfd, const char __user *pathname,
 
 	CLASS(filename_maybe_null, filename)(pathname, at_flags);
 	if (!filename) {
-		CLASS(fd, f)(dfd);
+		CLASS(fd_maybe_raw, f)(dfd, raw);
 		if (fd_empty(f))
 			return -EBADF;
 		return file_removexattr(fd_file(f), &kname);
@@ -1100,24 +1106,25 @@ static int path_removexattrat(int dfd, const char __user *pathname,
 SYSCALL_DEFINE4(removexattrat, int, dfd, const char __user *, pathname,
 		unsigned int, at_flags, const char __user *, name)
 {
-	return path_removexattrat(dfd, pathname, at_flags, name);
+	return path_removexattrat(dfd, pathname, at_flags, name, true);
 }
 
 SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
 		const char __user *, name)
 {
-	return path_removexattrat(AT_FDCWD, pathname, 0, name);
+	return path_removexattrat(AT_FDCWD, pathname, 0, name, false);
 }
 
 SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
 		const char __user *, name)
 {
-	return path_removexattrat(AT_FDCWD, pathname, AT_SYMLINK_NOFOLLOW, name);
+	return path_removexattrat(AT_FDCWD, pathname, AT_SYMLINK_NOFOLLOW, name,
+				  false);
 }
 
 SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
 {
-	return path_removexattrat(fd, NULL, AT_EMPTY_PATH, name);
+	return path_removexattrat(fd, NULL, AT_EMPTY_PATH, name, false);
 }
 
 int xattr_list_one(char **buffer, ssize_t *remaining_size, const char *name)
include/linux/file.h +2 -0
index 27484b444d31..2a1dc738981e 100644
@@ -82,6 +82,8 @@ static inline void fdput_pos(struct fd f)
 
 DEFINE_CLASS(fd, struct fd, fdput(_T), fdget(fd), int fd)
 DEFINE_CLASS(fd_raw, struct fd, fdput(_T), fdget_raw(fd), int fd)
+DEFINE_CLASS(fd_maybe_raw, struct fd, fdput(_T),
+	     raw ? fdget_raw(fd) : fdget(fd), int fd, bool raw)
 DEFINE_CLASS(fd_pos, struct fd, fdput_pos(_T), fdget_pos(fd), int fd)
 
 extern int f_dupfd(unsigned int from, struct file *file, unsigned flags);
Signature · 1 line
2.55.0
#1 lore raw
Quoted 5 lines · Andreas Gruenbacher

Christian,

here's an updated patch for making the *xattrat() system calls accept O_PATH file descriptors.

Quoted 7 lines

Compared to the previous version (see the discussion at [*]), this version adds a fd_maybe_raw cleanup class that uses either fdget() or fdget_raw() depending on which kinds of file descriptors are acceptable. That's still a but ugly, but all the alternatives I could come up with are much worse.

[*] https://lore.kernel.org/linux-fsdevel/[email protected]/

Thank you. How do you feel about?

---
 fs/xattr.c | 100 ++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 64 insertions(+), 36 deletions(-)
fs/xattr.c +64 -36
index d58979115200..133bca3371ee 100644
@@ -698,9 +698,10 @@ int filename_setxattr(int dfd, struct filename *filename,
 	return error;
 }
 
-static int path_setxattrat(int dfd, const char __user *pathname,
-			   unsigned int at_flags, const char __user *name,
-			   const void __user *value, size_t size, int flags)
+static int path_setxattrat(int dfd, struct file *dfd_file,
+			   const char __user *pathname, unsigned int at_flags,
+			   const char __user *name, const void __user *value,
+			   size_t size, int flags)
 {
 	struct xattr_name kname;
 	struct kernel_xattr_ctx ctx = {
@@ -725,11 +726,10 @@ static int path_setxattrat(int dfd, const char __user *pathname,
 
 	CLASS(filename_maybe_null, filename)(pathname, at_flags);
 	if (!filename && dfd >= 0) {
-		CLASS(fd, f)(dfd);
-		if (fd_empty(f))
+		if (!dfd_file)
 			error = -EBADF;
 		else
-			error = file_setxattr(fd_file(f), &ctx);
+			error = file_setxattr(dfd_file, &ctx);
 	} else {
 		error = filename_setxattr(dfd, filename, lookup_flags, &ctx);
 	}
@@ -756,7 +756,10 @@ SYSCALL_DEFINE6(setxattrat, int, dfd, const char __user *, pathname, unsigned in
 	if (error)
 		return error;
 
-	return path_setxattrat(dfd, pathname, at_flags, name,
+	/* setxattrat() accepts O_PATH file descriptors. */
+	CLASS(fd_raw, f)(dfd);
+
+	return path_setxattrat(dfd, fd_file(f), pathname, at_flags, name,
 			       u64_to_user_ptr(args.value), args.size,
 			       args.flags);
 }
@@ -765,21 +768,25 @@ SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
 		const char __user *, name, const void __user *, value,
 		size_t, size, int, flags)
 {
-	return path_setxattrat(AT_FDCWD, pathname, 0, name, value, size, flags);
+	return path_setxattrat(AT_FDCWD, NULL, pathname, 0, name, value, size,
+			       flags);
 }
 
 SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
 		const char __user *, name, const void __user *, value,
 		size_t, size, int, flags)
 {
-	return path_setxattrat(AT_FDCWD, pathname, AT_SYMLINK_NOFOLLOW, name,
-			       value, size, flags);
+	return path_setxattrat(AT_FDCWD, NULL, pathname, AT_SYMLINK_NOFOLLOW,
+			       name, value, size, flags);
 }
 
 SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
 		const void __user *,value, size_t, size, int, flags)
 {
-	return path_setxattrat(fd, NULL, AT_EMPTY_PATH, name,
+	/* fsetxattr() rejects O_PATH file descriptors. */
+	CLASS(fd, f)(fd);
+
+	return path_setxattrat(fd, fd_file(f), NULL, AT_EMPTY_PATH, name,
 			       value, size, flags);
 }
 
@@ -843,7 +850,8 @@ ssize_t filename_getxattr(int dfd, struct filename *filename,
 	return error;
 }
 
-static ssize_t path_getxattrat(int dfd, const char __user *pathname,
+static ssize_t path_getxattrat(int dfd, struct file *dfd_file,
+			       const char __user *pathname,
 			       unsigned int at_flags, const char __user *name,
 			       void __user *value, size_t size)
 {
@@ -865,10 +873,9 @@ static ssize_t path_getxattrat(int dfd, const char __user *pathname,
 
 	CLASS(filename_maybe_null, filename)(pathname, at_flags);
 	if (!filename && dfd >= 0) {
-		CLASS(fd, f)(dfd);
-		if (fd_empty(f))
+		if (!dfd_file)
 			return -EBADF;
-		return file_getxattr(fd_file(f), &ctx);
+		return file_getxattr(dfd_file, &ctx);
 	} else {
 		int lookup_flags = 0;
 		if (!(at_flags & AT_SYMLINK_NOFOLLOW))
@@ -898,27 +905,34 @@ SYSCALL_DEFINE6(getxattrat, int, dfd, const char __user *, pathname, unsigned in
 	if (args.flags != 0)
 		return -EINVAL;
 
-	return path_getxattrat(dfd, pathname, at_flags, name,
+	/* getxattrat() accepts O_PATH file descriptors. */
+	CLASS(fd_raw, f)(dfd);
+
+	return path_getxattrat(dfd, fd_file(f), pathname, at_flags, name,
 			       u64_to_user_ptr(args.value), args.size);
 }
 
 SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
 		const char __user *, name, void __user *, value, size_t, size)
 {
-	return path_getxattrat(AT_FDCWD, pathname, 0, name, value, size);
+	return path_getxattrat(AT_FDCWD, NULL, pathname, 0, name, value, size);
 }
 
 SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
 		const char __user *, name, void __user *, value, size_t, size)
 {
-	return path_getxattrat(AT_FDCWD, pathname, AT_SYMLINK_NOFOLLOW, name,
-			       value, size);
+	return path_getxattrat(AT_FDCWD, NULL, pathname, AT_SYMLINK_NOFOLLOW,
+			       name, value, size);
 }
 
 SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
 		void __user *, value, size_t, size)
 {
-	return path_getxattrat(fd, NULL, AT_EMPTY_PATH, name, value, size);
+	/* fgetxattr() rejects O_PATH file descriptors. */
+	CLASS(fd, f)(fd);
+
+	return path_getxattrat(fd, fd_file(f), NULL, AT_EMPTY_PATH, name, value,
+			       size);
 }
 
 /*
@@ -980,7 +994,8 @@ ssize_t filename_listxattr(int dfd, struct filename *filename,
 	return error;
 }
 
-static ssize_t path_listxattrat(int dfd, const char __user *pathname,
+static ssize_t path_listxattrat(int dfd, struct file *dfd_file,
+				const char __user *pathname,
 				unsigned int at_flags, char __user *list,
 				size_t size)
 {
@@ -991,10 +1006,9 @@ static ssize_t path_listxattrat(int dfd, const char __user *pathname,
 
 	CLASS(filename_maybe_null, filename)(pathname, at_flags);
 	if (!filename) {
-		CLASS(fd, f)(dfd);
-		if (fd_empty(f))
+		if (!dfd_file)
 			return -EBADF;
-		return file_listxattr(fd_file(f), list, size);
+		return file_listxattr(dfd_file, list, size);
 	}
 
 	lookup_flags = (at_flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
@@ -1005,24 +1019,31 @@ SYSCALL_DEFINE5(listxattrat, int, dfd, const char __user *, pathname,
 		unsigned int, at_flags,
 		char __user *, list, size_t, size)
 {
-	return path_listxattrat(dfd, pathname, at_flags, list, size);
+	/* listxattrat() accepts O_PATH file descriptors. */
+	CLASS(fd_raw, f)(dfd);
+
+	return path_listxattrat(dfd, fd_file(f), pathname, at_flags, list, size);
 }
 
 SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
 		size_t, size)
 {
-	return path_listxattrat(AT_FDCWD, pathname, 0, list, size);
+	return path_listxattrat(AT_FDCWD, NULL, pathname, 0, list, size);
 }
 
 SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
 		size_t, size)
 {
-	return path_listxattrat(AT_FDCWD, pathname, AT_SYMLINK_NOFOLLOW, list, size);
+	return path_listxattrat(AT_FDCWD, NULL, pathname, AT_SYMLINK_NOFOLLOW,
+				list, size);
 }
 
 SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
 {
-	return path_listxattrat(fd, NULL, AT_EMPTY_PATH, list, size);
+	/* flistxattr() rejects O_PATH file descriptors. */
+	CLASS(fd, f)(fd);
+
+	return path_listxattrat(fd, fd_file(f), NULL, AT_EMPTY_PATH, list, size);
 }
 
 /*
@@ -1072,7 +1093,8 @@ static int filename_removexattr(int dfd, struct filename *filename,
 	return error;
 }
 
-static int path_removexattrat(int dfd, const char __user *pathname,
+static int path_removexattrat(int dfd, struct file *dfd_file,
+			      const char __user *pathname,
 			      unsigned int at_flags, const char __user *name)
 {
 	struct xattr_name kname;
@@ -1088,10 +1110,9 @@ static int path_removexattrat(int dfd, const char __user *pathname,
 
 	CLASS(filename_maybe_null, filename)(pathname, at_flags);
 	if (!filename) {
-		CLASS(fd, f)(dfd);
-		if (fd_empty(f))
+		if (!dfd_file)
 			return -EBADF;
-		return file_removexattr(fd_file(f), &kname);
+		return file_removexattr(dfd_file, &kname);
 	}
 	lookup_flags = (at_flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
 	return filename_removexattr(dfd, filename, lookup_flags, &kname);
@@ -1100,24 +1121,31 @@ static int path_removexattrat(int dfd, const char __user *pathname,
 SYSCALL_DEFINE4(removexattrat, int, dfd, const char __user *, pathname,
 		unsigned int, at_flags, const char __user *, name)
 {
-	return path_removexattrat(dfd, pathname, at_flags, name);
+	/* removexattrat() accepts O_PATH file descriptors. */
+	CLASS(fd_raw, f)(dfd);
+
+	return path_removexattrat(dfd, fd_file(f), pathname, at_flags, name);
 }
 
 SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
 		const char __user *, name)
 {
-	return path_removexattrat(AT_FDCWD, pathname, 0, name);
+	return path_removexattrat(AT_FDCWD, NULL, pathname, 0, name);
 }
 
 SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
 		const char __user *, name)
 {
-	return path_removexattrat(AT_FDCWD, pathname, AT_SYMLINK_NOFOLLOW, name);
+	return path_removexattrat(AT_FDCWD, NULL, pathname, AT_SYMLINK_NOFOLLOW,
+				  name);
 }
 
 SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
 {
-	return path_removexattrat(fd, NULL, AT_EMPTY_PATH, name);
+	/* fremovexattr() rejects O_PATH file descriptors. */
+	CLASS(fd, f)(fd);
+
+	return path_removexattrat(fd, fd_file(f), NULL, AT_EMPTY_PATH, name);
 }
 
 int xattr_list_one(char **buffer, ssize_t *remaining_size, const char *name)
#2 lore raw
Quoted 12 lines · Christian Brauner
Quoted 11 lines · Andreas Gruenbacher

Christian,

here's an updated patch for making the *xattrat() system calls accept O_PATH file descriptors.

Compared to the previous version (see the discussion at [*]), this version adds a fd_maybe_raw cleanup class that uses either fdget() or fdget_raw() depending on which kinds of file descriptors are acceptable. That's still a but ugly, but all the alternatives I could come up with are much worse.

Quoted 3 lines
Quoted 1 line

[*] https://lore.kernel.org/linux-fsdevel/[email protected]/

Thank you. How do you feel about?

I don't think it's an improvement; see below.

Quoted 23 lines
---
 fs/xattr.c | 100 ++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 64 insertions(+), 36 deletions(-)
fs/xattr.c +4 -3
index d58979115200..133bca3371ee 100644
@@ -698,9 +698,10 @@ int filename_setxattr(int dfd, struct filename *filename,
        return error;
 }
 
-static int path_setxattrat(int dfd, const char __user *pathname,
-                          unsigned int at_flags, const char __user *name,
-                          const void __user *value, size_t size, int flags)
+static int path_setxattrat(int dfd, struct file *dfd_file,
+                          const char __user *pathname, unsigned int at_flags,
+                          const char __user *name, const void __user *value,
+                          size_t size, int flags)
 {
        struct xattr_name kname;
        struct kernel_xattr_ctx ctx = {

fs/xattr.c static int path_setxattrat(int dfd, const char __user *pathname, · line 730

Quoted 7 lines
fs/xattr.c +1 -2
@@ -725,11 +726,10 @@ static int path_setxattrat(int dfd, const char __user *pathname,
 
        CLASS(filename_maybe_null, filename)(pathname, at_flags);
        if (!filename && dfd >= 0) {
-               CLASS(fd, f)(dfd);
-               if (fd_empty(f))
+               if (!dfd_file)
                        error = -EBADF;

I don't particularly like that the initialization and the error checking are being split up here. The result is the same, though.

fs/xattr.c SYSCALL_DEFINE6(setxattrat, int, dfd, const char __user *, pathname, unsigned in · line 760

Quoted 13 lines
fs/xattr.c +3 -2
                else
-                       error = file_setxattr(fd_file(f), &ctx);
+                       error = file_setxattr(dfd_file, &ctx);
        } else {
                error = filename_setxattr(dfd, filename, lookup_flags, &ctx);
        }
@@ -756,7 +756,10 @@ SYSCALL_DEFINE6(setxattrat, int, dfd, const char __user *, pathname, unsigned in
        if (error)
                return error;
 
-       return path_setxattrat(dfd, pathname, at_flags, name,
+       /* setxattrat() accepts O_PATH file descriptors. */
+       CLASS(fd_raw, f)(dfd);

On this path, fdget_raw(dfd) will now be called unnecessarily when pathname is not NULL.

Quoted 59 lines
fs/xattr.c +14 -8
+
+       return path_setxattrat(dfd, fd_file(f), pathname, at_flags, name,
                               u64_to_user_ptr(args.value), args.size,
                               args.flags);
 }
@@ -765,21 +768,25 @@ SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
                const char __user *, name, const void __user *, value,
                size_t, size, int, flags)
 {
-       return path_setxattrat(AT_FDCWD, pathname, 0, name, value, size, flags);
+       return path_setxattrat(AT_FDCWD, NULL, pathname, 0, name, value, size,
+                              flags);
 }
 
 SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
                const char __user *, name, const void __user *, value,
                size_t, size, int, flags)
 {
-       return path_setxattrat(AT_FDCWD, pathname, AT_SYMLINK_NOFOLLOW, name,
-                              value, size, flags);
+       return path_setxattrat(AT_FDCWD, NULL, pathname, AT_SYMLINK_NOFOLLOW,
+                              name, value, size, flags);
 }
 
 SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
                const void __user *,value, size_t, size, int, flags)
 {
-       return path_setxattrat(fd, NULL, AT_EMPTY_PATH, name,
+       /* fsetxattr() rejects O_PATH file descriptors. */
+       CLASS(fd, f)(fd);
+
+       return path_setxattrat(fd, fd_file(f), NULL, AT_EMPTY_PATH, name,
                               value, size, flags);
 }
 
@@ -843,7 +850,8 @@ ssize_t filename_getxattr(int dfd, struct filename *filename,
        return error;
 }
 
-static ssize_t path_getxattrat(int dfd, const char __user *pathname,
+static ssize_t path_getxattrat(int dfd, struct file *dfd_file,
+                              const char __user *pathname,
                               unsigned int at_flags, const char __user *name,
                               void __user *value, size_t size)
 {
@@ -865,10 +873,9 @@ static ssize_t path_getxattrat(int dfd, const char __user *pathname,
 
        CLASS(filename_maybe_null, filename)(pathname, at_flags);
        if (!filename && dfd >= 0) {
-               CLASS(fd, f)(dfd);
-               if (fd_empty(f))
+               if (!dfd_file)
                        return -EBADF;
-               return file_getxattr(fd_file(f), &ctx);
+               return file_getxattr(dfd_file, &ctx);
        } else {
                int lookup_flags = 0;
                if (!(at_flags & AT_SYMLINK_NOFOLLOW))

fs/xattr.c SYSCALL_DEFINE6(getxattrat, int, dfd, const char __user *, pathname, unsigned in · line 909

Quoted 6 lines
fs/xattr.c +2 -1
@@ -898,27 +905,34 @@ SYSCALL_DEFINE6(getxattrat, int, dfd, const char __user *, pathname, unsigned in
        if (args.flags != 0)
                return -EINVAL;
 
-       return path_getxattrat(dfd, pathname, at_flags, name,
+       /* getxattrat() accepts O_PATH file descriptors. */
+       CLASS(fd_raw, f)(dfd);

Same here.

Quoted 57 lines
fs/xattr.c +14 -8
+
+       return path_getxattrat(dfd, fd_file(f), pathname, at_flags, name,
                               u64_to_user_ptr(args.value), args.size);
 }
 
 SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
                const char __user *, name, void __user *, value, size_t, size)
 {
-       return path_getxattrat(AT_FDCWD, pathname, 0, name, value, size);
+       return path_getxattrat(AT_FDCWD, NULL, pathname, 0, name, value, size);
 }
 
 SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
                const char __user *, name, void __user *, value, size_t, size)
 {
-       return path_getxattrat(AT_FDCWD, pathname, AT_SYMLINK_NOFOLLOW, name,
-                              value, size);
+       return path_getxattrat(AT_FDCWD, NULL, pathname, AT_SYMLINK_NOFOLLOW,
+                              name, value, size);
 }
 
 SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
                void __user *, value, size_t, size)
 {
-       return path_getxattrat(fd, NULL, AT_EMPTY_PATH, name, value, size);
+       /* fgetxattr() rejects O_PATH file descriptors. */
+       CLASS(fd, f)(fd);
+
+       return path_getxattrat(fd, fd_file(f), NULL, AT_EMPTY_PATH, name, value,
+                              size);
 }
 
 /*
@@ -980,7 +994,8 @@ ssize_t filename_listxattr(int dfd, struct filename *filename,
        return error;
 }
 
-static ssize_t path_listxattrat(int dfd, const char __user *pathname,
+static ssize_t path_listxattrat(int dfd, struct file *dfd_file,
+                               const char __user *pathname,
                                unsigned int at_flags, char __user *list,
                                size_t size)
 {
@@ -991,10 +1006,9 @@ static ssize_t path_listxattrat(int dfd, const char __user *pathname,
 
        CLASS(filename_maybe_null, filename)(pathname, at_flags);
        if (!filename) {
-               CLASS(fd, f)(dfd);
-               if (fd_empty(f))
+               if (!dfd_file)
                        return -EBADF;
-               return file_listxattr(fd_file(f), list, size);
+               return file_listxattr(dfd_file, list, size);
        }
 
        lookup_flags = (at_flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;

fs/xattr.c SYSCALL_DEFINE5(listxattrat, int, dfd, const char __user *, pathname, · line 1023

Quoted 6 lines
fs/xattr.c +2 -1
@@ -1005,24 +1019,31 @@ SYSCALL_DEFINE5(listxattrat, int, dfd, const char __user *, pathname,
                unsigned int, at_flags,
                char __user *, list, size_t, size)
 {
-       return path_listxattrat(dfd, pathname, at_flags, list, size);
+       /* listxattrat() accepts O_PATH file descriptors. */
+       CLASS(fd_raw, f)(dfd);

And here.

Quoted 53 lines
fs/xattr.c +13 -7
+
+       return path_listxattrat(dfd, fd_file(f), pathname, at_flags, list, size);
 }
 
 SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
                size_t, size)
 {
-       return path_listxattrat(AT_FDCWD, pathname, 0, list, size);
+       return path_listxattrat(AT_FDCWD, NULL, pathname, 0, list, size);
 }
 
 SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
                size_t, size)
 {
-       return path_listxattrat(AT_FDCWD, pathname, AT_SYMLINK_NOFOLLOW, list, size);
+       return path_listxattrat(AT_FDCWD, NULL, pathname, AT_SYMLINK_NOFOLLOW,
+                               list, size);
 }
 
 SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
 {
-       return path_listxattrat(fd, NULL, AT_EMPTY_PATH, list, size);
+       /* flistxattr() rejects O_PATH file descriptors. */
+       CLASS(fd, f)(fd);
+
+       return path_listxattrat(fd, fd_file(f), NULL, AT_EMPTY_PATH, list, size);
 }
 
 /*
@@ -1072,7 +1093,8 @@ static int filename_removexattr(int dfd, struct filename *filename,
        return error;
 }
 
-static int path_removexattrat(int dfd, const char __user *pathname,
+static int path_removexattrat(int dfd, struct file *dfd_file,
+                             const char __user *pathname,
                              unsigned int at_flags, const char __user *name)
 {
        struct xattr_name kname;
@@ -1088,10 +1110,9 @@ static int path_removexattrat(int dfd, const char __user *pathname,
 
        CLASS(filename_maybe_null, filename)(pathname, at_flags);
        if (!filename) {
-               CLASS(fd, f)(dfd);
-               if (fd_empty(f))
+               if (!dfd_file)
                        return -EBADF;
-               return file_removexattr(fd_file(f), &kname);
+               return file_removexattr(dfd_file, &kname);
        }
        lookup_flags = (at_flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
        return filename_removexattr(dfd, filename, lookup_flags, &kname);

fs/xattr.c static int path_removexattrat(int dfd, const char __user *pathname, · line 1125

Quoted 6 lines
fs/xattr.c +2 -1
@@ -1100,24 +1121,31 @@ static int path_removexattrat(int dfd, const char __user *pathname,
 SYSCALL_DEFINE4(removexattrat, int, dfd, const char __user *, pathname,
                unsigned int, at_flags, const char __user *, name)
 {
-       return path_removexattrat(dfd, pathname, at_flags, name);
+       /* removexattrat() accepts O_PATH file descriptors. */
+       CLASS(fd_raw, f)(dfd);

And here.

Quoted 20 lines
fs/xattr.c +5 -2
+
+       return path_removexattrat(dfd, fd_file(f), pathname, at_flags, name);
 }
 
 SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
                const char __user *, name)
 {
-       return path_removexattrat(AT_FDCWD, pathname, 0, name);
+       return path_removexattrat(AT_FDCWD, NULL, pathname, 0, name);
 }
 
 SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
                const char __user *, name)
 {
-       return path_removexattrat(AT_FDCWD, pathname, AT_SYMLINK_NOFOLLOW, name);
+       return path_removexattrat(AT_FDCWD, NULL, pathname, AT_SYMLINK_NOFOLLOW,
+                                 name);
 }
 

fs/xattr.c static int path_removexattrat(int dfd, const char __user *pathname, · line 1151

Quoted 10 lines
fs/xattr.c +4 -1
 SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
 {
-       return path_removexattrat(fd, NULL, AT_EMPTY_PATH, name);
+       /* fremovexattr() rejects O_PATH file descriptors. */
+       CLASS(fd, f)(fd);
+
+       return path_removexattrat(fd, fd_file(f), NULL, AT_EMPTY_PATH, name);
 }
 
 int xattr_list_one(char **buffer, ssize_t *remaining_size, const char *name)

Thanks,
Andreas

#3 lore raw
Quoted 13 lines · Andreas Gruenbacher
Quoted 12 lines · Christian Brauner
Quoted 11 lines · Andreas Gruenbacher

Christian,

here's an updated patch for making the *xattrat() system calls accept O_PATH file descriptors.

Compared to the previous version (see the discussion at [*]), this version adds a fd_maybe_raw cleanup class that uses either fdget() or fdget_raw() depending on which kinds of file descriptors are acceptable. That's still a but ugly, but all the alternatives I could come up with are much worse.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

[*] https://lore.kernel.org/linux-fsdevel/[email protected]/

Thank you. How do you feel about?

I don't think it's an improvement; see below.

Do you see other users outside of fs/xattr.c that would use this new fd class. If not it might be useful to just have a local version of this in fs/xattr.c?

#4 lore raw
Quoted 16 lines · Christian Brauner
Quoted 15 lines · Andreas Gruenbacher
Quoted 14 lines · Christian Brauner
Quoted 12 lines · Andreas Gruenbacher

Christian,

here's an updated patch for making the *xattrat() system calls accept O_PATH file descriptors.

Compared to the previous version (see the discussion at [*]), this version adds a fd_maybe_raw cleanup class that uses either fdget() or fdget_raw() depending on which kinds of file descriptors are acceptable. That's still a but ugly, but all the alternatives I could come up with are much worse.

[*] https://lore.kernel.org/linux-fsdevel/[email protected]/

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Thank you. How do you feel about?

I don't think it's an improvement; see below.

Do you see other users outside of fs/xattr.c that would use this new fd class. If not it might be useful to just have a local version of this in fs/xattr.c?

I hope nothing else will use fd_maybe_raw, but hiding it in fs/xattr.c still doesn't seem like a good idea. Do you insist on having it moved?

Thanks,
Andreas

#5 lore raw

Christian,

Quoted 19 lines · Andreas Gruenbacher
Quoted 18 lines · Christian Brauner
Quoted 17 lines · Andreas Gruenbacher
Quoted 15 lines · Christian Brauner
Quoted 5 lines · Andreas Gruenbacher

Christian,

here's an updated patch for making the *xattrat() system calls accept O_PATH file descriptors.

Quoted 7 lines

Compared to the previous version (see the discussion at [*]), this version adds a fd_maybe_raw cleanup class that uses either fdget() or fdget_raw() depending on which kinds of file descriptors are acceptable. That's still a but ugly, but all the alternatives I could come up with are much worse.

[*] https://lore.kernel.org/linux-fsdevel/[email protected]/

Thank you. How do you feel about?

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

I don't think it's an improvement; see below.

Do you see other users outside of fs/xattr.c that would use this new fd class. If not it might be useful to just have a local version of this in fs/xattr.c?

I hope nothing else will use fd_maybe_raw, but hiding it in fs/xattr.c still doesn't seem like a good idea. Do you insist on having it moved?

this patch from the end of July is still pending. There have been no changes in this area; the patch still applies cleanly. What are your thoughts on this?

Thanks,
Andreas