xref: /titanic_44/usr/src/uts/common/os/audit_zone.c (revision 726fad2a65f16c200a03969c29cb5c86c2d427db)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 
27 #include <c2/audit.h>
28 #include <c2/audit_kernel.h>
29 #include <c2/audit_record.h>
30 #include <sys/kmem.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/taskq.h>
34 #include <sys/t_lock.h>
35 #include <sys/thread.h>
36 #include <sys/types.h>
37 #include <sys/zone.h>
38 
39 zone_key_t au_zone_key;
40 
41 /*ARGSUSED*/
42 static void *
43 au_zone_init(zoneid_t zone)
44 {
45 	au_kcontext_t	*kctx = kmem_zalloc(sizeof (au_kcontext_t), KM_SLEEP);
46 	static au_kcontext_t	*global_kctx = NULL;
47 
48 	/*
49 	 * INGLOBALZONE(curproc) is invalid at this point, so check for
50 	 * zone 0
51 	 */
52 
53 	if (zone == 0) {
54 		global_kctx = kctx;
55 		global_zone->zone_audit_kctxt = kctx;
56 	} else {
57 		kctx->auk_policy = global_kctx->auk_policy;
58 		curproc->p_zone->zone_audit_kctxt = kctx;
59 	}
60 	kctx->auk_valid = AUK_VALID;
61 	kctx->auk_zid = zone;
62 
63 	kctx->auk_info.ai_termid.at_type = AU_IPv4;
64 	kctx->auk_info.ai_auid = AU_NOAUDITID;
65 	kctx->auk_auditstate = AUC_INIT_AUDIT;
66 
67 	/* setup defaults for audit queue flow control */
68 	kctx->auk_queue.hiwater = AQ_HIWATER;
69 	kctx->auk_queue.lowater = AQ_LOWATER;
70 	kctx->auk_queue.bufsz   = AQ_BUFSZ;
71 	kctx->auk_queue.buflen  = AQ_BUFSZ;
72 	kctx->auk_queue.delay   = AQ_DELAY;
73 
74 	/* statistics per zone */
75 	kctx->auk_statistics.as_version  = TOKEN_VERSION;
76 	kctx->auk_statistics.as_numevent = MAX_KEVENTS;
77 
78 	/* door IO buffer: */
79 	kctx->auk_dbuffer =
80 	    kmem_alloc(AU_DBUF_HEADER + kctx->auk_queue.bufsz, KM_SLEEP);
81 
82 	/* locks and cv's */
83 
84 	mutex_init(&(kctx->auk_eagain_mutex), NULL, MUTEX_DEFAULT, NULL);
85 	cv_init(&(kctx->auk_eagain_cv), NULL, CV_DRIVER, NULL);
86 
87 	mutex_init(&(kctx->auk_svc_lock), NULL, MUTEX_DEFAULT, NULL);
88 
89 	mutex_init(&(kctx->auk_queue.lock), NULL, MUTEX_DEFAULT, NULL);
90 	cv_init(&(kctx->auk_queue.write_cv), NULL, CV_DRIVER, NULL);
91 	cv_init(&(kctx->auk_queue.read_cv), NULL, CV_DRIVER, NULL);
92 
93 	return (kctx);
94 }
95 
96 /*ARGSUSED*/
97 static void
98 au_zone_shutdown(zoneid_t zone, void *arg)
99 {
100 	au_kcontext_t	*kctx = arg;
101 
102 	if (audit_active == C2AUDIT_LOADED && (kctx->auk_zid == GLOBAL_ZONEID ||
103 	    (audit_policy | AUDIT_PERZONE)) && (kctx->auk_current_vp != NULL))
104 		(void) au_doormsg(kctx, AU_DBUF_SHUTDOWN, NULL);
105 
106 	kctx->auk_valid = AUK_INVALID;
107 
108 	/* shutdown the output thread if it is still running */
109 	kctx->auk_auditstate = AUC_NOAUDIT;
110 
111 	if (kctx->auk_output_active) {
112 		mutex_enter(&(kctx->auk_queue.lock));
113 		cv_broadcast(&(kctx->auk_queue.read_cv));
114 		mutex_exit(&(kctx->auk_queue.lock));
115 
116 		taskq_destroy(kctx->auk_taskq);
117 	}
118 }
119 
120 /*ARGSUSED*/
121 static void
122 au_zone_destroy(zoneid_t zone, void *arg)
123 {
124 	au_kcontext_t	*kctx = arg;
125 
126 	ASSERT(kctx->auk_auditstate == AUC_NOAUDIT);
127 
128 	mutex_destroy(&(kctx->auk_eagain_mutex));
129 	cv_destroy(&(kctx->auk_eagain_cv));
130 
131 	mutex_destroy(&(kctx->auk_svc_lock));
132 
133 	mutex_enter(&(kctx->auk_queue.lock));
134 	if (kctx->auk_queue.head != NULL) {
135 		au_free_rec(kctx->auk_queue.head);
136 	}
137 	mutex_exit(&(kctx->auk_queue.lock));
138 
139 	mutex_destroy(&(kctx->auk_queue.lock));
140 
141 	cv_destroy(&(kctx->auk_queue.write_cv));
142 	cv_destroy(&(kctx->auk_queue.read_cv));
143 
144 	kmem_free(kctx->auk_dbuffer, AU_DBUF_HEADER + kctx->auk_queue.buflen);
145 
146 	kmem_free(kctx, sizeof (au_kcontext_t));
147 }
148 
149 void
150 au_zone_setup()
151 {
152 	zone_key_create(&au_zone_key, au_zone_init, au_zone_shutdown,
153 	    au_zone_destroy);
154 
155 }
156 
157 int
158 au_zone_getstate(const au_kcontext_t *context)
159 {
160 	au_kcontext_t *tcontext;
161 
162 	if (context != NULL)
163 		return (context->auk_auditstate);
164 	tcontext = GET_KCTX_PZ;
165 	return (tcontext->auk_auditstate);
166 }
167