xref: /illumos-gate/usr/src/uts/common/c2/audit.c (revision 9e9e6ab82d4247028c312ff50a65b8a05a194b33)
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 2007 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/types.h>
33 #include <sys/proc.h>
34 #include <sys/vnode.h>
35 #include <sys/vfs.h>
36 #include <sys/file.h>
37 #include <sys/user.h>
38 #include <sys/stropts.h>
39 #include <sys/systm.h>
40 #include <sys/pathname.h>
41 #include <sys/syscall.h>
42 #include <sys/fcntl.h>
43 #include <sys/ipc_impl.h>
44 #include <sys/msg_impl.h>
45 #include <sys/sem_impl.h>
46 #include <sys/shm_impl.h>
47 #include <sys/kmem.h>		/* for KM_SLEEP */
48 #include <sys/socket.h>
49 #include <sys/cmn_err.h>	/* snprintf... */
50 #include <sys/debug.h>
51 #include <sys/thread.h>
52 #include <netinet/in.h>
53 #include <c2/audit.h>		/* needs to be included before user.h */
54 #include <c2/audit_kernel.h>	/* for M_DONTWAIT */
55 #include <c2/audit_kevents.h>
56 #include <c2/audit_record.h>
57 #include <sys/strsubr.h>
58 #include <sys/tihdr.h>
59 #include <sys/tiuser.h>
60 #include <sys/timod.h>
61 #include <sys/model.h>		/* for model_t */
62 #include <sys/disp.h>		/* for servicing_interrupt() */
63 #include <sys/devpolicy.h>
64 #include <sys/crypto/ioctladmin.h>
65 #include <inet/kssl/kssl.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 && (tad->tad_scid == SYS_open ||
304 		    tad->tad_scid == SYS_open64 ||
305 		    tad->tad_scid == SYS_creat ||
306 		    tad->tad_scid == SYS_creat64 ||
307 		    tad->tad_scid == SYS_fsat)) {
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, crate) */
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_scid == SYS_fsat && 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_ATPATH;
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_ATPATH);
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 superceeded 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 occured
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()) != 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)) {
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 	short 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 |= (short)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());
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 = (short)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 || tad->tad_scid == SYS_creat ||
1124 	    tad->tad_scid == SYS_open64 || tad->tad_scid == SYS_creat64 ||
1125 	    tad->tad_scid == SYS_fsat))
1126 		return;
1127 
1128 	/* no path */
1129 	if (tad->tad_aupath == 0)
1130 		return;
1131 
1132 	/*
1133 	 * assign path information associated with file audit data
1134 	 * use tad hold
1135 	 */
1136 	fad->fad_aupath = tad->tad_aupath;
1137 	tad->tad_aupath = NULL;
1138 	tad->tad_vn = NULL;
1139 
1140 	if (!(tad->tad_ctrl & PAD_TRUE_CREATE)) {
1141 	/* adjust event type */
1142 		switch (tad->tad_event) {
1143 		case AUE_OPEN_RC:
1144 			tad->tad_event = AUE_OPEN_R;
1145 			tad->tad_ctrl |= PAD_PUBLIC_EV;
1146 			break;
1147 		case AUE_OPEN_RTC:
1148 			tad->tad_event = AUE_OPEN_RT;
1149 			break;
1150 		case AUE_OPEN_WC:
1151 			tad->tad_event = AUE_OPEN_W;
1152 			break;
1153 		case AUE_OPEN_WTC:
1154 			tad->tad_event = AUE_OPEN_WT;
1155 			break;
1156 		case AUE_OPEN_RWC:
1157 			tad->tad_event = AUE_OPEN_RW;
1158 			break;
1159 		case AUE_OPEN_RWTC:
1160 			tad->tad_event = AUE_OPEN_RWT;
1161 			break;
1162 		default:
1163 			break;
1164 		}
1165 	}
1166 }
1167 
1168 
1169 /*
1170  * ROUTINE:	AUDIT_COPEN
1171  * PURPOSE:
1172  * CALLBY:	COPEN
1173  * NOTE:
1174  * TODO:
1175  * QUESTION:
1176  */
1177 /*ARGSUSED*/
1178 void
1179 audit_copen(int fd, file_t *fp, vnode_t *vp)
1180 {
1181 }
1182 
1183 void
1184 audit_ipc(int type, int id, void *vp)
1185 {
1186 	/* if not auditing this event, then do nothing */
1187 	if (ad_flag == 0)
1188 		return;
1189 
1190 	switch (type) {
1191 	case AT_IPC_MSG:
1192 		au_uwrite(au_to_ipc(AT_IPC_MSG, id));
1193 		au_uwrite(au_to_ipc_perm(&(((kmsqid_t *)vp)->msg_perm)));
1194 		break;
1195 	case AT_IPC_SEM:
1196 		au_uwrite(au_to_ipc(AT_IPC_SEM, id));
1197 		au_uwrite(au_to_ipc_perm(&(((ksemid_t *)vp)->sem_perm)));
1198 		break;
1199 	case AT_IPC_SHM:
1200 		au_uwrite(au_to_ipc(AT_IPC_SHM, id));
1201 		au_uwrite(au_to_ipc_perm(&(((kshmid_t *)vp)->shm_perm)));
1202 		break;
1203 	}
1204 }
1205 
1206 void
1207 audit_ipcget(int type, void *vp)
1208 {
1209 	/* if not auditing this event, then do nothing */
1210 	if (ad_flag == 0)
1211 		return;
1212 
1213 	switch (type) {
1214 	case NULL:
1215 		au_uwrite(au_to_ipc_perm((struct kipc_perm *)vp));
1216 		break;
1217 	case AT_IPC_MSG:
1218 		au_uwrite(au_to_ipc_perm(&(((kmsqid_t *)vp)->msg_perm)));
1219 		break;
1220 	case AT_IPC_SEM:
1221 		au_uwrite(au_to_ipc_perm(&(((ksemid_t *)vp)->sem_perm)));
1222 		break;
1223 	case AT_IPC_SHM:
1224 		au_uwrite(au_to_ipc_perm(&(((kshmid_t *)vp)->shm_perm)));
1225 		break;
1226 	}
1227 }
1228 
1229 /*
1230  * ROUTINE:	AUDIT_REBOOT
1231  * PURPOSE:
1232  * CALLBY:
1233  * NOTE:
1234  * At this point we know that the system call reboot will not return. We thus
1235  * have to complete the audit record generation and put it onto the queue.
1236  * This might be fairly useless if the auditing daemon is already dead....
1237  * TODO:
1238  * QUESTION:	who calls audit_reboot
1239  */
1240 
1241 void
1242 audit_reboot(void)
1243 {
1244 	int flag;
1245 	t_audit_data_t *tad;
1246 	au_kcontext_t	*kctx = GET_KCTX_PZ;
1247 
1248 	tad = U2A(u);
1249 
1250 	/* if not auditing this event, then do nothing */
1251 	if (tad->tad_flag == 0)
1252 		return;
1253 
1254 	/* do preselection on success/failure */
1255 	if (flag = audit_success(kctx, tad, 0)) {
1256 		/* add a process token */
1257 
1258 		cred_t *cr = CRED();
1259 		const auditinfo_addr_t *ainfo = crgetauinfo(cr);
1260 
1261 		if (ainfo == NULL)
1262 			return;
1263 
1264 		/* Add subject information */
1265 		AUDIT_SETSUBJ(&(u_ad), cr, ainfo, kctx);
1266 
1267 		/* add a return token */
1268 		add_return_token((caddr_t *)&(u_ad), tad->tad_scid, 0, 0);
1269 
1270 		AS_INC(as_generated, 1, kctx);
1271 		AS_INC(as_kernel, 1, kctx);
1272 	}
1273 
1274 	/*
1275 	 * Flow control useless here since we're going
1276 	 * to drop everything in the queue anyway. Why
1277 	 * block and wait. There aint anyone left alive to
1278 	 * read the records remaining anyway.
1279 	 */
1280 
1281 	/* Close up everything */
1282 	au_close(kctx, &(u_ad), flag | AU_DONTBLOCK,
1283 	    tad->tad_event, tad->tad_evmod);
1284 }
1285 
1286 void
1287 audit_setfsat_path(int argnum)
1288 {
1289 	klwp_id_t clwp = ttolwp(curthread);
1290 	struct file  *fp;
1291 	uint32_t fd;
1292 	t_audit_data_t *tad;
1293 	struct f_audit_data *fad;
1294 	p_audit_data_t *pad;	/* current process */
1295 
1296 	struct b {
1297 		long arg1;
1298 		long arg2;
1299 		long arg3;
1300 		long arg4;
1301 		long arg5;
1302 	} *uap1;
1303 
1304 	if (clwp == NULL)
1305 		return;
1306 	uap1 = (struct b *)&clwp->lwp_ap[1];
1307 
1308 	tad = U2A(u);
1309 
1310 	ASSERT(tad != NULL);
1311 
1312 	if (tad->tad_scid != SYS_fsat)
1313 		return;
1314 
1315 	switch (argnum) {
1316 	case 1:
1317 		fd = (uint32_t)uap1->arg1;
1318 		break;
1319 	case 2:
1320 		fd = (uint32_t)uap1->arg2;
1321 		break;
1322 	case 3:
1323 		fd = (uint32_t)uap1->arg3;
1324 		break;
1325 	case 4:
1326 		fd = (uint32_t)uap1->arg4;
1327 		break;
1328 	case 5:
1329 		fd = (uint32_t)uap1->arg5;
1330 		break;
1331 	default:
1332 		return;
1333 	}
1334 
1335 	if (tad->tad_atpath != NULL) {
1336 		au_pathrele(tad->tad_atpath);
1337 		tad->tad_atpath = NULL;
1338 	}
1339 	if (fd != AT_FDCWD) {
1340 		if ((fp = getf(fd)) == NULL)
1341 			return;
1342 
1343 		fad = F2A(fp);
1344 		ASSERT(fad);
1345 		au_pathhold(fad->fad_aupath);
1346 		tad->tad_atpath = fad->fad_aupath;
1347 		releasef(fd);
1348 	} else {
1349 		pad = P2A(curproc);
1350 		mutex_enter(&pad->pad_lock);
1351 		au_pathhold(pad->pad_cwd);
1352 		tad->tad_atpath = pad->pad_cwd;
1353 		mutex_exit(&pad->pad_lock);
1354 	}
1355 }
1356 
1357 void
1358 audit_symlink_create(vnode_t *dvp, char *sname, char *target, int error)
1359 {
1360 	t_audit_data_t *tad;
1361 	vnode_t	*vp;
1362 
1363 	tad = U2A(u);
1364 
1365 	/* if not auditing this event, then do nothing */
1366 	if (tad->tad_flag == 0)
1367 		return;
1368 
1369 	au_uwrite(au_to_text(target));
1370 
1371 	if (error)
1372 		return;
1373 
1374 	error = VOP_LOOKUP(dvp, sname, &vp, NULL, 0, NULL, CRED());
1375 	if (error == 0) {
1376 		audit_attributes(vp);
1377 		VN_RELE(vp);
1378 	}
1379 }
1380 
1381 /*
1382  * ROUTINE:	AUDIT_VNCREATE_START
1383  * PURPOSE:	set flag so path name lookup in create will not add attribute
1384  * CALLBY:	VN_CREATE
1385  * NOTE:
1386  * TODO:
1387  * QUESTION:
1388  */
1389 
1390 void
1391 audit_vncreate_start()
1392 {
1393 	t_audit_data_t *tad;
1394 
1395 	tad = U2A(u);
1396 	tad->tad_ctrl |= PAD_NOATTRB;
1397 }
1398 
1399 /*
1400  * ROUTINE:	AUDIT_VNCREATE_FINISH
1401  * PURPOSE:
1402  * CALLBY:	VN_CREATE
1403  * NOTE:
1404  * TODO:
1405  * QUESTION:
1406  */
1407 void
1408 audit_vncreate_finish(struct vnode *vp, int error)
1409 {
1410 	t_audit_data_t *tad;
1411 
1412 	if (error)
1413 		return;
1414 
1415 	tad = U2A(u);
1416 
1417 	/* if not auditing this event, then do nothing */
1418 	if (tad->tad_flag == 0)
1419 		return;
1420 
1421 	if (tad->tad_ctrl & PAD_TRUE_CREATE) {
1422 		audit_attributes(vp);
1423 	}
1424 
1425 	if (tad->tad_ctrl & PAD_CORE) {
1426 		audit_attributes(vp);
1427 		tad->tad_ctrl &= ~PAD_CORE;
1428 	}
1429 
1430 	if (!error && ((tad->tad_event == AUE_MKNOD) ||
1431 			(tad->tad_event == AUE_MKDIR))) {
1432 		audit_attributes(vp);
1433 	}
1434 
1435 	/* for case where multiple lookups in one syscall (rename) */
1436 	tad->tad_ctrl &= ~PAD_NOATTRB;
1437 }
1438 
1439 
1440 
1441 
1442 
1443 
1444 
1445 
1446 /*
1447  * ROUTINE:	AUDIT_EXEC
1448  * PURPOSE:	Records the function arguments and environment variables
1449  * CALLBY:	EXEC_ARGS
1450  * NOTE:
1451  * TODO:
1452  * QUESTION:
1453  */
1454 
1455 /*ARGSUSED*/
1456 void
1457 audit_exec(
1458 	const char *argstr,	/* argument strings */
1459 	const char *envstr,	/* environment strings */
1460 	ssize_t argc,		/* total # arguments */
1461 	ssize_t envc)		/* total # environment variables */
1462 {
1463 	t_audit_data_t *tad;
1464 	au_kcontext_t	*kctx = GET_KCTX_PZ;
1465 
1466 	tad = U2A(u);
1467 
1468 	/* if not auditing this event, then do nothing */
1469 	if (!tad->tad_flag)
1470 		return;
1471 
1472 	/* return if not interested in argv or environment variables */
1473 	if (!(kctx->auk_policy & (AUDIT_ARGV|AUDIT_ARGE)))
1474 		return;
1475 
1476 	if (kctx->auk_policy & AUDIT_ARGV) {
1477 		au_uwrite(au_to_exec_args(argstr, argc));
1478 	}
1479 
1480 	if (kctx->auk_policy & AUDIT_ARGE) {
1481 		au_uwrite(au_to_exec_env(envstr, envc));
1482 	}
1483 }
1484 
1485 /*
1486  * ROUTINE:	AUDIT_ENTERPROM
1487  * PURPOSE:
1488  * CALLBY:	KBDINPUT
1489  *		ZSA_XSINT
1490  * NOTE:
1491  * TODO:
1492  * QUESTION:
1493  */
1494 void
1495 audit_enterprom(int flg)
1496 {
1497 	token_t *rp = NULL;
1498 	int sorf;
1499 
1500 	if (flg)
1501 		sorf = AUM_SUCC;
1502 	else
1503 		sorf = AUM_FAIL;
1504 
1505 	AUDIT_ASYNC_START(rp, AUE_ENTERPROM, sorf);
1506 
1507 	au_write((caddr_t *)&(rp), au_to_text("kmdb"));
1508 
1509 	if (flg)
1510 		au_write((caddr_t *)&(rp), au_to_return32(0, 0));
1511 	else
1512 		au_write((caddr_t *)&(rp), au_to_return32(ECANCELED, 0));
1513 
1514 	AUDIT_ASYNC_FINISH(rp, AUE_ENTERPROM, NULL);
1515 }
1516 
1517 
1518 /*
1519  * ROUTINE:	AUDIT_EXITPROM
1520  * PURPOSE:
1521  * CALLBY:	KBDINPUT
1522  *		ZSA_XSINT
1523  * NOTE:
1524  * TODO:
1525  * QUESTION:
1526  */
1527 void
1528 audit_exitprom(int flg)
1529 {
1530 	int sorf;
1531 	token_t *rp = NULL;
1532 
1533 	if (flg)
1534 		sorf = AUM_SUCC;
1535 	else
1536 		sorf = AUM_FAIL;
1537 
1538 	AUDIT_ASYNC_START(rp, AUE_EXITPROM, sorf);
1539 
1540 	au_write((caddr_t *)&(rp), au_to_text("kmdb"));
1541 
1542 	if (flg)
1543 		au_write((caddr_t *)&(rp), au_to_return32(0, 0));
1544 	else
1545 		au_write((caddr_t *)&(rp), au_to_return32(ECANCELED, 0));
1546 
1547 	AUDIT_ASYNC_FINISH(rp, AUE_EXITPROM, NULL);
1548 }
1549 
1550 struct fcntla {
1551 	int fdes;
1552 	int cmd;
1553 	intptr_t arg;
1554 };
1555 
1556 /*
1557  * ROUTINE:	AUDIT_C2_REVOKE
1558  * PURPOSE:
1559  * CALLBY:	FCNTL
1560  * NOTE:
1561  * TODO:
1562  * QUESTION:	are we keeping this func
1563  */
1564 
1565 /*ARGSUSED*/
1566 int
1567 audit_c2_revoke(struct fcntla *uap, rval_t *rvp)
1568 {
1569 	return (0);
1570 }
1571 
1572 
1573 /*
1574  * ROUTINE:	AUDIT_CHDIREC
1575  * PURPOSE:
1576  * CALLBY:	CHDIREC
1577  * NOTE:	The main function of CHDIREC
1578  * TODO:	Move the audit_chdirec hook above the VN_RELE in vncalls.c
1579  * QUESTION:
1580  */
1581 
1582 /*ARGSUSED*/
1583 void
1584 audit_chdirec(vnode_t *vp, vnode_t **vpp)
1585 {
1586 	int		chdir;
1587 	int		fchdir;
1588 	struct audit_path	**appp;
1589 	struct file	*fp;
1590 	f_audit_data_t *fad;
1591 	p_audit_data_t *pad = P2A(curproc);
1592 	t_audit_data_t *tad = T2A(curthread);
1593 
1594 	struct a {
1595 		long fd;
1596 	} *uap = (struct a *)ttolwp(curthread)->lwp_ap;
1597 
1598 	if ((tad->tad_scid == SYS_chdir) || (tad->tad_scid == SYS_chroot)) {
1599 		chdir = tad->tad_scid == SYS_chdir;
1600 		if (tad->tad_aupath) {
1601 			mutex_enter(&pad->pad_lock);
1602 			if (chdir)
1603 				appp = &(pad->pad_cwd);
1604 			else
1605 				appp = &(pad->pad_root);
1606 			au_pathrele(*appp);
1607 			/* use tad hold */
1608 			*appp = tad->tad_aupath;
1609 			tad->tad_aupath = NULL;
1610 			mutex_exit(&pad->pad_lock);
1611 		}
1612 	} else if ((tad->tad_scid == SYS_fchdir) ||
1613 	    (tad->tad_scid == SYS_fchroot)) {
1614 		fchdir = tad->tad_scid == SYS_fchdir;
1615 		if ((fp = getf(uap->fd)) == NULL)
1616 			return;
1617 		fad = F2A(fp);
1618 		if (fad->fad_aupath) {
1619 			au_pathhold(fad->fad_aupath);
1620 			mutex_enter(&pad->pad_lock);
1621 			if (fchdir)
1622 				appp = &(pad->pad_cwd);
1623 			else
1624 				appp = &(pad->pad_root);
1625 			au_pathrele(*appp);
1626 			*appp = fad->fad_aupath;
1627 			mutex_exit(&pad->pad_lock);
1628 			if (tad->tad_flag) {
1629 				au_uwrite(au_to_path(fad->fad_aupath));
1630 				audit_attributes(fp->f_vnode);
1631 			}
1632 		}
1633 		releasef(uap->fd);
1634 	}
1635 }
1636 
1637 /*
1638  * ROUTINE:	AUDIT_GETF
1639  * PURPOSE:
1640  * CALLBY:	GETF_INTERNAL
1641  * NOTE:	The main function of GETF_INTERNAL is to associate a given
1642  *		file descriptor with a file structure and increment the
1643  *		file pointer reference count.
1644  * TODO:	remove pass in of fpp.
1645  * increment a reference count so that even if a thread with same process delete
1646  * the same object, it will not panic our system
1647  * QUESTION:
1648  * where to decrement the f_count?????????????????
1649  * seems like I need to set a flag if f_count incrmented through audit_getf
1650  */
1651 
1652 /*ARGSUSED*/
1653 int
1654 audit_getf(int fd)
1655 {
1656 #ifdef NOTYET
1657 	t_audit_data_t *tad;
1658 
1659 	tad = T2A(curthread);
1660 
1661 	if (!(tad->tad_scid == SYS_open || tad->tad_scid == SYS_creat))
1662 		return;
1663 #endif
1664 	return (0);
1665 }
1666 
1667 /*
1668  *	Audit hook for stream based socket and tli request.
1669  *	Note that we do not have user context while executing
1670  *	this code so we had to record them earlier during the
1671  *	putmsg/getmsg to figure out which user we are dealing with.
1672  */
1673 
1674 /*ARGSUSED*/
1675 void
1676 audit_sock(
1677 	int type,	/* type of tihdr.h header requests */
1678 	queue_t *q,	/* contains the process and thread audit data */
1679 	mblk_t *mp,	/* contains the tihdr.h header structures */
1680 	int from)	/* timod or sockmod request */
1681 {
1682 	int32_t    len;
1683 	int32_t    offset;
1684 	struct sockaddr_in *sock_data;
1685 	struct T_conn_req *conn_req;
1686 	struct T_conn_ind *conn_ind;
1687 	struct T_unitdata_req *unitdata_req;
1688 	struct T_unitdata_ind *unitdata_ind;
1689 	au_state_t estate;
1690 	t_audit_data_t *tad;
1691 	caddr_t saved_thread_ptr;
1692 	au_mask_t amask;
1693 	const auditinfo_addr_t *ainfo;
1694 	au_kcontext_t	*kctx;
1695 
1696 	if (q->q_stream == NULL)
1697 		return;
1698 	mutex_enter(&q->q_stream->sd_lock);
1699 	/* are we being audited */
1700 	saved_thread_ptr = q->q_stream->sd_t_audit_data;
1701 	/* no pointer to thread, nothing to do */
1702 	if (saved_thread_ptr == NULL) {
1703 		mutex_exit(&q->q_stream->sd_lock);
1704 		return;
1705 	}
1706 	/* only allow one addition of a record token */
1707 	q->q_stream->sd_t_audit_data = NULL;
1708 	/*
1709 	 * thread is not the one being audited, then nothing to do
1710 	 * This could be the stream thread handling the module
1711 	 * service routine. In this case, the context for the audit
1712 	 * record can no longer be assumed. Simplest to just drop
1713 	 * the operation.
1714 	 */
1715 	if (curthread != (kthread_id_t)saved_thread_ptr) {
1716 		mutex_exit(&q->q_stream->sd_lock);
1717 		return;
1718 	}
1719 	if (curthread->t_sysnum >= SYS_so_socket &&
1720 	    curthread->t_sysnum <= SYS_sockconfig) {
1721 		mutex_exit(&q->q_stream->sd_lock);
1722 		return;
1723 	}
1724 	mutex_exit(&q->q_stream->sd_lock);
1725 	/*
1726 	 * we know that the thread that did the put/getmsg is the
1727 	 * one running. Now we can get the TAD and see if we should
1728 	 * add an audit token.
1729 	 */
1730 	tad = U2A(u);
1731 
1732 	kctx = GET_KCTX_PZ;
1733 
1734 	/* proceed ONLY if user is being audited */
1735 	if (!tad->tad_flag)
1736 		return;
1737 
1738 	ainfo = crgetauinfo(CRED());
1739 	if (ainfo == NULL)
1740 		return;
1741 	amask = ainfo->ai_mask;
1742 
1743 	/*
1744 	 * Figure out the type of stream networking request here.
1745 	 * Note that getmsg and putmsg are always preselected
1746 	 * because during the beginning of the system call we have
1747 	 * not yet figure out which of the socket or tli request
1748 	 * we are looking at until we are here. So we need to check
1749 	 * against that specific request and reset the type of event.
1750 	 */
1751 	switch (type) {
1752 	case T_CONN_REQ:	/* connection request */
1753 		conn_req = (struct T_conn_req *)mp->b_rptr;
1754 		if (conn_req->DEST_offset < sizeof (struct T_conn_req))
1755 			return;
1756 		offset = conn_req->DEST_offset;
1757 		len = conn_req->DEST_length;
1758 		estate = kctx->auk_ets[AUE_SOCKCONNECT];
1759 		if (amask.as_success & estate || amask.as_failure & estate) {
1760 			tad->tad_event = AUE_SOCKCONNECT;
1761 			break;
1762 		} else {
1763 			return;
1764 		}
1765 	case T_CONN_IND:	 /* connectionless receive request */
1766 		conn_ind = (struct T_conn_ind *)mp->b_rptr;
1767 		if (conn_ind->SRC_offset < sizeof (struct T_conn_ind))
1768 			return;
1769 		offset = conn_ind->SRC_offset;
1770 		len = conn_ind->SRC_length;
1771 		estate = kctx->auk_ets[AUE_SOCKACCEPT];
1772 		if (amask.as_success & estate || amask.as_failure & estate) {
1773 			tad->tad_event = AUE_SOCKACCEPT;
1774 			break;
1775 		} else {
1776 			return;
1777 		}
1778 	case T_UNITDATA_REQ:	 /* connectionless send request */
1779 		unitdata_req = (struct T_unitdata_req *)mp->b_rptr;
1780 		if (unitdata_req->DEST_offset < sizeof (struct T_unitdata_req))
1781 			return;
1782 		offset = unitdata_req->DEST_offset;
1783 		len = unitdata_req->DEST_length;
1784 		estate = kctx->auk_ets[AUE_SOCKSEND];
1785 		if (amask.as_success & estate || amask.as_failure & estate) {
1786 			tad->tad_event = AUE_SOCKSEND;
1787 			break;
1788 		} else {
1789 			return;
1790 		}
1791 	case T_UNITDATA_IND:	 /* connectionless receive request */
1792 		unitdata_ind = (struct T_unitdata_ind *)mp->b_rptr;
1793 		if (unitdata_ind->SRC_offset < sizeof (struct T_unitdata_ind))
1794 			return;
1795 		offset = unitdata_ind->SRC_offset;
1796 		len = unitdata_ind->SRC_length;
1797 		estate = kctx->auk_ets[AUE_SOCKRECEIVE];
1798 		if (amask.as_success & estate || amask.as_failure & estate) {
1799 			tad->tad_event = AUE_SOCKRECEIVE;
1800 			break;
1801 		} else {
1802 			return;
1803 		}
1804 	default:
1805 		return;
1806 	}
1807 
1808 	/*
1809 	 * we are only interested in tcp stream connections,
1810 	 * not unix domain stuff
1811 	 */
1812 	if ((len < 0) || (len > sizeof (struct sockaddr_in))) {
1813 		tad->tad_event = AUE_GETMSG;
1814 		return;
1815 	}
1816 	/* skip over TPI header and point to the ip address */
1817 	sock_data = (struct sockaddr_in *)((char *)mp->b_rptr + offset);
1818 
1819 	switch (sock_data->sin_family) {
1820 	case AF_INET:
1821 		au_write(&(tad->tad_ad), au_to_sock_inet(sock_data));
1822 		break;
1823 	default:	/* reset to AUE_PUTMSG if not a inet request */
1824 		tad->tad_event = AUE_GETMSG;
1825 		break;
1826 	}
1827 }
1828 
1829 void
1830 audit_lookupname()
1831 {
1832 }
1833 
1834 /*ARGSUSED*/
1835 int
1836 audit_pathcomp(struct pathname *pnp, vnode_t *cvp, cred_t *cr)
1837 {
1838 	return (0);
1839 }
1840 
1841 static void
1842 add_return_token(caddr_t *ad, unsigned int scid, int err, int rval)
1843 {
1844 	unsigned int sy_flags;
1845 
1846 #ifdef _SYSCALL32_IMPL
1847 	if (lwp_getdatamodel(
1848 		ttolwp(curthread)) == DATAMODEL_NATIVE)
1849 		sy_flags = sysent[scid].sy_flags & SE_RVAL_MASK;
1850 	else
1851 		sy_flags = sysent32[scid].sy_flags & SE_RVAL_MASK;
1852 #else
1853 		sy_flags = sysent[scid].sy_flags & SE_RVAL_MASK;
1854 #endif
1855 
1856 	if (sy_flags == SE_64RVAL)
1857 		au_write(ad, au_to_return64(err, rval));
1858 	else
1859 		au_write(ad, au_to_return32(err, rval));
1860 
1861 }
1862 
1863 /*ARGSUSED*/
1864 void
1865 audit_fdsend(fd, fp, error)
1866 	int fd;
1867 	struct file *fp;
1868 	int error;		/* ignore for now */
1869 {
1870 	t_audit_data_t *tad;	/* current thread */
1871 	f_audit_data_t *fad;	/* per file audit structure */
1872 	struct vnode *vp;	/* for file attributes */
1873 
1874 	/* is this system call being audited */
1875 	tad = U2A(u);
1876 	ASSERT(tad != (t_audit_data_t *)0);
1877 	if (!tad->tad_flag)
1878 		return;
1879 
1880 	fad = F2A(fp);
1881 
1882 	/* add path and file attributes */
1883 	if (fad != NULL && fad->fad_aupath != NULL) {
1884 		au_uwrite(au_to_arg32(0, "send fd", (uint32_t)fd));
1885 		au_uwrite(au_to_path(fad->fad_aupath));
1886 	} else {
1887 		au_uwrite(au_to_arg32(0, "send fd", (uint32_t)fd));
1888 #ifdef _LP64
1889 		au_uwrite(au_to_arg64(0, "no path", (uint64_t)fp));
1890 #else
1891 		au_uwrite(au_to_arg32(0, "no path", (uint32_t)fp));
1892 #endif
1893 	}
1894 	vp = fp->f_vnode;	/* include vnode attributes */
1895 	audit_attributes(vp);
1896 }
1897 
1898 /*
1899  * Record privileges sucessfully used and we attempted to use but
1900  * didn't have.
1901  */
1902 void
1903 audit_priv(int priv, const priv_set_t *set, int flag)
1904 {
1905 	t_audit_data_t *tad;
1906 	int sbit;
1907 	priv_set_t *target;
1908 
1909 	/* Make sure this isn't being called in an interrupt context */
1910 	ASSERT(servicing_interrupt() == 0);
1911 
1912 	tad = U2A(u);
1913 
1914 	if (tad->tad_flag == 0)
1915 		return;
1916 
1917 	target = flag ? &tad->tad_sprivs : &tad->tad_fprivs;
1918 	sbit = flag ? PAD_SPRIVUSE : PAD_FPRIVUSE;
1919 
1920 	/* Tell audit_success() and audit_finish() that we saw this case */
1921 	if (!(tad->tad_evmod & sbit)) {
1922 		/* Clear set first time around */
1923 		priv_emptyset(target);
1924 		tad->tad_evmod |= sbit;
1925 	}
1926 
1927 	/* Save the privileges in the tad */
1928 	if (priv == PRIV_ALL) {
1929 		priv_fillset(target);
1930 	} else {
1931 		ASSERT(set != NULL || priv != PRIV_NONE);
1932 		if (set != NULL)
1933 			priv_union(set, target);
1934 		if (priv != PRIV_NONE)
1935 			priv_addset(target, priv);
1936 	}
1937 }
1938 
1939 /*
1940  * Audit the setpriv() system call; the operation, the set name and
1941  * the current value as well as the set argument are put in the
1942  * audit trail.
1943  */
1944 void
1945 audit_setppriv(int op, int set, const priv_set_t *newpriv, const cred_t *ocr)
1946 {
1947 	t_audit_data_t *tad;
1948 	const priv_set_t *oldpriv;
1949 	priv_set_t report;
1950 	const char *setname;
1951 
1952 	tad = U2A(u);
1953 
1954 	if (tad->tad_flag == 0)
1955 		return;
1956 
1957 	oldpriv = priv_getset(ocr, set);
1958 
1959 	/* Generate the actual record, include the before and after */
1960 	au_uwrite(au_to_arg32(2, "op", op));
1961 	setname = priv_getsetbynum(set);
1962 
1963 	switch (op) {
1964 	case PRIV_OFF:
1965 		/* Report privileges actually switched off */
1966 		report = *oldpriv;
1967 		priv_intersect(newpriv, &report);
1968 		au_uwrite(au_to_privset(setname, &report, AUT_PRIV, 0));
1969 		break;
1970 	case PRIV_ON:
1971 		/* Report privileges actually switched on */
1972 		report = *oldpriv;
1973 		priv_inverse(&report);
1974 		priv_intersect(newpriv, &report);
1975 		au_uwrite(au_to_privset(setname, &report, AUT_PRIV, 0));
1976 		break;
1977 	case PRIV_SET:
1978 		/* Report before and after */
1979 		au_uwrite(au_to_privset(setname, oldpriv, AUT_PRIV, 0));
1980 		au_uwrite(au_to_privset(setname, newpriv, AUT_PRIV, 0));
1981 		break;
1982 	}
1983 }
1984 
1985 /*
1986  * Dump the full device policy setting in the audit trail.
1987  */
1988 void
1989 audit_devpolicy(int nitems, const devplcysys_t *items)
1990 {
1991 	t_audit_data_t *tad;
1992 	int i;
1993 
1994 	tad = U2A(u);
1995 
1996 	if (tad->tad_flag == 0)
1997 		return;
1998 
1999 	for (i = 0; i < nitems; i++) {
2000 		au_uwrite(au_to_arg32(2, "major", items[i].dps_maj));
2001 		if (items[i].dps_minornm[0] == '\0') {
2002 			au_uwrite(au_to_arg32(2, "lomin", items[i].dps_lomin));
2003 			au_uwrite(au_to_arg32(2, "himin", items[i].dps_himin));
2004 		} else
2005 			au_uwrite(au_to_text(items[i].dps_minornm));
2006 
2007 		au_uwrite(au_to_privset("read", &items[i].dps_rdp,
2008 		    AUT_PRIV, 0));
2009 		au_uwrite(au_to_privset("write", &items[i].dps_wrp,
2010 		    AUT_PRIV, 0));
2011 	}
2012 }
2013 
2014 /*ARGSUSED*/
2015 void
2016 audit_fdrecv(fd, fp)
2017 	int fd;
2018 	struct file *fp;
2019 {
2020 	t_audit_data_t *tad;	/* current thread */
2021 	f_audit_data_t *fad;	/* per file audit structure */
2022 	struct vnode *vp;	/* for file attributes */
2023 
2024 	/* is this system call being audited */
2025 	tad = U2A(u);
2026 	ASSERT(tad != (t_audit_data_t *)0);
2027 	if (!tad->tad_flag)
2028 		return;
2029 
2030 	fad = F2A(fp);
2031 
2032 	/* add path and file attributes */
2033 	if (fad != NULL && fad->fad_aupath != NULL) {
2034 		au_uwrite(au_to_arg32(0, "recv fd", (uint32_t)fd));
2035 		au_uwrite(au_to_path(fad->fad_aupath));
2036 	} else {
2037 		au_uwrite(au_to_arg32(0, "recv fd", (uint32_t)fd));
2038 #ifdef _LP64
2039 		au_uwrite(au_to_arg64(0, "no path", (uint64_t)fp));
2040 #else
2041 		au_uwrite(au_to_arg32(0, "no path", (uint32_t)fp));
2042 #endif
2043 	}
2044 	vp = fp->f_vnode;	/* include vnode attributes */
2045 	audit_attributes(vp);
2046 }
2047 
2048 /*
2049  * ROUTINE:	AUDIT_CRYPTOADM
2050  * PURPOSE:	Records arguments to administrative ioctls on /dev/cryptoadm
2051  * CALLBY:	CRYPTO_LOAD_DEV_DISABLED, CRYPTO_LOAD_SOFT_DISABLED,
2052  *		CRYPTO_UNLOAD_SOFT_MODULE, CRYPTO_LOAD_SOFT_CONFIG,
2053  *		CRYPTO_POOL_CREATE, CRYPTO_POOL_WAIT, CRYPTO_POOL_RUN,
2054  *		CRYPTO_LOAD_DOOR
2055  * NOTE:
2056  * TODO:
2057  * QUESTION:
2058  */
2059 
2060 void
2061 audit_cryptoadm(int cmd, char *module_name, crypto_mech_name_t *mech_names,
2062     uint_t mech_count, uint_t device_instance, uint32_t rv, int error)
2063 {
2064 	boolean_t		mech_list_required = B_FALSE;
2065 	cred_t			*cr = CRED();
2066 	t_audit_data_t		*tad;
2067 	token_t			*ad = NULL;
2068 	const auditinfo_addr_t	*ainfo = crgetauinfo(cr);
2069 	char			buffer[MAXNAMELEN * 2];
2070 	au_kcontext_t		*kctx = GET_KCTX_PZ;
2071 
2072 	tad = U2A(u);
2073 	if (tad == NULL)
2074 		return;
2075 
2076 	if (ainfo == NULL)
2077 		return;
2078 
2079 	tad->tad_event = AUE_CRYPTOADM;
2080 
2081 	if (audit_success(kctx, tad, error) != AU_OK)
2082 		return;
2083 
2084 	/* Add subject information */
2085 	AUDIT_SETSUBJ((caddr_t *)&(ad), cr, ainfo, kctx);
2086 
2087 	switch (cmd) {
2088 	case CRYPTO_LOAD_DEV_DISABLED:
2089 		if (error == 0 && rv == CRYPTO_SUCCESS) {
2090 			(void) snprintf(buffer, sizeof (buffer),
2091 			    "op=CRYPTO_LOAD_DEV_DISABLED, module=%s,"
2092 			    " dev_instance=%d",
2093 			    module_name, device_instance);
2094 			mech_list_required = B_TRUE;
2095 		} else {
2096 			(void) snprintf(buffer, sizeof (buffer),
2097 			    "op=CRYPTO_LOAD_DEV_DISABLED, return_val=%d", rv);
2098 		}
2099 		break;
2100 
2101 	case CRYPTO_LOAD_SOFT_DISABLED:
2102 		if (error == 0 && rv == CRYPTO_SUCCESS) {
2103 			(void) snprintf(buffer, sizeof (buffer),
2104 			    "op=CRYPTO_LOAD_SOFT_DISABLED, module=%s",
2105 			    module_name);
2106 			mech_list_required = B_TRUE;
2107 		} else {
2108 			(void) snprintf(buffer, sizeof (buffer),
2109 			    "op=CRYPTO_LOAD_SOFT_DISABLED, return_val=%d", rv);
2110 		}
2111 		break;
2112 
2113 	case CRYPTO_UNLOAD_SOFT_MODULE:
2114 		if (error == 0 && rv == CRYPTO_SUCCESS) {
2115 			(void) snprintf(buffer, sizeof (buffer),
2116 			    "op=CRYPTO_UNLOAD_SOFT_MODULE, module=%s",
2117 			    module_name);
2118 		} else {
2119 			(void) snprintf(buffer, sizeof (buffer),
2120 			    "op=CRYPTO_UNLOAD_SOFT_MODULE, return_val=%d", rv);
2121 		}
2122 		break;
2123 
2124 	case CRYPTO_LOAD_SOFT_CONFIG:
2125 		if (error == 0 && rv == CRYPTO_SUCCESS) {
2126 			(void) snprintf(buffer, sizeof (buffer),
2127 			    "op=CRYPTO_LOAD_SOFT_CONFIG, module=%s",
2128 			    module_name);
2129 			mech_list_required = B_TRUE;
2130 		} else {
2131 			(void) snprintf(buffer, sizeof (buffer),
2132 			    "op=CRYPTO_LOAD_SOFT_CONFIG, return_val=%d", rv);
2133 		}
2134 		break;
2135 
2136 	case CRYPTO_POOL_CREATE:
2137 		(void) snprintf(buffer, sizeof (buffer),
2138 		    "op=CRYPTO_POOL_CREATE");
2139 		break;
2140 
2141 	case CRYPTO_POOL_WAIT:
2142 		(void) snprintf(buffer, sizeof (buffer), "op=CRYPTO_POOL_WAIT");
2143 		break;
2144 
2145 	case CRYPTO_POOL_RUN:
2146 		(void) snprintf(buffer, sizeof (buffer), "op=CRYPTO_POOL_RUN");
2147 		break;
2148 
2149 	case CRYPTO_LOAD_DOOR:
2150 		if (error == 0 && rv == CRYPTO_SUCCESS)
2151 			(void) snprintf(buffer, sizeof (buffer),
2152 			    "op=CRYPTO_LOAD_DOOR");
2153 		else
2154 			(void) snprintf(buffer, sizeof (buffer),
2155 			    "op=CRYPTO_LOAD_DOOR, return_val=%d", rv);
2156 		break;
2157 
2158 	default:
2159 		return;
2160 	}
2161 
2162 	au_write((caddr_t *)&ad, au_to_text(buffer));
2163 
2164 	if (mech_list_required) {
2165 		int i;
2166 
2167 		if (mech_count == 0) {
2168 			au_write((caddr_t *)&ad, au_to_text("mech=list empty"));
2169 		} else {
2170 			char	*pb = buffer;
2171 			size_t	l = sizeof (buffer);
2172 			size_t	n;
2173 			char	space[2] = ":";
2174 
2175 			n = snprintf(pb, l, "mech=");
2176 
2177 			for (i = 0; i < mech_count; i++) {
2178 				pb += n;
2179 				l -= n;
2180 				if (l < 0)
2181 					l = 0;
2182 
2183 				if (i == mech_count - 1)
2184 					(void) strcpy(space, "");
2185 
2186 				n = snprintf(pb, l, "%s%s", mech_names[i],
2187 				    space);
2188 			}
2189 			au_write((caddr_t *)&ad, au_to_text(buffer));
2190 		}
2191 	}
2192 
2193 	/* add a return token */
2194 	if (error || (rv != CRYPTO_SUCCESS))
2195 		add_return_token((caddr_t *)&ad, tad->tad_scid, -1, error);
2196 	else
2197 		add_return_token((caddr_t *)&ad, tad->tad_scid, 0, rv);
2198 
2199 	AS_INC(as_generated, 1, kctx);
2200 	AS_INC(as_kernel, 1, kctx);
2201 
2202 	au_close(kctx, (caddr_t *)&ad, AU_OK, AUE_CRYPTOADM, 0);
2203 }
2204 
2205 /*
2206  * Audit the kernel SSL administration command. The address and the
2207  * port number for the SSL instance, and the proxy port are put in the
2208  * audit trail.
2209  */
2210 void
2211 audit_kssl(int cmd, void *params, int error)
2212 {
2213 	cred_t			*cr = CRED();
2214 	t_audit_data_t		*tad;
2215 	token_t			*ad = NULL;
2216 	const auditinfo_addr_t	*ainfo = crgetauinfo(cr);
2217 	au_kcontext_t		*kctx = GET_KCTX_PZ;
2218 
2219 	tad = U2A(u);
2220 
2221 	if (ainfo == NULL)
2222 		return;
2223 
2224 	tad->tad_event = AUE_CONFIGKSSL;
2225 
2226 	if (audit_success(kctx, tad, error) != AU_OK)
2227 		return;
2228 
2229 	/* Add subject information */
2230 	AUDIT_SETSUBJ((caddr_t *)&ad, cr, ainfo, kctx);
2231 
2232 	switch (cmd) {
2233 	case KSSL_ADD_ENTRY: {
2234 		char buf[32];
2235 		kssl_params_t *kp = (kssl_params_t *)params;
2236 		struct sockaddr_in *saddr = &(kp->kssl_addr);
2237 
2238 		au_write((caddr_t *)&ad, au_to_text("op=KSSL_ADD_ENTRY"));
2239 		au_write((caddr_t *)&ad, au_to_in_addr(&(saddr->sin_addr)));
2240 		(void) snprintf(buf, sizeof (buf), "SSL port=%d",
2241 		    saddr->sin_port);
2242 		au_write((caddr_t *)&ad, au_to_text(buf));
2243 
2244 		(void) snprintf(buf, sizeof (buf), "proxy port=%d",
2245 		    kp->kssl_proxy_port);
2246 		au_write((caddr_t *)&ad, au_to_text(buf));
2247 		break;
2248 	}
2249 
2250 	case KSSL_DELETE_ENTRY: {
2251 		char buf[32];
2252 		struct sockaddr_in *saddr = (struct sockaddr_in *)params;
2253 
2254 		au_write((caddr_t *)&ad, au_to_text("op=KSSL_DELETE_ENTRY"));
2255 		au_write((caddr_t *)&ad, au_to_in_addr(&(saddr->sin_addr)));
2256 		(void) snprintf(buf, sizeof (buf), "SSL port=%d",
2257 		    saddr->sin_port);
2258 		au_write((caddr_t *)&ad, au_to_text(buf));
2259 		break;
2260 	}
2261 
2262 	default:
2263 		return;
2264 	}
2265 
2266 	/* add a return token */
2267 	add_return_token((caddr_t *)&ad, tad->tad_scid, error, 0);
2268 
2269 	AS_INC(as_generated, 1, kctx);
2270 	AS_INC(as_kernel, 1, kctx);
2271 
2272 	au_close(kctx, (caddr_t *)&ad, AU_OK, AUE_CONFIGKSSL, 0);
2273 }
2274 
2275 /*
2276  * ROUTINE:	AUDIT_SEC_ATTRIBUTES
2277  * PURPOSE:	Add security attributes
2278  * CALLBY:	AUDIT_ATTRIBUTES
2279  *		AUDIT_CLOSEF
2280  *		AUS_CLOSE
2281  * NOTE:
2282  * TODO:
2283  * QUESTION:
2284  */
2285 
2286 void
2287 audit_sec_attributes(caddr_t *ad, struct vnode *vp)
2288 {
2289 	/* Dump the SL */
2290 	if (is_system_labeled()) {
2291 		ts_label_t	*tsl;
2292 		bslabel_t	*bsl;
2293 
2294 		tsl = getflabel(vp);
2295 		if (tsl == NULL)
2296 			return;			/* nothing else to do */
2297 
2298 		bsl = label2bslabel(tsl);
2299 		if (bsl == NULL)
2300 			return;			/* nothing else to do */
2301 		au_write(ad, au_to_label(bsl));
2302 		label_rele(tsl);
2303 	}
2304 
2305 }	/* AUDIT_SEC_ATTRIBUTES */
2306