All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] 9pfs/local: allow exporting traversable but non-readable directories
@ 2026-11-09 15:55 Niklas Gollenstede
  2026-11-09 15:55 ` [PATCH 1/2] " Niklas Gollenstede
  2026-11-09 15:55 ` [PATCH 2/2] tests/qtest/virtio-9p: test non-readable but traversable directories Niklas Gollenstede
  0 siblings, 2 replies; 4+ messages in thread
From: Niklas Gollenstede @ 2026-11-09 15:55 UTC (permalink / raw)
  To: qemu-devel; +Cc: Christian Schoenebeck, Greg Kurz, Niklas Gollenstede

I often use (NixOS) qemu VMs with the hosts `/nix/store` mounted as `9p` share from the host.
This generally works fine, but stops working when the read permission on the `/nix/store` dir itself is not granted to the user running qemu.
Almost no application should have a reason to read the `/nix/store` directory itself, and since both a direct and especially a recursive listing can be huge, some programs misbehave if they can read it.
Nix itself recently gained support for having `/nix/store` with mode `1771`.

The solution seems pretty straightforward: open a few root-directory file descriptors with `O_PATH` instead of `O_RDONLY`, as they are only used to `*at` child FDs. Reads, if attempted, should then fail in the client/guest.

This is a problem for real users, and I knew/suspected that changing a few open flags to `O_PATH` would probably fix it (and I did a quick attempt at this myself).
But since it is much faster and honestly also more reliable ...
AI disclosure:
I had Claude analyse the problem, find the necessary sites to change, and write a test that shows the problem and the fix.
The (100% AI generated) test is included in a separate commit, so that it can hopefully be accepted or rejected independently of the actual fix.
This and the first commit's message I wrote myself.

Claude (1):
  tests/qtest/virtio-9p: test non-readable but traversable directories

Niklas Gollenstede (1):
  9pfs/local: allow exporting traversable but non-readable directories

 hw/9pfs/9p-local.c                       |   9 +-
 tests/qtest/libqos/virtio-9p.c           |   4 +-
 tests/qtest/virtio-9p-test.c             | 134 +++++++++++++++++++++++
 5 files changed, 142 insertions(+), 5 deletions(-)

--
2.54.0



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

* [PATCH 1/2] 9pfs/local: allow exporting traversable but non-readable directories
  2026-11-09 15:55 [PATCH 0/2] 9pfs/local: allow exporting traversable but non-readable directories Niklas Gollenstede
@ 2026-11-09 15:55 ` Niklas Gollenstede
  2026-11-09 15:55 ` [PATCH 2/2] tests/qtest/virtio-9p: test non-readable but traversable directories Niklas Gollenstede
  1 sibling, 0 replies; 4+ messages in thread
From: Niklas Gollenstede @ 2026-11-09 15:55 UTC (permalink / raw)
  To: qemu-devel; +Cc: Christian Schoenebeck, Greg Kurz, Niklas Gollenstede

Exporting directories via 9p that are not readable (`r`) by the current user failed for no reason other than requesting read permissions when opening directories.
Since the resulting directory file descriptors are only to operate on child files, passing `O_PATH` (or rather `O_PATH_9P_UTIL`) to the open calls fixes this.

AI-used-for: root-cause analysis (Claude Opus 5.5)
Signed-off-by: Niklas Gollenstede <gollenstede@ibr.cs.tu-bs.de>
---
 hw/9pfs/9p-local.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/hw/9pfs/9p-local.c b/hw/9pfs/9p-local.c
index ee592b62f8..3bebebea04 100644
--- a/hw/9pfs/9p-local.c
+++ b/hw/9pfs/9p-local.c
@@ -97,7 +97,8 @@ int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags,

 int local_opendir_nofollow(FsContext *fs_ctx, const char *path)
 {
-    return local_open_nofollow(fs_ctx, path, O_DIRECTORY | O_RDONLY, 0);
+    return local_open_nofollow(fs_ctx, path,
+                               O_DIRECTORY | O_RDONLY | O_PATH_9P_UTIL, 0);
 }

 static void renameat_preserve_errno(int odirfd, const char *opath, int ndirfd,
@@ -526,7 +527,7 @@ static int local_opendir(FsContext *ctx,
     int dirfd;
     DIR *stream;

-    dirfd = local_opendir_nofollow(ctx, fs_path->data);
+    dirfd = local_open_nofollow(ctx, fs_path->data, O_DIRECTORY | O_RDONLY, 0);
     if (dirfd == -1) {
         return -1;
     }
@@ -1207,7 +1208,7 @@ static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
 {
     int fd, ret;

-    fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
+    fd = local_open_nofollow(s, fs_path->data, O_RDONLY | O_PATH_9P_UTIL, 0);
     if (fd == -1) {
         return -1;
     }
@@ -1471,7 +1472,7 @@ static int local_init(FsContext *ctx, Error **errp)
 {
     LocalData *data = g_malloc(sizeof(*data));

-    data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
+    data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY | O_PATH_9P_UTIL);
     if (data->mountfd == -1) {
         error_setg_file_open(errp, errno, ctx->fs_root);
         goto err;
--
2.54.0



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

* [PATCH 2/2] tests/qtest/virtio-9p: test non-readable but traversable directories
  2026-11-09 15:55 [PATCH 0/2] 9pfs/local: allow exporting traversable but non-readable directories Niklas Gollenstede
  2026-11-09 15:55 ` [PATCH 1/2] " Niklas Gollenstede
@ 2026-11-09 15:55 ` Niklas Gollenstede
  2026-11-23 16:18   ` Christian Schoenebeck
  1 sibling, 1 reply; 4+ messages in thread
From: Niklas Gollenstede @ 2026-11-09 15:55 UTC (permalink / raw)
  To: qemu-devel; +Cc: Christian Schoenebeck, Greg Kurz, Niklas Gollenstede

From: Claude <noreply@anthropic.com>

Add two 'local' driver tests for directories that the user running QEMU
may traverse (x) but not read (r), e.g. a host's /nix/store with mode
1771 that is shared with a VM:

- local/walk/unreadable_dir: such a directory inside the export
- local/attach/unreadable_root: the export root itself is such a directory

In both cases, walking to, stat'ing and reading a known file inside the
directory must work, while listing the directory must fail with EACCES.
The tests are skipped if directory permissions are not enforced (e.g.
when running as root).

Without the previous commit, both tests fail: the walk through the
non-readable directory stops at it, and with a non-readable export root
QEMU does not even start.

Also make the test dir cleanup restore permissions before 'rm -fr', so
that it can remove such directories.

AI-used-for: code (Claude Opus 5.5)
Signed-off-by: Niklas Gollenstede <gollenstede@ibr.cs.tu-bs.de>
---
 tests/qtest/libqos/virtio-9p.c |   4 +-
 tests/qtest/virtio-9p-test.c   | 134 +++++++++++++++++++++++++++++++++
 2 files changed, 137 insertions(+), 1 deletion(-)

diff --git a/tests/qtest/libqos/virtio-9p.c b/tests/qtest/libqos/virtio-9p.c
index 823756de8c..ddb5c5030b 100644
--- a/tests/qtest/libqos/virtio-9p.c
+++ b/tests/qtest/libqos/virtio-9p.c
@@ -64,7 +64,9 @@ void virtio_9p_create_local_test_dir(void)
 void virtio_9p_remove_local_test_dir(void)
 {
     g_assert(local_test_path != NULL);
-    g_autofree char *cmd = g_strdup_printf("rm -fr '%s'\n", local_test_path);
+    /* tests may have removed permissions that 'rm' needs */
+    g_autofree char *cmd = g_strdup_printf("chmod -R u+rwx '%s'; rm -fr '%s'\n",
+                                           local_test_path, local_test_path);
     int res = system(cmd);
     if (res < 0) {
         /* ignore error, dummy check to prevent compiler error */
diff --git a/tests/qtest/virtio-9p-test.c b/tests/qtest/virtio-9p-test.c
index 22302bd2f8..390b3cc0a3 100644
--- a/tests/qtest/virtio-9p-test.c
+++ b/tests/qtest/virtio-9p-test.c
@@ -1009,6 +1009,123 @@ static void fs_deep_absolute_path(void *obj, void *data,
     g_string_free(path, TRUE);
 }

+/*
+ * Tests for directories that are traversable (x) but not readable (r) by the
+ * user running QEMU, like a Nix store directory with mode 0751 or 1771 shared
+ * with a VM by a user that is not in its group: listing such a directory
+ * must fail, but all entries with known names must still be accessible.
+ */
+#define UNREADABLE_DIR_MODE 0111
+#define UNREADABLE_DIR_FILE_CONTENT "known file content\n"
+
+/* creates "<dir>/known/file", then removes read permission from <dir> */
+static void create_unreadable_dir(const char *dir)
+{
+    g_autofree char *known = g_build_filename(dir, "known", NULL);
+    g_autofree char *file = g_build_filename(known, "file", NULL);
+
+    g_assert(g_mkdir_with_parents(known, 0755) == 0);
+    g_assert(g_file_set_contents(file, UNREADABLE_DIR_FILE_CONTENT, -1, NULL));
+    g_assert(chmod(dir, UNREADABLE_DIR_MODE) == 0);
+}
+
+/* true if the permission setup is ineffective, e.g. because we are root */
+static bool unreadable_dir_is_readable(const char *dir)
+{
+    DIR *d = opendir(dir);
+
+    if (d) {
+        closedir(d);
+        return true;
+    }
+    g_assert_cmpint(errno, ==, EACCES);
+    return false;
+}
+
+/*
+ * Walk to and read "known/file" in the unreadable directory @dir_path (NULL
+ * for the export root), and check that the unreadable directory can't be
+ * listed.
+ */
+static void do_unreadable_dir_checks(QVirtio9P *v9p, const char *dir_path)
+{
+    const uint32_t len = strlen(UNREADABLE_DIR_FILE_CONTENT);
+    g_autofree char *buf = g_malloc0(len + 1);
+    g_autofree char *file_path =
+        g_build_filename(dir_path ? dir_path : "", "known/file", NULL);
+    struct v9fs_attr attr;
+    uint32_t fid_dir, fid_file, count;
+    uint16_t nwqid;
+
+    /* the unreadable dir itself can be walked to and stat'ed ... */
+    fid_dir = twalk({ .client = v9p, .fid = 0, .path = dir_path }).newfid;
+    tgetattr({
+        .client = v9p, .fid = fid_dir, .request_mask = P9_GETATTR_BASIC,
+        .rgetattr.attr = &attr
+    });
+    g_assert_cmpint(attr.mode & 07777, ==, UNREADABLE_DIR_MODE);
+    /* ... but not opened for listing */
+    tlopen({
+        .client = v9p, .fid = fid_dir, .flags = O_RDONLY | O_DIRECTORY,
+        .expectErr = EACCES
+    });
+
+    /* entries with known names in it are fully accessible */
+    /* (a partial walk is not an error on protocol level, so check nwqid) */
+    fid_file = twalk({
+        .client = v9p, .fid = 0, .path = file_path,
+        .rwalk = { .nwqid = &nwqid }
+    }).newfid;
+    g_assert_cmpint(nwqid, ==, dir_path ? 3 : 2);
+    tgetattr({
+        .client = v9p, .fid = fid_file, .request_mask = P9_GETATTR_BASIC,
+        .rgetattr.attr = &attr
+    });
+    g_assert_cmpint(attr.size, ==, len);
+    tlopen({ .client = v9p, .fid = fid_file, .flags = O_RDONLY });
+    count = tread({
+        .client = v9p, .fid = fid_file, .offset = 0, .count = len,
+        .rread = { .count = &count, .data = buf }
+    }).count;
+    g_assert_cmpint(count, ==, len);
+    g_assert_cmpstr(buf, ==, UNREADABLE_DIR_FILE_CONTENT);
+}
+
+/* non-readable but traversable directory within the export root */
+static void fs_walk_unreadable_dir(void *obj, void *data,
+                                   QGuestAllocator *t_alloc)
+{
+    QVirtio9P *v9p = obj;
+    v9fs_set_allocator(t_alloc);
+    g_autofree char *dir = virtio_9p_test_path("unreadable_dir");
+
+    create_unreadable_dir(dir);
+    if (unreadable_dir_is_readable(dir)) {
+        g_test_skip("Directory permissions are not enforced (root?)");
+        return;
+    }
+
+    tattach({ .client = v9p });
+    do_unreadable_dir_checks(v9p, "unreadable_dir");
+}
+
+/* the export root itself is non-readable but traversable */
+static void fs_attach_unreadable_root(void *obj, void *data,
+                                      QGuestAllocator *t_alloc)
+{
+    QVirtio9P *v9p = obj;
+    v9fs_set_allocator(t_alloc);
+    g_autofree char *root = virtio_9p_test_path("");
+
+    if (unreadable_dir_is_readable(root)) {
+        g_test_skip("Directory permissions are not enforced (root?)");
+        return;
+    }
+
+    tattach({ .client = v9p });
+    do_unreadable_dir_checks(v9p, NULL);
+}
+
 static void fs_local_xattr_limit_default(void *obj, void *data,
                                          QGuestAllocator *t_alloc)
 {
@@ -1070,6 +1187,18 @@ static void *assign_9p_local_driver(GString *cmd_line, void *arg)
     return arg;
 }

+static void *assign_9p_local_driver_unreadable_root(GString *cmd_line,
+                                                   void *arg)
+{
+    g_autofree char *root = NULL;
+
+    assign_9p_local_driver_with_args(cmd_line, NULL);
+    /* must be set up before QEMU opens the export root */
+    root = virtio_9p_test_path("");
+    create_unreadable_dir(root);
+    return arg;
+}
+
 static void *local_max_xattr_custom_opt(GString *cmd_line, void *arg)
 {
     assign_9p_local_driver_with_args(cmd_line, "max_xattr=100");
@@ -1140,6 +1269,8 @@ static void register_virtio_9p_test(void)
                  &opts);
     qos_add_test("local/deep_absolute_path", "virtio-9p",
                  fs_deep_absolute_path, &opts);
+    qos_add_test("local/walk/unreadable_dir", "virtio-9p",
+                 fs_walk_unreadable_dir, &opts);
     qos_add_test("local/xattr_limit/default", "virtio-9p",
                  fs_local_xattr_limit_default, &opts);
     opts.before = local_max_xattr_custom_opt;
@@ -1148,6 +1279,9 @@ static void register_virtio_9p_test(void)
     opts.before = local_max_xattr_unlimited_opt;
     qos_add_test("local/xattr_limit/unlimited", "virtio-9p",
                  fs_local_xattr_limit_unlimited, &opts);
+    opts.before = assign_9p_local_driver_unreadable_root;
+    qos_add_test("local/attach/unreadable_root", "virtio-9p",
+                 fs_attach_unreadable_root, &opts);
 }

 libqos_init(register_virtio_9p_test);
--
2.54.0



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

* Re: [PATCH 2/2] tests/qtest/virtio-9p: test non-readable but traversable directories
  2026-11-09 15:55 ` [PATCH 2/2] tests/qtest/virtio-9p: test non-readable but traversable directories Niklas Gollenstede
@ 2026-11-23 16:18   ` Christian Schoenebeck
  0 siblings, 0 replies; 4+ messages in thread
From: Christian Schoenebeck @ 2026-11-23 16:18 UTC (permalink / raw)
  To: qemu-devel; +Cc: Greg Kurz, Niklas Gollenstede

On Monday, 9 November 2026 16:55:03 CET Niklas Gollenstede wrote:
> From: Claude <noreply@anthropic.com>
> 
> Add two 'local' driver tests for directories that the user running QEMU
> may traverse (x) but not read (r), e.g. a host's /nix/store with mode
> 1771 that is shared with a VM:
> 
> - local/walk/unreadable_dir: such a directory inside the export
> - local/attach/unreadable_root: the export root itself is such a directory
> 
> In both cases, walking to, stat'ing and reading a known file inside the
> directory must work, while listing the directory must fail with EACCES.
> The tests are skipped if directory permissions are not enforced (e.g.
> when running as root).
> 
> Without the previous commit, both tests fail: the walk through the
> non-readable directory stops at it, and with a non-readable export root
> QEMU does not even start.
> 
> Also make the test dir cleanup restore permissions before 'rm -fr', so
> that it can remove such directories.
> 
> AI-used-for: code (Claude Opus 5.5)
> Signed-off-by: Niklas Gollenstede <gollenstede@ibr.cs.tu-bs.de>

I still had no time to look at this series more closely, but upfront: I can 
definitely not pick a patch with an AI identity as the author. So for the bare 
minimum, you must drop the From: line and report this series as v2 (toplevel 
post).

https://www.qemu.org/docs/master/devel/submitting-a-patch.html

Furthermore I am not sure what the current policy of QEMU regarding AI 
generated code now is. Originally they denied any AI generated content, but 
realistically that's now everywhere.

As for the issue itself, déja vu:
https://gitlab.com/qemu-project/qemu/-/work_items/1066

/Christian




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

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

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-09 15:55 [PATCH 0/2] 9pfs/local: allow exporting traversable but non-readable directories Niklas Gollenstede
2026-11-09 15:55 ` [PATCH 1/2] " Niklas Gollenstede
2026-11-09 15:55 ` [PATCH 2/2] tests/qtest/virtio-9p: test non-readable but traversable directories Niklas Gollenstede
2026-11-23 16:18   ` 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.