]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/audit_sessionport.c
xnu-6153.141.1.tar.gz
[apple/xnu.git] / osfmk / kern / audit_sessionport.c
CommitLineData
b0d623f7
A
1/*
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
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>
6d2010ae 33#include <libkern/OSAtomic.h>
b0d623f7
A
34
35#if CONFIG_AUDIT
36/*
37 * audit_session_mksend
38 *
6d2010ae 39 * Description: Obtain a send right for given audit session.
b0d623f7
A
40 *
41 * Parameters: *aia_p Audit session information to assosiate with
0a7de745
A
42 * the new port.
43 * *sessionport Pointer to the current session port. This may
44 * actually be set to IPC_PORT_NULL.
45 *
46 * Returns: !NULL Resulting send right.
47 * NULL Failed to allocate port (due to lack of memory
48 * resources).
b0d623f7 49 *
6d2010ae
A
50 * Assumptions: Caller holds a reference on the session during the call.
51 * If there were no outstanding send rights against the port,
52 * hold a reference on the session and arm a new no-senders
53 * notification to determine when to release that reference.
54 * Otherwise, by creating an additional send right, we share
55 * the port's reference until all send rights go away.
b0d623f7
A
56 */
57ipc_port_t
58audit_session_mksend(struct auditinfo_addr *aia_p, ipc_port_t *sessionport)
59{
cb323159
A
60 audit_session_aiaref(aia_p);
61 if (!ipc_kobject_make_send_lazy_alloc_port(sessionport,
62 (ipc_kobject_t)aia_p, IKOT_AU_SESSIONPORT)) {
63 audit_session_aiaunref(aia_p);
b0d623f7 64 }
b0d623f7 65
cb323159 66 return *sessionport;
b0d623f7
A
67}
68
69
70/*
71 * audit_session_porttoaia
72 *
73 * Description: Obtain the audit session info associated with the given port.
0a7de745 74 *
b0d623f7
A
75 * Parameters: port A Mach port.
76 *
77 * Returns: NULL The given Mach port did not reference audit
0a7de745 78 * session info.
b0d623f7
A
79 * !NULL The audit session info that is associated with
80 * the Mach port.
81 *
82 * Notes: The caller must have a reference on the sessionport.
83 */
84struct auditinfo_addr *
85audit_session_porttoaia(ipc_port_t port)
86{
87 struct auditinfo_addr *aia_p = NULL;
88
89 if (IP_VALID(port)) {
90 ip_lock(port);
6d2010ae 91 if (IKOT_AU_SESSIONPORT == ip_kotype(port)) {
cb323159 92 require_ip_active(port);
ea3f0419 93 aia_p = (struct auditinfo_addr *)ip_get_kobject(port);
6d2010ae 94 }
b0d623f7 95 ip_unlock(port);
6d2010ae 96 }
b0d623f7 97
0a7de745 98 return aia_p;
b0d623f7
A
99}
100
101
102/*
103 * audit_session_nosenders
104 *
105 * Description: Handle a no-senders notification for a sessionport.
106 *
107 * Parameters: msg A Mach no-senders notification message.
108 *
109 * Notes: It is possible that new send rights are created after a
cb323159
A
110 * no-senders notification has been sent, but they will be protected
111 * by another aia reference.
b0d623f7
A
112 */
113void
114audit_session_nosenders(mach_msg_header_t *msg)
115{
116 mach_no_senders_notification_t *notification = (void *)msg;
117 ipc_port_t port = notification->not_header.msgh_remote_port;
b0d623f7
A
118 struct auditinfo_addr *port_aia_p = NULL;
119
cb323159 120 require_ip_active(port);
6d2010ae 121 assert(IKOT_AU_SESSIONPORT == ip_kotype(port));
ea3f0419 122 port_aia_p = (struct auditinfo_addr *)ip_get_kobject(port);
6d2010ae
A
123 assert(NULL != port_aia_p);
124
6d2010ae
A
125 audit_session_aiaunref(port_aia_p);
126}
127
128void
129audit_session_portdestroy(ipc_port_t *sessionport)
130{
131 ipc_port_t port = *sessionport;
132
133 if (IP_VALID(port)) {
cb323159 134 require_ip_active(port);
6d2010ae
A
135 assert(IKOT_AU_SESSIONPORT == ip_kotype(port));
136 ipc_kobject_set_atomically(port, IKO_NULL, IKOT_NONE);
137 ipc_port_dealloc_kernel(port);
138 *sessionport = IP_NULL;
139 }
b0d623f7
A
140}
141#endif /* CONFIG_AUDIT */