* [PATCH RFC v4 0/2] pidfs: make the {g,u}id the owner of the inode
@ 2026-02-25 23:22 Christian Brauner
2026-02-25 23:22 ` [PATCH RFC v4 1/2] pidfs: add inode ownership and permission checks Christian Brauner
2026-02-25 23:22 ` [PATCH RFC v4 2/2] selftests/pidfd: add inode ownership and permission tests Christian Brauner
0 siblings, 2 replies; 3+ messages in thread
From: Christian Brauner @ 2026-02-25 23:22 UTC (permalink / raw)
To: linux-fsdevel, Jann Horn
Cc: Kees Cook, Andy Lutomirski, Alexander Viro, Jan Kara,
linux-kernel, Christian Brauner
This adds inode ownership and permission checking to pidfs.
Right now pidfs only supports trusted.* xattrs which require
CAP_SYS_ADMIN so there was never a need for real permission checking.
In order to support user.* xattrs and custom pidfs.* xattrs in the
future we need a permission model for pidfs inodes.
The {u,g}id of the target task becomes the owner of the pidfs inode.
Ownership is reported dynamically via getattr since credentials may
change due to setresuid() and similar operations. For kernel threads the
owner is root, for exited tasks the credentials saved at exit time via
pidfs_exit() are used.
The permission callback updates the inode ownership via
pidfs_update_owner() and then performs standard POSIX permission
checking via generic_permission().
This is intentionally less strict than ptrace_may_access() because pidfs
currently does not allow operating on data that is completely private to
the process such as its mm or file descriptors. Additional checks can be
layered on once that changes.
The second patch adds selftests covering ownership reporting via fstat
and the permission model via user.* xattr operations which trigger
pidfs_permission() through xattr_permission(). The tests exercise live
credential changes, exited tasks with saved exit credentials, same-user
cross-process access, and cross-user denial.
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
Changes in v4:
- Switch from euid/egid to uid/gid for inode ownership per Jann Horn's
feedback. Use cred->uid/cred->gid instead of cred->euid/cred->egid.
- Simplify pidfs_permission() to just call pidfs_update_owner() followed
by generic_permission() instead of open-coding credential checks.
- Drop the may_signal_creds() helper and the kill_ok_by_cred() changes
that were part of v3's two-step permission model.
- Fix bitmask enum values: use BIT(N) instead of plain integers since
attr_mask is now an atomic_t using atomic_or()/atomic_read() instead
of set_bit()/test_bit().
- Make PIDFS_ATTR_BIT_KTHREAD conditional on PF_KTHREAD in pidfs_exit()
instead of unconditionally setting it for all exiting tasks.
- Remove unused kuid_t/kgid_t variables from pidfs_update_owner().
- Link to v3: https://patch.msgid.link/20260223-work-pidfs-inode-owner-v3-0-490855c59999@kernel.org
Changes in v3:
- Simplify pidfs_fill_owner() into pidfs_update_owner() writing directly
to the inode via WRITE_ONCE() instead of using output parameters.
- Drop the separate pidfs_update_inode() helper and the
security_task_to_inode() call.
- Update pidfs_getattr() to write ownership to the inode via
pidfs_update_owner() instead of writing directly to stat.
- Update pidfs_permission() to also write ownership to the inode before
calling generic_permission(), handling kernel threads with -EPERM.
- Drop VFS_WARN_ON_ONCE() for idmap check from pidfs_permission().
- Link to v2: https://patch.msgid.link/20260217-work-pidfs-inode-owner-v2-1-f04b5638315a@kernel.org
Changes in v2:
- Fix an obvious null-deref during PIDFD_STALE (CLONE_PIDFD).
- Link to v1: https://patch.msgid.link/20260216-work-pidfs-inode-owner-v1-1-f8faa6b73983@kernel.org
---
Christian Brauner (2):
pidfs: add inode ownership and permission checks
selftests/pidfd: add inode ownership and permission tests
fs/pidfs.c | 110 ++++++--
tools/testing/selftests/pidfd/.gitignore | 1 +
tools/testing/selftests/pidfd/Makefile | 2 +-
.../selftests/pidfd/pidfd_inode_owner_test.c | 314 +++++++++++++++++++++
4 files changed, 409 insertions(+), 18 deletions(-)
---
base-commit: 6de23f81a5e08be8fbf5e8d7e9febc72a5b5f27f
change-id: 20260211-work-pidfs-inode-owner-0ca20de9ef23
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH RFC v4 1/2] pidfs: add inode ownership and permission checks
2026-02-25 23:22 [PATCH RFC v4 0/2] pidfs: make the {g,u}id the owner of the inode Christian Brauner
@ 2026-02-25 23:22 ` Christian Brauner
2026-02-25 23:22 ` [PATCH RFC v4 2/2] selftests/pidfd: add inode ownership and permission tests Christian Brauner
1 sibling, 0 replies; 3+ messages in thread
From: Christian Brauner @ 2026-02-25 23:22 UTC (permalink / raw)
To: linux-fsdevel, Jann Horn
Cc: Kees Cook, Andy Lutomirski, Alexander Viro, Jan Kara,
linux-kernel, Christian Brauner
Right now we only support trusted.* xattrs which require CAP_SYS_ADMIN
which doesn't really require any meaningful permission checking. But in
order to support user.* xattrs and custom pidfs.* xattrs in the future
we need permission checking for pidfs inodes. Add baseline permission
checking that can later be extended with additional write-time checks
for specific pidfs.* xattrs.
Make the {u,g}id of the task the owner of the pidfs inode. The ownership
is set when the dentry is first stashed and reported dynamically via
getattr since credentials may change due to setuid() and similar
operations. For kernel threads use root, for exited tasks use the
credentials saved at exit time.
The inode's ownership is dynamically updated via pidfs_update_owner()
which is called from the getattr() and permission() callbacks. It writes
the uid/gid directly to the inode via WRITE_ONCE(). This doesn't
serialize against inode->i_op->setattr() but since pidfs rejects
setattr() this isn't currently an issue. A seqcount-based approach can
be used if setattr() support is added in the future [1].
Save the task's credentials and thread group pid inode number at exit
time so that ownership and permission checks remain functional after the
task has been reaped.
The permission callback updates the inode's ownership via
pidfs_update_owner() and then performs standard POSIX permission checking
via generic_permission() against the inode's ownership and mode bits
(S_IRWXU / 0700).
This is intentionally less strict than ptrace_may_access() because pidfs
currently does not allow operating on data that is completely private to
the process such as its mm or file descriptors. Additional checks will
be needed once that changes.
Link: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git/log/?h=work.inode.seqcount [1]
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
fs/pidfs.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++----------
1 file changed, 93 insertions(+), 17 deletions(-)
diff --git a/fs/pidfs.c b/fs/pidfs.c
index 318253344b5c..4f480a814c5a 100644
--- a/fs/pidfs.c
+++ b/fs/pidfs.c
@@ -42,21 +42,30 @@ void pidfs_get_root(struct path *path)
}
enum pidfs_attr_mask_bits {
- PIDFS_ATTR_BIT_EXIT = 0,
- PIDFS_ATTR_BIT_COREDUMP = 1,
+ PIDFS_ATTR_BIT_EXIT = (1U << 0),
+ PIDFS_ATTR_BIT_COREDUMP = (1U << 1),
+ PIDFS_ATTR_BIT_KTHREAD = (1U << 2),
};
-struct pidfs_attr {
- unsigned long attr_mask;
- struct simple_xattrs *xattrs;
- struct /* exit info */ {
- __u64 cgroupid;
- __s32 exit_code;
- };
+struct pidfs_exit_attr {
+ __u64 cgroupid;
+ __s32 exit_code;
+ const struct cred *exit_cred;
+ u64 exit_tgid_ino;
+};
+
+struct pidfs_coredump_attr {
__u32 coredump_mask;
__u32 coredump_signal;
};
+struct pidfs_attr {
+ atomic_t attr_mask;
+ struct simple_xattrs *xattrs;
+ struct pidfs_exit_attr;
+ struct pidfs_coredump_attr;
+};
+
static struct rhashtable pidfs_ino_ht;
static const struct rhashtable_params pidfs_ino_ht_params = {
@@ -200,6 +209,7 @@ void pidfs_free_pid(struct pid *pid)
if (IS_ERR(attr))
return;
+ put_cred(attr->exit_cred);
xattrs = no_free_ptr(attr->xattrs);
if (xattrs)
simple_xattrs_free(xattrs, NULL);
@@ -364,7 +374,7 @@ static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg)
attr = READ_ONCE(pid->attr);
if (mask & PIDFD_INFO_EXIT) {
- if (test_bit(PIDFS_ATTR_BIT_EXIT, &attr->attr_mask)) {
+ if (atomic_read(&attr->attr_mask) & PIDFS_ATTR_BIT_EXIT) {
smp_rmb();
kinfo.mask |= PIDFD_INFO_EXIT;
#ifdef CONFIG_CGROUPS
@@ -376,7 +386,7 @@ static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg)
}
if (mask & PIDFD_INFO_COREDUMP) {
- if (test_bit(PIDFS_ATTR_BIT_COREDUMP, &attr->attr_mask)) {
+ if (atomic_read(&attr->attr_mask) & PIDFS_ATTR_BIT_COREDUMP) {
smp_rmb();
kinfo.mask |= PIDFD_INFO_COREDUMP | PIDFD_INFO_COREDUMP_SIGNAL;
kinfo.coredump_mask = attr->coredump_mask;
@@ -674,6 +684,7 @@ void pidfs_exit(struct task_struct *tsk)
{
struct pid *pid = task_pid(tsk);
struct pidfs_attr *attr;
+ unsigned int mask;
#ifdef CONFIG_CGROUPS
struct cgroup *cgrp;
#endif
@@ -703,17 +714,22 @@ void pidfs_exit(struct task_struct *tsk)
* is put
*/
-#ifdef CONFIG_CGROUPS
rcu_read_lock();
+#ifdef CONFIG_CGROUPS
cgrp = task_dfl_cgroup(tsk);
attr->cgroupid = cgroup_id(cgrp);
- rcu_read_unlock();
#endif
+ attr->exit_cred = get_cred(__task_cred(tsk));
+ rcu_read_unlock();
+ attr->exit_tgid_ino = task_tgid(tsk)->ino;
attr->exit_code = tsk->exit_code;
/* Ensure that PIDFD_GET_INFO sees either all or nothing. */
smp_wmb();
- set_bit(PIDFS_ATTR_BIT_EXIT, &attr->attr_mask);
+ mask = PIDFS_ATTR_BIT_EXIT;
+ if (unlikely(tsk->flags & PF_KTHREAD))
+ mask |= PIDFS_ATTR_BIT_KTHREAD;
+ atomic_or(mask, &attr->attr_mask);
}
#ifdef CONFIG_COREDUMP
@@ -735,12 +751,49 @@ void pidfs_coredump(const struct coredump_params *cprm)
/* Expose the signal number that caused the coredump. */
attr->coredump_signal = cprm->siginfo->si_signo;
smp_wmb();
- set_bit(PIDFS_ATTR_BIT_COREDUMP, &attr->attr_mask);
+ atomic_or(PIDFS_ATTR_BIT_COREDUMP, &attr->attr_mask);
}
#endif
static struct vfsmount *pidfs_mnt __ro_after_init;
+static void pidfs_update_owner(struct inode *inode)
+{
+ struct pid *pid = inode->i_private;
+ struct task_struct *task;
+ struct pidfs_attr *attr;
+ const struct cred *cred;
+
+ VFS_WARN_ON_ONCE(!pid);
+
+ attr = READ_ONCE(pid->attr);
+ VFS_WARN_ON_ONCE(!attr);
+
+ if (unlikely(atomic_read(&attr->attr_mask) & PIDFS_ATTR_BIT_KTHREAD))
+ return;
+
+ guard(rcu)();
+ task = pid_task(pid, PIDTYPE_PID);
+ if (task) {
+ cred = __task_cred(task);
+ WRITE_ONCE(inode->i_uid, cred->uid);
+ WRITE_ONCE(inode->i_gid, cred->gid);
+ return;
+ }
+
+ /*
+ * During copy_process() with CLONE_PIDFD the task hasn't been
+ * attached to the pid yet so pid_task() returns NULL and
+ * there's no exit_cred as the task obviously hasn't exited. Use
+ * the parent's credentials.
+ */
+ cred = attr->exit_cred;
+ if (!cred)
+ cred = current_cred();
+ WRITE_ONCE(inode->i_uid, cred->uid);
+ WRITE_ONCE(inode->i_gid, cred->gid);
+}
+
/*
* The vfs falls back to simple_setattr() if i_op->setattr() isn't
* implemented. Let's reject it completely until we have a clean
@@ -756,6 +809,9 @@ static int pidfs_getattr(struct mnt_idmap *idmap, const struct path *path,
struct kstat *stat, u32 request_mask,
unsigned int query_flags)
{
+ struct inode *inode = d_inode(path->dentry);
+
+ pidfs_update_owner(inode);
return anon_inode_getattr(idmap, path, stat, request_mask, query_flags);
}
@@ -773,10 +829,24 @@ static ssize_t pidfs_listxattr(struct dentry *dentry, char *buf, size_t size)
return simple_xattr_list(inode, xattrs, buf, size);
}
+static int pidfs_permission(struct mnt_idmap *idmap, struct inode *inode,
+ int mask)
+{
+ struct pid *pid = inode->i_private;
+ struct pidfs_attr *attr = READ_ONCE(pid->attr);
+
+ if (unlikely(atomic_read(&attr->attr_mask) & PIDFS_ATTR_BIT_KTHREAD))
+ return -EPERM;
+
+ pidfs_update_owner(inode);
+ return generic_permission(&nop_mnt_idmap, inode, mask);
+}
+
static const struct inode_operations pidfs_inode_operations = {
.getattr = pidfs_getattr,
.setattr = pidfs_setattr,
.listxattr = pidfs_listxattr,
+ .permission = pidfs_permission,
};
static void pidfs_evict_inode(struct inode *inode)
@@ -835,7 +905,7 @@ static struct pid *pidfs_ino_get_pid(u64 ino)
attr = READ_ONCE(pid->attr);
if (IS_ERR_OR_NULL(attr))
return NULL;
- if (test_bit(PIDFS_ATTR_BIT_EXIT, &attr->attr_mask))
+ if (atomic_read(&attr->attr_mask) & PIDFS_ATTR_BIT_EXIT)
return NULL;
/* Within our pid namespace hierarchy? */
if (pid_vnr(pid) == 0)
@@ -949,6 +1019,7 @@ static void pidfs_put_data(void *data)
int pidfs_register_pid(struct pid *pid)
{
struct pidfs_attr *new_attr __free(kfree) = NULL;
+ struct task_struct *task;
struct pidfs_attr *attr;
might_sleep();
@@ -975,6 +1046,9 @@ int pidfs_register_pid(struct pid *pid)
if (unlikely(attr))
return 0;
+ task = pid_task(pid, PIDTYPE_PID);
+ if (task && (task->flags & PF_KTHREAD))
+ atomic_or(PIDFS_ATTR_BIT_KTHREAD, &new_attr->attr_mask);
pid->attr = no_free_ptr(new_attr);
return 0;
}
@@ -983,7 +1057,8 @@ static struct dentry *pidfs_stash_dentry(struct dentry **stashed,
struct dentry *dentry)
{
int ret;
- struct pid *pid = d_inode(dentry)->i_private;
+ struct inode *inode = d_inode(dentry);
+ struct pid *pid = inode->i_private;
VFS_WARN_ON_ONCE(stashed != &pid->stashed);
@@ -991,6 +1066,7 @@ static struct dentry *pidfs_stash_dentry(struct dentry **stashed,
if (ret)
return ERR_PTR(ret);
+ pidfs_update_owner(inode);
return stash_dentry(stashed, dentry);
}
--
2.47.3
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH RFC v4 2/2] selftests/pidfd: add inode ownership and permission tests
2026-02-25 23:22 [PATCH RFC v4 0/2] pidfs: make the {g,u}id the owner of the inode Christian Brauner
2026-02-25 23:22 ` [PATCH RFC v4 1/2] pidfs: add inode ownership and permission checks Christian Brauner
@ 2026-02-25 23:22 ` Christian Brauner
1 sibling, 0 replies; 3+ messages in thread
From: Christian Brauner @ 2026-02-25 23:22 UTC (permalink / raw)
To: linux-fsdevel, Jann Horn
Cc: Kees Cook, Andy Lutomirski, Alexander Viro, Jan Kara,
linux-kernel, Christian Brauner
Test the pidfs inode ownership reporting (via fstat) and the permission
model (via user.* xattr operations that trigger pidfs_permission()):
Ownership tests:
- owner_self: own pidfd reports caller's uid/gid
- owner_child: child pidfd reports correct ownership
- owner_child_changed_uid: ownership tracks live credential changes
- owner_exited_child: ownership persists after exit and reap
- owner_exited_child_changed_uid: exit_cred preserves changed credentials
- owner_kthread: kernel thread pidfd reports root ownership
Permission tests:
- permission_same_user: same-user xattr access succeeds (EOPNOTSUPP)
- permission_different_user_denied: cross-user access denied (EACCES)
The user.* xattr namespace is used to exercise pidfs_permission() from
userspace: xattr_permission() calls inode_permission() for user.* on
S_IFREG inodes, so fgetxattr() returns EOPNOTSUPP when permission is
granted (no handler) and EACCES when denied.
Tests requiring root skip gracefully via SKIP().
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
tools/testing/selftests/pidfd/.gitignore | 1 +
tools/testing/selftests/pidfd/Makefile | 2 +-
.../selftests/pidfd/pidfd_inode_owner_test.c | 314 +++++++++++++++++++++
3 files changed, 316 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/pidfd/.gitignore b/tools/testing/selftests/pidfd/.gitignore
index 144e7ff65d6a..1981d39fe3dc 100644
--- a/tools/testing/selftests/pidfd/.gitignore
+++ b/tools/testing/selftests/pidfd/.gitignore
@@ -12,3 +12,4 @@ pidfd_info_test
pidfd_exec_helper
pidfd_xattr_test
pidfd_setattr_test
+pidfd_inode_owner_test
diff --git a/tools/testing/selftests/pidfd/Makefile b/tools/testing/selftests/pidfd/Makefile
index 764a8f9ecefa..904c9fd595c1 100644
--- a/tools/testing/selftests/pidfd/Makefile
+++ b/tools/testing/selftests/pidfd/Makefile
@@ -4,7 +4,7 @@ CFLAGS += -g $(KHDR_INCLUDES) $(TOOLS_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_setattr_test
+ pidfd_xattr_test pidfd_setattr_test pidfd_inode_owner_test
TEST_GEN_PROGS_EXTENDED := pidfd_exec_helper
diff --git a/tools/testing/selftests/pidfd/pidfd_inode_owner_test.c b/tools/testing/selftests/pidfd/pidfd_inode_owner_test.c
new file mode 100644
index 000000000000..0c15d0ccaafc
--- /dev/null
+++ b/tools/testing/selftests/pidfd/pidfd_inode_owner_test.c
@@ -0,0 +1,314 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <linux/types.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syscall.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <sys/xattr.h>
+#include <unistd.h>
+
+#include "pidfd.h"
+#include "kselftest_harness.h"
+
+FIXTURE(pidfs_inode_owner)
+{
+ pid_t child_pid;
+ int child_pidfd;
+};
+
+FIXTURE_SETUP(pidfs_inode_owner)
+{
+ int pipe_fds[2];
+ char buf;
+
+ self->child_pid = -1;
+ self->child_pidfd = -1;
+
+ ASSERT_EQ(pipe(pipe_fds), 0);
+
+ self->child_pid = create_child(&self->child_pidfd, 0);
+ ASSERT_GE(self->child_pid, 0);
+
+ if (self->child_pid == 0) {
+ close(pipe_fds[0]);
+ write_nointr(pipe_fds[1], "c", 1);
+ close(pipe_fds[1]);
+ pause();
+ _exit(EXIT_SUCCESS);
+ }
+
+ close(pipe_fds[1]);
+ ASSERT_EQ(read_nointr(pipe_fds[0], &buf, 1), 1);
+ close(pipe_fds[0]);
+}
+
+FIXTURE_TEARDOWN(pidfs_inode_owner)
+{
+ if (self->child_pid > 0) {
+ kill(self->child_pid, SIGKILL);
+ sys_waitid(P_PID, self->child_pid, NULL, WEXITED);
+ }
+ if (self->child_pidfd >= 0)
+ close(self->child_pidfd);
+}
+
+/* Own pidfd reports correct ownership. */
+TEST_F(pidfs_inode_owner, owner_self)
+{
+ int pidfd;
+ struct stat st;
+
+ pidfd = sys_pidfd_open(getpid(), 0);
+ ASSERT_GE(pidfd, 0);
+
+ ASSERT_EQ(fstat(pidfd, &st), 0);
+ EXPECT_EQ(st.st_uid, getuid());
+ EXPECT_EQ(st.st_gid, getgid());
+
+ close(pidfd);
+}
+
+/* Child pidfd reports correct ownership. */
+TEST_F(pidfs_inode_owner, owner_child)
+{
+ struct stat st;
+
+ ASSERT_EQ(fstat(self->child_pidfd, &st), 0);
+ EXPECT_EQ(st.st_uid, getuid());
+ EXPECT_EQ(st.st_gid, getgid());
+}
+
+/* Ownership tracks credential changes in a live task. */
+TEST_F(pidfs_inode_owner, owner_child_changed_uid)
+{
+ pid_t pid;
+ int pidfd, pipe_fds[2];
+ struct stat st;
+ char buf;
+
+ if (getuid() != 0)
+ SKIP(return, "Test requires root");
+
+ ASSERT_EQ(pipe(pipe_fds), 0);
+
+ pid = create_child(&pidfd, 0);
+ ASSERT_GE(pid, 0);
+
+ if (pid == 0) {
+ close(pipe_fds[0]);
+ if (setresgid(65534, 65534, 65534))
+ _exit(PIDFD_ERROR);
+ if (setresuid(65534, 65534, 65534))
+ _exit(PIDFD_ERROR);
+ write_nointr(pipe_fds[1], "c", 1);
+ close(pipe_fds[1]);
+ pause();
+ _exit(EXIT_SUCCESS);
+ }
+
+ close(pipe_fds[1]);
+ ASSERT_EQ(read_nointr(pipe_fds[0], &buf, 1), 1);
+ close(pipe_fds[0]);
+
+ ASSERT_EQ(fstat(pidfd, &st), 0);
+ EXPECT_EQ(st.st_uid, (uid_t)65534);
+ EXPECT_EQ(st.st_gid, (gid_t)65534);
+
+ kill(pid, SIGKILL);
+ sys_waitid(P_PID, pid, NULL, WEXITED);
+ close(pidfd);
+}
+
+/* Ownership persists after the child exits and is reaped. */
+TEST_F(pidfs_inode_owner, owner_exited_child)
+{
+ pid_t pid;
+ int pidfd;
+ struct stat st;
+
+ pid = create_child(&pidfd, 0);
+ ASSERT_GE(pid, 0);
+
+ if (pid == 0)
+ _exit(EXIT_SUCCESS);
+
+ ASSERT_EQ(sys_waitid(P_PID, pid, NULL, WEXITED), 0);
+
+ ASSERT_EQ(fstat(pidfd, &st), 0);
+ EXPECT_EQ(st.st_uid, getuid());
+ EXPECT_EQ(st.st_gid, getgid());
+
+ close(pidfd);
+}
+
+/* Exit credentials preserve changed credentials. */
+TEST_F(pidfs_inode_owner, owner_exited_child_changed_uid)
+{
+ pid_t pid;
+ int pidfd;
+ struct stat st;
+
+ if (getuid() != 0)
+ SKIP(return, "Test requires root");
+
+ pid = create_child(&pidfd, 0);
+ ASSERT_GE(pid, 0);
+
+ if (pid == 0) {
+ if (setresgid(65534, 65534, 65534))
+ _exit(PIDFD_ERROR);
+ if (setresuid(65534, 65534, 65534))
+ _exit(PIDFD_ERROR);
+ _exit(EXIT_SUCCESS);
+ }
+
+ ASSERT_EQ(sys_waitid(P_PID, pid, NULL, WEXITED), 0);
+
+ ASSERT_EQ(fstat(pidfd, &st), 0);
+ EXPECT_EQ(st.st_uid, (uid_t)65534);
+ EXPECT_EQ(st.st_gid, (gid_t)65534);
+
+ close(pidfd);
+}
+
+/* Same-user cross-process permission check succeeds. */
+TEST_F(pidfs_inode_owner, permission_same_user)
+{
+ pid_t pid;
+ int pidfd;
+ pid_t parent_pid = getpid();
+
+ pid = create_child(&pidfd, 0);
+ ASSERT_GE(pid, 0);
+
+ if (pid == 0) {
+ int fd;
+ char buf;
+
+ fd = sys_pidfd_open(parent_pid, 0);
+ if (fd < 0)
+ _exit(PIDFD_ERROR);
+
+ /*
+ * user.* xattr access triggers pidfs_permission().
+ * Same user's FSUID matches target's RUID, so
+ * generic_permission() passes and we get EOPNOTSUPP
+ * (no user.* xattr handler) instead of EACCES.
+ */
+ if (fgetxattr(fd, "user.test", &buf, sizeof(buf)) < 0 &&
+ errno == EOPNOTSUPP) {
+ close(fd);
+ _exit(PIDFD_PASS);
+ }
+
+ close(fd);
+ _exit(PIDFD_FAIL);
+ }
+
+ ASSERT_EQ(wait_for_pid(pid), PIDFD_PASS);
+ close(pidfd);
+}
+
+/* Cross-user access is denied when FSUID doesn't match target's RUID. */
+TEST_F(pidfs_inode_owner, permission_different_user_denied)
+{
+ pid_t pid;
+ int pidfd;
+
+ if (getuid() != 0)
+ SKIP(return, "Test requires root");
+
+ pid = create_child(&pidfd, 0);
+ ASSERT_GE(pid, 0);
+
+ if (pid == 0) {
+ int fd;
+ struct stat init_st;
+ char buf;
+
+ /* Open pidfd for init (uid 0). */
+ fd = sys_pidfd_open(1, 0);
+ if (fd < 0)
+ _exit(PIDFD_ERROR);
+
+ /* Verify init is actually uid 0 (may not be in all namespaces). */
+ if (fstat(fd, &init_st) || init_st.st_uid != 0) {
+ close(fd);
+ _exit(PIDFD_SKIP);
+ }
+
+ /* Drop to uid/gid 65534 and lose all capabilities. */
+ if (setresgid(65534, 65534, 65534))
+ _exit(PIDFD_ERROR);
+ if (setresuid(65534, 65534, 65534))
+ _exit(PIDFD_ERROR);
+
+ /*
+ * FSUID 65534 doesn't match target's RUID 0, and
+ * no CAP_DAC_OVERRIDE, so generic_permission()
+ * returns -EACCES.
+ */
+ if (fgetxattr(fd, "user.test", &buf, sizeof(buf)) < 0 &&
+ errno == EACCES) {
+ close(fd);
+ _exit(PIDFD_PASS);
+ }
+
+ close(fd);
+ _exit(PIDFD_FAIL);
+ }
+
+ {
+ int ret = wait_for_pid(pid);
+ if (ret == PIDFD_SKIP)
+ SKIP(goto out, "pid 1 is not uid 0 (not in init PID namespace?)");
+ ASSERT_EQ(ret, PIDFD_PASS);
+ }
+out:
+ close(pidfd);
+}
+
+/* Kernel thread pidfd reports root ownership. */
+TEST_F(pidfs_inode_owner, owner_kthread)
+{
+ int pidfd;
+ struct stat st;
+ char comm[16] = {};
+ FILE *f;
+
+ /*
+ * pid 2 is kthreadd only in the init PID namespace.
+ * Skip if we're in a different PID namespace.
+ */
+ f = fopen("/proc/2/comm", "r");
+ if (!f)
+ SKIP(return, "Cannot read /proc/2/comm");
+ if (!fgets(comm, sizeof(comm), f)) {
+ fclose(f);
+ SKIP(return, "Cannot read /proc/2/comm");
+ }
+ fclose(f);
+ comm[strcspn(comm, "\n")] = '\0';
+ if (strcmp(comm, "kthreadd") != 0)
+ SKIP(return, "pid 2 is not kthreadd (not in init PID namespace?)");
+
+ pidfd = sys_pidfd_open(2, 0);
+ ASSERT_GE(pidfd, 0);
+
+ ASSERT_EQ(fstat(pidfd, &st), 0);
+ EXPECT_EQ(st.st_uid, (uid_t)0);
+ EXPECT_EQ(st.st_gid, (gid_t)0);
+
+ close(pidfd);
+}
+
+TEST_HARNESS_MAIN
--
2.47.3
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-02-25 23:22 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-02-25 23:22 [PATCH RFC v4 0/2] pidfs: make the {g,u}id the owner of the inode Christian Brauner
2026-02-25 23:22 ` [PATCH RFC v4 1/2] pidfs: add inode ownership and permission checks Christian Brauner
2026-02-25 23:22 ` [PATCH RFC v4 2/2] selftests/pidfd: add inode ownership and permission tests Christian Brauner
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®