Skip to content

fix: add advisory file locking (flock) to prevent TOCTOU race in Installer::install() (closes #77)#157

Merged
s2x merged 1 commit into
mainfrom
fix/issue-77-toctou-flock-installer
Jul 7, 2026
Merged

fix: add advisory file locking (flock) to prevent TOCTOU race in Installer::install() (closes #77)#157
s2x merged 1 commit into
mainfrom
fix/issue-77-toctou-flock-installer

Conversation

@s2x

@s2x s2x commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Description

Closes #77

Adds advisory file locking (flock()) to Installer::install() to serialize concurrent installations of the FFI shared library, preventing a TOCTOU race condition between the file-exists check and the download+extract window.

Changes

  • src/Installer.php: Acquire LOCK_EX on lib/install.lock before the file_exists check. Double-check inside the lock (re-verify library isn't already installed). Lock file persists as sentinel (never deleted) to ensure all processes share the same inode for flock() serialization. Lock released in finally — crash-safe.
  • tests/test_installer_flock.phpt: 4 test scenarios — basic flock, concurrent serialization (3 child processes), source code pattern verification, and behavioral test calling Installer::install() with a dummy library.

Key Design Decisions

  1. Lock file persists — never deleted (unlike previous temp-file cleanup patterns). Deleting the lock file would let a new process create a different inode and bypass flock() serialization entirely.
  2. Lock-check pattern (not check-lock-check) — the pre-lock check is omitted for simplicity. In practice, install() is called at most once per Composer invoke, so the lock acquisition overhead is negligible.
  3. Advisory locking — sufficient since all callers go through Installer::install().

Testing

  • All installer tests pass (7/7)
  • Behavioral test with actual Installer::install() call
  • Concurrent serialization test with 3 child processes

Code Review

  • Passed subagent code review
  • All review comments addressed (lock file deletion bug fixed, PHPDoc clarified, test SKIPIF fixed, test behavior improved)

…aller::install() (closes #77)

Add LOCK_EX acquisition on lib/install.lock before the file-exists
check to serialize concurrent installations of the FFI shared library.

Key changes:
- Acquire exclusive flock before checking if library exists (lock-check pattern)
- Double-check inside the lock: re-verify library isn't already installed
- Lock file persists as sentinel (never deleted) — ensures all concurrent
  processes share the same inode for flock() serialization
- Lock released in finally block — crash-safe (kernel auto-releases flock)
- Add PHPDoc documenting the lock pattern and rationale
- Add test_installer_flock.phpt with 4 test scenarios:
  1. Basic lock file create/acquire/release/cleanup
  2. Concurrent serialization with 3 child processes (no overlap)
  3. Source code verification of flock/double-check patterns
  4. Behavioral test: calls Installer::install() with dummy library
     to verify double-check pattern avoids re-download
@s2x s2x self-assigned this Jul 7, 2026
@s2x
s2x merged commit f75f65b into main Jul 7, 2026
3 checks passed
@s2x
s2x deleted the fix/issue-77-toctou-flock-installer branch July 7, 2026 07:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SEC-009: TOCTOU Race Condition in Library Installation

1 participant