This is a common and fascinating security feature in macOS, particularly for binaries originating from system-protected locations like /usr/bin. The 137 exit code is 128 + 9, meaning the process was killed by SIGKILL (signal 9).
Here's why this happens:
-
Hardened Runtime, Entitlements, and Security Policy:
- Hardened Runtime: Modern macOS applications and system binaries are compiled with the Hardened Runtime. This is a security feature that applies a strict set of default entitlements, restricting what the app can do (e.g., it can't self-modify code, use JIT, or allocate executable memory without specific entitlements).
- Entitlements: Apple-signed system binaries (like those in
/usr/bin) have very specific entitlements associated with their signature. These entitlements grant them necessary permissions to perform their system-level tasks.
- Location-Based Trust: macOS's security policy ties the trust of an executable not just to its signature, but also to its location. A binary from
/usr/bin is expected to be in /usr/bin. When you copy it to a user-writable location (like your home directory, /tmp, or /Users/Shared), even though the binary's content is identical and its signature is still valid, its execution context changes.
- The Kill: The system (specifically
securityd in conjunction with the kernel) detects that a highly privileged, Apple-signed binary is being run from an unexpected, user-writable location. It interprets this as a potential security risk (e.g., an attempt to bypass security measures, or run a "trojaned" version) and proactively kills the process with SIGKILL to prevent any potential misuse or privilege escalation. It's a "you're a system binary, but you're not in a system place, so you die" policy.
-
System Integrity Protection (SIP):
- While SIP itself primarily protects system files and directories from modification (e.g., you can't delete or modify files in
/usr/bin even as root), it reinforces the overall security posture that system locations are sacred. Your act of copying the binary out of a SIP-protected directory and running it from an unprotected one triggers the security checks mentioned above.
-
Sandbox and Process Context:
- Many system binaries are designed to run within specific
launchd contexts or system-level sandboxes that apply additional restrictions or provide necessary environmental variables/resources. When you copy and run them manually from a different location, they might not receive the expected context, leading to security violations that result in a kill.
In essence: macOS sees a signed, system-critical binary trying to run from a user-owned, non-system location. Even if the signature is valid, this context switch is considered a red flag, and the system terminates it aggressively to prevent any potential security bypass or vulnerability exploitation.
What if you really need to run a copy? (Not recommended for most users!)
You generally shouldn't do this, as it bypasses critical security mechanisms. However, if you're a developer or security researcher and understand the risks, you might try:
-
Remove the signature:
cp /usr/bin/some_binary /tmp/my_copy
codesign --remove-signature /tmp/my_copy
chmod +x /tmp/my_copy
/tmp/my_copy
By removing the Apple signature, the system treats it as an unsigned, untrusted executable. It will then be subject to Gatekeeper checks (if it's the first time running it from that location), and potentially Hardened Runtime restrictions for unsigned code, but it might not be killed by the location-based security policy anymore. Be prepared for potential "developer not verified" warnings.
-
Disable security features (EXTREMELY NOT RECOMMENDED!):
You could potentially try to disable System Integrity Protection (SIP) and potentially other security features, but this drastically compromises your system's security and is only advised for very specific development or debugging scenarios, and even then, with extreme caution. Don't do this on your main machine.
For most cases, if you need to use a system binary, call it directly from its original location (e.g., /usr/bin/some_binary) or use launchctl to interact with its associated system service if it's a background process.