yeahuh / rpms / qemu-kvm

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