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

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