SwiftAuthorizationSample demonstrates how to run privileged operations using a helper tool installed with
SMJobBless and communicate with it using XPC.
(Note: If your minimum deployment target is macOS 13 or later, Apple recommends you use
SMAppService instead to register a
Launch Daemon.)
This sample was created with the expectation that you already have an app and are looking to add a privileged helper
tool in order to perform one or more operations as root. As such this sample is not a template and is instead
written in a modular way which should make it easy for you to add portions of this code to your project as desired.
To try out this sample, configure your Development signing certificate for both the app and helper tool, then you
should be good to go. Sign to Run Locally is not supported as this would result in code signing requirements which
cannot securely identify the app and helper tool.
Read the following sections to learn how you can incorporate portions of this sample into your own project. The source
code of the sample also contains comments throughout.
If you run into issues with this sample it may be because of Xcode compatibility issues; this sample was created with
Xcode 13.
Sample Overview
When you run the sample you should see an app which looks like this (shown in dark mode):

Initially the helper tool won't be installed, so start by trying out that functionality.
Once the helper tool is installed you can run any of the privileged commands. These are a pretty arbitrary set of
macOS Command Line Tools and some of the arguments you can pass to them. The only commonality between them is
they require root access to run (and are non-destructive, since it is a sample after all).
Two of the commands say "[Auth Required]" and when run will require you to provide an admin password. This is done to
demonstrate how, if you so desire, you can self-restrict access to portions of your helper tool.
macOS Support
This sample targets macOS 10.14.4 and above. If you would like to support pre-10.14.4 versions of macOS, the helper tool
cannot be written in Swift or Swift 5 Runtime Support for Command Line Tools must
be installed. This is because the helper tool must be a Command Line Tool (not an app bundle) and starting with Swift 5
and Xcode 10.2, Apple made the decision to end support for embedding the Swift runtime into Command Line Tools.
The helper tool cannot make use of Swift concurrency
prior to macOS 12. This is also because the helper tool must be a Command Line Tool and Apple chose to only implement
Swift concurrency backwards compatability on macOS 10.15 and 11 for app bundles. While code containing Swift concurrency
will compile, it will crash at run time.
Note: The helper tool, once installed, will not be run from inside of your app bundle and so it cannot target any
Swift runtime or concurrency framework bundled with your app. (This is unlike XPC Services which may do this.)
All of the Swift packages used by this sample target macOS 10.10 and later.
Dependencies
Several Swift packages were created specifically for this helper tool use case:
- Blessed: Helper tool installation
- Makes
SMJobBless functionality a
single function call; no need to directly use the Service Management & Authorization Services frameworks
- SecureXPC: Communication between your app and helper tool
- Easily send and receive Codable instances
- Designed for secure XPC Mach service connections, which by default allow any process to communicate
- EmbeddedPropertyList: Embedded property list reader
- Directly read the info and launchd property lists embedded in the helper tool
- Authorized: Self-restriction of certain actions
- In most cases you probably won't make direct use of this; it is a dependency of Blessed
- Provides a full Swift implementation of Authorization Services to enable advanced use cases
- Required: Parser & evaluator of code signing requirements
- While not directly used by this sample, it's a dependency of Blessed
- This helps Blessed to generate detailed error messages in case
SMJobBless fails
These are collectively available as a package collection
which you can add to Xcode 13 or later.
Each of these frameworks have their own READMEs as well as full DocC documentation.
Installing a Helper Tool
macOS 12 and earlier allows non-sandboxed apps to indirectly run code as root by installing a privileged helper tool. If
you were to directly use Apple's APIs you'd use the
Authorization Services
framework to have the user authenticate as an admin and then call
SMJobBless to perform
the installation. The Blessed package used by this sample simplifies this
to just one function call.
If this operation succeeds the helper tool will be copied from the Contents/Library/LaunchServices directory inside
your app bundle to /Library/PrivilegedHelperTools/. Once installed, it is managed by
launchd.
For this operation to succeed, Apple imposes numerous requirements:
- Your app must be signed.
- The helper tool must be signed.
- The helper tool must be located in the
Contents/Library/LaunchServices directory inside the app's bundle.
- The helper tool must be an executable, not an app bundle.
- The helper tool must have an embedded launchd property list.
- The helper tool's embedded launchd property list must have an entry with
Label as the key and the value
must be the filename of the helper tool.
- The helper tool must have an embedded info property list.
- The helper tool's embedded info property list must have an entry with
SMAuthorizedClients
as its key and its value must be an array of strings. Each string must be a
code signing requirement.
The app must satisify at least one of these requirements.
- The app must satisify one or more of these requirements to install or update the helper tool.
- To update the helper tool, the app must satisfy one or more requirements of both the installed helper tool and
the bundled helper tool.
- These requirements are only about whether an app can install or update the helper tool. They impose no
restrictions on communication with the helper tool.
- The helper tool's embedded info property list must have an entry with
CFBundleVersion
as its key and its value must be a string matching the format described in CFBundleVersion's documentation.
- This requirement is not documented by Apple, but is enforced.
- While not documented by Apple, calling this function will not overwrite an existing installation of a helper tool
with one that has an equal or lower value for its
CFBundleVersion entry.
- Despite Apple requiring the info property list contain a key named
CFBundleVersion, the helper tool must be
a Command Line Tool and must not be a bundle.
- Your app's Info.plist must have an entry with
SMPrivilegedExecutables
as its key and its value must be a dictionary. Each dictionary key must be a helper tool's filename; for example
"com.example.SwiftAuthorizationApp.helper". Each dictionary value must be a string representation of a code
signing requirement that the helper tool satisfies.
If your implementation does not meet these requirements, blessing will fail. The Blessed package will identify each
missing requirement along with a detail explanation as the thrown BlessError.
Satisfying These Requirements
While Apple imposes numerous requirements, many of them only need to be configured once. For the remainder, this sample
uses build variables and a custom build script to automate the process. In particular the build script handles:
- Generating the
SMPrivilegedExecutables entry
- Generating the
SMAuthorizedClients entry, with a code signing requirement that mitigates downgrade attacks
- Incrementing the helper tool's
CFBundleVersion so that it can be updated
- Ensuring the helper tool's launchd
Label value matches the helper tool's filename
Additionally the build variables, build scripts, and sample code are designed to avoid any duplicative hard coding of
values. If you follow the pattern used in this sample and ever wanted to change these values, you'd only need to
update them in one place each.
This section walks your through satisfying all of Apple's requirements.
1 & 2. Code Signing
Xcode can automatically code sign for you. If you don't already have an Apple
Developer ID you'll need to provision one. If your build process
does not use Xcode for signing as part of the build process you'll likely need to modify the PropertyListModifier.swift
build script.
3. Helper Tool Location
The exact steps for this may differ in future versions of Xcode. As of Xcode 13:
- Open your project's
xcodeproj file
- Select your app's target
- Switch to the Build Phases tab
- Create a Copy Files Phase
- Set the Destination as "Wrapper"
- Set the Subpath to "Contents/Library/LaunchServices"
- Add the helper tool product, for example "com.example.SwiftAuthorizationApp.helper"
- Make sure "Code Sign on Copy" is checked
Build Variables
This sample relies on build variables to satisfy several of these requirements. You will need to set these build
variables for three different places:
- the project
- the app target
- the helper tool target
The sample uses xcconfig files; however, you may do this using your xcodeproj file's Build Settings sections if you
prefer. If you would like to use xcconfig files, but are unfamiliar with them then read through this
excellent article by NSHipster.
For all of the entries needed, see the following xcconfig files:
- Config.xcconfig
- SwiftAuthorizationApp/AppConfig.xcconfig
- SwiftAuthorizationHelperTool/HelperToolConfig.xcconfig
Note that the setting of some target specific values in the project level Config.xcconfig is essential as both build
processes needs access to this information. For example, at build time the app needs to know the identifier for the
helper tool in order to generate its SMPrivilegedExecutables entry.
4. Helper Tool Executable
The helper tool in this sample is configured to be a Command Line Tool. This will result in a single executable file
being built by Xcode (unlike an application which produces a bundle directory).
5 & 7. Embedded launchd and Info Property Lists
In the root of the helper tool directory create a Info property list file with a
CFBundleVersion
entry. Unless you want to specify additional entries, you do not need to create a launchd property list as the build
script will do that for you automatically.
The compiler must be told to embed these property lists into the helper tool executable. If you configured the build
variables to match the sample your helper tool should have the following build variable configured:
OTHER_LDFLAGS = -sectcreate __TEXT __info_plist $(INFOPLIST_FILE) -sectcreate __TEXT __launchd_plist $(LAUNCHDPLIST_FILE)
Where INFOPLIST_FILE are LAUNCHDPLIST_FILE