From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from tor.source.kernel.org ([172.105.4.254]) by lists.ubuntu.com with esmtp (Exim 4.86_2) (envelope-from ) id 1xSuiq-0002YY-7Q for apparmor@lists.ubuntu.com; Mon, 16 Nov 2026 11:18:04 +0000 From: Christian Brauner Date: Mon, 16 Nov 2026 12:05:14 +0100 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261116-work-loop-pseudo-v3-41-8cbbea1cf34f@kernel.org> References: <20261116-work-loop-pseudo-v3-0-8cbbea1cf34f@kernel.org> In-Reply-To: <20261116-work-loop-pseudo-v3-0-8cbbea1cf34f@kernel.org> X-Developer-Signature: v=1; a=openpgp-sha256; l=2207; i=brauner@kernel.org; h=from:subject:message-id; bh=9yOpMEvSpwTZ88CI4cJoJKFDpbu7ZmjHFE7v8Gz8hhc=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWT9etynd0NX1vPeFOdEtq9nL7ydJOxr3SZTcqJWrC/8P 8+eTSY8HaUsDGJcDLJiiiwO7Sbhcst5KjYbZWrAzGFlAhnCwMUpABNpsmJk2P/2xMUG9fKOCS+q pmtt5+OefHRG0P2je5ZsqZU7vHHpSW+GvxKn1gcduJD7d9+yy53iOseiZErr8u6GsuasXRM//eL OvXwA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Subject: [apparmor] [PATCH RFC v3 41/44] usb: gadget: f_mass_storage: refuse files on pseudo filesystems List-Id: AppArmor discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: 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, "Christian Brauner (Amutable)" , Alexander Mikhalitsyn , Chris Down fsg_lun_open() takes any regular file as the backing file of a LUN. The file-storage kthread then reads and writes it with kernel_read() and kernel_write() on behalf of the USB host. With a procfs or sysfs file behind a LUN the host gets to read and write kernel interfaces with kernel credentials. Refuse files on pseudo filesystems just like loop does by switching to file_check_backing(). The helper also does the file type and readability checks. So the messages for an unsuitable backing file become one. It looks at the inode of the file instead of the host of ->f_mapping. For regular files and block devices both have the same type. It also wants ->read_iter() where FMODE_CAN_READ was enough so far. But kernel_read() never worked without ->read_iter() anyway. Reviewed-by: Alexander Mikhalitsyn Reviewed-by: Chris Down Signed-off-by: Christian Brauner (Amutable) --- drivers/usb/gadget/function/storage_common.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/drivers/usb/gadget/function/storage_common.c b/drivers/usb/gadget/function/storage_common.c index 75831f2c7abeb..af0c37a9fe029 100644 --- a/drivers/usb/gadget/function/storage_common.c +++ b/drivers/usb/gadget/function/storage_common.c @@ -205,23 +205,18 @@ int fsg_lun_open(struct fsg_lun *curlun, const char *filename) if (!(filp->f_mode & FMODE_WRITE)) ro = 1; - inode = filp->f_mapping->host; - if ((!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))) { - LINFO(curlun, "invalid file type: %s\n", filename); - goto out; - } - /* * If we can't read the file, it's no good. * If we can't write the file, use it read-only. */ - if (!(filp->f_mode & FMODE_CAN_READ)) { - LINFO(curlun, "file not readable: %s\n", filename); + if (file_check_backing(filp, FMODE_READ, S_IFBLK)) { + LINFO(curlun, "invalid backing file: %s\n", filename); goto out; } if (!(filp->f_mode & FMODE_CAN_WRITE)) ro = 1; + inode = filp->f_mapping->host; size = i_size_read(inode); if (size < 0) { LINFO(curlun, "unable to find file size: %s\n", filename); -- 2.53.0