From patchwork Thu Jan 23 11:57:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Dr. David Alan Gilbert" X-Patchwork-Id: 1228036 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=ObJGGxGq; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 483PLr3pxwz9sSL for ; Fri, 24 Jan 2020 01:10:44 +1100 (AEDT) Received: from localhost ([::1]:57716 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iudBy-0006x7-5p for incoming@patchwork.ozlabs.org; Thu, 23 Jan 2020 09:10:42 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40778) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iub9L-0002i9-2X for qemu-devel@nongnu.org; Thu, 23 Jan 2020 06:59:52 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iub9J-0005M8-6x for qemu-devel@nongnu.org; Thu, 23 Jan 2020 06:59:50 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:53772 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iub9J-0005Lf-2O for qemu-devel@nongnu.org; Thu, 23 Jan 2020 06:59:49 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1579780788; 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=rocNf17Cmm6LMNLAi+Vk5YilItERbDJCMjcMFpiQKVM=; b=ObJGGxGqwK5rlNEH/x3SrHideE5/XJrx77uKsl1OFC/fpL4zyuV9sEiCjlGuWUvrQSf95g 3fR5Of6dujelrXDYsG7z5fjqjttJfT4a3WNPiBch0xuGOmcGl1eST7FQerFtDHapsoSMCA t3pVeHOc2+DWTbWSb6nwXTK41P84Bqw= 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-286-sViTGFmTMpScRCGqrYh-UQ-1; Thu, 23 Jan 2020 06:59:45 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E3A2918A6EC0 for ; Thu, 23 Jan 2020 11:59:44 +0000 (UTC) Received: from dgilbert-t580.localhost (unknown [10.36.118.0]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3DDCE85757; Thu, 23 Jan 2020 11:59:44 +0000 (UTC) From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, stefanha@redhat.com Subject: [PULL 048/111] virtiofsd: use /proc/self/fd/ O_PATH file descriptor Date: Thu, 23 Jan 2020 11:57:38 +0000 Message-Id: <20200123115841.138849-49-dgilbert@redhat.com> In-Reply-To: <20200123115841.138849-1-dgilbert@redhat.com> References: <20200123115841.138849-1-dgilbert@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-MC-Unique: sViTGFmTMpScRCGqrYh-UQ-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.61 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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 Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: Dr. David Alan Gilbert --- tools/virtiofsd/passthrough_ll.c | 130 ++++++++++++++++++++++++------- 1 file changed, 103 insertions(+), 27 deletions(-) diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c index e3d65c3676..e2e2211ea1 100644 --- a/tools/virtiofsd/passthrough_ll.c +++ b/tools/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; }; static const struct fuse_opt lo_opts[] = { @@ -379,9 +382,9 @@ static int lo_parent_and_name(struct lo_data *lo, struct lo_inode *inode, int res; retry: - sprintf(procname, "/proc/self/fd/%i", inode->fd); + sprintf(procname, "%i", inode->fd); - res = readlink(procname, path, PATH_MAX); + res = readlinkat(lo->proc_self_fd, procname, path, PATH_MAX); if (res < 0) { fuse_log(FUSE_LOG_WARNING, "%s: readlink failed: %m\n", __func__); goto fail_noretry; @@ -477,9 +480,9 @@ static int utimensat_empty(struct lo_data *lo, struct lo_inode *inode, } return res; } - sprintf(path, "/proc/self/fd/%i", inode->fd); + sprintf(path, "%i", inode->fd); - return utimensat(AT_FDCWD, path, tv, 0); + return utimensat(lo->proc_self_fd, path, tv, 0); fallback: res = lo_parent_and_name(lo, inode, path, &parent); @@ -535,8 +538,8 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr, if (fi) { res = fchmod(fd, attr->st_mode); } else { - sprintf(procname, "/proc/self/fd/%i", ifd); - res = chmod(procname, attr->st_mode); + sprintf(procname, "%i", ifd); + res = fchmodat(lo->proc_self_fd, procname, attr->st_mode, 0); } if (res == -1) { goto out_err; @@ -552,11 +555,23 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr, } } if (valid & FUSE_SET_ATTR_SIZE) { + int truncfd; + if (fi) { - res = ftruncate(fd, attr->st_size); + truncfd = fd; } else { - sprintf(procname, "/proc/self/fd/%i", ifd); - res = truncate(procname, attr->st_size); + sprintf(procname, "%i", ifd); + truncfd = openat(lo->proc_self_fd, procname, O_RDWR); + if (truncfd < 0) { + goto out_err; + } + } + + res = ftruncate(truncfd, attr->st_size); + if (!fi) { + saverr = errno; + close(truncfd); + errno = saverr; } if (res == -1) { goto out_err; @@ -874,9 +889,9 @@ static int linkat_empty_nofollow(struct lo_data *lo, struct lo_inode *inode, return res; } - sprintf(path, "/proc/self/fd/%i", inode->fd); + sprintf(path, "%i", inode->fd); - return linkat(AT_FDCWD, path, dfd, name, AT_SYMLINK_FOLLOW); + return linkat(lo->proc_self_fd, path, dfd, name, AT_SYMLINK_FOLLOW); fallback: res = lo_parent_and_name(lo, inode, path, &parent); @@ -1404,8 +1419,8 @@ static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi) fi->flags &= ~O_APPEND; } - sprintf(buf, "/proc/self/fd/%i", lo_fd(req, ino)); - fd = open(buf, fi->flags & ~O_NOFOLLOW); + sprintf(buf, "%i", lo_fd(req, ino)); + fd = openat(lo->proc_self_fd, buf, fi->flags & ~O_NOFOLLOW); if (fd == -1) { return (void)fuse_reply_err(req, errno); } @@ -1458,7 +1473,6 @@ static void lo_fsync(fuse_req_t req, fuse_ino_t ino, int datasync, struct fuse_file_info *fi) { int res; - (void)ino; int fd; char *buf; @@ -1466,12 +1480,14 @@ static void lo_fsync(fuse_req_t req, fuse_ino_t ino, int datasync, (void *)fi); if (!fi) { - res = asprintf(&buf, "/proc/self/fd/%i", lo_fd(req, ino)); + struct lo_data *lo = lo_data(req); + + res = asprintf(&buf, "%i", lo_fd(req, ino)); if (res == -1) { return (void)fuse_reply_err(req, errno); } - fd = open(buf, O_RDWR); + fd = openat(lo->proc_self_fd, buf, O_RDWR); free(buf); if (fd == -1) { return (void)fuse_reply_err(req, errno); @@ -1587,11 +1603,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 = lo_data(req); char *value = NULL; char procname[64]; struct lo_inode *inode; ssize_t ret; int saverr; + int fd = -1; inode = lo_inode(req, ino); if (!inode) { @@ -1616,7 +1634,11 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name, goto out; } - sprintf(procname, "/proc/self/fd/%i", inode->fd); + sprintf(procname, "%i", inode->fd); + fd = openat(lo->proc_self_fd, procname, O_RDONLY); + if (fd < 0) { + goto out_err; + } if (size) { value = malloc(size); @@ -1624,7 +1646,7 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name, goto out_err; } - ret = getxattr(procname, name, value, size); + ret = fgetxattr(fd, name, value, size); if (ret == -1) { goto out_err; } @@ -1635,7 +1657,7 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name, fuse_reply_buf(req, value, ret); } else { - ret = getxattr(procname, name, NULL, 0); + ret = fgetxattr(fd, name, NULL, 0); if (ret == -1) { goto out_err; } @@ -1644,6 +1666,10 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name, } out_free: free(value); + + if (fd >= 0) { + close(fd); + } return; out_err: @@ -1655,11 +1681,13 @@ out: static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size) { + struct lo_data *lo = lo_data(req); char *value = NULL; char procname[64]; struct lo_inode *inode; ssize_t ret; int saverr; + int fd = -1; inode = lo_inode(req, ino); if (!inode) { @@ -1683,7 +1711,11 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size) goto out; } - sprintf(procname, "/proc/self/fd/%i", inode->fd); + sprintf(procname, "%i", inode->fd); + fd = openat(lo->proc_self_fd, procname, O_RDONLY); + if (fd < 0) { + goto out_err; + } if (size) { value = malloc(size); @@ -1691,7 +1723,7 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size) goto out_err; } - ret = listxattr(procname, value, size); + ret = flistxattr(fd, value, size); if (ret == -1) { goto out_err; } @@ -1702,7 +1734,7 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size) fuse_reply_buf(req, value, ret); } else { - ret = listxattr(procname, NULL, 0); + ret = flistxattr(fd, NULL, 0); if (ret == -1) { goto out_err; } @@ -1711,6 +1743,10 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size) } out_free: free(value); + + if (fd >= 0) { + close(fd); + } return; out_err: @@ -1724,9 +1760,11 @@ static void lo_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name, const char *value, size_t size, int flags) { char procname[64]; + struct lo_data *lo = lo_data(req); struct lo_inode *inode; ssize_t ret; int saverr; + int fd = -1; inode = lo_inode(req, ino); if (!inode) { @@ -1751,21 +1789,31 @@ static void lo_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name, goto out; } - sprintf(procname, "/proc/self/fd/%i", inode->fd); + sprintf(procname, "%i", inode->fd); + fd = openat(lo->proc_self_fd, procname, O_RDWR); + if (fd < 0) { + saverr = errno; + goto out; + } - ret = setxattr(procname, name, value, size, flags); + ret = fsetxattr(fd, name, value, size, flags); saverr = ret == -1 ? errno : 0; out: + if (fd >= 0) { + close(fd); + } fuse_reply_err(req, saverr); } static void lo_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name) { char procname[64]; + struct lo_data *lo = lo_data(req); struct lo_inode *inode; ssize_t ret; int saverr; + int fd = -1; inode = lo_inode(req, ino); if (!inode) { @@ -1789,12 +1837,20 @@ static void lo_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name) goto out; } - sprintf(procname, "/proc/self/fd/%i", inode->fd); + sprintf(procname, "%i", inode->fd); + fd = openat(lo->proc_self_fd, procname, O_RDWR); + if (fd < 0) { + saverr = errno; + goto out; + } - ret = removexattr(procname, name); + ret = fremovexattr(fd, name); saverr = ret == -1 ? errno : 0; out: + if (fd >= 0) { + close(fd); + } fuse_reply_err(req, saverr); } @@ -1887,12 +1943,25 @@ static void print_capabilities(void) printf("}\n"); } +static void setup_proc_self_fd(struct lo_data *lo) +{ + lo->proc_self_fd = open("/proc/self/fd", O_PATH); + if (lo->proc_self_fd == -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 = FUSE_ARGS_INIT(argc, argv); struct fuse_session *se; struct fuse_cmdline_opts opts; - struct lo_data lo = { .debug = 0, .writeback = 0 }; + struct lo_data lo = { + .debug = 0, + .writeback = 0, + .proc_self_fd = -1, + }; struct lo_map_elem *root_elem; int ret = -1; @@ -2003,6 +2072,9 @@ int main(int argc, char *argv[]) fuse_daemonize(opts.foreground); + /* Must be after daemonize to get the right /proc/self/fd */ + setup_proc_self_fd(&lo); + /* Block until ctrl+c or fusermount -u */ ret = virtio_loop(se); @@ -2018,6 +2090,10 @@ err_out1: lo_map_destroy(&lo.dirp_map); lo_map_destroy(&lo.ino_map); + if (lo.proc_self_fd >= 0) { + close(lo.proc_self_fd); + } + if (lo.root.fd >= 0) { close(lo.root.fd); }