Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

fix(plugin): publish new patch files on musl without hard links (#2530)

Michael D'Angeloff9f8dda4d92
diff --git a/plugins/codex-security/scripts/posix_scan_local_files.py b/plugins/codex-security/scripts/posix_scan_local_files.py
index bc75e38ab..a4669ac6e 100644
--- a/plugins/codex-security/scripts/posix_scan_local_files.py
+++ b/plugins/codex-security/scripts/posix_scan_local_files.py
@@ -84,6 +84,28 @@ def _rename_exclusive(parent_fd: int, source: str, destination: str) -> None:
if rename(parent_fd, os.fsencode(source), parent_fd, os.fsencode(destination), flag) == 0:
return
code = ctypes.get_errno()
+ elif sys.platform == "linux":
+ import sysconfig
+
+ # musl omits the renameat2 wrapper. Select the syscall for the Python
+ # process's ABI, which can differ from the host kernel's architecture.
+ arch = (sysconfig.get_config_var("MULTIARCH") or "").partition("-")[0]
+ number = {"x86_64": 316, "aarch64": 276}.get(arch)
+ syscall = getattr(libc, "syscall", None)
+ if number is not None and ctypes.sizeof(ctypes.c_void_p) == 8 and syscall is not None:
+ syscall.argtypes = [ctypes.c_long]
+ syscall.restype = ctypes.c_long
+ result = syscall(
+ number,
+ ctypes.c_long(parent_fd),
+ ctypes.c_char_p(os.fsencode(source)),
+ ctypes.c_long(parent_fd),
+ ctypes.c_char_p(os.fsencode(destination)),
+ ctypes.c_long(flag),
+ )
+ if result == 0:
+ return
+ code = ctypes.get_errno()
detail = os.strerror(code)
if code in {errno.ENOSYS, errno.ENOTSUP, errno.EOPNOTSUPP, errno.EINVAL}:
detail = f"cannot publish a new file atomically without hard links or exclusive rename ({detail})"
diff --git a/plugins/codex-security/tests/test_linux_scan_local_files.py b/plugins/codex-security/tests/test_linux_scan_local_files.py
new file mode 100644
index 000000000..bb8f6b6a3
--- /dev/null
+++ b/plugins/codex-security/tests/test_linux_scan_local_files.py
@@ -0,0 +1,37 @@
+"""Exercise the Linux syscall fallback for exclusive scan-local file publication."""
+
+import ctypes
+import os
+import sys
+import sysconfig
+from types import SimpleNamespace
+
+import pytest
+from workbench_test_support import load_script
+
+
+ sys.platform != "linux"
+ or (sysconfig.get_config_var("MULTIARCH") or "").partition("-")[0] not in {"x86_64", "aarch64"}
+ or ctypes.sizeof(ctypes.c_void_p) != 8,
+ reason="requires a supported 64-bit Linux syscall ABI",
+)
+def test_exclusive_rename_without_a_libc_wrapper(tmp_path, monkeypatch):
+ writer = load_script("posix_scan_local_files")
+ libc = SimpleNamespace(syscall=ctypes.CDLL(None, use_errno=True).syscall)
+ monkeypatch.setattr(ctypes, "CDLL", lambda *args, **kwargs: libc)
+ staged = tmp_path / "staged"
+ target = tmp_path / "résumé.patch"
+ staged.write_bytes(b"first")
+ original = staged.stat()
+ parent_fd = os.open(tmp_path, os.O_RDONLY | os.O_DIRECTORY)
+ try:
+ writer._rename_exclusive(parent_fd, staged.name, target.name)
+ assert os.path.samestat(target.stat(), original)
+ staged.write_bytes(b"second")
+ with pytest.raises(FileExistsError):
+ writer._rename_exclusive(parent_fd, staged.name, target.name)
+ finally:
+ os.close(parent_fd)
+ assert target.read_bytes() == b"first"
+ assert staged.read_bytes() == b"second"