2 * Copyright (c) 2011 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include "SecAssessment.h"
26 #include "policyengine.h"
27 #include "xpcengine.h"
28 #include "csutilities.h"
29 #include <CoreFoundation/CFRuntime.h>
30 #include <security_utilities/globalizer.h>
31 #include <security_utilities/unix++.h>
32 #include <security_utilities/cfmunge.h>
36 using namespace CodeSigning
;
39 static void esp_do_check(const char *op
, CFDictionaryRef dict
)
41 OSStatus result
= __esp_check_ns(op
, (void *)(CFDictionaryRef
)dict
);
43 MacOSError::throwMe(result
);
49 struct _SecAssessment
: private CFRuntimeBase
{
51 _SecAssessment(CFURLRef p
, AuthorityType typ
, CFDictionaryRef r
) : path(p
), type(typ
), result(r
) { }
53 CFCopyRef
<CFURLRef
> path
;
55 CFRef
<CFDictionaryRef
> result
;
58 static _SecAssessment
&ref(SecAssessmentRef r
)
59 { return *(_SecAssessment
*)r
; }
62 void *operator new (size_t size
)
64 return (void *)_CFRuntimeCreateInstance(NULL
, SecAssessmentGetTypeID(),
65 sizeof(_SecAssessment
) - sizeof(CFRuntimeBase
), NULL
);
68 static void finalize(CFTypeRef obj
)
69 { ((_SecAssessment
*)obj
)->~_SecAssessment(); }
72 typedef _SecAssessment SecAssessment
;
75 static const CFRuntimeClass assessmentClass
= {
77 "SecAssessment", // name
80 SecAssessment::finalize
, // finalize
88 static dispatch_once_t assessmentOnce
;
89 CFTypeID assessmentType
= _kCFRuntimeNotATypeID
;
91 CFTypeID
SecAssessmentGetTypeID()
93 dispatch_once(&assessmentOnce
, ^void() {
94 if ((assessmentType
= _CFRuntimeRegisterClass(&assessmentClass
)) == _kCFRuntimeNotATypeID
)
97 return assessmentType
;
102 // Common dictionary constants
104 CFStringRef kSecAssessmentContextKeyOperation
= CFSTR("operation");
105 CFStringRef kSecAssessmentOperationTypeExecute
= CFSTR("operation:execute");
106 CFStringRef kSecAssessmentOperationTypeInstall
= CFSTR("operation:install");
107 CFStringRef kSecAssessmentOperationTypeOpenDocument
= CFSTR("operation:lsopen");
111 // Read-only in-process access to the policy database
113 class ReadPolicy
: public PolicyDatabase
{
115 ReadPolicy() : PolicyDatabase(defaultDatabase
) { }
117 ModuleNexus
<ReadPolicy
> gDatabase
;
121 // An on-demand instance of the policy engine
123 ModuleNexus
<PolicyEngine
> gEngine
;
127 // Policy evaluation ("assessment") operations
129 CFStringRef kSecAssessmentAssessmentVerdict
= CFSTR("assessment:verdict");
130 CFStringRef kSecAssessmentAssessmentOriginator
= CFSTR("assessment:originator");
131 CFStringRef kSecAssessmentAssessmentAuthority
= CFSTR("assessment:authority");
132 CFStringRef kSecAssessmentAssessmentSource
= CFSTR("assessment:authority:source");
133 CFStringRef kSecAssessmentAssessmentAuthorityRow
= CFSTR("assessment:authority:row");
134 CFStringRef kSecAssessmentAssessmentAuthorityOverride
= CFSTR("assessment:authority:override");
135 CFStringRef kSecAssessmentAssessmentAuthorityOriginalVerdict
= CFSTR("assessment:authority:verdict");
136 CFStringRef kSecAssessmentAssessmentFromCache
= CFSTR("assessment:authority:cached");
138 CFStringRef kDisabledOverride
= CFSTR("security disabled");
140 SecAssessmentRef
SecAssessmentCreate(CFURLRef path
,
141 SecAssessmentFlags flags
,
142 CFDictionaryRef context
,
147 if (flags
& kSecAssessmentFlagAsynchronous
)
148 MacOSError::throwMe(errSecCSUnimplemented
);
150 AuthorityType type
= typeFor(context
, kAuthorityExecute
);
151 CFRef
<CFMutableDictionaryRef
> result
= makeCFMutableDictionary();
153 SYSPOLICY_ASSESS_API(cfString(path
).c_str(), int(type
), flags
);
156 if (__esp_enabled() && (flags
& kSecAssessmentFlagDirect
)) {
157 CFTemp
<CFDictionaryRef
> dict("{path=%O, flags=%d, context=%O, override=%d}", path
, flags
, context
, overrideAssessment());
158 esp_do_check("cs-assessment-evaluate", dict
);
161 // check the object cache first unless caller denied that or we need extended processing
162 if (!(flags
& (kSecAssessmentFlagRequestOrigin
| kSecAssessmentFlagIgnoreCache
))) {
163 if (gDatabase().checkCache(path
, type
, flags
, result
))
164 return new SecAssessment(path
, type
, result
.yield());
167 if (flags
& kSecAssessmentFlagDirect
) {
168 // ask the engine right here to do its thing
169 SYSPOLICY_ASSESS_LOCAL();
170 gEngine().evaluate(path
, type
, flags
, context
, result
);
172 // relay the question to our daemon for consideration
173 SYSPOLICY_ASSESS_REMOTE();
174 xpcEngineAssess(path
, flags
, context
, result
);
176 } catch (CommonError
&error
) {
177 switch (error
.osStatus()) {
178 case CSSMERR_TP_CERT_REVOKED
:
181 if (!overrideAssessment(flags
))
182 throw; // let it go as an error
185 // record the error we would have returned
186 cfadd(result
, "{%O=#F,'assessment:error'=%d}}", kSecAssessmentAssessmentVerdict
, error
.osStatus());
188 // catch stray errors not conforming to the CommonError scheme
189 if (!overrideAssessment(flags
))
190 throw; // let it go as an error
191 cfadd(result
, "{%O=#F}", kSecAssessmentAssessmentVerdict
);
194 if (__esp_enabled() && (flags
& kSecAssessmentFlagDirect
)) {
195 CFTemp
<CFDictionaryRef
> dict("{path=%O, flags=%d, context=%O, override=%d, result=%O}", path
, flags
, context
, overrideAssessment(), (CFDictionaryRef
)result
);
196 __esp_notify_ns("cs-assessment-evaluate", (void *)(CFDictionaryRef
)dict
);
199 return new SecAssessment(path
, type
, result
.yield());
201 END_CSAPI_ERRORS1(NULL
)
205 static void traceResult(CFURLRef target
, MessageTrace
&trace
, std::string
&sanitized
)
207 static const char *interestingBundles
[] = {
214 "com.adobe.flashplayer.installmanager",
215 "com.adobe.Installers.Setup",
216 "com.adobe.PDApp.setup",
217 "com.bittorrent.uTorrent",
218 "com.divx.divx6formacinstaller",
219 "com.getdropbox.dropbox",
221 "com.Google.GoogleEarthPlugin.plugin",
222 "com.Google.GoogleEarthPlus",
224 "com.macpaw.CleanMyMac",
225 "com.microsoft.SilverlightInstaller",
226 "com.paragon-software.filesystems.NTFS.pkg",
227 "com.RealNetworks.RealPlayer",
229 "it.alfanet.squared5.MPEGStreamclip",
230 "org.mozilla.firefox",
236 string identifier
= "UNBUNDLED";
237 string version
= "UNKNOWN";
238 if (CFRef
<CFBundleRef
> bundle
= CFBundleCreate(NULL
, target
)) {
239 if (CFStringRef ident
= CFBundleGetIdentifier(bundle
))
240 identifier
= cfString(ident
);
241 if (CFStringRef vers
= CFStringRef(CFBundleGetValueForInfoDictionaryKey(bundle
, CFSTR("CFBundleShortVersionString"))))
242 version
= cfString(vers
);
245 CFRef
<CFURLRef
> url
= CFURLCopyAbsoluteURL(target
);
246 sanitized
= cfString(url
);
247 string::size_type rslash
= sanitized
.rfind('/');
248 if (rslash
!= string::npos
)
249 sanitized
= sanitized
.substr(rslash
+1);
250 bool keepFilename
= false;
251 for (const char **pfx
= interestingBundles
; *pfx
; pfx
++) {
252 size_t pfxlen
= strlen(*pfx
);
253 if (identifier
.compare(0, pfxlen
, *pfx
, pfxlen
) == 0)
254 if (pfxlen
== identifier
.size() || (*pfx
)[pfxlen
-1] == '.') {
260 string::size_type dot
= sanitized
.rfind('.');
261 if (dot
!= string::npos
)
262 sanitized
= sanitized
.substr(dot
);
264 sanitized
= "(none)";
267 trace
.add("signature2", "bundle:%s", identifier
.c_str());
268 trace
.add("signature3", "%s", sanitized
.c_str());
269 trace
.add("signature5", "%s", version
.c_str());
272 static void traceAssessment(SecAssessment
&assessment
, AuthorityType type
, CFDictionaryRef result
)
274 if (CFDictionaryGetValue(result
, CFSTR("assessment:remote")))
275 return; // just traced in syspolicyd
277 string authority
= "UNSPECIFIED";
278 bool overridden
= false;
279 bool old_overridden
= false;
280 if (CFDictionaryRef authdict
= CFDictionaryRef(CFDictionaryGetValue(result
, kSecAssessmentAssessmentAuthority
))) {
281 if (CFStringRef auth
= CFStringRef(CFDictionaryGetValue(authdict
, kSecAssessmentAssessmentSource
)))
282 authority
= cfString(auth
);
284 authority
= "no authority";
285 if (CFTypeRef override
= CFDictionaryGetValue(authdict
, kSecAssessmentAssessmentAuthorityOverride
))
286 if (CFEqual(override
, kDisabledOverride
)) {
287 old_overridden
= true;
288 if (CFDictionaryGetValue(authdict
, kSecAssessmentAssessmentAuthorityOriginalVerdict
) == kCFBooleanFalse
)
293 MessageTrace
trace("com.apple.security.assessment.outcome2", NULL
);
294 std::string sanitized
;
295 traceResult(assessment
.path
, trace
, sanitized
);
296 trace
.add("signature4", "%d", type
);
298 if (CFDictionaryGetValue(result
, kSecAssessmentAssessmentVerdict
) == kCFBooleanFalse
) {
299 trace
.add("signature", "denied:%s", authority
.c_str());
300 trace
.send("assessment denied for %s", sanitized
.c_str());
301 } else if (overridden
) { // would have failed except for override
302 trace
.add("signature", "defeated:%s", authority
.c_str());
303 trace
.send("assessment denied for %s but overridden", sanitized
.c_str());
304 } else if (old_overridden
) { // would have succeeded even without override
305 trace
.add("signature", "override:%s", authority
.c_str());
306 trace
.send("assessment granted for %s and overridden", sanitized
.c_str());
308 trace
.add("signature", "granted:%s", authority
.c_str());
309 trace
.send("assessment granted for %s by %s", sanitized
.c_str(), authority
.c_str());
313 static void traceUpdate(CFTypeRef target
, CFDictionaryRef context
, CFDictionaryRef result
)
315 // only trace add operations on URL targets
316 if (target
== NULL
|| CFGetTypeID(target
) != CFURLGetTypeID())
318 CFStringRef edit
= CFStringRef(CFDictionaryGetValue(context
, kSecAssessmentContextKeyUpdate
));
319 if (!CFEqual(edit
, kSecAssessmentUpdateOperationAdd
))
321 MessageTrace
trace("com.apple.security.assessment.update", NULL
);
322 std::string sanitized
;
323 traceResult(CFURLRef(target
), trace
, sanitized
);
324 trace
.send("added rule for %s", sanitized
.c_str());
329 // At present, CopyResult simply retrieves the result already formed by Create.
330 // In the future, this will be more lazy.
332 CFDictionaryRef
SecAssessmentCopyResult(SecAssessmentRef assessmentRef
,
333 SecAssessmentFlags flags
,
338 SecAssessment
&assessment
= SecAssessment::ref(assessmentRef
);
339 CFCopyRef
<CFDictionaryRef
> result
= assessment
.result
;
340 if (overrideAssessment(flags
)) {
341 // turn rejections into approvals, but note that we did that
342 CFTypeRef verdict
= CFDictionaryGetValue(result
, kSecAssessmentAssessmentVerdict
);
343 if (verdict
== kCFBooleanFalse
) {
344 CFRef
<CFMutableDictionaryRef
> adulterated
= makeCFMutableDictionary(result
.get());
345 CFDictionarySetValue(adulterated
, kSecAssessmentAssessmentVerdict
, kCFBooleanTrue
);
346 if (CFDictionaryRef authority
= CFDictionaryRef(CFDictionaryGetValue(adulterated
, kSecAssessmentAssessmentAuthority
))) {
347 CFRef
<CFMutableDictionaryRef
> authority2
= makeCFMutableDictionary(authority
);
348 CFDictionarySetValue(authority2
, kSecAssessmentAssessmentAuthorityOverride
, kDisabledOverride
);
349 CFDictionarySetValue(authority2
, kSecAssessmentAssessmentAuthorityOriginalVerdict
, verdict
);
350 CFDictionarySetValue(adulterated
, kSecAssessmentAssessmentAuthority
, authority2
);
352 cfadd(adulterated
, "{%O={%O=%O}}",
353 kSecAssessmentAssessmentAuthority
, kSecAssessmentAssessmentAuthorityOverride
, kDisabledOverride
);
355 result
= adulterated
.get();
358 traceAssessment(assessment
, assessment
.type
, result
);
359 return result
.yield();
361 END_CSAPI_ERRORS1(NULL
)
366 // Policy editing operations.
367 // These all make permanent changes to the system-wide authority records.
369 CFStringRef kSecAssessmentContextKeyUpdate
= CFSTR("update");
370 CFStringRef kSecAssessmentUpdateOperationAdd
= CFSTR("update:add");
371 CFStringRef kSecAssessmentUpdateOperationRemove
= CFSTR("update:remove");
372 CFStringRef kSecAssessmentUpdateOperationEnable
= CFSTR("update:enable");
373 CFStringRef kSecAssessmentUpdateOperationDisable
= CFSTR("update:disable");
374 CFStringRef kSecAssessmentUpdateOperationFind
= CFSTR("update:find");
376 CFStringRef kSecAssessmentUpdateKeyAuthorization
= CFSTR("update:authorization");
377 CFStringRef kSecAssessmentUpdateKeyPriority
= CFSTR("update:priority");
378 CFStringRef kSecAssessmentUpdateKeyLabel
= CFSTR("update:label");
379 CFStringRef kSecAssessmentUpdateKeyExpires
= CFSTR("update:expires");
380 CFStringRef kSecAssessmentUpdateKeyAllow
= CFSTR("update:allow");
381 CFStringRef kSecAssessmentUpdateKeyRemarks
= CFSTR("update:remarks");
383 CFStringRef kSecAssessmentUpdateKeyRow
= CFSTR("update:row");
384 CFStringRef kSecAssessmentUpdateKeyCount
= CFSTR("update:count");
385 CFStringRef kSecAssessmentUpdateKeyFound
= CFSTR("update:found");
387 CFStringRef kSecAssessmentRuleKeyID
= CFSTR("rule:id");
388 CFStringRef kSecAssessmentRuleKeyPriority
= CFSTR("rule:priority");
389 CFStringRef kSecAssessmentRuleKeyAllow
= CFSTR("rule:allow");
390 CFStringRef kSecAssessmentRuleKeyLabel
= CFSTR("rule:label");
391 CFStringRef kSecAssessmentRuleKeyRemarks
= CFSTR("rule:remarks");
392 CFStringRef kSecAssessmentRuleKeyRequirement
= CFSTR("rule:requirement");
393 CFStringRef kSecAssessmentRuleKeyType
= CFSTR("rule:type");
394 CFStringRef kSecAssessmentRuleKeyExpires
= CFSTR("rule:expires");
395 CFStringRef kSecAssessmentRuleKeyDisabled
= CFSTR("rule:disabled");
396 CFStringRef kSecAssessmentRuleKeyBookmark
= CFSTR("rule:bookmark");
399 Boolean
SecAssessmentUpdate(CFTypeRef target
,
400 SecAssessmentFlags flags
,
401 CFDictionaryRef context
,
404 if (CFDictionaryRef outcome
= SecAssessmentCopyUpdate(target
, flags
, context
, errors
)) {
412 CFDictionaryRef
SecAssessmentCopyUpdate(CFTypeRef target
,
413 SecAssessmentFlags flags
,
414 CFDictionaryRef context
,
419 CFDictionary
ctx(context
, errSecCSInvalidAttributeValues
);
420 CFRef
<CFDictionaryRef
> result
;
422 if (flags
& kSecAssessmentFlagDirect
) {
423 if (__esp_enabled()) {
424 CFTemp
<CFDictionaryRef
> dict("{target=%O, flags=%d, context=%O}", target
, flags
, context
);
425 OSStatus esp_result
= __esp_check_ns("cs-assessment-update", (void *)(CFDictionaryRef
)dict
);
426 if (esp_result
!= noErr
)
430 // ask the engine right here to do its thing
431 result
= gEngine().update(target
, flags
, ctx
);
433 // relay the question to our daemon for consideration
434 result
= xpcEngineUpdate(target
, flags
, ctx
);
437 if (__esp_enabled() && (flags
& kSecAssessmentFlagDirect
)) {
438 CFTemp
<CFDictionaryRef
> dict("{target=%O, flags=%d, context=%O, outcome=%O}", target
, flags
, context
, (CFDictionaryRef
)result
);
439 __esp_notify_ns("cs-assessment-update", (void *)(CFDictionaryRef
)dict
);
442 traceUpdate(target
, context
, result
);
443 return result
.yield();
445 END_CSAPI_ERRORS1(false)
450 // The fcntl of System Policies.
451 // For those very special requests.
453 Boolean
SecAssessmentControl(CFStringRef control
, void *arguments
, CFErrorRef
*errors
)
457 CFTemp
<CFDictionaryRef
> dict("{control=%O}", control
);
458 esp_do_check("cs-assessment-control", dict
);
460 if (CFEqual(control
, CFSTR("ui-enable"))) {
462 MessageTrace
trace("com.apple.security.assessment.state", "enable");
463 trace
.send("enable assessment outcomes");
465 } else if (CFEqual(control
, CFSTR("ui-disable"))) {
466 setAssessment(false);
467 MessageTrace
trace("com.apple.security.assessment.state", "disable");
468 trace
.send("disable assessment outcomes");
470 } else if (CFEqual(control
, CFSTR("ui-status"))) {
471 CFBooleanRef
&result
= *(CFBooleanRef
*)(arguments
);
472 if (overrideAssessment())
473 result
= kCFBooleanFalse
;
475 result
= kCFBooleanTrue
;
477 } else if (CFEqual(control
, CFSTR("ui-enable-devid"))) {
478 CFTemp
<CFDictionaryRef
> ctx("{%O=%s}", kSecAssessmentUpdateKeyLabel
, "Developer ID");
479 if (CFDictionaryRef result
= gEngine().enable(NULL
, kAuthorityInvalid
, kSecCSDefaultFlags
, ctx
))
481 MessageTrace
trace("com.apple.security.assessment.state", "enable-devid");
482 trace
.send("enable Developer ID approval");
484 } else if (CFEqual(control
, CFSTR("ui-disable-devid"))) {
485 CFTemp
<CFDictionaryRef
> ctx("{%O=%s}", kSecAssessmentUpdateKeyLabel
, "Developer ID");
486 if (CFDictionaryRef result
= gEngine().disable(NULL
, kAuthorityInvalid
, kSecCSDefaultFlags
, ctx
))
488 MessageTrace
trace("com.apple.security.assessment.state", "disable-devid");
489 trace
.send("disable Developer ID approval");
491 } else if (CFEqual(control
, CFSTR("ui-get-devid"))) {
492 CFBooleanRef
&result
= *(CFBooleanRef
*)(arguments
);
493 if (gEngine().value
<int>("SELECT disabled FROM authority WHERE label = 'Developer ID';", true))
494 result
= kCFBooleanFalse
;
496 result
= kCFBooleanTrue
;
498 } else if (CFEqual(control
, CFSTR("ui-record-reject"))) {
499 // send this through syspolicyd for update validation
500 xpcEngineRecord(CFDictionaryRef(arguments
));
502 } else if (CFEqual(control
, CFSTR("ui-record-reject-local"))) {
503 // perform the local operation (requires root)
504 gEngine().recordFailure(CFDictionaryRef(arguments
));
506 } else if (CFEqual(control
, CFSTR("ui-recall-reject"))) {
507 // no special privileges required for this, so read directly
508 CFDictionaryRef
&result
= *(CFDictionaryRef
*)(arguments
);
509 CFRef
<CFDataRef
> infoData
= cfLoadFile(lastRejectFile
);
511 result
= makeCFDictionaryFrom(infoData
);
516 MacOSError::throwMe(errSecCSInvalidAttributeValues
);
518 END_CSAPI_ERRORS1(false)