2 * Copyright (c) 2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #include <mach/mach_types.h>
29 #include <mach/notify.h>
30 #include <ipc/ipc_port.h>
31 #include <kern/ipc_kobject.h>
32 #include <kern/audit_sessionport.h>
36 * audit_session_mksend
38 * Description: Obtain a send right for given audit session information.
40 * Parameters: *aia_p Audit session information to assosiate with
42 * *sessionport Pointer to the current session port. This may
43 * actually be set to IPC_PORT_NULL.
45 * Returns: !NULL Resulting send right.
46 * NULL Failed to allocate port (due to lack of memory
49 * *sessionport The session port that may have been allocated.
51 * Notes: On return, sendport will be set to the new send right on success,
52 * or null/dead on error.
55 audit_session_mksend(struct auditinfo_addr
*aia_p
, ipc_port_t
*sessionport
)
57 ipc_port_t notifyport
;
58 ipc_port_t sendport
= IPC_PORT_NULL
;
61 * If we have an existing, active session port then use it.
63 sendport
= ipc_port_make_send(*sessionport
);
64 if (IP_VALID(sendport
)) {
66 if (ip_active(sendport
) &&
67 IKOT_AU_SESSIONPORT
== ip_kotype(sendport
)) {
72 ipc_port_release_send(sendport
);
76 * Otherwise, create a new one for this session.
78 *sessionport
= ipc_port_alloc_kernel();
79 if (IP_VALID(*sessionport
)) {
80 ipc_kobject_set(*sessionport
, (ipc_kobject_t
)aia_p
,
83 /* Request a no-senders notification. */
84 notifyport
= ipc_port_make_sonce(*sessionport
);
85 ip_lock(*sessionport
);
86 /* unlocked by ipc_port_nsrequest */
87 ipc_port_nsrequest(*sessionport
, 1, notifyport
, ¬ifyport
);
89 sendport
= ipc_port_make_send(*sessionport
);
96 * audit_session_porttoaia
98 * Description: Obtain the audit session info associated with the given port.
100 * Parameters: port A Mach port.
102 * Returns: NULL The given Mach port did not reference audit
104 * !NULL The audit session info that is associated with
107 * Notes: The caller must have a reference on the sessionport.
109 struct auditinfo_addr
*
110 audit_session_porttoaia(ipc_port_t port
)
112 struct auditinfo_addr
*aia_p
= NULL
;
114 if (IP_VALID(port
)) {
116 if (ip_active(port
) && IKOT_AU_SESSIONPORT
== ip_kotype(port
))
117 aia_p
= (struct auditinfo_addr
*)port
->ip_kobject
;
126 * audit_session_nosenders
128 * Description: Handle a no-senders notification for a sessionport.
130 * Parameters: msg A Mach no-senders notification message.
132 * Notes: It is possible that new send rights are created after a
133 * no-senders notification has been sent (i.e. via audit_session_mksend).
134 * We check the port's mscount against the notification's not_count
135 * to detect when this happens, and re-arm the notification in that
138 * In the normal case (no new senders), we first mark the port
139 * as dying by setting its object type to IKOT_NONE so that
140 * audit_session_mksend will no longer use it to create
141 * additional send rights. We can then safely call
142 * audit_session_port_destroy with no locks.
145 audit_session_nosenders(mach_msg_header_t
*msg
)
147 mach_no_senders_notification_t
*notification
= (void *)msg
;
148 ipc_port_t port
= notification
->not_header
.msgh_remote_port
;
149 ipc_port_t notifyport
;
150 struct auditinfo_addr
*port_aia_p
= NULL
;
155 if (ip_active(port
) && IKOT_AU_SESSIONPORT
== ip_kotype(port
)) {
156 port_aia_p
= (struct auditinfo_addr
*)port
->ip_kobject
;
157 assert(NULL
!= port_aia_p
);
158 if (port
->ip_mscount
<= notification
->not_count
)
159 ipc_kobject_set_atomically(port
, IKO_NULL
, IKOT_NONE
);
161 /* re-arm the notification */
163 notifyport
= ipc_port_make_sonce(port
);
165 /* unlocked by ipc_port_nsrequest */
166 ipc_port_nsrequest(port
, port
->ip_mscount
, notifyport
,
172 if (NULL
!= port_aia_p
)
173 audit_session_portaiadestroy(port_aia_p
);
174 ipc_port_dealloc_kernel(port
);
176 #endif /* CONFIG_AUDIT */