1 /* Copyright (c) 2012-2013 Apple Inc. All Rights Reserved. */
5 #include "authd_private.h"
8 #include "authutilities.h"
11 #include "debugging.h"
14 #import <xpc/private.h>
15 #include <Security/AuthorizationPlugin.h>
17 #include <mach/mach.h>
18 #include <servers/bootstrap.h>
19 #include <bootstrap_priv.h>
21 #define SECURITYAGENT_BOOTSTRAP_NAME_BASE "com.apple.security.agent"
22 #define SECURITYAGENT_LOGINWINDOW_BOOTSTRAP_NAME_BASE "com.apple.security.agent.login"
23 #define AUTHORIZATIONHOST_BOOTSTRAP_NAME_BASE "com.apple.security.authhost"
25 #define UUID_INITIALIZER_FROM_SESSIONID(sessionid) \
26 { 0,0,0,0, 0,0,0,0, 0,0,0,0, (unsigned char)((0xff000000 & (sessionid))>>24), (unsigned char)((0x00ff0000 & (sessionid))>>16), (unsigned char)((0x0000ff00 & (sessionid))>>8), (unsigned char)((0x000000ff & (sessionid))) }
29 __AUTH_BASE_STRUCT_HEADER__
;
35 PluginState pluginState
;
38 xpc_connection_t agentConnection
;
39 dispatch_queue_t eventQueue
;
40 dispatch_queue_t actionQueue
;
46 _agent_finalize(CFTypeRef value
)
48 agent_t agent
= (agent_t
)value
;
50 // If this ever becomes a concurrent queue, then this would need to be a barrier sync
51 dispatch_sync(agent
->eventQueue
, ^{
52 // Mark the agent as dead
53 agent
->pluginState
= dead
;
56 // We're going away, which means no outside references exist. It's safe to dispose of the XPC connection.
57 if (NULL
!= agent
->agentConnection
) {
58 xpc_release(agent
->agentConnection
);
59 agent
->agentConnection
= NULL
;
62 // Now that we've released any XPC connection that may (or may not) be present
63 // it's safe to go ahead and free our memory. This is provided that all other
64 // blocks that were added to the event queue before the axe came down on the
65 // xpc connection have been executed.
67 // If this ever becomes a concurrent queue, then this would need to be a barrier sync
68 dispatch_sync(agent
->eventQueue
, ^{
69 CFReleaseSafe(agent
->hints
);
70 CFReleaseSafe(agent
->context
);
71 CFReleaseSafe(agent
->mech
);
72 CFReleaseSafe(agent
->engine
);
73 dispatch_release(agent
->actionQueue
);
76 dispatch_release(agent
->eventQueue
);
78 LOGD("agent[%i]: _agent_finalize called", agent
->agentPid
);
81 AUTH_TYPE_INSTANCE(agent
,
84 .finalize
= _agent_finalize
,
87 .copyFormattingDesc
= NULL
,
91 static CFTypeID
agent_get_type_id() {
92 static CFTypeID type_id
= _kCFRuntimeNotATypeID
;
93 static dispatch_once_t onceToken
;
95 dispatch_once(&onceToken
, ^{
96 type_id
= _CFRuntimeRegisterClass(&_auth_type_agent
);
103 agent_create(engine_t engine
, mechanism_t mech
, auth_token_t auth
, process_t proc
, bool firstMech
)
105 bool doSwitchAudit
= false;
106 bool doSwitchBootstrap
= false;
107 agent_t agent
= NULL
;
109 agent
= (agent_t
)_CFRuntimeCreateInstance(kCFAllocatorDefault
, agent_get_type_id(), AUTH_CLASS_SIZE(agent
), NULL
);
110 require(agent
!= NULL
, done
);
112 agent
->mech
= (mechanism_t
)CFRetain(mech
);
113 agent
->engine
= (engine_t
)CFRetain(engine
);
114 agent
->hints
= auth_items_create();
115 agent
->context
= auth_items_create();
116 agent
->pluginState
= init
;
117 agent
->agentPid
= process_get_pid(proc
);
119 const audit_info_s
*audit_info
= auth_token_get_audit_info(auth
);
121 auditinfo_addr_t tempAddr
;
122 tempAddr
.ai_asid
= audit_info
->asid
;
123 auditon(A_GETSINFO_ADDR
, &tempAddr
, sizeof(tempAddr
));
125 LOGV("agent[%i]: Stored auid %d fetched auid %d", agent
->agentPid
, audit_info
->auid
, tempAddr
.ai_auid
);
126 uid_t auid
= tempAddr
.ai_auid
;
127 uuid_t sessionUUID
= UUID_INITIALIZER_FROM_SESSIONID((uint32_t)audit_info
->asid
);
129 agent
->eventQueue
= dispatch_queue_create("Agent Event Queue", 0);
130 agent
->actionQueue
= dispatch_queue_create("Agent Action Queue", 0);
132 if (!mechanism_is_privileged(mech
)) {
133 if (auid
!= AU_DEFAUDITID
) {
134 // User => regular user-level SecurityAgent
135 agent
->agentConnection
= xpc_connection_create_mach_service(SECURITYAGENT_BOOTSTRAP_NAME_BASE
, NULL
, 0);
136 xpc_connection_set_target_uid(agent
->agentConnection
, auid
);
137 LOGV("agent[%i]: Creating a standard security agent", agent
->agentPid
);
139 // Root session => loginwindow SecurityAgent
140 agent
->agentConnection
= xpc_connection_create_mach_service(SECURITYAGENT_LOGINWINDOW_BOOTSTRAP_NAME_BASE
, NULL
, 0);
141 xpc_connection_set_instance(agent
->agentConnection
, sessionUUID
);
142 LOGV("agent[%i]: Creating a loginwindow security agent", agent
->agentPid
);
143 doSwitchAudit
= true;
144 doSwitchBootstrap
= true;
147 agent
->agentConnection
= xpc_connection_create_mach_service(AUTHORIZATIONHOST_BOOTSTRAP_NAME_BASE
, NULL
, 0);
148 xpc_connection_set_instance(agent
->agentConnection
, sessionUUID
);
149 LOGV("agent[%i]: Creating a standard authhost", agent
->agentPid
);
150 doSwitchAudit
= true;
151 doSwitchBootstrap
= true;
154 // **************** Here's the event handler, since I can never find it
155 xpc_connection_set_target_queue(agent
->agentConnection
, agent
->eventQueue
);
156 xpc_connection_set_event_handler(agent
->agentConnection
, ^(xpc_object_t object
) {
157 char* objectDesc
= xpc_copy_description(object
);
158 LOGV("agent[%i]: global xpc message received %s", agent
->agentPid
, objectDesc
);
161 if (agent
->pluginState
== dead
) {
162 // If the agent is dead for some reason, drop this message before we hurt ourselves.
166 if (xpc_get_type(object
) == XPC_TYPE_DICTIONARY
) {
167 const char *replyType
= xpc_dictionary_get_string(object
, AUTH_XPC_REPLY_METHOD_KEY
);
168 if (0 == strcmp(replyType
, AUTH_XPC_REPLY_METHOD_INTERRUPT
)) {
169 agent
->pluginState
= interrupting
;
170 engine_interrupt_agent(agent
->engine
);
175 xpc_connection_resume(agent
->agentConnection
);
177 xpc_object_t requestObject
= xpc_dictionary_create(NULL
, NULL
, 0);
178 xpc_dictionary_set_string(requestObject
, AUTH_XPC_REQUEST_METHOD_KEY
, AUTH_XPC_REQUEST_METHOD_CREATE
);
179 xpc_dictionary_set_string(requestObject
, AUTH_XPC_PLUGIN_NAME
, mechanism_get_plugin(mech
));
180 xpc_dictionary_set_string(requestObject
, AUTH_XPC_MECHANISM_NAME
, mechanism_get_param(mech
));
182 mach_port_name_t jobPort
;
183 if (audit_info
!= NULL
) {
184 if (0 == audit_session_port(audit_info
->asid
, &jobPort
)) {
185 LOGV("agent[%i]: attaching an audit session port", agent
->agentPid
);
186 xpc_dictionary_set_mach_send(requestObject
, AUTH_XPC_AUDIT_SESSION_PORT
, jobPort
);
188 if (mach_port_mod_refs(mach_task_self(), jobPort
, MACH_PORT_RIGHT_SEND
, -1) != KERN_SUCCESS
) {
189 LOGE("unable to release send right for audit session, leaking");
195 if (doSwitchBootstrap
) {
196 LOGV("agent[%i]: attaching a bootstrap port", agent
->agentPid
);
197 xpc_dictionary_set_mach_send(requestObject
, AUTH_XPC_BOOTSTRAP_PORT
, process_get_bootstrap(proc
));
200 // This loop will be repeated until we can get ahold of a SecurityAgent, or get a fatal error
201 // This will cause us to retry any CONNECTION_INTERRUPTED status messages
203 xpc_object_t object
= xpc_connection_send_message_with_reply_sync(agent
->agentConnection
, requestObject
);
205 if (xpc_get_type(object
) == XPC_TYPE_DICTIONARY
) {
206 const char *replyType
= xpc_dictionary_get_string(object
, AUTH_XPC_REPLY_METHOD_KEY
);
207 if (0 == strcmp(replyType
, AUTH_XPC_REPLY_METHOD_CREATE
)) {
208 agent
->status
= xpc_dictionary_get_uint64(object
, AUTH_XPC_REPLY_RESULT_VALUE
);
209 if (agent
->status
== kAuthorizationResultAllow
) {
210 // Only go here if we have an "allow" from the SecurityAgent (the plugin create might have failed)
211 // This is backwards compatible so that it goes gently into the build, as the default is "allow".
212 agent
->pluginState
= created
;
214 agent
->pluginState
= dead
;
217 } else if (xpc_get_type(object
) == XPC_TYPE_ERROR
) {
218 if (object
== XPC_ERROR_CONNECTION_INVALID
) {
219 agent
->pluginState
= dead
;
223 } while ((agent
->pluginState
== init
) && (firstMech
));
225 xpc_release(requestObject
);
227 if (agent
->pluginState
!= created
) {
236 agent_run(agent_t agent
, auth_items_t hints
, auth_items_t context
, auth_items_t immutable_hints
)
238 xpc_object_t hintsArray
= auth_items_export_xpc(hints
);
239 xpc_object_t contextArray
= auth_items_export_xpc(context
);
240 xpc_object_t immutableHintsArray
= auth_items_export_xpc(immutable_hints
);
242 dispatch_semaphore_t replyWaiter
= dispatch_semaphore_create(0);
243 dispatch_sync(agent
->actionQueue
, ^{
244 if (agent
->pluginState
!= mechinterrupting
) {
245 agent
->pluginState
= current
;
246 agent
->status
= kAuthorizationResultUndefined
; // Set this while the plugin chews on the user input.
248 auth_items_clear(agent
->hints
);
249 auth_items_clear(agent
->context
);
251 xpc_object_t requestObject
= xpc_dictionary_create(NULL
, NULL
, 0);
252 xpc_dictionary_set_string(requestObject
, AUTH_XPC_REQUEST_METHOD_KEY
, AUTH_XPC_REQUEST_METHOD_INVOKE
);
253 xpc_dictionary_set_value(requestObject
, AUTH_XPC_HINTS_NAME
, hintsArray
);
254 xpc_dictionary_set_value(requestObject
, AUTH_XPC_CONTEXT_NAME
, contextArray
);
255 xpc_dictionary_set_value(requestObject
, AUTH_XPC_IMMUTABLE_HINTS_NAME
, immutableHintsArray
);
257 xpc_connection_send_message_with_reply(agent
->agentConnection
, requestObject
, agent
->actionQueue
, ^(xpc_object_t object
){
258 if (xpc_get_type(object
) == XPC_TYPE_DICTIONARY
) {
259 const char *replyType
= xpc_dictionary_get_string(object
, AUTH_XPC_REPLY_METHOD_KEY
);
260 if (0 == strcmp(replyType
, AUTH_XPC_REPLY_METHOD_RESULT
)) {
261 if (agent
->pluginState
== current
) {
262 xpc_object_t xpcContext
= xpc_dictionary_get_value(object
, AUTH_XPC_CONTEXT_NAME
);
263 xpc_object_t xpcHints
= xpc_dictionary_get_value(object
, AUTH_XPC_HINTS_NAME
);
264 auth_items_copy_xpc(agent
->context
, xpcContext
);
265 auth_items_copy_xpc(agent
->hints
, xpcHints
);
266 agent
->status
= xpc_dictionary_get_uint64(object
, AUTH_XPC_REPLY_RESULT_VALUE
);
267 agent
->pluginState
= active
;
270 } else if (xpc_get_type(object
) == XPC_TYPE_ERROR
) {
271 if ((object
== XPC_ERROR_CONNECTION_INVALID
) || (object
== XPC_ERROR_CONNECTION_INTERRUPTED
)) {
272 agent
->pluginState
= dead
;
275 dispatch_semaphore_signal(replyWaiter
);
277 xpc_release(requestObject
);
281 if (agent
->pluginState
== current
) {
282 dispatch_semaphore_wait(replyWaiter
, DISPATCH_TIME_FOREVER
);
284 dispatch_release(replyWaiter
);
286 LOGV("agent[%i]: Finished call to SecurityAgent", agent
->agentPid
);
288 xpc_release(hintsArray
);
289 xpc_release(contextArray
);
290 xpc_release(immutableHintsArray
);
292 return agent
->status
;
296 agent_get_hints(agent_t agent
)
302 agent_get_context(agent_t agent
)
304 return agent
->context
;
308 agent_get_mechanism(agent_t agent
)
314 agent_deactivate(agent_t agent
)
316 xpc_object_t requestObject
= xpc_dictionary_create(NULL
, NULL
, 0);
317 xpc_dictionary_set_string(requestObject
, AUTH_XPC_REQUEST_METHOD_KEY
, AUTH_XPC_REQUEST_METHOD_DEACTIVATE
);
319 agent
->pluginState
= deactivating
;
321 xpc_object_t object
= xpc_connection_send_message_with_reply_sync(agent
->agentConnection
, requestObject
);
322 if (xpc_get_type(object
) == XPC_TYPE_DICTIONARY
) {
323 const char *replyType
= xpc_dictionary_get_string(object
, AUTH_XPC_REPLY_METHOD_KEY
);
324 if (0 == strcmp(replyType
, AUTH_XPC_REPLY_METHOD_DEACTIVATE
)) {
325 agent
->pluginState
= active
; // This is strange, but true. We do actually want to set the state 'active'.
327 } else if (xpc_get_type(object
) == XPC_TYPE_ERROR
) {
328 if ((object
== XPC_ERROR_CONNECTION_INVALID
) || (object
== XPC_ERROR_CONNECTION_INTERRUPTED
)) {
329 agent
->pluginState
= dead
;
333 xpc_release(requestObject
);
336 void agent_destroy(agent_t agent
)
338 xpc_object_t requestObject
= xpc_dictionary_create(NULL
, NULL
, 0);
339 xpc_dictionary_set_string(requestObject
, AUTH_XPC_REQUEST_METHOD_KEY
, AUTH_XPC_REQUEST_METHOD_DESTROY
);
340 xpc_connection_send_message(agent
->agentConnection
, requestObject
);
341 xpc_release(requestObject
);
345 agent_get_state(agent_t agent
)
347 return agent
->pluginState
;
351 agent_notify_interrupt(agent_t agent
)
353 dispatch_sync(agent
->actionQueue
, ^{
354 if (agent
->pluginState
== current
) {
355 xpc_object_t requestObject
= xpc_dictionary_create(NULL
, NULL
, 0);
356 xpc_dictionary_set_string(requestObject
, AUTH_XPC_REQUEST_METHOD_KEY
, AUTH_XPC_REQUEST_METHOD_INTERRUPT
);
357 xpc_connection_send_message(agent
->agentConnection
, requestObject
);
358 xpc_release(requestObject
);
359 } else if (agent
->pluginState
== active
) {
360 agent
->pluginState
= mechinterrupting
;
366 agent_clear_interrupt(agent_t agent
)
368 dispatch_sync(agent
->actionQueue
, ^{
369 if (agent
->pluginState
== mechinterrupting
) {
370 agent
->pluginState
= active
;
376 agent_receive(agent_t __unused agent
)