9pfs: hardening against export root escape (CVE-2026-93834)
cover 9pfs: hardening against export root escape (CVE-2026-93834)
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
Signature · 1 line
2.47.3
PATCH 1/7 hw/9pfs: local_open_nofollow(): abort on illegal FID path components
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.
--- hw/9pfs/9p-local.c | 71 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 3 deletions(-)
hw/9pfs/9p-local.c +68 -3
@@ -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);
Signature · 1 line
2.47.3
PATCH 2/7 hw/9pfs: move struct LocalData into 9p-local.h
In reply to Christian SchoenebeckLocalData 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.
--- hw/9pfs/9p-local.c | 4 ---- hw/9pfs/9p-local.h | 4 ++++ 2 files changed, 4 insertions(+), 4 deletions(-)
hw/9pfs/9p-local.c +0 -4
@@ -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) {
hw/9pfs/9p-local.h +4 -0
@@ -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);
Signature · 1 line
2.47.3
PATCH 6/7 hw/9pfs: local: abort on corrupt V9fsPath objects
In reply to Christian SchoenebeckHarden 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.
--- hw/9pfs/9p-local.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ hw/9pfs/9p-local.h | 1 + 2 files changed, 45 insertions(+)
hw/9pfs/9p-local.c +44 -0
@@ -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;
hw/9pfs/9p-local.h +1 -0
@@ -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);
Signature · 1 line
2.47.3
PATCH 4/7 tests/unit: add forktest.h, a forked-child run-and-verify helper
In reply to Christian SchoenebeckWe 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.
--- MAINTAINERS | 1 + tests/unit/forktest.h | 132 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 tests/unit/forktest.h
MAINTAINERS +1 -0
@@ -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
tests/unit/forktest.h new file +132 -0
132 lines not on this page · raw message
Signature · 1 line
2.47.3
PATCH 7/7 tests/9p-internals: test 9p's local_path_sanity_check() function
In reply to Christian SchoenebeckTest 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.
--- tests/unit/test-9p-internals.c | 73 ++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+)
tests/unit/test-9p-internals.c +73 -0
@@ -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 (); }
Signature · 1 line
2.47.3
PATCH 3/7 meson: move 9pfs-util selection to toplevel meson file
In reply to Christian Schoenebeck9p-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'.
--- hw/9pfs/meson.build | 8 ++------ meson.build | 10 ++++++++++ 2 files changed, 12 insertions(+), 6 deletions(-)
hw/9pfs/meson.build +2 -6
@@ -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)
meson.build +10 -0
@@ -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'
Signature · 1 line
2.47.3
PATCH 5/7 tests/unit: add test-9p-internals.c
In reply to Christian SchoenebeckAdd 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.
--- 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
MAINTAINERS +1 -0
@@ -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
tests/unit/meson.build +22 -0
@@ -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],
tests/unit/test-9p-internals.c new file +197 -0
197 lines not on this page · raw message
Signature · 1 line
2.47.3