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: eliminate installer filesystem races
Martin Vogelaa2366811b1b
diff --git a/src/cli/cli.c b/src/cli/cli.cindex 9ac94afe4..aec3cf102 100644--- a/src/cli/cli.c+++ b/src/cli/cli.c@@ -3632,41 +3632,8 @@ static int cbm_powershell_quote_word(const char *value, char *out, size_t out_sistatic bool cbm_write_owned_hook_script_with_legacy(const char *path, const char *script,const char *const *legacy_scripts,size_t legacy_count) {- int write_result = cbm_text_ensure_owned_document(path, script);- for (size_t index = 0U; write_result != 0 && index < legacy_count; index++) {- const char *legacy = legacy_scripts ? legacy_scripts[index] : NULL;- if (legacy) {- write_result =- cbm_text_write_owned_document_if_unchanged(path, script, legacy, strlen(legacy));- }- }- if (write_result != 0) {- return false;- }-#ifndef _WIN32- struct stat expected;- if (lstat(path, &expected) != 0 || !S_ISREG(expected.st_mode) || expected.st_nlink != 1U) {- return false;- }- int flags = O_RDONLY;-#ifdef O_NOFOLLOW- flags |= O_NOFOLLOW;-#endif-#ifdef O_CLOEXEC- flags |= O_CLOEXEC;-#endif- int descriptor = open(path, flags);- struct stat state;- bool ok = descriptor >= 0 && fstat(descriptor, &state) == 0 && S_ISREG(state.st_mode) &&- state.st_nlink == 1U && state.st_dev == expected.st_dev &&- state.st_ino == expected.st_ino && fchmod(descriptor, CLI_OCTAL_PERM) == 0;- if (descriptor >= 0) {- (void)close(descriptor);- }- return ok;-#else- return chmod(path, CLI_OCTAL_PERM) == 0;-#endif+ return cbm_text_migrate_owned_document_mode(path, script, legacy_scripts, legacy_count,+ CLI_OCTAL_PERM) == CLI_OK;}static bool cbm_write_owned_hook_script(const char *path, const char *script) {diff --git a/src/cli/config_text_edit.c b/src/cli/config_text_edit.cindex 0f8b2d091..27fa3fd1c 100644--- a/src/cli/config_text_edit.c+++ b/src/cli/config_text_edit.c@@ -43,12 +43,15 @@#define TEXT_MAX_MARKER_BYTES 4096U#define TEXT_TEMP_SUFFIX_BYTES 80U#define TEXT_TEMP_ATTEMPTS 64U+#define TEXT_OWNER_READ 0400U#ifdef CBM_TEXT_EDIT_ENABLE_TEST_APIstatic CBM_TLS cbm_text_precommit_test_hook_t text_precommit_test_hook = NULL;static CBM_TLS void *text_precommit_test_context = NULL;static CBM_TLS cbm_text_precommit_test_hook_t text_prepublish_test_hook = NULL;static CBM_TLS void *text_prepublish_test_context = NULL;+static CBM_TLS cbm_text_precommit_test_hook_t text_temp_closed_test_hook = NULL;+static CBM_TLS void *text_temp_closed_test_context = NULL;#endifstatic atomic_uint text_temp_sequence = 1U;@@ -287,6 +290,20 @@ static int text_snapshot_equal(const text_file_snapshot_t *left,#endif}+static int text_snapshot_publication_equal(const text_file_snapshot_t *trusted,+ const text_file_snapshot_t *reopened) {+#ifdef _WIN32+ return trusted->exists == reopened->exists && trusted->exists &&+ trusted->volume_serial == reopened->volume_serial &&+ trusted->file_index_high == reopened->file_index_high &&+ trusted->file_index_low == reopened->file_index_low &&+ trusted->attributes == reopened->attributes &&+ trusted->link_count == reopened->link_count && trusted->size == reopened->size;+#else+ return text_snapshot_equal(trusted, reopened);+#endif+}+#ifdef _WIN32static int text_snapshot_from_handle(HANDLE handle, text_file_snapshot_t *snapshot) {BY_HANDLE_FILE_INFORMATION info;@@ -344,6 +361,27 @@ static int text_snapshot_from_stat(const struct stat *state, text_file_snapshot_}#endif+static int text_snapshot_from_file(FILE *file, text_file_snapshot_t *snapshot) {+ if (!file || !snapshot) {+ return TEXT_ERROR;+ }+#ifdef _WIN32+ intptr_t native_handle = _get_osfhandle(cbm_fileno(file));+ return native_handle == -1+ ? TEXT_ERROR+ : text_snapshot_from_handle((HANDLE)(uintptr_t)native_handle, snapshot);+#else+ struct stat state;+ return fstat(cbm_fileno(file), &state) == 0 ? text_snapshot_from_stat(&state, snapshot)+ : TEXT_ERROR;+#endif+}++static int text_requested_mode_valid(int override_mode, unsigned int requested_mode) {+ return !override_mode ||+ ((requested_mode & ~0777U) == 0U && (requested_mode & TEXT_OWNER_READ) != 0U);+}+static int text_read_file(const char *path, char **data_out, size_t *len_out,text_file_snapshot_t *snapshot_out) {if (!path || !data_out || !len_out || !snapshot_out) {@@ -551,13 +589,23 @@ static int text_replace_file(const char *temp_path, const char *path, int destin#endif}-static int text_write_atomic(const char *path, const char *new_data, size_t new_len,- const char *old_data, size_t old_len,- const text_file_snapshot_t *snapshot) {- if (new_len > TEXT_MAX_BYTES || old_len > TEXT_MAX_BYTES) {+static int text_write_atomic_mode(const char *path, const char *new_data, size_t new_len,+ const char *old_data, size_t old_len,+ const text_file_snapshot_t *snapshot, int override_mode,+ unsigned int requested_mode) {+ if (new_len > TEXT_MAX_BYTES || old_len > TEXT_MAX_BYTES ||+ !text_requested_mode_valid(override_mode, requested_mode)) {return TEXT_ERROR;}- if (new_len == old_len && (new_len == 0U || memcmp(new_data, old_data, new_len) == 0)) {+ int content_same =+ new_len == old_len && (new_len == 0U || memcmp(new_data, old_data, new_len) == 0);+#ifndef _WIN32+ int mode_same =+ snapshot->exists && (snapshot->mode & 0777U) == (mode_t)(requested_mode & 0777U);+#else+ int mode_same = snapshot->exists;+#endif+ if (content_same && (!override_mode || mode_same)) {return TEXT_OK;}size_t path_len = 0U;@@ -583,7 +631,9 @@ static int text_write_atomic(const char *path, const char *new_data, size_t new_}errno = 0;#ifdef _WIN32- file = cbm_fopen(temp_path, "wbx");+ /* The descriptor-bound identity snapshot uses GetFileInformationByHandle,+ * so request read access as well as exclusive binary creation. */+ file = cbm_fopen(temp_path, "w+bx");#else#ifndef O_NOFOLLOWfree(temp_path);@@ -627,7 +677,9 @@ static int text_write_atomic(const char *path, const char *new_data, size_t new_fchown(cbm_fileno(file), snapshot->owner, snapshot->group) != 0) {failed = 1;}- mode_t mode = snapshot->exists ? snapshot->mode & 0777U : 0600U;+ mode_t mode = override_mode ? (mode_t)(requested_mode & 0777U)+ : snapshot->exists ? snapshot->mode & 0777U+ : 0600U;if (!failed && fchmod(cbm_fileno(file), mode) != 0) {failed = 1;}@@ -635,6 +687,17 @@ static int text_write_atomic(const char *path, const char *new_data, size_t new_if (!failed && TEXT_SYNC(cbm_fileno(file)) != 0) {failed = 1;}+ text_file_snapshot_t trusted_temp_snapshot = {0};+ if (!failed && text_snapshot_from_file(file, &trusted_temp_snapshot) != TEXT_OK) {+ failed = 1;+ }+#ifndef _WIN32+ if (!failed && override_mode &&+ (trusted_temp_snapshot.owner != geteuid() ||+ (trusted_temp_snapshot.mode & 0777U) != (mode_t)(requested_mode & 0777U))) {+ failed = 1;+ }+#endifif (fclose(file) != 0) {failed = 1;}@@ -643,12 +706,17 @@ static int text_write_atomic(const char *path, const char *new_data, size_t new_free(temp_path);return TEXT_ERROR;}+#ifdef CBM_TEXT_EDIT_ENABLE_TEST_API+ if (text_temp_closed_test_hook) {+ text_temp_closed_test_hook(temp_path, text_temp_closed_test_context);+ }+#endifchar *temp_data = NULL;size_t temp_len = 0U;text_file_snapshot_t temp_snapshot;if (text_read_file(temp_path, &temp_data, &temp_len, &temp_snapshot) != TEXT_OK ||- !temp_snapshot.exists || temp_len != new_len ||- (new_len != 0U && memcmp(temp_data, new_data, new_len) != 0)) {+ !text_snapshot_publication_equal(&trusted_temp_snapshot, &temp_snapshot) ||+ temp_len != new_len || (new_len != 0U && memcmp(temp_data, new_data, new_len) != 0)) {free(temp_data);(void)cbm_unlink(temp_path);free(temp_path);@@ -682,6 +750,12 @@ static int text_write_atomic(const char *path, const char *new_data, size_t new_return TEXT_OK;}+static int text_write_atomic(const char *path, const char *new_data, size_t new_len,+ const char *old_data, size_t old_len,+ const text_file_snapshot_t *snapshot) {+ return text_write_atomic_mode(path, new_data, new_len, old_data, old_len, snapshot, 0, 0U);+}+static int text_delete_file(const char *path, const char *old_data, size_t old_len,const text_file_snapshot_t *snapshot) {#ifdef CBM_TEXT_EDIT_ENABLE_TEST_API@@ -726,6 +800,11 @@ void cbm_text_set_prepublish_hook_for_testing(cbm_text_precommit_test_hook_t hootext_prepublish_test_hook = hook;text_prepublish_test_context = context;}++void cbm_text_set_temp_closed_hook_for_testing(cbm_text_precommit_test_hook_t hook, void *context) {+ text_temp_closed_test_hook = hook;+ text_temp_closed_test_context = context;+}#endifstatic int text_bytes_equal(const char *data, size_t start, size_t end, const char *value,@@ -1143,13 +1222,15 @@ static int text_matches_candidate(const char *data, size_t data_len, const charreturn TEXT_OK;}-int cbm_text_migrate_owned_document(const char *file_path, const char *current_content,- const char *const *released_contents, size_t released_count) {+static int text_migrate_owned_document(const char *file_path, const char *current_content,+ const char *const *released_contents, size_t released_count,+ int override_mode, unsigned int requested_mode) {size_t current_len = 0U;if (!text_valid_path(file_path) ||text_bounded_strlen(current_content, TEXT_MAX_BYTES, ¤t_len) != TEXT_OK ||text_validate_bytes(current_content, current_len, 1) != TEXT_OK ||- (released_count > 0U && !released_contents)) {+ (released_count > 0U && !released_contents) ||+ !text_requested_mode_valid(override_mode, requested_mode)) {return TEXT_ERROR;}for (size_t i = 0U; i < released_count; i++) {@@ -1167,9 +1248,15 @@ int cbm_text_migrate_owned_document(const char *file_path, const char *current_cfree(old_data);return TEXT_ERROR;}+#ifndef _WIN32+ if (override_mode && snapshot.exists && snapshot.owner != geteuid()) {+ free(old_data);+ return TEXT_ERROR;+ }+#endifif (!snapshot.exists) {- int result = text_write_atomic(file_path, current_content, current_len, old_data, old_len,- &snapshot);+ int result = text_write_atomic_mode(file_path, current_content, current_len, old_data,+ old_len, &snapshot, override_mode, requested_mode);free(old_data);return result;}@@ -1180,6 +1267,14 @@ int cbm_text_migrate_owned_document(const char *file_path, const char *current_creturn TEXT_ERROR;}if (matches) {+#ifndef _WIN32+ if (override_mode && (snapshot.mode & 0777U) != (mode_t)(requested_mode & 0777U)) {+ int result = text_write_atomic_mode(file_path, current_content, current_len, old_data,+ old_len, &snapshot, override_mode, requested_mode);+ free(old_data);+ return result;+ }+#endiffree(old_data);return TEXT_OK;}@@ -1189,8 +1284,8 @@ int cbm_text_migrate_owned_document(const char *file_path, const char *current_creturn TEXT_ERROR;}if (matches) {- int result = text_write_atomic(file_path, current_content, current_len, old_data,- old_len, &snapshot);+ int result = text_write_atomic_mode(file_path, current_content, current_len, old_data,+ old_len, &snapshot, override_mode, requested_mode);free(old_data);return result;}@@ -1199,6 +1294,19 @@ int cbm_text_migrate_owned_document(const char *file_path, const char *current_creturn TEXT_UNOWNED;}+int cbm_text_migrate_owned_document(const char *file_path, const char *current_content,+ const char *const *released_contents, size_t released_count) {+ return text_migrate_owned_document(file_path, current_content, released_contents,+ released_count, 0, 0U);+}++int cbm_text_migrate_owned_document_mode(const char *file_path, const char *current_content,+ const char *const *released_contents,+ size_t released_count, unsigned int mode) {+ return text_migrate_owned_document(file_path, current_content, released_contents,+ released_count, 1, mode);+}+int cbm_text_remove_owned_document(const char *file_path, const char *expected_owned_content) {size_t expected_len = 0U;if (!text_valid_path(file_path) ||diff --git a/src/cli/config_text_edit.h b/src/cli/config_text_edit.hindex 550a0caf7..d075a331b 100644--- a/src/cli/config_text_edit.h+++ b/src/cli/config_text_edit.h@@ -31,6 +31,14 @@ int cbm_text_ensure_owned_document(const char *file_path, const char *owned_cont* 1 for user-modified/unowned content. */int cbm_text_migrate_owned_document(const char *file_path, const char *current_content,const char *const *released_contents, size_t released_count);+/* As above, but publish the owned document with the requested low permission+ * bits as part of the same atomic replacement. The mode must include owner-read+ * permission. Existing POSIX documents must be owned by the effective user.+ * On Windows the mode is validated but otherwise ignored because command+ * scripts do not use POSIX execute bits. */+int cbm_text_migrate_owned_document_mode(const char *file_path, const char *current_content,+ const char *const *released_contents,+ size_t released_count, unsigned int mode);/* Returns 0 when removed/missing, 1 when a regular document exists but is not* byte-for-byte owned by the caller, and -1 for unsafe state or I/O failure. */int cbm_text_remove_owned_document(const char *file_path, const char *expected_owned_content);@@ -44,6 +52,9 @@ void cbm_text_set_precommit_hook_for_testing(cbm_text_precommit_test_hook_t hook/* Runs after the first stale-snapshot check and before the final identity* revalidation used for publication or exact deletion. */void cbm_text_set_prepublish_hook_for_testing(cbm_text_precommit_test_hook_t hook, void *context);+/* Runs after the synced temporary descriptor is closed but before its pathname+ * is reopened and matched against the descriptor-bound snapshot. */+void cbm_text_set_temp_closed_hook_for_testing(cbm_text_precommit_test_hook_t hook, void *context);#endif#endifdiff --git a/src/cli/config_yaml_edit.c b/src/cli/config_yaml_edit.cindex a9c71bf5a..d05696d9f 100644--- a/src/cli/config_yaml_edit.c+++ b/src/cli/config_yaml_edit.c@@ -33,6 +33,7 @@#define YAML_PROCESS_ID _getpid#define YAML_SYNC _commit#else+#include <sys/file.h>#include <sys/types.h>#include <unistd.h>#define YAML_PROCESS_ID getpid@@ -71,6 +72,7 @@ enum {YAML_BOM_BYTE_0 = 0xef,YAML_BOM_BYTE_1 = 0xbb,YAML_BOM_BYTE_2 = 0xbf,+ YAML_LOCK_FILE_MODE = 0600,YAML_NEW_FILE_MODE = 0600,YAML_PERMISSION_MASK = 0777,};@@ -334,23 +336,33 @@ static int yaml_build_lock_path(const char *path, char **out_path) {return 0;}-#ifndef _WIN32-static int yaml_remove_owned_lock_directory(const char *path, dev_t device, ino_t inode,- uid_t owner) {- struct stat current;- if (lstat(path, ¤t) != 0 || !S_ISDIR(current.st_mode) || current.st_dev != device ||- current.st_ino != inode || current.st_uid != owner) {- return YAML_ERROR;- }- return rmdir(path) == 0 ? 0 : YAML_ERROR;-}-#else+#ifdef _WIN32static void yaml_remove_open_lock_directory(HANDLE handle) {FILE_DISPOSITION_INFO disposition = {.DeleteFile = TRUE};(void)SetFileInformationByHandle(handle, FileDispositionInfo, &disposition,sizeof(disposition));(void)CloseHandle(handle);}+#else+static bool yaml_lock_file_state_is_safe(const struct stat *state) {+ return S_ISREG(state->st_mode) && state->st_nlink == 1 && state->st_uid == geteuid() &&+ (state->st_mode & YAML_PERMISSION_MASK) == YAML_LOCK_FILE_MODE &&+ (state->st_mode & (S_ISUID | S_ISGID | S_ISVTX)) == 0;+}++static bool yaml_lock_file_state_matches(const struct stat *left, const struct stat *right) {+ return left->st_dev == right->st_dev && left->st_ino == right->st_ino &&+ left->st_mode == right->st_mode && left->st_nlink == right->st_nlink &&+ left->st_uid == right->st_uid;+}++static int yaml_flock_nointr(int descriptor, int operation) {+ int result = 0;+ do {+ result = flock(descriptor, operation);+ } while (result != 0 && errno == EINTR);+ return result == 0 ? 0 : YAML_ERROR;+}#endifstatic int yaml_lock_acquire(const char *path, yaml_config_lock_t *lock) {@@ -421,14 +433,34 @@ static int yaml_lock_acquire(const char *path, yaml_config_lock_t *lock) {lock->file_index_high = info.nFileIndexHigh;lock->file_index_low = info.nFileIndexLow;#else- if (mkdir(lock->path, 0700) != 0) {+#ifndef O_NOFOLLOW+ free(lock->path);+ lock->path = NULL;+ return YAML_ERROR;+#else+ int flags = O_RDWR | O_NOFOLLOW | O_NONBLOCK;+#ifdef O_CLOEXEC+ flags |= O_CLOEXEC;+#endif+ bool created = false;+ int descriptor = open(lock->path, flags | O_CREAT | O_EXCL, YAML_LOCK_FILE_MODE);+ if (descriptor >= 0) {+ created = true;+ } else if (errno == EEXIST) {+ descriptor = open(lock->path, flags);+ }+ if (descriptor < 0 || (created && fchmod(descriptor, YAML_LOCK_FILE_MODE) != 0)) {+ if (descriptor >= 0) {+ (void)close(descriptor);+ }free(lock->path);lock->path = NULL;return YAML_ERROR;}- struct stat created_state;- if (lstat(lock->path, &created_state) != 0) {- (void)rmdir(lock->path);++ struct stat opened_state;+ if (fstat(descriptor, &opened_state) != 0 || !yaml_lock_file_state_is_safe(&opened_state)) {+ (void)close(descriptor);free(lock->path);lock->path = NULL;return YAML_ERROR;@@ -438,45 +470,23 @@ static int yaml_lock_acquire(const char *path, yaml_config_lock_t *lock) {yaml_lock_postcreate_test_hook(lock->path, yaml_lock_postcreate_test_context);}#endif- struct stat path_state;- if (lstat(lock->path, &path_state) != 0) {- (void)yaml_remove_owned_lock_directory(lock->path, created_state.st_dev,- created_state.st_ino, created_state.st_uid);+ if (yaml_flock_nointr(descriptor, LOCK_EX | LOCK_NB) != 0) {+ (void)close(descriptor);free(lock->path);lock->path = NULL;return YAML_ERROR;}-#ifndef O_NOFOLLOW- (void)yaml_remove_owned_lock_directory(lock->path, created_state.st_dev, created_state.st_ino,- created_state.st_uid);- free(lock->path);- lock->path = NULL;- return YAML_ERROR;-#else- int flags = O_RDONLY | O_NOFOLLOW;-#ifdef O_DIRECTORY- flags |= O_DIRECTORY;-#endif-#ifdef O_CLOEXEC- flags |= O_CLOEXEC;-#endif- int descriptor = open(lock->path, flags);+struct stat handle_state;- bool safe =- descriptor >= 0 && fstat(descriptor, &handle_state) == 0 && S_ISDIR(path_state.st_mode) &&- S_ISDIR(handle_state.st_mode) && path_state.st_dev == created_state.st_dev &&- path_state.st_ino == created_state.st_ino && path_state.st_uid == created_state.st_uid &&- path_state.st_dev == handle_state.st_dev && path_state.st_ino == handle_state.st_ino &&- path_state.st_uid == geteuid() && handle_state.st_uid == geteuid() &&- (path_state.st_mode & 0777U) == 0700U && (handle_state.st_mode & 0777U) == 0700U &&- (path_state.st_mode & (S_ISUID | S_ISGID | S_ISVTX)) == 0 &&- (handle_state.st_mode & (S_ISUID | S_ISGID | S_ISVTX)) == 0;+ struct stat path_state;+ bool safe = fstat(descriptor, &handle_state) == 0 && lstat(lock->path, &path_state) == 0 &&+ yaml_lock_file_state_is_safe(&handle_state) &&+ yaml_lock_file_state_is_safe(&path_state) &&+ yaml_lock_file_state_matches(&opened_state, &handle_state) &&+ yaml_lock_file_state_matches(&handle_state, &path_state);if (!safe) {- if (descriptor >= 0) {- (void)close(descriptor);- }- (void)yaml_remove_owned_lock_directory(lock->path, created_state.st_dev,- created_state.st_ino, created_state.st_uid);+ (void)yaml_flock_nointr(descriptor, LOCK_UN);+ (void)close(descriptor);free(lock->path);lock->path = NULL;return YAML_ERROR;@@ -511,14 +521,16 @@ static int yaml_lock_release(yaml_config_lock_t *lock) {#elseif (lock->descriptor >= 0) {struct stat handle_state;- bool same = fstat(lock->descriptor, &handle_state) == 0 && S_ISDIR(handle_state.st_mode) &&- handle_state.st_dev == lock->device && handle_state.st_ino == lock->inode &&- handle_state.st_uid == lock->owner;- int removed = same ? yaml_remove_owned_lock_directory(lock->path, lock->device, lock->inode,- lock->owner)- : YAML_ERROR;+ struct stat path_state;+ bool same =+ fstat(lock->descriptor, &handle_state) == 0 && lstat(lock->path, &path_state) == 0 &&+ yaml_lock_file_state_is_safe(&handle_state) &&+ yaml_lock_file_state_is_safe(&path_state) && handle_state.st_dev == lock->device &&+ handle_state.st_ino == lock->inode && handle_state.st_uid == lock->owner &&+ yaml_lock_file_state_matches(&handle_state, &path_state);+ int unlocked = yaml_flock_nointr(lock->descriptor, LOCK_UN);int closed = close(lock->descriptor);- result = removed == 0 && closed == 0 ? 0 : YAML_ERROR;+ result = same && unlocked == 0 && closed == 0 ? 0 : YAML_ERROR;lock->descriptor = -1;}#endifdiff --git a/src/cli/config_yaml_edit.h b/src/cli/config_yaml_edit.hindex fc5391142..73b4d8841 100644--- a/src/cli/config_yaml_edit.h+++ b/src/cli/config_yaml_edit.h@@ -18,10 +18,13 @@ extern "C" {* replacement, the editor verifies that both the file identity and bytes still* match the version it read. POSIX replacements preserve owner, group, and* permission bits and sync the parent directory.- * Cooperating editor processes are serialized by an adjacent atomic- * "<file_path>.cbm-yaml.lock" directory held across read, transform, final- * verification, and replacement. Contention fails closed. An interrupted- * process can leave a stale lock directory that must be removed explicitly.+ * Cooperating editor processes are serialized across read, transform, final+ * verification, and replacement. POSIX uses an adjacent persistent+ * "<file_path>.cbm-yaml.lock" regular file, created with mode 0600 and held by+ * a non-blocking advisory lock; release unlocks and closes it without removing+ * the pathname, and process exit releases the lock automatically. Windows uses+ * an adjacent temporary lock directory removed by its verified open handle.+ * Contention and unsafe lock metadata fail closed.* Initially missing targets use no-replace publication. A non-cooperating* writer can still race an existing-target replacement in the narrow interval* after the final verification on platforms without destination-identity CAS.@@ -113,8 +116,8 @@ void cbm_yaml_set_precommit_hook_for_testing(cbm_yaml_precommit_test_hook_t hook/* Runs after the first stale-snapshot check and before final destination and* temporary-file identity revalidation. */void cbm_yaml_set_prepublish_hook_for_testing(cbm_yaml_precommit_test_hook_t hook, void *context);-/* Runs after the adjacent lock directory's initial identity is captured and- * before its final ownership, mode, and handle identity verification. */+/* Runs after the adjacent lock object's initial identity is captured and+ * before locking and final ownership, mode, and handle identity verification. */typedef void (*cbm_yaml_lock_postcreate_test_hook_t)(const char *lock_path, void *context);void cbm_yaml_set_lock_postcreate_hook_for_testing(cbm_yaml_lock_postcreate_test_hook_t hook,void *context);diff --git a/tests/test_config_text_edit.c b/tests/test_config_text_edit.cindex 567adfd08..47135be84 100644--- a/tests/test_config_text_edit.c+++ b/tests/test_config_text_edit.c@@ -26,6 +26,7 @@* these declarations here makes this RED test file independent of production* header edits while the implementation is developed. */void cbm_text_set_prepublish_hook_for_testing(cbm_text_precommit_test_hook_t hook, void *context);+void cbm_text_set_temp_closed_hook_for_testing(cbm_text_precommit_test_hook_t hook, void *context);int cbm_text_write_owned_document_if_unchanged(const char *file_path, const char *owned_content,const char *expected_content,size_t expected_length);@@ -134,6 +135,20 @@ static void cte_change_before_commit(const char *path, void *context) {change->result = th_write_file(path, change->content);}+#ifndef _WIN32+static void cte_replace_closed_temp(const char *path, void *context) {+ cte_precommit_change_t *change = (cte_precommit_change_t *)context;+ if (!change->backup_path || cbm_rename_replace(path, change->backup_path) != 0) {+ change->result = -1;+ return;+ }+ change->result = th_write_file(path, change->content);+ if (change->result == 0 && chmod(path, 0644) != 0) {+ change->result = -1;+ }+}+#endif+TEST(config_text_managed_insert_preserves_bom_comments_and_is_idempotent) {char dir[CTE_PATH_CAP];char path[CTE_PATH_CAP];@@ -282,6 +297,10 @@ TEST(config_text_owned_document_migrates_exact_releases_only) {ASSERT_EQ(th_write_file(path, modified), 0);ASSERT_EQ(cbm_text_migrate_owned_document(path, current, released, 2U), 1);ASSERT(cte_assert_bytes(path, modified, strlen(modified)));+ ASSERT_EQ(cbm_text_migrate_owned_document_mode(path, current, released, 2U, 04755U), -1);+ ASSERT(cte_assert_bytes(path, modified, strlen(modified)));+ ASSERT_EQ(cbm_text_migrate_owned_document_mode(path, current, released, 2U, 0200U), -1);+ ASSERT(cte_assert_bytes(path, modified, strlen(modified)));ASSERT_EQ(cbm_text_remove_owned_document_any(path, current, released, 2U), 1);ASSERT(cte_assert_bytes(path, modified, strlen(modified)));@@ -366,11 +385,13 @@ TEST(config_text_rejects_links_privileged_mode_and_preserves_metadata) {ASSERT_EQ(th_write_file(target, "target\n"), 0);ASSERT_EQ(symlink(target, path), 0);ASSERT_EQ(cbm_text_write_owned_document(path, "owned\n"), -1);+ ASSERT_EQ(cbm_text_migrate_owned_document_mode(path, "owned\n", NULL, 0U, 0755U), -1);ASSERT_EQ(cbm_unlink(path), 0);ASSERT_EQ(th_write_file(path, "shared\n"), 0);ASSERT_EQ(link(path, alias), 0);ASSERT_EQ(cbm_text_upsert_managed_block(path, CTE_BEGIN, CTE_END, "owned"), -1);+ ASSERT_EQ(cbm_text_migrate_owned_document_mode(path, "owned\n", NULL, 0U, 0755U), -1);ASSERT_EQ(cbm_unlink(alias), 0);ASSERT_EQ(chmod(path, 04755), 0);@@ -392,6 +413,107 @@ TEST(config_text_rejects_links_privileged_mode_and_preserves_metadata) {th_cleanup(dir);PASS();}++TEST(config_text_owned_document_mode_publishes_exact_bytes_atomically) {+ char dir[CTE_PATH_CAP];+ char path[CTE_PATH_CAP];+ const char *current = "#!/bin/sh\necho current\n";+ const char *released[] = {"#!/bin/sh\necho released\n"};+ const char *foreign = "#!/bin/sh\necho foreign\n";+ ASSERT_EQ(cte_fixture(dir, sizeof(dir), path, sizeof(path)), 0);++ ASSERT_EQ(cbm_text_migrate_owned_document_mode(path, current, released, 1U, 0755U), 0);+ struct stat state;+ ASSERT_EQ(stat(path, &state), 0);+ ASSERT_EQ(state.st_mode & 0777U, 0755U);+ ASSERT_EQ(state.st_uid, geteuid());+ ASSERT(cte_assert_bytes(path, current, strlen(current)));+ struct stat stable;+ ASSERT_EQ(stat(path, &stable), 0);+ ASSERT_EQ(cbm_text_migrate_owned_document_mode(path, current, released, 1U, 0755U), 0);+ ASSERT_EQ(stat(path, &state), 0);+ ASSERT_EQ(state.st_ino, stable.st_ino);++ ASSERT_EQ(chmod(path, 0600), 0);+ struct stat before;+ ASSERT_EQ(stat(path, &before), 0);+ ASSERT_EQ(cbm_text_migrate_owned_document_mode(path, current, released, 1U, 0755U), 0);+ ASSERT_EQ(stat(path, &state), 0);+ ASSERT_EQ(state.st_mode & 0777U, 0755U);+ ASSERT(state.st_ino != before.st_ino);+ ASSERT(cte_assert_bytes(path, current, strlen(current)));++ ASSERT_EQ(th_write_file(path, released[0]), 0);+ ASSERT_EQ(chmod(path, 0640), 0);+ ASSERT_EQ(cbm_text_migrate_owned_document_mode(path, current, released, 1U, 0755U), 0);+ ASSERT_EQ(stat(path, &state), 0);+ ASSERT_EQ(state.st_mode & 0777U, 0755U);+ ASSERT(cte_assert_bytes(path, current, strlen(current)));++ ASSERT_EQ(th_write_file(path, foreign), 0);+ ASSERT_EQ(chmod(path, 0640), 0);+ ASSERT_EQ(cbm_text_migrate_owned_document_mode(path, current, released, 1U, 0755U), 1);+ ASSERT_EQ(stat(path, &state), 0);+ ASSERT_EQ(state.st_mode & 0777U, 0640U);+ ASSERT(cte_assert_bytes(path, foreign, strlen(foreign)));+ ASSERT_EQ(cte_temp_count(dir), 0U);+ th_cleanup(dir);+ PASS();+}++TEST(config_text_owned_document_mode_rejects_prepublish_replacement) {+ char dir[CTE_PATH_CAP];+ char path[CTE_PATH_CAP];+ char backup[CTE_PATH_CAP];+ const char *current = "#!/bin/sh\necho current\n";+ const char *winner = "#!/bin/sh\necho winner\n";+ ASSERT_EQ(cte_fixture(dir, sizeof(dir), path, sizeof(path)), 0);+ ASSERT(snprintf(backup, sizeof(backup), "%s/original.sh", dir) > 0);+ ASSERT_EQ(th_write_file(path, current), 0);+ ASSERT_EQ(chmod(path, 0600), 0);+ cte_precommit_change_t race = {+ .content = winner, .backup_path = backup, .replace_identity = 1, .result = -1};++ cbm_text_set_prepublish_hook_for_testing(cte_change_before_commit, &race);+ int result = cbm_text_migrate_owned_document_mode(path, current, NULL, 0U, 0755U);+ cbm_text_set_prepublish_hook_for_testing(NULL, NULL);++ ASSERT_EQ(race.result, 0);+ ASSERT_EQ(result, -1);+ ASSERT(cte_assert_bytes(path, winner, strlen(winner)));+ struct stat state;+ ASSERT_EQ(stat(path, &state), 0);+ ASSERT_EQ(state.st_mode & 0111U, 0U);+ ASSERT_EQ(cte_temp_count(dir), 0U);+ ASSERT_EQ(cbm_unlink(backup), 0);+ th_cleanup(dir);+ PASS();+}++TEST(config_text_owned_document_mode_rejects_closed_temp_replacement) {+ char dir[CTE_PATH_CAP];+ char path[CTE_PATH_CAP];+ char backup[CTE_PATH_CAP];+ const char *current = "#!/bin/sh\necho current\n";+ ASSERT_EQ(cte_fixture(dir, sizeof(dir), path, sizeof(path)), 0);+ ASSERT(snprintf(backup, sizeof(backup), "%s/original-temp", dir) > 0);+ cte_precommit_change_t race = {+ .content = current, .backup_path = backup, .replace_identity = 1, .result = -1};++ cbm_text_set_temp_closed_hook_for_testing(cte_replace_closed_temp, &race);+ int result = cbm_text_migrate_owned_document_mode(path, current, NULL, 0U, 0755U);+ cbm_text_set_temp_closed_hook_for_testing(NULL, NULL);++ ASSERT_EQ(race.result, 0);+ ASSERT_EQ(result, -1);+ ASSERT_FALSE(cte_path_exists(path));+ ASSERT(cte_assert_bytes(backup, current, strlen(current)));+ struct stat trusted;+ ASSERT_EQ(stat(backup, &trusted), 0);+ ASSERT_EQ(trusted.st_mode & 0777U, 0755U);+ th_cleanup(dir);+ PASS();+}#endifTEST(config_text_rejects_stale_content_and_identity) {@@ -548,6 +670,9 @@ SUITE(config_text_edit) {RUN_TEST(config_text_rejects_non_regular_paths);#ifndef _WIN32RUN_TEST(config_text_rejects_links_privileged_mode_and_preserves_metadata);+ RUN_TEST(config_text_owned_document_mode_publishes_exact_bytes_atomically);+ RUN_TEST(config_text_owned_document_mode_rejects_prepublish_replacement);+ RUN_TEST(config_text_owned_document_mode_rejects_closed_temp_replacement);#endifRUN_TEST(config_text_rejects_stale_content_and_identity);RUN_TEST(config_text_missing_target_race_does_not_replace_winner);diff --git a/tests/test_config_yaml_edit.c b/tests/test_config_yaml_edit.cindex 90c010084..88b15cf5c 100644--- a/tests/test_config_yaml_edit.c+++ b/tests/test_config_yaml_edit.c@@ -20,7 +20,7 @@#endif/* Expected test seams for races after final verification and failures after- * lock-directory creation. Production code must not expose them outside the+ * lock-object creation. Production code must not expose them outside the* test API build. */void cbm_yaml_set_prepublish_hook_for_testing(cbm_yaml_precommit_test_hook_t hook, void *context);#ifndef _WIN32@@ -105,6 +105,17 @@ static size_t yaml_temp_file_count(const yaml_fixture_t *fixture) {return count;}+static bool yaml_lock_released_state_is_safe(const char *path) {+ struct stat state;+#ifdef _WIN32+ return stat(path, &state) != 0;+#else+ return lstat(path, &state) == 0 && S_ISREG(state.st_mode) && state.st_nlink == 1 &&+ state.st_uid == geteuid() && (state.st_mode & 0777U) == 0600U &&+ (state.st_mode & (S_ISUID | S_ISGID | S_ISVTX)) == 0;+#endif+}+static bool yaml_upsert_failed_unchanged(const char *path, const char *original) {const char *block = " command: codebase-memory-mcp\n";if (th_write_file(path, original) != 0 ||@@ -151,8 +162,15 @@ static void yaml_attempt_competing_edit(const char *path, void *context) {char lock_path[1024];int written = snprintf(lock_path, sizeof(lock_path), "%s.cbm-yaml.lock", path);struct stat state;- contention->lock_observed = written > 0 && (size_t)written < sizeof(lock_path) &&- stat(lock_path, &state) == 0 && S_ISDIR(state.st_mode);+ bool expected_type = false;+ if (written > 0 && (size_t)written < sizeof(lock_path) && stat(lock_path, &state) == 0) {+#ifdef _WIN32+ expected_type = S_ISDIR(state.st_mode);+#else+ expected_type = S_ISREG(state.st_mode);+#endif+ }+ contention->lock_observed = expected_type;contention->competing_result = cbm_yaml_upsert_string_list_item(path, "read", "COMPETING.md");contention->lock_observed = contention->lock_observed || contention->competing_result == -1;}@@ -180,8 +198,7 @@ TEST(config_yaml_edit_serializes_two_editor_instances) {free(after);char lock_path[1024];ASSERT(snprintf(lock_path, sizeof(lock_path), "%s.cbm-yaml.lock", fixture.path) > 0);- struct stat state;- ASSERT(stat(lock_path, &state) != 0);+ ASSERT(yaml_lock_released_state_is_safe(lock_path));ASSERT_EQ(yaml_temp_file_count(&fixture), 0U);th_cleanup(fixture.dir);PASS();@@ -210,8 +227,7 @@ TEST(config_yaml_edit_missing_target_appearance_fails_without_replace) {free(after);char lock_path[1024];ASSERT(snprintf(lock_path, sizeof(lock_path), "%s.cbm-yaml.lock", fixture.path) > 0);- struct stat state;- ASSERT(stat(lock_path, &state) != 0);+ ASSERT(yaml_lock_released_state_is_safe(lock_path));ASSERT_EQ(yaml_temp_file_count(&fixture), 0U);th_cleanup(fixture.dir);PASS();@@ -307,14 +323,13 @@ TEST(config_yaml_edit_existing_target_swap_after_check_preserves_winner) {ASSERT_EQ(cbm_unlink(backup), 0);char lock_path[1024];ASSERT(snprintf(lock_path, sizeof(lock_path), "%s.cbm-yaml.lock", fixture.path) > 0);- struct stat lock_state;- ASSERT(stat(lock_path, &lock_state) != 0);+ ASSERT(yaml_lock_released_state_is_safe(lock_path));th_cleanup(fixture.dir);PASS();}#ifndef _WIN32-TEST(config_yaml_edit_lock_postcreate_verification_failure_cleans_owned_lock) {+TEST(config_yaml_edit_lock_postcreate_verification_failure_preserves_unsafe_sidecar) {yaml_fixture_t fixture;ASSERT_EQ(yaml_fixture_init(&fixture, "model: fast\n"), 0);int mutation_result = -1;@@ -328,11 +343,116 @@ TEST(config_yaml_edit_lock_postcreate_verification_failure_cleans_owned_lock) {char lock_path[1024];ASSERT(snprintf(lock_path, sizeof(lock_path), "%s.cbm-yaml.lock", fixture.path) > 0);struct stat state;- ASSERT(stat(lock_path, &state) != 0);+ ASSERT_EQ(lstat(lock_path, &state), 0);+ ASSERT(S_ISREG(state.st_mode));+ ASSERT_EQ(state.st_uid, geteuid());+ ASSERT_EQ(state.st_mode & 0777U, 0755U);ASSERT_EQ(yaml_temp_file_count(&fixture), 0U);th_cleanup(fixture.dir);PASS();}++TEST(config_yaml_edit_reuses_persistent_safe_lock_sidecar) {+ yaml_fixture_t fixture;+ ASSERT_EQ(yaml_fixture_init(&fixture, "model: fast\n"), 0);++ ASSERT_EQ(cbm_yaml_upsert_string_list_item(fixture.path, "read", "AGENTS.md"), 0);+ char lock_path[1024];+ ASSERT(snprintf(lock_path, sizeof(lock_path), "%s.cbm-yaml.lock", fixture.path) > 0);+ ASSERT(yaml_lock_released_state_is_safe(lock_path));+ struct stat first;+ ASSERT_EQ(lstat(lock_path, &first), 0);++ ASSERT_EQ(cbm_yaml_upsert_string_list_item(fixture.path, "read", "CONVENTIONS.md"), 0);+ ASSERT(yaml_lock_released_state_is_safe(lock_path));+ struct stat second;+ ASSERT_EQ(lstat(lock_path, &second), 0);+ ASSERT_EQ(first.st_dev, second.st_dev);+ ASSERT_EQ(first.st_ino, second.st_ino);+ char *after = yaml_read_alloc(fixture.path);+ ASSERT_NOT_NULL(after);+ ASSERT_NOT_NULL(strstr(after, "AGENTS.md"));+ ASSERT_NOT_NULL(strstr(after, "CONVENTIONS.md"));+ free(after);+ th_cleanup(fixture.dir);+ PASS();+}++TEST(config_yaml_edit_rejects_symlink_lock_sidecar) {+ const char *original = "model: fast\n";+ yaml_fixture_t fixture;+ ASSERT_EQ(yaml_fixture_init(&fixture, original), 0);+ char lock_path[1024];+ char target_path[1024];+ ASSERT(snprintf(lock_path, sizeof(lock_path), "%s.cbm-yaml.lock", fixture.path) > 0);+ ASSERT(snprintf(target_path, sizeof(target_path), "%s/foreign-lock", fixture.dir) > 0);+ ASSERT_EQ(th_write_file(target_path, "foreign\n"), 0);+ ASSERT_EQ(symlink(target_path, lock_path), 0);++ ASSERT_EQ(cbm_yaml_upsert_string_list_item(fixture.path, "read", "AGENTS.md"), -1);+ struct stat link_state;+ ASSERT_EQ(lstat(lock_path, &link_state), 0);+ ASSERT(S_ISLNK(link_state.st_mode));+ char *after = yaml_read_alloc(fixture.path);+ ASSERT_NOT_NULL(after);+ ASSERT_STR_EQ(after, original);+ free(after);+ char *target = yaml_read_alloc(target_path);+ ASSERT_NOT_NULL(target);+ ASSERT_STR_EQ(target, "foreign\n");+ free(target);+ th_cleanup(fixture.dir);+ PASS();+}++TEST(config_yaml_edit_rejects_hard_linked_lock_sidecar) {+ const char *original = "model: fast\n";+ yaml_fixture_t fixture;+ ASSERT_EQ(yaml_fixture_init(&fixture, original), 0);+ char lock_path[1024];+ char source_path[1024];+ ASSERT(snprintf(lock_path, sizeof(lock_path), "%s.cbm-yaml.lock", fixture.path) > 0);+ ASSERT(snprintf(source_path, sizeof(source_path), "%s/foreign-lock", fixture.dir) > 0);+ ASSERT_EQ(th_write_file(source_path, "foreign\n"), 0);+ ASSERT_EQ(chmod(source_path, 0600), 0);+ ASSERT_EQ(link(source_path, lock_path), 0);++ ASSERT_EQ(cbm_yaml_upsert_string_list_item(fixture.path, "read", "AGENTS.md"), -1);+ struct stat source_state;+ struct stat lock_state;+ ASSERT_EQ(lstat(source_path, &source_state), 0);+ ASSERT_EQ(lstat(lock_path, &lock_state), 0);+ ASSERT_EQ(source_state.st_ino, lock_state.st_ino);+ ASSERT_EQ(source_state.st_nlink, 2);+ char *after = yaml_read_alloc(fixture.path);+ ASSERT_NOT_NULL(after);+ ASSERT_STR_EQ(after, original);+ free(after);+ th_cleanup(fixture.dir);+ PASS();+}++TEST(config_yaml_edit_rejects_unsafe_mode_lock_sidecar) {+ const char *original = "model: fast\n";+ yaml_fixture_t fixture;+ ASSERT_EQ(yaml_fixture_init(&fixture, original), 0);+ char lock_path[1024];+ ASSERT(snprintf(lock_path, sizeof(lock_path), "%s.cbm-yaml.lock", fixture.path) > 0);+ ASSERT_EQ(th_write_file(lock_path, "foreign\n"), 0);+ ASSERT_EQ(chmod(lock_path, 0644), 0);++ ASSERT_EQ(cbm_yaml_upsert_string_list_item(fixture.path, "read", "AGENTS.md"), -1);+ struct stat state;+ ASSERT_EQ(lstat(lock_path, &state), 0);+ ASSERT(S_ISREG(state.st_mode));+ ASSERT_EQ(state.st_mode & 0777U, 0644U);+ char *after = yaml_read_alloc(fixture.path);+ ASSERT_NOT_NULL(after);+ ASSERT_STR_EQ(after, original);+ free(after);+ th_cleanup(fixture.dir);+ PASS();+}#endifTEST(config_yaml_edit_rejects_non_regular_path) {@@ -1413,7 +1533,11 @@ SUITE(config_yaml_edit) {RUN_TEST(config_yaml_edit_rejects_stale_identity_with_same_content);RUN_TEST(config_yaml_edit_existing_target_swap_after_check_preserves_winner);#ifndef _WIN32- RUN_TEST(config_yaml_edit_lock_postcreate_verification_failure_cleans_owned_lock);+ RUN_TEST(config_yaml_edit_lock_postcreate_verification_failure_preserves_unsafe_sidecar);+ RUN_TEST(config_yaml_edit_reuses_persistent_safe_lock_sidecar);+ RUN_TEST(config_yaml_edit_rejects_symlink_lock_sidecar);+ RUN_TEST(config_yaml_edit_rejects_hard_linked_lock_sidecar);+ RUN_TEST(config_yaml_edit_rejects_unsafe_mode_lock_sidecar);#endifRUN_TEST(config_yaml_edit_rejects_non_regular_path);#ifndef _WIN32