cryptospore / 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

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