Skip to content

Commit 47a1fbc

Browse files
committed
pidfs: convert to path_from_stashed() helper
Moving pidfds from the anonymous inode infrastructure to a separate tiny in-kernel filesystem similar to sockfs, pipefs, and anon_inodefs causes Selinux denials and thus various userspace components that make heavy use of pidfds to fail if we would be using dentry_open() directly. pidfds used anon_inode_getfile() which never was subject to any LSM hooks. But dentry_open() is and that would cause regressions: Feb 23 12:09:58 fed1 audit[353]: AVC avc: denied { read write open } for pid=353 comm="systemd-userdbd" path="pidfd:[709]" dev="pidfs" ino=709 scontext=system_u:system_r:systemd_userdbd_t:> The failures that are seen are selinux denials. But the core failure is dbus-broker. That cascades into other services failing that depend on dbus-broker. For example, when dbus-broker fails to start polkit and all the others won't be able to work because they depend on dbus-broker. The reason for dbus-broker failing is because it doesn't handle failures for SO_PEERPIDFD correctly. Last kernel release (either v6.7 or v6.6, I'm not completely sure right now) we introduced SO_PEERPIDFD (and SCM_PIDFD). SO_PEERPIDFD allows dbus-broker and polkit and others to receive a pidfd for the peer of an AF_UNIX socket. This is the first time in the history of Linux that we can safely authenticate clients in a race-free manner. dbus-broker immediately made use of this but messed up the error checking. It only allowed EINVAL as a valid failure for SO_PEERPIDFD. That's obviously problematic not just because of LSM denials but because of seccomp denials that would prevent SO_PEERPIDFD from working; or any other new error code from there. So this is catching a flawed implementation in dbus-broker as well. It has to fallback to the old pid-based authentication when SO_PEERPIDFD doesn't work no matter the reasons otherwise it'll always risk such failures. So overall that LSM denial should not have caused dbus-broker to fail. It can never assume that a feature released one kernel ago like SO_PEERPIDFD can be assumed to be available. So, the next fix separate from the selinux policy update is to fix dbus-broker at [3]. That should make it into Fedora as well. In addition the selinux reference policy should also be updated. See [4] for that. If Selinux is in enforcing mode in userspace and it encounters anything that it doesn't know about it will deny it by default. And the policy is entirely in userspace including declaring new types for stuff like nsfs or pidfs to allow it. There's just nothing to do in the kernel so we can't use dentry_open(). So instead we use alloc_file(). Once selinux is ready we can switch to dentry_open() or we introduce separate LSM hook for pidfds. Link: https://bugzilla.redhat.com/show_bug.cgi?id=2265630 [1] Link: fedora-selinux/selinux-policy#2050 [2] Link: bus1/dbus-broker#343 [3] Link: SELinuxProject/refpolicy#762 [4] Reported-by: Nathan Chancellor <nathan@kernel.org> Link: https://lore.kernel.org/r/20240222190334.GA412503@dev-arch.thelio-3990X Link: https://lore.kernel.org/r/20240218-neufahrzeuge-brauhaus-fb0eb6459771@brauner Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent ef86bcd commit 47a1fbc

File tree

7 files changed

+50
-29
lines changed

7 files changed

+50
-29
lines changed

fs/file_table.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ struct file *alloc_empty_backing_file(int flags, const struct cred *cred)
282282
* @flags: O_... flags with which the new file will be opened
283283
* @fop: the 'struct file_operations' for the new file
284284
*/
285-
static struct file *alloc_file(const struct path *path, int flags,
285+
struct file *alloc_file(const struct path *path, int flags,
286286
const struct file_operations *fop)
287287
{
288288
struct file *file;

fs/internal.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,7 @@ struct mnt_idmap *mnt_idmap_get(struct mnt_idmap *idmap);
312312
void mnt_idmap_put(struct mnt_idmap *idmap);
313313
int path_from_stashed(struct dentry **stashed, unsigned long ino,
314314
struct vfsmount *mnt, const struct file_operations *fops,
315-
void *data, struct path *path);
315+
const struct inode_operations *iops, void *data,
316+
struct path *path);
317+
struct file *alloc_file(const struct path *path, int flags,
318+
const struct file_operations *fop);

fs/libfs.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,6 +1990,7 @@ static inline struct dentry *get_stashed_dentry(struct dentry *stashed)
19901990
static struct dentry *stash_dentry(struct dentry **stashed, unsigned long ino,
19911991
struct super_block *sb,
19921992
const struct file_operations *fops,
1993+
const struct inode_operations *iops,
19931994
void *data)
19941995
{
19951996
struct dentry *dentry;
@@ -2008,7 +2009,10 @@ static struct dentry *stash_dentry(struct dentry **stashed, unsigned long ino,
20082009
inode->i_ino = ino;
20092010
inode->i_flags |= S_IMMUTABLE;
20102011
inode->i_mode = S_IFREG | S_IRUGO;
2011-
inode->i_fop = fops;
2012+
if (iops)
2013+
inode->i_op = iops;
2014+
if (fops)
2015+
inode->i_fop = fops;
20122016
inode->i_private = data;
20132017
simple_inode_init_ts(inode);
20142018

@@ -2030,6 +2034,7 @@ static struct dentry *stash_dentry(struct dentry **stashed, unsigned long ino,
20302034
* @stashed: where to retrieve or stash dentry
20312035
* @ino: inode number to use
20322036
* @mnt: mnt of the filesystems to use
2037+
* @iops: inode operations to use
20332038
* @fops: file operations to use
20342039
* @data: data to store in inode->i_private
20352040
* @path: path to create
@@ -2048,7 +2053,8 @@ static struct dentry *stash_dentry(struct dentry **stashed, unsigned long ino,
20482053
*/
20492054
int path_from_stashed(struct dentry **stashed, unsigned long ino,
20502055
struct vfsmount *mnt, const struct file_operations *fops,
2051-
void *data, struct path *path)
2056+
const struct inode_operations *iops, void *data,
2057+
struct path *path)
20522058
{
20532059
struct dentry *dentry;
20542060
int ret = 0;
@@ -2057,7 +2063,7 @@ int path_from_stashed(struct dentry **stashed, unsigned long ino,
20572063
if (dentry)
20582064
goto out_path;
20592065

2060-
dentry = stash_dentry(stashed, ino, mnt->mnt_sb, fops, data);
2066+
dentry = stash_dentry(stashed, ino, mnt->mnt_sb, fops, iops, data);
20612067
if (IS_ERR(dentry))
20622068
return PTR_ERR(dentry);
20632069
ret = 1;

fs/nsfs.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ int ns_get_path_cb(struct path *path, ns_get_path_helper_t *ns_get_cb,
6767
if (!ns)
6868
return -ENOENT;
6969
ret = path_from_stashed(&ns->stashed, ns->inum, nsfs_mnt,
70-
&ns_file_operations, ns, path);
70+
&ns_file_operations, NULL, ns, path);
7171
if (ret <= 0 && ret != -EAGAIN)
7272
ns->ops->put(ns);
7373
} while (ret == -EAGAIN);
@@ -122,8 +122,9 @@ int open_related_ns(struct ns_common *ns,
122122
return PTR_ERR(relative);
123123
}
124124

125-
err = path_from_stashed(&relative->stashed, relative->inum, nsfs_mnt,
126-
&ns_file_operations, relative, &path);
125+
err = path_from_stashed(&relative->stashed, relative->inum,
126+
nsfs_mnt, &ns_file_operations, NULL,
127+
relative, &path);
127128
if (err <= 0 && err != -EAGAIN)
128129
relative->ops->put(relative);
129130
} while (err == -EAGAIN);

fs/pidfs.c

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <linux/seq_file.h>
1515
#include <uapi/linux/pidfd.h>
1616

17+
#include "internal.h"
18+
1719
static int pidfd_release(struct inode *inode, struct file *file)
1820
{
1921
#ifndef CONFIG_FS_PID
@@ -186,9 +188,21 @@ static char *pidfs_dname(struct dentry *dentry, char *buffer, int buflen)
186188
d_inode(dentry)->i_ino);
187189
}
188190

191+
static void pidfdfs_prune_dentry(struct dentry *dentry)
192+
{
193+
struct inode *inode;
194+
195+
inode = d_inode(dentry);
196+
if (inode) {
197+
struct pid *pid = inode->i_private;
198+
WRITE_ONCE(pid->stashed, NULL);
199+
}
200+
}
201+
189202
static const struct dentry_operations pidfs_dentry_operations = {
190203
.d_delete = always_delete_dentry,
191204
.d_dname = pidfs_dname,
205+
.d_prune = pidfdfs_prune_dentry,
192206
};
193207

194208
static int pidfs_init_fs_context(struct fs_context *fc)
@@ -213,29 +227,24 @@ static struct file_system_type pidfs_type = {
213227
struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags)
214228
{
215229

216-
struct inode *inode;
217230
struct file *pidfd_file;
218-
219-
inode = iget_locked(pidfs_sb, pid->ino);
220-
if (!inode)
221-
return ERR_PTR(-ENOMEM);
222-
223-
if (inode->i_state & I_NEW) {
224-
inode->i_ino = pid->ino;
225-
inode->i_mode = S_IFREG | S_IRUGO;
226-
inode->i_op = &pidfs_inode_operations;
227-
inode->i_fop = &pidfs_file_operations;
228-
inode->i_flags |= S_IMMUTABLE;
229-
inode->i_private = get_pid(pid);
230-
simple_inode_init_ts(inode);
231-
unlock_new_inode(inode);
232-
}
233-
234-
pidfd_file = alloc_file_pseudo(inode, pidfs_mnt, "", flags,
235-
&pidfs_file_operations);
231+
struct path path;
232+
int ret;
233+
234+
do {
235+
ret = path_from_stashed(&pid->stashed, pid->ino, pidfs_mnt,
236+
&pidfs_file_operations,
237+
&pidfs_inode_operations, get_pid(pid),
238+
&path);
239+
if (ret <= 0 && ret != -EAGAIN)
240+
put_pid(pid);
241+
} while (ret == -EAGAIN);
242+
if (ret < 0)
243+
return ERR_PTR(ret);
244+
245+
pidfd_file = alloc_file(&path, flags, &pidfs_file_operations);
236246
if (IS_ERR(pidfd_file))
237-
iput(inode);
238-
247+
path_put(&path);
239248
return pidfd_file;
240249
}
241250

include/linux/pid.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ struct pid
5656
unsigned int level;
5757
spinlock_t lock;
5858
#ifdef CONFIG_FS_PID
59+
struct dentry *stashed;
5960
unsigned long ino;
6061
#endif
6162
/* lists of tasks that use this pid */

kernel/pid.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid,
277277
if (!(ns->pid_allocated & PIDNS_ADDING))
278278
goto out_unlock;
279279
#ifdef CONFIG_FS_PID
280+
pid->stashed = NULL;
280281
pid->ino = ++pidfs_ino;
281282
#endif
282283
for ( ; upid >= pid->numbers; --upid) {

0 commit comments

Comments
 (0)