Writing a privileged helper tool with SMJobBless()
Even though the API has been open since Mac OS X Leopard, there’s surprisingly, and unfortunately, very little documentation on how to correctly use
SMJobBless() for creating privileged helper tools. There are a lot of gotchas, even when copying code directly from Apple’s sample project. Luckily, I’ve found my way around this, and have gotten the basis for my helper tool working.
However, it would seem that
SMJobBless() only blesses the tool and copies it over, but doesn’t run it. I’ve included code in my helper tool’s
main() function that should run, but doesn’t (since
NSLog() inexplicably doesn’t work–according to the tiny bit of information I have found–I’ve tried
syslog()ing some “Hello world” type strings, but nothing appears on the system console). There’s no indication that the helper tool is launched at all.
The documentation is mostly useless. It simply says that after
SMJobBless() is called, the helper tool is ‘ready’, with no indication of what ‘ready’ even means.
Furthermore, Apple’s sample doesn’t include any interprocess communication code, and doesn’t explain how one is supposed to interact with the helper tool. Do you use Distributed Objects? Mach ports? Who knows? There’s no official word on how to do it.
So, does anyone have any information on how to get this done? I’ve confirmed that the helper tool is installed, and authentication works, but I simply can’t figure out how to launch the helper tool and communicate with it – there’s simply such a gap in the documentation that this is a mystery for now. It’s very frustrating; I can’t be the only one with this problem (but there’s little mention of it anywhere), and
SMJobBless() obviously works somehow, since it’s what Apple uses.
(Please don’t mention
AuthorizationExecuteWithPrivileges(). I’m not using it: it’s deprecated, sure to go away, and is a major security hole. No thanks.)
- Xcode not building app with changes incorporated
- App Sandbox: document-scoped bookmark not resolving; not returning any error
- NSTextField not calling delegate when inside an NSTableCellView
- Error with inline and Xcode 4.2.1
- Mac OS X Lion 10.7.2 Xcode 4.2 memory issues
- Osx Lion: Xcode 4.1 how do I setup a c++0x project
6 Solutions Collect From Internet About “Writing a privileged helper tool with SMJobBless()”
XPC isn’t an option if you’re trying to elevate privileges (from https://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingXPCServices.html):
By default, XPC services are run in the most restricted environment
possible—sandboxed with minimal filesystem access, network access, and
so on. Elevating a service’s privileges to root is not supported.
SMJobBless will install a helper tool and register it with Launchd, as in the SMJobBless example provided by Apple. The trick to getting your helper tool to actually launch is to simply attempt to connect to your helper tool’s advertised services.
There was a WWDC2010 example called
ssd that demonstrated a simple launchd client/server model via sockets. It’s not available from Apple any longer, but I’ve found a link here: http://lists.apple.com/archives/macnetworkprog/2011/Jul/msg00005.html
I’ve incorporated the dispatch queue handling in the server code from the ssd example into the helper tool in the SMJobBless example and can confirm that my helper tool is indeed running (as root) when my main app attempts a connection on the appropriate port. See the WWDC2010 video on Launchd to understand the other mechanisms with which you can communicate with your helper tool (other than sockets).
I’m not sure I can legally redistribute the modified sources I have, but it should be fairly straightforward to merge the two projects and get your helper tool running.
Edit: Here is an example project I wrote that uses a distributed object for communication between the app and helper: http://dl.dropbox.com/u/463624/Elevator.zip
In fact @KurtRevis’s comment is right, you can use XPC APIs without using XPC services, and it is ideally suited to the job since.
Nathan de Vries has an excellent writeup of using XPC APIs with SMJobBless and has even modified the SMJobBless sample app to use mach XPC to both activate the job and for bidirectional communications:
Somewhat related to all this is avoiding unnecessary admin password prompts. See the following email list thread for ideas on how to check if the bundle version and code signature of an already installed helper match (which also allows you to remove a higher versioned helper in the case of a user downgrading):
If you don’t want to wade through the thread, here is a link to the modified SMJobBless sample project provided by Eric Gorr:
Also note that the ssd example mentioned in other answers here is still available online from Apple as part of the WWDC 2010 download bundle:
I feel your pain and am in the same boat. I’m in charge of the Mac version of an app that needs to perform various system configuration tasks. Of course some of these task need to be done with administrative rights. I started by using the sample code from BetterAuthorizationSample. It was a major pain to implement but it seemed to work. But then ran into cases where it would crash on some systems. I didn’t understand everything that the BAS code did and my own lack of coding experience probably contributed to the problems. So I had to remove these privileged functions from my app.
Apple doesn’t seem to care about the lack of documentation. See this message from the creator of the ServiceManagement framework. From his comments, I assume that XPC is the “intuitive replacement” he is referring to, but since it is only available on Lion, you’ll still have to find another solution for Snow Leopard or earlier clients. It’s also not clear to me if XPC can be used for privileged helpers (system level tasks that require admin or root access) or is just intended for privilege separation within your own app to make it more secure.
The BAS documentation is in desperate need of an update, but it also doesn’t appear to be a top priority.
Now I’m attempting to rewrite my app from the ground up. Professional Cocoa Application Security by Graham Lee gives some insight on how to do use privileged helpers with SMJobBless, but doesn’t go into much detail about on-demand access to launchd jobs.
So here’s what I’ve been able to find:
If you want to launch your privileged helper on demand, you’ll have to use an IPC socket. You should add a Sockets entry to your helper’s launchd.plist. After you install the app with SMJobBless, the helper will need to “check-in” with launchd (via LAUNCH_KEY_CHECKIN) to get the socket file descriptors.
Sadly, the only mentions of LAUNCH_KEY_CHECKIN seem to be in the SampleD and BAS sample code.
I don’t have any experience with sockets, so that’s my roadblock at the moment. I’d like to use the highest level API I can, so I’m trying to find out if I can use any Objective-C classes for this (like NSStream).
You might find the launchd developers mailing list helpful. Another XPC option I just found out about is XPCKit. It’s worth a look.
Apple now (2015) has an “EvenBetterAuthorizationSample” that demonstrates installing a privileged helper tool and using the
NSXPCConnection API to communicate between the app and the helper tool:
The README is some of the best (only?) documentation of
Itai have you looked at the SMJobBless sample code from WWDC 2010? It includes a helper tool and app to bless it.
Its README file says:
This sample as it stands does not actually run the helper tool. The following samples show how to [sic] a launchd job and set up interprocess communication:
- ssd (Doesn’t seem to be online anymore. Was part of the WWDC 2010 sample code.)
- Why only my last local notification function is getting called?
- How do I use C++ STL containers in My iPhone App?
- Core Data: Abstract Entity in Fetch Request
- Select branch to build in Jenkins
- Storyboard in IOS 5 SDK but also target IOS 4 /3?
- How to navigate from one view controller to another view controller on button click?
- How do I atomically increment a variable in Swift?
- Using enum in Objective-C?
- How to go about adding a link/reference to another method in documentation Xcode?
- Optional dynamic properties in Swift
- Getting the values of data stored in firebase
- Cannot access property on Swift type from Objective-C
- AVPlayerViewController doesn't play local videos
- Create an NSManagedObject Without Saving?
- How to disable caching from NSURLSessionTask