yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-virtiofsd-use-proc-self-fd-O_PATH-file-descriptor.patch

22c213
From bce5070d1aada88154b811a08eec1586ab24fce5 Mon Sep 17 00:00:00 2001
22c213
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
22c213
Date: Mon, 27 Jan 2020 19:01:26 +0100
22c213
Subject: [PATCH 055/116] virtiofsd: use /proc/self/fd/ O_PATH file descriptor
22c213
MIME-Version: 1.0
22c213
Content-Type: text/plain; charset=UTF-8
22c213
Content-Transfer-Encoding: 8bit
22c213
22c213
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
22c213
Message-id: <20200127190227.40942-52-dgilbert@redhat.com>
22c213
Patchwork-id: 93506
22c213
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 051/112] virtiofsd: use /proc/self/fd/ O_PATH file descriptor
22c213
Bugzilla: 1694164
22c213
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
22c213
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
22c213
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
22c213
22c213
From: Stefan Hajnoczi <stefanha@redhat.com>
22c213
22c213
Sandboxing will remove /proc from the mount namespace so we can no
22c213
longer build string paths into "/proc/self/fd/...".
22c213
22c213
Keep an O_PATH file descriptor so we can still re-open fds via
22c213
/proc/self/fd.
22c213
22c213
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
22c213
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
22c213
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
22c213
(cherry picked from commit 9f59d175e2ca96f0b87f534dba69ea547dd35945)
22c213
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
22c213
---
22c213
 tools/virtiofsd/passthrough_ll.c | 130 +++++++++++++++++++++++++++++++--------
22c213
 1 file changed, 103 insertions(+), 27 deletions(-)
22c213
22c213
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
22c213
index e3d65c3..e2e2211 100644
22c213
--- a/tools/virtiofsd/passthrough_ll.c
22c213
+++ b/tools/virtiofsd/passthrough_ll.c
22c213
@@ -110,6 +110,9 @@ struct lo_data {
22c213
     struct lo_map ino_map; /* protected by lo->mutex */
22c213
     struct lo_map dirp_map; /* protected by lo->mutex */
22c213
     struct lo_map fd_map; /* protected by lo->mutex */
22c213
+
22c213
+    /* An O_PATH file descriptor to /proc/self/fd/ */
22c213
+    int proc_self_fd;
22c213
 };
22c213
 
22c213
 static const struct fuse_opt lo_opts[] = {
22c213
@@ -379,9 +382,9 @@ static int lo_parent_and_name(struct lo_data *lo, struct lo_inode *inode,
22c213
     int res;
22c213
 
22c213
 retry:
22c213
-    sprintf(procname, "/proc/self/fd/%i", inode->fd);
22c213
+    sprintf(procname, "%i", inode->fd);
22c213
 
22c213
-    res = readlink(procname, path, PATH_MAX);
22c213
+    res = readlinkat(lo->proc_self_fd, procname, path, PATH_MAX);
22c213
     if (res < 0) {
22c213
         fuse_log(FUSE_LOG_WARNING, "%s: readlink failed: %m\n", __func__);
22c213
         goto fail_noretry;
22c213
@@ -477,9 +480,9 @@ static int utimensat_empty(struct lo_data *lo, struct lo_inode *inode,
22c213
         }
22c213
         return res;
22c213
     }
22c213
-    sprintf(path, "/proc/self/fd/%i", inode->fd);
22c213
+    sprintf(path, "%i", inode->fd);
22c213
 
22c213
-    return utimensat(AT_FDCWD, path, tv, 0);
22c213
+    return utimensat(lo->proc_self_fd, path, tv, 0);
22c213
 
22c213
 fallback:
22c213
     res = lo_parent_and_name(lo, inode, path, &parent);
22c213
@@ -535,8 +538,8 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
22c213
         if (fi) {
22c213
             res = fchmod(fd, attr->st_mode);
22c213
         } else {
22c213
-            sprintf(procname, "/proc/self/fd/%i", ifd);
22c213
-            res = chmod(procname, attr->st_mode);
22c213
+            sprintf(procname, "%i", ifd);
22c213
+            res = fchmodat(lo->proc_self_fd, procname, attr->st_mode, 0);
22c213
         }
22c213
         if (res == -1) {
22c213
             goto out_err;
22c213
@@ -552,11 +555,23 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
22c213
         }
22c213
     }
22c213
     if (valid & FUSE_SET_ATTR_SIZE) {
22c213
+        int truncfd;
22c213
+
22c213
         if (fi) {
22c213
-            res = ftruncate(fd, attr->st_size);
22c213
+            truncfd = fd;
22c213
         } else {
22c213
-            sprintf(procname, "/proc/self/fd/%i", ifd);
22c213
-            res = truncate(procname, attr->st_size);
22c213
+            sprintf(procname, "%i", ifd);
22c213
+            truncfd = openat(lo->proc_self_fd, procname, O_RDWR);
22c213
+            if (truncfd < 0) {
22c213
+                goto out_err;
22c213
+            }
22c213
+        }
22c213
+
22c213
+        res = ftruncate(truncfd, attr->st_size);
22c213
+        if (!fi) {
22c213
+            saverr = errno;
22c213
+            close(truncfd);
22c213
+            errno = saverr;
22c213
         }
22c213
         if (res == -1) {
22c213
             goto out_err;
22c213
@@ -874,9 +889,9 @@ static int linkat_empty_nofollow(struct lo_data *lo, struct lo_inode *inode,
22c213
         return res;
22c213
     }
22c213
 
22c213
-    sprintf(path, "/proc/self/fd/%i", inode->fd);
22c213
+    sprintf(path, "%i", inode->fd);
22c213
 
22c213
-    return linkat(AT_FDCWD, path, dfd, name, AT_SYMLINK_FOLLOW);
22c213
+    return linkat(lo->proc_self_fd, path, dfd, name, AT_SYMLINK_FOLLOW);
22c213
 
22c213
 fallback:
22c213
     res = lo_parent_and_name(lo, inode, path, &parent);
22c213
@@ -1404,8 +1419,8 @@ static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
22c213
         fi->flags &= ~O_APPEND;
22c213
     }
22c213
 
22c213
-    sprintf(buf, "/proc/self/fd/%i", lo_fd(req, ino));
22c213
-    fd = open(buf, fi->flags & ~O_NOFOLLOW);
22c213
+    sprintf(buf, "%i", lo_fd(req, ino));
22c213
+    fd = openat(lo->proc_self_fd, buf, fi->flags & ~O_NOFOLLOW);
22c213
     if (fd == -1) {
22c213
         return (void)fuse_reply_err(req, errno);
22c213
     }
22c213
@@ -1458,7 +1473,6 @@ static void lo_fsync(fuse_req_t req, fuse_ino_t ino, int datasync,
22c213
                      struct fuse_file_info *fi)
22c213
 {
22c213
     int res;
22c213
-    (void)ino;
22c213
     int fd;
22c213
     char *buf;
22c213
 
22c213
@@ -1466,12 +1480,14 @@ static void lo_fsync(fuse_req_t req, fuse_ino_t ino, int datasync,
22c213
              (void *)fi);
22c213
 
22c213
     if (!fi) {
22c213
-        res = asprintf(&buf, "/proc/self/fd/%i", lo_fd(req, ino));
22c213
+        struct lo_data *lo = lo_data(req);
22c213
+
22c213
+        res = asprintf(&buf, "%i", lo_fd(req, ino));
22c213
         if (res == -1) {
22c213
             return (void)fuse_reply_err(req, errno);
22c213
         }
22c213
 
22c213
-        fd = open(buf, O_RDWR);
22c213
+        fd = openat(lo->proc_self_fd, buf, O_RDWR);
22c213
         free(buf);
22c213
         if (fd == -1) {
22c213
             return (void)fuse_reply_err(req, errno);
22c213
@@ -1587,11 +1603,13 @@ static void lo_flock(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi,
22c213
 static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
22c213
                         size_t size)
22c213
 {
22c213
+    struct lo_data *lo = lo_data(req);
22c213
     char *value = NULL;
22c213
     char procname[64];
22c213
     struct lo_inode *inode;
22c213
     ssize_t ret;
22c213
     int saverr;
22c213
+    int fd = -1;
22c213
 
22c213
     inode = lo_inode(req, ino);
22c213
     if (!inode) {
22c213
@@ -1616,7 +1634,11 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
22c213
         goto out;
22c213
     }
22c213
 
22c213
-    sprintf(procname, "/proc/self/fd/%i", inode->fd);
22c213
+    sprintf(procname, "%i", inode->fd);
22c213
+    fd = openat(lo->proc_self_fd, procname, O_RDONLY);
22c213
+    if (fd < 0) {
22c213
+        goto out_err;
22c213
+    }
22c213
 
22c213
     if (size) {
22c213
         value = malloc(size);
22c213
@@ -1624,7 +1646,7 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
22c213
             goto out_err;
22c213
         }
22c213
 
22c213
-        ret = getxattr(procname, name, value, size);
22c213
+        ret = fgetxattr(fd, name, value, size);
22c213
         if (ret == -1) {
22c213
             goto out_err;
22c213
         }
22c213
@@ -1635,7 +1657,7 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
22c213
 
22c213
         fuse_reply_buf(req, value, ret);
22c213
     } else {
22c213
-        ret = getxattr(procname, name, NULL, 0);
22c213
+        ret = fgetxattr(fd, name, NULL, 0);
22c213
         if (ret == -1) {
22c213
             goto out_err;
22c213
         }
22c213
@@ -1644,6 +1666,10 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
22c213
     }
22c213
 out_free:
22c213
     free(value);
22c213
+
22c213
+    if (fd >= 0) {
22c213
+        close(fd);
22c213
+    }
22c213
     return;
22c213
 
22c213
 out_err:
22c213
@@ -1655,11 +1681,13 @@ out:
22c213
 
22c213
 static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
22c213
 {
22c213
+    struct lo_data *lo = lo_data(req);
22c213
     char *value = NULL;
22c213
     char procname[64];
22c213
     struct lo_inode *inode;
22c213
     ssize_t ret;
22c213
     int saverr;
22c213
+    int fd = -1;
22c213
 
22c213
     inode = lo_inode(req, ino);
22c213
     if (!inode) {
22c213
@@ -1683,7 +1711,11 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
22c213
         goto out;
22c213
     }
22c213
 
22c213
-    sprintf(procname, "/proc/self/fd/%i", inode->fd);
22c213
+    sprintf(procname, "%i", inode->fd);
22c213
+    fd = openat(lo->proc_self_fd, procname, O_RDONLY);
22c213
+    if (fd < 0) {
22c213
+        goto out_err;
22c213
+    }
22c213
 
22c213
     if (size) {
22c213
         value = malloc(size);
22c213
@@ -1691,7 +1723,7 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
22c213
             goto out_err;
22c213
         }
22c213
 
22c213
-        ret = listxattr(procname, value, size);
22c213
+        ret = flistxattr(fd, value, size);
22c213
         if (ret == -1) {
22c213
             goto out_err;
22c213
         }
22c213
@@ -1702,7 +1734,7 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
22c213
 
22c213
         fuse_reply_buf(req, value, ret);
22c213
     } else {
22c213
-        ret = listxattr(procname, NULL, 0);
22c213
+        ret = flistxattr(fd, NULL, 0);
22c213
         if (ret == -1) {
22c213
             goto out_err;
22c213
         }
22c213
@@ -1711,6 +1743,10 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
22c213
     }
22c213
 out_free:
22c213
     free(value);
22c213
+
22c213
+    if (fd >= 0) {
22c213
+        close(fd);
22c213
+    }
22c213
     return;
22c213
 
22c213
 out_err:
22c213
@@ -1724,9 +1760,11 @@ static void lo_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
22c213
                         const char *value, size_t size, int flags)
22c213
 {
22c213
     char procname[64];
22c213
+    struct lo_data *lo = lo_data(req);
22c213
     struct lo_inode *inode;
22c213
     ssize_t ret;
22c213
     int saverr;
22c213
+    int fd = -1;
22c213
 
22c213
     inode = lo_inode(req, ino);
22c213
     if (!inode) {
22c213
@@ -1751,21 +1789,31 @@ static void lo_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
22c213
         goto out;
22c213
     }
22c213
 
22c213
-    sprintf(procname, "/proc/self/fd/%i", inode->fd);
22c213
+    sprintf(procname, "%i", inode->fd);
22c213
+    fd = openat(lo->proc_self_fd, procname, O_RDWR);
22c213
+    if (fd < 0) {
22c213
+        saverr = errno;
22c213
+        goto out;
22c213
+    }
22c213
 
22c213
-    ret = setxattr(procname, name, value, size, flags);
22c213
+    ret = fsetxattr(fd, name, value, size, flags);
22c213
     saverr = ret == -1 ? errno : 0;
22c213
 
22c213
 out:
22c213
+    if (fd >= 0) {
22c213
+        close(fd);
22c213
+    }
22c213
     fuse_reply_err(req, saverr);
22c213
 }
22c213
 
22c213
 static void lo_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name)
22c213
 {
22c213
     char procname[64];
22c213
+    struct lo_data *lo = lo_data(req);
22c213
     struct lo_inode *inode;
22c213
     ssize_t ret;
22c213
     int saverr;
22c213
+    int fd = -1;
22c213
 
22c213
     inode = lo_inode(req, ino);
22c213
     if (!inode) {
22c213
@@ -1789,12 +1837,20 @@ static void lo_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name)
22c213
         goto out;
22c213
     }
22c213
 
22c213
-    sprintf(procname, "/proc/self/fd/%i", inode->fd);
22c213
+    sprintf(procname, "%i", inode->fd);
22c213
+    fd = openat(lo->proc_self_fd, procname, O_RDWR);
22c213
+    if (fd < 0) {
22c213
+        saverr = errno;
22c213
+        goto out;
22c213
+    }
22c213
 
22c213
-    ret = removexattr(procname, name);
22c213
+    ret = fremovexattr(fd, name);
22c213
     saverr = ret == -1 ? errno : 0;
22c213
 
22c213
 out:
22c213
+    if (fd >= 0) {
22c213
+        close(fd);
22c213
+    }
22c213
     fuse_reply_err(req, saverr);
22c213
 }
22c213
 
22c213
@@ -1887,12 +1943,25 @@ static void print_capabilities(void)
22c213
     printf("}\n");
22c213
 }
22c213
 
22c213
+static void setup_proc_self_fd(struct lo_data *lo)
22c213
+{
22c213
+    lo->proc_self_fd = open("/proc/self/fd", O_PATH);
22c213
+    if (lo->proc_self_fd == -1) {
22c213
+        fuse_log(FUSE_LOG_ERR, "open(/proc/self/fd, O_PATH): %m\n");
22c213
+        exit(1);
22c213
+    }
22c213
+}
22c213
+
22c213
 int main(int argc, char *argv[])
22c213
 {
22c213
     struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
22c213
     struct fuse_session *se;
22c213
     struct fuse_cmdline_opts opts;
22c213
-    struct lo_data lo = { .debug = 0, .writeback = 0 };
22c213
+    struct lo_data lo = {
22c213
+        .debug = 0,
22c213
+        .writeback = 0,
22c213
+        .proc_self_fd = -1,
22c213
+    };
22c213
     struct lo_map_elem *root_elem;
22c213
     int ret = -1;
22c213
 
22c213
@@ -2003,6 +2072,9 @@ int main(int argc, char *argv[])
22c213
 
22c213
     fuse_daemonize(opts.foreground);
22c213
 
22c213
+    /* Must be after daemonize to get the right /proc/self/fd */
22c213
+    setup_proc_self_fd(&lo);
22c213
+
22c213
     /* Block until ctrl+c or fusermount -u */
22c213
     ret = virtio_loop(se);
22c213
 
22c213
@@ -2018,6 +2090,10 @@ err_out1:
22c213
     lo_map_destroy(&lo.dirp_map);
22c213
     lo_map_destroy(&lo.ino_map);
22c213
 
22c213
+    if (lo.proc_self_fd >= 0) {
22c213
+        close(lo.proc_self_fd);
22c213
+    }
22c213
+
22c213
     if (lo.root.fd >= 0) {
22c213
         close(lo.root.fd);
22c213
     }
22c213
-- 
22c213
1.8.3.1
22c213