Skip to content

Commit 3aec07e

Browse files
committed
fix(daemon): stop the lifetime-lock probe from dropping a held lock
posix_lifetime_lock_probe opened a throwaway fd on the lock file to confirm an already-claimed lock, then closed it. fcntl(2) record locks are scoped to (process, inode), so that close silently released the real lock too, even though the reservation's own fd stayed open. Check the in-process claim before opening anything and return early; that branch only ever needed to trust the registry. Signed-off-by: Amir Fathi <amirfathi.me@gmail.com>
1 parent 64c23fa commit 3aec07e

2 files changed

Lines changed: 113 additions & 4 deletions

File tree

‎src/daemon/ipc.c‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,15 @@ static int posix_lifetime_lock_probe(const cbm_daemon_ipc_endpoint_t *endpoint,
12051205
if (process_claimed < 0) {
12061206
return -1;
12071207
}
1208+
if (process_claimed == 1) {
1209+
/* Already held by this process per the in-process registry: trust
1210+
* that alone. Opening a throwaway fd on the lock file here and then
1211+
* closing it would release the real fcntl(2) record lock, which is
1212+
* scoped to (process, inode) rather than (fd, inode): closing any
1213+
* fd on this inode drops every lock this process holds on it, even
1214+
* one held via a different, still-open fd. */
1215+
return endpoint_runtime_still_valid(endpoint) ? 1 : -1;
1216+
}
12081217

12091218
int fd = openat(endpoint->dir_fd, lock_name, O_RDWR | O_CLOEXEC | O_NOFOLLOW);
12101219
if (fd < 0) {
@@ -1216,10 +1225,6 @@ static int posix_lifetime_lock_probe(const cbm_daemon_ipc_endpoint_t *endpoint,
12161225
(void)close(fd);
12171226
return -1;
12181227
}
1219-
if (process_claimed == 1) {
1220-
bool still_private = endpoint_runtime_still_valid(endpoint);
1221-
return close(fd) == 0 && still_private ? 1 : -1;
1222-
}
12231228
struct flock record_lock = {
12241229
.l_type = F_WRLCK,
12251230
.l_whence = SEEK_SET,

‎tests/test_daemon_ipc.c‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3365,6 +3365,109 @@ TEST(daemon_ipc_posix_lifetime_reservation_rejects_fork_inheritance) {
33653365
PASS();
33663366
}
33673367

3368+
static bool ipc_test_lifetime_lock_path(char out[TEST_PATH_CAP], const char *runtime_dir,
3369+
const char *key) {
3370+
int written = runtime_dir && key
3371+
? snprintf(out, TEST_PATH_CAP, "%s/cbm-%s.lifetime.lock", runtime_dir, key)
3372+
: -1;
3373+
return written > 0 && written < TEST_PATH_CAP;
3374+
}
3375+
3376+
TEST(daemon_ipc_posix_lifetime_reservation_probe_does_not_drop_held_lock) {
3377+
static const char key[] = "c1c1d2d2e3e3f4f4";
3378+
char parent[TEST_PATH_CAP] = {0};
3379+
char runtime_dir[TEST_PATH_CAP] = {0};
3380+
char lock_path[TEST_PATH_CAP] = {0};
3381+
cbm_daemon_ipc_endpoint_t *endpoint = NULL;
3382+
cbm_daemon_ipc_lifetime_reservation_t *reservation = NULL;
3383+
int result_pipe[2] = {-1, -1};
3384+
pid_t child = -1;
3385+
uint8_t child_result = 0;
3386+
int child_status = -1;
3387+
int acquired = -1;
3388+
int held_after_first_probe = -1;
3389+
int held_after_second_probe = -1;
3390+
int free_after_release = -1;
3391+
bool lock_path_ok = false;
3392+
3393+
if (ipc_test_parent_new(parent, "probe-holds-lock")) {
3394+
endpoint = cbm_daemon_ipc_endpoint_new(key, parent);
3395+
}
3396+
if (endpoint) {
3397+
ipc_test_copy_path(runtime_dir, cbm_daemon_ipc_endpoint_runtime_dir(endpoint));
3398+
lock_path_ok = ipc_test_lifetime_lock_path(lock_path, runtime_dir, key);
3399+
acquired = cbm_daemon_ipc_lifetime_reservation_try_acquire(endpoint, &reservation);
3400+
}
3401+
if (reservation) {
3402+
/* Two probes, matching how a real daemon polls its own reservation
3403+
* repeatedly while running. Each probe of an already-held lock used
3404+
* to open a throwaway fd on the lock file and close it. Since
3405+
* fcntl(2) record locks are scoped to (process, inode), that close
3406+
* silently released the real lock every time, regardless of the
3407+
* still-open fd the reservation itself is holding. */
3408+
held_after_first_probe = cbm_daemon_ipc_lifetime_reservation_probe(endpoint);
3409+
held_after_second_probe = cbm_daemon_ipc_lifetime_reservation_probe(endpoint);
3410+
}
3411+
if (reservation && lock_path_ok && pipe(result_pipe) == 0) {
3412+
child = fork();
3413+
}
3414+
if (child == 0) {
3415+
/* A genuinely independent process, not going through any of this
3416+
* codebase's APIs so it cannot inherit the parent's in-process
3417+
* claim registry: try to take the real OS-level lock directly. If
3418+
* the parent's fcntl lock survived the probes above, this fails. */
3419+
(void)close(result_pipe[0]);
3420+
int fd = open(lock_path, O_RDWR);
3421+
struct flock record_lock = {
3422+
.l_type = F_WRLCK,
3423+
.l_whence = SEEK_SET,
3424+
.l_start = 0,
3425+
.l_len = 0,
3426+
};
3427+
int lock_result = fd >= 0 ? fcntl(fd, F_SETLK, &record_lock) : -1;
3428+
child_result = lock_result == 0 ? 1 : 0;
3429+
if (fd >= 0) {
3430+
(void)close(fd);
3431+
}
3432+
bool reported = ipc_test_fd_write_all(result_pipe[1], &child_result, sizeof(child_result));
3433+
(void)close(result_pipe[1]);
3434+
_exit(reported ? 0 : 1);
3435+
}
3436+
if (child > 0) {
3437+
(void)close(result_pipe[1]);
3438+
result_pipe[1] = -1;
3439+
bool received = ipc_test_fd_read_all(result_pipe[0], &child_result, sizeof(child_result));
3440+
(void)close(result_pipe[0]);
3441+
result_pipe[0] = -1;
3442+
if (!received) {
3443+
child_result = 2;
3444+
}
3445+
while (waitpid(child, &child_status, 0) < 0 && errno == EINTR) {}
3446+
}
3447+
cbm_daemon_ipc_lifetime_reservation_release(reservation);
3448+
if (endpoint) {
3449+
free_after_release = cbm_daemon_ipc_lifetime_reservation_probe(endpoint);
3450+
}
3451+
for (size_t index = 0; index < 2; index++) {
3452+
if (result_pipe[index] >= 0) {
3453+
(void)close(result_pipe[index]);
3454+
}
3455+
}
3456+
cbm_daemon_ipc_endpoint_free(endpoint);
3457+
ipc_test_remove_tree(runtime_dir, parent);
3458+
3459+
ASSERT_EQ(acquired, 1);
3460+
ASSERT_TRUE(lock_path_ok);
3461+
ASSERT_EQ(held_after_first_probe, 1);
3462+
ASSERT_EQ(held_after_second_probe, 1);
3463+
ASSERT_GT(child, 0);
3464+
ASSERT_TRUE(WIFEXITED(child_status));
3465+
ASSERT_EQ(WEXITSTATUS(child_status), 0);
3466+
ASSERT_EQ(child_result, 0);
3467+
ASSERT_EQ(free_after_release, 0);
3468+
PASS();
3469+
}
3470+
33683471
TEST(daemon_ipc_posix_child_participant_handoff_retains_legacy_bridge) {
33693472
#ifdef _WIN32
33703473
PASS();
@@ -5524,6 +5627,7 @@ SUITE(daemon_ipc) {
55245627
#endif
55255628
RUN_TEST(daemon_ipc_posix_startup_lock_is_cross_process);
55265629
RUN_TEST(daemon_ipc_posix_lifetime_reservation_rejects_fork_inheritance);
5630+
RUN_TEST(daemon_ipc_posix_lifetime_reservation_probe_does_not_drop_held_lock);
55275631
RUN_TEST(daemon_ipc_posix_child_participant_handoff_retains_legacy_bridge);
55285632
RUN_TEST(daemon_ipc_posix_publication_boundaries_recover_from_crash);
55295633
RUN_TEST(daemon_ipc_posix_record_publication_windows_recover_from_crash);

0 commit comments

Comments
 (0)