thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone
ddf19c
From 69ac47502848c37ca3ede00f432c0675d9eef42c Mon Sep 17 00:00:00 2001
ddf19c
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
ddf19c
Date: Mon, 27 Jan 2020 19:01:18 +0100
ddf19c
Subject: [PATCH 047/116] virtiofsd: validate path components
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-44-dgilbert@redhat.com>
ddf19c
Patchwork-id: 93498
ddf19c
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 043/112] virtiofsd: validate path components
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
Several FUSE requests contain single path components.  A correct FUSE
ddf19c
client sends well-formed path components but there is currently no input
ddf19c
validation in case something went wrong or the client is malicious.
ddf19c
ddf19c
Refuse ".", "..", and paths containing '/' when we expect a path
ddf19c
component.
ddf19c
ddf19c
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
ddf19c
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
(cherry picked from commit 25dae28c58d7e706b5d5db99042c9db3cef2e657)
ddf19c
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
ddf19c
---
ddf19c
 tools/virtiofsd/passthrough_ll.c | 59 ++++++++++++++++++++++++++++++++++++----
ddf19c
 1 file changed, 53 insertions(+), 6 deletions(-)
ddf19c
ddf19c
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
ddf19c
index ac380ef..e375406 100644
ddf19c
--- a/tools/virtiofsd/passthrough_ll.c
ddf19c
+++ b/tools/virtiofsd/passthrough_ll.c
ddf19c
@@ -133,6 +133,21 @@ static void unref_inode(struct lo_data *lo, struct lo_inode *inode, uint64_t n);
ddf19c
 
ddf19c
 static struct lo_inode *lo_find(struct lo_data *lo, struct stat *st);
ddf19c
 
ddf19c
+static int is_dot_or_dotdot(const char *name)
ddf19c
+{
ddf19c
+    return name[0] == '.' &&
ddf19c
+           (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'));
ddf19c
+}
ddf19c
+
ddf19c
+/* Is `path` a single path component that is not "." or ".."? */
ddf19c
+static int is_safe_path_component(const char *path)
ddf19c
+{
ddf19c
+    if (strchr(path, '/')) {
ddf19c
+        return 0;
ddf19c
+    }
ddf19c
+
ddf19c
+    return !is_dot_or_dotdot(path);
ddf19c
+}
ddf19c
 
ddf19c
 static struct lo_data *lo_data(fuse_req_t req)
ddf19c
 {
ddf19c
@@ -681,6 +696,15 @@ static void lo_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
ddf19c
                  parent, name);
ddf19c
     }
ddf19c
 
ddf19c
+    /*
ddf19c
+     * Don't use is_safe_path_component(), allow "." and ".." for NFS export
ddf19c
+     * support.
ddf19c
+     */
ddf19c
+    if (strchr(name, '/')) {
ddf19c
+        fuse_reply_err(req, EINVAL);
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
     err = lo_do_lookup(req, parent, name, &e);
ddf19c
     if (err) {
ddf19c
         fuse_reply_err(req, err);
ddf19c
@@ -762,6 +786,11 @@ static void lo_mknod_symlink(fuse_req_t req, fuse_ino_t parent,
ddf19c
     struct fuse_entry_param e;
ddf19c
     struct lo_cred old = {};
ddf19c
 
ddf19c
+    if (!is_safe_path_component(name)) {
ddf19c
+        fuse_reply_err(req, EINVAL);
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
     dir = lo_inode(req, parent);
ddf19c
     if (!dir) {
ddf19c
         fuse_reply_err(req, EBADF);
ddf19c
@@ -863,6 +892,11 @@ static void lo_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t parent,
ddf19c
     struct fuse_entry_param e;
ddf19c
     int saverr;
ddf19c
 
ddf19c
+    if (!is_safe_path_component(name)) {
ddf19c
+        fuse_reply_err(req, EINVAL);
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
     inode = lo_inode(req, ino);
ddf19c
     if (!inode) {
ddf19c
         fuse_reply_err(req, EBADF);
ddf19c
@@ -904,6 +938,10 @@ out_err:
ddf19c
 static void lo_rmdir(fuse_req_t req, fuse_ino_t parent, const char *name)
ddf19c
 {
ddf19c
     int res;
ddf19c
+    if (!is_safe_path_component(name)) {
ddf19c
+        fuse_reply_err(req, EINVAL);
ddf19c
+        return;
ddf19c
+    }
ddf19c
 
ddf19c
     res = unlinkat(lo_fd(req, parent), name, AT_REMOVEDIR);
ddf19c
 
ddf19c
@@ -916,6 +954,11 @@ static void lo_rename(fuse_req_t req, fuse_ino_t parent, const char *name,
ddf19c
 {
ddf19c
     int res;
ddf19c
 
ddf19c
+    if (!is_safe_path_component(name) || !is_safe_path_component(newname)) {
ddf19c
+        fuse_reply_err(req, EINVAL);
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
     if (flags) {
ddf19c
         fuse_reply_err(req, EINVAL);
ddf19c
         return;
ddf19c
@@ -930,6 +973,11 @@ static void lo_unlink(fuse_req_t req, fuse_ino_t parent, const char *name)
ddf19c
 {
ddf19c
     int res;
ddf19c
 
ddf19c
+    if (!is_safe_path_component(name)) {
ddf19c
+        fuse_reply_err(req, EINVAL);
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
     res = unlinkat(lo_fd(req, parent), name, 0);
ddf19c
 
ddf19c
     fuse_reply_err(req, res == -1 ? errno : 0);
ddf19c
@@ -1093,12 +1141,6 @@ out_err:
ddf19c
     fuse_reply_err(req, error);
ddf19c
 }
ddf19c
 
ddf19c
-static int is_dot_or_dotdot(const char *name)
ddf19c
-{
ddf19c
-    return name[0] == '.' &&
ddf19c
-           (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'));
ddf19c
-}
ddf19c
-
ddf19c
 static void lo_do_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
ddf19c
                           off_t offset, struct fuse_file_info *fi, int plus)
ddf19c
 {
ddf19c
@@ -1248,6 +1290,11 @@ static void lo_create(fuse_req_t req, fuse_ino_t parent, const char *name,
ddf19c
                  parent, name);
ddf19c
     }
ddf19c
 
ddf19c
+    if (!is_safe_path_component(name)) {
ddf19c
+        fuse_reply_err(req, EINVAL);
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
     err = lo_change_cred(req, &old;;
ddf19c
     if (err) {
ddf19c
         goto out;
ddf19c
-- 
ddf19c
1.8.3.1
ddf19c