Skip to content

Commit 8bd9d37

Browse files
committed
fix(daemon): stop the lifetime-lock probe from dropping a held lock
posix_lifetime_lock_try_acquire opened a throwaway fd on the lock file to confirm an already-claimed lock, then closed it, in both the probe path and the try-acquire-again path. 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 in both places; neither branch ever needed more than the registry. Also hardens the child-process verdict in the regression test: it now reports held only when open() succeeded and F_SETLK failed with EACCES or EAGAIN, so a wrong path or an unrelated errno can no longer read as a pass. Signed-off-by: Amir Fathi <amirfathi.me@gmail.com>
1 parent 3aec07e commit 8bd9d37

2 files changed

Lines changed: 141 additions & 18 deletions

File tree

‎src/daemon/ipc.c‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,13 @@ static int posix_named_shared_lock_try_acquire(const cbm_daemon_ipc_endpoint_t *
11821182
return posix_named_lock_try_acquire_mode(endpoint, lock_name, true, fd_out, process_entry_out);
11831183
}
11841184

1185+
/* Never open and close the lifetime lock file in a process that may already
1186+
* hold it: fcntl(2) record locks are scoped to (process, inode), so that
1187+
* close would silently release every lock this process holds on the file,
1188+
* regardless of which fd took it. This lock is fcntl rather than flock for
1189+
* exactly this reason in reverse: flock locks survive a fork, fcntl locks
1190+
* do not, and a forked child must never appear to hold its parent's
1191+
* reservation. */
11851192
static int posix_record_lock_set(int fd, short lock_type) {
11861193
struct flock record_lock = {
11871194
.l_type = lock_type,
@@ -1262,11 +1269,13 @@ static int posix_lifetime_lock_try_acquire(const cbm_daemon_ipc_endpoint_t *endp
12621269
process_lock_entry_t *process_entry = NULL;
12631270
int process_result = process_lock_claim(endpoint, lock_name, &process_entry);
12641271
if (process_result != 1) {
1265-
return process_result == 0 &&
1266-
private_regular_file_at_is_safe(endpoint->dir_fd, lock_name, 1) &&
1267-
endpoint_runtime_still_valid(endpoint)
1268-
? 0
1269-
: -1;
1272+
/* Already held by this process per the in-process registry: trust
1273+
* that alone, the same as the probe's early return above. Calling
1274+
* private_regular_file_at_is_safe() here would open a throwaway fd
1275+
* on the lock file and close it, releasing the real fcntl(2) record
1276+
* lock this process holds, because that lock is scoped to (process,
1277+
* inode) rather than (fd, inode). */
1278+
return process_result == 0 && endpoint_runtime_still_valid(endpoint) ? 0 : -1;
12701279
}
12711280
int fd = openat(endpoint->dir_fd, lock_name, O_RDWR | O_CREAT | O_CLOEXEC | O_NOFOLLOW, 0600);
12721281
if (fd < 0 || !fd_set_cloexec(fd)) {

‎tests/test_daemon_ipc.c‎

Lines changed: 127 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3373,6 +3373,46 @@ static bool ipc_test_lifetime_lock_path(char out[TEST_PATH_CAP], const char *run
33733373
return written > 0 && written < TEST_PATH_CAP;
33743374
}
33753375

3376+
/* Child-process verdict on whether the lifetime lock at lock_path is held by
3377+
* someone else, going straight through open()/fcntl() rather than any of
3378+
* this codebase's APIs, so a genuinely separate process cannot inherit the
3379+
* parent's in-process claim registry. Distinguishes three outcomes instead
3380+
* of collapsing "held" and "could not even check" into the same value:
3381+
* IPC_TEST_LOCK_HELD only when open() succeeded and F_SETLK failed with
3382+
* EACCES or EAGAIN (the lock is held by another process, as expected);
3383+
* IPC_TEST_LOCK_TAKEN when F_SETLK succeeded (the lock was free, a bug);
3384+
* IPC_TEST_LOCK_ERROR for anything else (open() failed, e.g. a wrong path,
3385+
* or F_SETLK failed with an errno that says nothing about the lock's
3386+
* state), which must never be mistaken for IPC_TEST_LOCK_HELD. */
3387+
enum {
3388+
IPC_TEST_LOCK_HELD = 0,
3389+
IPC_TEST_LOCK_TAKEN = 1,
3390+
IPC_TEST_LOCK_ERROR = 3,
3391+
};
3392+
3393+
static uint8_t ipc_test_lock_child_verdict(const char *lock_path) {
3394+
int fd = open(lock_path, O_RDWR);
3395+
if (fd < 0) {
3396+
return IPC_TEST_LOCK_ERROR;
3397+
}
3398+
struct flock record_lock = {
3399+
.l_type = F_WRLCK,
3400+
.l_whence = SEEK_SET,
3401+
.l_start = 0,
3402+
.l_len = 0,
3403+
};
3404+
int lock_result;
3405+
do {
3406+
lock_result = fcntl(fd, F_SETLK, &record_lock);
3407+
} while (lock_result != 0 && errno == EINTR);
3408+
int lock_errno = errno;
3409+
(void)close(fd);
3410+
if (lock_result == 0) {
3411+
return IPC_TEST_LOCK_TAKEN;
3412+
}
3413+
return lock_errno == EACCES || lock_errno == EAGAIN ? IPC_TEST_LOCK_HELD : IPC_TEST_LOCK_ERROR;
3414+
}
3415+
33763416
TEST(daemon_ipc_posix_lifetime_reservation_probe_does_not_drop_held_lock) {
33773417
static const char key[] = "c1c1d2d2e3e3f4f4";
33783418
char parent[TEST_PATH_CAP] = {0};
@@ -3417,18 +3457,7 @@ TEST(daemon_ipc_posix_lifetime_reservation_probe_does_not_drop_held_lock) {
34173457
* claim registry: try to take the real OS-level lock directly. If
34183458
* the parent's fcntl lock survived the probes above, this fails. */
34193459
(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-
}
3460+
child_result = ipc_test_lock_child_verdict(lock_path);
34323461
bool reported = ipc_test_fd_write_all(result_pipe[1], &child_result, sizeof(child_result));
34333462
(void)close(result_pipe[1]);
34343463
_exit(reported ? 0 : 1);
@@ -3463,7 +3492,91 @@ TEST(daemon_ipc_posix_lifetime_reservation_probe_does_not_drop_held_lock) {
34633492
ASSERT_GT(child, 0);
34643493
ASSERT_TRUE(WIFEXITED(child_status));
34653494
ASSERT_EQ(WEXITSTATUS(child_status), 0);
3466-
ASSERT_EQ(child_result, 0);
3495+
ASSERT_EQ(child_result, IPC_TEST_LOCK_HELD);
3496+
ASSERT_EQ(free_after_release, 0);
3497+
PASS();
3498+
}
3499+
3500+
TEST(daemon_ipc_posix_lifetime_reservation_try_acquire_again_does_not_drop_held_lock) {
3501+
static const char key[] = "a1a1b2b2c3c3d4d4";
3502+
char parent[TEST_PATH_CAP] = {0};
3503+
char runtime_dir[TEST_PATH_CAP] = {0};
3504+
char lock_path[TEST_PATH_CAP] = {0};
3505+
cbm_daemon_ipc_endpoint_t *endpoint = NULL;
3506+
cbm_daemon_ipc_lifetime_reservation_t *reservation = NULL;
3507+
cbm_daemon_ipc_lifetime_reservation_t *second_reservation =
3508+
(cbm_daemon_ipc_lifetime_reservation_t *)(uintptr_t)1;
3509+
int result_pipe[2] = {-1, -1};
3510+
pid_t child = -1;
3511+
uint8_t child_result = 0;
3512+
int child_status = -1;
3513+
int acquired = -1;
3514+
int reacquired = -1;
3515+
int free_after_release = -1;
3516+
bool lock_path_ok = false;
3517+
3518+
if (ipc_test_parent_new(parent, "try-acquire-again-holds-lock")) {
3519+
endpoint = cbm_daemon_ipc_endpoint_new(key, parent);
3520+
}
3521+
if (endpoint) {
3522+
ipc_test_copy_path(runtime_dir, cbm_daemon_ipc_endpoint_runtime_dir(endpoint));
3523+
lock_path_ok = ipc_test_lifetime_lock_path(lock_path, runtime_dir, key);
3524+
acquired = cbm_daemon_ipc_lifetime_reservation_try_acquire(endpoint, &reservation);
3525+
}
3526+
if (reservation) {
3527+
/* This process already holds the lifetime lock. Calling try_acquire
3528+
* again, still in this process, must trust the in-process registry
3529+
* alone and return 0 (already held, nothing new to hand back)
3530+
* without touching the lock file: opening and closing a second fd
3531+
* on it would drop the fcntl(2) record lock this process already
3532+
* holds, exactly the way the probe's own fast path used to. */
3533+
reacquired = cbm_daemon_ipc_lifetime_reservation_try_acquire(endpoint, &second_reservation);
3534+
}
3535+
if (reservation && lock_path_ok && pipe(result_pipe) == 0) {
3536+
child = fork();
3537+
}
3538+
if (child == 0) {
3539+
/* A genuinely independent process, not going through any of this
3540+
* codebase's APIs so it cannot inherit the parent's in-process
3541+
* claim registry: try to take the real OS-level lock directly. If
3542+
* the repeated try_acquire above survived, this fails. */
3543+
(void)close(result_pipe[0]);
3544+
child_result = ipc_test_lock_child_verdict(lock_path);
3545+
bool reported = ipc_test_fd_write_all(result_pipe[1], &child_result, sizeof(child_result));
3546+
(void)close(result_pipe[1]);
3547+
_exit(reported ? 0 : 1);
3548+
}
3549+
if (child > 0) {
3550+
(void)close(result_pipe[1]);
3551+
result_pipe[1] = -1;
3552+
bool received = ipc_test_fd_read_all(result_pipe[0], &child_result, sizeof(child_result));
3553+
(void)close(result_pipe[0]);
3554+
result_pipe[0] = -1;
3555+
if (!received) {
3556+
child_result = 2;
3557+
}
3558+
while (waitpid(child, &child_status, 0) < 0 && errno == EINTR) {}
3559+
}
3560+
cbm_daemon_ipc_lifetime_reservation_release(reservation);
3561+
if (endpoint) {
3562+
free_after_release = cbm_daemon_ipc_lifetime_reservation_probe(endpoint);
3563+
}
3564+
for (size_t index = 0; index < 2; index++) {
3565+
if (result_pipe[index] >= 0) {
3566+
(void)close(result_pipe[index]);
3567+
}
3568+
}
3569+
cbm_daemon_ipc_endpoint_free(endpoint);
3570+
ipc_test_remove_tree(runtime_dir, parent);
3571+
3572+
ASSERT_EQ(acquired, 1);
3573+
ASSERT_TRUE(lock_path_ok);
3574+
ASSERT_EQ(reacquired, 0);
3575+
ASSERT_TRUE(second_reservation == NULL);
3576+
ASSERT_GT(child, 0);
3577+
ASSERT_TRUE(WIFEXITED(child_status));
3578+
ASSERT_EQ(WEXITSTATUS(child_status), 0);
3579+
ASSERT_EQ(child_result, IPC_TEST_LOCK_HELD);
34673580
ASSERT_EQ(free_after_release, 0);
34683581
PASS();
34693582
}
@@ -5628,6 +5741,7 @@ SUITE(daemon_ipc) {
56285741
RUN_TEST(daemon_ipc_posix_startup_lock_is_cross_process);
56295742
RUN_TEST(daemon_ipc_posix_lifetime_reservation_rejects_fork_inheritance);
56305743
RUN_TEST(daemon_ipc_posix_lifetime_reservation_probe_does_not_drop_held_lock);
5744+
RUN_TEST(daemon_ipc_posix_lifetime_reservation_try_acquire_again_does_not_drop_held_lock);
56315745
RUN_TEST(daemon_ipc_posix_child_participant_handoff_retains_legacy_bridge);
56325746
RUN_TEST(daemon_ipc_posix_publication_boundaries_recover_from_crash);
56335747
RUN_TEST(daemon_ipc_posix_record_publication_windows_recover_from_crash);

0 commit comments

Comments
 (0)