Re: [PATCH 1/2] perf: look up address filter files without ctx->mutex held
From: Peter Zijlstra
Date: Fri Nov 27 2026 - 06:57:58 EST
On Fri, Nov 27, 2026 at 10:45:28AM +0100, Christian Brauner wrote:
> > @@ -12310,11 +12361,8 @@ perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
> >
> > ret = -EINVAL;
> > if (!filter->path.dentry ||
> > - !S_ISREG(d_inode(filter->path.dentry)
> > - ->i_mode))
> > + !S_ISREG(d_inode(filter->path.dentry)->i_mode))
> > goto fail;
>
> Just a thought on this. In vfs-7.5.lookup I have:
>
> file_check_backing() which is the new default way to figure out whether
> something can be a backing file for a given subsystem. Its checks are:
>
> int file_check_backing(const struct file *file, fmode_t mode, umode_t type)
> {
> fmode_t io = mode & (FMODE_READ | FMODE_WRITE);
> /* Drivers may point ->f_mapping at something else. */
> umode_t fmt = file_inode(file)->i_mode & S_IFMT;
> struct inode *real_inode;
>
> /* O_PATH files are only fine for callers that just use the path. */
> if ((file->f_mode & FMODE_PATH) && !(mode & FMODE_PATH))
> return -EBADF;
>
> if ((file->f_mode & io) != io)
> return -EBADF;
>
> if (fmt != S_IFREG && !(type == S_IFBLK && fmt == S_IFBLK))
> return -EINVAL;
>
> /* Look through stacking filesystems at the inode with the data. */
> real_inode = d_real_data_inode(file->f_path.dentry);
> if (IS_ERR(real_inode))
> return PTR_ERR(real_inode);
> if (real_inode->i_sb->s_iflags & SB_I_PSEUDO)
> return -EINVAL;
>
> if ((mode & FMODE_READ) &&
> (!(file->f_mode & FMODE_CAN_READ) || !file->f_op->read_iter))
> return -EINVAL;
>
> if ((mode & FMODE_WRITE) &&
> (!(file->f_mode & FMODE_CAN_WRITE) || !file->f_op->write_iter))
> return -EINVAL;
>
> return 0;
> }
>
> It excludes anything that is a pseudo filesystem:
>
> arch/powerpc/platforms/cell/spufs/inode.c: fc_init_pseudo(fc);
> arch/s390/hypfs/inode.c: fc_init_pseudo(fc);
> drivers/android/binder/rust_binderfs.c: fc_init_pseudo(fc);
> drivers/usb/gadget/function/f_fs.c: fc_init_pseudo(fc);
> drivers/usb/gadget/legacy/inode.c: fc_init_pseudo(fc);
> drivers/xen/xenfs/super.c: fc_init_pseudo(fc);
> fs/binfmt_misc.c: fc_init_pseudo(fc);
> fs/configfs/mount.c: fc_init_pseudo(fc);
> fs/debugfs/inode.c: fc_init_pseudo(fc);
> fs/devpts/inode.c: fc_init_pseudo(fc);
> fs/efivarfs/super.c: fc_init_pseudo(fc);
> fs/failfs.c: fc_init_pseudo(fc);
> fs/fuse/control.c: fc_init_pseudo(fsc);
> fs/kernfs/mount.c: fc_init_pseudo(fc);
> fs/libfs.c: fc_init_pseudo(fc);
> fs/nfsd/nfsctl.c: fc_init_pseudo(fc);
> fs/nullfs.c: fc_init_pseudo(fc);
> fs/ocfs2/dlmfs/dlmfs.c: fc_init_pseudo(fc);
> fs/openpromfs/inode.c: fc_init_pseudo(fc);
> fs/proc/root.c: fc_init_pseudo(fc);
> fs/pstore/inode.c: fc_init_pseudo(fc);
> fs/tracefs/inode.c: fc_init_pseudo(fc);
> include/linux/pseudo_fs.h:static inline void fc_init_pseudo(struct fs_context *fc)
> ipc/mqueue.c: fc_init_pseudo(fc);
> kernel/bpf/inode.c: fc_init_pseudo(fc);
> net/sunrpc/rpc_pipe.c: fc_init_pseudo(fc);
> security/apparmor/apparmorfs.c: fc_init_pseudo(fc);
> security/inode.c: fc_init_pseudo(fc);
> security/selinux/selinuxfs.c: fc_init_pseudo(fc);
> security/smack/smackfs.c: fc_init_pseudo(fc);
>
> I don't think anything on this list is something that you would ever
> want to use as an object file?
Very very possibly people would like /proc/kcore to self reference or
something, but I'm not sure that's a thing. Adrian?
> I think we should do an O_PATH open of the target file and then you
> call:
>
> file = filp_open(filename, O_PATH | O_REGULAR, 0)
>
> if (file_check_backing(file, FMODE_PATH, 0))
> return -EINVAL;
We must close file, or does it have __free(file) on or something?
> We had a whole bug class where various subsystems that used backing
> files just allowed arbitrary files to be used.
This seems fine to me and can go in independent of this locking stuff.