Skip to content

Commit 96e44e2

Browse files
committed
fix(daemon): preserve held lifetime lock on same-process checks
Adapt upstream merge c80a256 (DeusData#2207). Concurrent probe and acquisition still require review.
1 parent 27b731c commit 96e44e2

2 files changed

Lines changed: 241 additions & 9 deletions

File tree

‎src/daemon/ipc.c‎

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,13 @@ static int posix_named_shared_lock_try_acquire(const cbm_daemon_ipc_endpoint_t *
12611261
return posix_named_lock_try_acquire_mode(endpoint, lock_name, true, fd_out, process_entry_out);
12621262
}
12631263

1264+
/* Never open and close the lifetime lock file in a process that may already
1265+
* hold it: fcntl(2) record locks are scoped to (process, inode), so that
1266+
* close would silently release every lock this process holds on the file,
1267+
* regardless of which fd took it. This lock is fcntl rather than flock for
1268+
* exactly this reason in reverse: flock locks survive a fork, fcntl locks
1269+
* do not, and a forked child must never appear to hold its parent's
1270+
* reservation. */
12641271
static int posix_record_lock_set(int fd, short lock_type) {
12651272
struct flock record_lock = {
12661273
.l_type = lock_type,
@@ -1284,6 +1291,15 @@ static int posix_lifetime_lock_probe(const cbm_daemon_ipc_endpoint_t *endpoint,
12841291
if (process_claimed < 0) {
12851292
return -1;
12861293
}
1294+
if (process_claimed == 1) {
1295+
/* Already held by this process per the in-process registry: trust
1296+
* that alone. Opening a throwaway fd on the lock file here and then
1297+
* closing it would release the real fcntl(2) record lock, which is
1298+
* scoped to (process, inode) rather than (fd, inode): closing any
1299+
* fd on this inode drops every lock this process holds on it, even
1300+
* one held via a different, still-open fd. */
1301+
return endpoint_runtime_still_valid(endpoint) ? 1 : -1;
1302+
}
12871303

12881304
int fd = openat(endpoint->dir_fd, lock_name, O_RDWR | O_CLOEXEC | O_NOFOLLOW);
12891305
if (fd < 0) {
@@ -1295,10 +1311,6 @@ static int posix_lifetime_lock_probe(const cbm_daemon_ipc_endpoint_t *endpoint,
12951311
(void)close(fd);
12961312
return -1;
12971313
}
1298-
if (process_claimed == 1) {
1299-
bool still_private = endpoint_runtime_still_valid(endpoint);
1300-
return close(fd) == 0 && still_private ? 1 : -1;
1301-
}
13021314
struct flock record_lock = {
13031315
.l_type = F_WRLCK,
13041316
.l_whence = SEEK_SET,
@@ -1336,11 +1348,13 @@ static int posix_lifetime_lock_try_acquire(const cbm_daemon_ipc_endpoint_t *endp
13361348
process_lock_entry_t *process_entry = NULL;
13371349
int process_result = process_lock_claim(endpoint, lock_name, &process_entry);
13381350
if (process_result != 1) {
1339-
return process_result == 0 &&
1340-
private_regular_file_at_is_safe(endpoint->dir_fd, lock_name, 1) &&
1341-
endpoint_runtime_still_valid(endpoint)
1342-
? 0
1343-
: -1;
1351+
/* Already held by this process per the in-process registry: trust
1352+
* that alone, the same as the probe's early return above. Calling
1353+
* private_regular_file_at_is_safe() here would open a throwaway fd
1354+
* on the lock file and close it, releasing the real fcntl(2) record
1355+
* lock this process holds, because that lock is scoped to (process,
1356+
* inode) rather than (fd, inode). */
1357+
return process_result == 0 && endpoint_runtime_still_valid(endpoint) ? 0 : -1;
13441358
}
13451359
int fd = openat(endpoint->dir_fd, lock_name, O_RDWR | O_CREAT | O_CLOEXEC | O_NOFOLLOW, 0600);
13461360
if (fd < 0 || !fd_set_cloexec(fd)) {

‎tests/test_daemon_ipc.c‎

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3398,6 +3398,222 @@ TEST(daemon_ipc_posix_lifetime_reservation_rejects_fork_inheritance) {
33983398
PASS();
33993399
}
34003400

3401+
static bool ipc_test_lifetime_lock_path(char out[TEST_PATH_CAP], const char *runtime_dir,
3402+
const char *key) {
3403+
int written = runtime_dir && key
3404+
? snprintf(out, TEST_PATH_CAP, "%s/cbm-%s.lifetime.lock", runtime_dir, key)
3405+
: -1;
3406+
return written > 0 && written < TEST_PATH_CAP;
3407+
}
3408+
3409+
/* Child-process verdict on whether the lifetime lock at lock_path is held by
3410+
* someone else, going straight through open()/fcntl() rather than any of
3411+
* this codebase's APIs, so a genuinely separate process cannot inherit the
3412+
* parent's in-process claim registry. Distinguishes three outcomes instead
3413+
* of collapsing "held" and "could not even check" into the same value:
3414+
* IPC_TEST_LOCK_HELD only when open() succeeded and F_SETLK failed with
3415+
* EACCES or EAGAIN (the lock is held by another process, as expected);
3416+
* IPC_TEST_LOCK_TAKEN when F_SETLK succeeded (the lock was free, a bug);
3417+
* IPC_TEST_LOCK_ERROR for anything else (open() failed, e.g. a wrong path,
3418+
* or F_SETLK failed with an errno that says nothing about the lock's
3419+
* state), which must never be mistaken for IPC_TEST_LOCK_HELD. */
3420+
enum {
3421+
IPC_TEST_LOCK_HELD = 0,
3422+
IPC_TEST_LOCK_TAKEN = 1,
3423+
IPC_TEST_LOCK_ERROR = 3,
3424+
};
3425+
3426+
static uint8_t ipc_test_lock_child_verdict(const char *lock_path) {
3427+
int fd = open(lock_path, O_RDWR);
3428+
if (fd < 0) {
3429+
return IPC_TEST_LOCK_ERROR;
3430+
}
3431+
struct flock record_lock = {
3432+
.l_type = F_WRLCK,
3433+
.l_whence = SEEK_SET,
3434+
.l_start = 0,
3435+
.l_len = 0,
3436+
};
3437+
int lock_result;
3438+
do {
3439+
lock_result = fcntl(fd, F_SETLK, &record_lock);
3440+
} while (lock_result != 0 && errno == EINTR);
3441+
int lock_errno = errno;
3442+
(void)close(fd);
3443+
if (lock_result == 0) {
3444+
return IPC_TEST_LOCK_TAKEN;
3445+
}
3446+
return lock_errno == EACCES || lock_errno == EAGAIN ? IPC_TEST_LOCK_HELD : IPC_TEST_LOCK_ERROR;
3447+
}
3448+
3449+
TEST(daemon_ipc_posix_lifetime_reservation_probe_does_not_drop_held_lock) {
3450+
static const char key[] = "c1c1d2d2e3e3f4f4";
3451+
char parent[TEST_PATH_CAP] = {0};
3452+
char runtime_dir[TEST_PATH_CAP] = {0};
3453+
char lock_path[TEST_PATH_CAP] = {0};
3454+
cbm_daemon_ipc_endpoint_t *endpoint = NULL;
3455+
cbm_daemon_ipc_lifetime_reservation_t *reservation = NULL;
3456+
int result_pipe[2] = {-1, -1};
3457+
pid_t child = -1;
3458+
uint8_t child_result = 0;
3459+
int child_status = -1;
3460+
int acquired = -1;
3461+
int held_after_first_probe = -1;
3462+
int held_after_second_probe = -1;
3463+
int free_after_release = -1;
3464+
bool lock_path_ok = false;
3465+
3466+
if (ipc_test_parent_new(parent, "probe-holds-lock")) {
3467+
endpoint = cbm_daemon_ipc_endpoint_new(key, parent);
3468+
}
3469+
if (endpoint) {
3470+
ipc_test_copy_path(runtime_dir, cbm_daemon_ipc_endpoint_runtime_dir(endpoint));
3471+
lock_path_ok = ipc_test_lifetime_lock_path(lock_path, runtime_dir, key);
3472+
acquired = cbm_daemon_ipc_lifetime_reservation_try_acquire(endpoint, &reservation);
3473+
}
3474+
if (reservation) {
3475+
/* Two probes, matching how a real daemon polls its own reservation
3476+
* repeatedly while running. Each probe of an already-held lock used
3477+
* to open a throwaway fd on the lock file and close it. Since
3478+
* fcntl(2) record locks are scoped to (process, inode), that close
3479+
* silently released the real lock every time, regardless of the
3480+
* still-open fd the reservation itself is holding. */
3481+
held_after_first_probe = cbm_daemon_ipc_lifetime_reservation_probe(endpoint);
3482+
held_after_second_probe = cbm_daemon_ipc_lifetime_reservation_probe(endpoint);
3483+
}
3484+
if (reservation && lock_path_ok && pipe(result_pipe) == 0) {
3485+
child = fork();
3486+
}
3487+
if (child == 0) {
3488+
/* A genuinely independent process, not going through any of this
3489+
* codebase's APIs so it cannot inherit the parent's in-process
3490+
* claim registry: try to take the real OS-level lock directly. If
3491+
* the parent's fcntl lock survived the probes above, this fails. */
3492+
(void)close(result_pipe[0]);
3493+
child_result = ipc_test_lock_child_verdict(lock_path);
3494+
bool reported = ipc_test_fd_write_all(result_pipe[1], &child_result, sizeof(child_result));
3495+
(void)close(result_pipe[1]);
3496+
_exit(reported ? 0 : 1);
3497+
}
3498+
if (child > 0) {
3499+
(void)close(result_pipe[1]);
3500+
result_pipe[1] = -1;
3501+
bool received = ipc_test_fd_read_all(result_pipe[0], &child_result, sizeof(child_result));
3502+
(void)close(result_pipe[0]);
3503+
result_pipe[0] = -1;
3504+
if (!received) {
3505+
child_result = 2;
3506+
}
3507+
while (waitpid(child, &child_status, 0) < 0 && errno == EINTR) {}
3508+
}
3509+
cbm_daemon_ipc_lifetime_reservation_release(reservation);
3510+
if (endpoint) {
3511+
free_after_release = cbm_daemon_ipc_lifetime_reservation_probe(endpoint);
3512+
}
3513+
for (size_t index = 0; index < 2; index++) {
3514+
if (result_pipe[index] >= 0) {
3515+
(void)close(result_pipe[index]);
3516+
}
3517+
}
3518+
cbm_daemon_ipc_endpoint_free(endpoint);
3519+
ipc_test_remove_tree(runtime_dir, parent);
3520+
3521+
ASSERT_EQ(acquired, 1);
3522+
ASSERT_TRUE(lock_path_ok);
3523+
ASSERT_EQ(held_after_first_probe, 1);
3524+
ASSERT_EQ(held_after_second_probe, 1);
3525+
ASSERT_GT(child, 0);
3526+
ASSERT_TRUE(WIFEXITED(child_status));
3527+
ASSERT_EQ(WEXITSTATUS(child_status), 0);
3528+
ASSERT_EQ(child_result, IPC_TEST_LOCK_HELD);
3529+
ASSERT_EQ(free_after_release, 0);
3530+
PASS();
3531+
}
3532+
3533+
TEST(daemon_ipc_posix_lifetime_reservation_try_acquire_again_does_not_drop_held_lock) {
3534+
static const char key[] = "a1a1b2b2c3c3d4d4";
3535+
char parent[TEST_PATH_CAP] = {0};
3536+
char runtime_dir[TEST_PATH_CAP] = {0};
3537+
char lock_path[TEST_PATH_CAP] = {0};
3538+
cbm_daemon_ipc_endpoint_t *endpoint = NULL;
3539+
cbm_daemon_ipc_lifetime_reservation_t *reservation = NULL;
3540+
cbm_daemon_ipc_lifetime_reservation_t *second_reservation =
3541+
(cbm_daemon_ipc_lifetime_reservation_t *)(uintptr_t)1;
3542+
int result_pipe[2] = {-1, -1};
3543+
pid_t child = -1;
3544+
uint8_t child_result = 0;
3545+
int child_status = -1;
3546+
int acquired = -1;
3547+
int reacquired = -1;
3548+
int free_after_release = -1;
3549+
bool lock_path_ok = false;
3550+
3551+
if (ipc_test_parent_new(parent, "try-acquire-again-holds-lock")) {
3552+
endpoint = cbm_daemon_ipc_endpoint_new(key, parent);
3553+
}
3554+
if (endpoint) {
3555+
ipc_test_copy_path(runtime_dir, cbm_daemon_ipc_endpoint_runtime_dir(endpoint));
3556+
lock_path_ok = ipc_test_lifetime_lock_path(lock_path, runtime_dir, key);
3557+
acquired = cbm_daemon_ipc_lifetime_reservation_try_acquire(endpoint, &reservation);
3558+
}
3559+
if (reservation) {
3560+
/* This process already holds the lifetime lock. Calling try_acquire
3561+
* again, still in this process, must trust the in-process registry
3562+
* alone and return 0 (already held, nothing new to hand back)
3563+
* without touching the lock file: opening and closing a second fd
3564+
* on it would drop the fcntl(2) record lock this process already
3565+
* holds, exactly the way the probe's own fast path used to. */
3566+
reacquired = cbm_daemon_ipc_lifetime_reservation_try_acquire(endpoint, &second_reservation);
3567+
}
3568+
if (reservation && lock_path_ok && pipe(result_pipe) == 0) {
3569+
child = fork();
3570+
}
3571+
if (child == 0) {
3572+
/* A genuinely independent process, not going through any of this
3573+
* codebase's APIs so it cannot inherit the parent's in-process
3574+
* claim registry: try to take the real OS-level lock directly. If
3575+
* the repeated try_acquire above survived, this fails. */
3576+
(void)close(result_pipe[0]);
3577+
child_result = ipc_test_lock_child_verdict(lock_path);
3578+
bool reported = ipc_test_fd_write_all(result_pipe[1], &child_result, sizeof(child_result));
3579+
(void)close(result_pipe[1]);
3580+
_exit(reported ? 0 : 1);
3581+
}
3582+
if (child > 0) {
3583+
(void)close(result_pipe[1]);
3584+
result_pipe[1] = -1;
3585+
bool received = ipc_test_fd_read_all(result_pipe[0], &child_result, sizeof(child_result));
3586+
(void)close(result_pipe[0]);
3587+
result_pipe[0] = -1;
3588+
if (!received) {
3589+
child_result = 2;
3590+
}
3591+
while (waitpid(child, &child_status, 0) < 0 && errno == EINTR) {}
3592+
}
3593+
cbm_daemon_ipc_lifetime_reservation_release(reservation);
3594+
if (endpoint) {
3595+
free_after_release = cbm_daemon_ipc_lifetime_reservation_probe(endpoint);
3596+
}
3597+
for (size_t index = 0; index < 2; index++) {
3598+
if (result_pipe[index] >= 0) {
3599+
(void)close(result_pipe[index]);
3600+
}
3601+
}
3602+
cbm_daemon_ipc_endpoint_free(endpoint);
3603+
ipc_test_remove_tree(runtime_dir, parent);
3604+
3605+
ASSERT_EQ(acquired, 1);
3606+
ASSERT_TRUE(lock_path_ok);
3607+
ASSERT_EQ(reacquired, 0);
3608+
ASSERT_TRUE(second_reservation == NULL);
3609+
ASSERT_GT(child, 0);
3610+
ASSERT_TRUE(WIFEXITED(child_status));
3611+
ASSERT_EQ(WEXITSTATUS(child_status), 0);
3612+
ASSERT_EQ(child_result, IPC_TEST_LOCK_HELD);
3613+
ASSERT_EQ(free_after_release, 0);
3614+
PASS();
3615+
}
3616+
34013617
TEST(daemon_ipc_posix_child_participant_handoff_retains_legacy_bridge) {
34023618
#ifdef _WIN32
34033619
PASS();
@@ -5824,6 +6040,8 @@ SUITE(daemon_ipc) {
58246040
#endif
58256041
RUN_TEST(daemon_ipc_posix_startup_lock_is_cross_process);
58266042
RUN_TEST(daemon_ipc_posix_lifetime_reservation_rejects_fork_inheritance);
6043+
RUN_TEST(daemon_ipc_posix_lifetime_reservation_probe_does_not_drop_held_lock);
6044+
RUN_TEST(daemon_ipc_posix_lifetime_reservation_try_acquire_again_does_not_drop_held_lock);
58276045
RUN_TEST(daemon_ipc_posix_child_participant_handoff_retains_legacy_bridge);
58286046
RUN_TEST(daemon_ipc_posix_publication_boundaries_recover_from_crash);
58296047
RUN_TEST(daemon_ipc_posix_record_publication_windows_recover_from_crash);

0 commit comments

Comments
 (0)