I'm working on a macOS app that I'd like to sandbox along with a login item that I'd also like to sandbox. Login items implicitly have an XPC Mach service created for them which my app is able to successfully use communicate with the sandboxed login item (because they're in the same application group).
The issue is that any non-sandboxed process can also connect to my login item's XPC Mach service, and I'd really rather that wasn't the case. I realize there's no privilege escalation going on, but this feels unnecessarily insecure. My attempts to secure the connection keep failing due to sandboxing. Is there a way to do what I'm attempting or is Apple's intention that any non-sandboxed process on the system ought to be able to successfully communicate with my login item?
If I don't sandbox my login item it's trivial for me to secure this connection.
Here's what I've tried so far:
Path based
- Retrieve the
SecCodeusingSecCodeCreateWithXPCMessage - Retrieve the
SecStaticCodeusingSecCodeCopyStaticCode - Retrieve the path of the static code using
SecCodeCopyPath - Compare this path with my login item's path based on
Bundle.main.bundleURL
This fails on step 2, the SecCodeCopyStaticCode function gets back a "UNIX error exception: 1". This kind of makes sense to me as it needs to read from the file system in order to get the static code of the running process.
Code requirements based
- Retrieve the
SecCodeusingSecCodeCreateWithXPCMessage - Construct a
SecRequirementincluding amongst other things thatcertificate leaf[subject.OU] = <my team id> - Use
SecCodeCheckValidityon the code instance from step #1 and the requirement from step #2
This fails on step 3, SecCodeCheckValidity also results in a "UNIX error exception: 1". Looking at the logs generated by sandboxd it looks like under the hood that function calls _CFBundleGetBundleVersionForURL and fails. The violation is:
deny(1) file-read-data ~/Library/Developer/Xcode/DerivedData/LoginItemSample-ajfwjiwmyuphdbeyugmssxszdlyq/Build/Products/Debug/LoginItemSample.app
Is there perhaps some combination of SecCSFlags values I can pass to prevent any file system access?