From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D5EDF606A891 for ; Tue, 17 Nov 2026 08:44:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amutable-com.20260707.gappssmtp.com header.i=@amutable-com.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=P3LiNfxF Received: from boromir.ozlabs.org (localhost [127.0.0.1]) by lists.ozlabs.org (Postfix) with ESMTP id 4jTJFW3WnLz2yD8; Tue, 17 Nov 2026 19:44:47 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; arc=none smtp.remote-ip="2a00:1450:4864:20::332" ARC-Seal: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1794905087; cv=none; b=Xws1nbpBv1EW11C6lU+w9d5Jvn9MTMCEY+y7Fh3AjK93mEg2eENdizt/BtjfLxpDNhRs2/VesL8CbE8uw6Mq2hqk+D9eLjqwKCCj/ZN8xYzNYdVX78e27YRr7KE4rLX6Y3aP7rCBC2n+mahBvtO0nSDFyUG+3vx20PX1TirVBaIo1ZkNa4G1HZJNi0FQtOsL3dT/PfTLB8YPL0fkOUWu4zbMNN8TZWbDNViXsRfDlVteL3rAGPIW16GiY9L4HFFBTc6MRvbcn5pw70Mhgx7ctPeHiqFzCjZX0SbPhADhHGIiFa+6HltlLUIHM9B/H+PAKjj1M+E4cxJ50BmOlDs4AA== ARC-Message-Signature: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1794905087; c=relaxed/relaxed; bh=YtgOPMlBsDbDRTTami0iaeC2PfdAlPMUJ3BhZBWqnDY=; h=Message-ID:Subject:From:To:Cc:Date:In-Reply-To:References: Content-Type:MIME-Version; b=E/LiNURIVB3QGa5U1F17UWFDjDF48tRn4rv4RYVJfroTVXGdnMXSXoMb+KXlAQeORHSwh/+blC+cAajihb3VSKE9KeiTDThDf1J50v70jlEaICq2qffzJKFNgHh9Z3aZhcOxebOW5uhN3j1ls7odeL5utb558lYVEcY5lBaJU3JcNbyqjuL53dX4Ox3CByuNvuEhFa+oo95KbiwQNNSFSrG7qenT6RVViNAXBLvI+HLVb3GI5/s6OF7Sgn9Ty6DCHAoV93xKx6SMncqQ7WpDyMF8pbWyD91IrUEz6oA6APJDk2xIYsiTmF2mRx7adzc3Gn/dj1aDO0a2diiESiXMDg== ARC-Authentication-Results: i=1; lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=amutable.com; dkim=pass (2048-bit key; unprotected) header.d=amutable-com.20260707.gappssmtp.com header.i=@amutable-com.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=P3LiNfxF; dkim-atps=neutral; spf=pass (client-ip=2a00:1450:4864:20::332; helo=mail-wm1-x332.google.com; envelope-from=alex@amutable.com; receiver=lists.ozlabs.org) smtp.mailfrom=amutable.com Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=amutable.com Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=amutable-com.20260707.gappssmtp.com header.i=@amutable-com.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=P3LiNfxF; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=amutable.com (client-ip=2a00:1450:4864:20::332; helo=mail-wm1-x332.google.com; envelope-from=alex@amutable.com; receiver=lists.ozlabs.org) Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4jTJFT5BW9z2y8m for ; Tue, 17 Nov 2026 19:44:44 +1100 (AEDT) Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-4a1fbd52ab6so33721795e9.0 for ; Tue, 17 Nov 2026 00:44:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amutable-com.20260707.gappssmtp.com; s=20260707; t=1794905081; x=1795509881; darn=lists.ozlabs.org; h=mime-version:user-agent:content-transfer-encoding:content-type :references:in-reply-to:date:cc:to:from:subject:message-id:from:to :cc:subject:date:message-id:reply-to:content-type; bh=YtgOPMlBsDbDRTTami0iaeC2PfdAlPMUJ3BhZBWqnDY=; b=P3LiNfxFr10TvpvrB9B1TB6EGOJ0cq8WmNNOpTFGJAOK3l9aw84e4cc1tMmrAHTmo+ hRGm0hjEbgxCy4yQluruH43yY8REAaIkeLho25ayanEeGi9lRtSdIys52bnTfK6WYk17 UA9R8K6jPxTQxYNFGixv1wz9H7Yigsf1teJ723H/e8cj9T5SDF2xI9cxzD31dwkfhw3l SbVO63Z/hMwJFkJrpwXfLzwyFALCMWjIgw9qRQs7egJ9Xq7krHPkd2ow6FnqoMkOXxM7 wFzyYaJSMx3Wlp+zly/EDzGMvnAtq1XfD+mHUBU8345i5VqZK9qXjxsuwPDhs0jfdGj+ Dkzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794905081; x=1795509881; h=mime-version:user-agent:content-transfer-encoding:content-type :references:in-reply-to:date:cc:to:from:subject:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=YtgOPMlBsDbDRTTami0iaeC2PfdAlPMUJ3BhZBWqnDY=; b=G7Ib9XH6GlMt5DuxukF7OjAVrq1sx6iBS8KlsmZULHO2Fd7h/4ycwdtlgSGaN5yPB/ XRbNCUNGOuUzNyomDk27YbbTosFqMRgxYpr6XSQXTOQK+8TUVfYPKmhPJfReqCmF4j4b GLC8Sx5wPr+9PjAcfbFlk8v6vJZu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kBFFPZ9Pnuah5Jg9qiWt+7s8HdCoHh9Yoa+asPHm 44VttuYNw/9hstzCPIKt/ababyeFeLjgbVgFJzVBW80W4heN0NRB5X6hkEcOZkd6tkYo70tkH9S tdtw== X-Received: by 2002:a05:6000:2411:b0:48f:f26d:c5c with SMTP id ffacd0b85a97d-49c70b051d1mr27471178f8f.9.1794905081269; Tue, 17 Nov 2026 00:44:41 -0800 (PST) Received: from ?IPv6:2003:cf:572c:2800:de4c:6789:33ce:97c2? (p200300cf572c2800de4c678933ce97c2.dip0.t-ipconnect.de. [2003:cf:572c:2800:de4c:6789:33ce:97c2]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-49d673ce75fsm19982260f8f.55.2026.11.17.00.44.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Nov 2026 00:44:40 -0800 (PST) Message-ID: Subject: Re: [PATCH RFC v3 35/44] fs: add file_check_backing() From: Alexander Mikhalitsyn To: Christian Brauner , linux-fsdevel@vger.kernel.org Cc: Jann Horn , Chris Mason , Josef Bacik , Christoph Hellwig , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-nvme@lists.infradead.org, Alexander Viro , Jan Kara , linux-scsi@vger.kernel.org, target-devel@vger.kernel.org, driver-core@lists.linux.dev, linux-mm@kvack.org, bpf@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, apparmor@lists.ubuntu.com, linux-nfs@vger.kernel.org, netdev@vger.kernel.org, fuse-devel@lists.linux.dev, linux-efi@vger.kernel.org, xen-devel@lists.xenproject.org, ocfs2-devel@lists.linux.dev, linux-s390@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-usb@vger.kernel.org, Amir Goldstein , linux-erofs@lists.ozlabs.org, linux-kselftest@vger.kernel.org Date: Tue, 17 Nov 2026 09:44:39 +0100 In-Reply-To: <20261116-work-loop-pseudo-v3-35-8cbbea1cf34f@kernel.org> References: <20261116-work-loop-pseudo-v3-0-8cbbea1cf34f@kernel.org> <20261116-work-loop-pseudo-v3-35-8cbbea1cf34f@kernel.org> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.56.2-0+deb13u1 X-Mailing-List: linuxppc-dev@lists.ozlabs.org List-Id: List-Help: List-Owner: List-Post: List-Archive: , List-Subscribe: , , Precedence: list X-Archived-By: MIME-Version: 1.0 X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS autolearn=unavailable autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On Mon, 2026-11-16 at 12:05 +0100, Christian Brauner wrote: > Loop, zloop, the nvme target, the scsi target, fuse passthrough, the > usb > mass storage gadget, erofs and acct all take a file from userspace > and > use it as storage. Currently, each of them has its own idea of what > to > check before using a file like that: >=20 > - loop and the gadget check for a regular file or a block device, the > =C2=A0 nvme target, the scsi target and zloop take any file type > - loop checks the open mode, the gadget checks FMODE_CAN_READ, the > nvme > =C2=A0 target, the scsi target and zloop only check for ->read_iter() and > =C2=A0 ->write_iter() > - apart from acct which refuses procfs and sysfs files none of them > =C2=A0 cares whether the file is a kernel interface >=20 > Add a helper that does all of it in one place. It only accepts > regular > files and, if the caller passes S_IFBLK, block devices. It refuses > files > on pseudo filesystems. And it checks that the file is open for what > the > caller is going to do with it and implements ->read_iter() for > reading > and ->write_iter() for writing. >=20 > O_PATH files are refused unless the caller passes FMODE_PATH. That is > for callers that only need the path of the file. fuse passthrough is > one of them and has accepted O_PATH file descriptors since commit > efad7153bf93 ("fuse: allow O_PATH fd for FUSE_DEV_IOC_BACKING_OPEN"). >=20 > Block devices are fine even though bdevfs is a pseudo filesystem. A > block device that userspace hands in is a device node on devtmpfs or > some other filesystem. Files on bdevfs only come from > bdev_file_open_by_*() and never end up here. >=20 > The pseudo filesystem check looks at the inode that actually provides > the data via d_real_data_inode(). overlayfs accepts sysfs, cgroup2, > debugfs, tracefs, configfs and securityfs as lower layers and reads > of > such an overlayfs file are served by the file on the pseudo > filesystem. > Checking the overlayfs inode would miss that. >=20 > For an O_PATH file nothing might have looked up the data yet. > overlayfs > only looks up the data of a metacopy file in a data-only layer when > the > file is opened. d_real_data_inode() does the lookup then and if that > fails we return the error. >=20 > Note that the file type is taken from the inode of the file and not > from > the host of ->f_mapping like loop and the gadget do it. A driver can > point ->f_mapping somewhere else in ->open(). It makes no difference > for > block devices as both inodes are block devices. >=20 > No functional changes. Nothing calls the helper yet. >=20 Reviewed-by: Alexander Mikhalitsyn > Signed-off-by: Christian Brauner (Amutable) > --- > =C2=A0fs/read_write.c=C2=A0=C2=A0=C2=A0 | 53 > +++++++++++++++++++++++++++++++++++++++++++++++++++++ > =C2=A0include/linux/fs.h |=C2=A0 2 ++ > =C2=A02 files changed, 55 insertions(+) >=20 > diff --git a/fs/read_write.c b/fs/read_write.c > index 6767283108991..9f1e7bfcbd4f1 100644 > --- a/fs/read_write.c > +++ b/fs/read_write.c > @@ -866,6 +866,59 @@ static ssize_t do_loop_readv_writev(struct file > *filp, struct iov_iter *iter, > =C2=A0 return ret; > =C2=A0} > =C2=A0 > +/** > + * file_check_backing - check that the kernel can use a file as > storage > + * @file: file that userspace handed in > + * @mode: FMODE_READ and/or FMODE_WRITE the kernel will do I/O with, > or > + *=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 FMODE_PATH if only the path= of @file is used > + * @type: S_IFBLK if a block device is fine as well, 0 otherwise > + * > + * Call this where a file from userspace is taken as a backing file. > Only > + * regular files and, if @type is S_IFBLK, block devices are > storage. Files > + * on pseudo filesystems are kernel interfaces and are refused. The > file > + * must be open for @mode and implement ->read_iter() for reading > and > + * ->write_iter() for writing. O_PATH files are only fine with > FMODE_PATH. > + * > + * Return: 0 on success, -EBADF if @file isn't open for @mode, - > EINVAL if it > + * can't be used as storage, or the error from looking up the data > of @file > + * on a stacking filesystem. > + */ > +int file_check_backing(const struct file *file, fmode_t mode, > umode_t type) > +{ > + fmode_t io =3D mode & (FMODE_READ | FMODE_WRITE); > + /* Drivers may point ->f_mapping at something else. */ > + umode_t fmt =3D 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) !=3D io) > + return -EBADF; > + > + if (fmt !=3D S_IFREG && !(type =3D=3D S_IFBLK && fmt =3D=3D S_IFBLK)) > + return -EINVAL; > + > + /* Look through stacking filesystems at the inode with the > data. */ > + real_inode =3D 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) && > + =C2=A0=C2=A0=C2=A0 (!(file->f_mode & FMODE_CAN_READ) || !file->f_op- > >read_iter)) > + return -EINVAL; > + > + if ((mode & FMODE_WRITE) && > + =C2=A0=C2=A0=C2=A0 (!(file->f_mode & FMODE_CAN_WRITE) || !file->f_op- > >write_iter)) > + return -EINVAL; > + > + return 0; > +} > +EXPORT_SYMBOL_GPL(file_check_backing); > + > =C2=A0ssize_t vfs_iocb_iter_read(struct file *file, struct kiocb *iocb, > =C2=A0 =C2=A0=C2=A0 struct iov_iter *iter) > =C2=A0{ > diff --git a/include/linux/fs.h b/include/linux/fs.h > index 99caef43ff3c4..b1c7ebafbadbe 100644 > --- a/include/linux/fs.h > +++ b/include/linux/fs.h > @@ -2553,6 +2553,8 @@ static inline const struct inode > *file_user_inode(const struct file *f) > =C2=A0 return file_inode(f); > =C2=A0} > =C2=A0 > +int file_check_backing(const struct file *file, fmode_t mode, > umode_t type); > + > =C2=A0static inline struct file *file_clone_open(struct file *file) > =C2=A0{ > =C2=A0 return dentry_open(&file->f_path, file->f_flags, file- > >f_cred);