xref: /illumos-gate/usr/src/uts/common/c2/audit.c (revision ead1f93ee620d7580f7e53350fe5a884fc4f158a)
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  * This file contains the audit hook support code for auditing.
28  */
29 
30 #include <sys/types.h>
31 #include <sys/proc.h>
32 #include <sys/vnode.h>
33 #include <sys/vfs.h>
34 #include <sys/file.h>
35 #include <sys/user.h>
36 #include <sys/stropts.h>
37 #include <sys/systm.h>
38 #include <sys/pathname.h>
39 #include <sys/syscall.h>
40 #include <sys/fcntl.h>
41 #include <sys/ipc_impl.h>
42 #include <sys/msg_impl.h>
43 #include <sys/sem_impl.h>
44 #include <sys/shm_impl.h>
45 #include <sys/kmem.h>		/* for KM_SLEEP */
46 #include <sys/socket.h>
47 #include <sys/cmn_err.h>	/* snprintf... */
48 #include <sys/debug.h>
49 #include <sys/thread.h>
50 #include <netinet/in.h>
51 #include <c2/audit.h>		/* needs to be included before user.h */
52 #include <c2/audit_kernel.h>	/* for M_DONTWAIT */
53 #include <c2/audit_kevents.h>
54 #include <c2/audit_record.h>
55 #include <sys/strsubr.h>
56 #include <sys/tihdr.h>
57 #include <sys/tiuser.h>
58 #include <sys/timod.h>
59 #include <sys/model.h>		/* for model_t */
60 #include <sys/disp.h>		/* for servicing_interrupt() */
61 #include <sys/devpolicy.h>
62 #include <sys/crypto/ioctladmin.h>
63 #include <sys/cred.h>
64 #include <inet/kssl/kssl.h>
65 #include <net/pfpolicy.h>
66 
67 static void add_return_token(caddr_t *, unsigned int scid, int err, int rval);
68 
69 static void audit_pathbuild(struct pathname *pnp);
70 
71 /*
72  * ROUTINE:	AUDIT_NEWPROC
73  * PURPOSE:	initialize the child p_audit_data structure
74  * CALLBY:	GETPROC
75  * NOTE:	All threads for the parent process are locked at this point.
76  *		We are essentially running singled threaded for this reason.
77  *		GETPROC is called when system creates a new process.
78  *		By the time AUDIT_NEWPROC is called, the child proc
79  *		structure has already been initialized. What we need
80  *		to do is to allocate the child p_audit_data and
81  *		initialize it with the content of current parent process.
82  */
83 
84 void
85 audit_newproc(struct proc *cp)	/* initialized child proc structure */
86 {
87 	p_audit_data_t *pad;	/* child process audit data */
88 	p_audit_data_t *opad;	/* parent process audit data */
89 
90 	pad = kmem_cache_alloc(au_pad_cache, KM_SLEEP);
91 
92 	P2A(cp) = pad;
93 
94 	opad = P2A(curproc);
95 
96 	/*
97 	 * copy the audit data. Note that all threads of current
98 	 *   process have been "held". Thus there is no race condition
99 	 *   here with mutiple threads trying to alter the cwrd
100 	 *   structure (such as releasing it).
101 	 *
102 	 *   The audit context in the cred is "duplicated" for the new
103 	 *   proc by elsewhere crhold'ing the parent's cred which it shares.
104 	 *
105 	 *   We still want to hold things since auditon() [A_SETUMASK,
106 	 *   A_SETSMASK] could be walking through the processes to
107 	 *   update things.
108 	 */
109 	mutex_enter(&opad->pad_lock);	/* lock opad structure during copy */
110 	pad->pad_data = opad->pad_data;	/* copy parent's process audit data */
111 	au_pathhold(pad->pad_root);
112 	au_pathhold(pad->pad_cwd);
113 	mutex_exit(&opad->pad_lock);	/* current proc will keep cwrd open */
114 
115 	/*
116 	 * finish auditing of parent here so that it will be done
117 	 * before child has a chance to run. We include the child
118 	 * pid since the return value in the return token is a dummy
119 	 * one and contains no useful information (it is included to
120 	 * make the audit record structure consistant).
121 	 *
122 	 * tad_flag is set if auditing is on
123 	 */
124 	if (((t_audit_data_t *)T2A(curthread))->tad_flag)
125 		au_uwrite(au_to_arg32(0, "child PID", (uint32_t)cp->p_pid));
126 
127 	/*
128 	 * finish up audit record generation here because child process
129 	 * is set to run before parent process. We distinguish here
130 	 * between FORK, FORK1, or VFORK by the saved system call ID.
131 	 */
132 	audit_finish(0, ((t_audit_data_t *)T2A(curthread))->tad_scid, 0, 0);
133 }
134 
135 /*
136  * ROUTINE:	AUDIT_PFREE
137  * PURPOSE:	deallocate the per-process udit data structure
138  * CALLBY:	EXIT
139  *		FORK_FAIL
140  * NOTE:	all lwp except current one have stopped in SEXITLWPS
141  * 		why we are single threaded?
142  *		. all lwp except current one have stopped in SEXITLWPS.
143  */
144 void
145 audit_pfree(struct proc *p)		/* proc structure to be freed */
146 
147 {	/* AUDIT_PFREE */
148 
149 	p_audit_data_t *pad;
150 
151 	pad = P2A(p);
152 
153 	/* better be a per process audit data structure */
154 	ASSERT(pad != (p_audit_data_t *)0);
155 
156 	if (pad == pad0) {
157 		return;
158 	}
159 
160 	/* deallocate all auditing resources for this process */
161 	au_pathrele(pad->pad_root);
162 	au_pathrele(pad->pad_cwd);
163 
164 	/*
165 	 * Since the pad structure is completely overwritten after alloc,
166 	 * we don't bother to clear it.
167 	 */
168 
169 	kmem_cache_free(au_pad_cache, pad);
170 }
171 
172 /*
173  * ROUTINE:	AUDIT_THREAD_CREATE
174  * PURPOSE:	allocate per-process thread audit data structure
175  * CALLBY:	THREAD_CREATE
176  * NOTE:	This is called just after *t was bzero'd.
177  *		We are single threaded in this routine.
178  * TODO:
179  * QUESTION:
180  */
181 
182 void
183 audit_thread_create(kthread_id_t t)
184 {
185 	t_audit_data_t *tad;	/* per-thread audit data */
186 
187 	tad = kmem_zalloc(sizeof (struct t_audit_data), KM_SLEEP);
188 
189 	T2A(t) = tad;		/* set up thread audit data ptr */
190 	tad->tad_thread = t;	/* back ptr to thread: DEBUG */
191 }
192 
193 /*
194  * ROUTINE:	AUDIT_THREAD_FREE
195  * PURPOSE:	free the per-thread audit data structure
196  * CALLBY:	THREAD_FREE
197  * NOTE:	most thread data is clear after return
198  */
199 void
200 audit_thread_free(kthread_t *t)
201 {
202 	t_audit_data_t *tad;
203 	au_defer_info_t	*attr;
204 
205 	tad = T2A(t);
206 
207 	/* thread audit data must still be set */
208 
209 	if (tad == tad0) {
210 		return;
211 	}
212 
213 	if (tad == NULL) {
214 		return;
215 	}
216 
217 	t->t_audit_data = 0;
218 
219 	/* must not have any audit record residual */
220 	ASSERT(tad->tad_ad == NULL);
221 
222 	/* saved path must be empty */
223 	ASSERT(tad->tad_aupath == NULL);
224 
225 	if (tad->tad_atpath)
226 		au_pathrele(tad->tad_atpath);
227 
228 	attr = tad->tad_defer_head;
229 	while (attr != NULL) {
230 		au_defer_info_t	*tmp_attr = attr;
231 
232 		au_free_rec(attr->audi_ad);
233 
234 		attr = attr->audi_next;
235 		kmem_free(tmp_attr, sizeof (au_defer_info_t));
236 	}
237 
238 	kmem_free(tad, sizeof (*tad));
239 }
240 
241 /*
242  * ROUTINE:	AUDIT_SAVEPATH
243  * PURPOSE:
244  * CALLBY:	LOOKUPPN
245  *
246  * NOTE:	We have reached the end of a path in fs/lookup.c.
247  *		We get two pieces of information here:
248  *		the vnode of the last component (vp) and
249  *		the status of the last access (flag).
250  * TODO:
251  * QUESTION:
252  */
253 
254 /*ARGSUSED*/
255 int
256 audit_savepath(
257 	struct pathname *pnp,		/* pathname to lookup */
258 	struct vnode *vp,		/* vnode of the last component */
259 	int    flag,			/* status of the last access */
260 	cred_t *cr)			/* cred of requestor */
261 {
262 
263 	t_audit_data_t *tad;	/* current thread */
264 	au_kcontext_t	*kctx = GET_KCTX_PZ;
265 
266 	tad = U2A(u);
267 
268 	/*
269 	 * this event being audited or do we need path information
270 	 * later? This might be for a chdir/chroot or open (add path
271 	 * to file pointer. If the path has already been found for an
272 	 * open/creat then we don't need to process the path.
273 	 *
274 	 * S2E_SP (PAD_SAVPATH) flag comes from audit_s2e[].au_ctrl. Used with
275 	 *	chroot, chdir, open, creat system call processing. It determines
276 	 *	if audit_savepath() will discard the path or we need it later.
277 	 * PAD_PATHFND means path already included in this audit record. It
278 	 *	is used in cases where multiple path lookups are done per
279 	 *	system call. The policy flag, AUDIT_PATH, controls if multiple
280 	 *	paths are allowed.
281 	 * S2E_NPT (PAD_NOPATH) flag comes from audit_s2e[].au_ctrl. Used with
282 	 *	exit processing to inhibit any paths that may be added due to
283 	 *	closes.
284 	 */
285 	if ((tad->tad_flag == 0 && !(tad->tad_ctrl & PAD_SAVPATH)) ||
286 	    ((tad->tad_ctrl & PAD_PATHFND) &&
287 	    !(kctx->auk_policy & AUDIT_PATH)) ||
288 	    (tad->tad_ctrl & PAD_NOPATH)) {
289 		return (0);
290 	}
291 
292 	tad->tad_ctrl |= PAD_NOPATH;		/* prevent possible reentry */
293 
294 	audit_pathbuild(pnp);
295 	tad->tad_vn = vp;
296 
297 	/*
298 	 * are we auditing only if error, or if it is not open or create
299 	 * otherwise audit_setf will do it
300 	 */
301 
302 	if (tad->tad_flag) {
303 		if (flag &&
304 		    (tad->tad_scid == SYS_open ||
305 		    tad->tad_scid == SYS_open64 ||
306 		    tad->tad_scid == SYS_openat ||
307 		    tad->tad_scid == SYS_openat64)) {
308 			tad->tad_ctrl |= PAD_TRUE_CREATE;
309 		}
310 
311 		/* add token to audit record for this name */
312 		au_uwrite(au_to_path(tad->tad_aupath));
313 
314 		/* add the attributes of the object */
315 		if (vp) {
316 			/*
317 			 * only capture attributes when there is no error
318 			 * lookup will not return the vnode of the failing
319 			 * component.
320 			 *
321 			 * if there was a lookup error, then don't add
322 			 * attribute. if lookup in vn_create(),
323 			 * then don't add attribute,
324 			 * it will be added at end of vn_create().
325 			 */
326 			if (!flag && !(tad->tad_ctrl & PAD_NOATTRB))
327 				audit_attributes(vp);
328 		}
329 	}
330 
331 	/* free up space if we're not going to save path (open, creat) */
332 	if ((tad->tad_ctrl & PAD_SAVPATH) == 0) {
333 		if (tad->tad_aupath != NULL) {
334 			au_pathrele(tad->tad_aupath);
335 			tad->tad_aupath = NULL;
336 			tad->tad_vn = NULL;
337 		}
338 	}
339 	if (tad->tad_ctrl & PAD_MLD)
340 		tad->tad_ctrl |= PAD_PATHFND;
341 
342 	tad->tad_ctrl &= ~PAD_NOPATH;		/* restore */
343 	return (0);
344 }
345 
346 static void
347 audit_pathbuild(struct pathname *pnp)
348 {
349 	char *pp;	/* pointer to path */
350 	int len;	/* length of incoming segment */
351 	int newsect;	/* path requires a new section */
352 	struct audit_path	*pfxapp;	/* prefix for path */
353 	struct audit_path	*newapp;	/* new audit_path */
354 	t_audit_data_t *tad;	/* current thread */
355 	p_audit_data_t *pad;	/* current process */
356 
357 	tad = U2A(u);
358 	ASSERT(tad != NULL);
359 	pad = P2A(curproc);
360 	ASSERT(pad != NULL);
361 
362 	len = (pnp->pn_path - pnp->pn_buf) + 1;		/* +1 for terminator */
363 	ASSERT(len > 0);
364 
365 	/* adjust for path prefix: tad_aupath, ATPATH, CRD, or CWD */
366 	mutex_enter(&pad->pad_lock);
367 	if (tad->tad_aupath != NULL) {
368 		pfxapp = tad->tad_aupath;
369 	} else if ((tad->tad_ctrl & PAD_ATCALL) && pnp->pn_buf[0] != '/') {
370 		ASSERT(tad->tad_atpath != NULL);
371 		pfxapp = tad->tad_atpath;
372 	} else if (tad->tad_ctrl & PAD_ABSPATH) {
373 		pfxapp = pad->pad_root;
374 	} else {
375 		pfxapp = pad->pad_cwd;
376 	}
377 	au_pathhold(pfxapp);
378 	mutex_exit(&pad->pad_lock);
379 
380 	/* get an expanded buffer to hold the anchored path */
381 	newsect = tad->tad_ctrl & PAD_ATTPATH;
382 	newapp = au_pathdup(pfxapp, newsect, len);
383 	au_pathrele(pfxapp);
384 
385 	pp = newapp->audp_sect[newapp->audp_cnt] - len;
386 	if (!newsect) {
387 		/* overlay previous NUL terminator */
388 		*(pp - 1) = '/';
389 	}
390 
391 	/* now add string of processed path */
392 	bcopy(pnp->pn_buf, pp, len);
393 	pp[len - 1] = '\0';
394 
395 	/* perform path simplification as necessary */
396 	audit_fixpath(newapp, len);
397 
398 	if (tad->tad_aupath)
399 		au_pathrele(tad->tad_aupath);
400 	tad->tad_aupath = newapp;
401 
402 	/* for case where multiple lookups in one syscall (rename) */
403 	tad->tad_ctrl &= ~(PAD_ABSPATH | PAD_ATTPATH);
404 }
405 
406 
407 
408 /*ARGSUSED*/
409 
410 /*
411  * ROUTINE:	AUDIT_ADDCOMPONENT
412  * PURPOSE:	extend the path by the component accepted
413  * CALLBY:	LOOKUPPN
414  * NOTE:	This function is called only when there is an error in
415  *		parsing a path component
416  * TODO:	Add the error component to audit record
417  * QUESTION:	what is this for
418  */
419 
420 void
421 audit_addcomponent(struct pathname *pnp)
422 {
423 	au_kcontext_t	*kctx = GET_KCTX_PZ;
424 	t_audit_data_t *tad;
425 
426 	tad = U2A(u);
427 	/*
428 	 * S2E_SP (PAD_SAVPATH) flag comes from audit_s2e[].au_ctrl. Used with
429 	 *	chroot, chdir, open, creat system call processing. It determines
430 	 *	if audit_savepath() will discard the path or we need it later.
431 	 * PAD_PATHFND means path already included in this audit record. It
432 	 *	is used in cases where multiple path lookups are done per
433 	 *	system call. The policy flag, AUDIT_PATH, controls if multiple
434 	 *	paths are allowed.
435 	 * S2E_NPT (PAD_NOPATH) flag comes from audit_s2e[].au_ctrl. Used with
436 	 *	exit processing to inhibit any paths that may be added due to
437 	 *	closes.
438 	 */
439 	if ((tad->tad_flag == 0 && !(tad->tad_ctrl & PAD_SAVPATH)) ||
440 	    ((tad->tad_ctrl & PAD_PATHFND) &&
441 	    !(kctx->auk_policy & AUDIT_PATH)) ||
442 	    (tad->tad_ctrl & PAD_NOPATH)) {
443 		return;
444 	}
445 
446 	return;
447 
448 }	/* AUDIT_ADDCOMPONENT */
449 
450 
451 
452 
453 
454 
455 
456 
457 /*
458  * ROUTINE:	AUDIT_ANCHORPATH
459  * PURPOSE:
460  * CALLBY:	LOOKUPPN
461  * NOTE:
462  * anchor path at "/". We have seen a symbolic link or entering for the
463  * first time we will throw away any saved path if path is anchored.
464  *
465  * flag = 0, path is relative.
466  * flag = 1, path is absolute. Free any saved path and set flag to PAD_ABSPATH.
467  *
468  * If the (new) path is absolute, then we have to throw away whatever we have
469  * already accumulated since it is being superseded by new path which is
470  * anchored at the root.
471  *		Note that if the path is relative, this function does nothing
472  * TODO:
473  * QUESTION:
474  */
475 /*ARGSUSED*/
476 void
477 audit_anchorpath(struct pathname *pnp, int flag)
478 {
479 	au_kcontext_t	*kctx = GET_KCTX_PZ;
480 	t_audit_data_t *tad;
481 
482 	tad = U2A(u);
483 
484 	/*
485 	 * this event being audited or do we need path information
486 	 * later? This might be for a chdir/chroot or open (add path
487 	 * to file pointer. If the path has already been found for an
488 	 * open/creat then we don't need to process the path.
489 	 *
490 	 * S2E_SP (PAD_SAVPATH) flag comes from audit_s2e[].au_ctrl. Used with
491 	 *	chroot, chdir, open, creat system call processing. It determines
492 	 *	if audit_savepath() will discard the path or we need it later.
493 	 * PAD_PATHFND means path already included in this audit record. It
494 	 *	is used in cases where multiple path lookups are done per
495 	 *	system call. The policy flag, AUDIT_PATH, controls if multiple
496 	 *	paths are allowed.
497 	 * S2E_NPT (PAD_NOPATH) flag comes from audit_s2e[].au_ctrl. Used with
498 	 *	exit processing to inhibit any paths that may be added due to
499 	 *	closes.
500 	 */
501 	if ((tad->tad_flag == 0 && !(tad->tad_ctrl & PAD_SAVPATH)) ||
502 	    ((tad->tad_ctrl & PAD_PATHFND) &&
503 	    !(kctx->auk_policy & AUDIT_PATH)) ||
504 	    (tad->tad_ctrl & PAD_NOPATH)) {
505 		return;
506 	}
507 
508 	if (flag) {
509 		tad->tad_ctrl |= PAD_ABSPATH;
510 		if (tad->tad_aupath != NULL) {
511 			au_pathrele(tad->tad_aupath);
512 			tad->tad_aupath = NULL;
513 			tad->tad_vn = NULL;
514 		}
515 	}
516 }
517 
518 
519 /*
520  * symbolic link. Save previous components.
521  *
522  * the path seen so far looks like this
523  *
524  *  +-----------------------+----------------+
525  *  | path processed so far | remaining path |
526  *  +-----------------------+----------------+
527  *  \-----------------------/
528  *	save this string if
529  *	symbolic link relative
530  *	(but don't include  symlink component)
531  */
532 
533 /*ARGSUSED*/
534 
535 
536 /*
537  * ROUTINE:	AUDIT_SYMLINK
538  * PURPOSE:
539  * CALLBY:	LOOKUPPN
540  * NOTE:
541  * TODO:
542  * QUESTION:
543  */
544 void
545 audit_symlink(struct pathname *pnp, struct pathname *sympath)
546 {
547 	char *sp;	/* saved initial pp */
548 	char *cp;	/* start of symlink path */
549 	uint_t len_path;	/* processed path before symlink */
550 	t_audit_data_t *tad;
551 	au_kcontext_t	*kctx = GET_KCTX_PZ;
552 
553 	tad = U2A(u);
554 
555 	/*
556 	 * this event being audited or do we need path information
557 	 * later? This might be for a chdir/chroot or open (add path
558 	 * to file pointer. If the path has already been found for an
559 	 * open/creat then we don't need to process the path.
560 	 *
561 	 * S2E_SP (PAD_SAVPATH) flag comes from audit_s2e[].au_ctrl. Used with
562 	 *	chroot, chdir, open, creat system call processing. It determines
563 	 *	if audit_savepath() will discard the path or we need it later.
564 	 * PAD_PATHFND means path already included in this audit record. It
565 	 *	is used in cases where multiple path lookups are done per
566 	 *	system call. The policy flag, AUDIT_PATH, controls if multiple
567 	 *	paths are allowed.
568 	 * S2E_NPT (PAD_NOPATH) flag comes from audit_s2e[].au_ctrl. Used with
569 	 *	exit processing to inhibit any paths that may be added due to
570 	 *	closes.
571 	 */
572 	if ((tad->tad_flag == 0 &&
573 	    !(tad->tad_ctrl & PAD_SAVPATH)) ||
574 	    ((tad->tad_ctrl & PAD_PATHFND) &&
575 	    !(kctx->auk_policy & AUDIT_PATH)) ||
576 	    (tad->tad_ctrl & PAD_NOPATH)) {
577 		return;
578 	}
579 
580 	/*
581 	 * if symbolic link is anchored at / then do nothing.
582 	 * When we cycle back to begin: in lookuppn() we will
583 	 * call audit_anchorpath() with a flag indicating if the
584 	 * path is anchored at / or is relative. We will release
585 	 * any saved path at that point.
586 	 *
587 	 * Note In the event that an error occurs in pn_combine then
588 	 * we want to remain pointing at the component that caused the
589 	 * path to overflow the pnp structure.
590 	 */
591 	if (sympath->pn_buf[0] == '/')
592 		return;
593 
594 	/* backup over last component */
595 	sp = cp = pnp->pn_path;
596 	while (*--cp != '/' && cp > pnp->pn_buf)
597 		;
598 
599 	len_path = cp - pnp->pn_buf;
600 
601 	/* is there anything to save? */
602 	if (len_path) {
603 		pnp->pn_path = pnp->pn_buf;
604 		audit_pathbuild(pnp);
605 		pnp->pn_path = sp;
606 	}
607 }
608 
609 /*
610  * file_is_public : determine whether events for the file (corresponding to
611  * 			the specified file attr) should be audited or ignored.
612  *
613  * returns: 	1 - if audit policy and file attributes indicate that
614  *			file is effectively public. read events for
615  *			the file should not be audited.
616  *		0 - otherwise
617  *
618  * The required attributes to be considered a public object are:
619  * - owned by root, AND
620  * - world-readable (permissions for other include read), AND
621  * - NOT world-writeable (permissions for other don't
622  *	include write)
623  *   (mode doesn't need to be checked for symlinks)
624  */
625 int
626 file_is_public(struct vattr *attr)
627 {
628 	au_kcontext_t	*kctx = GET_KCTX_PZ;
629 
630 	if (!(kctx->auk_policy & AUDIT_PUBLIC) && (attr->va_uid == 0) &&
631 	    ((attr->va_type == VLNK) ||
632 	    ((attr->va_mode & (VREAD>>6)) != 0) &&
633 	    ((attr->va_mode & (VWRITE>>6)) == 0))) {
634 		return (1);
635 	}
636 	return (0);
637 }
638 
639 
640 /*
641  * ROUTINE:	AUDIT_ATTRIBUTES
642  * PURPOSE:	Audit the attributes so we can tell why the error occurred
643  * CALLBY:	AUDIT_SAVEPATH
644  *		AUDIT_VNCREATE_FINISH
645  *		AUS_FCHOWN...audit_event.c...audit_path.c
646  * NOTE:
647  * TODO:
648  * QUESTION:
649  */
650 void
651 audit_attributes(struct vnode *vp)
652 {
653 	struct vattr attr;
654 	struct t_audit_data *tad;
655 
656 	tad = U2A(u);
657 
658 	if (vp) {
659 		attr.va_mask = AT_ALL;
660 		if (VOP_GETATTR(vp, &attr, 0, CRED(), NULL) != 0)
661 			return;
662 
663 		if (file_is_public(&attr) && (tad->tad_ctrl & PAD_PUBLIC_EV)) {
664 			/*
665 			 * This is a public object and a "public" event
666 			 * (i.e., read only) -- either by definition
667 			 * (e.g., stat, access...) or by virtue of write access
668 			 * not being requested (e.g. mmap).
669 			 * Flag it in the tad to prevent this audit at the end.
670 			 */
671 			tad->tad_ctrl |= PAD_NOAUDIT;
672 		} else {
673 			au_uwrite(au_to_attr(&attr));
674 			audit_sec_attributes(&(u_ad), vp);
675 		}
676 	}
677 }
678 
679 
680 /*
681  * ROUTINE:	AUDIT_FALLOC
682  * PURPOSE:	allocating a new file structure
683  * CALLBY:	FALLOC
684  * NOTE:	file structure already initialized
685  * TODO:
686  * QUESTION:
687  */
688 
689 void
690 audit_falloc(struct file *fp)
691 {	/* AUDIT_FALLOC */
692 
693 	f_audit_data_t *fad;
694 
695 	/* allocate per file audit structure if there a'int any */
696 	ASSERT(F2A(fp) == NULL);
697 
698 	fad = kmem_zalloc(sizeof (struct f_audit_data), KM_SLEEP);
699 
700 	F2A(fp) = fad;
701 
702 	fad->fad_thread = curthread; 	/* file audit data back ptr; DEBUG */
703 }
704 
705 /*
706  * ROUTINE:	AUDIT_UNFALLOC
707  * PURPOSE:	deallocate file audit data structure
708  * CALLBY:	CLOSEF
709  *		UNFALLOC
710  * NOTE:
711  * TODO:
712  * QUESTION:
713  */
714 
715 void
716 audit_unfalloc(struct file *fp)
717 {
718 	f_audit_data_t *fad;
719 
720 	fad = F2A(fp);
721 
722 	if (!fad) {
723 		return;
724 	}
725 	if (fad->fad_aupath != NULL) {
726 		au_pathrele(fad->fad_aupath);
727 	}
728 	fp->f_audit_data = 0;
729 	kmem_free(fad, sizeof (struct f_audit_data));
730 }
731 
732 /*
733  * ROUTINE:	AUDIT_EXIT
734  * PURPOSE:
735  * CALLBY:	EXIT
736  * NOTE:
737  * TODO:
738  * QUESTION:	why cmw code as offset by 2 but not here
739  */
740 /* ARGSUSED */
741 void
742 audit_exit(int code, int what)
743 {
744 	struct t_audit_data *tad;
745 	tad = U2A(u);
746 
747 	/*
748 	 * tad_scid will be set by audit_start even if we are not auditing
749 	 * the event.
750 	 */
751 	if (tad->tad_scid == SYS_exit) {
752 		/*
753 		 * if we are auditing the exit system call, then complete
754 		 * audit record generation (no return from system call).
755 		 */
756 		if (tad->tad_flag && tad->tad_event == AUE_EXIT)
757 			audit_finish(0, SYS_exit, 0, 0);
758 		return;
759 	}
760 
761 	/*
762 	 * Anyone auditing the system call that was aborted?
763 	 */
764 	if (tad->tad_flag) {
765 		au_uwrite(au_to_text("event aborted"));
766 		audit_finish(0, tad->tad_scid, 0, 0);
767 	}
768 
769 	/*
770 	 * Generate an audit record for process exit if preselected.
771 	 */
772 	(void) audit_start(0, SYS_exit, 0, 0);
773 	audit_finish(0, SYS_exit, 0, 0);
774 }
775 
776 /*
777  * ROUTINE:	AUDIT_CORE_START
778  * PURPOSE:
779  * CALLBY: 	PSIG
780  * NOTE:
781  * TODO:
782  */
783 void
784 audit_core_start(int sig)
785 {
786 	au_event_t event;
787 	au_state_t estate;
788 	t_audit_data_t *tad;
789 	au_kcontext_t	*kctx;
790 
791 	tad = U2A(u);
792 
793 	ASSERT(tad != (t_audit_data_t *)0);
794 
795 	ASSERT(tad->tad_scid == 0);
796 	ASSERT(tad->tad_event == 0);
797 	ASSERT(tad->tad_evmod == 0);
798 	ASSERT(tad->tad_ctrl == 0);
799 	ASSERT(tad->tad_flag == 0);
800 	ASSERT(tad->tad_aupath == NULL);
801 
802 	kctx = GET_KCTX_PZ;
803 
804 	/* get basic event for system call */
805 	event = AUE_CORE;
806 	estate = kctx->auk_ets[event];
807 
808 	if ((tad->tad_flag = auditme(kctx, tad, estate)) == 0)
809 		return;
810 
811 	/* reset the flags for non-user attributable events */
812 	tad->tad_ctrl   = PAD_CORE;
813 	tad->tad_scid   = 0;
814 
815 	/* if auditing not enabled, then don't generate an audit record */
816 
817 	if (!((kctx->auk_auditstate == AUC_AUDITING ||
818 	    kctx->auk_auditstate == AUC_INIT_AUDIT) ||
819 	    kctx->auk_auditstate == AUC_NOSPACE)) {
820 		tad->tad_flag = 0;
821 		tad->tad_ctrl = 0;
822 		return;
823 	}
824 
825 	tad->tad_event  = event;
826 	tad->tad_evmod  = 0;
827 
828 	ASSERT(tad->tad_ad == NULL);
829 
830 	au_write(&(u_ad), au_to_arg32(1, "signal", (uint32_t)sig));
831 }
832 
833 /*
834  * ROUTINE:	AUDIT_CORE_FINISH
835  * PURPOSE:
836  * CALLBY:	PSIG
837  * NOTE:
838  * TODO:
839  * QUESTION:
840  */
841 
842 /*ARGSUSED*/
843 void
844 audit_core_finish(int code)
845 {
846 	int flag;
847 	t_audit_data_t *tad;
848 	au_kcontext_t	*kctx;
849 
850 	tad = U2A(u);
851 
852 	ASSERT(tad != (t_audit_data_t *)0);
853 
854 	if ((flag = tad->tad_flag) == 0) {
855 		tad->tad_event = 0;
856 		tad->tad_evmod = 0;
857 		tad->tad_ctrl  = 0;
858 		ASSERT(tad->tad_aupath == NULL);
859 		return;
860 	}
861 	tad->tad_flag = 0;
862 
863 	kctx = GET_KCTX_PZ;
864 
865 	/* kludge for error 0, should use `code==CLD_DUMPED' instead */
866 	if (flag = audit_success(kctx, tad, 0, NULL)) {
867 		cred_t *cr = CRED();
868 		const auditinfo_addr_t *ainfo = crgetauinfo(cr);
869 
870 		ASSERT(ainfo != NULL);
871 
872 		/*
873 		 * Add subject information (no locks since our private copy of
874 		 * credential
875 		 */
876 		AUDIT_SETSUBJ(&(u_ad), cr, ainfo, kctx);
877 
878 		/* Add a return token (should use f argument) */
879 		add_return_token((caddr_t *)&(u_ad), tad->tad_scid, 0, 0);
880 
881 		AS_INC(as_generated, 1, kctx);
882 		AS_INC(as_kernel, 1, kctx);
883 	}
884 
885 	/* Close up everything */
886 	au_close(kctx, &(u_ad), flag, tad->tad_event, tad->tad_evmod);
887 
888 	/* free up any space remaining with the path's */
889 	if (tad->tad_aupath != NULL) {
890 		au_pathrele(tad->tad_aupath);
891 		tad->tad_aupath = NULL;
892 		tad->tad_vn = NULL;
893 	}
894 	tad->tad_event = 0;
895 	tad->tad_evmod = 0;
896 	tad->tad_ctrl  = 0;
897 }
898 
899 /*ARGSUSED*/
900 void
901 audit_stropen(struct vnode *vp, dev_t *devp, int flag, cred_t *crp)
902 {
903 }
904 
905 /*ARGSUSED*/
906 void
907 audit_strclose(struct vnode *vp, int flag, cred_t *crp)
908 {
909 }
910 
911 /*ARGSUSED*/
912 void
913 audit_strioctl(struct vnode *vp, int cmd, intptr_t arg, int flag,
914     int copyflag, cred_t *crp, int *rvalp)
915 {
916 }
917 
918 
919 /*ARGSUSED*/
920 void
921 audit_strgetmsg(struct vnode *vp, struct strbuf *mctl, struct strbuf *mdata,
922     unsigned char *pri, int *flag, int fmode)
923 {
924 	struct stdata *stp;
925 	t_audit_data_t *tad = U2A(u);
926 
927 	ASSERT(tad != (t_audit_data_t *)0);
928 
929 	stp = vp->v_stream;
930 
931 	/* lock stdata from audit_sock */
932 	mutex_enter(&stp->sd_lock);
933 
934 	/* proceed ONLY if user is being audited */
935 	if (!tad->tad_flag) {
936 		/*
937 		 * this is so we will not add audit data onto
938 		 * a thread that is not being audited.
939 		 */
940 		stp->sd_t_audit_data = NULL;
941 		mutex_exit(&stp->sd_lock);
942 		return;
943 	}
944 
945 	stp->sd_t_audit_data = (caddr_t)curthread;
946 	mutex_exit(&stp->sd_lock);
947 }
948 
949 /*ARGSUSED*/
950 void
951 audit_strputmsg(struct vnode *vp, struct strbuf *mctl, struct strbuf *mdata,
952     unsigned char pri, int flag, int fmode)
953 {
954 	struct stdata *stp;
955 	t_audit_data_t *tad = U2A(u);
956 
957 	ASSERT(tad != (t_audit_data_t *)0);
958 
959 	stp = vp->v_stream;
960 
961 	/* lock stdata from audit_sock */
962 	mutex_enter(&stp->sd_lock);
963 
964 	/* proceed ONLY if user is being audited */
965 	if (!tad->tad_flag) {
966 		/*
967 		 * this is so we will not add audit data onto
968 		 * a thread that is not being audited.
969 		 */
970 		stp->sd_t_audit_data = NULL;
971 		mutex_exit(&stp->sd_lock);
972 		return;
973 	}
974 
975 	stp->sd_t_audit_data = (caddr_t)curthread;
976 	mutex_exit(&stp->sd_lock);
977 }
978 
979 /*
980  * ROUTINE:	AUDIT_CLOSEF
981  * PURPOSE:
982  * CALLBY:	CLOSEF
983  * NOTE:
984  * release per file audit resources when file structure is being released.
985  *
986  * IMPORTANT NOTE: Since we generate an audit record here, we may sleep
987  *	on the audit queue if it becomes full. This means
988  *	audit_closef can not be called when f_count == 0. Since
989  *	f_count == 0 indicates the file structure is free, another
990  *	process could attempt to use the file while we were still
991  *	asleep waiting on the audit queue. This would cause the
992  *	per file audit data to be corrupted when we finally do
993  *	wakeup.
994  * TODO:
995  * QUESTION:
996  */
997 
998 void
999 audit_closef(struct file *fp)
1000 {	/* AUDIT_CLOSEF */
1001 	f_audit_data_t *fad;
1002 	t_audit_data_t *tad;
1003 	int success;
1004 	au_state_t estate;
1005 	struct vnode *vp;
1006 	token_t *ad = NULL;
1007 	struct vattr attr;
1008 	au_emod_t evmod = 0;
1009 	const auditinfo_addr_t *ainfo;
1010 	int getattr_ret;
1011 	cred_t *cr;
1012 	au_kcontext_t	*kctx = GET_KCTX_PZ;
1013 
1014 	fad = F2A(fp);
1015 	estate = kctx->auk_ets[AUE_CLOSE];
1016 	tad = U2A(u);
1017 	cr = CRED();
1018 
1019 	/* audit record already generated by system call envelope */
1020 	if (tad->tad_event == AUE_CLOSE) {
1021 		/* so close audit event will have bits set */
1022 		tad->tad_evmod |= (au_emod_t)fad->fad_flags;
1023 		return;
1024 	}
1025 
1026 	/* if auditing not enabled, then don't generate an audit record */
1027 	if (!((kctx->auk_auditstate == AUC_AUDITING ||
1028 	    kctx->auk_auditstate == AUC_INIT_AUDIT) ||
1029 	    kctx->auk_auditstate == AUC_NOSPACE))
1030 		return;
1031 
1032 	ainfo = crgetauinfo(cr);
1033 	if (ainfo == NULL)
1034 		return;
1035 
1036 	success = ainfo->ai_mask.as_success & estate;
1037 
1038 	/* not selected for this event */
1039 	if (success == 0)
1040 		return;
1041 
1042 	/*
1043 	 * can't use audit_attributes here since we use a private audit area
1044 	 * to build the audit record instead of the one off the thread.
1045 	 */
1046 	if ((vp = fp->f_vnode) != NULL) {
1047 		attr.va_mask = AT_ALL;
1048 		getattr_ret = VOP_GETATTR(vp, &attr, 0, CRED(), NULL);
1049 	}
1050 
1051 	/*
1052 	 * When write was not used and the file can be considered public,
1053 	 * then skip the audit.
1054 	 */
1055 	if ((getattr_ret == 0) && ((fp->f_flag & FWRITE) == 0)) {
1056 		if (file_is_public(&attr)) {
1057 			return;
1058 		}
1059 	}
1060 
1061 	evmod = (au_emod_t)fad->fad_flags;
1062 	if (fad->fad_aupath != NULL) {
1063 		au_write((caddr_t *)&(ad), au_to_path(fad->fad_aupath));
1064 	} else {
1065 #ifdef _LP64
1066 		au_write((caddr_t *)&(ad), au_to_arg64(
1067 			1, "no path: fp", (uint64_t)fp));
1068 #else
1069 		au_write((caddr_t *)&(ad), au_to_arg32(
1070 			1, "no path: fp", (uint32_t)fp));
1071 #endif
1072 	}
1073 
1074 	if (getattr_ret == 0) {
1075 		au_write((caddr_t *)&(ad), au_to_attr(&attr));
1076 		audit_sec_attributes((caddr_t *)&(ad), vp);
1077 	}
1078 
1079 	/* Add subject information */
1080 	AUDIT_SETSUBJ((caddr_t *)&(ad), cr, ainfo, kctx);
1081 
1082 	/* add a return token */
1083 	add_return_token((caddr_t *)&(ad), tad->tad_scid, 0, 0);
1084 
1085 	AS_INC(as_generated, 1, kctx);
1086 	AS_INC(as_kernel, 1, kctx);
1087 
1088 	/*
1089 	 * Close up everything
1090 	 * Note: path space recovery handled by normal system
1091 	 * call envelope if not at last close.
1092 	 * Note there is no failure at this point since
1093 	 *   this represents closes due to exit of process,
1094 	 *   thus we always indicate successful closes.
1095 	 */
1096 	au_close(kctx, (caddr_t *)&(ad), AU_OK | AU_DEFER,
1097 	    AUE_CLOSE, evmod);
1098 }
1099 
1100 /*
1101  * ROUTINE:	AUDIT_SET
1102  * PURPOSE:	Audit the file path and file attributes.
1103  * CALLBY:	SETF
1104  * NOTE:	SETF associate a file pointer with user area's open files.
1105  * TODO:
1106  * call audit_finish directly ???
1107  * QUESTION:
1108  */
1109 
1110 /*ARGSUSED*/
1111 void
1112 audit_setf(file_t *fp, int fd)
1113 {
1114 	f_audit_data_t *fad;
1115 	t_audit_data_t *tad;
1116 
1117 	if (fp == NULL)
1118 		return;
1119 
1120 	tad = T2A(curthread);
1121 	fad = F2A(fp);
1122 
1123 	if (!(tad->tad_scid == SYS_open ||
1124 	    tad->tad_scid == SYS_open64 ||
1125 	    tad->tad_scid == SYS_openat ||
1126 	    tad->tad_scid == SYS_openat64))
1127 		return;
1128 
1129 	/* no path */
1130 	if (tad->tad_aupath == 0)
1131 		return;
1132 
1133 	/*
1134 	 * assign path information associated with file audit data
1135 	 * use tad hold
1136 	 */
1137 	fad->fad_aupath = tad->tad_aupath;
1138 	tad->tad_aupath = NULL;
1139 	tad->tad_vn = NULL;
1140 
1141 	if (!(tad->tad_ctrl & PAD_TRUE_CREATE)) {
1142 		/* adjust event type by dropping the 'creat' part */
1143 		switch (tad->tad_event) {
1144 		case AUE_OPEN_RC:
1145 			tad->tad_event = AUE_OPEN_R;
1146 			tad->tad_ctrl |= PAD_PUBLIC_EV;
1147 			break;
1148 		case AUE_OPEN_RTC:
1149 			tad->tad_event = AUE_OPEN_RT;
1150 			break;
1151 		case AUE_OPEN_WC:
1152 			tad->tad_event = AUE_OPEN_W;
1153 			break;
1154 		case AUE_OPEN_WTC:
1155 			tad->tad_event = AUE_OPEN_WT;
1156 			break;
1157 		case AUE_OPEN_RWC:
1158 			tad->tad_event = AUE_OPEN_RW;
1159 			break;
1160 		case AUE_OPEN_RWTC:
1161 			tad->tad_event = AUE_OPEN_RWT;
1162 			break;
1163 		default:
1164 			break;
1165 		}
1166 	}
1167 }
1168 
1169 
1170 /*
1171  * ROUTINE:	AUDIT_COPEN
1172  * PURPOSE:
1173  * CALLBY:	COPEN
1174  * NOTE:
1175  * TODO:
1176  * QUESTION:
1177  */
1178 /*ARGSUSED*/
1179 void
1180 audit_copen(int fd, file_t *fp, vnode_t *vp)
1181 {
1182 }
1183 
1184 void
1185 audit_ipc(int type, int id, void *vp)
1186 {
1187 	/* if not auditing this event, then do nothing */
1188 	if (ad_flag == 0)
1189 		return;
1190 
1191 	switch (type) {
1192 	case AT_IPC_MSG:
1193 		au_uwrite(au_to_ipc(AT_IPC_MSG, id));
1194 		au_uwrite(au_to_ipc_perm(&(((kmsqid_t *)vp)->msg_perm)));
1195 		break;
1196 	case AT_IPC_SEM:
1197 		au_uwrite(au_to_ipc(AT_IPC_SEM, id));
1198 		au_uwrite(au_to_ipc_perm(&(((ksemid_t *)vp)->sem_perm)));
1199 		break;
1200 	case AT_IPC_SHM:
1201 		au_uwrite(au_to_ipc(AT_IPC_SHM, id));
1202 		au_uwrite(au_to_ipc_perm(&(((kshmid_t *)vp)->shm_perm)));
1203 		break;
1204 	}
1205 }
1206 
1207 void
1208 audit_ipcget(int type, void *vp)
1209 {
1210 	/* if not auditing this event, then do nothing */
1211 	if (ad_flag == 0)
1212 		return;
1213 
1214 	switch (type) {
1215 	case NULL:
1216 		au_uwrite(au_to_ipc_perm((struct kipc_perm *)vp));
1217 		break;
1218 	case AT_IPC_MSG:
1219 		au_uwrite(au_to_ipc_perm(&(((kmsqid_t *)vp)->msg_perm)));
1220 		break;
1221 	case AT_IPC_SEM:
1222 		au_uwrite(au_to_ipc_perm(&(((ksemid_t *)vp)->sem_perm)));
1223 		break;
1224 	case AT_IPC_SHM:
1225 		au_uwrite(au_to_ipc_perm(&(((kshmid_t *)vp)->shm_perm)));
1226 		break;
1227 	}
1228 }
1229 
1230 /*
1231  * ROUTINE:	AUDIT_REBOOT
1232  * PURPOSE:
1233  * CALLBY:
1234  * NOTE:
1235  * At this point we know that the system call reboot will not return. We thus
1236  * have to complete the audit record generation and put it onto the queue.
1237  * This might be fairly useless if the auditing daemon is already dead....
1238  * TODO:
1239  * QUESTION:	who calls audit_reboot
1240  */
1241 
1242 void
1243 audit_reboot(void)
1244 {
1245 	int flag;
1246 	t_audit_data_t *tad;
1247 	au_kcontext_t	*kctx = GET_KCTX_PZ;
1248 
1249 	tad = U2A(u);
1250 
1251 	/* if not auditing this event, then do nothing */
1252 	if (tad->tad_flag == 0)
1253 		return;
1254 
1255 	/* do preselection on success/failure */
1256 	if (flag = audit_success(kctx, tad, 0, NULL)) {
1257 		/* add a process token */
1258 
1259 		cred_t *cr = CRED();
1260 		const auditinfo_addr_t *ainfo = crgetauinfo(cr);
1261 
1262 		if (ainfo == NULL)
1263 			return;
1264 
1265 		/* Add subject information */
1266 		AUDIT_SETSUBJ(&(u_ad), cr, ainfo, kctx);
1267 
1268 		/* add a return token */
1269 		add_return_token((caddr_t *)&(u_ad), tad->tad_scid, 0, 0);
1270 
1271 		AS_INC(as_generated, 1, kctx);
1272 		AS_INC(as_kernel, 1, kctx);
1273 	}
1274 
1275 	/*
1276 	 * Flow control useless here since we're going
1277 	 * to drop everything in the queue anyway. Why
1278 	 * block and wait. There aint anyone left alive to
1279 	 * read the records remaining anyway.
1280 	 */
1281 
1282 	/* Close up everything */
1283 	au_close(kctx, &(u_ad), flag | AU_DONTBLOCK,
1284 	    tad->tad_event, tad->tad_evmod);
1285 }
1286 
1287 void
1288 audit_setfsat_path(int argnum)
1289 {
1290 	klwp_id_t clwp = ttolwp(curthread);
1291 	struct file  *fp;
1292 	uint32_t fd;
1293 	t_audit_data_t *tad;
1294 	struct f_audit_data *fad;
1295 	p_audit_data_t *pad;	/* current process */
1296 	struct a {
1297 		long arg1;
1298 		long arg2;
1299 		long arg3;
1300 		long arg4;
1301 		long arg5;
1302 	} *uap;
1303 
1304 	if (clwp == NULL)
1305 		return;
1306 	uap = (struct a *)clwp->lwp_ap;
1307 
1308 	tad = U2A(u);
1309 	ASSERT(tad != NULL);
1310 
1311 	switch (tad->tad_scid) {
1312 	case SYS_faccessat:
1313 	case SYS_fchownat:
1314 	case SYS_fstatat:
1315 	case SYS_fstatat64:
1316 	case SYS_openat:
1317 	case SYS_openat64:
1318 	case SYS_unlinkat:
1319 		fd = uap->arg1;
1320 		break;
1321 	case SYS_renameat:
1322 		if (argnum == 3)
1323 			fd = uap->arg3;
1324 		else
1325 			fd = uap->arg1;
1326 		break;
1327 	case SYS_utimesys:
1328 		fd = uap->arg2;
1329 		break;
1330 	default:
1331 		return;
1332 	}
1333 
1334 	if (tad->tad_atpath != NULL) {
1335 		au_pathrele(tad->tad_atpath);
1336 		tad->tad_atpath = NULL;
1337 	}
1338 	if (fd != AT_FDCWD) {
1339 		if ((fp = getf(fd)) == NULL) {
1340 			tad->tad_ctrl |= PAD_NOPATH;
1341 			return;
1342 		}
1343 		fad = F2A(fp);
1344 		ASSERT(fad);
1345 		if (fad->fad_aupath == NULL) {
1346 			tad->tad_ctrl |= PAD_NOPATH;
1347 			releasef(fd);
1348 			return;
1349 		}
1350 		au_pathhold(fad->fad_aupath);
1351 		tad->tad_atpath = fad->fad_aupath;
1352 		releasef(fd);
1353 	} else {
1354 		pad = P2A(curproc);
1355 		mutex_enter(&pad->pad_lock);
1356 		au_pathhold(pad->pad_cwd);
1357 		tad->tad_atpath = pad->pad_cwd;
1358 		mutex_exit(&pad->pad_lock);
1359 	}
1360 }
1361 
1362 void
1363 audit_symlink_create(vnode_t *dvp, char *sname, char *target, int error)
1364 {
1365 	t_audit_data_t *tad;
1366 	vnode_t	*vp;
1367 
1368 	tad = U2A(u);
1369 
1370 	/* if not auditing this event, then do nothing */
1371 	if (tad->tad_flag == 0)
1372 		return;
1373 
1374 	au_uwrite(au_to_text(target));
1375 
1376 	if (error)
1377 		return;
1378 
1379 	error = VOP_LOOKUP(dvp, sname, &vp, NULL, 0, NULL, CRED(),
1380 	    NULL, NULL, NULL);
1381 	if (error == 0) {
1382 		audit_attributes(vp);
1383 		VN_RELE(vp);
1384 	}
1385 }
1386 
1387 /*
1388  * ROUTINE:	AUDIT_VNCREATE_START
1389  * PURPOSE:	set flag so path name lookup in create will not add attribute
1390  * CALLBY:	VN_CREATE
1391  * NOTE:
1392  * TODO:
1393  * QUESTION:
1394  */
1395 
1396 void
1397 audit_vncreate_start()
1398 {
1399 	t_audit_data_t *tad;
1400 
1401 	tad = U2A(u);
1402 	tad->tad_ctrl |= PAD_NOATTRB;
1403 }
1404 
1405 /*
1406  * ROUTINE:	AUDIT_VNCREATE_FINISH
1407  * PURPOSE:
1408  * CALLBY:	VN_CREATE
1409  * NOTE:
1410  * TODO:
1411  * QUESTION:
1412  */
1413 void
1414 audit_vncreate_finish(struct vnode *vp, int error)
1415 {
1416 	t_audit_data_t *tad;
1417 
1418 	if (error)
1419 		return;
1420 
1421 	tad = U2A(u);
1422 
1423 	/* if not auditing this event, then do nothing */
1424 	if (tad->tad_flag == 0)
1425 		return;
1426 
1427 	if (tad->tad_ctrl & PAD_TRUE_CREATE) {
1428 		audit_attributes(vp);
1429 	}
1430 
1431 	if (tad->tad_ctrl & PAD_CORE) {
1432 		audit_attributes(vp);
1433 		tad->tad_ctrl &= ~PAD_CORE;
1434 	}
1435 
1436 	if (!error && ((tad->tad_event == AUE_MKNOD) ||
1437 	    (tad->tad_event == AUE_MKDIR))) {
1438 		audit_attributes(vp);
1439 	}
1440 
1441 	/* for case where multiple lookups in one syscall (rename) */
1442 	tad->tad_ctrl &= ~PAD_NOATTRB;
1443 }
1444 
1445 
1446 
1447 
1448 
1449 
1450 
1451 
1452 /*
1453  * ROUTINE:	AUDIT_EXEC
1454  * PURPOSE:	Records the function arguments and environment variables
1455  * CALLBY:	EXEC_ARGS
1456  * NOTE:
1457  * TODO:
1458  * QUESTION:
1459  */
1460 
1461 /*ARGSUSED*/
1462 void
1463 audit_exec(
1464 	const char *argstr,	/* argument strings */
1465 	const char *envstr,	/* environment strings */
1466 	ssize_t argc,		/* total # arguments */
1467 	ssize_t envc)		/* total # environment variables */
1468 {
1469 	t_audit_data_t *tad;
1470 	au_kcontext_t	*kctx = GET_KCTX_PZ;
1471 
1472 	tad = U2A(u);
1473 
1474 	/* if not auditing this event, then do nothing */
1475 	if (!tad->tad_flag)
1476 		return;
1477 
1478 	/* return if not interested in argv or environment variables */
1479 	if (!(kctx->auk_policy & (AUDIT_ARGV|AUDIT_ARGE)))
1480 		return;
1481 
1482 	if (kctx->auk_policy & AUDIT_ARGV) {
1483 		au_uwrite(au_to_exec_args(argstr, argc));
1484 	}
1485 
1486 	if (kctx->auk_policy & AUDIT_ARGE) {
1487 		au_uwrite(au_to_exec_env(envstr, envc));
1488 	}
1489 }
1490 
1491 /*
1492  * ROUTINE:	AUDIT_ENTERPROM
1493  * PURPOSE:
1494  * CALLBY:	KBDINPUT
1495  *		ZSA_XSINT
1496  * NOTE:
1497  * TODO:
1498  * QUESTION:
1499  */
1500 void
1501 audit_enterprom(int flg)
1502 {
1503 	token_t *rp = NULL;
1504 	int sorf;
1505 
1506 	if (flg)
1507 		sorf = AUM_SUCC;
1508 	else
1509 		sorf = AUM_FAIL;
1510 
1511 	AUDIT_ASYNC_START(rp, AUE_ENTERPROM, sorf);
1512 
1513 	au_write((caddr_t *)&(rp), au_to_text("kmdb"));
1514 
1515 	if (flg)
1516 		au_write((caddr_t *)&(rp), au_to_return32(0, 0));
1517 	else
1518 		au_write((caddr_t *)&(rp), au_to_return32(ECANCELED, 0));
1519 
1520 	AUDIT_ASYNC_FINISH(rp, AUE_ENTERPROM, NULL);
1521 }
1522 
1523 
1524 /*
1525  * ROUTINE:	AUDIT_EXITPROM
1526  * PURPOSE:
1527  * CALLBY:	KBDINPUT
1528  *		ZSA_XSINT
1529  * NOTE:
1530  * TODO:
1531  * QUESTION:
1532  */
1533 void
1534 audit_exitprom(int flg)
1535 {
1536 	int sorf;
1537 	token_t *rp = NULL;
1538 
1539 	if (flg)
1540 		sorf = AUM_SUCC;
1541 	else
1542 		sorf = AUM_FAIL;
1543 
1544 	AUDIT_ASYNC_START(rp, AUE_EXITPROM, sorf);
1545 
1546 	au_write((caddr_t *)&(rp), au_to_text("kmdb"));
1547 
1548 	if (flg)
1549 		au_write((caddr_t *)&(rp), au_to_return32(0, 0));
1550 	else
1551 		au_write((caddr_t *)&(rp), au_to_return32(ECANCELED, 0));
1552 
1553 	AUDIT_ASYNC_FINISH(rp, AUE_EXITPROM, NULL);
1554 }
1555 
1556 struct fcntla {
1557 	int fdes;
1558 	int cmd;
1559 	intptr_t arg;
1560 };
1561 
1562 /*
1563  * ROUTINE:	AUDIT_C2_REVOKE
1564  * PURPOSE:
1565  * CALLBY:	FCNTL
1566  * NOTE:
1567  * TODO:
1568  * QUESTION:	are we keeping this func
1569  */
1570 
1571 /*ARGSUSED*/
1572 int
1573 audit_c2_revoke(struct fcntla *uap, rval_t *rvp)
1574 {
1575 	return (0);
1576 }
1577 
1578 
1579 /*
1580  * ROUTINE:	AUDIT_CHDIREC
1581  * PURPOSE:
1582  * CALLBY:	CHDIREC
1583  * NOTE:	The main function of CHDIREC
1584  * TODO:	Move the audit_chdirec hook above the VN_RELE in vncalls.c
1585  * QUESTION:
1586  */
1587 
1588 /*ARGSUSED*/
1589 void
1590 audit_chdirec(vnode_t *vp, vnode_t **vpp)
1591 {
1592 	int		chdir;
1593 	int		fchdir;
1594 	struct audit_path	**appp;
1595 	struct file	*fp;
1596 	f_audit_data_t *fad;
1597 	p_audit_data_t *pad = P2A(curproc);
1598 	t_audit_data_t *tad = T2A(curthread);
1599 
1600 	struct a {
1601 		long fd;
1602 	} *uap = (struct a *)ttolwp(curthread)->lwp_ap;
1603 
1604 	if ((tad->tad_scid == SYS_chdir) || (tad->tad_scid == SYS_chroot)) {
1605 		chdir = tad->tad_scid == SYS_chdir;
1606 		if (tad->tad_aupath) {
1607 			mutex_enter(&pad->pad_lock);
1608 			if (chdir)
1609 				appp = &(pad->pad_cwd);
1610 			else
1611 				appp = &(pad->pad_root);
1612 			au_pathrele(*appp);
1613 			/* use tad hold */
1614 			*appp = tad->tad_aupath;
1615 			tad->tad_aupath = NULL;
1616 			mutex_exit(&pad->pad_lock);
1617 		}
1618 	} else if ((tad->tad_scid == SYS_fchdir) ||
1619 	    (tad->tad_scid == SYS_fchroot)) {
1620 		fchdir = tad->tad_scid == SYS_fchdir;
1621 		if ((fp = getf(uap->fd)) == NULL)
1622 			return;
1623 		fad = F2A(fp);
1624 		if (fad->fad_aupath) {
1625 			au_pathhold(fad->fad_aupath);
1626 			mutex_enter(&pad->pad_lock);
1627 			if (fchdir)
1628 				appp = &(pad->pad_cwd);
1629 			else
1630 				appp = &(pad->pad_root);
1631 			au_pathrele(*appp);
1632 			*appp = fad->fad_aupath;
1633 			mutex_exit(&pad->pad_lock);
1634 			if (tad->tad_flag) {
1635 				au_uwrite(au_to_path(fad->fad_aupath));
1636 				audit_attributes(fp->f_vnode);
1637 			}
1638 		}
1639 		releasef(uap->fd);
1640 	}
1641 }
1642 
1643 /*
1644  * ROUTINE:	AUDIT_GETF
1645  * PURPOSE:
1646  * CALLBY:	GETF_INTERNAL
1647  * NOTE:	The main function of GETF_INTERNAL is to associate a given
1648  *		file descriptor with a file structure and increment the
1649  *		file pointer reference count.
1650  * TODO:	remove pass in of fpp.
1651  * increment a reference count so that even if a thread with same process delete
1652  * the same object, it will not panic our system
1653  * QUESTION:
1654  * where to decrement the f_count?????????????????
1655  * seems like I need to set a flag if f_count incremented through audit_getf
1656  */
1657 
1658 /*ARGSUSED*/
1659 int
1660 audit_getf(int fd)
1661 {
1662 #ifdef NOTYET
1663 	t_audit_data_t *tad;
1664 
1665 	tad = T2A(curthread);
1666 
1667 	if (!(tad->tad_scid == SYS_openat ||
1668 	    tad->tad_scid == SYS_openat64 ||
1669 	    tad->tad_scid == SYS_open ||
1670 	    tad->tad_scid == SYS_open64))
1671 		return (0);
1672 #endif
1673 	return (0);
1674 }
1675 
1676 /*
1677  *	Audit hook for stream based socket and tli request.
1678  *	Note that we do not have user context while executing
1679  *	this code so we had to record them earlier during the
1680  *	putmsg/getmsg to figure out which user we are dealing with.
1681  */
1682 
1683 /*ARGSUSED*/
1684 void
1685 audit_sock(
1686 	int type,	/* type of tihdr.h header requests */
1687 	queue_t *q,	/* contains the process and thread audit data */
1688 	mblk_t *mp,	/* contains the tihdr.h header structures */
1689 	int from)	/* timod or sockmod request */
1690 {
1691 	int32_t    len;
1692 	int32_t    offset;
1693 	struct sockaddr_in *sock_data;
1694 	struct T_conn_req *conn_req;
1695 	struct T_conn_ind *conn_ind;
1696 	struct T_unitdata_req *unitdata_req;
1697 	struct T_unitdata_ind *unitdata_ind;
1698 	au_state_t estate;
1699 	t_audit_data_t *tad;
1700 	caddr_t saved_thread_ptr;
1701 	au_mask_t amask;
1702 	const auditinfo_addr_t *ainfo;
1703 	au_kcontext_t	*kctx;
1704 
1705 	if (q->q_stream == NULL)
1706 		return;
1707 	mutex_enter(&q->q_stream->sd_lock);
1708 	/* are we being audited */
1709 	saved_thread_ptr = q->q_stream->sd_t_audit_data;
1710 	/* no pointer to thread, nothing to do */
1711 	if (saved_thread_ptr == NULL) {
1712 		mutex_exit(&q->q_stream->sd_lock);
1713 		return;
1714 	}
1715 	/* only allow one addition of a record token */
1716 	q->q_stream->sd_t_audit_data = NULL;
1717 	/*
1718 	 * thread is not the one being audited, then nothing to do
1719 	 * This could be the stream thread handling the module
1720 	 * service routine. In this case, the context for the audit
1721 	 * record can no longer be assumed. Simplest to just drop
1722 	 * the operation.
1723 	 */
1724 	if (curthread != (kthread_id_t)saved_thread_ptr) {
1725 		mutex_exit(&q->q_stream->sd_lock);
1726 		return;
1727 	}
1728 	if (curthread->t_sysnum >= SYS_so_socket &&
1729 	    curthread->t_sysnum <= SYS_sockconfig) {
1730 		mutex_exit(&q->q_stream->sd_lock);
1731 		return;
1732 	}
1733 	mutex_exit(&q->q_stream->sd_lock);
1734 	/*
1735 	 * we know that the thread that did the put/getmsg is the
1736 	 * one running. Now we can get the TAD and see if we should
1737 	 * add an audit token.
1738 	 */
1739 	tad = U2A(u);
1740 
1741 	kctx = GET_KCTX_PZ;
1742 
1743 	/* proceed ONLY if user is being audited */
1744 	if (!tad->tad_flag)
1745 		return;
1746 
1747 	ainfo = crgetauinfo(CRED());
1748 	if (ainfo == NULL)
1749 		return;
1750 	amask = ainfo->ai_mask;
1751 
1752 	/*
1753 	 * Figure out the type of stream networking request here.
1754 	 * Note that getmsg and putmsg are always preselected
1755 	 * because during the beginning of the system call we have
1756 	 * not yet figure out which of the socket or tli request
1757 	 * we are looking at until we are here. So we need to check
1758 	 * against that specific request and reset the type of event.
1759 	 */
1760 	switch (type) {
1761 	case T_CONN_REQ:	/* connection request */
1762 		conn_req = (struct T_conn_req *)mp->b_rptr;
1763 		if (conn_req->DEST_offset < sizeof (struct T_conn_req))
1764 			return;
1765 		offset = conn_req->DEST_offset;
1766 		len = conn_req->DEST_length;
1767 		estate = kctx->auk_ets[AUE_SOCKCONNECT];
1768 		if (amask.as_success & estate || amask.as_failure & estate) {
1769 			tad->tad_event = AUE_SOCKCONNECT;
1770 			break;
1771 		} else {
1772 			return;
1773 		}
1774 	case T_CONN_IND:	 /* connectionless receive request */
1775 		conn_ind = (struct T_conn_ind *)mp->b_rptr;
1776 		if (conn_ind->SRC_offset < sizeof (struct T_conn_ind))
1777 			return;
1778 		offset = conn_ind->SRC_offset;
1779 		len = conn_ind->SRC_length;
1780 		estate = kctx->auk_ets[AUE_SOCKACCEPT];
1781 		if (amask.as_success & estate || amask.as_failure & estate) {
1782 			tad->tad_event = AUE_SOCKACCEPT;
1783 			break;
1784 		} else {
1785 			return;
1786 		}
1787 	case T_UNITDATA_REQ:	 /* connectionless send request */
1788 		unitdata_req = (struct T_unitdata_req *)mp->b_rptr;
1789 		if (unitdata_req->DEST_offset < sizeof (struct T_unitdata_req))
1790 			return;
1791 		offset = unitdata_req->DEST_offset;
1792 		len = unitdata_req->DEST_length;
1793 		estate = kctx->auk_ets[AUE_SOCKSEND];
1794 		if (amask.as_success & estate || amask.as_failure & estate) {
1795 			tad->tad_event = AUE_SOCKSEND;
1796 			break;
1797 		} else {
1798 			return;
1799 		}
1800 	case T_UNITDATA_IND:	 /* connectionless receive request */
1801 		unitdata_ind = (struct T_unitdata_ind *)mp->b_rptr;
1802 		if (unitdata_ind->SRC_offset < sizeof (struct T_unitdata_ind))
1803 			return;
1804 		offset = unitdata_ind->SRC_offset;
1805 		len = unitdata_ind->SRC_length;
1806 		estate = kctx->auk_ets[AUE_SOCKRECEIVE];
1807 		if (amask.as_success & estate || amask.as_failure & estate) {
1808 			tad->tad_event = AUE_SOCKRECEIVE;
1809 			break;
1810 		} else {
1811 			return;
1812 		}
1813 	default:
1814 		return;
1815 	}
1816 
1817 	/*
1818 	 * we are only interested in tcp stream connections,
1819 	 * not unix domain stuff
1820 	 */
1821 	if ((len < 0) || (len > sizeof (struct sockaddr_in))) {
1822 		tad->tad_event = AUE_GETMSG;
1823 		return;
1824 	}
1825 	/* skip over TPI header and point to the ip address */
1826 	sock_data = (struct sockaddr_in *)((char *)mp->b_rptr + offset);
1827 
1828 	switch (sock_data->sin_family) {
1829 	case AF_INET:
1830 		au_write(&(tad->tad_ad), au_to_sock_inet(sock_data));
1831 		break;
1832 	default:	/* reset to AUE_PUTMSG if not a inet request */
1833 		tad->tad_event = AUE_GETMSG;
1834 		break;
1835 	}
1836 }
1837 
1838 void
1839 audit_lookupname()
1840 {
1841 }
1842 
1843 /*ARGSUSED*/
1844 int
1845 audit_pathcomp(struct pathname *pnp, vnode_t *cvp, cred_t *cr)
1846 {
1847 	return (0);
1848 }
1849 
1850 static void
1851 add_return_token(caddr_t *ad, unsigned int scid, int err, int rval)
1852 {
1853 	unsigned int sy_flags;
1854 
1855 #ifdef _SYSCALL32_IMPL
1856 	/*
1857 	 * Guard against t_lwp being NULL when this function is called
1858 	 * from a kernel queue instead of from a direct system call.
1859 	 * In that case, assume the running kernel data model.
1860 	 */
1861 	if ((curthread->t_lwp == NULL) || (lwp_getdatamodel(
1862 	    ttolwp(curthread)) == DATAMODEL_NATIVE))
1863 		sy_flags = sysent[scid].sy_flags & SE_RVAL_MASK;
1864 	else
1865 		sy_flags = sysent32[scid].sy_flags & SE_RVAL_MASK;
1866 #else
1867 		sy_flags = sysent[scid].sy_flags & SE_RVAL_MASK;
1868 #endif
1869 
1870 	if (sy_flags == SE_64RVAL)
1871 		au_write(ad, au_to_return64(err, rval));
1872 	else
1873 		au_write(ad, au_to_return32(err, rval));
1874 
1875 }
1876 
1877 /*ARGSUSED*/
1878 void
1879 audit_fdsend(fd, fp, error)
1880 	int fd;
1881 	struct file *fp;
1882 	int error;		/* ignore for now */
1883 {
1884 	t_audit_data_t *tad;	/* current thread */
1885 	f_audit_data_t *fad;	/* per file audit structure */
1886 	struct vnode *vp;	/* for file attributes */
1887 
1888 	/* is this system call being audited */
1889 	tad = U2A(u);
1890 	ASSERT(tad != (t_audit_data_t *)0);
1891 	if (!tad->tad_flag)
1892 		return;
1893 
1894 	fad = F2A(fp);
1895 
1896 	/* add path and file attributes */
1897 	if (fad != NULL && fad->fad_aupath != NULL) {
1898 		au_uwrite(au_to_arg32(0, "send fd", (uint32_t)fd));
1899 		au_uwrite(au_to_path(fad->fad_aupath));
1900 	} else {
1901 		au_uwrite(au_to_arg32(0, "send fd", (uint32_t)fd));
1902 #ifdef _LP64
1903 		au_uwrite(au_to_arg64(0, "no path", (uint64_t)fp));
1904 #else
1905 		au_uwrite(au_to_arg32(0, "no path", (uint32_t)fp));
1906 #endif
1907 	}
1908 	vp = fp->f_vnode;	/* include vnode attributes */
1909 	audit_attributes(vp);
1910 }
1911 
1912 /*
1913  * Record privileges successfully used and we attempted to use but
1914  * didn't have.
1915  */
1916 void
1917 audit_priv(int priv, const priv_set_t *set, int flag)
1918 {
1919 	t_audit_data_t *tad;
1920 	int sbit;
1921 	priv_set_t *target;
1922 
1923 	/* Make sure this isn't being called in an interrupt context */
1924 	ASSERT(servicing_interrupt() == 0);
1925 
1926 	tad = U2A(u);
1927 
1928 	if (tad->tad_flag == 0)
1929 		return;
1930 
1931 	target = flag ? &tad->tad_sprivs : &tad->tad_fprivs;
1932 	sbit = flag ? PAD_SPRIVUSE : PAD_FPRIVUSE;
1933 
1934 	/* Tell audit_success() and audit_finish() that we saw this case */
1935 	if (!(tad->tad_evmod & sbit)) {
1936 		/* Clear set first time around */
1937 		priv_emptyset(target);
1938 		tad->tad_evmod |= sbit;
1939 	}
1940 
1941 	/* Save the privileges in the tad */
1942 	if (priv == PRIV_ALL) {
1943 		priv_fillset(target);
1944 	} else {
1945 		ASSERT(set != NULL || priv != PRIV_NONE);
1946 		if (set != NULL)
1947 			priv_union(set, target);
1948 		if (priv != PRIV_NONE)
1949 			priv_addset(target, priv);
1950 	}
1951 }
1952 
1953 /*
1954  * Audit the setpriv() system call; the operation, the set name and
1955  * the current value as well as the set argument are put in the
1956  * audit trail.
1957  */
1958 void
1959 audit_setppriv(int op, int set, const priv_set_t *newpriv, const cred_t *ocr)
1960 {
1961 	t_audit_data_t *tad;
1962 	const priv_set_t *oldpriv;
1963 	priv_set_t report;
1964 	const char *setname;
1965 
1966 	tad = U2A(u);
1967 
1968 	if (tad->tad_flag == 0)
1969 		return;
1970 
1971 	oldpriv = priv_getset(ocr, set);
1972 
1973 	/* Generate the actual record, include the before and after */
1974 	au_uwrite(au_to_arg32(2, "op", op));
1975 	setname = priv_getsetbynum(set);
1976 
1977 	switch (op) {
1978 	case PRIV_OFF:
1979 		/* Report privileges actually switched off */
1980 		report = *oldpriv;
1981 		priv_intersect(newpriv, &report);
1982 		au_uwrite(au_to_privset(setname, &report, AUT_PRIV, 0));
1983 		break;
1984 	case PRIV_ON:
1985 		/* Report privileges actually switched on */
1986 		report = *oldpriv;
1987 		priv_inverse(&report);
1988 		priv_intersect(newpriv, &report);
1989 		au_uwrite(au_to_privset(setname, &report, AUT_PRIV, 0));
1990 		break;
1991 	case PRIV_SET:
1992 		/* Report before and after */
1993 		au_uwrite(au_to_privset(setname, oldpriv, AUT_PRIV, 0));
1994 		au_uwrite(au_to_privset(setname, newpriv, AUT_PRIV, 0));
1995 		break;
1996 	}
1997 }
1998 
1999 /*
2000  * Dump the full device policy setting in the audit trail.
2001  */
2002 void
2003 audit_devpolicy(int nitems, const devplcysys_t *items)
2004 {
2005 	t_audit_data_t *tad;
2006 	int i;
2007 
2008 	tad = U2A(u);
2009 
2010 	if (tad->tad_flag == 0)
2011 		return;
2012 
2013 	for (i = 0; i < nitems; i++) {
2014 		au_uwrite(au_to_arg32(2, "major", items[i].dps_maj));
2015 		if (items[i].dps_minornm[0] == '\0') {
2016 			au_uwrite(au_to_arg32(2, "lomin", items[i].dps_lomin));
2017 			au_uwrite(au_to_arg32(2, "himin", items[i].dps_himin));
2018 		} else
2019 			au_uwrite(au_to_text(items[i].dps_minornm));
2020 
2021 		au_uwrite(au_to_privset("read", &items[i].dps_rdp,
2022 		    AUT_PRIV, 0));
2023 		au_uwrite(au_to_privset("write", &items[i].dps_wrp,
2024 		    AUT_PRIV, 0));
2025 	}
2026 }
2027 
2028 /*ARGSUSED*/
2029 void
2030 audit_fdrecv(fd, fp)
2031 	int fd;
2032 	struct file *fp;
2033 {
2034 	t_audit_data_t *tad;	/* current thread */
2035 	f_audit_data_t *fad;	/* per file audit structure */
2036 	struct vnode *vp;	/* for file attributes */
2037 
2038 	/* is this system call being audited */
2039 	tad = U2A(u);
2040 	ASSERT(tad != (t_audit_data_t *)0);
2041 	if (!tad->tad_flag)
2042 		return;
2043 
2044 	fad = F2A(fp);
2045 
2046 	/* add path and file attributes */
2047 	if (fad != NULL && fad->fad_aupath != NULL) {
2048 		au_uwrite(au_to_arg32(0, "recv fd", (uint32_t)fd));
2049 		au_uwrite(au_to_path(fad->fad_aupath));
2050 	} else {
2051 		au_uwrite(au_to_arg32(0, "recv fd", (uint32_t)fd));
2052 #ifdef _LP64
2053 		au_uwrite(au_to_arg64(0, "no path", (uint64_t)fp));
2054 #else
2055 		au_uwrite(au_to_arg32(0, "no path", (uint32_t)fp));
2056 #endif
2057 	}
2058 	vp = fp->f_vnode;	/* include vnode attributes */
2059 	audit_attributes(vp);
2060 }
2061 
2062 /*
2063  * ROUTINE:	AUDIT_CRYPTOADM
2064  * PURPOSE:	Records arguments to administrative ioctls on /dev/cryptoadm
2065  * CALLBY:	CRYPTO_LOAD_DEV_DISABLED, CRYPTO_LOAD_SOFT_DISABLED,
2066  *		CRYPTO_UNLOAD_SOFT_MODULE, CRYPTO_LOAD_SOFT_CONFIG,
2067  *		CRYPTO_POOL_CREATE, CRYPTO_POOL_WAIT, CRYPTO_POOL_RUN,
2068  *		CRYPTO_LOAD_DOOR
2069  * NOTE:
2070  * TODO:
2071  * QUESTION:
2072  */
2073 
2074 void
2075 audit_cryptoadm(int cmd, char *module_name, crypto_mech_name_t *mech_names,
2076     uint_t mech_count, uint_t device_instance, uint32_t rv, int error)
2077 {
2078 	boolean_t		mech_list_required = B_FALSE;
2079 	cred_t			*cr = CRED();
2080 	t_audit_data_t		*tad;
2081 	token_t			*ad = NULL;
2082 	const auditinfo_addr_t	*ainfo = crgetauinfo(cr);
2083 	char			buffer[MAXNAMELEN * 2];
2084 	au_kcontext_t		*kctx = GET_KCTX_PZ;
2085 
2086 	tad = U2A(u);
2087 	if (tad == NULL)
2088 		return;
2089 
2090 	if (ainfo == NULL)
2091 		return;
2092 
2093 	tad->tad_event = AUE_CRYPTOADM;
2094 
2095 	if (audit_success(kctx, tad, error, NULL) != AU_OK)
2096 		return;
2097 
2098 	/* Add subject information */
2099 	AUDIT_SETSUBJ((caddr_t *)&(ad), cr, ainfo, kctx);
2100 
2101 	switch (cmd) {
2102 	case CRYPTO_LOAD_DEV_DISABLED:
2103 		if (error == 0 && rv == CRYPTO_SUCCESS) {
2104 			(void) snprintf(buffer, sizeof (buffer),
2105 			    "op=CRYPTO_LOAD_DEV_DISABLED, module=%s,"
2106 			    " dev_instance=%d",
2107 			    module_name, device_instance);
2108 			mech_list_required = B_TRUE;
2109 		} else {
2110 			(void) snprintf(buffer, sizeof (buffer),
2111 			    "op=CRYPTO_LOAD_DEV_DISABLED, return_val=%d", rv);
2112 		}
2113 		break;
2114 
2115 	case CRYPTO_LOAD_SOFT_DISABLED:
2116 		if (error == 0 && rv == CRYPTO_SUCCESS) {
2117 			(void) snprintf(buffer, sizeof (buffer),
2118 			    "op=CRYPTO_LOAD_SOFT_DISABLED, module=%s",
2119 			    module_name);
2120 			mech_list_required = B_TRUE;
2121 		} else {
2122 			(void) snprintf(buffer, sizeof (buffer),
2123 			    "op=CRYPTO_LOAD_SOFT_DISABLED, return_val=%d", rv);
2124 		}
2125 		break;
2126 
2127 	case CRYPTO_UNLOAD_SOFT_MODULE:
2128 		if (error == 0 && rv == CRYPTO_SUCCESS) {
2129 			(void) snprintf(buffer, sizeof (buffer),
2130 			    "op=CRYPTO_UNLOAD_SOFT_MODULE, module=%s",
2131 			    module_name);
2132 		} else {
2133 			(void) snprintf(buffer, sizeof (buffer),
2134 			    "op=CRYPTO_UNLOAD_SOFT_MODULE, return_val=%d", rv);
2135 		}
2136 		break;
2137 
2138 	case CRYPTO_LOAD_SOFT_CONFIG:
2139 		if (error == 0 && rv == CRYPTO_SUCCESS) {
2140 			(void) snprintf(buffer, sizeof (buffer),
2141 			    "op=CRYPTO_LOAD_SOFT_CONFIG, module=%s",
2142 			    module_name);
2143 			mech_list_required = B_TRUE;
2144 		} else {
2145 			(void) snprintf(buffer, sizeof (buffer),
2146 			    "op=CRYPTO_LOAD_SOFT_CONFIG, return_val=%d", rv);
2147 		}
2148 		break;
2149 
2150 	case CRYPTO_POOL_CREATE:
2151 		(void) snprintf(buffer, sizeof (buffer),
2152 		    "op=CRYPTO_POOL_CREATE");
2153 		break;
2154 
2155 	case CRYPTO_POOL_WAIT:
2156 		(void) snprintf(buffer, sizeof (buffer), "op=CRYPTO_POOL_WAIT");
2157 		break;
2158 
2159 	case CRYPTO_POOL_RUN:
2160 		(void) snprintf(buffer, sizeof (buffer), "op=CRYPTO_POOL_RUN");
2161 		break;
2162 
2163 	case CRYPTO_LOAD_DOOR:
2164 		if (error == 0 && rv == CRYPTO_SUCCESS)
2165 			(void) snprintf(buffer, sizeof (buffer),
2166 			    "op=CRYPTO_LOAD_DOOR");
2167 		else
2168 			(void) snprintf(buffer, sizeof (buffer),
2169 			    "op=CRYPTO_LOAD_DOOR, return_val=%d", rv);
2170 		break;
2171 
2172 	case CRYPTO_FIPS140_SET:
2173 		(void) snprintf(buffer, sizeof (buffer),
2174 		    "op=CRYPTO_FIPS140_SET, fips_state=%d", rv);
2175 		break;
2176 
2177 	default:
2178 		return;
2179 	}
2180 
2181 	au_write((caddr_t *)&ad, au_to_text(buffer));
2182 
2183 	if (mech_list_required) {
2184 		int i;
2185 
2186 		if (mech_count == 0) {
2187 			au_write((caddr_t *)&ad, au_to_text("mech=list empty"));
2188 		} else {
2189 			char	*pb = buffer;
2190 			size_t	l = sizeof (buffer);
2191 			size_t	n;
2192 			char	space[2] = ":";
2193 
2194 			n = snprintf(pb, l, "mech=");
2195 
2196 			for (i = 0; i < mech_count; i++) {
2197 				pb += n;
2198 				l -= n;
2199 				if (l < 0)
2200 					l = 0;
2201 
2202 				if (i == mech_count - 1)
2203 					(void) strcpy(space, "");
2204 
2205 				n = snprintf(pb, l, "%s%s", mech_names[i],
2206 				    space);
2207 			}
2208 			au_write((caddr_t *)&ad, au_to_text(buffer));
2209 		}
2210 	}
2211 
2212 	/* add a return token */
2213 	if (error || (rv != CRYPTO_SUCCESS))
2214 		add_return_token((caddr_t *)&ad, tad->tad_scid, -1, error);
2215 	else
2216 		add_return_token((caddr_t *)&ad, tad->tad_scid, 0, rv);
2217 
2218 	AS_INC(as_generated, 1, kctx);
2219 	AS_INC(as_kernel, 1, kctx);
2220 
2221 	au_close(kctx, (caddr_t *)&ad, AU_OK, AUE_CRYPTOADM, tad->tad_evmod);
2222 }
2223 
2224 /*
2225  * Audit the kernel SSL administration command. The address and the
2226  * port number for the SSL instance, and the proxy port are put in the
2227  * audit trail.
2228  */
2229 void
2230 audit_kssl(int cmd, void *params, int error)
2231 {
2232 	cred_t			*cr = CRED();
2233 	t_audit_data_t		*tad;
2234 	token_t			*ad = NULL;
2235 	const auditinfo_addr_t	*ainfo = crgetauinfo(cr);
2236 	au_kcontext_t		*kctx = GET_KCTX_PZ;
2237 
2238 	tad = U2A(u);
2239 
2240 	if (ainfo == NULL)
2241 		return;
2242 
2243 	tad->tad_event = AUE_CONFIGKSSL;
2244 
2245 	if (audit_success(kctx, tad, error, NULL) != AU_OK)
2246 		return;
2247 
2248 	/* Add subject information */
2249 	AUDIT_SETSUBJ((caddr_t *)&ad, cr, ainfo, kctx);
2250 
2251 	switch (cmd) {
2252 	case KSSL_ADD_ENTRY: {
2253 		char buf[32];
2254 		kssl_params_t *kp = (kssl_params_t *)params;
2255 		struct sockaddr_in6 *saddr = &kp->kssl_addr;
2256 
2257 		au_write((caddr_t *)&ad, au_to_text("op=KSSL_ADD_ENTRY"));
2258 		au_write((caddr_t *)&ad,
2259 		    au_to_in_addr_ex((int32_t *)&saddr->sin6_addr));
2260 		(void) snprintf(buf, sizeof (buf), "SSL port=%d",
2261 		    saddr->sin6_port);
2262 		au_write((caddr_t *)&ad, au_to_text(buf));
2263 
2264 		(void) snprintf(buf, sizeof (buf), "proxy port=%d",
2265 		    kp->kssl_proxy_port);
2266 		au_write((caddr_t *)&ad, au_to_text(buf));
2267 		break;
2268 	}
2269 
2270 	case KSSL_DELETE_ENTRY: {
2271 		char buf[32];
2272 		struct sockaddr_in6 *saddr = (struct sockaddr_in6 *)params;
2273 
2274 		au_write((caddr_t *)&ad, au_to_text("op=KSSL_DELETE_ENTRY"));
2275 		au_write((caddr_t *)&ad,
2276 		    au_to_in_addr_ex((int32_t *)&saddr->sin6_addr));
2277 		(void) snprintf(buf, sizeof (buf), "SSL port=%d",
2278 		    saddr->sin6_port);
2279 		au_write((caddr_t *)&ad, au_to_text(buf));
2280 		break;
2281 	}
2282 
2283 	default:
2284 		return;
2285 	}
2286 
2287 	/* add a return token */
2288 	add_return_token((caddr_t *)&ad, tad->tad_scid, error, 0);
2289 
2290 	AS_INC(as_generated, 1, kctx);
2291 	AS_INC(as_kernel, 1, kctx);
2292 
2293 	au_close(kctx, (caddr_t *)&ad, AU_OK, AUE_CONFIGKSSL, tad->tad_evmod);
2294 }
2295 
2296 /*
2297  * Audit the kernel PF_POLICY administration commands.  Record command,
2298  * zone, policy type (global or tunnel, active or inactive)
2299  */
2300 /*
2301  * ROUTINE:	AUDIT_PF_POLICY
2302  * PURPOSE:	Records arguments to administrative ioctls on PF_POLICY socket
2303  * CALLBY:	SPD_ADDRULE, SPD_DELETERULE, SPD_FLUSH, SPD_UPDATEALGS,
2304  *		SPD_CLONE, SPD_FLIP
2305  * NOTE:
2306  * TODO:
2307  * QUESTION:
2308  */
2309 
2310 void
2311 audit_pf_policy(int cmd, cred_t *cred, netstack_t *ns, char *tun,
2312     boolean_t active, int error, pid_t pid)
2313 {
2314 	const auditinfo_addr_t	*ainfo;
2315 	t_audit_data_t		*tad;
2316 	token_t			*ad = NULL;
2317 	au_kcontext_t		*kctx = GET_KCTX_PZ;
2318 	char			buf[80];
2319 	int			flag;
2320 
2321 	tad = U2A(u);
2322 	if (tad == NULL)
2323 		return;
2324 
2325 	ainfo = crgetauinfo((cred != NULL) ? cred : CRED());
2326 	if (ainfo == NULL)
2327 		return;
2328 
2329 	/*
2330 	 * Initialize some variables since these are only set
2331 	 * with system calls.
2332 	 */
2333 
2334 	switch (cmd) {
2335 	case SPD_ADDRULE: {
2336 		tad->tad_event = AUE_PF_POLICY_ADDRULE;
2337 		break;
2338 	}
2339 
2340 	case SPD_DELETERULE: {
2341 		tad->tad_event = AUE_PF_POLICY_DELRULE;
2342 		break;
2343 	}
2344 
2345 	case SPD_FLUSH: {
2346 		tad->tad_event = AUE_PF_POLICY_FLUSH;
2347 		break;
2348 	}
2349 
2350 	case SPD_UPDATEALGS: {
2351 		tad->tad_event = AUE_PF_POLICY_ALGS;
2352 		break;
2353 	}
2354 
2355 	case SPD_CLONE: {
2356 		tad->tad_event = AUE_PF_POLICY_CLONE;
2357 		break;
2358 	}
2359 
2360 	case SPD_FLIP: {
2361 		tad->tad_event = AUE_PF_POLICY_FLIP;
2362 		break;
2363 	}
2364 
2365 	default:
2366 		tad->tad_event = AUE_NULL;
2367 	}
2368 
2369 	tad->tad_evmod = 0;
2370 
2371 	if (flag = audit_success(kctx, tad, error, cred)) {
2372 		zone_t *nszone;
2373 
2374 		/*
2375 		 * For now, just audit that an event happened,
2376 		 * along with the error code.
2377 		 */
2378 		au_write((caddr_t *)&ad,
2379 		    au_to_arg32(1, "Policy Active?", (uint32_t)active));
2380 		au_write((caddr_t *)&ad,
2381 		    au_to_arg32(2, "Policy Global?", (uint32_t)(tun == NULL)));
2382 
2383 		/* Supplemental data */
2384 
2385 		/*
2386 		 * Generate this zone token if the target zone differs
2387 		 * from the administrative zone.  If netstacks are expanded
2388 		 * to something other than a 1-1 relationship with zones,
2389 		 * the auditing framework should create a new token type
2390 		 * and audit it as a netstack instead.
2391 		 * Turn on general zone auditing to get the administrative zone.
2392 		 */
2393 
2394 		nszone = zone_find_by_id(netstackid_to_zoneid(
2395 		    ns->netstack_stackid));
2396 		if (nszone != NULL) {
2397 			if (strncmp(crgetzone(cred)->zone_name,
2398 			    nszone->zone_name, ZONENAME_MAX) != 0) {
2399 				token_t *ztoken;
2400 
2401 				ztoken = au_to_zonename(0, nszone);
2402 				au_write((caddr_t *)&ad, ztoken);
2403 			}
2404 			zone_rele(nszone);
2405 		}
2406 
2407 		if (tun != NULL) {
2408 			/* write tunnel name - tun is bounded */
2409 			(void) snprintf(buf, sizeof (buf), "tunnel_name:%s",
2410 			    tun);
2411 			au_write((caddr_t *)&ad, au_to_text(buf));
2412 		}
2413 
2414 		/* Add subject information */
2415 		AUDIT_SETSUBJ_GENERIC((caddr_t *)&ad,
2416 		    ((cred != NULL) ? cred : CRED()), ainfo, kctx, pid);
2417 
2418 		/* add a return token */
2419 		add_return_token((caddr_t *)&ad, 0, error, 0);
2420 
2421 		AS_INC(as_generated, 1, kctx);
2422 		AS_INC(as_kernel, 1, kctx);
2423 
2424 	}
2425 	au_close(kctx, (caddr_t *)&ad, flag, tad->tad_event, tad->tad_evmod);
2426 
2427 	/*
2428 	 * clear the ctrl flag so that we don't have spurious collection of
2429 	 * audit information.
2430 	 */
2431 	tad->tad_scid  = 0;
2432 	tad->tad_event = 0;
2433 	tad->tad_evmod = 0;
2434 	tad->tad_ctrl  = 0;
2435 }
2436 
2437 /*
2438  * ROUTINE:	AUDIT_SEC_ATTRIBUTES
2439  * PURPOSE:	Add security attributes
2440  * CALLBY:	AUDIT_ATTRIBUTES
2441  *		AUDIT_CLOSEF
2442  *		AUS_CLOSE
2443  * NOTE:
2444  * TODO:
2445  * QUESTION:
2446  */
2447 
2448 void
2449 audit_sec_attributes(caddr_t *ad, struct vnode *vp)
2450 {
2451 	/* Dump the SL */
2452 	if (is_system_labeled()) {
2453 		ts_label_t	*tsl;
2454 		bslabel_t	*bsl;
2455 
2456 		tsl = getflabel(vp);
2457 		if (tsl == NULL)
2458 			return;			/* nothing else to do */
2459 
2460 		bsl = label2bslabel(tsl);
2461 		if (bsl == NULL)
2462 			return;			/* nothing else to do */
2463 		au_write(ad, au_to_label(bsl));
2464 		label_rele(tsl);
2465 	}
2466 
2467 }	/* AUDIT_SEC_ATTRIBUTES */
2468