2 * Copyright (c) 2008,2010-2013 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@
26 #include <utilities/debugging.h>
28 #include <AssertMacros.h>
29 #include <CoreFoundation/CFRuntime.h>
30 #include <IOKit/IOKitLib.h>
31 #include <IOKit/IOCFUnserialize.h>
32 #include <System/sys/codesign.h>
33 #include <bsm/libbsm.h>
36 #include <utilities/SecCFWrappers.h>
42 #include <sys/sysctl.h>
51 /* Track whether we've loaded entitlements independently since after the
52 * load, entitlements may legitimately be NULL */
53 Boolean entitlementsLoaded
;
54 CFDictionaryRef entitlements
;
56 /* for debugging only, shown by debugDescription */
60 static bool check_task(SecTaskRef task
) {
61 return SecTaskGetTypeID() == CFGetTypeID(task
);
64 static void SecTaskFinalize(CFTypeRef cfTask
)
66 SecTaskRef task
= (SecTaskRef
) cfTask
;
68 if (task
->entitlements
!= NULL
) {
69 CFRelease(task
->entitlements
);
70 task
->entitlements
= NULL
;
75 // Define PRIdPID (proper printf format string for pid_t)
76 #define PRIdPID PRId32
78 static CFStringRef
SecTaskCopyDebugDescription(CFTypeRef cfTask
)
80 SecTaskRef task
= (SecTaskRef
) cfTask
;
82 if (task
->pid_self
==-1) {
83 audit_token_to_au32(task
->token
, NULL
, NULL
, NULL
, NULL
, NULL
, &pid
, NULL
, NULL
);
89 #define MAX_PROCNAME 32
90 char task_name
[MAX_PROCNAME
+ 1] = {};
91 proc_name(pid
, task_name
, MAX_PROCNAME
);
93 const char *task_name
;
94 int mib
[] = {CTL_KERN
, KERN_PROC
, KERN_PROC_PID
, pid
};
96 size_t len
= sizeof(kp
);
97 if (sysctl(mib
, 4, &kp
, &len
, NULL
, 0) == -1 || len
== 0)
98 task_name
= strerror(errno
);
100 task_name
= kp
.kp_proc
.p_comm
;
103 return CFStringCreateWithFormat(CFGetAllocator(task
), NULL
, CFSTR("%s[%" PRIdPID
"]/%d#%d LF=%d"), task_name
, pid
,
104 task
->entitlementsLoaded
, task
->entitlements
? (int)CFDictionaryGetCount(task
->entitlements
) : -1, task
->lastFailure
);
107 CFGiblisWithFunctions(SecTask
, NULL
, NULL
, SecTaskFinalize
, NULL
, NULL
, NULL
, SecTaskCopyDebugDescription
, NULL
, NULL
, NULL
)
109 static SecTaskRef
init_task_ref(CFAllocatorRef allocator
)
111 CFIndex extra
= sizeof(struct __SecTask
) - sizeof(CFRuntimeBase
);
112 return (SecTaskRef
) _CFRuntimeCreateInstance(allocator
, SecTaskGetTypeID(), extra
, NULL
);
115 SecTaskRef
SecTaskCreateFromSelf(CFAllocatorRef allocator
)
117 SecTaskRef task
= init_task_ref(allocator
);
120 memset(&task
->token
, 0, sizeof(task
->token
));
121 task
->entitlementsLoaded
= false;
122 task
->entitlements
= NULL
;
123 task
->pid_self
= getpid();
129 SecTaskRef
SecTaskCreateWithAuditToken(CFAllocatorRef allocator
, audit_token_t token
)
131 SecTaskRef task
= init_task_ref(allocator
);
134 memcpy(&task
->token
, &token
, sizeof(token
));
135 task
->entitlementsLoaded
= false;
136 task
->entitlements
= NULL
;
149 csops_task(SecTaskRef task
, int ops
, void *blob
, size_t size
)
152 if (task
->pid_self
==-1) {
154 audit_token_to_au32(task
->token
, NULL
, NULL
, NULL
, NULL
, NULL
, &pid
, NULL
, NULL
);
155 rc
= csops_audittoken(pid
, ops
, blob
, size
, &task
->token
);
158 rc
= csops(task
->pid_self
, ops
, blob
, size
);
159 task
->lastFailure
= (rc
== -1) ? errno
: 0;
164 SecTaskCopySigningIdentifier(SecTaskRef task
, CFErrorRef
*error
)
166 CFStringRef signingId
= NULL
;
168 struct csheader header
;
172 ret
= csops_task(task
, CS_OPS_IDENTITY
, &header
, sizeof(header
));
173 if (ret
!= -1 || errno
!= ERANGE
)
176 bufferlen
= ntohl(header
.length
);
177 /* check for insane values */
178 if (bufferlen
> 1024 * 1024 || bufferlen
< 8) {
182 data
= malloc(bufferlen
+ 1);
187 ret
= csops_task(task
, CS_OPS_IDENTITY
, data
, bufferlen
);
192 data
[bufferlen
] = '\0';
194 signingId
= CFStringCreateWithCString(NULL
, data
+ 8, kCFStringEncodingUTF8
);
200 *error
= CFErrorCreate(NULL
, kCFErrorDomainPOSIX
, ret
, NULL
);
206 SecTaskGetCodeSignStatus(SecTaskRef task
)
209 if (csops_task(task
, CS_OPS_STATUS
, &flags
, sizeof(flags
)) != 0)
214 static bool SecTaskLoadEntitlements(SecTaskRef task
, CFErrorRef
*error
)
216 CFMutableDictionaryRef entitlements
= NULL
;
217 struct csheader header
;
218 uint8_t *buffer
= NULL
;
223 ret
= csops_task(task
, CS_OPS_ENTITLEMENTS_BLOB
, &header
, sizeof(header
));
224 /* Any other combination means no entitlements */
226 if (errno
!= ERANGE
) {
227 int entitlementErrno
= errno
;
229 uint32_t cs_flags
= -1;
230 if (-1 == csops_task(task
, CS_OPS_STATUS
, &cs_flags
, sizeof(cs_flags
))) {
231 syslog(LOG_NOTICE
, "Failed to get cs_flags, error=%d", errno
);
234 if (cs_flags
!= 0) { // was signed
235 syslog(LOG_NOTICE
, "SecTaskLoadEntitlements failed error=%d cs_flags=%x, task->pid_self=%d", entitlementErrno
, cs_flags
, task
->pid_self
); // to ease diagnostics
237 CFStringRef description
= SecTaskCopyDebugDescription(task
);
238 char *descriptionBuf
= NULL
;
239 CFIndex descriptionSize
= CFStringGetLength(description
) * 4;
240 descriptionBuf
= (char *)malloc(descriptionSize
);
241 if (!CFStringGetCString(description
, descriptionBuf
, descriptionSize
, kCFStringEncodingUTF8
)) {
242 descriptionBuf
[0] = 0;
245 syslog(LOG_NOTICE
, "SecTaskCopyDebugDescription: %s", descriptionBuf
);
246 CFRelease(description
);
247 free(descriptionBuf
);
249 task
->lastFailure
= entitlementErrno
; // was overwritten by csops_task(CS_OPS_STATUS) above
251 // EINVAL is what the kernel says for unsigned code, so we'll have to let that pass
252 if (entitlementErrno
== EINVAL
) {
253 task
->entitlementsLoaded
= true;
256 ret
= entitlementErrno
; // what really went wrong
257 goto out
; // bail out
259 bufferlen
= ntohl(header
.length
);
260 /* check for insane values */
261 if (bufferlen
> 1024 * 1024 || bufferlen
< 8) {
265 buffer
= malloc(bufferlen
);
266 if (buffer
== NULL
) {
270 ret
= csops_task(task
, CS_OPS_ENTITLEMENTS_BLOB
, buffer
, bufferlen
);
276 CFDataRef data
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, buffer
+8, bufferlen
-8, kCFAllocatorNull
);
277 entitlements
= (CFMutableDictionaryRef
) CFPropertyListCreateWithData(kCFAllocatorDefault
, data
, kCFPropertyListMutableContainers
, NULL
, error
);
280 if((entitlements
==NULL
) || (CFGetTypeID(entitlements
)!=CFDictionaryGetTypeID())){
281 ret
= EDOM
; // don't use EINVAL here; it conflates problems with syscall error returns
286 task
->entitlements
= entitlements
? CFRetain(entitlements
) : NULL
;
287 task
->entitlementsLoaded
= true;
291 CFRelease(entitlements
);
294 if (ret
&& error
&& *error
==NULL
)
295 *error
= CFErrorCreate(NULL
, kCFErrorDomainPOSIX
, ret
, NULL
);
300 CFTypeRef
SecTaskCopyValueForEntitlement(SecTaskRef task
, CFStringRef entitlement
, CFErrorRef
*error
)
302 CFTypeRef value
= NULL
;
303 require(check_task(task
), out
);
305 /* Load entitlements if necessary */
306 if (task
->entitlementsLoaded
== false) {
307 require_quiet(SecTaskLoadEntitlements(task
, error
), out
);
310 if (task
->entitlements
!= NULL
) {
311 value
= CFDictionaryGetValue(task
->entitlements
, entitlement
);
313 /* Return something the caller must release */
322 CFDictionaryRef
SecTaskCopyValuesForEntitlements(SecTaskRef task
, CFArrayRef entitlements
, CFErrorRef
*error
)
324 CFMutableDictionaryRef values
= NULL
;
325 require(check_task(task
), out
);
327 /* Load entitlements if necessary */
328 if (task
->entitlementsLoaded
== false) {
329 SecTaskLoadEntitlements(task
, error
);
332 /* Iterate over the passed in entitlements, populating the dictionary
333 * If entitlements were loaded but none were present, return an empty
335 if (task
->entitlementsLoaded
== true) {
337 CFIndex i
, count
= CFArrayGetCount(entitlements
);
338 values
= CFDictionaryCreateMutable(CFGetAllocator(task
), count
, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
339 if (task
->entitlements
!= NULL
) {
340 for (i
= 0; i
< count
; i
++) {
341 CFStringRef entitlement
= CFArrayGetValueAtIndex(entitlements
, i
);
342 CFTypeRef value
= CFDictionaryGetValue(task
->entitlements
, entitlement
);
344 CFDictionarySetValue(values
, entitlement
, value
);