All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg Kurz <groug@kaod.org>
To: qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>,
	"Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com>,
	Greg Kurz <groug@kaod.org>
Subject: [Qemu-devel] [PULL 04/28] 9pfs: introduce relative_openat_nofollow() helper
Date: Tue, 28 Feb 2017 11:30:16 +0100	[thread overview]
Message-ID: <1488277840-18608-5-git-send-email-groug@kaod.org> (raw)
In-Reply-To: <1488277840-18608-1-git-send-email-groug@kaod.org>

When using the passthrough security mode, symbolic links created by the
guest are actual symbolic links on the host file system.

Since the resolution of symbolic links during path walk is supposed to
occur on the client side. The server should hence never receive any path
pointing to an actual symbolic link. This isn't guaranteed by the protocol
though, and malicious code in the guest can trick the server to issue
various syscalls on paths whose one or more elements are symbolic links.
In the case of the "local" backend using the "passthrough" or "none"
security modes, the guest can directly create symbolic links to arbitrary
locations on the host (as per spec). The "mapped-xattr" and "mapped-file"
security modes are also affected to a lesser extent as they require some
help from an external entity to create actual symbolic links on the host,
i.e. another guest using "passthrough" mode for example.

The current code hence relies on O_NOFOLLOW and "l*()" variants of system
calls. Unfortunately, this only applies to the rightmost path component.
A guest could maliciously replace any component in a trusted path with a
symbolic link. This could allow any guest to escape a virtfs shared folder.

This patch introduces a variant of the openat() syscall that successively
opens each path element with O_NOFOLLOW. When passing a file descriptor
pointing to a trusted directory, one is guaranteed to be returned a
file descriptor pointing to a path which is beneath the trusted directory.
This will be used by subsequent patches to implement symlink-safe path walk
for any access to the backend.

Symbolic links aren't the only threats actually: a malicious guest could
change a path element to point to other types of file with undesirable
effects:
- a named pipe or any other thing that would cause openat() to block
- a terminal device which would become QEMU's controlling terminal

These issues can be addressed with O_NONBLOCK and O_NOCTTY.

Two helpers are introduced: one to open intermediate path elements and one
to open the rightmost path element.

Suggested-by: Jann Horn <jannh@google.com>
Signed-off-by: Greg Kurz <groug@kaod.org>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
(renamed openat_nofollow() to relative_openat_nofollow(),
 assert path is relative and doesn't contain '//',
 fixed side-effect in assert, Greg Kurz)
Signed-off-by: Greg Kurz <groug@kaod.org>
---
 hw/9pfs/9p-util.c     | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/9pfs/9p-util.h     | 50 ++++++++++++++++++++++++++++++++++++++++++++
 hw/9pfs/Makefile.objs |  2 +-
 3 files changed, 108 insertions(+), 1 deletion(-)
 create mode 100644 hw/9pfs/9p-util.c
 create mode 100644 hw/9pfs/9p-util.h

diff --git a/hw/9pfs/9p-util.c b/hw/9pfs/9p-util.c
new file mode 100644
index 000000000000..54134b0bb424
--- /dev/null
+++ b/hw/9pfs/9p-util.c
@@ -0,0 +1,57 @@
+/*
+ * 9p utilities
+ *
+ * Copyright IBM, Corp. 2017
+ *
+ * Authors:
+ *  Greg Kurz <groug@kaod.org>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "9p-util.h"
+
+int relative_openat_nofollow(int dirfd, const char *path, int flags,
+                             mode_t mode)
+{
+    int fd;
+
+    fd = dup(dirfd);
+    if (fd == -1) {
+        return -1;
+    }
+
+    while (*path) {
+        const char *c;
+        int next_fd;
+        char *head;
+
+        /* Only relative paths without consecutive slashes */
+        assert(path[0] != '/');
+
+        head = g_strdup(path);
+        c = strchr(path, '/');
+        if (c) {
+            head[c - path] = 0;
+            next_fd = openat_dir(fd, head);
+        } else {
+            next_fd = openat_file(fd, head, flags, mode);
+        }
+        g_free(head);
+        if (next_fd == -1) {
+            close_preserve_errno(fd);
+            return -1;
+        }
+        close(fd);
+        fd = next_fd;
+
+        if (!c) {
+            break;
+        }
+        path = c + 1;
+    }
+
+    return fd;
+}
diff --git a/hw/9pfs/9p-util.h b/hw/9pfs/9p-util.h
new file mode 100644
index 000000000000..e80b5a551d0a
--- /dev/null
+++ b/hw/9pfs/9p-util.h
@@ -0,0 +1,50 @@
+/*
+ * 9p utilities
+ *
+ * Copyright IBM, Corp. 2017
+ *
+ * Authors:
+ *  Greg Kurz <groug@kaod.org>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_9P_UTIL_H
+#define QEMU_9P_UTIL_H
+
+static inline void close_preserve_errno(int fd)
+{
+    int serrno = errno;
+    close(fd);
+    errno = serrno;
+}
+
+static inline int openat_dir(int dirfd, const char *name)
+{
+    return openat(dirfd, name, O_DIRECTORY | O_RDONLY | O_PATH);
+}
+
+static inline int openat_file(int dirfd, const char *name, int flags,
+                              mode_t mode)
+{
+    int fd, serrno, ret;
+
+    fd = openat(dirfd, name, flags | O_NOFOLLOW | O_NOCTTY | O_NONBLOCK,
+                mode);
+    if (fd == -1) {
+        return -1;
+    }
+
+    serrno = errno;
+    /* O_NONBLOCK was only needed to open the file. Let's drop it. */
+    ret = fcntl(fd, F_SETFL, flags);
+    assert(!ret);
+    errno = serrno;
+    return fd;
+}
+
+int relative_openat_nofollow(int dirfd, const char *path, int flags,
+                             mode_t mode);
+
+#endif
diff --git a/hw/9pfs/Makefile.objs b/hw/9pfs/Makefile.objs
index da0ae0cfdbae..32197e6671dd 100644
--- a/hw/9pfs/Makefile.objs
+++ b/hw/9pfs/Makefile.objs
@@ -1,4 +1,4 @@
-common-obj-y  = 9p.o
+common-obj-y  = 9p.o 9p-util.o
 common-obj-y += 9p-local.o 9p-xattr.o
 common-obj-y += 9p-xattr-user.o 9p-posix-acl.o
 common-obj-y += coth.o cofs.o codir.o cofile.o
-- 
2.7.4

  parent reply	other threads:[~2017-02-28 10:31 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-28 10:30 [Qemu-devel] [PULL 00/28] 9p CVE-2016-9602 fixes 2017-02-28 for 2.9 soft freeze Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 01/28] 9pfs: local: move xattr security ops to 9p-xattr.c Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 02/28] 9pfs: remove side-effects in local_init() Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 03/28] 9pfs: remove side-effects in local_open() and local_opendir() Greg Kurz
2017-02-28 10:30 ` Greg Kurz [this message]
2017-02-28 10:30 ` [Qemu-devel] [PULL 05/28] 9pfs: local: keep a file descriptor on the shared folder Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 06/28] 9pfs: local: open/opendir: don't follow symlinks Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 07/28] 9pfs: local: lgetxattr: " Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 08/28] 9pfs: local: llistxattr: " Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 09/28] 9pfs: local: lsetxattr: " Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 10/28] 9pfs: local: lremovexattr: " Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 11/28] 9pfs: local: unlinkat: " Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 12/28] 9pfs: local: remove: " Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 13/28] 9pfs: local: utimensat: " Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 14/28] 9pfs: local: statfs: " Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 15/28] 9pfs: local: truncate: " Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 16/28] 9pfs: local: readlink: " Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 17/28] 9pfs: local: lstat: " Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 18/28] 9pfs: local: renameat: " Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 19/28] 9pfs: local: rename: use renameat Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 20/28] 9pfs: local: improve error handling in link op Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 21/28] 9pfs: local: link: don't follow symlinks Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 22/28] 9pfs: local: chmod: " Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 23/28] 9pfs: local: chown: " Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 24/28] 9pfs: local: symlink: " Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 25/28] 9pfs: local: mknod: " Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 26/28] 9pfs: local: mkdir: " Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 27/28] 9pfs: local: open2: " Greg Kurz
2017-02-28 10:30 ` [Qemu-devel] [PULL 28/28] 9pfs: local: drop unused code Greg Kurz
2017-02-28 14:02 ` [Qemu-devel] [PULL 00/28] 9p CVE-2016-9602 fixes 2017-02-28 for 2.9 soft freeze Michael Tokarev
2017-02-28 14:22   ` Greg Kurz
2017-02-28 14:55     ` Michael Tokarev
2017-02-28 15:11       ` Greg Kurz
2017-02-28 16:01       ` Daniel P. Berrange
2017-02-28 16:09       ` Pranith Kumar
2017-03-01 14:33 ` Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1488277840-18608-5-git-send-email-groug@kaod.org \
    --to=groug@kaod.org \
    --cc=aneesh.kumar@linux.vnet.ibm.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.