Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 902
Itai Ferber Avatar asked Feb 03 '12 20:02

Itai Ferber


People also ask

Where is privileged helper tools Mac?

Configuration files for privileged helper tools are placed in the /Library/LaunchDaemons folder on your startup disk. CCC never touches this folder directly, rather it uses the macOS "Service Management" service to install and load its helper tool configuration.

What is privileged helper tools Mac?

Yep, the exact purpose of PrivilegedHelperTools is for apps to be able to run code as root in a more secure way. Here's a technical overview of how it is used in practice.

How do I remove helper tools from my Mac?

The helper is likely located either in LaunchAgents or LaunchDaemons. These folders are found in the /Library/ or /Home/Library/ folders. Check in them for an item with "Corel" in the filename. Select the file, CTRL- or RIGHT-click and select "Move to Trash" from the context menu.

How do I uninstall helper?

On the computer where AD Helper is installed, select Windows Control Panel > Programs and Features. Find the WatchGuard Active Directory Helper application. Right-click the application and select Uninstall. AD Helper is uninstalled.


1 Answers

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

like image 84
xdissent Avatar answered Oct 11 '22 21:10

xdissent