Sandboxing without Xcode lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- felipemdc · 2015-04-03 12:58 So, I'm trying to run a trivial LCL-Cocoa app in sandboxing because it is mandatory for the App Store. Ok, so I created a cocoatest.plist file: Code: [Select]             com.apple.security.app-sandbox             And a cocoatest.sb file: Code: [Select](version 1) (deny default) (import "system.sb") (allow process-exec   (regex "/Users/felipe/Programas/test/cocoatest/cocoatest.app/Content s/MacOS/cocoatest")) (allow file-write* file-read-data file-read-metadata   (regex "/Users/felipe/Programas/test/cocoatest/cocoatest.app") ) I code-signed my app with: codesign -s - -f --entitlements cocoatest.plist cocoatest.app/ And attempted without success to run it like this: $ sandbox-exec -f cocoatest.sb cocoatest.app/Contents/MacOS/cocoatest Illegal instruction: 4 This is as far as I reached so far, and it brought up so many questions, and I'm googling like crazy to no avail =( 1> Any ideas what is this "Illegal instruction: 4" ??? The Console shows this: Code: [Select]03/04/15 14:54:09,319 secinitd[33806]: _secinitd_realpath(cocoatest.app/Contents/MacOS/cocoatest) failed; errno: 2 03/04/15 14:54:09,319 secinitd[33806]: cocoatest[37932]: unable to realpath main executable; image[0]: cocoatest.app/Contents/MacOS/cocoatest 03/04/15 14:54:09,355 com.apple.xpc.launchd[1]: (com.apple.ReportCrash[37933]) Endpoint has been activated through legacy launch(3) APIs. Please switch to XPC or bootstrap_check_in(): com.apple.ReportCrash 03/04/15 14:54:09,396 sandboxd[33892]: ([37932]) cocoatest(37932) deny file-read-data /Users/felipe/Programas/test/cocoatest 03/04/15 14:54:09,413 sandboxd[33892]: ([37932]) cocoatest(37932) deny file-read-metadata / 03/04/15 14:54:09,428 configd[25]: Copy statistics for 43CD4DA4-FC27-4A53-BA0D-AFAF5D67191A failed: Invalid argument 03/04/15 14:54:09,534 sandboxd[33892]: ([37932]) cocoatest(37932) deny forbidden-sandbox-reinit 03/04/15 14:54:09,888 ReportCrash[37933]: Saved crash report for cocoatest[37932] version ??? to /Users/felipe/Library/Logs/DiagnosticR eports/cocoatest_2015-04-03-145409_Felipes-MacBook-Pro.crash 03/04/15 14:54:10,434 configd[25]: Copy statistics for 43CD4DA4-FC27-4A53-BA0D-AFAF5D67191A failed: Invalid argument 2> Why do I need to use this stupid .sb file at all? =( The permissions are already in the plist file, I just want to run with whatever sandbox settings Mac OS X will use for a normal user opening my app, but I can't find anywhere what those .sb file settings would be =( 3> Any idea where to find example .plist files with file access permissions and stuff like that? 4> Any beginner instructions that I am missing? I looked everywhere and I am sure that some people are already using sandboxing with Lazarus since there are Lazarus apps in the app store, but there is so little information about it despite that =( No beginner step-by-step tutorials .... I'll write something in the wiki, but first I need to figure out how this mess works =D In Android sandboxing is so easy, Apple sandboxing is so unnecessarily complicated =( ---------------------------------------------------------------------- felipemdc · 2015-04-04 11:47 ok, I can answer my own questions already. Testing if the app is sandboxed seams very complicated, there is objc code here: http://oleb.net/blog/2012/02/checking-code-signing-and- sandboxing-status-in-code/ but it looked a long and boring task to convert it and test that it works correctly. So instead I created a minimal objective Pascal app with a window and a button, and I documented this example here: http://wiki.freepascal.org/FPC_PasCocoa#Button_which_handle_an_event I wrote a button handler which makes a forbitten task: It uses TStringList.SaveToFile to create a file procedure TMyDelegate.HandleButtonClick(sender: id); var   lStrings: TStringList; begin   lText.setStringValue(NSSTR('New text!'));   lStrings := TStringList.Create();   lStrings.Add('Line of text');   lStrings.SaveToFile('/Users/felipe/Programas/forbitten_file.txt');   lStrings.Free; end; I ran the app and verified that it works perfectly and creates the file. I created a larger simplewindow.plist file exactly like the one here: http://oleb.net/blog/2012/02/checking-code-signing-and-sandboxing- status-in-code/ To have more rights. Then I code signed the app with: $ mv simplewindow simplewindow.app/Contents/MacOS/simplewindow $ codesign -s - -f --entitlements simplewindow.plist simplewindow.app/ Then I ran the app with: $ open simplewindow.app/ Then I clicked the button, and the app crashed with this console message: 04/04/15 13:41:32,603 sandboxd[1675]: ([8980]) simplewindow(8980) deny file-write-create /Users/felipe/Programas/forbitten_file.txt So success! The app ran in a sandbox and upon doing a forbitten task it was closed by the sandbox. I didn't start any daemon or whatever else. ---------------------------------------------------------------------- felipemdc · 2015-04-04 12:41 Documented here: http://wiki.lazarus.freepascal.org/Code_Signing_for_M ac_OS_X#Running_an_app_inside_a_sandbox_in_Mac_OS_X ---------------------------------------------------------------------- jwdietrich · 2015-04-04 13:44 Thanks for sharing your experiences.