Skip to contentSkip to conversation

fs/xattr: add *at family syscalls

RFC PATCH In review Christian Göttsche 4 participants 6 messages May 11, 2023 – May 15, 2023

Subsystems (21)
Arm (32-bit) x86 VFS PowerPC +17 more subsystems
lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 1/1 fs/xattr: add *at family syscalls

Add the four syscalls setxattrat(), getxattrat(), listxattrat() and removexattrat(). Those can be used to operate on extended attributes, especially security related ones, either relative to a pinned directory or on a file descriptor without read access, avoiding a /proc/<pid>/fd/<fd> detour, requiring a mounted procfs.

One use case will be setfiles(8) setting SELinux file contexts ("security.selinux") without race conditions.

Add XATTR flags to the private namespace of AT_* flags.

Use the do_{name}at() pattern from fs/open.c.

Use a single flag parameter for extended attribute flags (currently XATTR_CREATE and XATTR_REPLACE) and *at() flags to not exceed six syscall arguments in setxattrat().

Previous approach ("f*xattr: allow O_PATH descriptors"): https://lore.kernel.org/all/[email protected]/
v1 discussion: https://lore.kernel.org/all/[email protected]/

---
v2:
- squash syscall introduction and wire up commits
- add AT_XATTR_CREATE and AT_XATTR_REPLACE constants

---
 arch/alpha/kernel/syscalls/syscall.tbl      |   4 +
 arch/arm/tools/syscall.tbl                  |   4 +
 arch/arm64/include/asm/unistd.h             |   2 +-
 arch/arm64/include/asm/unistd32.h           |   8 ++
 arch/ia64/kernel/syscalls/syscall.tbl       |   4 +
 arch/m68k/kernel/syscalls/syscall.tbl       |   4 +
 arch/microblaze/kernel/syscalls/syscall.tbl |   4 +
 arch/mips/kernel/syscalls/syscall_n32.tbl   |   4 +
 arch/mips/kernel/syscalls/syscall_n64.tbl   |   4 +
 arch/mips/kernel/syscalls/syscall_o32.tbl   |   4 +
 arch/parisc/kernel/syscalls/syscall.tbl     |   4 +
 arch/powerpc/kernel/syscalls/syscall.tbl    |   4 +
 arch/s390/kernel/syscalls/syscall.tbl       |   4 +
 arch/sh/kernel/syscalls/syscall.tbl         |   4 +
 arch/sparc/kernel/syscalls/syscall.tbl      |   4 +
 arch/x86/entry/syscalls/syscall_32.tbl      |   4 +
 arch/x86/entry/syscalls/syscall_64.tbl      |   4 +
 arch/xtensa/kernel/syscalls/syscall.tbl     |   4 +
 fs/xattr.c                                  | 107 ++++++++++++++++----
 include/asm-generic/audit_change_attr.h     |   6 ++
 include/linux/syscalls.h                    |   8 ++
 include/uapi/asm-generic/unistd.h           |  12 ++-
 include/uapi/linux/fcntl.h                  |   2 +
 23 files changed, 185 insertions(+), 24 deletions(-)
arch/alpha/kernel/syscalls/syscall.tbl +4 -0
index 8ebacf37a8cf..1dc58a5dc730 100644
@@ -490,3 +490,7 @@
 558	common	process_mrelease		sys_process_mrelease
 559	common  futex_waitv                     sys_futex_waitv
 560	common	set_mempolicy_home_node		sys_ni_syscall
+561	common	setxattrat			sys_setxattrat
+562	common	getxattrat			sys_getxattrat
+563	common	listxattrat			sys_listxattrat
+564	common	removexattrat			sys_removexattrat
arch/arm/tools/syscall.tbl +4 -0
index ac964612d8b0..f0e9d9d487f0 100644
@@ -464,3 +464,7 @@
 448	common	process_mrelease		sys_process_mrelease
 449	common	futex_waitv			sys_futex_waitv
 450	common	set_mempolicy_home_node		sys_set_mempolicy_home_node
+451	common	setxattrat			sys_setxattrat
+452	common	getxattrat			sys_getxattrat
+453	common	listxattrat			sys_listxattrat
+454	common	removexattrat			sys_removexattrat
arch/arm64/include/asm/unistd.h +1 -1
index 037feba03a51..63a8a9c4abc1 100644
@@ -39,7 +39,7 @@
 #define __ARM_NR_compat_set_tls		(__ARM_NR_COMPAT_BASE + 5)
 #define __ARM_NR_COMPAT_END		(__ARM_NR_COMPAT_BASE + 0x800)
 
-#define __NR_compat_syscalls		451
+#define __NR_compat_syscalls		455
 #endif
 
 #define __ARCH_WANT_SYS_CLONE
arch/arm64/include/asm/unistd32.h +8 -0
index 604a2053d006..cd6ac63376d1 100644
@@ -907,6 +907,14 @@ __SYSCALL(__NR_process_mrelease, sys_process_mrelease)
 __SYSCALL(__NR_futex_waitv, sys_futex_waitv)
 #define __NR_set_mempolicy_home_node 450
 __SYSCALL(__NR_set_mempolicy_home_node, sys_set_mempolicy_home_node)
+#define __NR_setxattrat 451
+__SYSCALL(__NR_setxattrat, sys_setxattrat)
+#define __NR_getxattrat 452
+__SYSCALL(__NR_getxattrat, sys_getxattrat)
+#define __NR_listxattrat 453
+__SYSCALL(__NR_listxattrat, sys_listxattrat)
+#define __NR_removexattrat 454
+__SYSCALL(__NR_removexattrat, sys_removexattrat)
 
 /*
  * Please add new compat syscalls above this comment and update
arch/ia64/kernel/syscalls/syscall.tbl +4 -0
index 72c929d9902b..fe9aea54222c 100644
@@ -371,3 +371,7 @@
 448	common	process_mrelease		sys_process_mrelease
 449	common  futex_waitv                     sys_futex_waitv
 450	common	set_mempolicy_home_node		sys_set_mempolicy_home_node
+451	common	setxattrat			sys_setxattrat
+452	common	getxattrat			sys_getxattrat
+453	common	listxattrat			sys_listxattrat
+454	common	removexattrat			sys_removexattrat
arch/m68k/kernel/syscalls/syscall.tbl +4 -0
index b1f3940bc298..0847efdee734 100644
@@ -450,3 +450,7 @@
 448	common	process_mrelease		sys_process_mrelease
 449	common  futex_waitv                     sys_futex_waitv
 450	common	set_mempolicy_home_node		sys_set_mempolicy_home_node
+451	common	setxattrat			sys_setxattrat
+452	common	getxattrat			sys_getxattrat
+453	common	listxattrat			sys_listxattrat
+454	common	removexattrat			sys_removexattrat
arch/microblaze/kernel/syscalls/syscall.tbl +4 -0
index 820145e47350..7f619bbc718d 100644
@@ -456,3 +456,7 @@
 448	common	process_mrelease		sys_process_mrelease
 449	common  futex_waitv                     sys_futex_waitv
 450	common	set_mempolicy_home_node		sys_set_mempolicy_home_node
+451	common	setxattrat			sys_setxattrat
+452	common	getxattrat			sys_getxattrat
+453	common	listxattrat			sys_listxattrat
+454	common	removexattrat			sys_removexattrat
arch/mips/kernel/syscalls/syscall_n32.tbl +4 -0
index 253ff994ed2e..5e4206c0aede 100644
@@ -389,3 +389,7 @@
 448	n32	process_mrelease		sys_process_mrelease
 449	n32	futex_waitv			sys_futex_waitv
 450	n32	set_mempolicy_home_node		sys_set_mempolicy_home_node
+451	n32	setxattrat			sys_setxattrat
+452	n32	getxattrat			sys_getxattrat
+453	n32	listxattrat			sys_listxattrat
+454	n32	removexattrat			sys_removexattrat
arch/mips/kernel/syscalls/syscall_n64.tbl +4 -0
index 3f1886ad9d80..df0f053e76cd 100644
@@ -365,3 +365,7 @@
 448	n64	process_mrelease		sys_process_mrelease
 449	n64	futex_waitv			sys_futex_waitv
 450	common	set_mempolicy_home_node		sys_set_mempolicy_home_node
+451	n64	setxattrat			sys_setxattrat
+452	n64	getxattrat			sys_getxattrat
+453	n64	listxattrat			sys_listxattrat
+454	n64	removexattrat			sys_removexattrat
arch/mips/kernel/syscalls/syscall_o32.tbl +4 -0
index 8f243e35a7b2..09ec31ad475f 100644
@@ -438,3 +438,7 @@
 448	o32	process_mrelease		sys_process_mrelease
 449	o32	futex_waitv			sys_futex_waitv
 450	o32	set_mempolicy_home_node		sys_set_mempolicy_home_node
+451	o32	setxattrat			sys_setxattrat
+452	o32	getxattrat			sys_getxattrat
+453	o32	listxattrat			sys_listxattrat
+454	o32	removexattrat			sys_removexattrat
arch/parisc/kernel/syscalls/syscall.tbl +4 -0
index 0e42fceb2d5e..0123f895a674 100644
@@ -448,3 +448,7 @@
 448	common	process_mrelease		sys_process_mrelease
 449	common	futex_waitv			sys_futex_waitv
 450	common	set_mempolicy_home_node		sys_set_mempolicy_home_node
+451	common	setxattrat			sys_setxattrat
+452	common	getxattrat			sys_getxattrat
+453	common	listxattrat			sys_listxattrat
+454	common	removexattrat			sys_removexattrat
arch/powerpc/kernel/syscalls/syscall.tbl +4 -0
index a0be127475b1..06fd4153f0d1 100644
@@ -537,3 +537,7 @@
 448	common	process_mrelease		sys_process_mrelease
 449	common  futex_waitv                     sys_futex_waitv
 450 	nospu	set_mempolicy_home_node		sys_set_mempolicy_home_node
+451	common	setxattrat			sys_setxattrat
+452	common	getxattrat			sys_getxattrat
+453	common	listxattrat			sys_listxattrat
+454	common	removexattrat			sys_removexattrat
arch/s390/kernel/syscalls/syscall.tbl +4 -0
index b68f47541169..9babd831fe1e 100644
@@ -453,3 +453,7 @@
 448  common	process_mrelease	sys_process_mrelease		sys_process_mrelease
 449  common	futex_waitv		sys_futex_waitv			sys_futex_waitv
 450  common	set_mempolicy_home_node	sys_set_mempolicy_home_node	sys_set_mempolicy_home_node
+451  common	setxattrat		sys_setxattrat			sys_setxattrat
+452  common	getxattrat		sys_getxattrat			sys_getxattrat
+453  common	listxattrat		sys_listxattrat			sys_listxattrat
+454  common	removexattrat		sys_removexattrat		sys_removexattrat
arch/sh/kernel/syscalls/syscall.tbl +4 -0
index 2de85c977f54..d4daa8afe45c 100644
@@ -453,3 +453,7 @@
 448	common	process_mrelease		sys_process_mrelease
 449	common  futex_waitv                     sys_futex_waitv
 450	common	set_mempolicy_home_node		sys_set_mempolicy_home_node
+451	common	setxattrat			sys_setxattrat
+452	common	getxattrat			sys_getxattrat
+453	common	listxattrat			sys_listxattrat
+454	common	removexattrat			sys_removexattrat
arch/sparc/kernel/syscalls/syscall.tbl +4 -0
index 4398cc6fb68d..510d5175f80a 100644
@@ -496,3 +496,7 @@
 448	common	process_mrelease		sys_process_mrelease
 449	common  futex_waitv                     sys_futex_waitv
 450	common	set_mempolicy_home_node		sys_set_mempolicy_home_node
+451	common	setxattrat			sys_setxattrat
+452	common	getxattrat			sys_getxattrat
+453	common	listxattrat			sys_listxattrat
+454	common	removexattrat			sys_removexattrat
arch/x86/entry/syscalls/syscall_32.tbl +4 -0
index 320480a8db4f..8488cc157fe0 100644
@@ -455,3 +455,7 @@
 448	i386	process_mrelease	sys_process_mrelease
 449	i386	futex_waitv		sys_futex_waitv
 450	i386	set_mempolicy_home_node		sys_set_mempolicy_home_node
+451	i386	setxattrat		sys_setxattrat
+452	i386	getxattrat		sys_getxattrat
+453	i386	listxattrat		sys_listxattrat
+454	i386	removexattrat		sys_removexattrat
arch/x86/entry/syscalls/syscall_64.tbl +4 -0
index c84d12608cd2..f45d723d5a30 100644
@@ -372,6 +372,10 @@
 448	common	process_mrelease	sys_process_mrelease
 449	common	futex_waitv		sys_futex_waitv
 450	common	set_mempolicy_home_node	sys_set_mempolicy_home_node
+451	common	setxattrat		sys_setxattrat
+452	common	getxattrat		sys_getxattrat
+453	common	listxattrat		sys_listxattrat
+454	common	removexattrat		sys_removexattrat
 
 #
 # Due to a historical design error, certain syscalls are numbered differently
arch/xtensa/kernel/syscalls/syscall.tbl +4 -0
index 52c94ab5c205..dbafe441a83f 100644
@@ -421,3 +421,7 @@
 448	common	process_mrelease		sys_process_mrelease
 449	common  futex_waitv                     sys_futex_waitv
 450	common	set_mempolicy_home_node		sys_set_mempolicy_home_node
+451	common	setxattrat			sys_setxattrat
+452	common	getxattrat			sys_getxattrat
+453	common	listxattrat			sys_listxattrat
+454	common	removexattrat			sys_removexattrat
fs/xattr.c +85 -22

219 lines not on this page · raw message

include/asm-generic/audit_change_attr.h +6 -0
index 331670807cf0..cc840537885f 100644
@@ -11,9 +11,15 @@ __NR_lchown,
 __NR_fchown,
 #endif
 __NR_setxattr,
+#ifdef __NR_setxattrat
+__NR_setxattrat,
+#endif
 __NR_lsetxattr,
 __NR_fsetxattr,
 __NR_removexattr,
+#ifdef __NR_removexattrat
+__NR_removexattrat,
+#endif
 __NR_lremovexattr,
 __NR_fremovexattr,
 #ifdef __NR_fchownat
include/linux/syscalls.h +8 -0
index 33a0ee3bcb2e..0612661c9eca 100644
@@ -350,23 +350,31 @@ asmlinkage long sys_io_uring_register(unsigned int fd, unsigned int op,
 /* fs/xattr.c */
 asmlinkage long sys_setxattr(const char __user *path, const char __user *name,
 			     const void __user *value, size_t size, int flags);
+asmlinkage long sys_setxattrat(int dfd, const char __user *path, const char __user *name,
+			     const void __user *value, size_t size, int flags);
 asmlinkage long sys_lsetxattr(const char __user *path, const char __user *name,
 			      const void __user *value, size_t size, int flags);
 asmlinkage long sys_fsetxattr(int fd, const char __user *name,
 			      const void __user *value, size_t size, int flags);
 asmlinkage long sys_getxattr(const char __user *path, const char __user *name,
 			     void __user *value, size_t size);
+asmlinkage long sys_getxattrat(int dfd, const char __user *path, const char __user *name,
+			     void __user *value, size_t size, int flags);
 asmlinkage long sys_lgetxattr(const char __user *path, const char __user *name,
 			      void __user *value, size_t size);
 asmlinkage long sys_fgetxattr(int fd, const char __user *name,
 			      void __user *value, size_t size);
 asmlinkage long sys_listxattr(const char __user *path, char __user *list,
 			      size_t size);
+asmlinkage long sys_listxattrat(int dfd, const char __user *path, char __user *list,
+			      size_t size, int flags);
 asmlinkage long sys_llistxattr(const char __user *path, char __user *list,
 			       size_t size);
 asmlinkage long sys_flistxattr(int fd, char __user *list, size_t size);
 asmlinkage long sys_removexattr(const char __user *path,
 				const char __user *name);
+asmlinkage long sys_removexattrat(int dfd, const char __user *path,
+				const char __user *name, int flags);
 asmlinkage long sys_lremovexattr(const char __user *path,
 				 const char __user *name);
 asmlinkage long sys_fremovexattr(int fd, const char __user *name);
include/uapi/asm-generic/unistd.h +11 -1
index 45fa180cc56a..4fcc71612b7a 100644
@@ -886,8 +886,18 @@ __SYSCALL(__NR_futex_waitv, sys_futex_waitv)
 #define __NR_set_mempolicy_home_node 450
 __SYSCALL(__NR_set_mempolicy_home_node, sys_set_mempolicy_home_node)
 
+/* fs/xattr.c */
+#define __NR_setxattrat 451
+__SYSCALL(__NR_setxattrat, sys_setxattrat)
+#define __NR_getxattrat 452
+__SYSCALL(__NR_getxattrat, sys_getxattrat)
+#define __NR_listxattrat 453
+__SYSCALL(__NR_listxattrat, sys_listxattrat)
+#define __NR_removexattrat 454
+__SYSCALL(__NR_removexattrat, sys_removexattrat)
+
 #undef __NR_syscalls
-#define __NR_syscalls 451
+#define __NR_syscalls 455
 
 /*
  * 32 bit systems traditionally used different
include/uapi/linux/fcntl.h +2 -0
index e8c07da58c9f..b456547c8460 100644
@@ -96,6 +96,8 @@
 #define AT_FDCWD		-100    /* Special value used to indicate
                                            openat should use the current
                                            working directory. */
+#define AT_XATTR_CREATE	        0x1	/* setxattrat(2): set value, fail if attr already exists */
+#define AT_XATTR_REPLACE	0x2	/* setxattrat(2): set value, fail if attr does not exist */
 #define AT_SYMLINK_NOFOLLOW	0x100   /* Do not follow symbolic links.  */
 #define AT_EACCESS		0x200	/* Test access permitted for
                                            effective IDs, not real IDs.  */
Signature · 1 line
2.40.1
maintainer Acked-by on v2 1/1
#1 lore raw

Hi Christian,

Quoted 13 lines · Christian Göttsche

Add the four syscalls setxattrat(), getxattrat(), listxattrat() and removexattrat(). Those can be used to operate on extended attributes, especially security related ones, either relative to a pinned directory or on a file descriptor without read access, avoiding a /proc/<pid>/fd/<fd> detour, requiring a mounted procfs.

One use case will be setfiles(8) setting SELinux file contexts ("security.selinux") without race conditions.

Add XATTR flags to the private namespace of AT_* flags.

Use the do_{name}at() pattern from fs/open.c.

Quoted 8 lines

Use a single flag parameter for extended attribute flags (currently XATTR_CREATE and XATTR_REPLACE) and *at() flags to not exceed six syscall arguments in setxattrat().

Previous approach ("f*xattr: allow O_PATH descriptors"): https://lore.kernel.org/all/[email protected]/
v1 discussion: https://lore.kernel.org/all/[email protected]/

Thanks for your patch!

The syscall numbers conflict with those used in "[PATCH] cachestat:
wire up cachestat for other architectures", so this needs some synchronization.
https://lore.kernel.org/linux-sh/[email protected]

Quoted 1 line
 arch/m68k/kernel/syscalls/syscall.tbl       |   4 +

For m68k:

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#2 lore raw
In reply to Christian Göttsche
Quoted 39 lines · Christian Göttsche

Add the four syscalls setxattrat(), getxattrat(), listxattrat() and removexattrat(). Those can be used to operate on extended attributes, especially security related ones, either relative to a pinned directory or on a file descriptor without read access, avoiding a /proc/<pid>/fd/<fd> detour, requiring a mounted procfs.

One use case will be setfiles(8) setting SELinux file contexts ("security.selinux") without race conditions.

Add XATTR flags to the private namespace of AT_* flags.

Use the do_{name}at() pattern from fs/open.c.

Use a single flag parameter for extended attribute flags (currently XATTR_CREATE and XATTR_REPLACE) and *at() flags to not exceed six syscall arguments in setxattrat().

Previous approach ("f*xattr: allow O_PATH descriptors"): https://lore.kernel.org/all/[email protected]/
v1 discussion: https://lore.kernel.org/all/[email protected]/

Quoted 1 line

---

Fwiw, your header doesn't let me see who the mail was directly sent to so I'm only able to reply to lists which is a bit pointless...

include/uapi/linux/fcntl.h line 100

Quoted 6 lines

v2:
- squash syscall introduction and wire up commits
- add AT_XATTR_CREATE and AT_XATTR_REPLACE constants

include/uapi/linux/fcntl.h +2 -0
+#define AT_XATTR_CREATE	        0x1	/* setxattrat(2): set value, fail if attr already exists */
+#define AT_XATTR_REPLACE	0x2	/* setxattrat(2): set value, fail if attr does not exist */

We really shouldn't waste any AT_* flags for this. Otherwise we'll run out of them rather quickly. Two weeks ago we added another AT_* flag which is up for merging for v6.5 iirc and I've glimpsed another AT_* flag proposal in one of the talks at last weeks Vancouver conference extravaganza.

Even if we reuse 0x200 for AT_XATTR_CREATE (like we did for AT_EACCESS and AT_REMOVEDIR) we still need another bit for AT_XATTR_REPLACE.

Plus, this is really ugly since AT_XATTR_{CREATE,REPLACE} really isn't in any way related to lookup and we're mixing it in with lookup modifying flags.

So my proposal for {g,s}etxattrat() would be:

struct xattr_args {
        __aligned_u64 value;
        __u32 size;
        __u32 cmd;
};

So everything's nicely 64bit aligned in the struct. Use the @cmd member to set either XATTR_REPLACE or XATTR_CREATE and treat it as a proper enum and not as a flag argument like the old calls did.

So then we'd have:

setxattrat(int dfd, const char *path, const char __user *name,
struct xattr_args __user *args, size_t size, unsigned int flags)
getxattrat(int dfd, const char *path, const char __user *name,
struct xattr_args __user *args, size_t size, unsigned int flags)

The current in-kernel struct xattr_ctx would be renamed to struct kernel_xattr_args and then we do the usual copy_struct_from_user() dance:

struct xattr_args args;
err = copy_struct_from_user(&args, sizeof(args), uargs, usize);

and then go on to handle value/size for setxattrat()/getxattrat() accordingly.

getxattr()/setxattr() aren't meaningfully filterable by seccomp already so there's not point in not using a struct.

If that isn't very appealing then another option is to add a new flag namespace just for setxattrat() similar to fspick() and move_mount() duplicating the needed lookup modifying flags.
Thoughts?

#3 lore raw
Quoted 95 lines · Christian Brauner
Quoted 39 lines · Christian Göttsche

Add the four syscalls setxattrat(), getxattrat(), listxattrat() and removexattrat(). Those can be used to operate on extended attributes, especially security related ones, either relative to a pinned directory or on a file descriptor without read access, avoiding a /proc/<pid>/fd/<fd> detour, requiring a mounted procfs.

One use case will be setfiles(8) setting SELinux file contexts ("security.selinux") without race conditions.

Add XATTR flags to the private namespace of AT_* flags.

Use the do_{name}at() pattern from fs/open.c.

Use a single flag parameter for extended attribute flags (currently XATTR_CREATE and XATTR_REPLACE) and *at() flags to not exceed six syscall arguments in setxattrat().

Previous approach ("f*xattr: allow O_PATH descriptors"): https://lore.kernel.org/all/[email protected]/
v1 discussion: https://lore.kernel.org/all/[email protected]/

Quoted 1 line

---

Fwiw, your header doesn't let me see who the mail was directly sent to so I'm only able to reply to lists which is a bit pointless...

include/uapi/linux/fcntl.h line 100

Quoted 6 lines

v2:
- squash syscall introduction and wire up commits
- add AT_XATTR_CREATE and AT_XATTR_REPLACE constants

include/uapi/linux/fcntl.h +2 -0
+#define AT_XATTR_CREATE              0x1     /* setxattrat(2): set value, fail if attr already exists */
+#define AT_XATTR_REPLACE     0x2     /* setxattrat(2): set value, fail if attr does not exist */

We really shouldn't waste any AT_* flags for this. Otherwise we'll run out of them rather quickly. Two weeks ago we added another AT_* flag which is up for merging for v6.5 iirc and I've glimpsed another AT_* flag proposal in one of the talks at last weeks Vancouver conference extravaganza.

Even if we reuse 0x200 for AT_XATTR_CREATE (like we did for AT_EACCESS and AT_REMOVEDIR) we still need another bit for AT_XATTR_REPLACE.

Plus, this is really ugly since AT_XATTR_{CREATE,REPLACE} really isn't in any way related to lookup and we're mixing it in with lookup modifying flags.

So my proposal for {g,s}etxattrat() would be:

struct xattr_args {
        __aligned_u64 value;
        __u32 size;
        __u32 cmd;
};

So everything's nicely 64bit aligned in the struct. Use the @cmd member to set either XATTR_REPLACE or XATTR_CREATE and treat it as a proper enum and not as a flag argument like the old calls did.

So then we'd have:

setxattrat(int dfd, const char *path, const char __user *name,
struct xattr_args __user *args, size_t size, unsigned int flags)
getxattrat(int dfd, const char *path, const char __user *name,
struct xattr_args __user *args, size_t size, unsigned int flags)

The current in-kernel struct xattr_ctx would be renamed to struct kernel_xattr_args and then we do the usual copy_struct_from_user() dance:

struct xattr_args args;
err = copy_struct_from_user(&args, sizeof(args), uargs, usize);

and then go on to handle value/size for setxattrat()/getxattrat() accordingly.

Quoted 7 lines

getxattr()/setxattr() aren't meaningfully filterable by seccomp already so there's not point in not using a struct.

If that isn't very appealing then another option is to add a new flag namespace just for setxattrat() similar to fspick() and move_mount() duplicating the needed lookup modifying flags.
Thoughts?

Here is a thought: I am not sure if I am sorry we did not discuss this API issue in LSFMM or happy that we did not waste our time on this... :-/

I must say that I dislike redefined flag namespace like FSPICK_* just as much as I dislike overloading the AT_* namespace and TBH, I am not crazy about avoiding this problem with xattr_args either.

A more sane solution IMO could have been:
- Use lower word of flags for generic AT_ flags
- Use the upper word of flags for syscall specific flags

So if it were up to me, I would vote starting this practice:

+ /* Start of syscall specific range */
+ #define AT_XATTR_CREATE 0x10000 /* setxattrat(2): set
value, fail if attr already exists */
+ #define AT_XATTR_REPLACE 0x20000 /* setxattrat(2): set
value, fail if attr does not exist */

Which coincidentally happens to be inline with my AT_HANDLE_FID patch...

Sure, we will have some special cases like MOVE_MOUNT_* and legacy pollution to the lower AT_ flags word, but as a generic solution for syscalls that need the common AT_ lookup flags and just a few private flags, that seems like the lesser evil to me.

Thanks,
Amir.

#4 lore raw
Quoted 107 lines · Amir Goldstein
Quoted 95 lines · Christian Brauner
Quoted 39 lines · Christian Göttsche

Add the four syscalls setxattrat(), getxattrat(), listxattrat() and removexattrat(). Those can be used to operate on extended attributes, especially security related ones, either relative to a pinned directory or on a file descriptor without read access, avoiding a /proc/<pid>/fd/<fd> detour, requiring a mounted procfs.

One use case will be setfiles(8) setting SELinux file contexts ("security.selinux") without race conditions.

Add XATTR flags to the private namespace of AT_* flags.

Use the do_{name}at() pattern from fs/open.c.

Use a single flag parameter for extended attribute flags (currently XATTR_CREATE and XATTR_REPLACE) and *at() flags to not exceed six syscall arguments in setxattrat().

Previous approach ("f*xattr: allow O_PATH descriptors"): https://lore.kernel.org/all/20220607153139.35588-1-cgzones-gM/[email protected]/
v1 discussion: https://lore.kernel.org/all/20220830152858.14866-2-cgzones-gM/[email protected]/

Quoted 1 line

---

Fwiw, your header doesn't let me see who the mail was directly sent to so I'm only able to reply to lists which is a bit pointless...

include/uapi/linux/fcntl.h line 100

Quoted 6 lines

v2:
- squash syscall introduction and wire up commits
- add AT_XATTR_CREATE and AT_XATTR_REPLACE constants

include/uapi/linux/fcntl.h +2 -0
+#define AT_XATTR_CREATE              0x1     /* setxattrat(2): set value, fail if attr already exists */
+#define AT_XATTR_REPLACE     0x2     /* setxattrat(2): set value, fail if attr does not exist */

We really shouldn't waste any AT_* flags for this. Otherwise we'll run out of them rather quickly. Two weeks ago we added another AT_* flag which is up for merging for v6.5 iirc and I've glimpsed another AT_* flag proposal in one of the talks at last weeks Vancouver conference extravaganza.

Even if we reuse 0x200 for AT_XATTR_CREATE (like we did for AT_EACCESS and AT_REMOVEDIR) we still need another bit for AT_XATTR_REPLACE.

Plus, this is really ugly since AT_XATTR_{CREATE,REPLACE} really isn't in any way related to lookup and we're mixing it in with lookup modifying flags.

So my proposal for {g,s}etxattrat() would be:

struct xattr_args {
        __aligned_u64 value;
        __u32 size;
        __u32 cmd;
};

So everything's nicely 64bit aligned in the struct. Use the @cmd member to set either XATTR_REPLACE or XATTR_CREATE and treat it as a proper enum and not as a flag argument like the old calls did.

So then we'd have:

setxattrat(int dfd, const char *path, const char __user *name,
struct xattr_args __user *args, size_t size, unsigned int flags)
getxattrat(int dfd, const char *path, const char __user *name,
struct xattr_args __user *args, size_t size, unsigned int flags)

The current in-kernel struct xattr_ctx would be renamed to struct kernel_xattr_args and then we do the usual copy_struct_from_user() dance:

struct xattr_args args;
err = copy_struct_from_user(&args, sizeof(args), uargs, usize);

and then go on to handle value/size for setxattrat()/getxattrat() accordingly.

Quoted 7 lines

getxattr()/setxattr() aren't meaningfully filterable by seccomp already so there's not point in not using a struct.

If that isn't very appealing then another option is to add a new flag namespace just for setxattrat() similar to fspick() and move_mount() duplicating the needed lookup modifying flags.
Thoughts?

Here is a thought: I am not sure if I am sorry we did not discuss this API issue in LSFMM or happy that we did not waste our time on this... :-/

Quoted 7 lines

I must say that I dislike redefined flag namespace like FSPICK_* just as much as I dislike overloading the AT_* namespace and TBH, I am not crazy about avoiding this problem with xattr_args either.

A more sane solution IMO could have been:
- Use lower word of flags for generic AT_ flags
- Use the upper word of flags for syscall specific flags

We'd have 16 lower bits for AT_* flags and upper 16 bits for non-AT_* flags. That might be ok but it isn't great because if we ever extend AT_* flags into the upper 16 bits that are generally useful for all AT_* flag taking system calls we'd not be able to use them. And at the rate people keep suggesting new AT_* flags that issue might arise quicker than we might think.

And we really don't want 64 bit flag arguments because of 32 bit architectures as that gets really ugly to handle cleanly (Arnd has talked a lot about issues in this area before).

Quoted 10 lines

So if it were up to me, I would vote starting this practice:

+ /* Start of syscall specific range */
+ #define AT_XATTR_CREATE 0x10000 /* setxattrat(2): set
value, fail if attr already exists */
+ #define AT_XATTR_REPLACE 0x20000 /* setxattrat(2): set
value, fail if attr does not exist */

Which coincidentally happens to be inline with my AT_HANDLE_FID patch...

This is different though. The reason AT_HANDLE_FID is acceptable is because we need the ability to extend an existing system call and we're reusing a bit that is already used in two other system calls. So we avoid adding a new system call just to add another flag argument and we're also not using up an additional AT_* bit. This makes it bearable imho. But here we're talking about new system calls where we can avoid this problem arising in the first place.

Quoted 5 lines

Sure, we will have some special cases like MOVE_MOUNT_* and legacy pollution to the lower AT_ flags word, but as a generic solution for syscalls that need the common AT_ lookup flags and just a few private flags, that seems like the lesser evil to me.

It is fine to do this in some cases but we shouldn't encourage mixing distinct flag namespaces let alone advertising this as a generic solution imho. The AT_XATTR_* flags aren't even flags they behave like an enum.

#5 lore raw
Quoted 153 lines · Christian Brauner
Quoted 107 lines · Amir Goldstein
Quoted 95 lines · Christian Brauner
Quoted 39 lines · Christian Göttsche

Add the four syscalls setxattrat(), getxattrat(), listxattrat() and removexattrat(). Those can be used to operate on extended attributes, especially security related ones, either relative to a pinned directory or on a file descriptor without read access, avoiding a /proc/<pid>/fd/<fd> detour, requiring a mounted procfs.

One use case will be setfiles(8) setting SELinux file contexts ("security.selinux") without race conditions.

Add XATTR flags to the private namespace of AT_* flags.

Use the do_{name}at() pattern from fs/open.c.

Use a single flag parameter for extended attribute flags (currently XATTR_CREATE and XATTR_REPLACE) and *at() flags to not exceed six syscall arguments in setxattrat().

Previous approach ("f*xattr: allow O_PATH descriptors"): https://lore.kernel.org/all/[email protected]/
v1 discussion: https://lore.kernel.org/all/[email protected]/

Quoted 1 line

---

Fwiw, your header doesn't let me see who the mail was directly sent to so I'm only able to reply to lists which is a bit pointless...

include/uapi/linux/fcntl.h line 100

Quoted 6 lines

v2:
- squash syscall introduction and wire up commits
- add AT_XATTR_CREATE and AT_XATTR_REPLACE constants

include/uapi/linux/fcntl.h +2 -0
+#define AT_XATTR_CREATE              0x1     /* setxattrat(2): set value, fail if attr already exists */
+#define AT_XATTR_REPLACE     0x2     /* setxattrat(2): set value, fail if attr does not exist */

We really shouldn't waste any AT_* flags for this. Otherwise we'll run out of them rather quickly. Two weeks ago we added another AT_* flag which is up for merging for v6.5 iirc and I've glimpsed another AT_* flag proposal in one of the talks at last weeks Vancouver conference extravaganza.

Even if we reuse 0x200 for AT_XATTR_CREATE (like we did for AT_EACCESS and AT_REMOVEDIR) we still need another bit for AT_XATTR_REPLACE.

Plus, this is really ugly since AT_XATTR_{CREATE,REPLACE} really isn't in any way related to lookup and we're mixing it in with lookup modifying flags.

So my proposal for {g,s}etxattrat() would be:

struct xattr_args {
        __aligned_u64 value;
        __u32 size;
        __u32 cmd;
};

So everything's nicely 64bit aligned in the struct. Use the @cmd member to set either XATTR_REPLACE or XATTR_CREATE and treat it as a proper enum and not as a flag argument like the old calls did.

So then we'd have:

setxattrat(int dfd, const char *path, const char __user *name,
struct xattr_args __user *args, size_t size, unsigned int flags)
getxattrat(int dfd, const char *path, const char __user *name,
struct xattr_args __user *args, size_t size, unsigned int flags)

The current in-kernel struct xattr_ctx would be renamed to struct kernel_xattr_args and then we do the usual copy_struct_from_user() dance:

struct xattr_args args;
err = copy_struct_from_user(&args, sizeof(args), uargs, usize);

and then go on to handle value/size for setxattrat()/getxattrat() accordingly.

Quoted 7 lines

getxattr()/setxattr() aren't meaningfully filterable by seccomp already so there's not point in not using a struct.

If that isn't very appealing then another option is to add a new flag namespace just for setxattrat() similar to fspick() and move_mount() duplicating the needed lookup modifying flags.
Thoughts?

Here is a thought: I am not sure if I am sorry we did not discuss this API issue in LSFMM or happy that we did not waste our time on this... :-/

Quoted 7 lines

I must say that I dislike redefined flag namespace like FSPICK_* just as much as I dislike overloading the AT_* namespace and TBH, I am not crazy about avoiding this problem with xattr_args either.

A more sane solution IMO could have been:
- Use lower word of flags for generic AT_ flags
- Use the upper word of flags for syscall specific flags

We'd have 16 lower bits for AT_* flags and upper 16 bits for non-AT_* flags. That might be ok but it isn't great because if we ever extend AT_* flags into the upper 16 bits that are generally useful for all AT_* flag taking system calls we'd not be able to use them. And at the rate people keep suggesting new AT_* flags that issue might arise quicker than we might think.

And we really don't want 64 bit flag arguments because of 32 bit architectures as that gets really ugly to handle cleanly (Arnd has talked a lot about issues in this area before).

Quoted 10 lines

So if it were up to me, I would vote starting this practice:

+ /* Start of syscall specific range */
+ #define AT_XATTR_CREATE 0x10000 /* setxattrat(2): set
value, fail if attr already exists */
+ #define AT_XATTR_REPLACE 0x20000 /* setxattrat(2): set
value, fail if attr does not exist */

Which coincidentally happens to be inline with my AT_HANDLE_FID patch...

This is different though. The reason AT_HANDLE_FID is acceptable is because we need the ability to extend an existing system call and we're reusing a bit that is already used in two other system calls. So we avoid adding a new system call just to add another flag argument and we're also not using up an additional AT_* bit. This makes it bearable imho. But here we're talking about new system calls where we can avoid this problem arising in the first place.

Quoted 5 lines

Sure, we will have some special cases like MOVE_MOUNT_* and legacy pollution to the lower AT_ flags word, but as a generic solution for syscalls that need the common AT_ lookup flags and just a few private flags, that seems like the lesser evil to me.

Quoted 4 lines

It is fine to do this in some cases but we shouldn't encourage mixing distinct flag namespaces let alone advertising this as a generic solution imho. The AT_XATTR_* flags aren't even flags they behave like an enum.

OK. I see your point.
Also, wrt struct xattr_args, there is sort of a precedent with XFS_IOC_ATTRMULTI_BY_HANDLE ioctl, struct xfs_attr_multiop and flags XFS_IOC_ATTR_{CREATE,REPLACE}.

Just a nit, I would use xattr_args field names that are the same as setxattr() arg names, so s/cmd/flags.

Thanks,
Amir.