xref: /xnu-8020.101.4/osfmk/kern/audit_sessionport.c (revision e7776783b89a353188416a9a346c6cdb4928faad)
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>
33 #include <libkern/OSAtomic.h>
34 
35 #if CONFIG_AUDIT
36 /*
37  * audit_session_mksend
38  *
39  * Description: Obtain a send right for given audit session.
40  *
41  * Parameters:	*aia_p		Audit session information to assosiate with
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).
49  *
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.
56  */
57 ipc_port_t
audit_session_mksend(struct auditinfo_addr * aia_p,ipc_port_t * sessionport)58 audit_session_mksend(struct auditinfo_addr *aia_p, ipc_port_t *sessionport)
59 {
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 	    IPC_KOBJECT_ALLOC_NONE, 0)) {
64 		audit_session_aiaunref(aia_p);
65 	}
66 
67 	return *sessionport;
68 }
69 
70 
71 /*
72  * audit_session_porttoaia
73  *
74  * Description: Obtain the audit session info associated with the given port.
75  *
76  * Parameters: port		A Mach port.
77  *
78  * Returns:    NULL		The given Mach port did not reference audit
79  *                              session info.
80  *	       !NULL		The audit session info that is associated with
81  *				the Mach port.
82  *
83  * Notes: The caller must hold an outstanding send-right on the sessionport.
84  */
85 struct auditinfo_addr *
audit_session_porttoaia(ipc_port_t port)86 audit_session_porttoaia(ipc_port_t port)
87 {
88 	struct auditinfo_addr *aia_p = NULL;
89 
90 	if (IP_VALID(port)) {
91 		aia_p = ipc_kobject_get_stable(port, IKOT_AU_SESSIONPORT);
92 	}
93 
94 	return aia_p;
95 }
96 
97 
98 /*
99  * audit_session_no_senders
100  *
101  * Description: Handle a no-senders notification for a sessionport.
102  *
103  * Notes: It is possible that new send rights are created after a
104  *	  no-senders notification has been sent, but they will be protected
105  *	  by another aia reference.
106  */
107 static void
audit_session_no_senders(ipc_port_t port,__unused mach_port_mscount_t mscount)108 audit_session_no_senders(ipc_port_t port, __unused mach_port_mscount_t mscount)
109 {
110 	struct auditinfo_addr *aia_p = NULL;
111 
112 	aia_p = ipc_kobject_get_stable(port, IKOT_AU_SESSIONPORT);
113 	assert(NULL != aia_p);
114 
115 	audit_session_aiaunref(aia_p);
116 }
117 
118 /*
119  * audit_session_portdestroy
120  *
121  * Description: Destroy the kobject associated with the audit_session
122  *
123  * Notes: It is called when there is no outstanding references on the aia
124  *        anymore (it also won't have any outstanding send rights)
125  */
126 void
audit_session_portdestroy(ipc_port_t * sessionport)127 audit_session_portdestroy(ipc_port_t *sessionport)
128 {
129 	ipc_port_t port = *sessionport;
130 
131 	*sessionport = IP_NULL;
132 	if (IP_VALID(port)) {
133 		ipc_kobject_dealloc_port(port, 0, IKOT_AU_SESSIONPORT);
134 	}
135 }
136 
137 IPC_KOBJECT_DEFINE(IKOT_AU_SESSIONPORT,
138     .iko_op_stable     = true,
139     .iko_op_no_senders = audit_session_no_senders);
140 
141 #endif /* CONFIG_AUDIT */
142