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