pidfs: support extended attributes
Christian Brauner
maintainercover pidfs: support extended attributes
With the incoming support for permanent pidfs dentries we can start supporting extended attributes on pidfds. This will allow to attach meta information to tasks. This currently adds support for trusted extended attributes which is a first natural target.
One natural extension would be to introduce a custom pidfs.* extended attribute space and allow for the inheritance of extended attributes across fork() and exec().
The first simple scheme will allow privileged userspace to slap tags onto pidfds which is useful for e.g., service managers such as systemd.
---
Christian Brauner (7):
libfs: prepare to allow for non-immutable pidfd inodes
pidfs: make inodes mutable
pidfs: raise SB_I_NODEV and SB_I_NOEXEC
pidfs: support xattrs on pidfds selftests/pidfd: test extended attribute support selftests/pidfd: test extended attribute support selftests/pidfd: test setattr support
fs/libfs.c | 1 - fs/pidfs.c | 98 ++++++++++++++- tools/testing/selftests/pidfd/.gitignore | 2 + tools/testing/selftests/pidfd/Makefile | 3 +- tools/testing/selftests/pidfd/pidfd_setattr_test.c | 69 +++++++++++ tools/testing/selftests/pidfd/pidfd_xattr_test.c | 132 +++++++++++++++++++++ 6 files changed, 301 insertions(+), 4 deletions(-)
---
base-commit: ac79f1c5b96d8d2f39b3eed1084588160198f6d3
change-id: 20250617-work-pidfs-xattr-1111246fe9b2
Christian Brauner
maintainerRFC PATCH 1/7 libfs: prepare to allow for non-immutable pidfd inodes
Allow for S_IMMUTABLE to be stripped so that we can support xattrs on pidfds.
--- fs/libfs.c | 1 - 1 file changed, 1 deletion(-)
fs/libfs.c +0 -1
@@ -2162,7 +2162,6 @@ static struct dentry *prepare_anon_dentry(struct dentry **stashed,/* Notice when this is changed. */ WARN_ON_ONCE (!S_ISREG (inode->i_mode)); -WARN_ON_ONCE (!IS_IMMUTABLE (inode)); dentry =d_alloc_anon (sb);if (!dentry) {
Signature · 1 line
2.47.2
Re: [PATCH RFC 1/7] libfs: prepare to allow for non-immutable pidfd inodes
Am Di., 17. Juni 2025 um 17:45 Uhr schrieb Christian Brauner <[email protected]>:
Quoted 5 lines
Allow for
S_IMMUTABLEto be stripped so that we can support xattrs on pidfds.Signed-off-by Christian Brauner <[email protected]>
Quoted 20 lines
--- fs/libfs.c | 1 - 1 file changed, 1 deletion(-)fs/libfs.c +0
-2@@ -2162,7 +2162,6 @@ static struct dentry *prepare_anon_dentry(struct dentry **stashed,/* Notice when this is changed. */ WARN_ON_ONCE (!S_ISREG (inode->i_mode)); -WARN_ON_ONCE (!IS_IMMUTABLE (inode)); dentry =d_alloc_anon (sb);if (!dentry) { --2.47.2
Christian Brauner
maintainerRFC PATCH 2/7 pidfs: make inodes mutable
In reply to Christian BraunerPrepare for allowing extended attributes to be set on pidfd inodes by allowing them to be mutable.
--- fs/pidfs.c | 2 ++ 1 file changed, 2 insertions(+)
fs/pidfs.c +2 -0
@@ -892,6 +892,8 @@ static int pidfs_init_inode(struct inode *inode, void *data) inode->i_private = data; inode->i_flags |=S_PRIVATE |S_ANON_INODE ; +/* We allow to set xattrs. */ + inode->i_flags &= ~S_IMMUTABLE ; inode->i_mode |=S_IRWXU ; inode->i_op = &pidfs_inode_operations; inode->i_fop = &pidfs_file_operations;
Signature · 1 line
2.47.2
Re: [PATCH RFC 2/7] pidfs: make inodes mutable
Am Di., 17. Juni 2025 um 17:45 Uhr schrieb Christian Brauner <[email protected]>:
Quoted 5 lines
Prepare for allowing extended attributes to be set on pidfd inodes by allowing them to be mutable.
Signed-off-by Christian Brauner <[email protected]>
Quoted 21 lines
--- fs/pidfs.c | 2 ++ 1 file changed, 2 insertions(+)fs/pidfs.c +2
-1@@ -892,6 +892,8 @@ static int pidfs_init_inode(struct inode *inode, void *data) inode->i_private = data; inode->i_flags |=S_PRIVATE |S_ANON_INODE ; +/* We allow to set xattrs. */ + inode->i_flags &= ~S_IMMUTABLE ; inode->i_mode |=S_IRWXU ; inode->i_op = &pidfs_inode_operations; inode->i_fop = &pidfs_file_operations; --2.47.2
Christian Brauner
maintainerRFC PATCH 3/7 pidfs: raise SB_I_NODEV and SB_I_NOEXEC
In reply to Christian BraunerSimilar to commit 1ed95281c0c7 ("anon_inode: raise SB_I_NODEV and SB_I_NOEXEC"):
it shouldn't be possible to execute pidfds via execveat(fd_anon_inode, "", NULL, NULL, AT_EMPTY_PATH)
so raise SB_I_NOEXEC so that no one gets any creative ideas.
Also raise SB_I_NODEV as we don't expect or support any devices on pidfs.
--- fs/pidfs.c | 2 ++ 1 file changed, 2 insertions(+)
fs/pidfs.c +2 -0
@@ -959,6 +959,8 @@ static int pidfs_init_fs_context(struct fs_context *fc)if (!ctx)return -ENOMEM ; + fc->s_iflags |=SB_I_NOEXEC ; + fc->s_iflags |=SB_I_NODEV ; ctx->ops = &pidfs_sops; ctx->eops = &pidfs_export_operations; ctx->dops = &pidfs_dentry_operations;
Signature · 1 line
2.47.2
Re: [PATCH RFC 3/7] pidfs: raise SB_I_NODEV and SB_I_NOEXEC
Am Di., 17. Juni 2025 um 17:45 Uhr schrieb Christian Brauner <[email protected]>:
Quoted 9 lines
Similar to commit
1ed95281c0c7("anon_inode: raiseSB_I_NODEVandSB_I_NOEXEC"):
it shouldn't be possible to execute pidfds viaexecveat(fd_anon_inode, "", NULL, NULL, AT_EMPTY_PATH)
so raiseSB_I_NOEXECso that no one gets any creative ideas.Also raise
SB_I_NODEVas we don't expect or support any devices on pidfs.Signed-off-by Christian Brauner <[email protected]>
Quoted 21 lines
--- fs/pidfs.c | 2 ++ 1 file changed, 2 insertions(+)fs/pidfs.c +2
-1@@ -959,6 +959,8 @@ static int pidfs_init_fs_context(struct fs_context *fc)if (!ctx)return -ENOMEM ; + fc->s_iflags |=SB_I_NOEXEC ; + fc->s_iflags |=SB_I_NODEV ; ctx->ops = &pidfs_sops; ctx->eops = &pidfs_export_operations; ctx->dops = &pidfs_dentry_operations; --2.47.2
Christian Brauner
maintainerRFC PATCH 4/7 pidfs: support xattrs on pidfds
In reply to Christian Brauner--- fs/pidfs.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 92 insertions(+), 2 deletions(-)
fs/pidfs.c +92 -2
@@ -21,6 +21,7 @@#include <linux/utsname.h> #include <net/net_namespace.h> #include <linux/coredump.h> +#include <linux/xattr.h> #include "internal.h" #include "mount.h"
@@ -40,6 +41,7 @@#define PIDFS_PID_DEAD ERR_PTR(-ESRCH)static struct kmem_cache *pidfs_cachep__ro_after_init ; +static struct kmem_cache *pidfs_attrs_cachep__ro_after_init ;/* * Stashes information that userspace needs to access even after the
@@ -51,9 +53,14 @@ struct pidfs_exit_info {__u32 coredump_mask; }; +struct pidfs_attrs { +struct simple_xattrs xattrs; +}; +struct pidfs_inode {struct pidfs_exit_info __pei;struct pidfs_exit_info *exit_info; +struct pidfs_attrs *attrs;struct inode vfs_inode; };
@@ -672,15 +679,34 @@ static int pidfs_getattr(struct mnt_idmap *idmap, const struct path *path,return anon_inode_getattr (idmap, path, stat, request_mask, query_flags); } +static ssize_t pidfs_listxattr (struct dentry *dentry,char *buf,size_t size) +{ +struct inode *inode =d_inode (dentry); +struct pidfs_attrs *attrs; + + attrs =READ_ONCE (pidfs_i (inode)->attrs); +if (!attrs) +return -ENODATA ; + +return simple_xattr_list (inode, &attrs->xattrs, buf, size); +} +static const struct inode_operations pidfs_inode_operations = { - .getattr = pidfs_getattr, - .setattr = pidfs_setattr, + .getattr = pidfs_getattr, + .setattr = pidfs_setattr, + .listxattr = pidfs_listxattr, };static void pidfs_evict_inode (struct inode *inode) {struct pid *pid = inode->i_private; +struct pidfs_attrs *attrs; + attrs =READ_ONCE (pidfs_i (inode)->attrs); +if (attrs) { +simple_xattrs_free (&attrs->xattrs,NULL ); +kmem_cache_free (pidfs_attrs_cachep, attrs); + }clear_inode (inode);put_pid (pid); }
@@ -695,6 +721,7 @@ static struct inode *pidfs_alloc_inode(struct super_block *sb)memset (&pi->__pei,0 ,sizeof (pi->__pei)); pi->exit_info =NULL ; + pi->attrs =NULL ;return &pi->vfs_inode; }
@@ -951,6 +978,63 @@ static const struct stashed_operations pidfs_stashed_ops = { .put_data = pidfs_put_data, }; +static int pidfs_xattr_get (const struct xattr_handler *handler, +struct dentry *unused,struct inode *inode, +const char *suffix,void *value,size_t size) +{ +const char *name; +struct pidfs_attrs *attrs; + + attrs =READ_ONCE (pidfs_i (inode)->attrs); +if (!attrs) +return -ENODATA ; + + name =xattr_full_name (handler, suffix); +return simple_xattr_get (&attrs->xattrs, name, value, size); +} + +static int pidfs_xattr_set (const struct xattr_handler *handler, +struct mnt_idmap *idmap,struct dentry *unused, +struct inode *inode,const char *suffix, +const void *value,size_t size,int flags) +{ +const char *name; +struct pidfs_attrs *attrs; +struct simple_xattr *old_xattr; + +/* Make sure we're the only one here. */ +WARN_ON_ONCE (!inode_is_locked (inode)); + + attrs =READ_ONCE (pidfs_i (inode)->attrs); +if (!attrs) { + attrs =kmem_cache_zalloc (pidfs_attrs_cachep,GFP_KERNEL ); +if (!attrs) +return -ENOMEM ; + +simple_xattrs_init (&attrs->xattrs); +smp_store_release (&pidfs_i (inode)->attrs, attrs); + } + + name =xattr_full_name (handler, suffix); + old_xattr =simple_xattr_set (&attrs->xattrs, name, value, size, flags); +if (IS_ERR (old_xattr)) +return PTR_ERR (old_xattr); + +simple_xattr_free (old_xattr); +return 0 ; +} + +static const struct xattr_handler pidfs_trusted_xattr_handler = { + .prefix =XATTR_TRUSTED_PREFIX , + .get = pidfs_xattr_get, + .set = pidfs_xattr_set, +}; + +static const struct xattr_handler *const pidfs_xattr_handlers[] = { + &pidfs_trusted_xattr_handler, +NULL +}; +static int pidfs_init_fs_context (struct fs_context *fc) {struct pseudo_fs_context *ctx;
@@ -964,6 +1048,7 @@ static int pidfs_init_fs_context(struct fs_context *fc) ctx->ops = &pidfs_sops; ctx->eops = &pidfs_export_operations; ctx->dops = &pidfs_dentry_operations; + ctx->xattr = pidfs_xattr_handlers; fc->s_fs_info = (void *)&pidfs_stashed_ops;return 0 ; }
@@ -1073,6 +1158,11 @@ void __init pidfs_init(void) (SLAB_HWCACHE_ALIGN |SLAB_RECLAIM_ACCOUNT |SLAB_ACCOUNT |SLAB_PANIC ), pidfs_inode_init_once); + + pidfs_attrs_cachep =kmem_cache_create ("pidfs_attrs_cache" , +sizeof (struct pidfs_attrs ),0 , +SLAB_PANIC ,NULL ); + pidfs_mnt =kern_mount (&pidfs_type);if (IS_ERR (pidfs_mnt))panic ("Failed to mount pidfs pseudo filesystem" );
Signature · 1 line
2.47.2
Re: [PATCH RFC 4/7] pidfs: support xattrs on pidfds
Am Di., 17. Juni 2025 um 17:45 Uhr schrieb Christian Brauner <[email protected]>:
Quoted 2 lines
Signed-off-by Christian Brauner <[email protected]>
Quoted 157 lines
--- fs/pidfs.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 92 insertions(+), 2 deletions(-)fs/pidfs.c +87
-2@@ -21,6 +21,7 @@#include <linux/utsname.h> #include <net/net_namespace.h> #include <linux/coredump.h> +#include <linux/xattr.h> #include "internal.h" #include "mount.h" @@ -40,6 +41,7 @@#define PIDFS_PID_DEAD ERR_PTR(-ESRCH)static struct kmem_cache *pidfs_cachep__ro_after_init ; +static struct kmem_cache *pidfs_attrs_cachep__ro_after_init ;/* * Stashes information that userspace needs to access even after the @@ -51,9 +53,14 @@ struct pidfs_exit_info {__u32 coredump_mask; }; +struct pidfs_attrs { +struct simple_xattrs xattrs; +}; +struct pidfs_inode {struct pidfs_exit_info __pei;struct pidfs_exit_info *exit_info; +struct pidfs_attrs *attrs;struct inode vfs_inode; };@@ -672,15 +679,34 @@ static int pidfs_getattr(struct mnt_idmap *idmap, const struct path *path,return anon_inode_getattr (idmap, path, stat, request_mask, query_flags); } +static ssize_t pidfs_listxattr (struct dentry *dentry,char *buf,size_t size) +{ +struct inode *inode =d_inode (dentry); +struct pidfs_attrs *attrs; + + attrs =READ_ONCE (pidfs_i (inode)->attrs); +if (!attrs) +return -ENODATA ; + +return simple_xattr_list (inode, &attrs->xattrs, buf, size); +} +static const struct inode_operations pidfs_inode_operations = { - .getattr = pidfs_getattr, - .setattr = pidfs_setattr, + .getattr = pidfs_getattr, + .setattr = pidfs_setattr, + .listxattr = pidfs_listxattr, };static void pidfs_evict_inode (struct inode *inode) {struct pid *pid = inode->i_private; +struct pidfs_attrs *attrs; + attrs =READ_ONCE (pidfs_i (inode)->attrs); +if (attrs) { +simple_xattrs_free (&attrs->xattrs,NULL ); +kmem_cache_free (pidfs_attrs_cachep, attrs); + }clear_inode (inode);put_pid (pid); }@@ -695,6 +721,7 @@ static struct inode *pidfs_alloc_inode(struct super_block *sb)memset (&pi->__pei,0 ,sizeof (pi->__pei)); pi->exit_info =NULL ; + pi->attrs =NULL ;return &pi->vfs_inode; }@@ -951,6 +978,63 @@ static const struct stashed_operations pidfs_stashed_ops = { .put_data = pidfs_put_data, }; +static int pidfs_xattr_get (const struct xattr_handler *handler, +struct dentry *unused,struct inode *inode, +const char *suffix,void *value,size_t size) +{ +const char *name; +struct pidfs_attrs *attrs; + + attrs =READ_ONCE (pidfs_i (inode)->attrs); +if (!attrs) +return -ENODATA ; + + name =xattr_full_name (handler, suffix); +return simple_xattr_get (&attrs->xattrs, name, value, size); +} + +static int pidfs_xattr_set (const struct xattr_handler *handler, +struct mnt_idmap *idmap,struct dentry *unused, +struct inode *inode,const char *suffix, +const void *value,size_t size,int flags) +{ +const char *name; +struct pidfs_attrs *attrs; +struct simple_xattr *old_xattr; + +/* Make sure we're the only one here. */ +WARN_ON_ONCE (!inode_is_locked (inode)); + + attrs =READ_ONCE (pidfs_i (inode)->attrs); +if (!attrs) { + attrs =kmem_cache_zalloc (pidfs_attrs_cachep,GFP_KERNEL ); +if (!attrs) +return -ENOMEM ; + +simple_xattrs_init (&attrs->xattrs); +smp_store_release (&pidfs_i (inode)->attrs, attrs); + } + + name =xattr_full_name (handler, suffix); + old_xattr =simple_xattr_set (&attrs->xattrs, name, value, size, flags); +if (IS_ERR (old_xattr)) +return PTR_ERR (old_xattr); + +simple_xattr_free (old_xattr); +return 0 ; +} + +static const struct xattr_handler pidfs_trusted_xattr_handler = { + .prefix =XATTR_TRUSTED_PREFIX , + .get = pidfs_xattr_get, + .set = pidfs_xattr_set, +}; + +static const struct xattr_handler *const pidfs_xattr_handlers[] = { + &pidfs_trusted_xattr_handler, +NULL +}; +static int pidfs_init_fs_context (struct fs_context *fc) {struct pseudo_fs_context *ctx;@@ -964,6 +1048,7 @@ static int pidfs_init_fs_context(struct fs_context *fc) ctx->ops = &pidfs_sops; ctx->eops = &pidfs_export_operations; ctx->dops = &pidfs_dentry_operations; + ctx->xattr = pidfs_xattr_handlers; fc->s_fs_info = (void *)&pidfs_stashed_ops;return 0 ; }
fs/pidfs.c void __init pidfs_init(void) · line 1164
Quoted 7 lines
fs/pidfs.c +4
-0@@ -1073,6 +1158,11 @@ void __init pidfs_init(void) (SLAB_HWCACHE_ALIGN |SLAB_RECLAIM_ACCOUNT |SLAB_ACCOUNT |SLAB_PANIC ), pidfs_inode_init_once); + + pidfs_attrs_cachep =kmem_cache_create ("pidfs_attrs_cache" , +sizeof (struct pidfs_attrs ),0 , +SLAB_PANIC ,NULL );
nit: WDYT about adding SLAB_ACCOUNT too?
Quoted 8 lines
fs/pidfs.c +1
-1+ pidfs_mnt =kern_mount (&pidfs_type);if (IS_ERR (pidfs_mnt))panic ("Failed to mount pidfs pseudo filesystem" ); --2.47.2
Christian Brauner
maintainerRFC PATCH 5/7 selftests/pidfd: test extended attribute support
In reply to Christian BraunerAdd tests for extended attribute support on pidfds.
--- tools/testing/selftests/pidfd/.gitignore | 1 + tools/testing/selftests/pidfd/Makefile | 3 +- tools/testing/selftests/pidfd/pidfd_xattr_test.c | 97 ++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-)
tools/testing/selftests/pidfd/.gitignore +1 -0
@@ -10,3 +10,4 @@ pidfd_file_handle_test pidfd_bind_mount pidfd_info_test pidfd_exec_helper +pidfd_xattr_test
tools/testing/selftests/pidfd/Makefile +2 -1
@@ -3,7 +3,8 @@ CFLAGS += -g $(KHDR_INCLUDES) -pthread -Wall TEST_GEN_PROGS := pidfd_test pidfd_fdinfo_test pidfd_open_test \ pidfd_poll_test pidfd_wait pidfd_getfd_test pidfd_setns_test \ - pidfd_file_handle_test pidfd_bind_mount pidfd_info_test + pidfd_file_handle_test pidfd_bind_mount pidfd_info_test \ + pidfd_xattr_test TEST_GEN_PROGS_EXTENDED := pidfd_exec_helper
tools/testing/selftests/pidfd/pidfd_xattr_test.c new file +97 -0
97 lines not on this page · raw message
Signature · 1 line
2.47.2
Christian Brauner
maintainerRFC PATCH 6/7 selftests/pidfd: test extended attribute support
In reply to Christian BraunerTest that extended attributes are permanent.
--- tools/testing/selftests/pidfd/pidfd_xattr_test.c | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+)
tools/testing/selftests/pidfd/pidfd_xattr_test.c +35 -0
@@ -94,4 +94,39 @@ TEST_F(pidfs_xattr, set_get_list_xattr_multiple) } } +TEST_F(pidfs_xattr, set_get_list_xattr_persistent) +{ +int ret; +char buf[32 ]; +char list[PATH_MAX ] = {}; + + ret =fsetxattr (self->child_pidfd,"trusted.persistent" ,"persistent value" ,strlen ("persistent value" ),0 ); +ASSERT_EQ (ret,0 ); + +memset (buf,0 ,sizeof (buf)); + ret =fgetxattr (self->child_pidfd,"trusted.persistent" , buf,sizeof (buf)); +ASSERT_EQ (ret,strlen ("persistent value" )); +ASSERT_EQ (strcmp (buf,"persistent value" ),0 ); + + ret =flistxattr (self->child_pidfd, list,sizeof (list)); +ASSERT_GT (ret,0 ); + ASSERT_EQ(strcmp(list, "trusted.persistent"), 0) + +ASSERT_EQ (close (self->child_pidfd),0 ); + self->child_pidfd = -EBADF ; +sleep (2 ); + + self->child_pidfd =sys_pidfd_open (self->child_pid,0 ); +ASSERT_GE (self->child_pidfd,0 ); + +memset (buf,0 ,sizeof (buf)); + ret =fgetxattr (self->child_pidfd,"trusted.persistent" , buf,sizeof (buf)); +ASSERT_EQ (ret,strlen ("persistent value" )); +ASSERT_EQ (strcmp (buf,"persistent value" ),0 ); + + ret =flistxattr (self->child_pidfd, list,sizeof (list)); +ASSERT_GT (ret,0 ); +ASSERT_EQ (strcmp (list,"trusted.persistent" ),0 ); +} +TEST_HARNESS_MAIN
Signature · 1 line
2.47.2
Christian Brauner
maintainerRFC PATCH 7/7 selftests/pidfd: test setattr support
In reply to Christian BraunerVerify that ->setattr() on a pidfd doens't work.
--- tools/testing/selftests/pidfd/.gitignore | 1 + tools/testing/selftests/pidfd/Makefile | 2 +- tools/testing/selftests/pidfd/pidfd_setattr_test.c | 69 ++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-)
tools/testing/selftests/pidfd/.gitignore +1 -0
@@ -11,3 +11,4 @@ pidfd_bind_mount pidfd_info_test pidfd_exec_helper pidfd_xattr_test +pidfd_setattr_test
tools/testing/selftests/pidfd/Makefile +1 -1
@@ -4,7 +4,7 @@ CFLAGS += -g $(KHDR_INCLUDES) -pthread -Wall TEST_GEN_PROGS := pidfd_test pidfd_fdinfo_test pidfd_open_test \ pidfd_poll_test pidfd_wait pidfd_getfd_test pidfd_setns_test \ pidfd_file_handle_test pidfd_bind_mount pidfd_info_test \ - pidfd_xattr_test + pidfd_xattr_test pidfd_setattr_test TEST_GEN_PROGS_EXTENDED := pidfd_exec_helper
tools/testing/selftests/pidfd/pidfd_setattr_test.c new file +69 -0
69 lines not on this page · raw message
Signature · 1 line
2.47.2