← Back to Pull requests

objcache: Check Seatbelt permissions before opening LMDB on Apple #63636

Open
KristofferC opened this pull request with 40 lines diff across 1 files
View on GitHub
KristofferC KristofferC commented on Oct 07, 2026 10:05 UTC
Author

On Apple platforms, LMDB uses SysV semaphores for its locks. Seatbelt can allow a new semaphore set to be created while denying initialization (semctl(SETALL)), locking, and removal. LMDB then fails before initializing its lock region, and concurrent openers can encounter MDB_INVALID: File is not an LMDB file.

Check the ipc-sysv-sem permission with sandbox_check before opening LMDB, and disable the cache with a notice when the sandbox explicitly denies access. This avoids allocating a probe semaphore that the sandbox would prevent us from removing. The private libSystem API is weakly imported and the check is best-effort: if the API is unavailable or the query fails, LMDB attempts to open the cache normally. Removing the API therefore does not disable caching for ordinary Mac users.

The companion change, JuliaCI/sandboxed-buildkite-agent#176, allows ipc-sysv-sem so the cache can operate in macOS CI. Its permission rule was validated locally; the full CI profile has not been tested here.

Fixes #62800

Implemented with assistance from Codex (GPT-6).

Diff: 40 lines (+29 / -11) across 1 files Reviewer: xal-0 Review duration: 33650m
All Vouch review confidence checks passed
Residual risk 0.04 = Change Risk (0.48) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 40 lines diff across 1 files
0.48
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.90 · 5 substantive comment(s)
0.90
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
91%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.222
Sensitive Paths sensitive_paths
0.048
Real PR Conversation Timeline
7 items
xal-0 xal-0 commented on code on Oct 07, 2026 21:47 UTC
Code Review
src/objcache.cpp:1
@@ -72,16 +75,26 @@ static FILE *getLogFile()
 
 static FILE *LogFile = getLogFile();
 
-// Linux LMDB uses robust process-shared mutexes to recover locks after an
-// owner dies. QEMU user mode returns ENOSYS for both robust-list syscalls,
-// but glibc still allows creating these mutexes, leaving dead owners' locks
-// unrecoverable. Query the current thread without changing libc's robust list.
-static bool robustMutexesAvailable() JL_NOTSAFEPOINT
+// Check that the lock primitives LMDB's robust lock backend relies on work.
+static bool robustLocksAvailable() JL_NOTSAFEPOINT
 {
-#ifdef __linux__
+#if defined(__linux__)
+    // Linux LMDB uses robust process-shared mutexes to recover locks after an
+    // owner dies. QEMU user mode returns ENOSYS for both robust-list syscalls,
+    // but glibc still allows creating these mutexes, leaving dead owners' locks
+    // unrecoverable. Query the current thread without changing libc's robust list.
     void *head = nullptr;
     size_t len = 0;
     return syscall(SYS_get_robust_list, 0, &head, &len) == 0;
+#elif defined(__APPLE__)
+    // Apple LMDB uses SysV semaphores, which sandboxes (e.g. sandbox-exec
+    // without ipc-sysv-sem) may deny. LMDB only finds out after resetting the
+    // lock file, so concurrent openers then fail with MDB_INVALID.
+    int semid = semget(IPC_PRIVATE, 2, IPC_CREAT | 0600);
+    if (semid < 0)
+        return false;
+    semctl(semid, 0, IPC_RMID);

Ugh, Fable tells me an XNU kernel bug makes this leak the semaphore set every time the check fails, and I think it's right.

KristofferC KristofferC commented on code on Oct 07, 2026 22:00 UTC
Code Review
src/objcache.cpp:1
@@ -72,16 +75,26 @@ static FILE *getLogFile()
 
 static FILE *LogFile = getLogFile();
 
-// Linux LMDB uses robust process-shared mutexes to recover locks after an
-// owner dies. QEMU user mode returns ENOSYS for both robust-list syscalls,
-// but glibc still allows creating these mutexes, leaving dead owners' locks
-// unrecoverable. Query the current thread without changing libc's robust list.
-static bool robustMutexesAvailable() JL_NOTSAFEPOINT
+// Check that the lock primitives LMDB's robust lock backend relies on work.
+static bool robustLocksAvailable() JL_NOTSAFEPOINT
 {
-#ifdef __linux__
+#if defined(__linux__)
+    // Linux LMDB uses robust process-shared mutexes to recover locks after an
+    // owner dies. QEMU user mode returns ENOSYS for both robust-list syscalls,
+    // but glibc still allows creating these mutexes, leaving dead owners' locks
+    // unrecoverable. Query the current thread without changing libc's robust list.
     void *head = nullptr;
     size_t len = 0;
     return syscall(SYS_get_robust_list, 0, &head, &len) == 0;
+#elif defined(__APPLE__)
+    // Apple LMDB uses SysV semaphores, which sandboxes (e.g. sandbox-exec
+    // without ipc-sysv-sem) may deny. LMDB only finds out after resetting the
+    // lock file, so concurrent openers then fail with MDB_INVALID.
+    int semid = semget(IPC_PRIVATE, 2, IPC_CREAT | 0600);
+    if (semid < 0)
+        return false;
+    semctl(semid, 0, IPC_RMID);

🫠

xal-0 xal-0 commented on code on Oct 07, 2026 22:35 UTC
Code Review
src/objcache.cpp:1
@@ -72,16 +75,26 @@ static FILE *getLogFile()
 
 static FILE *LogFile = getLogFile();
 
-// Linux LMDB uses robust process-shared mutexes to recover locks after an
-// owner dies. QEMU user mode returns ENOSYS for both robust-list syscalls,
-// but glibc still allows creating these mutexes, leaving dead owners' locks
-// unrecoverable. Query the current thread without changing libc's robust list.
-static bool robustMutexesAvailable() JL_NOTSAFEPOINT
+// Check that the lock primitives LMDB's robust lock backend relies on work.
+static bool robustLocksAvailable() JL_NOTSAFEPOINT
 {
-#ifdef __linux__
+#if defined(__linux__)
+    // Linux LMDB uses robust process-shared mutexes to recover locks after an
+    // owner dies. QEMU user mode returns ENOSYS for both robust-list syscalls,
+    // but glibc still allows creating these mutexes, leaving dead owners' locks
+    // unrecoverable. Query the current thread without changing libc's robust list.
     void *head = nullptr;
     size_t len = 0;
     return syscall(SYS_get_robust_list, 0, &head, &len) == 0;
+#elif defined(__APPLE__)
+    // Apple LMDB uses SysV semaphores, which sandboxes (e.g. sandbox-exec
+    // without ipc-sysv-sem) may deny. LMDB only finds out after resetting the
+    // lock file, so concurrent openers then fail with MDB_INVALID.
+    int semid = semget(IPC_PRIVATE, 2, IPC_CREAT | 0600);
+    if (semid < 0)
+        return false;
+    semctl(semid, 0, IPC_RMID);

I'm not sure this check even fixes the problem we encountered in the sandbox, because Seatbelt's (deny ipc-sysv-sem) doesn't make semaphore set creation with semget fail at all! Unclear if that is a bug

KristofferC KristofferC commented on code on Oct 08, 2026 06:34 UTC
Code Review
src/objcache.cpp:1
@@ -72,16 +75,26 @@ static FILE *getLogFile()
 
 static FILE *LogFile = getLogFile();
 
-// Linux LMDB uses robust process-shared mutexes to recover locks after an
-// owner dies. QEMU user mode returns ENOSYS for both robust-list syscalls,
-// but glibc still allows creating these mutexes, leaving dead owners' locks
-// unrecoverable. Query the current thread without changing libc's robust list.
-static bool robustMutexesAvailable() JL_NOTSAFEPOINT
+// Check that the lock primitives LMDB's robust lock backend relies on work.
+static bool robustLocksAvailable() JL_NOTSAFEPOINT
 {
-#ifdef __linux__
+#if defined(__linux__)
+    // Linux LMDB uses robust process-shared mutexes to recover locks after an
+    // owner dies. QEMU user mode returns ENOSYS for both robust-list syscalls,
+    // but glibc still allows creating these mutexes, leaving dead owners' locks
+    // unrecoverable. Query the current thread without changing libc's robust list.
     void *head = nullptr;
     size_t len = 0;
     return syscall(SYS_get_robust_list, 0, &head, &len) == 0;
+#elif defined(__APPLE__)
+    // Apple LMDB uses SysV semaphores, which sandboxes (e.g. sandbox-exec
+    // without ipc-sysv-sem) may deny. LMDB only finds out after resetting the
+    // lock file, so concurrent openers then fail with MDB_INVALID.
+    int semid = semget(IPC_PRIVATE, 2, IPC_CREAT | 0600);
+    if (semid < 0)
+        return false;
+    semctl(semid, 0, IPC_RMID);

There is apparently some private sandbox_check function that you can just query if it has the required support so I changed to that... I don't know if this is worth doing or if we should just merge https://github.com/JuliaCI/sandboxed-buildkite-agent/pull/176 and forget about it..

xal-0 xal-0 commented on code on Oct 08, 2026 17:32 UTC
Code Review
src/objcache.cpp:1
@@ -72,16 +75,26 @@ static FILE *getLogFile()
 
 static FILE *LogFile = getLogFile();
 
-// Linux LMDB uses robust process-shared mutexes to recover locks after an
-// owner dies. QEMU user mode returns ENOSYS for both robust-list syscalls,
-// but glibc still allows creating these mutexes, leaving dead owners' locks
-// unrecoverable. Query the current thread without changing libc's robust list.
-static bool robustMutexesAvailable() JL_NOTSAFEPOINT
+// Check that the lock primitives LMDB's robust lock backend relies on work.
+static bool robustLocksAvailable() JL_NOTSAFEPOINT
 {
-#ifdef __linux__
+#if defined(__linux__)
+    // Linux LMDB uses robust process-shared mutexes to recover locks after an
+    // owner dies. QEMU user mode returns ENOSYS for both robust-list syscalls,
+    // but glibc still allows creating these mutexes, leaving dead owners' locks
+    // unrecoverable. Query the current thread without changing libc's robust list.
     void *head = nullptr;
     size_t len = 0;
     return syscall(SYS_get_robust_list, 0, &head, &len) == 0;
+#elif defined(__APPLE__)
+    // Apple LMDB uses SysV semaphores, which sandboxes (e.g. sandbox-exec
+    // without ipc-sysv-sem) may deny. LMDB only finds out after resetting the
+    // lock file, so concurrent openers then fail with MDB_INVALID.
+    int semid = semget(IPC_PRIVATE, 2, IPC_CREAT | 0600);
+    if (semid < 0)
+        return false;
+    semctl(semid, 0, IPC_RMID);

We shouldn't use an undocumented internal function IMO. Best to have upstream LMDB change things so we don't leave the database in a bad state if SysV IPC is blocked. Thanks for looking in to this

vtjnash vtjnash commented on Oct 08, 2026 18:28 UTC
Reviewer

LGTM. Seems likely not the first time we've used internal APIs, especially due to working around platform bugs on Apple.

Reviewers
Assignees
KristofferC KristofferC
Labels
risk: low confidence: 91% reviewed sensitive_module
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 48.4% (MEDIUM) · Higher = larger blast radius
48.4%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 91% · Lower = rushed or shallow review
91%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 4.3% · Safe to merge
4.3%