Sign in Join
Wiki copytemporary

Hardened runtime for macOS: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Hardened runtime for macOS

OldNewLine
1+## Overview
2+
3+The hardened runtime was introduced by Apple in macOS 10.14 (Mojave) and while it is optional for applications, it is required in order to [[Notarization for macOS 10.14.5+|notarize]] your application.
4+
5+The hardened runtime, along with System Integrity Protection (SIP), protects the runtime integrity of your application software by preventing certain classes of exploits, like code injection, dynamically linked library hijacking, and process memory space tampering. If your application relies on a capability that the hardened runtime restricts, you need to add an entitlement to disable an individual protection. You should only add the entitlements that are absolutely necessary for your applications functionality.
6+
7+[[Sandboxing for macOS|Sandboxing]] and the hardened runtime prevent an application from doing things it would ordinarily have had permission to do. It should be noted that these two protections are overlapping. The sandbox and the hardened runtime could prevent the same action, so even if the hardened runtime would allow the action, the sandbox may prevent it, and vice versa. See the external links to Apple's lists of hardened runtime entitlements and sandboxing entitlements below.
8+
9+## Hardening your application
10+
11+The hardening of an application is done during [[Code Signing for macOS|code signing]]. To harden your application, open a Terminal and run this command:
12+
13+``` bash
14+codesign --force --options runtime --timestamp --sign "Developer ID Application: YOUR NAME (TEAM_ID)" /path/to/bundle.app
15+```
16+
17+## Hardened runtime entitlements
18+
19+To allow your application to do something that is now prevented by your hardened runtime, you need to give it that entitlement. You do this by creating an entitlements plist file like this:
20+
21+``` xml
22+<?xml version="1.0" encoding="UTF-8"?>
23+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
24+<plist version="1.0">
25+<dict>
26+ <key>com.apple.security.device.camera</key>
27+ <true/>
28+</dict>
29+</plist>
30+```
31+
32+This would allow your application to capture movies and still images using the built-in camera. To verify you have a valid plist file, open a Terminal and run the following command:
33+
34+``` bash
35+plutil entitlements.plist
36+```
37+
38+If the file is properly formatted, the output of that command should be:
39+
40+ entitlements.plist: OK
41+
42+To add this entitlement to your application, you need to add it to your code signing command like this:
43+
44+``` bash
45+codesign --force --options runtime --timestamp --entitlements entitlements.plist --sign "Developer ID Application: YOUR NAME (TEAM_ID)" /path/to/bundle.app
46+```
47+
48+## Verifying hardening
49+
50+To verify that you have successfully hardened your application, open a Terminal and run this command:
51+
52+``` bash
53+codesign --display --verbose /path/to/bundle.app
54+```
55+
56+The output of that command should be similar to:
57+
58+ Executable=/Users/trev/your.app/Contents/MacOS/yourapp
59+ Identifier=org.yourdomain.yourapp
60+ Format=app bundle with Mach-O thin (x86_64)
61+ CodeDirectory v=20500 size=101470 flags=0x10000(runtime) hashes=3164+3 location=embedded
62+ Signature size=9063
63+ Timestamp=7 Dec 2019 at 18:36:08
64+ Info.plist entries=16
65+ TeamIdentifier=<10 alpha numerical digits>
66+ Runtime Version=10.14.0
67+ Sealed Resources version=2 rules=13 files=45
68+ Internal requirements count=1 size=180
69+
70+The (runtime) flag in the "CodeDirectory" line indicates that your application is hardened.
71+
72+To check whether the required hardened runtime entitlements are available, open a Terminal and run the following command:
73+
74+``` bash
75+codesign -d --entitlements :- /path/to/bundle.app
76+```
77+
78+which should result in the following output:
79+
80+``` xml
81+<?xml version="1.0" encoding="UTF-8"?>
82+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
83+<plist version="1.0">
84+<dict>
85+ <key>com.apple.security.device.camera</key>
86+ <true/>
87+</dict>
88+</plist>
89+```
90+
91+## Protected resource access
92+
93+Your application needs to declare its intent to access protected resources by providing a purpose string that explains to the user why you need access to protected resources. When declaring resource access, only set the entitlements
 and usage strings on your main bundle.
94+
95+| Description | Entitlement | Usage string Info.plist key |
96+|----|----|----|
97+| Audio input and microphone | com.apple.security.device.audio-input | NSMicrophoneUsageDescription |
98+| Any camera exposed via AVFoundation | com.apple.security.device.camera | NSCameraUsageDescription |
99+| Location | com.apple.security.personal-information.location | NSLocationUsageDescription |
100+| Contacts | com.apple.security.personal-information.addressbook | NSContactsUsageDescription |
101+| Calendars and Reminders | com.apple.security.personal-information.calendars | NSCalendarUsageDescription |
102+| Apple Photos library | com.apple.security.personal-information.photos-library | NSPhotoLibraryUsageDescription |
103+| Sending Apple Events to other apps | com.apple.security.automation.apple-events | NSAppleEventsUsageDescription |
104+
105+## Hardened runtime issues and solutions
106+
107+**Issue:** My application runs non-native code, and I want that code to run blazing fast with JIT, but my application crashes when I enable Hardened Runtime.
108+
109+- Recommended solution: Adopt the “com.apple.security.cs.allow-jit” entitlement; use mmap and the MAP_JIT flag to allocate anonymous Read/Write/Execute memory.
110+
111+- Fallback solution: Disable Runtime Code Signing Enforcement with the “com.apple.security.cs.allow-unsigned-executable-memory” entitlement; bytes mapped from disk will still be checked against any associated 
code signature.
112+
113+**Issue:** My application patches system frameworks it loads into memory to 
 accomplish “...” but now my application crashes when I enable Hardened Runtime.
114+
115+- Recommended Solution: Don’t do this! (Library Validation may meet your use case.)
116+
117+- Fallback Solution: Disable Runtime Code Signing Enforcement with the “com.apple.security.cs.allow-unsigned-executable-memory” entitlement.
118+
119+**Issue:** My application crashes when I adopt the Hardened Runtime and then run my auto-update mechanism.
120+
121+- Explanation: Code signatures are latched to files on first use. Modifying files in place causes a signature mismatch.
122+
123+- Recommended Solution: Whenever you update a signed file, create a new file.
124+
125+**Issue:** My application loads plugins from other developers in-process, but plug-in loading fails when I adopt the Hardened Runtime.
126+
127+- Recommended Solution: Consider moving to an out of process plugin model.
128+
129+- Fallback Solution: Use the “com.apple.security.cs.disable-library-validation” entitlement Allows loading unsigned and adhoc signed plug-ins.
130+
131+**Issue:** I need to use DYLD environment variables while building and debugging my application, but they are being ignored when I enable Hardened Runtime.
132+
133+- Solution: Use the “com.apple.security.get-task-allow” entitlement on your debug build.
134+
135+**Issue:** My application uses DYLD environment variables when it ships to my customers and now it doesn’t work with Hardened Runtime.
136+
137+- Recommend Solution: Don’t do this!
138+
139+- Fallback Solution: Use the “com.apple.security.cs.allow-dyld-environment-variables” entitlement.
140+
141+**Issue:** Hardened Runtime prevents debugging of hardened processes by default. How can I build and test with the Hardened Runtime if I cannot attach 
 a debugger?
142+
143+- Solution: Use the “com.apple.security.get-task-allow” entitlement on your debug build. Note: Running an application under a debugger will mask Hardened Runtime related issues, so be sure to test a release build.
144+
145+**Issue:** My application supports an in-process plug-in ecosystem. How can my plug-in developers debug their plug-ins?
146+
147+- Recommended Solution: Move to an out of process plug-in model.
148+
149+- Alternative Solution: Ship a debug version to registered plug-in developers.
150+
151+- Fallback Solution: Combine “com.apple.security.get-task-allow” with 
“com.apple.security.cs.disable-library-validation”.
152+
153+## See also
154+
155+- [[Code Signing for macOS]]
156+- [[Notarization for macOS 10.14.5+]]
157+- [[Sandboxing for macOS]]
158+
159+## External links
160+
161+- [Apple: Hardened runtime entitlements](https://developer.apple.com/documentation/security/hardened_runtime_entitlements)
162+- [Apple: Sandbox entitlements](https://developer.apple.com/documentation/security/app_sandbox_entitlements)
163+
164+---
165+*From the Free Pascal wiki, page "Hardened runtime for macOS", as it stood on 2023-01-03 after a change by Mischi, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history