Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
fix(daemon): name the WSL DrvFs remedy when a /mnt/<drive> cache is refused (#1687)
Martin Vogel19f50de3d2e2
diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.mdindex 038256976..024f37e93 100644--- a/docs/CONFIGURATION.md+++ b/docs/CONFIGURATION.md@@ -200,6 +200,20 @@ process that should share one daemon must see the same value — set it in theenvironment of your MCP client and your shell alike, or a CLI invocation withoutit will coordinate through the default location instead.+**WSL2 and Windows drives (`/mnt/c`, `/mnt/e`, ...).** `CBM_CACHE_DIR` goes through+the same private-directory check. With WSL's default automount options, DrvFs+reports every directory as `0777` and ignores `chmod`, so a cache under+`/mnt/<drive>` is refused, and the refusal names this remedy. Either turn on+permission metadata in `/etc/wsl.conf`, then run `wsl --shutdown` and reopen WSL:++```ini+[automount]+options = "metadata,umask=22,fmask=11"+```++or keep the cache on the Linux filesystem (the default `~/.cache/codebase-memory-mcp`),+which is also much faster than a 9p-mounted Windows drive.+Environment used by daemon-owned components—such as diagnostics, daemon logging, and process-wide indexing resource limits—is captured from the first daemon-backed session that starts the daemon. Later sessions join the existing process and cannot replace those values. To change them, close every daemon-backed session, update the relevant agent configurations consistently, and restart a session. `CBM_ALLOWED_ROOT` remains session-specific, a conflicting `CBM_CACHE_DIR` is rejected, and one-shot CLI commands use their own current environment without starting the daemon.diff --git a/src/daemon/ipc.c b/src/daemon/ipc.cindex 3917a7a05..eb87e43be 100644--- a/src/daemon/ipc.c+++ b/src/daemon/ipc.c@@ -408,6 +408,9 @@ int cbm_daemon_ipc_wait_pending(const cbm_ipc_pending_ops_t *ops, uint32_t timeo#include <sys/un.h>#include <time.h>#include <unistd.h>+#if defined(__linux__)+#include <sys/vfs.h>+#endif#ifndef O_CLOEXEC#define O_CLOEXEC 0@@ -1680,6 +1683,55 @@ static bool posix_ancestor_stat_ok(uid_t owner, mode_t mode, uid_t euid, uid_t oreturn true;}+/* #1687: WSL2 mounts Windows drives (/mnt/c, /mnt/e, ...) as DrvFs over 9p.+ * Without the `metadata` mount option such a mount reports 0777 for every+ * directory and ignores chmod, so both the ancestor rule (not world-writable)+ * and the leaf 0700 rule refuse it -- correctly: the 0777 is real. The gate+ * stays exactly as it is; this only lets the refusal name the remedy instead+ * of leaving a WSL user to guess. Detection is a statfs magic (9p) confirmed by+ * a WSL kernel release string, so plain 9p mounts elsewhere (QEMU virtfs) keep+ * the generic message. Diagnostic only: it never changes accept/refuse. */+#define POSIX_WSL_DRVFS_REMEDY \+ "WSL DrvFs mount: set /etc/wsl.conf [automount] options = " \+ "\"metadata,umask=22,fmask=11\", then run `wsl --shutdown`; or keep the cache on the Linux " \+ "filesystem (e.g. ~/.cache)"++#ifdef CBM_ENABLE_TEST_SEAMS+static bool g_posix_force_wsl_drvfs;+void cbm_daemon_ipc_posix_force_wsl_drvfs_for_test(bool force) {+ g_posix_force_wsl_drvfs = force;+}+#endif++#if defined(__linux__)+#define POSIX_V9FS_MAGIC 0x01021997UL++static bool posix_kernel_is_wsl(void) {+ char release[128];+ if (!posix_read_small_proc_file("/proc/sys/kernel/osrelease", release, sizeof(release))) {+ return false;+ }+ return strstr(release, "microsoft") != NULL || strstr(release, "Microsoft") != NULL ||+ strstr(release, "WSL") != NULL;+}+#endif++static bool posix_fd_on_wsl_drvfs(int directory_fd) {+#ifdef CBM_ENABLE_TEST_SEAMS+ if (g_posix_force_wsl_drvfs) {+ return true;+ }+#endif+#if defined(__linux__)+ struct statfs filesystem;+ return directory_fd >= 0 && fstatfs(directory_fd, &filesystem) == 0 &&+ (unsigned long)filesystem.f_type == POSIX_V9FS_MAGIC && posix_kernel_is_wsl();+#else+ (void)directory_fd;+ return false;+#endif+}+static bool posix_directory_ancestor_owner_trusted(uid_t owner) {return posix_ancestor_owner_ok(owner, geteuid(), posix_ancestor_overflow_uid());}@@ -1796,11 +1848,18 @@ static int private_directory_tree_open(const char *directory_path) {* refusing, found it clean, and said so — correctly. Naming the* containing directory is the difference between a report we* can act on and weeks of talking past each other. */- ipc_validation_detail_set(- "%s: the directory CONTAINING '%s' is not a usable private-directory parent "- "(it must be owned by you, not world-writable, and carry no allow-ACL). Check "- "that containing directory, not '%s' itself",- directory_path, component, component);+ if (posix_fd_on_wsl_drvfs(current_fd)) {+ /* #1687: shorter rule text so the remedy fits the buffer. */+ ipc_validation_detail_set("%s: the directory CONTAINING '%s' is not private "+ "(DrvFs reports 0777). " POSIX_WSL_DRVFS_REMEDY,+ directory_path, component);+ } else {+ ipc_validation_detail_set(+ "%s: the directory CONTAINING '%s' is not a usable private-directory "+ "parent (it must be owned by you, not world-writable, and carry no "+ "allow-ACL). Check that containing directory, not '%s' itself",+ directory_path, component, component);+ }}bool created = ok && mkdirat(current_fd, component, 0700) == 0;if (!created && errno != EEXIST) {@@ -1854,8 +1913,11 @@ static int private_directory_tree_open(const char *directory_path) {ok = false;}if (ok && (fstat(current_fd, &final_status) != 0 || (final_status.st_mode & 07777) != 0700)) {- ipc_validation_detail_set("%s: mode 0%o survived chmod, expected 0700", directory_path,- (unsigned)(final_status.st_mode & 07777));+ /* #1687: DrvFs without `metadata` ignores chmod; name the remedy. */+ bool drvfs = posix_fd_on_wsl_drvfs(current_fd);+ ipc_validation_detail_set("%s: mode 0%o survived chmod, expected 0700%s%s", directory_path,+ (unsigned)(final_status.st_mode & 07777), drvfs ? ". " : "",+ drvfs ? POSIX_WSL_DRVFS_REMEDY : "");ok = false;}if (ok && !cbm_macos_extended_acl_fd_is_empty(current_fd)) {diff --git a/src/daemon/ipc.h b/src/daemon/ipc.hindex 545ecb357..effe6a852 100644--- a/src/daemon/ipc.h+++ b/src/daemon/ipc.h@@ -131,6 +131,9 @@ bool cbm_daemon_ipc_posix_uid_map_is_single_uid_for_test(const char *uid_map, unbool cbm_daemon_ipc_posix_ancestor_stat_ok_for_test(unsigned long owner, unsigned int mode,unsigned long euid, bool overflow_active,unsigned long overflow_uid);+/* #1687: treat every directory as on a WSL DrvFs mount so the remedy text in+ * the private-directory refusal is testable off WSL. Diagnostic only. */+void cbm_daemon_ipc_posix_force_wsl_drvfs_for_test(bool force);#if defined(__linux__)/* Number of REAL overflow-uid derivations so far. The value is meaningless on* its own; the point is that it must rise on every ancestor check, proving nodiff --git a/tests/test_daemon_ipc.c b/tests/test_daemon_ipc.cindex 815f630a6..e4d54e5f6 100644--- a/tests/test_daemon_ipc.c+++ b/tests/test_daemon_ipc.c@@ -5193,6 +5193,59 @@ TEST(daemon_ipc_posix_world_writable_ancestor_still_refused_issue1537) {}#ifdef CBM_ENABLE_TEST_SEAMS+/* #1687: on a WSL DrvFs mount (/mnt/e, default 0777) the ancestor refusal is+ * correct but used to leave the user guessing. The gate must still refuse, and+ * the detail must now name the containing directory AND the WSL remedy. */+TEST(daemon_ipc_posix_wsl_drvfs_refusal_names_remedy_issue1687) {+ char parent[TEST_PATH_CAP];+ char ancestor[TEST_PATH_CAP];+ char cache[TEST_PATH_CAP];+ char detail[512] = {0};+ char plain_detail[512] = {0};+ bool paths_ok = false;+ bool ancestor_ready = false;+ bool refused = false;+ bool plain_refused = false;++ if (ipc_test_parent_new(parent, "posix-wsl-drvfs")) {+ int a = snprintf(ancestor, sizeof(ancestor), "%s/e", parent);+ int c = snprintf(cache, sizeof(cache), "%s/codebase_memory_cache", ancestor);+ paths_ok = a > 0 && a < (int)sizeof(ancestor) && c > 0 && c < (int)sizeof(cache);+ }+ if (paths_ok) {+ ancestor_ready = mkdir(ancestor, 0777) == 0 && chmod(ancestor, 0777) == 0;+ }+ if (ancestor_ready) {+ cbm_daemon_ipc_posix_force_wsl_drvfs_for_test(true);+ refused = !cbm_daemon_ipc_private_directory_secure(cache);+ cbm_daemon_ipc_posix_force_wsl_drvfs_for_test(false);+ (void)snprintf(detail, sizeof(detail), "%s", cbm_daemon_ipc_validation_detail());+ plain_refused = !cbm_daemon_ipc_private_directory_secure(cache);+ (void)snprintf(plain_detail, sizeof(plain_detail), "%s",+ cbm_daemon_ipc_validation_detail());+ }++ (void)rmdir(cache);+ (void)rmdir(ancestor);+ ipc_test_remove_flat_dir(parent);++ ASSERT_TRUE(paths_ok);+ ASSERT_TRUE(ancestor_ready);+ ASSERT_TRUE(refused); /* policy unchanged: still refused */+ ASSERT_NOT_NULL(strstr(detail, "CONTAINING 'codebase_memory_cache'"));+ ASSERT_NOT_NULL(strstr(detail, "/etc/wsl.conf"));+ ASSERT_NOT_NULL(strstr(detail, "metadata,umask=22,fmask=11"));+ ASSERT_NOT_NULL(strstr(detail, "wsl --shutdown"));+ ASSERT_NOT_NULL(strstr(detail, "~/.cache"));+ /* The remedy must fit whole, not be clipped by the detail buffer. */+ ASSERT_NOT_NULL(strstr(detail, "(e.g. ~/.cache)"));+ /* Off DrvFs the generic message stays and carries no WSL advice. */+ ASSERT_TRUE(plain_refused);+ ASSERT_NOT_NULL(strstr(plain_detail, "CONTAINING 'codebase_memory_cache'"));+ ASSERT_NULL(strstr(plain_detail, "wsl.conf"));+ PASS();+}+/* #1830: /proc/self/uid_map single-uid detection. A single-uid map is exactly* one line "<inside> <outside> 1" whose inside id is our euid; anything else —* the init map, a count other than 1, a foreign inside id, extra lines, or junk@@ -5517,6 +5570,7 @@ SUITE(daemon_ipc) {RUN_TEST(daemon_ipc_posix_group_writable_ancestor_is_admitted_issue1537);RUN_TEST(daemon_ipc_posix_world_writable_ancestor_still_refused_issue1537);#ifdef CBM_ENABLE_TEST_SEAMS+ RUN_TEST(daemon_ipc_posix_wsl_drvfs_refusal_names_remedy_issue1687);RUN_TEST(daemon_ipc_posix_uid_map_single_uid_parse_issue1830);RUN_TEST(daemon_ipc_posix_overflow_ancestor_tolerated_only_in_single_uid_ns_issue1830);RUN_TEST(daemon_ipc_posix_overflow_uid_is_never_cached_issue1830);