TL;DR How to make this question - https://developer.apple.com/forums/thread/72881's answer work within the sandbox? Is there a good way to make calling SecCodeCopyGuestWithAttributes possible from within a sandbox?
We are developing a macOS system extension (more precisely a DNSProxy Network Extension) that is providing an NSXPCListener which is communicating with its host app via XPC, just as in the SimpleFirewall - https://developer.apple.com/videos/play/wwdc2019/714 code example from WWDC 2019.
Both the host app and the system extension are sandboxed and share an app group, and this is a requirement as we want to be accepted into the mac app store.
As we believe it is good practice to keep potential attack surfaces small, we would like to make sure that only our host app can connect to the system extension via XPC.
There are numerous posts here on what exactly to do, most helpful is this one - https://developer.apple.com/forums/thread/72881 which inspired this question's title. The crucial step for this question is to get the connecting process' code object through a call to SecCodeCopyGuestWithAttributes by process id, which is provided in NSXPCListenerDelegate.listener(_, shouldAcceptNewConnection). From within the sandbox this returns an OSStatus of 100001, which is some general "Operation not permitted" and in the console I can see an error logged by kernel where the sandbox denies read access to the .app file of the connecting app:
error 14:12:13.946693+0200 kernel Sandbox: bundleidentifier(33545) deny(1) file-read-data /Users/name/Library/Developer/Xcode/DerivedData/projectworkspace/Build/Products/Debug/appname.app
If I disable the sandbox, or add a temporary exception entitlement to allow read access to the Xcode/DerivedData folder then I can retrieve the code object, so I conclude that indeed the sandbox blocking the read of the file is the problem.
The obvious fix to me seems to be to add a com.apple.security.temporary-exception.files.absolute-path.read-only entitlement for /Applications/appname.app/, but this a) might be frowned upon in the app review and b) seems somewhat brittle. For example, it would prevent the app from working from inside the user's application folder, and needs a separate entitlement to work from the Xcode debugger.
Since making sure that your inter process communication is only open to a few trusted apps seems like a fairly standard precaution, I would like to ask whether there is a better option, or a recommended way to secure your XPC against unwanted connections from within a sandbox?
P.S. 1: This fairly recent question - https://developer.apple.com/forums/thread/671488 is very similar, however it remains without a satisfying conclusion and I hope to provide a better solution for the benefit of future investigators already inside this question.
P.S. 2: The fact that NSXPCListener is running inside a system extension rather than any other "normal" app in the same app group is likely not relevant, just note that the system extension is running as root while the host app is running as the normal user. This might for example make this bug - https://developer.apple.com/forums/thread/127779?answerId=423311022#423311022 (r. 63976204) relevant as we are targeting Catalina (macOS 10.15), but we are already using the entitlement necessary for a workaround for unrelated reasons.
P.S. 3: For the purposes of this question I am ignoring issues with using the process identifier, like the ones linked here - https://developer.apple.com/forums/thread/72881?answerId=382674022#382674022