Setting DYLD variables for a stand-alone application at runtime

I have a Cocoa application that uses several system and third-party frameworks, some of which disable XPC services. I would like to insert my own library into the process space of these XPC services so that I can return information to the main application. Sound data that is reproduced as a specific example, by intermediate or mechanical interception.

TL DR: How to set the DYLD program environment variables for my software application with software when the program starts?

Longer version ...

I can easily insert the library by setting several shell variables before running the application (unlike what this question suggests ):

$ export __XPC_DYLD_FORCE_FLAT_NAMESPACE=1 
$ export __XPC_DYLD_INSERT_LIBRARIES=`pwd`/My.app/Contents/Frameworks/XpcMonitor.dylb
$ open My.app

I thought that at that moment I was free to go home, and I just needed to set these variables when starting the application:

int main(int argc, const char * argv[])
{
     NSString* libPath = [[[NSBundle mainBundle] privateFrameworksPath] stringByAppendingPathComponent:@"XpcMonitor.dylib"];
     setenv("__XPC_FORCE_FLAT_NAMESPACE", "1", 1);
     setenv("__XPC_DYLD_INSERT_LIBRARIES", [libPath UTF8String], 1);
     return NSApplicationMain(argc, argv);
}

But it had no effect. It seems strange, because, as far as I can tell, the XPC processes have not yet been deployed. Obviously, the starting services have already decided how it will handle the subprocesses before it gets to my main (). To be sure that this does not happen in the library constructor, I moved all my application logic to dylib and dynamically loaded it after setting the variables; the same results.

I tried adding them to the LSEnvironment section of my Info.plist.

 <key>LSEnvironment</key>
 <dict>
      <key>__XPC_DYLD_FORCE_FLAT_NAMESPACE</key>
      <string>1</string>
      <key>__XPC_DYLD_INSERT_LIBRARIES</key>
      <string>/Applications/My.app/Contents/Frameworks/XpcMonitor.dylib</string>
 </dict>

, . , , /, " ".

"@executable_path/../Frameworks/XpcMonitor.dylib". , (.. DYLD_INSERT_LIBRARIES), XPC, -, . "@rpath/XpcMonitor.dylib" "@executable_path/../Frameworks", . - , .

, :

 [NSTask launchedTaskWithLaunchPath:[[NSBundle mainBundle] executablePath] 
                          arguments:[NSArray array]];

, : deny forbidden-sandbox-reinit.

[NSWorkspace launchApplicationAtURL: options: configuration: error], NSWorkspaceLaunchNewInstance NSWorkspaceLaunchAsync . , .

, [NSTask launchTaskWithLaunchPath: arguments], ( Framework, MacOS, Helpers, PlugIns) Info.plist. , , :

 5/30/14 9:55:04.000 AM kernel[0]: Sandbox: appleeventsd(57) deny file-read-metadata /Library

:

 5/30/14 9:55:04.000 AM kernel[0]: Sandbox: appleeventsd(57) deny mach-lookup com.apple.ocspd

, , :

 appleeventsd[57]: <rdar://problem/11489077> A sandboxed application with pid 2119, "MyAppHelper" checked in with appleeventsd, but its code signature could not be validated ( either because it was corrupt, or could not be read by appleeventsd ) and so it cannot receive AppleEvents targeted by name, bundle id, or signature. Error=ERROR: #-67061  { "NSDescription"="SecCodeCheckValidity() returned -67061, <SecCode 0x7fa30bc0bd20 [0x7fff7b46ff00]>." }  (handleMessage()/appleEventsD.cp #2072) client-reqs-q

... Sparkle Sparkle. Spctl :

 $ spctl --verbose=4 --assess --type execute ~/Desktop/MyApp.app/
 /Users/Jason/Desktop/MyApp.app/: accepted
 source=Developer ID

- , , , , ( ) , . Google , , , , .

(, XPC , (- ..) , XPC).

... , , , . , : DYLD , , ? ( , ?)

+4
1

Ok. :

, DYLD : DYLD , , ( BySetGUid limitedBySegment). , , , , , , .

-, vars __XPC_ * , libxpc.dylib _xpc_collect_environment: , 10.10.3, :

__xpc_collect_environment:
    0000000000004970        pushq   %rbp
   ....
    00000000000049f7        leaq    0x1daa1(%rip), %rsi     ## literal pool for: "__XPC_"
00000000000049fe            callq   __xpc_has_prefix

XPC ( ) xpc. , __XPC_ , DYLD_ *.. - dyld , ( , ), , ( DYLD vars).

DYLD , . DYLD Mach-O. . , 0- ( DYLD_INSERT_LIBRARIES, , r00t ).

+2

Source: https://habr.com/ru/post/1542662/


All Articles