Writing a privileged helper tool with SMJobBless()

后端 未结 6 1225
一生所求
一生所求 2020-11-30 22:29

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()

相关标签:
6条回答
  • 2020-11-30 23:04

    XPC isn't an option if you're trying to elevate privileges (from https://developer.apple.com/library/archive/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: https://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: https://www.dropbox.com/s/5kjl8koyqzvszrl/Elevator.zip

    0 讨论(0)
  • 2020-11-30 23:09

    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.

    HTH

    0 讨论(0)
  • 2020-11-30 23:21

    I wrote a blog post on this a few months ago, which included a cleaned up version of Apple's SMJobBless sample. Might help...

    http://www.bornsleepy.com/bornsleepy/os-x-helper-applications

    0 讨论(0)
  • 2020-11-30 23:24

    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:

    • https://developer.apple.com/library/mac/samplecode/EvenBetterAuthorizationSample/Listings/Read_Me_About_EvenBetterAuthorizationSample_txt.html

    The README is some of the best (only?) documentation of SMJobBless() available.

    0 讨论(0)
  • 2020-11-30 23:26

    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:

    http://atnan.com/blog/2012/02/29/modern-privileged-helper-tools-using-smjobbless-plus-xpc/

    https://github.com/atnan/SMJobBlessXPC

    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):

    http://www.cocoabuilder.com/archive/cocoa/309298-question-about-smjobbless.html

    If you don't want to wade through the thread, here is a link to the modified SMJobBless sample project provided by Eric Gorr:

    http://ericgorr.net/cocoadev/SMJobBless.zip

    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:

    http://connect.apple.com/cgi-bin/WebObjects/MemberSite.woa/wa/getSoftware?code=y&source=x&bundleID=20645

    0 讨论(0)
  • 2020-11-30 23:26

    Itai have you looked at the SMJobBless sample code from WWDC 2010? It includes a helper tool and app to bless it.

    https://developer.apple.com/library/mac/#samplecode/SMJobBless/Listings/ReadMe_txt.html#//apple_ref/doc/uid/DTS40010071-ReadMe_txt-DontLinkElementID_3

    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.)
    • BetterAuthorizationSample
    0 讨论(0)
提交回复
热议问题