mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/4] xattr: fix AT_FDCWD handling
@ 2026-11-30 15:51 Christian Brauner
  2026-11-30 15:51 ` [PATCH 1/4] xattr: don't route f*xattr() through the *xattrat() helpers Christian Brauner
                   ` (4 more replies)
  0 siblings, 5 replies; 9+ messages in thread
From: Christian Brauner @ 2026-11-30 15:51 UTC (permalink / raw)
  To: linux-fsdevel
  Cc: Andreas Gruenbacher, Jan Kara, Alexander Viro,
	Christian Göttsche, linux-kernel,
	Christian Brauner (Amutable),
	stable

Two bugs in the AT_FDCWD handling of the xattr system calls while
reviewing Andreas' patch that lets *xattrat() accept O_PATH file
descriptors (cf. [1]) surfaced. Both predate that patch:

* fsetxattr() and fgetxattr() operate on the current working directory
  when passed AT_FDCWD instead of failing with EBADF. They're
  implemented on top of the *xattrat() helpers and since commit
  f520bed25d17 ("fs/xattr: Fix handling of AT_FDCWD in setxattrat(2) and
  getxattrat(2)") those resolve AT_FDCWD with an empty path to the
  current working directory. That's a regression since v6.15.

* listxattrat() and removexattrat() fail with EBADF when called with
  AT_FDCWD, an empty path and AT_EMPTY_PATH while setxattrat() and
  getxattrat() operate on the current working directory. They never got
  the same fix.

The f*xattr() system calls now resolve the file descriptor themselves
like fchmod() and fchown() do instead of going through the *xattrat()
helpers. That fixes the first bug and leaves the helpers to the path
based system calls. With this Andreas' patch doesn't need the
fd_maybe_raw class anymore as the helpers can simply use CLASS(fd_raw).
I've reworked it accordingly and it's the last patch in this series.

The new selftest covers both bugs.

[1]: https://lore.kernel.org/20260722151023.1308069-1-agruenba@redhat.com

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
Andreas Gruenbacher (1):
      xattrat: accept empty O_PATH file descriptors

Christian Brauner (3):
      xattr: don't route f*xattr() through the *xattrat() helpers
      xattr: let listxattrat() and removexattrat() handle AT_FDCWD
      selftests/xattr: add AT_FDCWD tests

 fs/xattr.c                                         |  73 +++++-
 .../testing/selftests/filesystems/xattr/.gitignore |   1 +
 tools/testing/selftests/filesystems/xattr/Makefile |   3 +-
 .../selftests/filesystems/xattr/xattr_fdcwd_test.c | 265 +++++++++++++++++++++
 4 files changed, 330 insertions(+), 12 deletions(-)
---
base-commit: 4a3b05a191ed84099d4ffaff7d29f73f08d27408
change-id: 20261130-work-xattr-at_fdcwd-misc-7b6ba7c0bc79


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 1/4] xattr: don't route f*xattr() through the *xattrat() helpers
  2026-11-30 15:51 [PATCH 0/4] xattr: fix AT_FDCWD handling Christian Brauner
@ 2026-11-30 15:51 ` Christian Brauner
  2026-11-30 18:02   ` Andreas Gruenbacher
  2026-11-30 15:51 ` [PATCH 2/4] xattr: let listxattrat() and removexattrat() handle AT_FDCWD Christian Brauner
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 9+ messages in thread
From: Christian Brauner @ 2026-11-30 15:51 UTC (permalink / raw)
  To: linux-fsdevel
  Cc: Andreas Gruenbacher, Jan Kara, Alexander Viro,
	Christian Göttsche, linux-kernel,
	Christian Brauner (Amutable),
	stable

Commit 6140be90ec70 ("fs/xattr: add *at family syscalls") turned the
f*xattr() system calls into *xattrat() calls with a NULL path and
AT_EMPTY_PATH. Commit f520bed25d17 ("fs/xattr: Fix handling of AT_FDCWD
in setxattrat(2) and getxattrat(2)") then made the helpers resolve
AT_FDCWD with an empty path to the current working directory. So
fsetxattr(AT_FDCWD, ...) and fgetxattr(AT_FDCWD, ...) now operate on
the current working directory instead of failing with EBADF.

Do what fchmod() and fchown() do and let the f*xattr() system calls
resolve the file descriptor themselves. They never need a path lookup
and the *xattrat() helpers are left to the path based system calls.

Fixes: f520bed25d17 ("fs/xattr: Fix handling of AT_FDCWD in setxattrat(2) and getxattrat(2)")
Cc: stable@vger.kernel.org # v6.15+
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 fs/xattr.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 56 insertions(+), 5 deletions(-)

diff --git a/fs/xattr.c b/fs/xattr.c
index 0705801a62cc5..8e7cceeb0d23c 100644
--- a/fs/xattr.c
+++ b/fs/xattr.c
@@ -779,8 +779,27 @@ SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
 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);
+	struct xattr_name kname;
+	struct kernel_xattr_ctx ctx = {
+		.cvalue	= value,
+		.kvalue	= NULL,
+		.size	= size,
+		.kname	= &kname,
+		.flags	= flags,
+	};
+	int error;
+
+	CLASS(fd, f)(fd);
+	if (fd_empty(f))
+		return -EBADF;
+
+	error = setxattr_copy(name, &ctx);
+	if (error)
+		return error;
+
+	error = file_setxattr(fd_file(f), &ctx);
+	kvfree(ctx.kvalue);
+	return error;
 }
 
 /*
@@ -918,7 +937,24 @@ SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
 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);
+	struct xattr_name kname;
+	struct kernel_xattr_ctx ctx = {
+		.value    = value,
+		.size     = size,
+		.kname    = &kname,
+		.flags    = 0,
+	};
+	ssize_t error;
+
+	CLASS(fd, f)(fd);
+	if (fd_empty(f))
+		return -EBADF;
+
+	error = import_xattr_name(&kname, name);
+	if (error)
+		return error;
+
+	return file_getxattr(fd_file(f), &ctx);
 }
 
 /*
@@ -1022,7 +1058,11 @@ SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
 
 SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
 {
-	return path_listxattrat(fd, NULL, AT_EMPTY_PATH, list, size);
+	CLASS(fd, f)(fd);
+	if (fd_empty(f))
+		return -EBADF;
+
+	return file_listxattr(fd_file(f), list, size);
 }
 
 /*
@@ -1117,7 +1157,18 @@ SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
 
 SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
 {
-	return path_removexattrat(fd, NULL, AT_EMPTY_PATH, name);
+	struct xattr_name kname;
+	int error;
+
+	CLASS(fd, f)(fd);
+	if (fd_empty(f))
+		return -EBADF;
+
+	error = import_xattr_name(&kname, name);
+	if (error)
+		return error;
+
+	return file_removexattr(fd_file(f), &kname);
 }
 
 int xattr_list_one(char **buffer, ssize_t *remaining_size, const char *name)

-- 
2.53.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 2/4] xattr: let listxattrat() and removexattrat() handle AT_FDCWD
  2026-11-30 15:51 [PATCH 0/4] xattr: fix AT_FDCWD handling Christian Brauner
  2026-11-30 15:51 ` [PATCH 1/4] xattr: don't route f*xattr() through the *xattrat() helpers Christian Brauner
@ 2026-11-30 15:51 ` Christian Brauner
  2026-11-30 18:03   ` Andreas Gruenbacher
  2026-11-30 18:04   ` Andreas Gruenbacher
  2026-11-30 15:51 ` [PATCH 3/4] selftests/xattr: add AT_FDCWD tests Christian Brauner
                   ` (2 subsequent siblings)
  4 siblings, 2 replies; 9+ messages in thread
From: Christian Brauner @ 2026-11-30 15:51 UTC (permalink / raw)
  To: linux-fsdevel
  Cc: Andreas Gruenbacher, Jan Kara, Alexander Viro,
	Christian Göttsche, linux-kernel,
	Christian Brauner (Amutable)

Commit f520bed25d17 ("fs/xattr: Fix handling of AT_FDCWD in
setxattrat(2) and getxattrat(2)") made setxattrat() and getxattrat()
operate on the current working directory when called with AT_FDCWD, an
empty path and AT_EMPTY_PATH, the same way fstatat() does. listxattrat()
and removexattrat() didn't get the same fix and still fail with EBADF.
Do what setxattrat() and getxattrat() do.

Fixes: 6140be90ec70 ("fs/xattr: add *at family syscalls")
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 fs/xattr.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/fs/xattr.c b/fs/xattr.c
index 8e7cceeb0d23c..0c59dfcbe6958 100644
--- a/fs/xattr.c
+++ b/fs/xattr.c
@@ -1026,7 +1026,7 @@ static ssize_t path_listxattrat(int dfd, const char __user *pathname,
 		return -EINVAL;
 
 	CLASS(filename_maybe_null, filename)(pathname, at_flags);
-	if (!filename) {
+	if (!filename && dfd >= 0) {
 		CLASS(fd, f)(dfd);
 		if (fd_empty(f))
 			return -EBADF;
@@ -1127,7 +1127,7 @@ static int path_removexattrat(int dfd, const char __user *pathname,
 		return error;
 
 	CLASS(filename_maybe_null, filename)(pathname, at_flags);
-	if (!filename) {
+	if (!filename && dfd >= 0) {
 		CLASS(fd, f)(dfd);
 		if (fd_empty(f))
 			return -EBADF;

-- 
2.53.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 3/4] selftests/xattr: add AT_FDCWD tests
  2026-11-30 15:51 [PATCH 0/4] xattr: fix AT_FDCWD handling Christian Brauner
  2026-11-30 15:51 ` [PATCH 1/4] xattr: don't route f*xattr() through the *xattrat() helpers Christian Brauner
  2026-11-30 15:51 ` [PATCH 2/4] xattr: let listxattrat() and removexattrat() handle AT_FDCWD Christian Brauner
@ 2026-11-30 15:51 ` Christian Brauner
  2026-11-30 15:51 ` [PATCH 4/4] xattrat: accept empty O_PATH file descriptors Christian Brauner
  2026-11-30 18:05 ` [PATCH 0/4] xattr: fix AT_FDCWD handling Andreas Gruenbacher
  4 siblings, 0 replies; 9+ messages in thread
From: Christian Brauner @ 2026-11-30 15:51 UTC (permalink / raw)
  To: linux-fsdevel
  Cc: Andreas Gruenbacher, Jan Kara, Alexander Viro,
	Christian Göttsche, linux-kernel,
	Christian Brauner (Amutable)

Add tests for the AT_FDCWD handling of the xattr system calls:

- f*xattr() with AT_FDCWD fail with EBADF and leave the cwd alone
- *xattrat() with AT_FDCWD, an empty or NULL path and AT_EMPTY_PATH
  operate on the cwd
- *xattrat() with any other negative dfd still fail with EBADF

Without the fixes fsetxattr() and fgetxattr() operate on the cwd and
listxattrat() and removexattrat() fail with EBADF.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 .../testing/selftests/filesystems/xattr/.gitignore |   1 +
 tools/testing/selftests/filesystems/xattr/Makefile |   3 +-
 .../selftests/filesystems/xattr/xattr_fdcwd_test.c | 265 +++++++++++++++++++++
 3 files changed, 268 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/filesystems/xattr/.gitignore b/tools/testing/selftests/filesystems/xattr/.gitignore
index 092d14094c0f6..fb7ef25ef077b 100644
--- a/tools/testing/selftests/filesystems/xattr/.gitignore
+++ b/tools/testing/selftests/filesystems/xattr/.gitignore
@@ -1,3 +1,4 @@
 xattr_socket_test
 xattr_sockfs_test
 xattr_socket_types_test
+xattr_fdcwd_test
diff --git a/tools/testing/selftests/filesystems/xattr/Makefile b/tools/testing/selftests/filesystems/xattr/Makefile
index 95364ffb10e91..3b466c13d5811 100644
--- a/tools/testing/selftests/filesystems/xattr/Makefile
+++ b/tools/testing/selftests/filesystems/xattr/Makefile
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 
 CFLAGS += $(KHDR_INCLUDES)
-TEST_GEN_PROGS := xattr_socket_test xattr_sockfs_test xattr_socket_types_test
+TEST_GEN_PROGS := xattr_socket_test xattr_sockfs_test xattr_socket_types_test \
+		  xattr_fdcwd_test
 
 include ../../lib.mk
diff --git a/tools/testing/selftests/filesystems/xattr/xattr_fdcwd_test.c b/tools/testing/selftests/filesystems/xattr/xattr_fdcwd_test.c
new file mode 100644
index 0000000000000..c782c057b6650
--- /dev/null
+++ b/tools/testing/selftests/filesystems/xattr/xattr_fdcwd_test.c
@@ -0,0 +1,265 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) 2026 Christian Brauner <brauner@kernel.org>
+/*
+ * Test AT_FDCWD handling of the f*xattr() and *xattrat() system calls.
+ *
+ * f*xattr() only take file descriptors and must fail with EBADF for
+ * AT_FDCWD. *xattrat() with AT_FDCWD, an empty or NULL path and
+ * AT_EMPTY_PATH operate on the current working directory.
+ */
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <sys/types.h>
+/* Must come before <sys/xattr.h> or struct xattr_args is hidden. */
+#include <linux/xattr.h>
+#include <sys/xattr.h>
+#include <unistd.h>
+
+#include "../../kselftest_harness.h"
+
+#define TEST_XATTR_NAME		"user.testattr"
+#define TEST_XATTR_VALUE	"testvalue"
+
+static const char *const empty_paths[] = { "", NULL };
+
+static int sys_setxattrat(int dfd, const char *path, unsigned int at_flags,
+			  const char *name, const void *value, size_t size,
+			  int flags)
+{
+	struct xattr_args args = {
+		.value	= (uintptr_t)value,
+		.size	= size,
+		.flags	= flags,
+	};
+
+	return syscall(__NR_setxattrat, dfd, path, at_flags, name, &args,
+		       sizeof(args));
+}
+
+static ssize_t sys_getxattrat(int dfd, const char *path,
+			      unsigned int at_flags, const char *name,
+			      void *value, size_t size)
+{
+	struct xattr_args args = {
+		.value	= (uintptr_t)value,
+		.size	= size,
+	};
+
+	return syscall(__NR_getxattrat, dfd, path, at_flags, name, &args,
+		       sizeof(args));
+}
+
+static ssize_t sys_listxattrat(int dfd, const char *path,
+			       unsigned int at_flags, char *list, size_t size)
+{
+	return syscall(__NR_listxattrat, dfd, path, at_flags, list, size);
+}
+
+static int sys_removexattrat(int dfd, const char *path,
+			     unsigned int at_flags, const char *name)
+{
+	return syscall(__NR_removexattrat, dfd, path, at_flags, name);
+}
+
+static bool list_has_name(const char *list, ssize_t len, const char *name)
+{
+	for (const char *p = list; p < list + len; p += strlen(p) + 1) {
+		if (!strcmp(p, name))
+			return true;
+	}
+	return false;
+}
+
+/* Each test runs in a fresh directory in /tmp that is its cwd. */
+FIXTURE(xattr_fdcwd)
+{
+	char dir[PATH_MAX];
+	int cwd_fd;
+};
+
+FIXTURE_SETUP(xattr_fdcwd)
+{
+	int ret;
+
+	self->cwd_fd = open(".", O_PATH | O_DIRECTORY | O_CLOEXEC);
+	ASSERT_GE(self->cwd_fd, 0);
+
+	snprintf(self->dir, sizeof(self->dir), "/tmp/xattr_fdcwd_test.XXXXXX");
+	ASSERT_NE(mkdtemp(self->dir), NULL);
+
+	ret = setxattr(self->dir, TEST_XATTR_NAME, TEST_XATTR_VALUE,
+		       strlen(TEST_XATTR_VALUE), 0);
+	if (ret < 0 && errno == EOPNOTSUPP) {
+		rmdir(self->dir);
+		SKIP(return, "user.* xattrs not supported in /tmp");
+	}
+	ASSERT_EQ(ret, 0);
+	ASSERT_EQ(removexattr(self->dir, TEST_XATTR_NAME), 0);
+
+	ASSERT_EQ(chdir(self->dir), 0);
+}
+
+FIXTURE_TEARDOWN(xattr_fdcwd)
+{
+	EXPECT_EQ(fchdir(self->cwd_fd), 0);
+	EXPECT_EQ(rmdir(self->dir), 0);
+	close(self->cwd_fd);
+}
+
+TEST_F(xattr_fdcwd, fsetxattr)
+{
+	char buf[64];
+
+	ASSERT_EQ(fsetxattr(AT_FDCWD, TEST_XATTR_NAME, TEST_XATTR_VALUE,
+			    strlen(TEST_XATTR_VALUE), 0), -1);
+	ASSERT_EQ(errno, EBADF);
+
+	/* The cwd must not have been touched. */
+	ASSERT_EQ(getxattr(".", TEST_XATTR_NAME, buf, sizeof(buf)), -1);
+	ASSERT_EQ(errno, ENODATA);
+}
+
+TEST_F(xattr_fdcwd, fgetxattr)
+{
+	char buf[64];
+
+	ASSERT_EQ(setxattr(".", TEST_XATTR_NAME, TEST_XATTR_VALUE,
+			   strlen(TEST_XATTR_VALUE), 0), 0);
+
+	ASSERT_EQ(fgetxattr(AT_FDCWD, TEST_XATTR_NAME, buf, sizeof(buf)), -1);
+	ASSERT_EQ(errno, EBADF);
+}
+
+TEST_F(xattr_fdcwd, flistxattr)
+{
+	char list[256];
+
+	ASSERT_EQ(setxattr(".", TEST_XATTR_NAME, TEST_XATTR_VALUE,
+			   strlen(TEST_XATTR_VALUE), 0), 0);
+
+	ASSERT_EQ(flistxattr(AT_FDCWD, list, sizeof(list)), -1);
+	ASSERT_EQ(errno, EBADF);
+}
+
+TEST_F(xattr_fdcwd, fremovexattr)
+{
+	char buf[64];
+
+	ASSERT_EQ(setxattr(".", TEST_XATTR_NAME, TEST_XATTR_VALUE,
+			   strlen(TEST_XATTR_VALUE), 0), 0);
+
+	ASSERT_EQ(fremovexattr(AT_FDCWD, TEST_XATTR_NAME), -1);
+	ASSERT_EQ(errno, EBADF);
+
+	/* The cwd must not have been touched. */
+	ASSERT_EQ(getxattr(".", TEST_XATTR_NAME, buf, sizeof(buf)),
+		  (ssize_t)strlen(TEST_XATTR_VALUE));
+}
+
+TEST_F(xattr_fdcwd, setxattrat)
+{
+	char buf[64];
+
+	for (size_t i = 0; i < ARRAY_SIZE(empty_paths); i++) {
+		ASSERT_EQ(sys_setxattrat(AT_FDCWD, empty_paths[i],
+					 AT_EMPTY_PATH, TEST_XATTR_NAME,
+					 TEST_XATTR_VALUE,
+					 strlen(TEST_XATTR_VALUE), 0), 0);
+
+		ASSERT_EQ(getxattr(".", TEST_XATTR_NAME, buf, sizeof(buf)),
+			  (ssize_t)strlen(TEST_XATTR_VALUE));
+		ASSERT_EQ(memcmp(buf, TEST_XATTR_VALUE,
+				 strlen(TEST_XATTR_VALUE)), 0);
+		ASSERT_EQ(removexattr(".", TEST_XATTR_NAME), 0);
+	}
+}
+
+TEST_F(xattr_fdcwd, getxattrat)
+{
+	char buf[64];
+
+	ASSERT_EQ(setxattr(".", TEST_XATTR_NAME, TEST_XATTR_VALUE,
+			   strlen(TEST_XATTR_VALUE), 0), 0);
+
+	for (size_t i = 0; i < ARRAY_SIZE(empty_paths); i++) {
+		memset(buf, 0, sizeof(buf));
+		ASSERT_EQ(sys_getxattrat(AT_FDCWD, empty_paths[i],
+					 AT_EMPTY_PATH, TEST_XATTR_NAME,
+					 buf, sizeof(buf)),
+			  (ssize_t)strlen(TEST_XATTR_VALUE));
+		ASSERT_EQ(memcmp(buf, TEST_XATTR_VALUE,
+				 strlen(TEST_XATTR_VALUE)), 0);
+	}
+}
+
+TEST_F(xattr_fdcwd, listxattrat)
+{
+	char list[256];
+	ssize_t len;
+
+	ASSERT_EQ(setxattr(".", TEST_XATTR_NAME, TEST_XATTR_VALUE,
+			   strlen(TEST_XATTR_VALUE), 0), 0);
+
+	for (size_t i = 0; i < ARRAY_SIZE(empty_paths); i++) {
+		len = sys_listxattrat(AT_FDCWD, empty_paths[i], AT_EMPTY_PATH,
+				      list, sizeof(list));
+		ASSERT_GT(len, 0) {
+			TH_LOG("listxattrat() failed: %s", strerror(errno));
+		}
+		ASSERT_TRUE(list_has_name(list, len, TEST_XATTR_NAME));
+	}
+}
+
+TEST_F(xattr_fdcwd, removexattrat)
+{
+	char buf[64];
+
+	for (size_t i = 0; i < ARRAY_SIZE(empty_paths); i++) {
+		ASSERT_EQ(setxattr(".", TEST_XATTR_NAME, TEST_XATTR_VALUE,
+				   strlen(TEST_XATTR_VALUE), 0), 0);
+
+		ASSERT_EQ(sys_removexattrat(AT_FDCWD, empty_paths[i],
+					    AT_EMPTY_PATH, TEST_XATTR_NAME), 0) {
+			TH_LOG("removexattrat() failed: %s", strerror(errno));
+		}
+
+		ASSERT_EQ(getxattr(".", TEST_XATTR_NAME, buf, sizeof(buf)), -1);
+		ASSERT_EQ(errno, ENODATA);
+	}
+}
+
+/* AT_FDCWD is special, any other negative dfd is still a bad fd. */
+TEST_F(xattr_fdcwd, xattrat_bad_dfd)
+{
+	char buf[256];
+
+	for (size_t i = 0; i < ARRAY_SIZE(empty_paths); i++) {
+		ASSERT_EQ(sys_setxattrat(-1, empty_paths[i], AT_EMPTY_PATH,
+					 TEST_XATTR_NAME, TEST_XATTR_VALUE,
+					 strlen(TEST_XATTR_VALUE), 0), -1);
+		ASSERT_EQ(errno, EBADF);
+
+		ASSERT_EQ(sys_getxattrat(-1, empty_paths[i], AT_EMPTY_PATH,
+					 TEST_XATTR_NAME, buf, sizeof(buf)), -1);
+		ASSERT_EQ(errno, EBADF);
+
+		ASSERT_EQ(sys_listxattrat(-1, empty_paths[i], AT_EMPTY_PATH,
+					  buf, sizeof(buf)), -1);
+		ASSERT_EQ(errno, EBADF);
+
+		ASSERT_EQ(sys_removexattrat(-1, empty_paths[i], AT_EMPTY_PATH,
+					    TEST_XATTR_NAME), -1);
+		ASSERT_EQ(errno, EBADF);
+	}
+}
+
+TEST_HARNESS_MAIN

-- 
2.53.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 4/4] xattrat: accept empty O_PATH file descriptors
  2026-11-30 15:51 [PATCH 0/4] xattr: fix AT_FDCWD handling Christian Brauner
                   ` (2 preceding siblings ...)
  2026-11-30 15:51 ` [PATCH 3/4] selftests/xattr: add AT_FDCWD tests Christian Brauner
@ 2026-11-30 15:51 ` Christian Brauner
  2026-11-30 18:05 ` [PATCH 0/4] xattr: fix AT_FDCWD handling Andreas Gruenbacher
  4 siblings, 0 replies; 9+ messages in thread
From: Christian Brauner @ 2026-11-30 15:51 UTC (permalink / raw)
  To: linux-fsdevel
  Cc: Andreas Gruenbacher, Jan Kara, Alexander Viro,
	Christian Göttsche, linux-kernel,
	Christian Brauner (Amutable)

From: Andreas Gruenbacher <agruenba@redhat.com>

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.

Christian Brauner <brauner@kernel.org> says:

The f*xattr() system calls don't go through the *xattrat() helpers
anymore. So the helpers can simply use CLASS(fd_raw) and the
fd_maybe_raw class isn't needed.

Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com>
Link: https://patch.msgid.link/20260722151023.1308069-1-agruenba@redhat.com
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 fs/xattr.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/fs/xattr.c b/fs/xattr.c
index 0c59dfcbe6958..1866618ecf723 100644
--- a/fs/xattr.c
+++ b/fs/xattr.c
@@ -725,7 +725,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_raw, f)(dfd);
 		if (fd_empty(f))
 			error = -EBADF;
 		else
@@ -884,7 +884,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_raw, f)(dfd);
 		if (fd_empty(f))
 			return -EBADF;
 		return file_getxattr(fd_file(f), &ctx);
@@ -1027,7 +1027,7 @@ static ssize_t path_listxattrat(int dfd, const char __user *pathname,
 
 	CLASS(filename_maybe_null, filename)(pathname, at_flags);
 	if (!filename && dfd >= 0) {
-		CLASS(fd, f)(dfd);
+		CLASS(fd_raw, f)(dfd);
 		if (fd_empty(f))
 			return -EBADF;
 		return file_listxattr(fd_file(f), list, size);
@@ -1128,7 +1128,7 @@ static int path_removexattrat(int dfd, const char __user *pathname,
 
 	CLASS(filename_maybe_null, filename)(pathname, at_flags);
 	if (!filename && dfd >= 0) {
-		CLASS(fd, f)(dfd);
+		CLASS(fd_raw, f)(dfd);
 		if (fd_empty(f))
 			return -EBADF;
 		return file_removexattr(fd_file(f), &kname);

-- 
2.53.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/4] xattr: don't route f*xattr() through the *xattrat() helpers
  2026-11-30 15:51 ` [PATCH 1/4] xattr: don't route f*xattr() through the *xattrat() helpers Christian Brauner
@ 2026-11-30 18:02   ` Andreas Gruenbacher
  0 siblings, 0 replies; 9+ messages in thread
From: Andreas Gruenbacher @ 2026-11-30 18:02 UTC (permalink / raw)
  To: Christian Brauner
  Cc: linux-fsdevel, Jan Kara, Alexander Viro, Christian Göttsche,
	linux-kernel, stable

On Mon, 30 Nov 2026 at 16:52, Christian Brauner <brauner@kernel.org> wrote:
> Commit 6140be90ec70 ("fs/xattr: add *at family syscalls") turned the
> f*xattr() system calls into *xattrat() calls with a NULL path and
> AT_EMPTY_PATH. Commit f520bed25d17 ("fs/xattr: Fix handling of AT_FDCWD
> in setxattrat(2) and getxattrat(2)") then made the helpers resolve
> AT_FDCWD with an empty path to the current working directory. So
> fsetxattr(AT_FDCWD, ...) and fgetxattr(AT_FDCWD, ...) now operate on
> the current working directory instead of failing with EBADF.
>
> Do what fchmod() and fchown() do and let the f*xattr() system calls
> resolve the file descriptor themselves. They never need a path lookup
> and the *xattrat() helpers are left to the path based system calls.
>
> Fixes: f520bed25d17 ("fs/xattr: Fix handling of AT_FDCWD in setxattrat(2) and getxattrat(2)")
> Cc: stable@vger.kernel.org # v6.15+
> Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>

Reviewd-by: Andreas Gruenbacher <agruenba@redhat.com>

> ---
>  fs/xattr.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
>  1 file changed, 56 insertions(+), 5 deletions(-)
>
> diff --git a/fs/xattr.c b/fs/xattr.c
> index 0705801a62cc5..8e7cceeb0d23c 100644
> --- a/fs/xattr.c
> +++ b/fs/xattr.c
> @@ -779,8 +779,27 @@ SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
>  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);
> +       struct xattr_name kname;
> +       struct kernel_xattr_ctx ctx = {
> +               .cvalue = value,
> +               .kvalue = NULL,
> +               .size   = size,
> +               .kname  = &kname,
> +               .flags  = flags,
> +       };
> +       int error;
> +
> +       CLASS(fd, f)(fd);
> +       if (fd_empty(f))
> +               return -EBADF;
> +
> +       error = setxattr_copy(name, &ctx);
> +       if (error)
> +               return error;
> +
> +       error = file_setxattr(fd_file(f), &ctx);
> +       kvfree(ctx.kvalue);
> +       return error;
>  }
>
>  /*
> @@ -918,7 +937,24 @@ SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
>  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);
> +       struct xattr_name kname;
> +       struct kernel_xattr_ctx ctx = {
> +               .value    = value,
> +               .size     = size,
> +               .kname    = &kname,
> +               .flags    = 0,
> +       };
> +       ssize_t error;
> +
> +       CLASS(fd, f)(fd);
> +       if (fd_empty(f))
> +               return -EBADF;
> +
> +       error = import_xattr_name(&kname, name);
> +       if (error)
> +               return error;
> +
> +       return file_getxattr(fd_file(f), &ctx);
>  }
>
>  /*
> @@ -1022,7 +1058,11 @@ SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
>
>  SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
>  {
> -       return path_listxattrat(fd, NULL, AT_EMPTY_PATH, list, size);
> +       CLASS(fd, f)(fd);
> +       if (fd_empty(f))
> +               return -EBADF;
> +
> +       return file_listxattr(fd_file(f), list, size);
>  }
>
>  /*
> @@ -1117,7 +1157,18 @@ SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
>
>  SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
>  {
> -       return path_removexattrat(fd, NULL, AT_EMPTY_PATH, name);
> +       struct xattr_name kname;
> +       int error;
> +
> +       CLASS(fd, f)(fd);
> +       if (fd_empty(f))
> +               return -EBADF;
> +
> +       error = import_xattr_name(&kname, name);
> +       if (error)
> +               return error;
> +
> +       return file_removexattr(fd_file(f), &kname);
>  }
>
>  int xattr_list_one(char **buffer, ssize_t *remaining_size, const char *name)
>
> --
> 2.53.0
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2/4] xattr: let listxattrat() and removexattrat() handle AT_FDCWD
  2026-11-30 15:51 ` [PATCH 2/4] xattr: let listxattrat() and removexattrat() handle AT_FDCWD Christian Brauner
@ 2026-11-30 18:03   ` Andreas Gruenbacher
  2026-11-30 18:04   ` Andreas Gruenbacher
  1 sibling, 0 replies; 9+ messages in thread
From: Andreas Gruenbacher @ 2026-11-30 18:03 UTC (permalink / raw)
  To: Christian Brauner
  Cc: linux-fsdevel, Jan Kara, Alexander Viro, Christian Göttsche,
	linux-kernel

On Mon, 30 Nov 2026 at 16:52, Christian Brauner <brauner@kernel.org> wrote:
> Commit f520bed25d17 ("fs/xattr: Fix handling of AT_FDCWD in
> setxattrat(2) and getxattrat(2)") made setxattrat() and getxattrat()
> operate on the current working directory when called with AT_FDCWD, an
> empty path and AT_EMPTY_PATH, the same way fstatat() does. listxattrat()
> and removexattrat() didn't get the same fix and still fail with EBADF.
> Do what setxattrat() and getxattrat() do.
>
> Fixes: 6140be90ec70 ("fs/xattr: add *at family syscalls")
> Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>

> ---
>  fs/xattr.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/fs/xattr.c b/fs/xattr.c
> index 8e7cceeb0d23c..0c59dfcbe6958 100644
> --- a/fs/xattr.c
> +++ b/fs/xattr.c
> @@ -1026,7 +1026,7 @@ static ssize_t path_listxattrat(int dfd, const char __user *pathname,
>                 return -EINVAL;
>
>         CLASS(filename_maybe_null, filename)(pathname, at_flags);
> -       if (!filename) {
> +       if (!filename && dfd >= 0) {
>                 CLASS(fd, f)(dfd);
>                 if (fd_empty(f))
>                         return -EBADF;
> @@ -1127,7 +1127,7 @@ static int path_removexattrat(int dfd, const char __user *pathname,
>                 return error;
>
>         CLASS(filename_maybe_null, filename)(pathname, at_flags);
> -       if (!filename) {
> +       if (!filename && dfd >= 0) {
>                 CLASS(fd, f)(dfd);
>                 if (fd_empty(f))
>                         return -EBADF;
>
> --
> 2.53.0
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2/4] xattr: let listxattrat() and removexattrat() handle AT_FDCWD
  2026-11-30 15:51 ` [PATCH 2/4] xattr: let listxattrat() and removexattrat() handle AT_FDCWD Christian Brauner
  2026-11-30 18:03   ` Andreas Gruenbacher
@ 2026-11-30 18:04   ` Andreas Gruenbacher
  1 sibling, 0 replies; 9+ messages in thread
From: Andreas Gruenbacher @ 2026-11-30 18:04 UTC (permalink / raw)
  To: Christian Brauner
  Cc: linux-fsdevel, Jan Kara, Alexander Viro, Christian Göttsche,
	linux-kernel

On Mon, 30 Nov 2026 at 16:52, Christian Brauner <brauner@kernel.org> wrote:
> Commit f520bed25d17 ("fs/xattr: Fix handling of AT_FDCWD in
> setxattrat(2) and getxattrat(2)") made setxattrat() and getxattrat()
> operate on the current working directory when called with AT_FDCWD, an
> empty path and AT_EMPTY_PATH, the same way fstatat() does. listxattrat()
> and removexattrat() didn't get the same fix and still fail with EBADF.
> Do what setxattrat() and getxattrat() do.
>
> Fixes: 6140be90ec70 ("fs/xattr: add *at family syscalls")
> Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>

Reviewd-by: Andreas Gruenbacher <agruenba@redhat.com>

> ---
>  fs/xattr.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/fs/xattr.c b/fs/xattr.c
> index 8e7cceeb0d23c..0c59dfcbe6958 100644
> --- a/fs/xattr.c
> +++ b/fs/xattr.c
> @@ -1026,7 +1026,7 @@ static ssize_t path_listxattrat(int dfd, const char __user *pathname,
>                 return -EINVAL;
>
>         CLASS(filename_maybe_null, filename)(pathname, at_flags);
> -       if (!filename) {
> +       if (!filename && dfd >= 0) {
>                 CLASS(fd, f)(dfd);
>                 if (fd_empty(f))
>                         return -EBADF;
> @@ -1127,7 +1127,7 @@ static int path_removexattrat(int dfd, const char __user *pathname,
>                 return error;
>
>         CLASS(filename_maybe_null, filename)(pathname, at_flags);
> -       if (!filename) {
> +       if (!filename && dfd >= 0) {
>                 CLASS(fd, f)(dfd);
>                 if (fd_empty(f))
>                         return -EBADF;
>
> --
> 2.53.0
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 0/4] xattr: fix AT_FDCWD handling
  2026-11-30 15:51 [PATCH 0/4] xattr: fix AT_FDCWD handling Christian Brauner
                   ` (3 preceding siblings ...)
  2026-11-30 15:51 ` [PATCH 4/4] xattrat: accept empty O_PATH file descriptors Christian Brauner
@ 2026-11-30 18:05 ` Andreas Gruenbacher
  4 siblings, 0 replies; 9+ messages in thread
From: Andreas Gruenbacher @ 2026-11-30 18:05 UTC (permalink / raw)
  To: Christian Brauner
  Cc: linux-fsdevel, Jan Kara, Alexander Viro, Christian Göttsche,
	linux-kernel, stable

On Mon, 30 Nov 2026 at 16:52, Christian Brauner <brauner@kernel.org> wrote:
> Two bugs in the AT_FDCWD handling of the xattr system calls while
> reviewing Andreas' patch that lets *xattrat() accept O_PATH file
> descriptors (cf. [1]) surfaced. Both predate that patch:
>
> * fsetxattr() and fgetxattr() operate on the current working directory
>   when passed AT_FDCWD instead of failing with EBADF. They're
>   implemented on top of the *xattrat() helpers and since commit
>   f520bed25d17 ("fs/xattr: Fix handling of AT_FDCWD in setxattrat(2) and
>   getxattrat(2)") those resolve AT_FDCWD with an empty path to the
>   current working directory. That's a regression since v6.15.
>
> * listxattrat() and removexattrat() fail with EBADF when called with
>   AT_FDCWD, an empty path and AT_EMPTY_PATH while setxattrat() and
>   getxattrat() operate on the current working directory. They never got
>   the same fix.
>
> The f*xattr() system calls now resolve the file descriptor themselves
> like fchmod() and fchown() do instead of going through the *xattrat()
> helpers. That fixes the first bug and leaves the helpers to the path
> based system calls. With this Andreas' patch doesn't need the
> fd_maybe_raw class anymore as the helpers can simply use CLASS(fd_raw).
> I've reworked it accordingly and it's the last patch in this series.
>
> The new selftest covers both bugs.

Thanks, this is all looking good.

Andreas

> [1]: https://lore.kernel.org/20260722151023.1308069-1-agruenba@redhat.com
>
> Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
> ---
> Andreas Gruenbacher (1):
>       xattrat: accept empty O_PATH file descriptors
>
> Christian Brauner (3):
>       xattr: don't route f*xattr() through the *xattrat() helpers
>       xattr: let listxattrat() and removexattrat() handle AT_FDCWD
>       selftests/xattr: add AT_FDCWD tests
>
>  fs/xattr.c                                         |  73 +++++-
>  .../testing/selftests/filesystems/xattr/.gitignore |   1 +
>  tools/testing/selftests/filesystems/xattr/Makefile |   3 +-
>  .../selftests/filesystems/xattr/xattr_fdcwd_test.c | 265 +++++++++++++++++++++
>  4 files changed, 330 insertions(+), 12 deletions(-)
> ---
> base-commit: 4a3b05a191ed84099d4ffaff7d29f73f08d27408
> change-id: 20261130-work-xattr-at_fdcwd-misc-7b6ba7c0bc79
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-11-30 18:05 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-30 15:51 [PATCH 0/4] xattr: fix AT_FDCWD handling Christian Brauner
2026-11-30 15:51 ` [PATCH 1/4] xattr: don't route f*xattr() through the *xattrat() helpers Christian Brauner
2026-11-30 18:02   ` Andreas Gruenbacher
2026-11-30 15:51 ` [PATCH 2/4] xattr: let listxattrat() and removexattrat() handle AT_FDCWD Christian Brauner
2026-11-30 18:03   ` Andreas Gruenbacher
2026-11-30 18:04   ` Andreas Gruenbacher
2026-11-30 15:51 ` [PATCH 3/4] selftests/xattr: add AT_FDCWD tests Christian Brauner
2026-11-30 15:51 ` [PATCH 4/4] xattrat: accept empty O_PATH file descriptors Christian Brauner
2026-11-30 18:05 ` [PATCH 0/4] xattr: fix AT_FDCWD handling Andreas Gruenbacher

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®