6198 Let's EOL cachefs
[illumos-gate.git] / usr / src / uts / common / os / audit_core.c
blob5292e62c726148a9fa00bad92283f2e8f7feac95
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <sys/param.h>
26 #include <sys/types.h>
27 #include <sys/time.h>
28 #include <sys/kmem.h>
29 #include <sys/proc.h>
30 #include <sys/vnode.h>
31 #include <sys/file.h>
32 #include <sys/user.h>
33 #include <sys/stropts.h>
34 #include <sys/systm.h>
35 #include <sys/pathname.h>
36 #include <sys/debug.h>
37 #include <sys/cred_impl.h>
38 #include <sys/zone.h>
39 #include <sys/modctl.h>
40 #include <sys/sysconf.h>
41 #include <c2/audit.h>
42 #include <c2/audit_kernel.h>
43 #include <c2/audit_kevents.h>
44 #include <c2/audit_record.h>
47 struct p_audit_data *pad0;
48 struct t_audit_data *tad0;
50 extern uint_t num_syscall; /* size of audit_s2e table */
51 extern kmutex_t pidlock; /* proc table lock */
54 void
55 audit_init()
57 kthread_t *au_thread;
58 auditinfo_addr_t *ainfo;
59 struct audit_path apempty;
62 * If the c2audit module is explicitely excluded in /etc/system,
63 * it cannot be loaded later (e.g. using modload). Make a notice
64 * that the module won't be present and do nothing.
67 if (mod_sysctl(SYS_CHECK_EXCLUDE, "c2audit") != 0) {
68 audit_active = C2AUDIT_DISABLED;
69 return;
72 /* c2audit module can be loaded anytime */
73 audit_active = C2AUDIT_UNLOADED;
75 /* initialize the process audit data (pad) memory allocator */
76 au_pad_init();
78 /* initialize the zone audit context */
79 au_zone_setup();
81 /* inital thread structure */
82 tad0 = kmem_zalloc(sizeof (struct t_audit_data), KM_SLEEP);
84 /* initial process structure */
85 pad0 = kmem_cache_alloc(au_pad_cache, KM_SLEEP);
86 bzero(&pad0->pad_data, sizeof (pad0->pad_data));
88 curthread->t_audit_data = tad0;
89 curproc->p_audit_data = pad0;
92 * The kernel allocates a bunch of threads make sure they have
93 * a valid tad
96 mutex_enter(&pidlock);
98 au_thread = curthread;
99 do {
100 if (T2A(au_thread) == NULL) {
101 T2A(au_thread) = tad0;
103 au_thread = au_thread->t_next;
104 } while (au_thread != curthread);
106 tad0->tad_ad = NULL;
107 mutex_exit(&pidlock);
110 * Initialize audit context in our cred (kcred).
111 * No copy-on-write needed here because it's so early in init.
114 ainfo = crgetauinfo_modifiable(kcred);
115 ASSERT(ainfo != NULL);
116 bzero(ainfo, sizeof (auditinfo_addr_t));
117 ainfo->ai_auid = AU_NOAUDITID;
119 /* fabricate an empty audit_path to extend */
120 apempty.audp_cnt = 0;
121 apempty.audp_sect[0] = (char *)(&apempty.audp_sect[1]);
122 pad0->pad_root = au_pathdup(&apempty, 1, 2);
123 bcopy("/", pad0->pad_root->audp_sect[0], 2);
124 au_pathhold(pad0->pad_root);
125 pad0->pad_cwd = pad0->pad_root;
129 * Check for any pending changes to the audit context for the given proc.
130 * p_crlock and pad_lock for the process are acquired here. Caller is
131 * responsible for assuring the process doesn't go away. If context is
132 * updated, the specified cralloc'ed cred will be used, otherwise it's freed.
133 * If no cred is given, it will be cralloc'ed here and caller assures that
134 * it is safe to allocate memory.
137 void
138 audit_update_context(proc_t *p, cred_t *ncr)
140 struct p_audit_data *pad;
141 cred_t *newcred = ncr;
143 pad = P2A(p);
144 if (pad == NULL) {
145 if (newcred != NULL)
146 crfree(newcred);
147 return;
150 /* If a mask update is pending, take care of it. */
151 if (pad->pad_flags & PAD_SETMASK) {
152 auditinfo_addr_t *ainfo;
154 if (newcred == NULL)
155 newcred = cralloc();
157 mutex_enter(&pad->pad_lock);
158 /* the condition may have been handled by the time we lock */
159 if (pad->pad_flags & PAD_SETMASK) {
160 ainfo = crgetauinfo_modifiable(newcred);
161 if (ainfo == NULL) {
162 mutex_exit(&pad->pad_lock);
163 crfree(newcred);
164 return;
167 mutex_enter(&p->p_crlock);
168 crcopy_to(p->p_cred, newcred);
169 p->p_cred = newcred;
171 ainfo->ai_mask = pad->pad_newmask;
173 /* Unlock and cleanup. */
174 mutex_exit(&p->p_crlock);
175 pad->pad_flags &= ~PAD_SETMASK;
178 * For curproc, assure that our thread points to right
179 * cred, so CRED() will be correct. Otherwise, no need
180 * to broadcast changes (via set_proc_pre_sys), since
181 * t_pre_sys is ALWAYS on when audit is enabled... due
182 * to syscall auditing.
184 if (p == curproc)
185 crset(p, newcred);
186 else
187 crfree(newcred);
188 } else {
189 crfree(newcred);
191 mutex_exit(&pad->pad_lock);
192 } else {
193 if (newcred != NULL)
194 crfree(newcred);
199 * ROUTINE: AUDIT_NEWPROC
200 * PURPOSE: initialize the child p_audit_data structure
201 * CALLBY: GETPROC
202 * NOTE: All threads for the parent process are locked at this point.
203 * We are essentially running singled threaded for this reason.
204 * GETPROC is called when system creates a new process.
205 * By the time AUDIT_NEWPROC is called, the child proc
206 * structure has already been initialized. What we need
207 * to do is to allocate the child p_audit_data and
208 * initialize it with the content of current parent process.
211 void
212 audit_newproc(struct proc *cp) /* initialized child proc structure */
214 p_audit_data_t *pad; /* child process audit data */
215 p_audit_data_t *opad; /* parent process audit data */
217 pad = kmem_cache_alloc(au_pad_cache, KM_SLEEP);
219 P2A(cp) = pad;
221 opad = P2A(curproc);
224 * copy the audit data. Note that all threads of current
225 * process have been "held". Thus there is no race condition
226 * here with mutiple threads trying to alter the cwrd
227 * structure (such as releasing it).
229 * The audit context in the cred is "duplicated" for the new
230 * proc by elsewhere crhold'ing the parent's cred which it shares.
232 * We still want to hold things since auditon() [A_SETUMASK,
233 * A_SETSMASK] could be walking through the processes to
234 * update things.
236 mutex_enter(&opad->pad_lock); /* lock opad structure during copy */
237 pad->pad_data = opad->pad_data; /* copy parent's process audit data */
238 au_pathhold(pad->pad_root);
239 au_pathhold(pad->pad_cwd);
240 mutex_exit(&opad->pad_lock); /* current proc will keep cwrd open */
243 * If we are in the limited mode, there is nothing to audit and
244 * there could not have been anything to audit, since it is not
245 * possible to switch from the full mode into the limited mode
246 * once the full mode is set.
248 if (audit_active != C2AUDIT_LOADED)
249 return;
252 * finish auditing of parent here so that it will be done
253 * before child has a chance to run. We include the child
254 * pid since the return value in the return token is a dummy
255 * one and contains no useful information (it is included to
256 * make the audit record structure consistant).
258 * tad_flag is set if auditing is on
260 if (((t_audit_data_t *)T2A(curthread))->tad_flag)
261 au_uwrite(au_to_arg32(0, "child PID", (uint32_t)cp->p_pid));
264 * finish up audit record generation here because child process
265 * is set to run before parent process. We distinguish here
266 * between FORK, FORK1, or VFORK by the saved system call ID.
268 audit_finish(0, ((t_audit_data_t *)T2A(curthread))->tad_scid, 0, 0);
272 * ROUTINE: AUDIT_PFREE
273 * PURPOSE: deallocate the per-process udit data structure
274 * CALLBY: EXIT
275 * FORK_FAIL
276 * NOTE: all lwp except current one have stopped in SEXITLWPS
277 * why we are single threaded?
278 * . all lwp except current one have stopped in SEXITLWPS.
281 void
282 audit_pfree(struct proc *p) /* proc structure to be freed */
284 { /* AUDIT_PFREE */
286 p_audit_data_t *pad;
288 pad = P2A(p);
290 /* better be a per process audit data structure */
291 ASSERT(pad != (p_audit_data_t *)0);
293 if (pad == pad0) {
294 return;
297 /* deallocate all auditing resources for this process */
298 au_pathrele(pad->pad_root);
299 au_pathrele(pad->pad_cwd);
302 * Since the pad structure is completely overwritten after alloc,
303 * we don't bother to clear it.
306 kmem_cache_free(au_pad_cache, pad);
310 * ROUTINE: AUDIT_THREAD_CREATE
311 * PURPOSE: allocate per-process thread audit data structure
312 * CALLBY: THREAD_CREATE
313 * NOTE: This is called just after *t was bzero'd.
314 * We are single threaded in this routine.
315 * TODO:
316 * QUESTION:
319 void
320 audit_thread_create(kthread_id_t t)
322 t_audit_data_t *tad; /* per-thread audit data */
324 tad = kmem_zalloc(sizeof (struct t_audit_data), KM_SLEEP);
326 T2A(t) = tad; /* set up thread audit data ptr */
327 tad->tad_thread = t; /* back ptr to thread: DEBUG */
331 * ROUTINE: AUDIT_THREAD_FREE
332 * PURPOSE: free the per-thread audit data structure
333 * CALLBY: THREAD_FREE
334 * NOTE: most thread data is clear after return
337 void
338 audit_thread_free(kthread_t *t)
340 t_audit_data_t *tad;
341 au_defer_info_t *attr;
343 tad = T2A(t);
345 /* thread audit data must still be set */
347 if (tad == tad0) {
348 return;
351 if (tad == NULL) {
352 return;
355 t->t_audit_data = 0;
357 /* must not have any audit record residual */
358 ASSERT(tad->tad_ad == NULL);
360 /* saved path must be empty */
361 ASSERT(tad->tad_aupath == NULL);
363 if (tad->tad_atpath)
364 au_pathrele(tad->tad_atpath);
366 if (audit_active == C2AUDIT_LOADED) {
367 attr = tad->tad_defer_head;
368 while (attr != NULL) {
369 au_defer_info_t *tmp_attr = attr;
371 au_free_rec(attr->audi_ad);
373 attr = attr->audi_next;
374 kmem_free(tmp_attr, sizeof (au_defer_info_t));
378 kmem_free(tad, sizeof (*tad));
382 * ROUTINE: AUDIT_FALLOC
383 * PURPOSE: allocating a new file structure
384 * CALLBY: FALLOC
385 * NOTE: file structure already initialized
386 * TODO:
387 * QUESTION:
390 void
391 audit_falloc(struct file *fp)
392 { /* AUDIT_FALLOC */
394 f_audit_data_t *fad;
396 /* allocate per file audit structure if there a'int any */
397 ASSERT(F2A(fp) == NULL);
399 fad = kmem_zalloc(sizeof (struct f_audit_data), KM_SLEEP);
401 F2A(fp) = fad;
403 fad->fad_thread = curthread; /* file audit data back ptr; DEBUG */
407 * ROUTINE: AUDIT_UNFALLOC
408 * PURPOSE: deallocate file audit data structure
409 * CALLBY: CLOSEF
410 * UNFALLOC
411 * NOTE:
412 * TODO:
413 * QUESTION:
416 void
417 audit_unfalloc(struct file *fp)
419 f_audit_data_t *fad;
421 fad = F2A(fp);
423 if (!fad) {
424 return;
426 if (fad->fad_aupath != NULL) {
427 au_pathrele(fad->fad_aupath);
429 fp->f_audit_data = 0;
430 kmem_free(fad, sizeof (struct f_audit_data));
433 uint32_t
434 audit_getstate()
436 return (audit_active == C2AUDIT_LOADED &&
437 ((AU_AUDIT_MASK) & U2A(u)->tad_audit));