History

Merge tag 'vfs-6.17-rc1.pidfs' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs

Pull pidfs updates from Christian Brauner: - persistent info Persist exit and coredump information independent of whether anyone currently holds a pidfd for the struct pid. The current scheme allocated pidfs dentries on-demand repeatedly. This scheme is reaching it's limits as it makes it impossible to pin information that needs to be available after the task has exited or coredumped and that should not be lost simply because the pidfd got closed temporarily. The next opener should still see the stashed information. This is also a prerequisite for supporting extended attributes on pidfds to allow attaching meta information to them. If someone opens a pidfd for a struct pid a pidfs dentry is allocated and stashed in pid->stashed. Once the last pidfd for the struct pid is closed the pidfs dentry is released and removed from pid->stashed. So if 10 callers create a pidfs dentry for the same struct pid sequentially, i.e., each closing the pidfd before the other creates a new one then a new pidfs dentry is allocated every time. Because multiple tasks acquiring and releasing a pidfd for the same struct pid can race with each another a task may still find a valid pidfs entry from the previous task in pid->stashed and reuse it. Or it might find a dead dentry in there and fail to reuse it and so stashes a new pidfs dentry. Multiple tasks may race to stash a new pidfs dentry but only one will succeed, the other ones will put their dentry. The current scheme aims to ensure that a pidfs dentry for a struct pid can only be created if the task is still alive or if a pidfs dentry already existed before the task was reaped and so exit information has been was stashed in the pidfs inode. That's great except that it's buggy. If a pidfs dentry is stashed in pid->stashed after pidfs_exit() but before __unhash_process() is called we will return a pidfd for a reaped task without exit information being available. The pidfds_pid_valid() check does not guard against this race as it doens't sync at all with pidfs_exit(). The pid_has_task() check might be successful simply because we're before __unhash_process() but after pidfs_exit(). Introduce a new scheme where the lifetime of information associated with a pidfs entry (coredump and exit information) isn't bound to the lifetime of the pidfs inode but the struct pid itself. The first time a pidfs dentry is allocated for a struct pid a struct pidfs_attr will be allocated which will be used to store exit and coredump information. If all pidfs for the pidfs dentry are closed the dentry and inode can be cleaned up but the struct pidfs_attr will stick until the struct pid itself is freed. This will ensure minimal memory usage while persisting relevant information. The new scheme has various advantages. First, it allows to close the race where we end up handing out a pidfd for a reaped task for which no exit information is available. Second, it minimizes memory usage. Third, it allows to remove complex lifetime tracking via dentries when registering a struct pid with pidfs. There's no need to get or put a reference. Instead, the lifetime of exit and coredump information associated with a struct pid is bound to the lifetime of struct pid itself. - extended attributes Now that we have a way to persist information for pidfs dentries we can start supporting extended attributes on pidfds. This will allow userspace to attach meta information to tasks. 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 set trusted extended attributes on pidfs inodes. - Allow autonomous pidfs file handles Various filesystems such as pidfs and drm support opening file handles without having to require a file descriptor to identify the filesystem. The filesystem are global single instances and can be trivially identified solely on the information encoded in the file handle. This makes it possible to not have to keep or acquire a sentinal file descriptor just to pass it to open_by_handle_at() to identify the filesystem. That's especially useful when such sentinel file descriptor cannot or should not be acquired. For pidfs this means a file handle can function as full replacement for storing a pid in a file. Instead a file handle can be stored and reopened purely based on the file handle. Such autonomous file handles can be opened with or without specifying a a file descriptor. If no proper file descriptor is used the FD_PIDFS_ROOT sentinel must be passed. This allows us to define further special negative fd sentinels in the future. Userspace can trivially test for support by trying to open the file handle with an invalid file descriptor. - Allow pidfds for reaped tasks with SCM_PIDFD messages This is a logical continuation of the earlier work to create pidfds for reaped tasks through the SO_PEERPIDFD socket option merged in 923ea4d4482b ("Merge patch series "net, pidfs: enable handing out pidfds for reaped sk->sk_peer_pid""). - Two minor fixes: * Fold fs_struct->{lock,seq} into a seqlock * Don't bother with path_{get,put}() in unix_open_file() * tag 'vfs-6.17-rc1.pidfs' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs: (37 commits) don't bother with path_get()/path_put() in unix_open_file() fold fs_struct->{lock,seq} into a seqlock selftests: net: extend SCM_PIDFD test to cover stale pidfds af_unix: enable handing out pidfds for reaped tasks in SCM_PIDFD af_unix: stash pidfs dentry when needed af_unix/scm: fix whitespace errors af_unix: introduce and use scm_replace_pid() helper af_unix: introduce unix_skb_to_scm helper af_unix: rework unix_maybe_add_creds() to allow sleep selftests/pidfd: decode pidfd file handles withou having to specify an fd fhandle, pidfs: support open_by_handle_at() purely based on file handle uapi/fcntl: add FD_PIDFS_ROOT uapi/fcntl: add FD_INVALID fcntl/pidfd: redefine PIDFD_SELF_THREAD_GROUP uapi/fcntl: mark range as reserved fhandle: reflow get_path_anchor() pidfs: add pidfs_root_path() helper fhandle: rename to get_path_anchor() fhandle: hoist copy_from_user() above get_path_from_fd() fhandle: raise FILEID_IS_DIR in handle_type ...

Linus Torvalds committed 1 year ago

Commit 672dcda2 parents 7031769 1f531e326 files · +894 −381Browse files

Unified · Split

26 files
  1. fs/coredump.c +0 −5
  2. fs/d_path.c +4 −4
  3. fs/exec.c +2 −2
  4. fs/fhandle.c +30 −32
  5. fs/fs_struct.c +14 −22
  6. fs/internal.h +4 −0
  7. fs/libfs.c +22 −12
  8. fs/namei.c +4 −4
  9. fs/pidfs.c +253 −183
  10. include/linux/fs_struct.h +5 −6
  11. include/linux/pid.h +9 −5
  12. include/linux/pidfs.h +1 −2
  13. include/net/scm.h +2 −2
  14. include/uapi/linux/fcntl.h +18 −0
  15. include/uapi/linux/pidfd.h +0 −15
  16. kernel/fork.c +5 −5
  17. kernel/pid.c +1 −1
  18. net/core/scm.c +28 −4
  19. net/unix/af_unix.c +48 −30
  20. tools/testing/selftests/net/af_unix/scm_pidfd.c +173 −44
  21. tools/testing/selftests/pidfd/.gitignore +2 −0
  22. tools/testing/selftests/pidfd/Makefile +3 −2
  23. tools/testing/selftests/pidfd/pidfd.h +5 −1
  24. tools/testing/selftests/pidfd/pidfd_file_handle_test.c +60 −0
  25. tools/testing/selftests/pidfd/pidfd_setattr_test.c +69 −0
  26. tools/testing/selftests/pidfd/pidfd_xattr_test.c +132 −0
fs/coredump.c+0−5
@@ -710,11 +710,6 @@ static bool coredump_sock_connect(struct core_name *cn, struct coredump_params * · 709 unmodified lines
710710
711711 retval = kernel_connect(socket, (struct sockaddr *)(&addr), addr_len,
712712 O_NONBLOCK | SOCK_COREDUMP);
713− /*
714− * ... Make sure to only put our reference after connect() took
715− * its own reference keeping the pidfs entry alive ...
716− */
717− pidfs_put_pid(cprm->pid);
718713
719714 if (retval) {
720715 if (retval == -EAGAIN)
fs/d_path.c+4−4
@@ -241,9 +241,9 @@ static void get_fs_root_rcu(struct fs_struct *fs, struct path *root) · 240 unmodified lines
241241 unsigned seq;
242242
243243 do {
244− seq = read_seqcount_begin(&fs->seq);
244+ seq = read_seqbegin(&fs->seq);
245245 *root = fs->root;
246− } while (read_seqcount_retry(&fs->seq, seq));
246+ } while (read_seqretry(&fs->seq, seq));
247247 }
248248
249249 /**
@@ -385,10 +385,10 @@ static void get_fs_root_and_pwd_rcu(struct fs_struct *fs, struct path *root, · 135 unmodified lines
385385 unsigned seq;
386386
387387 do {
388− seq = read_seqcount_begin(&fs->seq);
388+ seq = read_seqbegin(&fs->seq);
389389 *root = fs->root;
390390 *pwd = fs->pwd;
391− } while (read_seqcount_retry(&fs->seq, seq));
391+ } while (read_seqretry(&fs->seq, seq));
392392 }
393393
394394 /*
fs/exec.c+2−2
@@ -1515,7 +1515,7 @@ static void check_unsafe_exec(struct linux_binprm *bprm) · 1514 unmodified lines
15151515 * state is protected by cred_guard_mutex we hold.
15161516 */
15171517 n_fs = 1;
1518− spin_lock(&p->fs->lock);
1518+ read_seqlock_excl(&p->fs->seq);
15191519 rcu_read_lock();
15201520 for_other_threads(p, t) {
15211521 if (t->fs == p->fs)
@@ -1528,7 +1528,7 @@ static void check_unsafe_exec(struct linux_binprm *bprm) · 6 unmodified lines
15281528 bprm->unsafe |= LSM_UNSAFE_SHARE;
15291529 else
15301530 p->fs->in_exec = 1;
1531− spin_unlock(&p->fs->lock);
1531+ read_sequnlock_excl(&p->fs->seq);
15321532 }
15331533
15341534 static void bprm_fill_uid(struct linux_binprm *bprm, struct file *file)
fs/fhandle.c+30−32
@@ -88,7 +88,7 @@ static long do_sys_name_to_handle(const struct path *path, · 87 unmodified lines
8888 if (fh_flags & EXPORT_FH_CONNECTABLE) {
8989 handle->handle_type |= FILEID_IS_CONNECTABLE;
9090 if (d_is_dir(path->dentry))
91− fh_flags |= FILEID_IS_DIR;
91+ handle->handle_type |= FILEID_IS_DIR;
9292 }
9393 retval = 0;
9494 }
@@ -168,23 +168,28 @@ SYSCALL_DEFINE5(name_to_handle_at, int, dfd, const char __user *, name, · 73 unmodified lines
168168 return err;
169169 }
170170
171−static int get_path_from_fd(int fd, struct path *root)
171+static int get_path_anchor(int fd, struct path *root)
172172 {
173− if (fd == AT_FDCWD) {
174− struct fs_struct *fs = current->fs;
175− spin_lock(&fs->lock);
176− *root = fs->pwd;
177− path_get(root);
178− spin_unlock(&fs->lock);
179− } else {
173+ if (fd >= 0) {
180174 CLASS(fd, f)(fd);
181175 if (fd_empty(f))
182176 return -EBADF;
183177 *root = fd_file(f)->f_path;
184178 path_get(root);
179+ return 0;
185180 }
186181
187− return 0;
182+ if (fd == AT_FDCWD) {
183+ get_fs_pwd(current->fs, root);
184+ return 0;
185+ }
186+
187+ if (fd == FD_PIDFS_ROOT) {
188+ pidfs_get_root(root);
189+ return 0;
190+ }
191+
192+ return -EBADF;
188193 }
189194
190195 static int vfs_dentry_acceptable(void *context, struct dentry *dentry)
@@ -323,13 +328,24 @@ static int handle_to_path(int mountdirfd, struct file_handle __user *ufh, · 132 unmodified lines
323328 {
324329 int retval = 0;
325330 struct file_handle f_handle;
326− struct file_handle *handle = NULL;
331+ struct file_handle *handle __free(kfree) = NULL;
327332 struct handle_to_path_ctx ctx = {};
328333 const struct export_operations *eops;
329334
330− retval = get_path_from_fd(mountdirfd, &ctx.root);
335+ if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle)))
336+ return -EFAULT;
337+
338+ if ((f_handle.handle_bytes > MAX_HANDLE_SZ) ||
339+ (f_handle.handle_bytes == 0))
340+ return -EINVAL;
341+
342+ if (f_handle.handle_type < 0 ||
343+ FILEID_USER_FLAGS(f_handle.handle_type) & ~FILEID_VALID_USER_FLAGS)
344+ return -EINVAL;
345+
346+ retval = get_path_anchor(mountdirfd, &ctx.root);
331347 if (retval)
332− goto out_err;
348+ return retval;
333349
334350 eops = ctx.root.mnt->mnt_sb->s_export_op;
335351 if (eops && eops->permission)
@@ -339,21 +355,6 @@ static int handle_to_path(int mountdirfd, struct file_handle __user *ufh, · 3 unmodified lines
339355 if (retval)
340356 goto out_path;
341357
342− if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle))) {
343− retval = -EFAULT;
344− goto out_path;
345− }
346− if ((f_handle.handle_bytes > MAX_HANDLE_SZ) ||
347− (f_handle.handle_bytes == 0)) {
348− retval = -EINVAL;
349− goto out_path;
350− }
351− if (f_handle.handle_type < 0 ||
352− FILEID_USER_FLAGS(f_handle.handle_type) & ~FILEID_VALID_USER_FLAGS) {
353− retval = -EINVAL;
354− goto out_path;
355− }
356−
357358 handle = kmalloc(struct_size(handle, f_handle, f_handle.handle_bytes),
358359 GFP_KERNEL);
359360 if (!handle) {
@@ -366,7 +367,7 @@ static int handle_to_path(int mountdirfd, struct file_handle __user *ufh, · 6 unmodified lines
366367 &ufh->f_handle,
367368 f_handle.handle_bytes)) {
368369 retval = -EFAULT;
369− goto out_handle;
370+ goto out_path;
370371 }
371372
372373 /*
@@ -384,11 +385,8 @@ static int handle_to_path(int mountdirfd, struct file_handle __user *ufh, · 11 unmodified lines
384385 handle->handle_type &= ~FILEID_USER_FLAGS_MASK;
385386 retval = do_handle_to_path(handle, path, &ctx);
386387
387−out_handle:
388− kfree(handle);
389388 out_path:
390389 path_put(&ctx.root);
391−out_err:
392390 return retval;
393391 }
394392
fs/fs_struct.c+14−22
@@ -17,12 +17,10 @@ void set_fs_root(struct fs_struct *fs, const struct path *path) · 16 unmodified lines
1717 struct path old_root;
1818
1919 path_get(path);
20− spin_lock(&fs->lock);
21− write_seqcount_begin(&fs->seq);
20+ write_seqlock(&fs->seq);
2221 old_root = fs->root;
2322 fs->root = *path;
24− write_seqcount_end(&fs->seq);
25− spin_unlock(&fs->lock);
23+ write_sequnlock(&fs->seq);
2624 if (old_root.dentry)
2725 path_put(&old_root);
2826 }
@@ -36,12 +34,10 @@ void set_fs_pwd(struct fs_struct *fs, const struct path *path) · 7 unmodified lines
3634 struct path old_pwd;
3735
3836 path_get(path);
39− spin_lock(&fs->lock);
40− write_seqcount_begin(&fs->seq);
37+ write_seqlock(&fs->seq);
4138 old_pwd = fs->pwd;
4239 fs->pwd = *path;
43− write_seqcount_end(&fs->seq);
44− spin_unlock(&fs->lock);
40+ write_sequnlock(&fs->seq);
4541
4642 if (old_pwd.dentry)
4743 path_put(&old_pwd);
@@ -67,16 +63,14 @@ void chroot_fs_refs(const struct path *old_root, const struct path *new_root) · 19 unmodified lines
6763 fs = p->fs;
6864 if (fs) {
6965 int hits = 0;
70− spin_lock(&fs->lock);
71− write_seqcount_begin(&fs->seq);
66+ write_seqlock(&fs->seq);
7267 hits += replace_path(&fs->root, old_root, new_root);
7368 hits += replace_path(&fs->pwd, old_root, new_root);
74− write_seqcount_end(&fs->seq);
7569 while (hits--) {
7670 count++;
7771 path_get(new_root);
7872 }
79− spin_unlock(&fs->lock);
73+ write_sequnlock(&fs->seq);
8074 }
8175 task_unlock(p);
8276 }
@@ -99,10 +93,10 @@ void exit_fs(struct task_struct *tsk) · 16 unmodified lines
9993 if (fs) {
10094 int kill;
10195 task_lock(tsk);
102− spin_lock(&fs->lock);
96+ read_seqlock_excl(&fs->seq);
10397 tsk->fs = NULL;
10498 kill = !--fs->users;
105− spin_unlock(&fs->lock);
99+ read_sequnlock_excl(&fs->seq);
106100 task_unlock(tsk);
107101 if (kill)
108102 free_fs_struct(fs);
@@ -116,16 +110,15 @@ struct fs_struct *copy_fs_struct(struct fs_struct *old) · 7 unmodified lines
116110 if (fs) {
117111 fs->users = 1;
118112 fs->in_exec = 0;
119− spin_lock_init(&fs->lock);
120− seqcount_spinlock_init(&fs->seq, &fs->lock);
113+ seqlock_init(&fs->seq);
121114 fs->umask = old->umask;
122115
123− spin_lock(&old->lock);
116+ read_seqlock_excl(&old->seq);
124117 fs->root = old->root;
125118 path_get(&fs->root);
126119 fs->pwd = old->pwd;
127120 path_get(&fs->pwd);
128− spin_unlock(&old->lock);
121+ read_sequnlock_excl(&old->seq);
129122 }
130123 return fs;
131124 }
@@ -140,10 +133,10 @@ int unshare_fs_struct(void) · 8 unmodified lines
140133 return -ENOMEM;
141134
142135 task_lock(current);
143− spin_lock(&fs->lock);
136+ read_seqlock_excl(&fs->seq);
144137 kill = !--fs->users;
145138 current->fs = new_fs;
146− spin_unlock(&fs->lock);
139+ read_sequnlock_excl(&fs->seq);
147140 task_unlock(current);
148141
149142 if (kill)
@@ -162,7 +155,6 @@ EXPORT_SYMBOL(current_umask); · 12 unmodified lines
162155 /* to be mentioned only in INIT_TASK */
163156 struct fs_struct init_fs = {
164157 .users = 1,
165− .lock = __SPIN_LOCK_UNLOCKED(init_fs.lock),
166− .seq = SEQCNT_SPINLOCK_ZERO(init_fs.seq, &init_fs.lock),
158+ .seq = __SEQLOCK_UNLOCKED(init_fs.seq),
167159 .umask = 0022,
168160 };
fs/internal.h+4−0
@@ -323,12 +323,15 @@ struct mnt_idmap *alloc_mnt_idmap(struct user_namespace *mnt_userns); · 322 unmodified lines
323323 struct mnt_idmap *mnt_idmap_get(struct mnt_idmap *idmap);
324324 void mnt_idmap_put(struct mnt_idmap *idmap);
325325 struct stashed_operations {
326+ struct dentry *(*stash_dentry)(struct dentry **stashed,
327+ struct dentry *dentry);
326328 void (*put_data)(void *data);
327329 int (*init_inode)(struct inode *inode, void *data);
328330 };
329331 int path_from_stashed(struct dentry **stashed, struct vfsmount *mnt, void *data,
330332 struct path *path);
331333 void stashed_dentry_prune(struct dentry *dentry);
334+struct dentry *stash_dentry(struct dentry **stashed, struct dentry *dentry);
332335 struct dentry *stashed_dentry_get(struct dentry **stashed);
333336 /**
334337 * path_mounted - check whether path is mounted
@@ -351,3 +354,4 @@ int anon_inode_getattr(struct mnt_idmap *idmap, const struct path *path, · 16 unmodified lines
351354 unsigned int query_flags);
352355 int anon_inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
353356 struct iattr *attr);
357+void pidfs_get_root(struct path *path);
fs/libfs.c+22−12
@@ -2143,6 +2143,8 @@ struct dentry *stashed_dentry_get(struct dentry **stashed) · 2142 unmodified lines
21432143 dentry = rcu_dereference(*stashed);
21442144 if (!dentry)
21452145 return NULL;
2146+ if (IS_ERR(dentry))
2147+ return dentry;
21462148 if (!lockref_get_not_dead(&dentry->d_lockref))
21472149 return NULL;
21482150 return dentry;
@@ -2175,7 +2177,6 @@ static struct dentry *prepare_anon_dentry(struct dentry **stashed, · 26 unmodified lines
21752177
21762178 /* Notice when this is changed. */
21772179 WARN_ON_ONCE(!S_ISREG(inode->i_mode));
2178− WARN_ON_ONCE(!IS_IMMUTABLE(inode));
21792180
21802181 dentry = d_alloc_anon(sb);
21812182 if (!dentry) {
@@ -2191,8 +2192,7 @@ static struct dentry *prepare_anon_dentry(struct dentry **stashed, · 9 unmodified lines
21912192 return dentry;
21922193 }
21932194
2194−static struct dentry *stash_dentry(struct dentry **stashed,
2195− struct dentry *dentry)
2195+struct dentry *stash_dentry(struct dentry **stashed, struct dentry *dentry)
21962196 {
21972197 guard(rcu)();
21982198 for (;;) {
@@ -2233,14 +2233,16 @@ static struct dentry *stash_dentry(struct dentry **stashed, · 34 unmodified lines
22332233 int path_from_stashed(struct dentry **stashed, struct vfsmount *mnt, void *data,
22342234 struct path *path)
22352235 {
2236− struct dentry *dentry;
2236+ struct dentry *dentry, *res;
22372237 const struct stashed_operations *sops = mnt->mnt_sb->s_fs_info;
22382238
22392239 /* See if dentry can be reused. */
2240− path->dentry = stashed_dentry_get(stashed);
2241− if (path->dentry) {
2240+ res = stashed_dentry_get(stashed);
2241+ if (IS_ERR(res))
2242+ return PTR_ERR(res);
2243+ if (res) {
22422244 sops->put_data(data);
2243− goto out_path;
2245+ goto make_path;
22442246 }
22452247
22462248 /* Allocate a new dentry. */
@@ -2249,14 +2251,22 @@ int path_from_stashed(struct dentry **stashed, struct vfsmount *mnt, void *data, · 2 unmodified lines
22492251 return PTR_ERR(dentry);
22502252
22512253 /* Added a new dentry. @data is now owned by the filesystem. */
2252− path->dentry = stash_dentry(stashed, dentry);
2253− if (path->dentry != dentry)
2254+ if (sops->stash_dentry)
2255+ res = sops->stash_dentry(stashed, dentry);
2256+ else
2257+ res = stash_dentry(stashed, dentry);
2258+ if (IS_ERR(res)) {
2259+ dput(dentry);
2260+ return PTR_ERR(res);
2261+ }
2262+ if (res != dentry)
22542263 dput(dentry);
22552264
2256−out_path:
2257− WARN_ON_ONCE(path->dentry->d_fsdata != stashed);
2258− WARN_ON_ONCE(d_inode(path->dentry)->i_private != data);
2265+make_path:
2266+ path->dentry = res;
22592267 path->mnt = mntget(mnt);
2268+ VFS_WARN_ON_ONCE(path->dentry->d_fsdata != stashed);
2269+ VFS_WARN_ON_ONCE(d_inode(path->dentry)->i_private != data);
22602270 return 0;
22612271 }
22622272
fs/namei.c+4−4
@@ -1012,10 +1012,10 @@ static int set_root(struct nameidata *nd) · 1011 unmodified lines
10121012 unsigned seq;
10131013
10141014 do {
1015− seq = read_seqcount_begin(&fs->seq);
1015+ seq = read_seqbegin(&fs->seq);
10161016 nd->root = fs->root;
10171017 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
1018− } while (read_seqcount_retry(&fs->seq, seq));
1018+ } while (read_seqretry(&fs->seq, seq));
10191019 } else {
10201020 get_fs_root(fs, &nd->root);
10211021 nd->state |= ND_ROOT_GRABBED;
@@ -2571,11 +2571,11 @@ static const char *path_init(struct nameidata *nd, unsigned flags) · 1549 unmodified lines
25712571 unsigned seq;
25722572
25732573 do {
2574− seq = read_seqcount_begin(&fs->seq);
2574+ seq = read_seqbegin(&fs->seq);
25752575 nd->path = fs->pwd;
25762576 nd->inode = nd->path.dentry->d_inode;
25772577 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2578− } while (read_seqcount_retry(&fs->seq, seq));
2578+ } while (read_seqretry(&fs->seq, seq));
25792579 } else {
25802580 get_fs_pwd(current->fs, &nd->path);
25812581 nd->inode = nd->path.dentry->d_inode;
fs/pidfs.c+253−183
@@ -21,11 +21,23 @@ · 20 unmodified lines
2121 #include <linux/utsname.h>
2222 #include <net/net_namespace.h>
2323 #include <linux/coredump.h>
24+#include <linux/xattr.h>
2425
2526 #include "internal.h"
2627 #include "mount.h"
2728
28−static struct kmem_cache *pidfs_cachep __ro_after_init;
29+#define PIDFS_PID_DEAD ERR_PTR(-ESRCH)
30+
31+static struct kmem_cache *pidfs_attr_cachep __ro_after_init;
32+static struct kmem_cache *pidfs_xattr_cachep __ro_after_init;
33+
34+static struct path pidfs_root_path = {};
35+
36+void pidfs_get_root(struct path *path)
37+{
38+ *path = pidfs_root_path;
39+ path_get(path);
40+}
2941
3042 /*
3143 * Stashes information that userspace needs to access even after the
@@ -37,17 +49,12 @@ struct pidfs_exit_info { · 5 unmodified lines
3749 __u32 coredump_mask;
3850 };
3951
40−struct pidfs_inode {
52+struct pidfs_attr {
53+ struct simple_xattrs *xattrs;
4154 struct pidfs_exit_info __pei;
4255 struct pidfs_exit_info *exit_info;
43− struct inode vfs_inode;
4456 };
4557
46−static inline struct pidfs_inode *pidfs_i(struct inode *inode)
47−{
48− return container_of(inode, struct pidfs_inode, vfs_inode);
49−}
50−
5158 static struct rb_root pidfs_ino_tree = RB_ROOT;
5259
5360 #if BITS_PER_LONG == 32
@@ -125,6 +132,7 @@ void pidfs_add_pid(struct pid *pid) · 71 unmodified lines
125132
126133 pid->ino = pidfs_ino_nr;
127134 pid->stashed = NULL;
135+ pid->attr = NULL;
128136 pidfs_ino_nr++;
129137
130138 write_seqcount_begin(&pidmap_lock_seq);
@@ -139,6 +147,33 @@ void pidfs_remove_pid(struct pid *pid) · 8 unmodified lines
139147 write_seqcount_end(&pidmap_lock_seq);
140148 }
141149
150+void pidfs_free_pid(struct pid *pid)
151+{
152+ struct pidfs_attr *attr __free(kfree) = no_free_ptr(pid->attr);
153+ struct simple_xattrs *xattrs __free(kfree) = NULL;
154+
155+ /*
156+ * Any dentry must've been wiped from the pid by now.
157+ * Otherwise there's a reference count bug.
158+ */
159+ VFS_WARN_ON_ONCE(pid->stashed);
160+
161+ /*
162+ * This if an error occurred during e.g., task creation that
163+ * causes us to never go through the exit path.
164+ */
165+ if (unlikely(!attr))
166+ return;
167+
168+ /* This never had a pidfd created. */
169+ if (IS_ERR(attr))
170+ return;
171+
172+ xattrs = no_free_ptr(attr->xattrs);
173+ if (xattrs)
174+ simple_xattrs_free(xattrs, NULL);
175+}
176+
142177 #ifdef CONFIG_PROC_FS
143178 /**
144179 * pidfd_show_fdinfo - print information about a pidfd
@@ -261,13 +296,13 @@ static __u32 pidfs_coredump_mask(unsigned long mm_flags) · 116 unmodified lines
261296 static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg)
262297 {
263298 struct pidfd_info __user *uinfo = (struct pidfd_info __user *)arg;
264− struct inode *inode = file_inode(file);
265299 struct pid *pid = pidfd_pid(file);
266300 size_t usize = _IOC_SIZE(cmd);
267301 struct pidfd_info kinfo = {};
268302 struct pidfs_exit_info *exit_info;
269303 struct user_namespace *user_ns;
270304 struct task_struct *task;
305+ struct pidfs_attr *attr;
271306 const struct cred *c;
272307 __u64 mask;
273308
@@ -286,8 +321,9 @@ static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg) · 12 unmodified lines
286321 if (!pid_in_current_pidns(pid))
287322 return -ESRCH;
288323
324+ attr = READ_ONCE(pid->attr);
289325 if (mask & PIDFD_INFO_EXIT) {
290− exit_info = READ_ONCE(pidfs_i(inode)->exit_info);
326+ exit_info = READ_ONCE(attr->exit_info);
291327 if (exit_info) {
292328 kinfo.mask |= PIDFD_INFO_EXIT;
293329 #ifdef CONFIG_CGROUPS
@@ -300,7 +336,7 @@ static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg) · 6 unmodified lines
300336
301337 if (mask & PIDFD_INFO_COREDUMP) {
302338 kinfo.mask |= PIDFD_INFO_COREDUMP;
303− kinfo.coredump_mask = READ_ONCE(pidfs_i(inode)->__pei.coredump_mask);
339+ kinfo.coredump_mask = READ_ONCE(attr->__pei.coredump_mask);
304340 }
305341
306342 task = get_pid_task(pid, PIDTYPE_PID);
@@ -552,41 +588,61 @@ struct pid *pidfd_pid(const struct file *file) · 245 unmodified lines
552588 * task has been reaped which cannot happen until we're out of
553589 * release_task().
554590 *
555− * If this struct pid is referred to by a pidfd then
556− * stashed_dentry_get() will return the dentry and inode for that struct
557− * pid. Since we've taken a reference on it there's now an additional
558− * reference from the exit path on it. Which is fine. We're going to put
559− * it again in a second and we know that the pid is kept alive anyway.
591+ * If this struct pid has at least once been referred to by a pidfd then
592+ * pid->attr will be allocated. If not we mark the struct pid as dead so
593+ * anyone who is trying to register it with pidfs will fail to do so.
594+ * Otherwise we would hand out pidfs for reaped tasks without having
595+ * exit information available.
560596 *
561− * Worst case is that we've filled in the info and immediately free the
562− * dentry and inode afterwards since the pidfd has been closed. Since
597+ * Worst case is that we've filled in the info and the pid gets freed
598+ * right away in free_pid() when no one holds a pidfd anymore. Since
563599 * pidfs_exit() currently is placed after exit_task_work() we know that
564− * it cannot be us aka the exiting task holding a pidfd to ourselves.
600+ * it cannot be us aka the exiting task holding a pidfd to itself.
565601 */
566602 void pidfs_exit(struct task_struct *tsk)
567603 {
568− struct dentry *dentry;
604+ struct pid *pid = task_pid(tsk);
605+ struct pidfs_attr *attr;
606+ struct pidfs_exit_info *exit_info;
607+#ifdef CONFIG_CGROUPS
608+ struct cgroup *cgrp;
609+#endif
569610
570611 might_sleep();
571612
572− dentry = stashed_dentry_get(&task_pid(tsk)->stashed);
573− if (dentry) {
574− struct inode *inode = d_inode(dentry);
575− struct pidfs_exit_info *exit_info = &pidfs_i(inode)->__pei;
576−#ifdef CONFIG_CGROUPS
577− struct cgroup *cgrp;
613+ guard(spinlock_irq)(&pid->wait_pidfd.lock);
614+ attr = pid->attr;
615+ if (!attr) {
616+ /*
617+ * No one ever held a pidfd for this struct pid.
618+ * Mark it as dead so no one can add a pidfs
619+ * entry anymore. We're about to be reaped and
620+ * so no exit information would be available.
621+ */
622+ pid->attr = PIDFS_PID_DEAD;
623+ return;
624+ }
578625
579− rcu_read_lock();
580− cgrp = task_dfl_cgroup(tsk);
581− exit_info->cgroupid = cgroup_id(cgrp);
582− rcu_read_unlock();
626+ /*
627+ * If @pid->attr is set someone might still legitimately hold a
628+ * pidfd to @pid or someone might concurrently still be getting
629+ * a reference to an already stashed dentry from @pid->stashed.
630+ * So defer cleaning @pid->attr until the last reference to @pid
631+ * is put
632+ */
633+
634+ exit_info = &attr->__pei;
635+
636+#ifdef CONFIG_CGROUPS
637+ rcu_read_lock();
638+ cgrp = task_dfl_cgroup(tsk);
639+ exit_info->cgroupid = cgroup_id(cgrp);
640+ rcu_read_unlock();
583641 #endif
584− exit_info->exit_code = tsk->exit_code;
642+ exit_info->exit_code = tsk->exit_code;
585643
586− /* Ensure that PIDFD_GET_INFO sees either all or nothing. */
587− smp_store_release(&pidfs_i(inode)->exit_info, &pidfs_i(inode)->__pei);
588− dput(dentry);
589− }
644+ /* Ensure that PIDFD_GET_INFO sees either all or nothing. */
645+ smp_store_release(&attr->exit_info, &attr->__pei);
590646 }
591647
592648 #ifdef CONFIG_COREDUMP
@@ -594,16 +650,15 @@ void pidfs_coredump(const struct coredump_params *cprm) · 1 unmodified line
594650 {
595651 struct pid *pid = cprm->pid;
596652 struct pidfs_exit_info *exit_info;
597− struct dentry *dentry;
598− struct inode *inode;
653+ struct pidfs_attr *attr;
599654 __u32 coredump_mask = 0;
600655
601− dentry = pid->stashed;
602− if (WARN_ON_ONCE(!dentry))
603− return;
656+ attr = READ_ONCE(pid->attr);
604657
605− inode = d_inode(dentry);
606− exit_info = &pidfs_i(inode)->__pei;
658+ VFS_WARN_ON_ONCE(!attr);
659+ VFS_WARN_ON_ONCE(attr == PIDFS_PID_DEAD);
660+
661+ exit_info = &attr->__pei;
607662 /* Note how we were coredumped. */
608663 coredump_mask = pidfs_coredump_mask(cprm->mm_flags);
609664 /* Note that we actually did coredump. */
@@ -634,9 +689,24 @@ static int pidfs_getattr(struct mnt_idmap *idmap, const struct path *path, · 24 unmodified lines
634689 return anon_inode_getattr(idmap, path, stat, request_mask, query_flags);
635690 }
636691
692+static ssize_t pidfs_listxattr(struct dentry *dentry, char *buf, size_t size)
693+{
694+ struct inode *inode = d_inode(dentry);
695+ struct pid *pid = inode->i_private;
696+ struct pidfs_attr *attr = pid->attr;
697+ struct simple_xattrs *xattrs;
698+
699+ xattrs = READ_ONCE(attr->xattrs);
700+ if (!xattrs)
701+ return 0;
702+
703+ return simple_xattr_list(inode, xattrs, buf, size);
704+}
705+
637706 static const struct inode_operations pidfs_inode_operations = {
638− .getattr = pidfs_getattr,
639− .setattr = pidfs_setattr,
707+ .getattr = pidfs_getattr,
708+ .setattr = pidfs_setattr,
709+ .listxattr = pidfs_listxattr,
640710 };
641711
642712 static void pidfs_evict_inode(struct inode *inode)
@@ -647,30 +717,9 @@ static void pidfs_evict_inode(struct inode *inode) · 4 unmodified lines
647717 put_pid(pid);
648718 }
649719
650−static struct inode *pidfs_alloc_inode(struct super_block *sb)
651−{
652− struct pidfs_inode *pi;
653−
654− pi = alloc_inode_sb(sb, pidfs_cachep, GFP_KERNEL);
655− if (!pi)
656− return NULL;
657−
658− memset(&pi->__pei, 0, sizeof(pi->__pei));
659− pi->exit_info = NULL;
660−
661− return &pi->vfs_inode;
662−}
663−
664−static void pidfs_free_inode(struct inode *inode)
665−{
666− kmem_cache_free(pidfs_cachep, pidfs_i(inode));
667−}
668−
669720 static const struct super_operations pidfs_sops = {
670− .alloc_inode = pidfs_alloc_inode,
671721 .drop_inode = generic_delete_inode,
672722 .evict_inode = pidfs_evict_inode,
673− .free_inode = pidfs_free_inode,
674723 .statfs = simple_statfs,
675724 };
676725
@@ -770,6 +819,8 @@ static struct dentry *pidfs_fh_to_dentry(struct super_block *sb, · 93 unmodified lines
770819 if (ret < 0)
771820 return ERR_PTR(ret);
772821
822+ VFS_WARN_ON_ONCE(!pid->attr);
823+
773824 mntput(path.mnt);
774825 return path.dentry;
775826 }
@@ -796,53 +847,8 @@ static int pidfs_export_permission(struct handle_to_path_ctx *ctx, · 20 unmodified lines
796847 return 0;
797848 }
798849
799−static inline bool pidfs_pid_valid(struct pid *pid, const struct path *path,
800− unsigned int flags)
801−{
802− enum pid_type type;
803−
804− if (flags & PIDFD_STALE)
805− return true;
806−
807− /*
808− * Make sure that if a pidfd is created PIDFD_INFO_EXIT
809− * information will be available. So after an inode for the
810− * pidfd has been allocated perform another check that the pid
811− * is still alive. If it is exit information is available even
812− * if the task gets reaped before the pidfd is returned to
813− * userspace. The only exception are indicated by PIDFD_STALE:
814− *
815− * (1) The kernel is in the middle of task creation and thus no
816− * task linkage has been established yet.
817− * (2) The caller knows @pid has been registered in pidfs at a
818− * time when the task was still alive.
819− *
820− * In both cases exit information will have been reported.
821− */
822− if (flags & PIDFD_THREAD)
823− type = PIDTYPE_PID;
824− else
825− type = PIDTYPE_TGID;
826−
827− /*
828− * Since pidfs_exit() is called before struct pid's task linkage
829− * is removed the case where the task got reaped but a dentry
830− * was already attached to struct pid and exit information was
831− * recorded and published can be handled correctly.
832− */
833− if (unlikely(!pid_has_task(pid, type))) {
834− struct inode *inode = d_inode(path->dentry);
835− return !!READ_ONCE(pidfs_i(inode)->exit_info);
836− }
837−
838− return true;
839−}
840−
841850 static struct file *pidfs_export_open(struct path *path, unsigned int oflags)
842851 {
843− if (!pidfs_pid_valid(d_inode(path->dentry)->i_private, path, oflags))
844− return ERR_PTR(-ESRCH);
845−
846852 /*
847853 * Clear O_LARGEFILE as open_by_handle_at() forces it and raise
848854 * O_RDWR as pidfds always are.
@@ -864,6 +870,8 @@ static int pidfs_init_inode(struct inode *inode, void *data) · 15 unmodified lines
864870
865871 inode->i_private = data;
866872 inode->i_flags |= S_PRIVATE | S_ANON_INODE;
873+ /* We allow to set xattrs. */
874+ inode->i_flags &= ~S_IMMUTABLE;
867875 inode->i_mode |= S_IRWXU;
868876 inode->i_op = &pidfs_inode_operations;
869877 inode->i_fop = &pidfs_file_operations;
@@ -878,9 +886,127 @@ static void pidfs_put_data(void *data) · 8 unmodified lines
878886 put_pid(pid);
879887 }
880888
889+/**
890+ * pidfs_register_pid - register a struct pid in pidfs
891+ * @pid: pid to pin
892+ *
893+ * Register a struct pid in pidfs.
894+ *
895+ * Return: On success zero, on error a negative error code is returned.
896+ */
897+int pidfs_register_pid(struct pid *pid)
898+{
899+ struct pidfs_attr *new_attr __free(kfree) = NULL;
900+ struct pidfs_attr *attr;
901+
902+ might_sleep();
903+
904+ if (!pid)
905+ return 0;
906+
907+ attr = READ_ONCE(pid->attr);
908+ if (unlikely(attr == PIDFS_PID_DEAD))
909+ return PTR_ERR(PIDFS_PID_DEAD);
910+ if (attr)
911+ return 0;
912+
913+ new_attr = kmem_cache_zalloc(pidfs_attr_cachep, GFP_KERNEL);
914+ if (!new_attr)
915+ return -ENOMEM;
916+
917+ /* Synchronize with pidfs_exit(). */
918+ guard(spinlock_irq)(&pid->wait_pidfd.lock);
919+
920+ attr = pid->attr;
921+ if (unlikely(attr == PIDFS_PID_DEAD))
922+ return PTR_ERR(PIDFS_PID_DEAD);
923+ if (unlikely(attr))
924+ return 0;
925+
926+ pid->attr = no_free_ptr(new_attr);
927+ return 0;
928+}
929+
930+static struct dentry *pidfs_stash_dentry(struct dentry **stashed,
931+ struct dentry *dentry)
932+{
933+ int ret;
934+ struct pid *pid = d_inode(dentry)->i_private;
935+
936+ VFS_WARN_ON_ONCE(stashed != &pid->stashed);
937+
938+ ret = pidfs_register_pid(pid);
939+ if (ret)
940+ return ERR_PTR(ret);
941+
942+ return stash_dentry(stashed, dentry);
943+}
944+
881945 static const struct stashed_operations pidfs_stashed_ops = {
882− .init_inode = pidfs_init_inode,
883− .put_data = pidfs_put_data,
946+ .stash_dentry = pidfs_stash_dentry,
947+ .init_inode = pidfs_init_inode,
948+ .put_data = pidfs_put_data,
949+};
950+
951+static int pidfs_xattr_get(const struct xattr_handler *handler,
952+ struct dentry *unused, struct inode *inode,
953+ const char *suffix, void *value, size_t size)
954+{
955+ struct pid *pid = inode->i_private;
956+ struct pidfs_attr *attr = pid->attr;
957+ const char *name;
958+ struct simple_xattrs *xattrs;
959+
960+ xattrs = READ_ONCE(attr->xattrs);
961+ if (!xattrs)
962+ return 0;
963+
964+ name = xattr_full_name(handler, suffix);
965+ return simple_xattr_get(xattrs, name, value, size);
966+}
967+
968+static int pidfs_xattr_set(const struct xattr_handler *handler,
969+ struct mnt_idmap *idmap, struct dentry *unused,
970+ struct inode *inode, const char *suffix,
971+ const void *value, size_t size, int flags)
972+{
973+ struct pid *pid = inode->i_private;
974+ struct pidfs_attr *attr = pid->attr;
975+ const char *name;
976+ struct simple_xattrs *xattrs;
977+ struct simple_xattr *old_xattr;
978+
979+ /* Ensure we're the only one to set @attr->xattrs. */
980+ WARN_ON_ONCE(!inode_is_locked(inode));
981+
982+ xattrs = READ_ONCE(attr->xattrs);
983+ if (!xattrs) {
984+ xattrs = kmem_cache_zalloc(pidfs_xattr_cachep, GFP_KERNEL);
985+ if (!xattrs)
986+ return -ENOMEM;
987+
988+ simple_xattrs_init(xattrs);
989+ smp_store_release(&pid->attr->xattrs, xattrs);
990+ }
991+
992+ name = xattr_full_name(handler, suffix);
993+ old_xattr = simple_xattr_set(xattrs, name, value, size, flags);
994+ if (IS_ERR(old_xattr))
995+ return PTR_ERR(old_xattr);
996+
997+ simple_xattr_free(old_xattr);
998+ return 0;
999+}
1000+
1001+static const struct xattr_handler pidfs_trusted_xattr_handler = {
1002+ .prefix = XATTR_TRUSTED_PREFIX,
1003+ .get = pidfs_xattr_get,
1004+ .set = pidfs_xattr_set,
1005+};
1006+
1007+static const struct xattr_handler *const pidfs_xattr_handlers[] = {
1008+ &pidfs_trusted_xattr_handler,
1009+ NULL
8841010 };
8851011
8861012 static int pidfs_init_fs_context(struct fs_context *fc)
@@ -891,9 +1017,12 @@ static int pidfs_init_fs_context(struct fs_context *fc) · 4 unmodified lines
8911017 if (!ctx)
8921018 return -ENOMEM;
8931019
1020+ fc->s_iflags |= SB_I_NOEXEC;
1021+ fc->s_iflags |= SB_I_NODEV;
8941022 ctx->ops = &pidfs_sops;
8951023 ctx->eops = &pidfs_export_operations;
8961024 ctx->dops = &pidfs_dentry_operations;
1025+ ctx->xattr = pidfs_xattr_handlers;
8971026 fc->s_fs_info = (void *)&pidfs_stashed_ops;
8981027 return 0;
8991028 }
@@ -921,8 +1050,7 @@ struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags) · 21 unmodified lines
9211050 if (ret < 0)
9221051 return ERR_PTR(ret);
9231052
924− if (!pidfs_pid_valid(pid, &path, flags))
925− return ERR_PTR(-ESRCH);
1053+ VFS_WARN_ON_ONCE(!pid->attr);
9261054
9271055 flags &= ~PIDFD_STALE;
9281056 flags |= O_RDWR;
@@ -934,79 +1062,21 @@ struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags) · 5 unmodified lines
9341062 return pidfd_file;
9351063 }
9361064
937−/**
938− * pidfs_register_pid - register a struct pid in pidfs
939− * @pid: pid to pin
940− *
941− * Register a struct pid in pidfs. Needs to be paired with
942− * pidfs_put_pid() to not risk leaking the pidfs dentry and inode.
943− *
944− * Return: On success zero, on error a negative error code is returned.
945− */
946−int pidfs_register_pid(struct pid *pid)
947−{
948− struct path path __free(path_put) = {};
949− int ret;
950−
951− might_sleep();
952−
953− if (!pid)
954− return 0;
955−
956− ret = path_from_stashed(&pid->stashed, pidfs_mnt, get_pid(pid), &path);
957− if (unlikely(ret))
958− return ret;
959− /* Keep the dentry and only put the reference to the mount. */
960− path.dentry = NULL;
961− return 0;
962−}
963−
964−/**
965− * pidfs_get_pid - pin a struct pid through pidfs
966− * @pid: pid to pin
967− *
968− * Similar to pidfs_register_pid() but only valid if the caller knows
969− * there's a reference to the @pid through a dentry already that can't
970− * go away.
971− */
972−void pidfs_get_pid(struct pid *pid)
973−{
974− if (!pid)
975− return;
976− WARN_ON_ONCE(!stashed_dentry_get(&pid->stashed));
977−}
978−
979−/**
980− * pidfs_put_pid - drop a pidfs reference
981− * @pid: pid to drop
982− *
983− * Drop a reference to @pid via pidfs. This is only safe if the
984− * reference has been taken via pidfs_get_pid().
985− */
986−void pidfs_put_pid(struct pid *pid)
987−{
988− might_sleep();
989−
990− if (!pid)
991− return;
992− VFS_WARN_ON_ONCE(!pid->stashed);
993− dput(pid->stashed);
994−}
995−
996−static void pidfs_inode_init_once(void *data)
997−{
998− struct pidfs_inode *pi = data;
999−
1000− inode_init_once(&pi->vfs_inode);
1001−}
1002−
10031065 void __init pidfs_init(void)
10041066 {
1005− pidfs_cachep = kmem_cache_create("pidfs_cache", sizeof(struct pidfs_inode), 0,
1067+ pidfs_attr_cachep = kmem_cache_create("pidfs_attr_cache", sizeof(struct pidfs_attr), 0,
10061068 (SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT |
1007− SLAB_ACCOUNT | SLAB_PANIC),
1008− pidfs_inode_init_once);
1069+ SLAB_ACCOUNT | SLAB_PANIC), NULL);
1070+
1071+ pidfs_xattr_cachep = kmem_cache_create("pidfs_xattr_cache",
1072+ sizeof(struct simple_xattrs), 0,
1073+ (SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT |
1074+ SLAB_ACCOUNT | SLAB_PANIC), NULL);
1075+
10091076 pidfs_mnt = kern_mount(&pidfs_type);
10101077 if (IS_ERR(pidfs_mnt))
10111078 panic("Failed to mount pidfs pseudo filesystem");
1079+
1080+ pidfs_root_path.mnt = pidfs_mnt;
1081+ pidfs_root_path.dentry = pidfs_mnt->mnt_root;
10121082 }
include/linux/fs_struct.h+5−6
@@ -8,8 +8,7 @@ · 7 unmodified lines
88
99 struct fs_struct {
1010 int users;
11− spinlock_t lock;
12− seqcount_spinlock_t seq;
11+ seqlock_t seq;
1312 int umask;
1413 int in_exec;
1514 struct path root, pwd;
@@ -26,18 +25,18 @@ extern int unshare_fs_struct(void); · 10 unmodified lines
2625
2726 static inline void get_fs_root(struct fs_struct *fs, struct path *root)
2827 {
29− spin_lock(&fs->lock);
28+ read_seqlock_excl(&fs->seq);
3029 *root = fs->root;
3130 path_get(root);
32− spin_unlock(&fs->lock);
31+ read_sequnlock_excl(&fs->seq);
3332 }
3433
3534 static inline void get_fs_pwd(struct fs_struct *fs, struct path *pwd)
3635 {
37− spin_lock(&fs->lock);
36+ read_seqlock_excl(&fs->seq);
3837 *pwd = fs->pwd;
3938 path_get(pwd);
40− spin_unlock(&fs->lock);
39+ read_sequnlock_excl(&fs->seq);
4140 }
4241
4342 extern bool current_chrooted(void);
include/linux/pid.h+9−5
@@ -47,19 +47,23 @@ · 46 unmodified lines
4747
4848 #define RESERVED_PIDS 300
4949
50+struct pidfs_attr;
51+
5052 struct upid {
5153 int nr;
5254 struct pid_namespace *ns;
5355 };
5456
55−struct pid
56−{
57+struct pid {
5758 refcount_t count;
5859 unsigned int level;
5960 spinlock_t lock;
60− struct dentry *stashed;
61− u64 ino;
62− struct rb_node pidfs_node;
61+ struct {
62+ u64 ino;
63+ struct rb_node pidfs_node;
64+ struct dentry *stashed;
65+ struct pidfs_attr *attr;
66+ };
6367 /* lists of tasks that use this pid */
6468 struct hlist_head tasks[PIDTYPE_MAX];
6569 struct hlist_head inodes;
include/linux/pidfs.h+1−2
@@ -14,7 +14,6 @@ void pidfs_coredump(const struct coredump_params *cprm); · 13 unmodified lines
1414 #endif
1515 extern const struct dentry_operations pidfs_dentry_operations;
1616 int pidfs_register_pid(struct pid *pid);
17−void pidfs_get_pid(struct pid *pid);
18−void pidfs_put_pid(struct pid *pid);
17+void pidfs_free_pid(struct pid *pid);
1918
2019 #endif /* _LINUX_PID_FS_H */
include/net/scm.h+2−2
@@ -69,7 +69,7 @@ static __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_co · 68 unmodified lines
6969 static __inline__ void scm_set_cred(struct scm_cookie *scm,
7070 struct pid *pid, kuid_t uid, kgid_t gid)
7171 {
72− scm->pid = get_pid(pid);
72+ scm->pid = get_pid(pid);
7373 scm->creds.pid = pid_vnr(pid);
7474 scm->creds.uid = uid;
7575 scm->creds.gid = gid;
@@ -78,7 +78,7 @@ static __inline__ void scm_set_cred(struct scm_cookie *scm, · 2 unmodified lines
7878 static __inline__ void scm_destroy_cred(struct scm_cookie *scm)
7979 {
8080 put_pid(scm->pid);
81− scm->pid = NULL;
81+ scm->pid = NULL;
8282 }
8383
8484 static __inline__ void scm_destroy(struct scm_cookie *scm)
include/uapi/linux/fcntl.h+18−0
@@ -90,10 +90,28 @@ · 89 unmodified lines
9090 #define DN_ATTRIB 0x00000020 /* File changed attibutes */
9191 #define DN_MULTISHOT 0x80000000 /* Don't remove notifier */
9292
93+/* Reserved kernel ranges [-100], [-10000, -40000]. */
9394 #define AT_FDCWD -100 /* Special value for dirfd used to
9495 indicate openat should use the
9596 current working directory. */
9697
98+/*
99+ * The concept of process and threads in userland and the kernel is a confusing
100+ * one - within the kernel every thread is a 'task' with its own individual PID,
101+ * however from userland's point of view threads are grouped by a single PID,
102+ * which is that of the 'thread group leader', typically the first thread
103+ * spawned.
104+ *
105+ * To cut the Gideon knot, for internal kernel usage, we refer to
106+ * PIDFD_SELF_THREAD to refer to the current thread (or task from a kernel
107+ * perspective), and PIDFD_SELF_THREAD_GROUP to refer to the current thread
108+ * group leader...
109+ */
110+#define PIDFD_SELF_THREAD -10000 /* Current thread. */
111+#define PIDFD_SELF_THREAD_GROUP -10001 /* Current thread group leader. */
112+
113+#define FD_PIDFS_ROOT -10002 /* Root of the pidfs filesystem */
114+#define FD_INVALID -10009 /* Invalid file descriptor: -10000 - EBADF = -10009 */
97115
98116 /* Generic flags for the *at(2) family of syscalls. */
99117
include/uapi/linux/pidfd.h+0−15
@@ -42,21 +42,6 @@ · 41 unmodified lines
4242 #define PIDFD_COREDUMP_USER (1U << 2) /* coredump was done as the user. */
4343 #define PIDFD_COREDUMP_ROOT (1U << 3) /* coredump was done as root. */
4444
45−/*
46− * The concept of process and threads in userland and the kernel is a confusing
47− * one - within the kernel every thread is a 'task' with its own individual PID,
48− * however from userland's point of view threads are grouped by a single PID,
49− * which is that of the 'thread group leader', typically the first thread
50− * spawned.
51− *
52− * To cut the Gideon knot, for internal kernel usage, we refer to
53− * PIDFD_SELF_THREAD to refer to the current thread (or task from a kernel
54− * perspective), and PIDFD_SELF_THREAD_GROUP to refer to the current thread
55− * group leader...
56− */
57−#define PIDFD_SELF_THREAD -10000 /* Current thread. */
58−#define PIDFD_SELF_THREAD_GROUP -20000 /* Current thread group leader. */
59−
6045 /*
6146 * ...and for userland we make life simpler - PIDFD_SELF refers to the current
6247 * thread, PIDFD_SELF_PROCESS refers to the process thread group leader.
kernel/fork.c+5−5
@@ -1542,14 +1542,14 @@ static int copy_fs(unsigned long clone_flags, struct task_struct *tsk) · 1541 unmodified lines
15421542 struct fs_struct *fs = current->fs;
15431543 if (clone_flags & CLONE_FS) {
15441544 /* tsk->fs is already what we want */
1545− spin_lock(&fs->lock);
1545+ read_seqlock_excl(&fs->seq);
15461546 /* "users" and "in_exec" locked for check_unsafe_exec() */
15471547 if (fs->in_exec) {
1548− spin_unlock(&fs->lock);
1548+ read_sequnlock_excl(&fs->seq);
15491549 return -EAGAIN;
15501550 }
15511551 fs->users++;
1552− spin_unlock(&fs->lock);
1552+ read_sequnlock_excl(&fs->seq);
15531553 return 0;
15541554 }
15551555 tsk->fs = copy_fs_struct(fs);
@@ -3149,13 +3149,13 @@ int ksys_unshare(unsigned long unshare_flags) · 1593 unmodified lines
31493149
31503150 if (new_fs) {
31513151 fs = current->fs;
3152− spin_lock(&fs->lock);
3152+ read_seqlock_excl(&fs->seq);
31533153 current->fs = new_fs;
31543154 if (--fs->users)
31553155 new_fs = NULL;
31563156 else
31573157 new_fs = fs;
3158− spin_unlock(&fs->lock);
3158+ read_sequnlock_excl(&fs->seq);
31593159 }
31603160
31613161 if (new_fd)
kernel/pid.c+1−1
@@ -100,7 +100,7 @@ void put_pid(struct pid *pid) · 99 unmodified lines
100100
101101 ns = pid->numbers[pid->level].ns;
102102 if (refcount_dec_and_test(&pid->count)) {
103− WARN_ON_ONCE(pid->stashed);
103+ pidfs_free_pid(pid);
104104 kmem_cache_free(ns->pid_cachep, pid);
105105 put_pid_ns(ns);
106106 }
net/core/scm.c+28−4
@@ -23,6 +23,8 @@ · 22 unmodified lines
2323 #include <linux/security.h>
2424 #include <linux/pid_namespace.h>
2525 #include <linux/pid.h>
26+#include <uapi/linux/pidfd.h>
27+#include <linux/pidfs.h>
2628 #include <linux/nsproxy.h>
2729 #include <linux/slab.h>
2830 #include <linux/errqueue.h>
@@ -145,6 +147,22 @@ void __scm_destroy(struct scm_cookie *scm) · 116 unmodified lines
145147 }
146148 EXPORT_SYMBOL(__scm_destroy);
147149
150+static inline int scm_replace_pid(struct scm_cookie *scm, struct pid *pid)
151+{
152+ int err;
153+
154+ /* drop all previous references */
155+ scm_destroy_cred(scm);
156+
157+ err = pidfs_register_pid(pid);
158+ if (unlikely(err))
159+ return err;
160+
161+ scm->pid = pid;
162+ scm->creds.pid = pid_vnr(pid);
163+ return 0;
164+}
165+
148166 int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
149167 {
150168 const struct proto_ops *ops = READ_ONCE(sock->ops);
@@ -189,15 +207,21 @@ int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p) · 38 unmodified lines
189207 if (err)
190208 goto error;
191209
192− p->creds.pid = creds.pid;
193210 if (!p->pid || pid_vnr(p->pid) != creds.pid) {
194211 struct pid *pid;
195212 err = -ESRCH;
196213 pid = find_get_pid(creds.pid);
197214 if (!pid)
198215 goto error;
199− put_pid(p->pid);
200− p->pid = pid;
216+
217+ /* pass a struct pid reference from
218+ * find_get_pid() to scm_replace_pid().
219+ */
220+ err = scm_replace_pid(p, pid);
221+ if (err) {
222+ put_pid(pid);
223+ goto error;
224+ }
201225 }
202226
203227 err = -EINVAL;
@@ -459,7 +483,7 @@ static void scm_pidfd_recv(struct msghdr *msg, struct scm_cookie *scm) · 255 unmodified lines
459483 if (!scm->pid)
460484 return;
461485
462− pidfd = pidfd_prepare(scm->pid, 0, &pidfd_file);
486+ pidfd = pidfd_prepare(scm->pid, PIDFD_STALE, &pidfd_file);
463487
464488 if (put_cmsg(msg, SOL_SOCKET, SCM_PIDFD, sizeof(int), &pidfd)) {
465489 if (pidfd_file) {
net/unix/af_unix.c+48−30
@@ -646,9 +646,6 @@ static void unix_sock_destructor(struct sock *sk) · 645 unmodified lines
646646 return;
647647 }
648648
649− if (sk->sk_peer_pid)
650− pidfs_put_pid(sk->sk_peer_pid);
651−
652649 if (u->addr)
653650 unix_release_addr(u->addr);
654651
@@ -780,7 +777,6 @@ static void drop_peercred(struct unix_peercred *peercred) · 125 unmodified lines
780777 swap(peercred->peer_pid, pid);
781778 swap(peercred->peer_cred, cred);
782779
783− pidfs_put_pid(pid);
784780 put_pid(pid);
785781 put_cred(cred);
786782 }
@@ -813,7 +809,6 @@ static void copy_peercred(struct sock *sk, struct sock *peersk) · 26 unmodified lines
813809
814810 spin_lock(&sk->sk_peer_lock);
815811 sk->sk_peer_pid = get_pid(peersk->sk_peer_pid);
816− pidfs_get_pid(sk->sk_peer_pid);
817812 sk->sk_peer_cred = get_cred(peersk->sk_peer_cred);
818813 spin_unlock(&sk->sk_peer_lock);
819814 }
@@ -1945,7 +1940,7 @@ static void unix_destruct_scm(struct sk_buff *skb) · 1125 unmodified lines
19451940 struct scm_cookie scm;
19461941
19471942 memset(&scm, 0, sizeof(scm));
1948− scm.pid = UNIXCB(skb).pid;
1943+ scm.pid = UNIXCB(skb).pid;
19491944 if (UNIXCB(skb).fp)
19501945 unix_detach_fds(&scm, skb);
19511946
@@ -1971,22 +1966,46 @@ static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool sen · 19 unmodified lines
19711966 return err;
19721967 }
19731968
1974−/*
1969+static void unix_skb_to_scm(struct sk_buff *skb, struct scm_cookie *scm)
1970+{
1971+ scm_set_cred(scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid);
1972+ unix_set_secdata(scm, skb);
1973+}
1974+
1975+/**
1976+ * unix_maybe_add_creds() - Adds current task uid/gid and struct pid to skb if needed.
1977+ * @skb: skb to attach creds to.
1978+ * @sk: Sender sock.
1979+ * @other: Receiver sock.
1980+ *
19751981 * Some apps rely on write() giving SCM_CREDENTIALS
19761982 * We include credentials if source or destination socket
19771983 * asserted SOCK_PASSCRED.
1984+ *
1985+ * Context: May sleep.
1986+ * Return: On success zero, on error a negative error code is returned.
19781987 */
1979−static void unix_maybe_add_creds(struct sk_buff *skb, const struct sock *sk,
1980− const struct sock *other)
1988+static int unix_maybe_add_creds(struct sk_buff *skb, const struct sock *sk,
1989+ const struct sock *other)
19811990 {
19821991 if (UNIXCB(skb).pid)
1983− return;
1992+ return 0;
19841993
19851994 if (unix_may_passcred(sk) || unix_may_passcred(other) ||
19861995 !other->sk_socket) {
1987− UNIXCB(skb).pid = get_pid(task_tgid(current));
1996+ struct pid *pid;
1997+ int err;
1998+
1999+ pid = task_tgid(current);
2000+ err = pidfs_register_pid(pid);
2001+ if (unlikely(err))
2002+ return err;
2003+
2004+ UNIXCB(skb).pid = get_pid(pid);
19882005 current_uid_gid(&UNIXCB(skb).uid, &UNIXCB(skb).gid);
19892006 }
2007+
2008+ return 0;
19902009 }
19912010
19922011 static bool unix_skb_scm_eq(struct sk_buff *skb,
@@ -2121,6 +2140,10 @@ lookup: · 128 unmodified lines
21212140 goto out_sock_put;
21222141 }
21232142
2143+ err = unix_maybe_add_creds(skb, sk, other);
2144+ if (err)
2145+ goto out_sock_put;
2146+
21242147 restart:
21252148 sk_locked = 0;
21262149 unix_state_lock(other);
@@ -2229,7 +2252,6 @@ restart_locked: · 102 unmodified lines
22292252 if (sock_flag(other, SOCK_RCVTSTAMP))
22302253 __net_timestamp(skb);
22312254
2232− unix_maybe_add_creds(skb, sk, other);
22332255 scm_stat_add(other, skb);
22342256 skb_queue_tail(&other->sk_receive_queue, skb);
22352257 unix_state_unlock(other);
@@ -2273,6 +2295,10 @@ static int queue_oob(struct sock *sk, struct msghdr *msg, struct sock *other, · 37 unmodified lines
22732295 if (err < 0)
22742296 goto out;
22752297
2298+ err = unix_maybe_add_creds(skb, sk, other);
2299+ if (err)
2300+ goto out;
2301+
22762302 skb_put(skb, 1);
22772303 err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, 1);
22782304
@@ -2292,7 +2318,6 @@ static int queue_oob(struct sock *sk, struct msghdr *msg, struct sock *other, · 13 unmodified lines
22922318 goto out_unlock;
22932319 }
22942320
2295− unix_maybe_add_creds(skb, sk, other);
22962321 scm_stat_add(other, skb);
22972322
22982323 spin_lock(&other->sk_receive_queue.lock);
@@ -2386,6 +2411,10 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg, · 87 unmodified lines
23862411
23872412 fds_sent = true;
23882413
2414+ err = unix_maybe_add_creds(skb, sk, other);
2415+ if (err)
2416+ goto out_free;
2417+
23892418 if (unlikely(msg->msg_flags & MSG_SPLICE_PAGES)) {
23902419 skb->ip_summed = CHECKSUM_UNNECESSARY;
23912420 err = skb_splice_from_iter(skb, &msg->msg_iter, size,
@@ -2416,7 +2445,6 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg, · 24 unmodified lines
24162445 goto out_free;
24172446 }
24182447
2419− unix_maybe_add_creds(skb, sk, other);
24202448 scm_stat_add(other, skb);
24212449 skb_queue_tail(&other->sk_receive_queue, skb);
24222450 unix_state_unlock(other);
@@ -2564,8 +2592,7 @@ int __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size, · 141 unmodified lines
25642592
25652593 memset(&scm, 0, sizeof(scm));
25662594
2567− scm_set_cred(&scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid);
2568− unix_set_secdata(&scm, skb);
2595+ unix_skb_to_scm(skb, &scm);
25692596
25702597 if (!(flags & MSG_PEEK)) {
25712598 if (UNIXCB(skb).fp)
@@ -2954,8 +2981,7 @@ unlock: · 382 unmodified lines
29542981 break;
29552982 } else if (unix_may_passcred(sk)) {
29562983 /* Copy credentials */
2957− scm_set_cred(&scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid);
2958− unix_set_secdata(&scm, skb);
2984+ unix_skb_to_scm(skb, &scm);
29592985 check_creds = true;
29602986 }
29612987
@@ -3191,7 +3217,6 @@ EXPORT_SYMBOL_GPL(unix_outq_len); · 229 unmodified lines
31913217
31923218 static int unix_open_file(struct sock *sk)
31933219 {
3194− struct path path;
31953220 struct file *f;
31963221 int fd;
31973222
@@ -3201,27 +3226,20 @@ static int unix_open_file(struct sock *sk) · 3 unmodified lines
32013226 if (!smp_load_acquire(&unix_sk(sk)->addr))
32023227 return -ENOENT;
32033228
3204− path = unix_sk(sk)->path;
3205− if (!path.dentry)
3229+ if (!unix_sk(sk)->path.dentry)
32063230 return -ENOENT;
32073231
3208− path_get(&path);
3209−
32103232 fd = get_unused_fd_flags(O_CLOEXEC);
32113233 if (fd < 0)
3212− goto out;
3234+ return fd;
32133235
3214− f = dentry_open(&path, O_PATH, current_cred());
3236+ f = dentry_open(&unix_sk(sk)->path, O_PATH, current_cred());
32153237 if (IS_ERR(f)) {
32163238 put_unused_fd(fd);
3217− fd = PTR_ERR(f);
3218− goto out;
3239+ return PTR_ERR(f);
32193240 }
32203241
32213242 fd_install(fd, f);
3222−out:
3223− path_put(&path);
3224−
32253243 return fd;
32263244 }
32273245
tools/testing/selftests/net/af_unix/scm_pidfd.c+173−44
@@ -15,6 +15,7 @@ · 14 unmodified lines
1515 #include <sys/types.h>
1616 #include <sys/wait.h>
1717
18+#include "../../pidfd/pidfd.h"
1819 #include "../../kselftest_harness.h"
1920
2021 #define clean_errno() (errno == 0 ? "None" : strerror(errno))
@@ -26,6 +27,8 @@ · 5 unmodified lines
2627 #define SCM_PIDFD 0x04
2728 #endif
2829
30+#define CHILD_EXIT_CODE_OK 123
31+
2932 static void child_die()
3033 {
3134 exit(1);
@@ -126,16 +129,65 @@ out: · 94 unmodified lines
126129 return result;
127130 }
128131
132+struct cmsg_data {
133+ struct ucred *ucred;
134+ int *pidfd;
135+};
136+
137+static int parse_cmsg(struct msghdr *msg, struct cmsg_data *res)
138+{
139+ struct cmsghdr *cmsg;
140+ int data = 0;
141+
142+ if (msg->msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
143+ log_err("recvmsg: truncated");
144+ return 1;
145+ }
146+
147+ for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
148+ cmsg = CMSG_NXTHDR(msg, cmsg)) {
149+ if (cmsg->cmsg_level == SOL_SOCKET &&
150+ cmsg->cmsg_type == SCM_PIDFD) {
151+ if (cmsg->cmsg_len < sizeof(*res->pidfd)) {
152+ log_err("CMSG parse: SCM_PIDFD wrong len");
153+ return 1;
154+ }
155+
156+ res->pidfd = (void *)CMSG_DATA(cmsg);
157+ }
158+
159+ if (cmsg->cmsg_level == SOL_SOCKET &&
160+ cmsg->cmsg_type == SCM_CREDENTIALS) {
161+ if (cmsg->cmsg_len < sizeof(*res->ucred)) {
162+ log_err("CMSG parse: SCM_CREDENTIALS wrong len");
163+ return 1;
164+ }
165+
166+ res->ucred = (void *)CMSG_DATA(cmsg);
167+ }
168+ }
169+
170+ if (!res->pidfd) {
171+ log_err("CMSG parse: SCM_PIDFD not found");
172+ return 1;
173+ }
174+
175+ if (!res->ucred) {
176+ log_err("CMSG parse: SCM_CREDENTIALS not found");
177+ return 1;
178+ }
179+
180+ return 0;
181+}
182+
129183 static int cmsg_check(int fd)
130184 {
131185 struct msghdr msg = { 0 };
132− struct cmsghdr *cmsg;
186+ struct cmsg_data res;
133187 struct iovec iov;
134− struct ucred *ucred = NULL;
135188 int data = 0;
136189 char control[CMSG_SPACE(sizeof(struct ucred)) +
137190 CMSG_SPACE(sizeof(int))] = { 0 };
138− int *pidfd = NULL;
139191 pid_t parent_pid;
140192 int err;
141193
@@ -158,53 +210,99 @@ static int cmsg_check(int fd) · 16 unmodified lines
158210 return 1;
159211 }
160212
161− for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
162− cmsg = CMSG_NXTHDR(&msg, cmsg)) {
163− if (cmsg->cmsg_level == SOL_SOCKET &&
164− cmsg->cmsg_type == SCM_PIDFD) {
165− if (cmsg->cmsg_len < sizeof(*pidfd)) {
166− log_err("CMSG parse: SCM_PIDFD wrong len");
167− return 1;
168− }
213+ /* send(pfd, "x", sizeof(char), 0) */
214+ if (data != 'x') {
215+ log_err("recvmsg: data corruption");
216+ return 1;
217+ }
169218
170− pidfd = (void *)CMSG_DATA(cmsg);
171− }
219+ if (parse_cmsg(&msg, &res)) {
220+ log_err("CMSG parse: parse_cmsg() failed");
221+ return 1;
222+ }
172223
173− if (cmsg->cmsg_level == SOL_SOCKET &&
174− cmsg->cmsg_type == SCM_CREDENTIALS) {
175− if (cmsg->cmsg_len < sizeof(*ucred)) {
176− log_err("CMSG parse: SCM_CREDENTIALS wrong len");
177− return 1;
178− }
224+ /* pidfd from SCM_PIDFD should point to the parent process PID */
225+ parent_pid =
226+ get_pid_from_fdinfo_file(*res.pidfd, "Pid:", sizeof("Pid:") - 1);
227+ if (parent_pid != getppid()) {
228+ log_err("wrong SCM_PIDFD %d != %d", parent_pid, getppid());
229+ close(*res.pidfd);
230+ return 1;
231+ }
179232
180− ucred = (void *)CMSG_DATA(cmsg);
181− }
233+ close(*res.pidfd);
234+ return 0;
235+}
236+
237+static int cmsg_check_dead(int fd, int expected_pid)
238+{
239+ int err;
240+ struct msghdr msg = { 0 };
241+ struct cmsg_data res;
242+ struct iovec iov;
243+ int data = 0;
244+ char control[CMSG_SPACE(sizeof(struct ucred)) +
245+ CMSG_SPACE(sizeof(int))] = { 0 };
246+ pid_t client_pid;
247+ struct pidfd_info info = {
248+ .mask = PIDFD_INFO_EXIT,
249+ };
250+
251+ iov.iov_base = &data;
252+ iov.iov_len = sizeof(data);
253+
254+ msg.msg_iov = &iov;
255+ msg.msg_iovlen = 1;
256+ msg.msg_control = control;
257+ msg.msg_controllen = sizeof(control);
258+
259+ err = recvmsg(fd, &msg, 0);
260+ if (err < 0) {
261+ log_err("recvmsg");
262+ return 1;
182263 }
183264
184− /* send(pfd, "x", sizeof(char), 0) */
185− if (data != 'x') {
265+ if (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
266+ log_err("recvmsg: truncated");
267+ return 1;
268+ }
269+
270+ /* send(cfd, "y", sizeof(char), 0) */
271+ if (data != 'y') {
186272 log_err("recvmsg: data corruption");
187273 return 1;
188274 }
189275
190− if (!pidfd) {
191− log_err("CMSG parse: SCM_PIDFD not found");
276+ if (parse_cmsg(&msg, &res)) {
277+ log_err("CMSG parse: parse_cmsg() failed");
192278 return 1;
193279 }
194280
195− if (!ucred) {
196− log_err("CMSG parse: SCM_CREDENTIALS not found");
281+ /*
282+ * pidfd from SCM_PIDFD should point to the client_pid.
283+ * Let's read exit information and check if it's what
284+ * we expect to see.
285+ */
286+ if (ioctl(*res.pidfd, PIDFD_GET_INFO, &info)) {
287+ log_err("%s: ioctl(PIDFD_GET_INFO) failed", __func__);
288+ close(*res.pidfd);
197289 return 1;
198290 }
199291
200− /* pidfd from SCM_PIDFD should point to the parent process PID */
201− parent_pid =
202− get_pid_from_fdinfo_file(*pidfd, "Pid:", sizeof("Pid:") - 1);
203− if (parent_pid != getppid()) {
204− log_err("wrong SCM_PIDFD %d != %d", parent_pid, getppid());
292+ if (!(info.mask & PIDFD_INFO_EXIT)) {
293+ log_err("%s: No exit information from ioctl(PIDFD_GET_INFO)", __func__);
294+ close(*res.pidfd);
205295 return 1;
206296 }
207297
298+ err = WIFEXITED(info.exit_code) ? WEXITSTATUS(info.exit_code) : 1;
299+ if (err != CHILD_EXIT_CODE_OK) {
300+ log_err("%s: wrong exit_code %d != %d", __func__, err, CHILD_EXIT_CODE_OK);
301+ close(*res.pidfd);
302+ return 1;
303+ }
304+
305+ close(*res.pidfd);
208306 return 0;
209307 }
210308
@@ -291,6 +389,24 @@ static void fill_sockaddr(struct sock_addr *addr, bool abstract) · 80 unmodified lines
291389 memcpy(sun_path_buf, addr->sock_name, strlen(addr->sock_name));
292390 }
293391
392+static int sk_enable_cred_pass(int sk)
393+{
394+ int on = 0;
395+
396+ on = 1;
397+ if (setsockopt(sk, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on))) {
398+ log_err("Failed to set SO_PASSCRED");
399+ return 1;
400+ }
401+
402+ if (setsockopt(sk, SOL_SOCKET, SO_PASSPIDFD, &on, sizeof(on))) {
403+ log_err("Failed to set SO_PASSPIDFD");
404+ return 1;
405+ }
406+
407+ return 0;
408+}
409+
294410 static void client(FIXTURE_DATA(scm_pidfd) *self,
295411 const FIXTURE_VARIANT(scm_pidfd) *variant)
296412 {
@@ -299,7 +415,6 @@ static void client(FIXTURE_DATA(scm_pidfd) *self, · 2 unmodified lines
299415 struct ucred peer_cred;
300416 int peer_pidfd;
301417 pid_t peer_pid;
302− int on = 0;
303418
304419 cfd = socket(AF_UNIX, variant->type, 0);
305420 if (cfd < 0) {
@@ -322,14 +437,8 @@ static void client(FIXTURE_DATA(scm_pidfd) *self, · 16 unmodified lines
322437 child_die();
323438 }
324439
325− on = 1;
326− if (setsockopt(cfd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on))) {
327− log_err("Failed to set SO_PASSCRED");
328− child_die();
329− }
330−
331− if (setsockopt(cfd, SOL_SOCKET, SO_PASSPIDFD, &on, sizeof(on))) {
332− log_err("Failed to set SO_PASSPIDFD");
440+ if (sk_enable_cred_pass(cfd)) {
441+ log_err("sk_enable_cred_pass() failed");
333442 child_die();
334443 }
335444
@@ -340,6 +449,12 @@ static void client(FIXTURE_DATA(scm_pidfd) *self, · 4 unmodified lines
340449 child_die();
341450 }
342451
452+ /* send something to the parent so it can receive SCM_PIDFD too and validate it */
453+ if (send(cfd, "y", sizeof(char), 0) == -1) {
454+ log_err("Failed to send(cfd, \"y\", sizeof(char), 0)");
455+ child_die();
456+ }
457+
343458 /* skip further for SOCK_DGRAM as it's not applicable */
344459 if (variant->type == SOCK_DGRAM)
345460 return;
@@ -398,7 +513,13 @@ TEST_F(scm_pidfd, test) · 52 unmodified lines
398513 close(self->server);
399514 close(self->startup_pipe[0]);
400515 client(self, variant);
401− exit(0);
516+
517+ /*
518+ * It's a bit unusual, but in case of success we return non-zero
519+ * exit code (CHILD_EXIT_CODE_OK) and then we expect to read it
520+ * from ioctl(PIDFD_GET_INFO) in cmsg_check_dead().
521+ */
522+ exit(CHILD_EXIT_CODE_OK);
402523 }
403524 close(self->startup_pipe[1]);
404525
@@ -421,9 +542,17 @@ TEST_F(scm_pidfd, test) · 16 unmodified lines
421542 ASSERT_NE(-1, err);
422543 }
423544
424− close(pfd);
425545 waitpid(self->client_pid, &child_status, 0);
426− ASSERT_EQ(0, WIFEXITED(child_status) ? WEXITSTATUS(child_status) : 1);
546+ /* see comment before exit(CHILD_EXIT_CODE_OK) */
547+ ASSERT_EQ(CHILD_EXIT_CODE_OK, WIFEXITED(child_status) ? WEXITSTATUS(child_status) : 1);
548+
549+ err = sk_enable_cred_pass(pfd);
550+ ASSERT_EQ(0, err);
551+
552+ err = cmsg_check_dead(pfd, self->client_pid);
553+ ASSERT_EQ(0, err);
554+
555+ close(pfd);
427556 }
428557
429558 TEST_HARNESS_MAIN
tools/testing/selftests/pidfd/.gitignore+2−0
@@ -10,3 +10,5 @@ pidfd_file_handle_test · 9 unmodified lines
1010 pidfd_bind_mount
1111 pidfd_info_test
1212 pidfd_exec_helper
13+pidfd_xattr_test
14+pidfd_setattr_test
tools/testing/selftests/pidfd/Makefile+3−2
@@ -1,9 +1,10 @@
11 # SPDX-License-Identifier: GPL-2.0-only
2−CFLAGS += -g $(KHDR_INCLUDES) -pthread -Wall
2+CFLAGS += -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES) -pthread -Wall
33
44 TEST_GEN_PROGS := pidfd_test pidfd_fdinfo_test pidfd_open_test \
55 pidfd_poll_test pidfd_wait pidfd_getfd_test pidfd_setns_test \
6− pidfd_file_handle_test pidfd_bind_mount pidfd_info_test
6+ pidfd_file_handle_test pidfd_bind_mount pidfd_info_test \
7+ pidfd_xattr_test pidfd_setattr_test
78
89 TEST_GEN_PROGS_EXTENDED := pidfd_exec_helper
910
tools/testing/selftests/pidfd/pidfd.h+5−1
@@ -19,6 +19,10 @@ · 18 unmodified lines
1919 #include "../kselftest.h"
2020 #include "../clone3/clone3_selftests.h"
2121
22+#ifndef FD_PIDFS_ROOT
23+#define FD_PIDFS_ROOT -10002
24+#endif
25+
2226 #ifndef P_PIDFD
2327 #define P_PIDFD 3
2428 #endif
@@ -56,7 +60,7 @@ · 31 unmodified lines
5660 #endif
5761
5862 #ifndef PIDFD_SELF_THREAD_GROUP
59−#define PIDFD_SELF_THREAD_GROUP -20000 /* Current thread group leader. */
63+#define PIDFD_SELF_THREAD_GROUP -10001 /* Current thread group leader. */
6064 #endif
6165
6266 #ifndef PIDFD_SELF
tools/testing/selftests/pidfd/pidfd_file_handle_test.c+60−0
@@ -500,4 +500,64 @@ TEST_F(file_handle, valid_name_to_handle_at_flags) · 499 unmodified lines
500500 ASSERT_EQ(close(pidfd), 0);
501501 }
502502
503+/*
504+ * That we decode a file handle without having to pass a pidfd.
505+ */
506+TEST_F(file_handle, decode_purely_based_on_file_handle)
507+{
508+ int mnt_id;
509+ struct file_handle *fh;
510+ int pidfd = -EBADF;
511+ struct stat st1, st2;
512+
513+ fh = malloc(sizeof(struct file_handle) + MAX_HANDLE_SZ);
514+ ASSERT_NE(fh, NULL);
515+ memset(fh, 0, sizeof(struct file_handle) + MAX_HANDLE_SZ);
516+ fh->handle_bytes = MAX_HANDLE_SZ;
517+
518+ ASSERT_EQ(name_to_handle_at(self->child_pidfd1, "", fh, &mnt_id, AT_EMPTY_PATH), 0);
519+
520+ ASSERT_EQ(fstat(self->child_pidfd1, &st1), 0);
521+
522+ pidfd = open_by_handle_at(FD_PIDFS_ROOT, fh, 0);
523+ ASSERT_GE(pidfd, 0);
524+
525+ ASSERT_EQ(fstat(pidfd, &st2), 0);
526+ ASSERT_TRUE(st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino);
527+
528+ ASSERT_EQ(close(pidfd), 0);
529+
530+ pidfd = open_by_handle_at(FD_PIDFS_ROOT, fh, O_CLOEXEC);
531+ ASSERT_GE(pidfd, 0);
532+
533+ ASSERT_EQ(fstat(pidfd, &st2), 0);
534+ ASSERT_TRUE(st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino);
535+
536+ ASSERT_EQ(close(pidfd), 0);
537+
538+ pidfd = open_by_handle_at(FD_PIDFS_ROOT, fh, O_NONBLOCK);
539+ ASSERT_GE(pidfd, 0);
540+
541+ ASSERT_EQ(fstat(pidfd, &st2), 0);
542+ ASSERT_TRUE(st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino);
543+
544+ ASSERT_EQ(close(pidfd), 0);
545+
546+ pidfd = open_by_handle_at(self->pidfd, fh, 0);
547+ ASSERT_GE(pidfd, 0);
548+
549+ ASSERT_EQ(fstat(pidfd, &st2), 0);
550+ ASSERT_TRUE(st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino);
551+
552+ ASSERT_EQ(close(pidfd), 0);
553+
554+ pidfd = open_by_handle_at(-EBADF, fh, 0);
555+ ASSERT_LT(pidfd, 0);
556+
557+ pidfd = open_by_handle_at(AT_FDCWD, fh, 0);
558+ ASSERT_LT(pidfd, 0);
559+
560+ free(fh);
561+}
562+
503563 TEST_HARNESS_MAIN
tools/testing/selftests/pidfd/pidfd_setattr_test.c+69−0
@@ -0,0 +1,69 @@
1+// SPDX-License-Identifier: GPL-2.0
2+
3+#define _GNU_SOURCE
4+#include <errno.h>
5+#include <fcntl.h>
6+#include <limits.h>
7+#include <linux/types.h>
8+#include <poll.h>
9+#include <pthread.h>
10+#include <sched.h>
11+#include <signal.h>
12+#include <stdio.h>
13+#include <stdlib.h>
14+#include <string.h>
15+#include <syscall.h>
16+#include <sys/prctl.h>
17+#include <sys/wait.h>
18+#include <unistd.h>
19+#include <sys/socket.h>
20+#include <linux/kcmp.h>
21+#include <sys/stat.h>
22+#include <sys/xattr.h>
23+
24+#include "pidfd.h"
25+#include "../kselftest_harness.h"
26+
27+FIXTURE(pidfs_setattr)
28+{
29+ pid_t child_pid;
30+ int child_pidfd;
31+};
32+
33+FIXTURE_SETUP(pidfs_setattr)
34+{
35+ self->child_pid = create_child(&self->child_pidfd, CLONE_NEWUSER | CLONE_NEWPID);
36+ EXPECT_GE(self->child_pid, 0);
37+
38+ if (self->child_pid == 0)
39+ _exit(EXIT_SUCCESS);
40+}
41+
42+FIXTURE_TEARDOWN(pidfs_setattr)
43+{
44+ sys_waitid(P_PID, self->child_pid, NULL, WEXITED);
45+ EXPECT_EQ(close(self->child_pidfd), 0);
46+}
47+
48+TEST_F(pidfs_setattr, no_chown)
49+{
50+ ASSERT_LT(fchown(self->child_pidfd, 1234, 5678), 0);
51+ ASSERT_EQ(errno, EOPNOTSUPP);
52+}
53+
54+TEST_F(pidfs_setattr, no_chmod)
55+{
56+ ASSERT_LT(fchmod(self->child_pidfd, 0777), 0);
57+ ASSERT_EQ(errno, EOPNOTSUPP);
58+}
59+
60+TEST_F(pidfs_setattr, no_exec)
61+{
62+ char *const argv[] = { NULL };
63+ char *const envp[] = { NULL };
64+
65+ ASSERT_LT(execveat(self->child_pidfd, "", argv, envp, AT_EMPTY_PATH), 0);
66+ ASSERT_EQ(errno, EACCES);
67+}
68+
69+TEST_HARNESS_MAIN
tools/testing/selftests/pidfd/pidfd_xattr_test.c+132−0
@@ -0,0 +1,132 @@
1+// SPDX-License-Identifier: GPL-2.0
2+
3+#define _GNU_SOURCE
4+#include <errno.h>
5+#include <fcntl.h>
6+#include <limits.h>
7+#include <linux/types.h>
8+#include <poll.h>
9+#include <pthread.h>
10+#include <sched.h>
11+#include <signal.h>
12+#include <stdio.h>
13+#include <stdlib.h>
14+#include <string.h>
15+#include <syscall.h>
16+#include <sys/prctl.h>
17+#include <sys/wait.h>
18+#include <unistd.h>
19+#include <sys/socket.h>
20+#include <linux/kcmp.h>
21+#include <sys/stat.h>
22+#include <sys/xattr.h>
23+
24+#include "pidfd.h"
25+#include "../kselftest_harness.h"
26+
27+FIXTURE(pidfs_xattr)
28+{
29+ pid_t child_pid;
30+ int child_pidfd;
31+};
32+
33+FIXTURE_SETUP(pidfs_xattr)
34+{
35+ self->child_pid = create_child(&self->child_pidfd, CLONE_NEWUSER | CLONE_NEWPID);
36+ EXPECT_GE(self->child_pid, 0);
37+
38+ if (self->child_pid == 0)
39+ _exit(EXIT_SUCCESS);
40+}
41+
42+FIXTURE_TEARDOWN(pidfs_xattr)
43+{
44+ sys_waitid(P_PID, self->child_pid, NULL, WEXITED);
45+}
46+
47+TEST_F(pidfs_xattr, set_get_list_xattr_multiple)
48+{
49+ int ret, i;
50+ char xattr_name[32];
51+ char xattr_value[32];
52+ char buf[32];
53+ const int num_xattrs = 10;
54+ char list[PATH_MAX] = {};
55+
56+ for (i = 0; i < num_xattrs; i++) {
57+ snprintf(xattr_name, sizeof(xattr_name), "trusted.testattr%d", i);
58+ snprintf(xattr_value, sizeof(xattr_value), "testvalue%d", i);
59+ ret = fsetxattr(self->child_pidfd, xattr_name, xattr_value, strlen(xattr_value), 0);
60+ ASSERT_EQ(ret, 0);
61+ }
62+
63+ for (i = 0; i < num_xattrs; i++) {
64+ snprintf(xattr_name, sizeof(xattr_name), "trusted.testattr%d", i);
65+ snprintf(xattr_value, sizeof(xattr_value), "testvalue%d", i);
66+ memset(buf, 0, sizeof(buf));
67+ ret = fgetxattr(self->child_pidfd, xattr_name, buf, sizeof(buf));
68+ ASSERT_EQ(ret, strlen(xattr_value));
69+ ASSERT_EQ(strcmp(buf, xattr_value), 0);
70+ }
71+
72+ ret = flistxattr(self->child_pidfd, list, sizeof(list));
73+ ASSERT_GT(ret, 0);
74+ for (i = 0; i < num_xattrs; i++) {
75+ snprintf(xattr_name, sizeof(xattr_name), "trusted.testattr%d", i);
76+ bool found = false;
77+ for (char *it = list; it < list + ret; it += strlen(it) + 1) {
78+ if (strcmp(it, xattr_name))
79+ continue;
80+ found = true;
81+ break;
82+ }
83+ ASSERT_TRUE(found);
84+ }
85+
86+ for (i = 0; i < num_xattrs; i++) {
87+ snprintf(xattr_name, sizeof(xattr_name), "trusted.testattr%d", i);
88+ ret = fremovexattr(self->child_pidfd, xattr_name);
89+ ASSERT_EQ(ret, 0);
90+
91+ ret = fgetxattr(self->child_pidfd, xattr_name, buf, sizeof(buf));
92+ ASSERT_EQ(ret, -1);
93+ ASSERT_EQ(errno, ENODATA);
94+ }
95+}
96+
97+TEST_F(pidfs_xattr, set_get_list_xattr_persistent)
98+{
99+ int ret;
100+ char buf[32];
101+ char list[PATH_MAX] = {};
102+
103+ ret = fsetxattr(self->child_pidfd, "trusted.persistent", "persistent value", strlen("persistent value"), 0);
104+ ASSERT_EQ(ret, 0);
105+
106+ memset(buf, 0, sizeof(buf));
107+ ret = fgetxattr(self->child_pidfd, "trusted.persistent", buf, sizeof(buf));
108+ ASSERT_EQ(ret, strlen("persistent value"));
109+ ASSERT_EQ(strcmp(buf, "persistent value"), 0);
110+
111+ ret = flistxattr(self->child_pidfd, list, sizeof(list));
112+ ASSERT_GT(ret, 0);
113+ ASSERT_EQ(strcmp(list, "trusted.persistent"), 0)
114+
115+ ASSERT_EQ(close(self->child_pidfd), 0);
116+ self->child_pidfd = -EBADF;
117+ sleep(2);
118+
119+ self->child_pidfd = sys_pidfd_open(self->child_pid, 0);
120+ ASSERT_GE(self->child_pidfd, 0);
121+
122+ memset(buf, 0, sizeof(buf));
123+ ret = fgetxattr(self->child_pidfd, "trusted.persistent", buf, sizeof(buf));
124+ ASSERT_EQ(ret, strlen("persistent value"));
125+ ASSERT_EQ(strcmp(buf, "persistent value"), 0);
126+
127+ ret = flistxattr(self->child_pidfd, list, sizeof(list));
128+ ASSERT_GT(ret, 0);
129+ ASSERT_EQ(strcmp(list, "trusted.persistent"), 0);
130+}
131+
132+TEST_HARNESS_MAIN