2 * Copyright (c) 2011-2012 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@
25 #include "policyengine.h"
26 #include <Security/CodeSigning.h>
27 #include <security_utilities/cfutilities.h>
28 #include <security_utilities/cfmunge.h>
29 #include <security_utilities/blob.h>
30 #include <security_utilities/logging.h>
31 #include <security_utilities/simpleprefs.h>
32 #include <security_utilities/logging.h>
33 #include "csdatabase.h"
35 #include <dispatch/dispatch.h>
36 #include <sys/types.h>
41 namespace CodeSigning
{
44 using namespace SQLite
;
48 // Determine the database path
50 static const char *dbPath()
52 if (const char *s
= getenv("SYSPOLICYDATABASE"))
54 return defaultDatabase
;
59 // Help mapping API-ish CFString keys to more convenient internal enumerations
62 const CFStringRef
&cstring
;
66 static uint
mapEnum(CFDictionaryRef context
, CFStringRef attr
, const StringMap
*map
, uint value
= 0)
69 if (CFTypeRef value
= CFDictionaryGetValue(context
, attr
))
70 for (const StringMap
*mp
= map
; mp
->cstring
; ++mp
)
71 if (CFEqual(mp
->cstring
, value
))
72 return mp
->enumeration
;
76 static const StringMap mapType
[] = {
77 { kSecAssessmentOperationTypeExecute
, kAuthorityExecute
},
78 { kSecAssessmentOperationTypeInstall
, kAuthorityInstall
},
79 { kSecAssessmentOperationTypeOpenDocument
, kAuthorityOpenDoc
},
83 AuthorityType
typeFor(CFDictionaryRef context
, AuthorityType type
/* = kAuthorityInvalid */)
85 return mapEnum(context
, kSecAssessmentContextKeyOperation
, mapType
, type
);
88 CFStringRef
typeNameFor(AuthorityType type
)
90 for (const StringMap
*mp
= mapType
; mp
->cstring
; ++mp
)
91 if (type
== mp
->enumeration
)
93 return CFStringCreateWithFormat(NULL
, NULL
, CFSTR("type %d"), type
);
100 PolicyDatabase::PolicyDatabase(const char *path
, int flags
)
101 : SQLite::Database(path
? path
: dbPath(), flags
),
102 mLastExplicitCheck(0)
104 // sqlite3 doesn't do foreign key support by default, have to turn this on per connection
105 SQLite::Statement
foreign(*this, "PRAGMA foreign_keys = true");
108 // Try upgrade processing if we may be open for write.
109 // Ignore any errors (we may have been downgraded to read-only)
110 // and try again later.
111 if (openFlags() & SQLITE_OPEN_READWRITE
)
114 installExplicitSet(gkeAuthFile
, gkeSigsFile
);
119 PolicyDatabase::~PolicyDatabase()
124 // Quick-check the cache for a match.
125 // Return true on a cache hit, false on failure to confirm a hit for any reason.
127 bool PolicyDatabase::checkCache(CFURLRef path
, AuthorityType type
, CFMutableDictionaryRef result
)
129 // we currently don't use the cache for anything but execution rules
130 if (type
!= kAuthorityExecute
)
133 CFRef
<SecStaticCodeRef
> code
;
134 MacOSError::check(SecStaticCodeCreateWithPath(path
, kSecCSDefaultFlags
, &code
.aref()));
135 if (SecStaticCodeCheckValidity(code
, kSecCSBasicValidateOnly
, NULL
) != noErr
)
136 return false; // quick pass - any error is a cache miss
137 CFRef
<CFDictionaryRef
> info
;
138 MacOSError::check(SecCodeCopySigningInformation(code
, kSecCSDefaultFlags
, &info
.aref()));
139 CFDataRef cdHash
= CFDataRef(CFDictionaryGetValue(info
, kSecCodeInfoUnique
));
141 // check the cache table for a fast match
142 SQLite::Statement
cached(*this, "SELECT object.allow, authority.label, authority FROM object, authority"
143 " WHERE object.authority = authority.id AND object.type = :type AND object.hash = :hash AND authority.disabled = 0"
144 " AND JULIANDAY('now') < object.expires;");
145 cached
.bind(":type").integer(type
);
146 cached
.bind(":hash") = cdHash
;
147 if (cached
.nextRow()) {
148 bool allow
= int(cached
[0]);
149 const char *label
= cached
[1];
150 SQLite::int64 auth
= cached
[2];
151 SYSPOLICY_ASSESS_CACHE_HIT();
153 // If its allowed, lets do a full validation unless if
154 // we are overriding the assessement, since that force
155 // the verdict to 'pass' at the end
157 if (allow
&& !overrideAssessment())
158 MacOSError::check(SecStaticCodeCheckValidity(code
, kSecCSDefaultFlags
, NULL
));
160 cfadd(result
, "{%O=%B}", kSecAssessmentAssessmentVerdict
, allow
);
161 PolicyEngine::addAuthority(result
, label
, auth
, kCFBooleanTrue
);
169 // Purge the object cache of all expired entries.
170 // These are meant to run within the caller's transaction.
172 void PolicyDatabase::purgeAuthority()
174 SQLite::Statement
cleaner(*this,
175 "DELETE FROM authority WHERE expires <= JULIANDAY('now');");
179 void PolicyDatabase::purgeObjects()
181 SQLite::Statement
cleaner(*this,
182 "DELETE FROM object WHERE expires <= JULIANDAY('now');");
186 void PolicyDatabase::purgeObjects(double priority
)
188 SQLite::Statement
cleaner(*this,
189 "DELETE FROM object WHERE expires <= JULIANDAY('now') OR (SELECT priority FROM authority WHERE id = object.authority) <= :priority;");
190 cleaner
.bind(":priority") = priority
;
196 // Database migration
198 std::string
PolicyDatabase::featureLevel(const char *name
)
200 SQLite::Statement
feature(*this, "SELECT value FROM feature WHERE name=:name");
201 feature
.bind(":name") = name
;
202 if (feature
.nextRow())
203 return feature
[0].string();
205 return ""; // new feature (no level)
208 void PolicyDatabase::addFeature(const char *name
, const char *value
, const char *remarks
)
210 SQLite::Statement
feature(*this, "INSERT OR REPLACE INTO feature (name,value,remarks) VALUES(:name, :value, :remarks)");
211 feature
.bind(":name") = name
;
212 feature
.bind(":value") = value
;
213 feature
.bind(":remarks") = remarks
;
217 void PolicyDatabase::simpleFeature(const char *feature
, void (^perform
)())
219 if (!hasFeature(feature
)) {
220 addFeature(feature
, "upgraded", "upgraded");
221 SQLite::Transaction
update(*this);
227 void PolicyDatabase::simpleFeature(const char *feature
, const char *sql
)
229 simpleFeature(feature
, ^{
230 SQLite::Statement
perform(*this, sql
);
231 addFeature(feature
, "upgraded", "upgraded");
237 void PolicyDatabase::upgradeDatabase()
239 simpleFeature("bookmarkhints",
240 "CREATE TABLE bookmarkhints ("
241 " id INTEGER PRIMARY KEY AUTOINCREMENT, "
243 " authority INTEGER NOT NULL"
244 " REFERENCES authority(id) ON DELETE CASCADE"
247 simpleFeature("codesignedpackages", ^{
248 SQLite::Statement
update(*this,
250 " SET requirement = 'anchor apple generic and certificate 1[field.1.2.840.113635.100.6.2.6] exists and "
251 "(certificate leaf[field.1.2.840.113635.100.6.1.14] or certificate leaf[field.1.2.840.113635.100.6.1.13])'"
252 " WHERE type = 2 and label = 'Developer ID' and flags & :flag");
253 update
.bind(":flag") = kAuthorityFlagDefault
;
257 simpleFeature("filter_unsigned",
258 "ALTER TABLE authority ADD COLUMN filter_unsigned TEXT NULL"
261 simpleFeature("strict_apple_installer", ^{
262 SQLite::Statement
update(*this,
264 " SET requirement = 'anchor apple generic and certificate 1[subject.CN] = \"Apple Software Update Certification Authority\"'"
265 " WHERE flags & :flag AND label = 'Apple Installer'");
266 update
.bind(":flag") = kAuthorityFlagDefault
;
268 SQLite::Statement
add(*this,
269 "INSERT INTO authority (type, label, flags, requirement)"
270 " VALUES (2, 'Mac App Store', :flags, 'anchor apple generic and certificate leaf[field.1.2.840.113635.100.6.1.10] exists')");
271 add
.bind(":flags") = kAuthorityFlagDefault
;
278 // Install Gatekeeper override (GKE) data.
279 // The arguments are paths to the authority and signature files.
281 void PolicyDatabase::installExplicitSet(const char *authfile
, const char *sigfile
)
283 // only try this every gkeCheckInterval seconds
284 time_t now
= time(NULL
);
285 if (mLastExplicitCheck
+ gkeCheckInterval
> now
)
287 mLastExplicitCheck
= now
;
290 if (CFRef
<CFDataRef
> authData
= cfLoadFile(authfile
)) {
291 CFDictionary
auth(CFRef
<CFDictionaryRef
>(makeCFDictionaryFrom(authData
)), errSecCSDbCorrupt
);
292 CFDictionaryRef content
= auth
.get
<CFDictionaryRef
>(CFSTR("authority"));
293 std::string authUUID
= cfString(auth
.get
<CFStringRef
>(CFSTR("uuid")));
294 if (authUUID
.empty()) {
295 secdebug("gkupgrade", "no uuid in auth file; ignoring gke.auth");
299 SQLite::Statement
uuidQuery(*this, "SELECT value FROM feature WHERE name='gke'");
300 if (uuidQuery
.nextRow())
301 dbUUID
= (const char *)uuidQuery
[0];
302 if (dbUUID
== authUUID
) {
303 secdebug("gkupgrade", "gke.auth already present, ignoring");
306 Syslog::notice("loading GKE %s (replacing %s)", authUUID
.c_str(), dbUUID
.empty() ? "nothing" : dbUUID
.c_str());
308 // first, load code signatures. This is pretty much idempotent
310 if (FILE *sigs
= fopen(sigfile
, "r")) {
312 while (const BlobCore
*blob
= BlobCore::readBlob(sigs
)) {
313 signatureDatabaseWriter().storeCode(blob
, "<remote>");
316 secdebug("gkupgrade", "%d detached signature(s) loaded from override data", count
);
320 // start transaction (atomic from here on out)
321 SQLite::Transaction
loadAuth(*this, SQLite::Transaction::exclusive
, "GKE_Upgrade");
323 // purge prior authority data
324 SQLite::Statement
purge(*this, "DELETE FROM authority WHERE flags & :flag");
325 purge
.bind(":flag") = kAuthorityFlagWhitelist
;
329 CFIndex count
= CFDictionaryGetCount(content
);
330 CFStringRef keys
[count
];
331 CFDictionaryRef values
[count
];
332 CFDictionaryGetKeysAndValues(content
, (const void **)keys
, (const void **)values
);
334 SQLite::Statement
insert(*this, "INSERT INTO authority (type, allow, requirement, label, filter_unsigned, flags, remarks)"
335 " VALUES (:type, 1, :requirement, 'GKE', :filter, :flags, :path)");
336 for (CFIndex n
= 0; n
< count
; n
++) {
337 CFDictionary
info(values
[n
], errSecCSDbCorrupt
);
339 insert
.bind(":type") = cfString(info
.get
<CFStringRef
>(CFSTR("type")));
340 insert
.bind(":path") = cfString(info
.get
<CFStringRef
>(CFSTR("path")));
341 insert
.bind(":requirement") = "cdhash H\"" + cfString(info
.get
<CFStringRef
>(CFSTR("cdhash"))) + "\"";
342 insert
.bind(":filter") = cfString(info
.get
<CFStringRef
>(CFSTR("screen")));
343 insert
.bind(":flags") = kAuthorityFlagWhitelist
;
347 // update version and commit
348 addFeature("gke", authUUID
.c_str(), "gke loaded");
352 secdebug("gkupgrade", "exception during GKE upgrade");
358 // Check the override-enable master flag
360 #define SP_ENABLE_KEY CFSTR("enabled")
361 #define SP_ENABLED CFSTR("yes")
362 #define SP_DISABLED CFSTR("no")
364 bool overrideAssessment()
366 static bool enabled
= true;
367 static dispatch_once_t once
;
368 static int token
= -1;
369 static int have_token
= 0;
370 static dispatch_queue_t queue
;
373 if (have_token
&& notify_check(token
, &check
) == NOTIFY_STATUS_OK
&& !check
)
376 dispatch_once(&once
, ^{
377 if (notify_register_check(kNotifySecAssessmentMasterSwitch
, &token
) == NOTIFY_STATUS_OK
)
379 queue
= dispatch_queue_create("com.apple.SecAssessment.assessment", NULL
);
382 dispatch_sync(queue
, ^{
383 /* upgrade configuration from emir, ignore all error since we might not be able to write to */
384 if (::access(visibleSecurityFlagFile
, F_OK
) == 0) {
387 ::unlink(visibleSecurityFlagFile
);
395 Dictionary
* prefsDict
= Dictionary::CreateDictionary(prefsFile
);
396 if (prefsDict
== NULL
)
399 CFStringRef value
= prefsDict
->getStringValue(SP_ENABLE_KEY
);
400 if (value
&& CFStringCompare(value
, SP_DISABLED
, 0) == 0)
412 void setAssessment(bool masterSwitch
)
414 MutableDictionary
*prefsDict
= MutableDictionary::CreateMutableDictionary(prefsFile
);
415 if (prefsDict
== NULL
)
416 prefsDict
= new MutableDictionary::MutableDictionary();
417 prefsDict
->setValue(SP_ENABLE_KEY
, masterSwitch
? SP_ENABLED
: SP_DISABLED
);
418 prefsDict
->writePlistToFile(prefsFile
);
421 /* make sure permissions is right */
422 ::chmod(prefsFile
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
);
424 notify_post(kNotifySecAssessmentMasterSwitch
);
428 } // end namespace CodeSigning
429 } // end namespace Security