All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/7] hw/9pfs: local_open_nofollow(): abort on illegal FID path components
  2026-11-23 15:31 [PATCH 0/7] 9pfs: hardening against export root escape (CVE-2026-93834) Christian Schoenebeck
@ 2026-11-23 15:24 ` Christian Schoenebeck
  2026-11-23 15:24 ` [PATCH 2/7] hw/9pfs: move struct LocalData into 9p-local.h Christian Schoenebeck
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Christian Schoenebeck @ 2026-11-23 15:24 UTC (permalink / raw)
  To: qemu-devel; +Cc: Greg Kurz

Harden local_open_nofollow() against potentially corrupted,
malicious paths.

CVE-2026-93834 (fixed by fe9e30899ff) allowed guests to escape from
9p server's export root. Due to that severe impact, it is therefore
worth hardening the 9p server at relevant, affected code locations.

The target victim of that CVE was local_open_nofollow(), which is
9p server's central place for opening files and directories. This
function opens the passed FID path by walking and opening one path
element at a time, starting from the 9p export root directory. And
the key mechanism exploited by the CVE was to smuggle ".." elements
into the path, which eventually caused local_open_nofollow() to
open files outside of the allowed 9p exported root directory.

That could be exploited by the CVE this way, because so far
local_open_nofollow() relied on local_name_to_path() having
translated a client-side path into a safe host-side path
representation (which especially includes having stripped all ".."
path elements already), before that path was ever passed to
local_open_nofollow().

And that assumption of local_open_nofollow() was actually valid,
because the code structure guaranteed that local_name_to_path()
was always called before the (translated / defused) path would
eventually reach local_open_nofollow().

However the CVE used a side effect, by exploiting a concurrency
issue, which allowed it to mutate the path after the path has
already been translated by local_name_to_path(), eventually
causing local_open_nofollow() to blindly follow a crafted,
malicious path.

Since local_open_nofollow() is such a fragile place, and since
concurrency issues can be complex for being completely ruled out
(especially with C), we no longer blindly rely on a path that
has been passed to local_open_nofollow() to be always sane, so:

 - Copy the passed path as very first action, so nobody else
   could mutate it while we are checking and processing it.

Then from there, by using the path copy only:

 - Check the path for any ".." element.

 - Check the path for not being an absolute path.

 - Check the path to not contain any empty path element.

If the path fails any of these checks, abort the QEMU process
deliberately loudly instead of trying to return a regular error
to client. Because if we detected a bad path at this point of
the code, it means somebody already managed to corrupt the overall
server state. So better to die immediately and loudly with a clear
admin message, than giving an attacker the chance to make the
situation even worse.

Signed-off-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
---
 hw/9pfs/9p-local.c | 71 ++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 68 insertions(+), 3 deletions(-)

diff --git a/hw/9pfs/9p-local.c b/hw/9pfs/9p-local.c
index ee592b62f8..710652dbca 100644
--- a/hw/9pfs/9p-local.c
+++ b/hw/9pfs/9p-local.c
@@ -58,24 +58,89 @@ typedef struct {
     int mountfd;
 } LocalData;
 
-int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags,
+int local_open_nofollow(FsContext *fs_ctx, const char *_path, int flags,
                         mode_t mode)
 {
     LocalData *data = fs_ctx->private;
     int fd = data->mountfd;
 
+    /*
+     * Hardening: snapshot the FID path to prevent any mutations (by
+     * another thread) during the checks and individual directory
+     * walks below (i.e. preventing TOCTOU issues). This is usually
+     * not expected to happen, as we are holding a read lock on the
+     * path, but the impact of CVE-2026-93834 deserves more robust
+     * handling.
+     */
+    g_autofree char *snap = g_strdup(_path);
+    const char *path = snap;
+
+    if (!*path) {
+        /*
+         * Hardening: check for empty string.
+         *
+         * An empty string means the FID path is corrupted (the
+         * export's root FID path is a single "."), so stop with a
+         * loud failure instead of serving a corrupted state.
+         */
+        error_report("9pfs: FATAL: corrupted state detected: empty FID "
+                     "path");
+        abort();
+    }
+
     while (*path && fd != -1) {
         const char *c;
         int next_fd;
         char *head;
 
-        /* Only relative paths without consecutive slashes */
-        assert(*path != '/');
+        /*
+         * Hardening: check for leading slash.
+         *
+         * An absolute path or an empty path element at this point
+         * means the path is corrupted (the client supplied path was
+         * already translated by local_name_to_path() to a safe host
+         * path before).
+         */
+        if (*path == '/') {
+            /* Do not print this path: it is corrupted. */
+            error_report("9pfs: FATAL: corrupted state detected: FID "
+                         "path contains a leading or empty path "
+                         "component");
+            abort();
+        }
 
         head = g_strdup(path);
         c = qemu_strchrnul(path, '/');
+        /*
+         * Hardening: check for "..".
+         *
+         * Relative ".." path elements were already translated by
+         * local_name_to_path() before, so a ".." element at this
+         * point means a corrupted path.
+         */
+        if (c - path == 2 && head[0] == '.' && head[1] == '.') {
+            /* Do not print this path: it is corrupted. */
+            error_report("9pfs: FATAL: corrupted state detected: FID "
+                         "path contains a '..' component");
+            abort();
+        }
         if (*c) {
             /* Intermediate path element */
+
+            /*
+             * Hardening: check for trailing slash.
+             *
+             * Prevents calling openat() with an empty string (""). It
+             * would not actually be fatal, but this can only happen
+             * with a corrupted path. So abort, just to be sure.
+             */
+            if (c[1] == '\0') {
+                /* Do not print this path: it is corrupted. */
+                error_report("9pfs: FATAL: corrupted state detected: "
+                             "FID path ends with a '/' (empty final "
+                             "component)");
+                abort();
+            }
             head[c - path] = 0;
             path = c + 1;
             next_fd = openat_dir(fd, head);
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 2/7] hw/9pfs: move struct LocalData into 9p-local.h
  2026-11-23 15:31 [PATCH 0/7] 9pfs: hardening against export root escape (CVE-2026-93834) Christian Schoenebeck
  2026-11-23 15:24 ` [PATCH 1/7] hw/9pfs: local_open_nofollow(): abort on illegal FID path components Christian Schoenebeck
@ 2026-11-23 15:24 ` Christian Schoenebeck
  2026-11-23 15:24 ` [PATCH 3/7] meson: move 9pfs-util selection to toplevel meson file Christian Schoenebeck
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Christian Schoenebeck @ 2026-11-23 15:24 UTC (permalink / raw)
  To: qemu-devel; +Cc: Greg Kurz

LocalData is the fs driver implementation specific data structure of
the local 9p backend. Its declaration is currently restricted to
9p-local.c.

Move it into 9p-local.h so that the subsequently added new test
suite can use the 9p-local function without having to re-declare
this struct on test site.

Signed-off-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
---
 hw/9pfs/9p-local.c | 4 ----
 hw/9pfs/9p-local.h | 4 ++++
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/hw/9pfs/9p-local.c b/hw/9pfs/9p-local.c
index 710652dbca..8c8cf53bad 100644
--- a/hw/9pfs/9p-local.c
+++ b/hw/9pfs/9p-local.c
@@ -54,10 +54,6 @@
 #define BTRFS_SUPER_MAGIC 0x9123683E
 #endif
 
-typedef struct {
-    int mountfd;
-} LocalData;
-
 int local_open_nofollow(FsContext *fs_ctx, const char *_path, int flags,
                         mode_t mode)
 {
diff --git a/hw/9pfs/9p-local.h b/hw/9pfs/9p-local.h
index 32c72749d9..b61f894195 100644
--- a/hw/9pfs/9p-local.h
+++ b/hw/9pfs/9p-local.h
@@ -13,6 +13,10 @@
 #ifndef QEMU_9P_LOCAL_H
 #define QEMU_9P_LOCAL_H
 
+typedef struct {
+    int mountfd;
+} LocalData;
+
 int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags,
                         mode_t mode);
 int local_opendir_nofollow(FsContext *fs_ctx, const char *path);
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 3/7] meson: move 9pfs-util selection to toplevel meson file
  2026-11-23 15:31 [PATCH 0/7] 9pfs: hardening against export root escape (CVE-2026-93834) Christian Schoenebeck
  2026-11-23 15:24 ` [PATCH 1/7] hw/9pfs: local_open_nofollow(): abort on illegal FID path components Christian Schoenebeck
  2026-11-23 15:24 ` [PATCH 2/7] hw/9pfs: move struct LocalData into 9p-local.h Christian Schoenebeck
@ 2026-11-23 15:24 ` Christian Schoenebeck
  2026-11-23 15:24 ` [PATCH 4/7] tests/unit: add forktest.h, a forked-child run-and-verify helper Christian Schoenebeck
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Christian Schoenebeck @ 2026-11-23 15:24 UTC (permalink / raw)
  To: qemu-devel
  Cc: Greg Kurz, Paolo Bonzini, Marc-André Lureau,
	Daniel P. Berrangé, Pierrick Bouvier

9p-util-<host>.c are host system dependent function implementations.
For each supported system there is a separate source file implementation
which meson automatically selects.

Move this selection from hw/9pfs/meson.build to toplevel meson.build,
such that hw/9pfs/ and the subsequently added new test suite (under
tests/unit/) can share the selection as variable 'fs_9p_host_util'.

Signed-off-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
---
 hw/9pfs/meson.build |  8 ++------
 meson.build         | 10 ++++++++++
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/hw/9pfs/meson.build b/hw/9pfs/meson.build
index 91b51af838..3232aa4aaa 100644
--- a/hw/9pfs/meson.build
+++ b/hw/9pfs/meson.build
@@ -13,12 +13,8 @@ fs_ss.add(files(
   'coth.c',
   'coxattr.c',
 ))
-if host_os == 'darwin'
-  fs_ss.add(files('9p-util-darwin.c'))
-elif host_os == 'freebsd'
-  fs_ss.add(files('9p-util-freebsd.c'))
-elif host_os == 'linux'
-  fs_ss.add(files('9p-util-linux.c'))
+if fs_9p_host_util != ''
+  fs_ss.add(files(fs_9p_host_util))
 endif
 fs_ss.add(when: 'CONFIG_XEN_BUS', if_true: files('xen-9p-backend.c'))
 system_ss.add_all(when: 'CONFIG_FSDEV_9P', if_true: fs_ss)
diff --git a/meson.build b/meson.build
index 4d267e10b5..feeb4a029c 100644
--- a/meson.build
+++ b/meson.build
@@ -2348,6 +2348,16 @@ have_virtfs = get_option('virtfs') \
     .disable_auto_if(not have_tools and not have_system) \
     .allowed()
 
+if host_os == 'linux'
+  fs_9p_host_util = '9p-util-linux.c'
+elif host_os == 'darwin'
+  fs_9p_host_util = '9p-util-darwin.c'
+elif host_os == 'freebsd'
+  fs_9p_host_util = '9p-util-freebsd.c'
+else
+  fs_9p_host_util = ''
+endif
+
 qga_fsfreeze = false
 qga_fstrim = false
 if host_os == 'linux'
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 4/7] tests/unit: add forktest.h, a forked-child run-and-verify helper
  2026-11-23 15:31 [PATCH 0/7] 9pfs: hardening against export root escape (CVE-2026-93834) Christian Schoenebeck
                   ` (2 preceding siblings ...)
  2026-11-23 15:24 ` [PATCH 3/7] meson: move 9pfs-util selection to toplevel meson file Christian Schoenebeck
@ 2026-11-23 15:24 ` Christian Schoenebeck
  2026-11-23 15:25 ` [PATCH 5/7] tests/unit: add test-9p-internals.c Christian Schoenebeck
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Christian Schoenebeck @ 2026-11-23 15:24 UTC (permalink / raw)
  To: qemu-devel; +Cc: Greg Kurz

We are going to test QEMU internal functions (actual tests added
by subsequent patch) which are expected to abort() the running
process on certain conditions. So we are going to simulate those
conditions, and aborting the process is hence the sane result of
those test cases.

Since aborting the process would also terminate the test and
falsely yield a failed test result; we therefore need to fork and
run these special tests from a separate subprocess. That way we
can detect from the parent process whether the subprocess was
aborted as expected, and then proclaim the test case as succeeded.

This header file provides the general purpose boilerplate code to
run this kind of forked test cases.

Signed-off-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
---
 MAINTAINERS           |   1 +
 tests/unit/forktest.h | 132 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 133 insertions(+)
 create mode 100644 tests/unit/forktest.h

diff --git a/MAINTAINERS b/MAINTAINERS
index 40bf63651c..0f4f1ee442 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2609,6 +2609,7 @@ X: hw/9pfs/xen-9p*
 F: fsdev/
 F: tests/qtest/virtio-9p-test.c
 F: tests/qtest/virtio-9p-race-test.c
+F: tests/unit/forktest.h
 F: tests/qtest/libqos/virtio-9p*
 T: git https://github.com/cschoenebeck/qemu.git 9p.next
 
diff --git a/tests/unit/forktest.h b/tests/unit/forktest.h
new file mode 100644
index 0000000000..ece5ccd835
--- /dev/null
+++ b/tests/unit/forktest.h
@@ -0,0 +1,132 @@
+/*
+ * Boilerplate code for running tests forked to a subprocess
+ * (e.g. for testing functions to abort() the running process as expected)
+ *
+ * Copyright (c) 2026 Christian Schoenebeck <qemu_oss@crudebyte.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef FORKTEST_H
+#define FORKTEST_H
+
+#include <signal.h>
+#include <sys/wait.h>
+
+/* Test case passed without aborting, as expected. */
+#define FORKTEST_EXIT_OK 0
+/* Test case failed because it did not abort even though expected to abort. */
+#define FORKTEST_EXIT_SURVIVED 124
+/* Test case (subprocess) returned without calling expected forktest_exit(). */
+#define FORKTEST_EXIT_MISSED 255
+
+/*
+ * The only way out of a forktest child: terminates it without
+ * running the atexit, glib or ASan teardown inherited from the
+ * parent. @code is one of the FORKTEST_EXIT codes above.
+ */
+static inline void forktest_exit(int code)
+{
+    _exit(code);
+}
+
+/* Wraps the actual test code to run, forked into a subprocess. */
+typedef struct {
+    /* entry point of the test case (to be executed on a subprocess) */
+    void (*child_fn)(void *);
+    /* user data passed to @child_fn */
+    void *opaque;
+    /* defines the success criteria: whether subprocess is expected to abort */
+    bool expect_abort;
+    /* printed as context on failure cases */
+    const char *name;
+} ForkTest;
+
+static inline bool forktest_aborted(int status)
+{
+    return WIFSIGNALED(status) && WTERMSIG(status) == SIGABRT;
+}
+
+static inline bool forktest_exited(int status, int code)
+{
+    return WIFEXITED(status) && WEXITSTATUS(status) == code;
+}
+
+/* launch (fork and run) the passed test case in a subprocess */
+static inline void forktest_run(ForkTest test)
+{
+    pid_t pid;
+    int status = 0;
+
+    pid = fork();
+    if (pid == -1) {
+        g_error("fork failed: %s", g_strerror(errno));
+    }
+    if (pid == 0) {
+        test.child_fn(test.opaque);
+        forktest_exit(FORKTEST_EXIT_MISSED);
+    }
+    if (waitpid(pid, &status, 0) != pid) {
+        g_error("waitpid failed: %s", g_strerror(errno));
+    }
+    if (forktest_aborted(status)) {
+        if (!test.expect_abort) {
+            g_error("child on %s aborted unexpectedly",
+                    test.name);
+        }
+    } else if (test.expect_abort &&
+               forktest_exited(status, FORKTEST_EXIT_SURVIVED)) {
+        g_error("child on %s survived; the case was expected to "
+                "abort", test.name);
+    } else if (!test.expect_abort &&
+               forktest_exited(status, FORKTEST_EXIT_OK)) {
+        /* passed cleanly */
+    } else {
+        g_error("child on %s died unexpectedly (status 0x%x)",
+                test.name, status);
+    }
+}
+
+/*
+ * Setup and teardown tasks to be executed before and after a test case
+ * respectively, sharable between several test cases.
+ */
+typedef struct {
+    void *(*setup_fn)(void);
+    void (*teardown_fn)(void *data);
+} ForkTestEnv;
+
+/* internal: g_test data of the trampoline below */
+struct ForkTestRun {
+    const ForkTestEnv *env;
+    void (*test_fn)(void *data);
+};
+
+/* internal trampoline function */
+static inline void forktest_case_run(const void *opaque)
+{
+    const struct ForkTestRun *tc = opaque;
+    void *state = NULL;
+
+    if (tc->env) {
+        state = tc->env->setup_fn();
+    }
+    tc->test_fn(state);
+    if (tc->env) {
+        tc->env->teardown_fn(state);
+    }
+}
+
+/* register a test case; @env may be NULL */
+static inline void forktest_add(const char *path,
+                                void (*test_fn)(void *data),
+                                const ForkTestEnv *env)
+{
+    struct ForkTestRun *tc = g_new(struct ForkTestRun, 1);
+
+    tc->test_fn = test_fn;
+    tc->env = env;
+    g_test_add_data_func_full(path, tc, forktest_case_run, g_free);
+}
+
+#endif
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 5/7] tests/unit: add test-9p-internals.c
  2026-11-23 15:31 [PATCH 0/7] 9pfs: hardening against export root escape (CVE-2026-93834) Christian Schoenebeck
                   ` (3 preceding siblings ...)
  2026-11-23 15:24 ` [PATCH 4/7] tests/unit: add forktest.h, a forked-child run-and-verify helper Christian Schoenebeck
@ 2026-11-23 15:25 ` Christian Schoenebeck
  2026-11-23 15:25 ` [PATCH 6/7] hw/9pfs: local: abort on corrupt V9fsPath objects Christian Schoenebeck
  2026-11-23 15:25 ` [PATCH 7/7] tests/9p-internals: test 9p's local_path_sanity_check() function Christian Schoenebeck
  6 siblings, 0 replies; 8+ messages in thread
From: Christian Schoenebeck @ 2026-11-23 15:25 UTC (permalink / raw)
  To: qemu-devel; +Cc: Greg Kurz

Add a new 9pfs test-suite that tests 9pfs server-internal
functions, i.e. by calling these server internal functions directly.

This first patch tests the server internal local_open_nofollow()
function of the 9p local backend, specifically that it would abort
the QEMU process upon illegal FID paths being fed to it.

Signed-off-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
---
 MAINTAINERS                    |   1 +
 tests/unit/meson.build         |  22 ++++
 tests/unit/test-9p-internals.c | 197 +++++++++++++++++++++++++++++++++
 3 files changed, 220 insertions(+)
 create mode 100644 tests/unit/test-9p-internals.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 0f4f1ee442..5daea6d6e0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2609,6 +2609,7 @@ X: hw/9pfs/xen-9p*
 F: fsdev/
 F: tests/qtest/virtio-9p-test.c
 F: tests/qtest/virtio-9p-race-test.c
+F: tests/unit/test-9p-internals.c
 F: tests/unit/forktest.h
 F: tests/qtest/libqos/virtio-9p*
 T: git https://github.com/cschoenebeck/qemu.git 9p.next
diff --git a/tests/unit/meson.build b/tests/unit/meson.build
index 37b561d4a7..6e7dd2829a 100644
--- a/tests/unit/meson.build
+++ b/tests/unit/meson.build
@@ -52,6 +52,28 @@ tests = {
   'test-envlist': [],
 }
 
+if have_virtfs
+  tests += {
+    'test-9p-internals': [files('../../hw/9pfs/9p.c',
+                                '../../hw/9pfs/9p-local.c',
+                                '../../hw/9pfs/9p-xattr.c',
+                                '../../hw/9pfs/9p-xattr-user.c',
+                                '../../hw/9pfs/9p-posix-acl.c',
+                                '../../hw/9pfs/' + fs_9p_host_util,
+                                '../../hw/9pfs/9p-util-generic.c',
+                                '../../hw/9pfs/codir.c',
+                                '../../hw/9pfs/cofile.c',
+                                '../../hw/9pfs/cofs.c',
+                                '../../hw/9pfs/coxattr.c',
+                                '../../hw/9pfs/coth.c',
+                                '../../fsdev/9p-marshal.c',
+                                '../../fsdev/9p-iov-marshal.c',
+                                '../../fsdev/qemu-fsdev.c',
+                                '../../hw/9pfs/9p-synth.c',
+                                '../../fsdev/qemu-fsdev-throttle.c')],
+  }
+endif
+
 if have_system or have_tools
   tests += {
     'test-qmp-event': [testqapi],
diff --git a/tests/unit/test-9p-internals.c b/tests/unit/test-9p-internals.c
new file mode 100644
index 0000000000..61c7fc7779
--- /dev/null
+++ b/tests/unit/test-9p-internals.c
@@ -0,0 +1,197 @@
+/*
+ * tests of 9p server's internal functions
+ *
+ * Copyright (c) 2026 Christian Schoenebeck <qemu_oss@crudebyte.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+/*
+ * Not so fast! You might want to read the 9p developer docs first:
+ * https://wiki.qemu.org/Documentation/9p
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/module.h"
+#include "fsdev/file-op-9p.h"
+#include "hw/9pfs/9p-local.h"
+#include "forktest.h"
+#include <stdlib.h>
+
+/* one filesystem entry of this test */
+typedef struct {
+    const char *path;
+    bool is_dir;
+} V9pInternalsFileTreeEntry;
+
+/* the test's filesystem tree */
+static const V9pInternalsFileTreeEntry v9p_internals_file_tree[] = {
+    { "export_root", true },
+    { "export_root/deep_dir", true },
+    { "outside.txt", false },
+    { "export_root/inside.txt", false },
+    { "export_root/...", false },
+    { "export_root/....", false },
+};
+
+/* valid paths expected to be allowed by the test filesystem */
+static const char *v9p_internals_legal_paths[] = {
+    "inside.txt",
+    ".", /* export root */
+    "...",
+    "....",
+};
+
+/* illegal paths expected to cause an abort */
+static const char *v9p_internals_corrupt_paths[] = {
+    /* attempts export root escape via ".." */
+    "deep_dir/../../outside.txt",
+    /* attempts export root escape via absolute path */
+    "/outside.txt",
+    /* empty path component (openat(fd, "")) */
+    "deep_dir//outside.txt",
+    /* trailing slash (openat(fd, "")) */
+    "deep_dir/",
+    /* empty path (openat(fd, "")) */
+    "",
+};
+
+/* shared test-suite state */
+typedef struct {
+    LocalData localdata;
+    FsContext ctx;
+    char *test_dir;
+} V9pInternalsState;
+
+/* create the tmp directory for the test's filesystem tree */
+static char *v9p_internals_create_test_dir(void)
+{
+    char *test_dir = g_dir_make_tmp("9p-internals-XXXXXX", NULL);
+
+    g_assert_nonnull(test_dir);
+    return test_dir;
+}
+
+/* create regular file in the test filesystem */
+static void v9p_internals_create_file(const char *path)
+{
+    int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644);
+
+    g_assert_cmpint(fd, !=, -1);
+    g_assert_cmpint(close(fd), ==, 0);
+}
+
+/* create the filesystem tree for this test */
+static void v9p_internals_create_tree(const char *base)
+{
+    size_t i;
+
+    for (i = 0; i < ARRAY_SIZE(v9p_internals_file_tree); i++) {
+        const V9pInternalsFileTreeEntry *e = &v9p_internals_file_tree[i];
+        g_autofree char *path = g_build_filename(base, e->path, NULL);
+
+        if (e->is_dir) {
+            g_assert_cmpint(g_mkdir_with_parents(path, 0755), ==, 0);
+        } else {
+            v9p_internals_create_file(path);
+        }
+    }
+}
+
+/* prepare to run next test case */
+static void *v9p_internals_setup(void)
+{
+    V9pInternalsState *s = g_new0(V9pInternalsState, 1);
+    g_autofree char *export_root;
+
+    s->test_dir = v9p_internals_create_test_dir();
+    v9p_internals_create_tree(s->test_dir);
+    export_root = g_build_filename(s->test_dir, "export_root", NULL);
+    s->localdata.mountfd = open(export_root, O_RDONLY | O_DIRECTORY);
+    g_assert_cmpint(s->localdata.mountfd, !=, -1);
+    s->ctx.private = &s->localdata;
+    return s;
+}
+
+/* cleanup after running test case */
+static void v9p_internals_teardown(void *data)
+{
+    V9pInternalsState *s = data;
+    g_autofree char *cmd = g_strdup_printf("rm -fr '%s'\n", s->test_dir);
+    int res;
+
+    close(s->localdata.mountfd);
+    res = system(cmd);
+    if (res < 0) {
+        /* ignore any error */
+    }
+    g_free(s->test_dir);
+    g_free(s);
+}
+
+static const ForkTestEnv v9p_internals_env = {
+    .setup_fn = v9p_internals_setup,
+    .teardown_fn = v9p_internals_teardown,
+};
+
+static void v9p_internals_local_open_nofollow_legal_paths(void *data)
+{
+    V9pInternalsState *s = data;
+    size_t i;
+    int fd;
+
+    for (i = 0; i < ARRAY_SIZE(v9p_internals_legal_paths); i++) {
+        const char *path = v9p_internals_legal_paths[i];
+
+        fd = local_open_nofollow(&s->ctx, path, O_RDONLY, 0);
+        g_assert_cmpint(fd, !=, -1);
+        close(fd);
+    }
+}
+
+typedef struct {
+    FsContext *ctx;
+    const char *path;
+} V9pInternalsPathCase;
+
+static void v9p_internals_check_corrupt_path(void *opaque)
+{
+    V9pInternalsPathCase *c = opaque;
+
+    local_open_nofollow(c->ctx, c->path, O_RDONLY, 0);
+    forktest_exit(FORKTEST_EXIT_SURVIVED);
+}
+
+static void v9p_internals_local_open_nofollow_corrupt_paths(void *data)
+{
+    V9pInternalsState *s = data;
+    size_t i;
+
+    for (i = 0; i < ARRAY_SIZE(v9p_internals_corrupt_paths); i++) {
+        const char *path = v9p_internals_corrupt_paths[i];
+        V9pInternalsPathCase c = {
+            .ctx = &s->ctx,
+            .path = path,
+        };
+
+        forktest_run((ForkTest) {
+            .child_fn = v9p_internals_check_corrupt_path,
+            .opaque = &c,
+            .expect_abort = true,
+            .name = path,
+        });
+    }
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+
+    forktest_add("/9p-internals/local_open_nofollow/legal-paths",
+                 v9p_internals_local_open_nofollow_legal_paths,
+                 &v9p_internals_env);
+    forktest_add("/9p-internals/local_open_nofollow/corrupt-paths",
+                 v9p_internals_local_open_nofollow_corrupt_paths,
+                 &v9p_internals_env);
+    return g_test_run();
+}
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 6/7] hw/9pfs: local: abort on corrupt V9fsPath objects
  2026-11-23 15:31 [PATCH 0/7] 9pfs: hardening against export root escape (CVE-2026-93834) Christian Schoenebeck
                   ` (4 preceding siblings ...)
  2026-11-23 15:25 ` [PATCH 5/7] tests/unit: add test-9p-internals.c Christian Schoenebeck
@ 2026-11-23 15:25 ` Christian Schoenebeck
  2026-11-23 15:25 ` [PATCH 7/7] tests/9p-internals: test 9p's local_path_sanity_check() function Christian Schoenebeck
  6 siblings, 0 replies; 8+ messages in thread
From: Christian Schoenebeck @ 2026-11-23 15:25 UTC (permalink / raw)
  To: qemu-devel; +Cc: Greg Kurz

Harden several local fs backend driver functions against
corrupted paths.

This is another hardening patch as a consequence of
CVE-2026-93834 (fixed by fe9e30899ff).

 - Add a new function local_path_sanity_check() which checks
   the passed V9fsPath object for corruption, and abort the
   QEMU process if a corrupted path was detected.

 - Call local_path_sanity_check() from exactly 4 functions
   that verify V9fsPath objects: local_lstat(), local_open(),
   local_open2() and local_opendir().

A V9fsPath object consists of exactly two fields:

  struct V9fsPath {
      size_t size;
      char *data;
  };

In the local fs driver backend `data` is the path string, and
`size` is the size of the path string including the terminating
NUL character.

The mentioned CVE exploited an incorrectly synchronized
concurrency window to mutate the two fields in a diverging way,
such that `size` would no longer reflect the correct size of
string stored by `data`, and it additionally tricked a mutation
of a sub-portion of the string only (in order to bypass
validation of the actual string content).

The check is placed at exactly these 4 functions, because in them
the path's contents turn into actions on the host file system:
they open files or directory streams, or they look file attributes
up by path. The CVE's exploit acted at these places.

Plus, for local_lstat() specifically: making a path copy
(v9fs_path_copy()) during Twalk is the only code that uses the
path's size field to make a decision, and such a copy enters the
local backend at local_lstat() first.

The other local backend functions receive a V9fsPath argument,
too, but never use its size field, so a diverged pair cannot do
damage at their entrances. The path elements they walk get
checked inside local_open_nofollow() anyway.

Signed-off-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
---
 hw/9pfs/9p-local.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 hw/9pfs/9p-local.h |  1 +
 2 files changed, 45 insertions(+)

diff --git a/hw/9pfs/9p-local.c b/hw/9pfs/9p-local.c
index 8c8cf53bad..83658ce39d 100644
--- a/hw/9pfs/9p-local.c
+++ b/hw/9pfs/9p-local.c
@@ -54,6 +54,46 @@
 #define BTRFS_SUPER_MAGIC 0x9123683E
 #endif
 
+/* Hardening: check path, abort if corrupted path detected */
+void local_path_sanity_check(const V9fsPath *path)
+{
+    const char *data = path->data;
+    const size_t size = path->size;
+    size_t len;
+
+    if (!data) {
+        error_report("9pfs: FATAL: corrupted state detected: fid path "
+                     "has size %zu but no data", size);
+        abort();
+    }
+    /*
+     * 4 MiB is hard-coded and deliberately not PATH_MAX (usually
+     * 4k). The latter is the max. path size for a Linux syscall,
+     * which we legally surpass as we always walk and openat() paths
+     * element-wise.
+     */
+    if (size > 4 * 1024 * 1024) {
+        error_report("9pfs: FATAL: corrupted state detected: implausibly "
+                     "large fid path size %zu", size);
+        abort();
+    }
+    len = strnlen(data, size);
+    if (len != size - 1) {
+        /* Do not print the path, it is corrupted. */
+        if (len == size) {
+            error_report("9pfs: FATAL: corrupted state detected: fid "
+                         "path size %zu: no terminating NUL within the "
+                         "claimed window, string is at least %zu long",
+                         size, len);
+        } else {
+            error_report("9pfs: FATAL: corrupted state detected: fid "
+                         "path size %zu diverges from its actual "
+                         "string length %zu", size, len);
+        }
+        abort();
+    }
+}
+
 int local_open_nofollow(FsContext *fs_ctx, const char *_path, int flags,
                         mode_t mode)
 {
@@ -246,6 +286,7 @@ static void local_mapped_file_attr(int dirfd, const char *name,
 
 static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
 {
+    local_path_sanity_check(fs_path);
     int err = -1;
     char *dirpath = g_path_get_dirname(fs_path->data);
     char *name = g_path_get_basename(fs_path->data);
@@ -571,6 +612,7 @@ static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
 static int local_open(FsContext *ctx, V9fsPath *fs_path,
                       int flags, V9fsFidOpenState *fs)
 {
+    local_path_sanity_check(fs_path);
     int fd;
 
     fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
@@ -584,6 +626,7 @@ static int local_open(FsContext *ctx, V9fsPath *fs_path,
 static int local_opendir(FsContext *ctx,
                          V9fsPath *fs_path, V9fsFidOpenState *fs)
 {
+    local_path_sanity_check(fs_path);
     int dirfd;
     DIR *stream;
 
@@ -886,6 +929,7 @@ static int local_fstat(FsContext *fs_ctx, int fid_type,
 static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
                        int flags, FsCred *credp, V9fsFidOpenState *fs)
 {
+    local_path_sanity_check(dir_path);
     int fd = -1;
     int err = -1;
     int dirfd;
diff --git a/hw/9pfs/9p-local.h b/hw/9pfs/9p-local.h
index b61f894195..b523017099 100644
--- a/hw/9pfs/9p-local.h
+++ b/hw/9pfs/9p-local.h
@@ -17,6 +17,7 @@ typedef struct {
     int mountfd;
 } LocalData;
 
+void local_path_sanity_check(const V9fsPath *path);
 int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags,
                         mode_t mode);
 int local_opendir_nofollow(FsContext *fs_ctx, const char *path);
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 7/7] tests/9p-internals: test 9p's local_path_sanity_check() function
  2026-11-23 15:31 [PATCH 0/7] 9pfs: hardening against export root escape (CVE-2026-93834) Christian Schoenebeck
                   ` (5 preceding siblings ...)
  2026-11-23 15:25 ` [PATCH 6/7] hw/9pfs: local: abort on corrupt V9fsPath objects Christian Schoenebeck
@ 2026-11-23 15:25 ` Christian Schoenebeck
  6 siblings, 0 replies; 8+ messages in thread
From: Christian Schoenebeck @ 2026-11-23 15:25 UTC (permalink / raw)
  To: qemu-devel; +Cc: Greg Kurz

Test 9p local fs backend's internal local_path_sanity_check()
function (introduced by previous patch) for it to abort() the
process as expected on passed, corrupted V9fsPath objects.

Signed-off-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
---
 tests/unit/test-9p-internals.c | 73 ++++++++++++++++++++++++++++++++++
 1 file changed, 73 insertions(+)

diff --git a/tests/unit/test-9p-internals.c b/tests/unit/test-9p-internals.c
index 61c7fc7779..30c7c58afc 100644
--- a/tests/unit/test-9p-internals.c
+++ b/tests/unit/test-9p-internals.c
@@ -14,6 +14,7 @@
 #include "qemu/osdep.h"
 #include "qemu/module.h"
 #include "fsdev/file-op-9p.h"
+#include "hw/9pfs/9p.h"
 #include "hw/9pfs/9p-local.h"
 #include "forktest.h"
 #include <stdlib.h>
@@ -56,6 +57,29 @@ static const char *v9p_internals_corrupt_paths[] = {
     "",
 };
 
+/*
+ * Resembles a V9fsPath - @expect_abort reflects whether that state
+ * is expected to cause an abort
+ */
+typedef struct {
+    const char *data;
+    size_t size;
+    bool expect_abort;
+} V9pInternalsPathPair;
+
+static const V9pInternalsPathPair v9p_internals_path_pairs[] = {
+    { NULL, 0, true },
+    { NULL, 4, true },
+    /* short size: a copy would not be NUL-terminated */
+    { "abc", 2, true },
+    /* long size: captured copy would exceed the string */
+    { "abc", 99, true },
+    /* implausible size: larger than our hard-coded 4 MiB limit */
+    { "abc", 5 * 1024 * 1024, true },
+    /* valid size: this should not abort */
+    { "abc", 4, false },
+};
+
 /* shared test-suite state */
 typedef struct {
     LocalData localdata;
@@ -183,6 +207,53 @@ static void v9p_internals_local_open_nofollow_corrupt_paths(void *data)
     }
 }
 
+/*
+ * Called from within a child process, because the tested
+ * local_path_sanity_check() function aborts if it detected a
+ * corrupted V9fsPath. So we test whether the child process was
+ * aborted as expected (for corrupted paths).
+ */
+static void v9p_internals_check_v9fspath(void *opaque)
+{
+    const V9pInternalsPathPair *p = opaque;
+    V9fsPath pair;
+    V9fsPath dst;
+
+    pair = (V9fsPath) { .data = (char *)p->data, .size = p->size };
+
+    local_path_sanity_check(&pair);
+    if (p->expect_abort) {
+        forktest_exit(FORKTEST_EXIT_SURVIVED);
+    }
+
+    v9fs_path_init(&dst);
+    v9fs_path_copy(&dst, &pair);
+    g_assert_cmpuint(dst.size, ==, p->size);
+    g_assert_nonnull(dst.data);
+    g_assert_cmpint(dst.data[p->size - 1], ==, '\0');
+    v9fs_path_free(&dst);
+    forktest_exit(FORKTEST_EXIT_OK);
+}
+
+static void v9p_internals_local_path_sanity_check(void *data)
+{
+    size_t i;
+
+    for (i = 0; i < ARRAY_SIZE(v9p_internals_path_pairs); i++) {
+        const V9pInternalsPathPair *pair = &v9p_internals_path_pairs[i];
+        g_autofree char *name =
+            g_strdup_printf("path pair (data %s, size %zu)", pair->data,
+                            pair->size);
+
+        forktest_run((ForkTest) {
+            .child_fn = v9p_internals_check_v9fspath,
+            .opaque = (void *)pair,
+            .expect_abort = pair->expect_abort,
+            .name = name,
+        });
+    }
+}
+
 int main(int argc, char **argv)
 {
     g_test_init(&argc, &argv, NULL);
@@ -193,5 +264,7 @@ int main(int argc, char **argv)
     forktest_add("/9p-internals/local_open_nofollow/corrupt-paths",
                  v9p_internals_local_open_nofollow_corrupt_paths,
                  &v9p_internals_env);
+    forktest_add("/9p-internals/local_path_sanity_check",
+                 v9p_internals_local_path_sanity_check, NULL);
     return g_test_run();
 }
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 0/7] 9pfs: hardening against export root escape (CVE-2026-93834)
@ 2026-11-23 15:31 Christian Schoenebeck
  2026-11-23 15:24 ` [PATCH 1/7] hw/9pfs: local_open_nofollow(): abort on illegal FID path components Christian Schoenebeck
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: Christian Schoenebeck @ 2026-11-23 15:31 UTC (permalink / raw)
  To: qemu-devel
  Cc: Greg Kurz, Paolo Bonzini, Marc-André Lureau,
	Daniel P. Berrangé, Pierrick Bouvier

CVE-2026-93834 (fixed by fe9e30899ff) allowed guests to escape from
9p server's export root. Due to that severe impact, it is therefore
worth hardening the 9p server at relevant, affected code locations.

Core patches of this series are the following 2 patches:

 - Patch 1 hardens local_open_nofollow(), the most vulnerable place in 9p
   server for this, as this function does the actual element-wise path walk
   and openat() calls. Key change of this patch is to longer trust the passed
   path. The path is instead copied right at the beginning of the function to
   retain an immutable string so that nobody could pull the rug under our feet
   while we are then validating the path string upon that immutable path
   string, and the function would now abort QEMU process on any illegal path,
   not expected at this point, as it means server state is already corrupted.

 - Patch 6 adds a sanity check exactly at those locations in the 9p local
   backend, where a manipulated path would manifest into real filesystem
   action. This new sanity check verifies the V9fsPath object, and again aborts
   the QEMU process loudly in this case, for the same reason described above.
   
Furthermore this series adds a new 9p test suite tests/unit/test-9p-internals.c,
which checks expected behaviour of specific 9p server internal functions.

Christian Schoenebeck (7):
  hw/9pfs: local_open_nofollow(): abort on illegal FID path components
  hw/9pfs: move struct LocalData into 9p-local.h
  meson: move 9pfs-util selection to toplevel meson file
  tests/unit: add forktest.h, a forked-child run-and-verify helper
  tests/unit: add test-9p-internals.c
  hw/9pfs: local: abort on corrupt V9fsPath objects
  tests/9p-internals: test 9p's local_path_sanity_check() function

 MAINTAINERS                    |   2 +
 hw/9pfs/9p-local.c             | 117 +++++++++++++-
 hw/9pfs/9p-local.h             |   5 +
 hw/9pfs/meson.build            |   8 +-
 meson.build                    |  10 ++
 tests/unit/forktest.h          | 132 ++++++++++++++++
 tests/unit/meson.build         |  22 +++
 tests/unit/test-9p-internals.c | 270 +++++++++++++++++++++++++++++++++
 8 files changed, 554 insertions(+), 12 deletions(-)
 create mode 100644 tests/unit/forktest.h
 create mode 100644 tests/unit/test-9p-internals.c

-- 
2.47.3



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-11-23 15:48 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 15:31 [PATCH 0/7] 9pfs: hardening against export root escape (CVE-2026-93834) Christian Schoenebeck
2026-11-23 15:24 ` [PATCH 1/7] hw/9pfs: local_open_nofollow(): abort on illegal FID path components Christian Schoenebeck
2026-11-23 15:24 ` [PATCH 2/7] hw/9pfs: move struct LocalData into 9p-local.h Christian Schoenebeck
2026-11-23 15:24 ` [PATCH 3/7] meson: move 9pfs-util selection to toplevel meson file Christian Schoenebeck
2026-11-23 15:24 ` [PATCH 4/7] tests/unit: add forktest.h, a forked-child run-and-verify helper Christian Schoenebeck
2026-11-23 15:25 ` [PATCH 5/7] tests/unit: add test-9p-internals.c Christian Schoenebeck
2026-11-23 15:25 ` [PATCH 6/7] hw/9pfs: local: abort on corrupt V9fsPath objects Christian Schoenebeck
2026-11-23 15:25 ` [PATCH 7/7] tests/9p-internals: test 9p's local_path_sanity_check() function Christian Schoenebeck

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.