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