[vc_row][vc_column][vc_column_text]
The Gatekeeper system has protected macOS users against malicious software since its introduction in OS X 10.7.3 (Lion). This system assures users that software comes from a trusted source and doesn’t contain malicious content. But how does it work?
[/vc_column_text][us_separator][vc_column_text]
Introduction
The Mac software ecosystem has historically been fairly untroubled by malicious viruses and software. This was due in part to the comparatively small user base and partly because the system — which is based on Unix — is naturally partitioned into users and groups, making it difficult for malicious code to obtain administrative privileges.
But the platform is increasing in popularity. Apple’s desktop operating system accounted for approximately 10% of desktop market share in 2019. The same kernel — Apple XNU, a modified Mach microkernel — is also used by the company’s IOS devices: iPhone and Apple Watch. This increasing install base makes the platform an attractive target for malicious software.
Apple has two main strategies in place to protect its users. We’ll look at each stage of this protection regime in the next sections, but broadly they comprise:
- Code Signing: this ensures that code comes from a known, trusted source, and hasn’t been altered since it was signed.
- Notarization: the code is inspected by Apple to ensure it falls within its safety guidelines; the resulting receipt can be attached to the software permanently, in a process known as “stapling.”
[/vc_column_text][us_separator][vc_column_text]
Code Signing
Signing ensures the code belongs to us and can’t be changed after it’s signed. This is done by the codesign
tool, which:
- Creates a secure hash of the code itself (this hash will change if the code is tampered-with after the fact)
- Signs the hash and the code with our Developer Certificate. This puts our name and details on the code. Apple has checked our credentials and has also signed our Developer Certificate to say that we are a valid, trusted developer.
- Stamps the resulting signature with the current time. This ensures that if our certificate expires, you can continue to use this software.
Here’s how we test-sign the FusionReactor macOS Native Library, which is used by the FusionReactor Production Debugger:
xcrun codesign --verbose --strict --keychain /Users/jhawksley/Library/Keychains/login.keychain -s CERT_ID_HERE --timestamp target/libfrjvmti_x64.dylib
[/vc_column_text][us_separator][vc_column_text]
Notarization and Stapling
The second stage is to have Apple actually check our code.
The Notarization command looks like this:
xcrun altool --notarize-app --username "our_apple_id@intergral.com" --password "our_password" --primary-bundle-id "com.intergral.bundleid" --file bundle.zip
Before we ship the library to Apple for Notarization, we have to sign it using codesign
, and we have to zip it up to minimize the transfer size. The username and password are those of a valid Apple Developer Account.
Notarization is an automated service, which — while not providing usability or design feedback, like App Review — does checks code is correctly signed, and doesn’t contain malicious content.
The result of this is a Notarization Ticket, which is a piece of secure data that Apple sends back to us and also publishes online in the Gatekeeper Code Directory Catalog.
Some types of software — for instance the native library we showed in Code Signing above — don’t have space in their structure for a ticket, so they can’t be stapled. Other types of software, like the FusionReactor macOS Installer, do have space, and the Notarization Ticket obtained above can be stapled to them.
When you run our software on your machine, Gatekeeper automatically checks to see if the software is valid. If there’s a network connection available, Gatekeeper uses the online Code Directory to look up the ticket and checks it against the software. Should no network is available, Gatekeeper uses the stapled ticket.
If a valid ticket is located, Gatekeeper knows that Apple has checked this software and that it meets their standards — and can run.
[/vc_column_text][us_separator][vc_column_text]
Why Bother?
Apple has been gradually tightening up the conditions under which unsigned software can run. In macOS Catalina, this is still possible (you have to turn Gatekeeper off using Terminal commands) although in the future even that may no longer be possible.
When macOS tries to use or install unsigned content on macOS Catalina, you’ll see the following dialog (which can’t be bypassed) — and the content is not opened.
[/vc_column_text][us_separator][us_image image=”57819″][us_separator][vc_column_text]
When content has been correctly signed, Gatekeeper tells you where it came from and lets you decide whether to open it. Here’s what we see when open our (signed, notarized, stapled) installer from our build server.
[/vc_column_text][us_separator][us_image image=”57822″][us_separator][vc_column_text]
Trust, but Verify
If you want to check a signature, this is easy to do. Open the Terminal app, and use the codesign
command to retrieve the certificate:
codesign -d --verbose=4 ~/Downloads/FusionReactor_macos_8_3_0-SNAPSHOT.dmg
This spits out the following (excerpted for clarity):
Identifier=FusionReactor_macos_8_3_0-SNAPSHOT
CodeDirectory v=20100 size=179 flags=0x0(none) hashes=1+2 location=embedded
CandidateCDHashFull sha256=f684fe6584f8249c3bfb60c188dd18c614adc29e6539490094947e1e09bbb6c8
Authority=Developer ID Application: Intergral Information Solutions GmbH (R3VQ6KXHEL)
Authority=Developer ID Certification Authority
Authority=Apple Root CA
Timestamp=Dec 5, 2019 at 14:52:15
The Identifier
is the file name (minus extension) we signed, and the Identifier
and CandidateCDHashFull
values tell Gatekeeper how to perform an online lookup of our Notarization Ticket.
The three Authority
lines are the chain of trust: they show that Apple has trusted us by checking our details and issued a certificate, whose serial number is R3VQ6KXHEL.
Finally, the Timestamp
shows when we actually signed this software. If you try to use it in future, perhaps when our certificate has expired (hopefully there’ll be many new versions of FusionReactor before then!), the Timestamp assures Gatekeeper that the software was signed within the certificate validity period, and should be treated as if the certificate was still valid. Gatekeeper should then continue to open this software indefinitely.
[/vc_column_text][us_separator][vc_column_text]
Automation
It’s possible to automate the signing, notarization and stapling process, but it’s not exactly straightforward.
Apple’s development tool – the venerable Xcode – handles signing, notarization and stapling seamlessly as part of its user interface. Apple does provide command-line tools to perform these tasks (codesign
, altool
and stapler
) but these all make some assumptions that the user running them is logged in.
The exact mechanics of automated signing of Apple binaries are rather beyond the scope of this article. However, we can give you some hints:
- A Jenkins node running on a Mac Mini is used. Apple doesn’t allow virtualization of its hardware, and Docker on Mac is a linux environment, so the node must be a bare-metal Apple environment. Mac Minis are excellent little machines for this.
- The user under which the Jenkins node is running:
- Must be logged in to the desktop. This creates an Aqua desktop session for the user — which is valid even if the Jenkins node is launched as that user using
ssh
. The Aqua session is required to use the code signing tools. The login can be done on the Mac itself, or using Apple Screen Sharing (which is a customized VNC) but the session should be ended by closing the window, not logging the user out. - Must have the Apple Developer ID code signing certificate, along with the corresponding private key, installed into its Login Keychain (the default keychain).
- Must have the keychain unlocked prior to code signing using the command
security unlock-keychain
- Must be logged in to the desktop. This creates an Aqua desktop session for the user — which is valid even if the Jenkins node is launched as that user using
Another wrinkle in the automation of this procedure is that the notarization command (xcrun altool --notarize-app
) is asynchronous. Once the code is submitted (which itself can take a couple of minutes), the Apple Notarization Service returns some XML containing a RequestUUID
. You have to then poll the Service using this UUID until it returns either success or failure. This can take up to 10 minutes, in our experience. If you don’t parallelize this part of your build, it will be impose a long delay.
[/vc_column_text][/vc_column][/vc_row]