From nobody Sat Feb 7 09:36:32 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1571923042; cv=none; d=zoho.com; s=zohoarc; b=IQHMh8p2JeTCusFFNcu0B7E/Iodmyk62aAmc67pTY9asq/EhL1AZsWnARzsINVxNQoilz0Fijh0w6GeD4K1VmCUUsvjiiC0ElLFbnSM6qHP+aYZAYyg+RR4J+skdXBiqylIXKFmkaIaLvR3wGNi0zFBcK4AvSL24B+RxzkdeEes= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571923042; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Zat1t+hKG0Zk8YyYdD8I3eOrGeDc/SeMU3HKQTfuqnA=; b=ZBCOjPLjy1PsmJxk+8lZu4J1LxFK9QIaj2MoIMUbp7HzWrC1HTQ2+FIsmWOEbaMSvRWSvXlGMrNEUK9zfEbtHfmRFZrvhT4ANe3MNgQjnNB8ROv/rmgNz1jCdZYQIe62Ew7R7OC5hLQeNwshr6PbNabnWCRFoRTPVcPi4Z+AXEo= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1571923042850313.9638172326887; Thu, 24 Oct 2019 06:17:22 -0700 (PDT) Received: from localhost ([::1]:42284 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNczP-0005TT-Ud for importer@patchew.org; Thu, 24 Oct 2019 09:17:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39417) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNbID-0003oK-Ai for qemu-devel@nongnu.org; Thu, 24 Oct 2019 07:28:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNbIB-00006i-An for qemu-devel@nongnu.org; Thu, 24 Oct 2019 07:28:37 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:27203 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNbIB-00005C-5S for qemu-devel@nongnu.org; Thu, 24 Oct 2019 07:28:35 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-204-HpkeFZk8O3mtTFlMMPJkRQ-1; Thu, 24 Oct 2019 07:28:28 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 092FE107AD31; Thu, 24 Oct 2019 11:28:27 +0000 (UTC) Received: from dgilbert-t580.localhost (ovpn-117-248.ams2.redhat.com [10.36.117.248]) by smtp.corp.redhat.com (Postfix) with ESMTP id 81FD44524; Thu, 24 Oct 2019 11:28:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1571916510; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Zat1t+hKG0Zk8YyYdD8I3eOrGeDc/SeMU3HKQTfuqnA=; b=NsqTt+UndQeVu2T1BsDPz3mjbx8VLxBBcvM6YPAv+b4DjJr5Xig+nvVH8AA1BMM4etO5mr 80GkAukxPpZsah+JFconR6fucjV2FgGR1G/MZCrmmgUtCoiQvtCMr3ZfaUnQ3r64tuOLmI yxXdTkBKk8vviAGtOsk0qhqQqBqmLRw= From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, renzhen@linux.alibaba.com, eguan@linux.alibaba.com, ganesh.mahalingam@intel.com, m.mizuma@jp.fujitsu.com, mszeredi@redhat.com, misono.tomohiro@jp.fujitsu.com, tao.peng@linux.alibaba.com, piaojun@huawei.com, stefanha@redhat.com, vgoyal@redhat.com, mst@redhat.com, berrange@redhat.com Subject: [PATCH 15/25] virtiofsd: use /proc/self/fd/ O_PATH file descriptor Date: Thu, 24 Oct 2019 12:27:08 +0100 Message-Id: <20191024112718.34657-16-dgilbert@redhat.com> In-Reply-To: <20191024112718.34657-1-dgilbert@redhat.com> References: <20191024112718.34657-1-dgilbert@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-MC-Unique: HpkeFZk8O3mtTFlMMPJkRQ-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 205.139.110.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" From: Stefan Hajnoczi Sandboxing will remove /proc from the mount namespace so we can no longer build string paths into "/proc/self/fd/...". Keep an O_PATH file descriptor so we can still re-open fds via /proc/self/fd. Signed-off-by: Stefan Hajnoczi --- contrib/virtiofsd/passthrough_ll.c | 128 +++++++++++++++++++++++------ 1 file changed, 101 insertions(+), 27 deletions(-) diff --git a/contrib/virtiofsd/passthrough_ll.c b/contrib/virtiofsd/passthr= ough_ll.c index cb01e3f088..3ddf22d162 100644 --- a/contrib/virtiofsd/passthrough_ll.c +++ b/contrib/virtiofsd/passthrough_ll.c @@ -110,6 +110,9 @@ struct lo_data { struct lo_map ino_map; /* protected by lo->mutex */ struct lo_map dirp_map; /* protected by lo->mutex */ struct lo_map fd_map; /* protected by lo->mutex */ + + /* An O_PATH file descriptor to /proc/self/fd/ */ + int proc_self_fd; }; =20 static const struct fuse_opt lo_opts[] =3D { @@ -378,9 +381,9 @@ static int lo_parent_and_name(struct lo_data *lo, struc= t lo_inode *inode, int res; =20 retry: - sprintf(procname, "/proc/self/fd/%i", inode->fd); + sprintf(procname, "%i", inode->fd); =20 - res =3D readlink(procname, path, PATH_MAX); + res =3D readlinkat(lo->proc_self_fd, procname, path, PATH_MAX); if (res < 0) { fuse_log(FUSE_LOG_WARNING, "lo_parent_and_name: readlink failed: %m\n"); goto fail_noretry; @@ -465,9 +468,9 @@ static int utimensat_empty(struct lo_data *lo, struct l= o_inode *inode, } return res; } - sprintf(path, "/proc/self/fd/%i", inode->fd); + sprintf(path, "%i", inode->fd); =20 - return utimensat(AT_FDCWD, path, tv, 0); + return utimensat(lo->proc_self_fd, path, tv, 0); =20 fallback: res =3D lo_parent_and_name(lo, inode, path, &parent); @@ -521,8 +524,9 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino, = struct stat *attr, if (fi) { res =3D fchmod(fd, attr->st_mode); } else { - sprintf(procname, "/proc/self/fd/%i", ifd); - res =3D chmod(procname, attr->st_mode); + sprintf(procname, "%i", ifd); + res =3D fchmodat(lo->proc_self_fd, procname, + attr->st_mode, 0); } if (res =3D=3D -1) goto out_err; @@ -539,11 +543,23 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino= , struct stat *attr, goto out_err; } if (valid & FUSE_SET_ATTR_SIZE) { + int truncfd; + if (fi) { - res =3D ftruncate(fd, attr->st_size); + truncfd =3D fd; } else { - sprintf(procname, "/proc/self/fd/%i", ifd); - res =3D truncate(procname, attr->st_size); + sprintf(procname, "%i", ifd); + truncfd =3D openat(lo->proc_self_fd, procname, O_RDWR); + if (truncfd < 0) { + goto out_err; + } + } + + res =3D ftruncate(truncfd, attr->st_size); + if (!fi) { + saverr =3D errno; + close(truncfd); + errno =3D saverr; } if (res =3D=3D -1) goto out_err; @@ -825,9 +841,9 @@ static int linkat_empty_nofollow(struct lo_data *lo, st= ruct lo_inode *inode, return res; } =20 - sprintf(path, "/proc/self/fd/%i", inode->fd); + sprintf(path, "%i", inode->fd); =20 - return linkat(AT_FDCWD, path, dfd, name, AT_SYMLINK_FOLLOW); + return linkat(lo->proc_self_fd, path, dfd, name, AT_SYMLINK_FOLLOW); =20 fallback: res =3D lo_parent_and_name(lo, inode, path, &parent); @@ -1325,8 +1341,8 @@ static void lo_open(fuse_req_t req, fuse_ino_t ino, s= truct fuse_file_info *fi) if (lo->writeback && (fi->flags & O_APPEND)) fi->flags &=3D ~O_APPEND; =20 - sprintf(buf, "/proc/self/fd/%i", lo_fd(req, ino)); - fd =3D open(buf, fi->flags & ~O_NOFOLLOW); + sprintf(buf, "%i", lo_fd(req, ino)); + fd =3D openat(lo->proc_self_fd, buf, fi->flags & ~O_NOFOLLOW); if (fd =3D=3D -1) return (void) fuse_reply_err(req, errno); =20 @@ -1375,8 +1391,8 @@ static void lo_flush(fuse_req_t req, fuse_ino_t ino, = struct fuse_file_info *fi) static void lo_fsync(fuse_req_t req, fuse_ino_t ino, int datasync, struct fuse_file_info *fi) { + struct lo_data *lo =3D lo_data(req); int res; - (void) ino; int fd; char *buf; =20 @@ -1384,11 +1400,11 @@ static void lo_fsync(fuse_req_t req, fuse_ino_t ino= , int datasync, (void *)fi); =20 if (!fi) { - res =3D asprintf(&buf, "/proc/self/fd/%i", lo_fd(req, ino)); + res =3D asprintf(&buf, "%i", lo_fd(req, ino)); if (res =3D=3D -1) return (void) fuse_reply_err(req, errno); =20 - fd =3D open(buf, O_RDWR); + fd =3D openat(lo->proc_self_fd, buf, O_RDWR); free(buf); if (fd =3D=3D -1) return (void) fuse_reply_err(req, errno); @@ -1493,11 +1509,13 @@ static void lo_flock(fuse_req_t req, fuse_ino_t ino= , struct fuse_file_info *fi, static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name, size_t size) { + struct lo_data *lo =3D lo_data(req); char *value =3D NULL; char procname[64]; struct lo_inode *inode; ssize_t ret; int saverr; + int fd =3D -1; =20 inode =3D lo_inode(req, ino); if (!inode) { @@ -1520,14 +1538,18 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t = ino, const char *name, goto out; } =20 - sprintf(procname, "/proc/self/fd/%i", inode->fd); + sprintf(procname, "%i", inode->fd); + fd =3D openat(lo->proc_self_fd, procname, O_RDONLY); + if (fd < 0) { + goto out_err; + } =20 if (size) { value =3D malloc(size); if (!value) goto out_err; =20 - ret =3D getxattr(procname, name, value, size); + ret =3D fgetxattr(fd, name, value, size); if (ret =3D=3D -1) goto out_err; saverr =3D 0; @@ -1536,7 +1558,7 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t in= o, const char *name, =20 fuse_reply_buf(req, value, ret); } else { - ret =3D getxattr(procname, name, NULL, 0); + ret =3D fgetxattr(fd, name, NULL, 0); if (ret =3D=3D -1) goto out_err; =20 @@ -1544,6 +1566,10 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t i= no, const char *name, } out_free: free(value); + + if (fd >=3D 0) { + close(fd); + } return; =20 out_err: @@ -1555,11 +1581,13 @@ out: =20 static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size) { + struct lo_data *lo =3D lo_data(req); char *value =3D NULL; char procname[64]; struct lo_inode *inode; ssize_t ret; int saverr; + int fd =3D -1; =20 inode =3D lo_inode(req, ino); if (!inode) { @@ -1582,14 +1610,18 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t= ino, size_t size) goto out; } =20 - sprintf(procname, "/proc/self/fd/%i", inode->fd); + sprintf(procname, "%i", inode->fd); + fd =3D openat(lo->proc_self_fd, procname, O_RDONLY); + if (fd < 0) { + goto out_err; + } =20 if (size) { value =3D malloc(size); if (!value) goto out_err; =20 - ret =3D listxattr(procname, value, size); + ret =3D flistxattr(fd, value, size); if (ret =3D=3D -1) goto out_err; saverr =3D 0; @@ -1598,7 +1630,7 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t i= no, size_t size) =20 fuse_reply_buf(req, value, ret); } else { - ret =3D listxattr(procname, NULL, 0); + ret =3D flistxattr(fd, NULL, 0); if (ret =3D=3D -1) goto out_err; =20 @@ -1606,6 +1638,10 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t = ino, size_t size) } out_free: free(value); + + if (fd >=3D 0) { + close(fd); + } return; =20 out_err: @@ -1619,9 +1655,11 @@ static void lo_setxattr(fuse_req_t req, fuse_ino_t i= no, const char *name, const char *value, size_t size, int flags) { char procname[64]; + struct lo_data *lo =3D lo_data(req); struct lo_inode *inode; ssize_t ret; int saverr; + int fd =3D -1; =20 inode =3D lo_inode(req, ino); if (!inode) { @@ -1644,21 +1682,31 @@ static void lo_setxattr(fuse_req_t req, fuse_ino_t = ino, const char *name, goto out; } =20 - sprintf(procname, "/proc/self/fd/%i", inode->fd); + sprintf(procname, "%i", inode->fd); + fd =3D openat(lo->proc_self_fd, procname, O_RDWR); + if (fd < 0) { + saverr =3D errno; + goto out; + } =20 - ret =3D setxattr(procname, name, value, size, flags); + ret =3D fsetxattr(fd, name, value, size, flags); saverr =3D ret =3D=3D -1 ? errno : 0; =20 out: + if (fd >=3D 0) { + close(fd); + } fuse_reply_err(req, saverr); } =20 static void lo_removexattr(fuse_req_t req, fuse_ino_t ino, const char *nam= e) { char procname[64]; + struct lo_data *lo =3D lo_data(req); struct lo_inode *inode; ssize_t ret; int saverr; + int fd =3D -1; =20 inode =3D lo_inode(req, ino); if (!inode) { @@ -1681,12 +1729,20 @@ static void lo_removexattr(fuse_req_t req, fuse_ino= _t ino, const char *name) goto out; } =20 - sprintf(procname, "/proc/self/fd/%i", inode->fd); + sprintf(procname, "%i", inode->fd); + fd =3D openat(lo->proc_self_fd, procname, O_RDWR); + if (fd < 0) { + saverr =3D errno; + goto out; + } =20 - ret =3D removexattr(procname, name); + ret =3D fremovexattr(fd, name); saverr =3D ret =3D=3D -1 ? errno : 0; =20 out: + if (fd >=3D 0) { + close(fd); + } fuse_reply_err(req, saverr); } =20 @@ -1765,13 +1821,24 @@ static void print_capabilities(void) printf("}\n"); } =20 +static void setup_proc_self_fd(struct lo_data *lo) +{ + lo->proc_self_fd =3D open("/proc/self/fd", O_PATH); + if (lo->proc_self_fd =3D=3D -1) { + fuse_log(FUSE_LOG_ERR, "open(/proc/self/fd, O_PATH): %m\n"); + exit(1); + } +} + int main(int argc, char *argv[]) { struct fuse_args args =3D FUSE_ARGS_INIT(argc, argv); struct fuse_session *se; struct fuse_cmdline_opts opts; struct lo_data lo =3D { .debug =3D 0, - .writeback =3D 0 }; + .writeback =3D 0, + .proc_self_fd =3D -1, + }; struct lo_map_elem *root_elem; int ret =3D -1; =20 @@ -1878,6 +1945,9 @@ int main(int argc, char *argv[]) =20 fuse_daemonize(opts.foreground); =20 + /* Must be after daemonize to get the right /proc/self/fd */ + setup_proc_self_fd(&lo); + /* Block until ctrl+c or fusermount -u */ ret =3D virtio_loop(se); =20 @@ -1893,6 +1963,10 @@ err_out1: lo_map_destroy(&lo.dirp_map); lo_map_destroy(&lo.ino_map); =20 + if (lo.proc_self_fd >=3D 0) { + close(lo.proc_self_fd); + } + if (lo.root.fd >=3D 0) close(lo.root.fd); =20 --=20 2.23.0