xref: /freebsd/sys/security/audit/audit_arg.c (revision 0bb263df82e129f5f8c82da6deb55dfe10daa677)
1 /*
2  * Copyright (c) 1999-2005 Apple Computer, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #include <sys/param.h>
33 #include <sys/filedesc.h>
34 #include <sys/ipc.h>
35 #include <sys/mount.h>
36 #include <sys/proc.h>
37 #include <sys/socket.h>
38 #include <sys/socketvar.h>
39 #include <sys/protosw.h>
40 #include <sys/domain.h>
41 #include <sys/sbuf.h>
42 #include <sys/systm.h>
43 #include <sys/un.h>
44 #include <sys/vnode.h>
45 
46 #include <netinet/in.h>
47 #include <netinet/in_pcb.h>
48 
49 #include <security/audit/audit.h>
50 #include <security/audit/audit_private.h>
51 
52 /*
53  * Calls to manipulate elements of the audit record structure from system
54  * call code.  Macro wrappers will prevent this functions from being entered
55  * if auditing is disabled, avoiding the function call cost.  We check the
56  * thread audit record pointer anyway, as the audit condition could change,
57  * and pre-selection may not have allocated an audit record for this event.
58  *
59  * XXXAUDIT: Should we assert, in each case, that this field of the record
60  * hasn't already been filled in?
61  */
62 void
63 audit_arg_addr(void *addr)
64 {
65 	struct kaudit_record *ar;
66 
67 	ar = currecord();
68 	if (ar == NULL)
69 		return;
70 
71 	ar->k_ar.ar_arg_addr = addr;
72 	ARG_SET_VALID(ar, ARG_ADDR);
73 }
74 
75 void
76 audit_arg_exit(int status, int retval)
77 {
78 	struct kaudit_record *ar;
79 
80 	ar = currecord();
81 	if (ar == NULL)
82 		return;
83 
84 	ar->k_ar.ar_arg_exitstatus = status;
85 	ar->k_ar.ar_arg_exitretval = retval;
86 	ARG_SET_VALID(ar, ARG_EXIT);
87 }
88 
89 void
90 audit_arg_len(int len)
91 {
92 	struct kaudit_record *ar;
93 
94 	ar = currecord();
95 	if (ar == NULL)
96 		return;
97 
98 	ar->k_ar.ar_arg_len = len;
99 	ARG_SET_VALID(ar, ARG_LEN);
100 }
101 
102 void
103 audit_arg_fd(int fd)
104 {
105 	struct kaudit_record *ar;
106 
107 	ar = currecord();
108 	if (ar == NULL)
109 		return;
110 
111 	ar->k_ar.ar_arg_fd = fd;
112 	ARG_SET_VALID(ar, ARG_FD);
113 }
114 
115 void
116 audit_arg_fflags(int fflags)
117 {
118 	struct kaudit_record *ar;
119 
120 	ar = currecord();
121 	if (ar == NULL)
122 		return;
123 
124 	ar->k_ar.ar_arg_fflags = fflags;
125 	ARG_SET_VALID(ar, ARG_FFLAGS);
126 }
127 
128 void
129 audit_arg_gid(gid_t gid)
130 {
131 	struct kaudit_record *ar;
132 
133 	ar = currecord();
134 	if (ar == NULL)
135 		return;
136 
137 	ar->k_ar.ar_arg_gid = gid;
138 	ARG_SET_VALID(ar, ARG_GID);
139 }
140 
141 void
142 audit_arg_uid(uid_t uid)
143 {
144 	struct kaudit_record *ar;
145 
146 	ar = currecord();
147 	if (ar == NULL)
148 		return;
149 
150 	ar->k_ar.ar_arg_uid = uid;
151 	ARG_SET_VALID(ar, ARG_UID);
152 }
153 
154 void
155 audit_arg_egid(gid_t egid)
156 {
157 	struct kaudit_record *ar;
158 
159 	ar = currecord();
160 	if (ar == NULL)
161 		return;
162 
163 	ar->k_ar.ar_arg_egid = egid;
164 	ARG_SET_VALID(ar, ARG_EGID);
165 }
166 
167 void
168 audit_arg_euid(uid_t euid)
169 {
170 	struct kaudit_record *ar;
171 
172 	ar = currecord();
173 	if (ar == NULL)
174 		return;
175 
176 	ar->k_ar.ar_arg_euid = euid;
177 	ARG_SET_VALID(ar, ARG_EUID);
178 }
179 
180 void
181 audit_arg_rgid(gid_t rgid)
182 {
183 	struct kaudit_record *ar;
184 
185 	ar = currecord();
186 	if (ar == NULL)
187 		return;
188 
189 	ar->k_ar.ar_arg_rgid = rgid;
190 	ARG_SET_VALID(ar, ARG_RGID);
191 }
192 
193 void
194 audit_arg_ruid(uid_t ruid)
195 {
196 	struct kaudit_record *ar;
197 
198 	ar = currecord();
199 	if (ar == NULL)
200 		return;
201 
202 	ar->k_ar.ar_arg_ruid = ruid;
203 	ARG_SET_VALID(ar, ARG_RUID);
204 }
205 
206 void
207 audit_arg_sgid(gid_t sgid)
208 {
209 	struct kaudit_record *ar;
210 
211 	ar = currecord();
212 	if (ar == NULL)
213 		return;
214 
215 	ar->k_ar.ar_arg_sgid = sgid;
216 	ARG_SET_VALID(ar, ARG_SGID);
217 }
218 
219 void
220 audit_arg_suid(uid_t suid)
221 {
222 	struct kaudit_record *ar;
223 
224 	ar = currecord();
225 	if (ar == NULL)
226 		return;
227 
228 	ar->k_ar.ar_arg_suid = suid;
229 	ARG_SET_VALID(ar, ARG_SUID);
230 }
231 
232 void
233 audit_arg_groupset(gid_t *gidset, u_int gidset_size)
234 {
235 	int i;
236 	struct kaudit_record *ar;
237 
238 	ar = currecord();
239 	if (ar == NULL)
240 		return;
241 
242 	for (i = 0; i < gidset_size; i++)
243 		ar->k_ar.ar_arg_groups.gidset[i] = gidset[i];
244 	ar->k_ar.ar_arg_groups.gidset_size = gidset_size;
245 	ARG_SET_VALID(ar, ARG_GROUPSET);
246 }
247 
248 void
249 audit_arg_login(char *login)
250 {
251 	struct kaudit_record *ar;
252 
253 	ar = currecord();
254 	if (ar == NULL)
255 		return;
256 
257 	strlcpy(ar->k_ar.ar_arg_login, login, MAXLOGNAME);
258 	ARG_SET_VALID(ar, ARG_LOGIN);
259 }
260 
261 void
262 audit_arg_ctlname(int *name, int namelen)
263 {
264 	struct kaudit_record *ar;
265 
266 	ar = currecord();
267 	if (ar == NULL)
268 		return;
269 
270 	bcopy(name, &ar->k_ar.ar_arg_ctlname, namelen * sizeof(int));
271 	ar->k_ar.ar_arg_len = namelen;
272 	ARG_SET_VALID(ar, ARG_CTLNAME | ARG_LEN);
273 }
274 
275 void
276 audit_arg_mask(int mask)
277 {
278 	struct kaudit_record *ar;
279 
280 	ar = currecord();
281 	if (ar == NULL)
282 		return;
283 
284 	ar->k_ar.ar_arg_mask = mask;
285 	ARG_SET_VALID(ar, ARG_MASK);
286 }
287 
288 void
289 audit_arg_mode(mode_t mode)
290 {
291 	struct kaudit_record *ar;
292 
293 	ar = currecord();
294 	if (ar == NULL)
295 		return;
296 
297 	ar->k_ar.ar_arg_mode = mode;
298 	ARG_SET_VALID(ar, ARG_MODE);
299 }
300 
301 void
302 audit_arg_dev(int dev)
303 {
304 	struct kaudit_record *ar;
305 
306 	ar = currecord();
307 	if (ar == NULL)
308 		return;
309 
310 	ar->k_ar.ar_arg_dev = dev;
311 	ARG_SET_VALID(ar, ARG_DEV);
312 }
313 
314 void
315 audit_arg_value(long value)
316 {
317 	struct kaudit_record *ar;
318 
319 	ar = currecord();
320 	if (ar == NULL)
321 		return;
322 
323 	ar->k_ar.ar_arg_value = value;
324 	ARG_SET_VALID(ar, ARG_VALUE);
325 }
326 
327 void
328 audit_arg_owner(uid_t uid, gid_t gid)
329 {
330 	struct kaudit_record *ar;
331 
332 	ar = currecord();
333 	if (ar == NULL)
334 		return;
335 
336 	ar->k_ar.ar_arg_uid = uid;
337 	ar->k_ar.ar_arg_gid = gid;
338 	ARG_SET_VALID(ar, ARG_UID | ARG_GID);
339 }
340 
341 void
342 audit_arg_pid(pid_t pid)
343 {
344 	struct kaudit_record *ar;
345 
346 	ar = currecord();
347 	if (ar == NULL)
348 		return;
349 
350 	ar->k_ar.ar_arg_pid = pid;
351 	ARG_SET_VALID(ar, ARG_PID);
352 }
353 
354 void
355 audit_arg_process(struct proc *p)
356 {
357 	struct kaudit_record *ar;
358 
359 	KASSERT(p != NULL, ("audit_arg_process: p == NULL"));
360 
361 	PROC_LOCK_ASSERT(p, MA_OWNED);
362 
363 	ar = currecord();
364 	if (ar == NULL)
365 		return;
366 
367 	ar->k_ar.ar_arg_auid = p->p_ucred->cr_audit.ai_auid;
368 	ar->k_ar.ar_arg_euid = p->p_ucred->cr_uid;
369 	ar->k_ar.ar_arg_egid = p->p_ucred->cr_groups[0];
370 	ar->k_ar.ar_arg_ruid = p->p_ucred->cr_ruid;
371 	ar->k_ar.ar_arg_rgid = p->p_ucred->cr_rgid;
372 	ar->k_ar.ar_arg_asid = p->p_ucred->cr_audit.ai_asid;
373 	ar->k_ar.ar_arg_termid_addr = p->p_ucred->cr_audit.ai_termid;
374 	ar->k_ar.ar_arg_pid = p->p_pid;
375 	ARG_SET_VALID(ar, ARG_AUID | ARG_EUID | ARG_EGID | ARG_RUID |
376 	    ARG_RGID | ARG_ASID | ARG_TERMID_ADDR | ARG_PID | ARG_PROCESS);
377 }
378 
379 void
380 audit_arg_signum(u_int signum)
381 {
382 	struct kaudit_record *ar;
383 
384 	ar = currecord();
385 	if (ar == NULL)
386 		return;
387 
388 	ar->k_ar.ar_arg_signum = signum;
389 	ARG_SET_VALID(ar, ARG_SIGNUM);
390 }
391 
392 void
393 audit_arg_socket(int sodomain, int sotype, int soprotocol)
394 {
395 	struct kaudit_record *ar;
396 
397 	ar = currecord();
398 	if (ar == NULL)
399 		return;
400 
401 	ar->k_ar.ar_arg_sockinfo.so_domain = sodomain;
402 	ar->k_ar.ar_arg_sockinfo.so_type = sotype;
403 	ar->k_ar.ar_arg_sockinfo.so_protocol = soprotocol;
404 	ARG_SET_VALID(ar, ARG_SOCKINFO);
405 }
406 
407 void
408 audit_arg_sockaddr(struct thread *td, struct sockaddr *sa)
409 {
410 	struct kaudit_record *ar;
411 
412 	KASSERT(td != NULL, ("audit_arg_sockaddr: td == NULL"));
413 	KASSERT(sa != NULL, ("audit_arg_sockaddr: sa == NULL"));
414 
415 	ar = currecord();
416 	if (ar == NULL)
417 		return;
418 
419 	bcopy(sa, &ar->k_ar.ar_arg_sockaddr, sa->sa_len);
420 	switch (sa->sa_family) {
421 	case AF_INET:
422 		ARG_SET_VALID(ar, ARG_SADDRINET);
423 		break;
424 
425 	case AF_INET6:
426 		ARG_SET_VALID(ar, ARG_SADDRINET6);
427 		break;
428 
429 	case AF_UNIX:
430 		audit_arg_upath(td, ((struct sockaddr_un *)sa)->sun_path,
431 				ARG_UPATH1);
432 		ARG_SET_VALID(ar, ARG_SADDRUNIX);
433 		break;
434 	/* XXXAUDIT: default:? */
435 	}
436 }
437 
438 void
439 audit_arg_auid(uid_t auid)
440 {
441 	struct kaudit_record *ar;
442 
443 	ar = currecord();
444 	if (ar == NULL)
445 		return;
446 
447 	ar->k_ar.ar_arg_auid = auid;
448 	ARG_SET_VALID(ar, ARG_AUID);
449 }
450 
451 void
452 audit_arg_auditinfo(struct auditinfo *au_info)
453 {
454 	struct kaudit_record *ar;
455 
456 	ar = currecord();
457 	if (ar == NULL)
458 		return;
459 
460 	ar->k_ar.ar_arg_auid = au_info->ai_auid;
461 	ar->k_ar.ar_arg_asid = au_info->ai_asid;
462 	ar->k_ar.ar_arg_amask.am_success = au_info->ai_mask.am_success;
463 	ar->k_ar.ar_arg_amask.am_failure = au_info->ai_mask.am_failure;
464 	ar->k_ar.ar_arg_termid.port = au_info->ai_termid.port;
465 	ar->k_ar.ar_arg_termid.machine = au_info->ai_termid.machine;
466 	ARG_SET_VALID(ar, ARG_AUID | ARG_ASID | ARG_AMASK | ARG_TERMID);
467 }
468 
469 void
470 audit_arg_text(char *text)
471 {
472 	struct kaudit_record *ar;
473 
474 	KASSERT(text != NULL, ("audit_arg_text: text == NULL"));
475 
476 	ar = currecord();
477 	if (ar == NULL)
478 		return;
479 
480 	/* Invalidate the text string */
481 	ar->k_ar.ar_valid_arg &= (ARG_ALL ^ ARG_TEXT);
482 
483 	if (ar->k_ar.ar_arg_text == NULL)
484 		ar->k_ar.ar_arg_text = malloc(MAXPATHLEN, M_AUDITTEXT,
485 		    M_WAITOK);
486 
487 	strncpy(ar->k_ar.ar_arg_text, text, MAXPATHLEN);
488 	ARG_SET_VALID(ar, ARG_TEXT);
489 }
490 
491 void
492 audit_arg_cmd(int cmd)
493 {
494 	struct kaudit_record *ar;
495 
496 	ar = currecord();
497 	if (ar == NULL)
498 		return;
499 
500 	ar->k_ar.ar_arg_cmd = cmd;
501 	ARG_SET_VALID(ar, ARG_CMD);
502 }
503 
504 void
505 audit_arg_svipc_cmd(int cmd)
506 {
507 	struct kaudit_record *ar;
508 
509 	ar = currecord();
510 	if (ar == NULL)
511 		return;
512 
513 	ar->k_ar.ar_arg_svipc_cmd = cmd;
514 	ARG_SET_VALID(ar, ARG_SVIPC_CMD);
515 }
516 
517 void
518 audit_arg_svipc_perm(struct ipc_perm *perm)
519 {
520 	struct kaudit_record *ar;
521 
522 	ar = currecord();
523 	if (ar == NULL)
524 		return;
525 
526 	bcopy(perm, &ar->k_ar.ar_arg_svipc_perm,
527 	    sizeof(ar->k_ar.ar_arg_svipc_perm));
528 	ARG_SET_VALID(ar, ARG_SVIPC_PERM);
529 }
530 
531 void
532 audit_arg_svipc_id(int id)
533 {
534 	struct kaudit_record *ar;
535 
536 	ar = currecord();
537 	if (ar == NULL)
538 		return;
539 
540 	ar->k_ar.ar_arg_svipc_id = id;
541 	ARG_SET_VALID(ar, ARG_SVIPC_ID);
542 }
543 
544 void
545 audit_arg_svipc_addr(void * addr)
546 {
547 	struct kaudit_record *ar;
548 
549 	ar = currecord();
550 	if (ar == NULL)
551 		return;
552 
553 	ar->k_ar.ar_arg_svipc_addr = addr;
554 	ARG_SET_VALID(ar, ARG_SVIPC_ADDR);
555 }
556 
557 void
558 audit_arg_posix_ipc_perm(uid_t uid, gid_t gid, mode_t mode)
559 {
560 	struct kaudit_record *ar;
561 
562 	ar = currecord();
563 	if (ar == NULL)
564 		return;
565 
566 	ar->k_ar.ar_arg_pipc_perm.pipc_uid = uid;
567 	ar->k_ar.ar_arg_pipc_perm.pipc_gid = gid;
568 	ar->k_ar.ar_arg_pipc_perm.pipc_mode = mode;
569 	ARG_SET_VALID(ar, ARG_POSIX_IPC_PERM);
570 }
571 
572 void
573 audit_arg_auditon(union auditon_udata *udata)
574 {
575 	struct kaudit_record *ar;
576 
577 	ar = currecord();
578 	if (ar == NULL)
579 		return;
580 
581 	bcopy((void *)udata, &ar->k_ar.ar_arg_auditon,
582 	    sizeof(ar->k_ar.ar_arg_auditon));
583 	ARG_SET_VALID(ar, ARG_AUDITON);
584 }
585 
586 /*
587  * Audit information about a file, either the file's vnode info, or its
588  * socket address info.
589  */
590 void
591 audit_arg_file(struct proc *p, struct file *fp)
592 {
593 	struct kaudit_record *ar;
594 	struct socket *so;
595 	struct inpcb *pcb;
596 	struct vnode *vp;
597 	int vfslocked;
598 
599 	ar = currecord();
600 	if (ar == NULL)
601 		return;
602 
603 	switch (fp->f_type) {
604 	case DTYPE_VNODE:
605 	case DTYPE_FIFO:
606 		/*
607 		 * XXXAUDIT: Only possibly to record as first vnode?
608 		 */
609 		vp = fp->f_vnode;
610 		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
611 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
612 		audit_arg_vnode(vp, ARG_VNODE1);
613 		VOP_UNLOCK(vp, 0, curthread);
614 		VFS_UNLOCK_GIANT(vfslocked);
615 		break;
616 
617 	case DTYPE_SOCKET:
618 		so = (struct socket *)fp->f_data;
619 		if (INP_CHECK_SOCKAF(so, PF_INET)) {
620 			SOCK_LOCK(so);
621 			ar->k_ar.ar_arg_sockinfo.so_type =
622 			    so->so_type;
623 			ar->k_ar.ar_arg_sockinfo.so_domain =
624 			    INP_SOCKAF(so);
625 			ar->k_ar.ar_arg_sockinfo.so_protocol =
626 			    so->so_proto->pr_protocol;
627 			SOCK_UNLOCK(so);
628 			pcb = (struct inpcb *)so->so_pcb;
629 			INP_LOCK(pcb);
630 			ar->k_ar.ar_arg_sockinfo.so_raddr =
631 			    pcb->inp_faddr.s_addr;
632 			ar->k_ar.ar_arg_sockinfo.so_laddr =
633 			    pcb->inp_laddr.s_addr;
634 			ar->k_ar.ar_arg_sockinfo.so_rport =
635 			    pcb->inp_fport;
636 			ar->k_ar.ar_arg_sockinfo.so_lport =
637 			    pcb->inp_lport;
638 			INP_UNLOCK(pcb);
639 			ARG_SET_VALID(ar, ARG_SOCKINFO);
640 		}
641 		break;
642 
643 	default:
644 		/* XXXAUDIT: else? */
645 		break;
646 	}
647 }
648 
649 /*
650  * Store a path as given by the user process for auditing into the audit
651  * record stored on the user thread. This function will allocate the memory
652  * to store the path info if not already available. This memory will be freed
653  * when the audit record is freed.
654  *
655  * XXXAUDIT: Possibly assert that the memory isn't already allocated?
656  */
657 void
658 audit_arg_upath(struct thread *td, char *upath, u_int64_t flag)
659 {
660 	struct kaudit_record *ar;
661 	char **pathp;
662 
663 	KASSERT(td != NULL, ("audit_arg_upath: td == NULL"));
664 	KASSERT(upath != NULL, ("audit_arg_upath: upath == NULL"));
665 
666 	ar = currecord();
667 	if (ar == NULL)
668 		return;
669 
670 	KASSERT((flag == ARG_UPATH1) || (flag == ARG_UPATH2),
671 	    ("audit_arg_upath: flag %llu", (unsigned long long)flag));
672 	KASSERT((flag != ARG_UPATH1) || (flag != ARG_UPATH2),
673 	    ("audit_arg_upath: flag %llu", (unsigned long long)flag));
674 
675 	if (flag == ARG_UPATH1)
676 		pathp = &ar->k_ar.ar_arg_upath1;
677 	else
678 		pathp = &ar->k_ar.ar_arg_upath2;
679 
680 	if (*pathp == NULL)
681 		*pathp = malloc(MAXPATHLEN, M_AUDITPATH, M_WAITOK);
682 
683 	canon_path(td, upath, *pathp);
684 
685 	ARG_SET_VALID(ar, flag);
686 }
687 
688 /*
689  * Function to save the path and vnode attr information into the audit
690  * record.
691  *
692  * It is assumed that the caller will hold any vnode locks necessary to
693  * perform a VOP_GETATTR() on the passed vnode.
694  *
695  * XXX: The attr code is very similar to vfs_vnops.c:vn_stat(), but always
696  * provides access to the generation number as we need that to construct the
697  * BSM file ID.
698  *
699  * XXX: We should accept the process argument from the caller, since it's
700  * very likely they already have a reference.
701  *
702  * XXX: Error handling in this function is poor.
703  *
704  * XXXAUDIT: Possibly KASSERT the path pointer is NULL?
705  */
706 void
707 audit_arg_vnode(struct vnode *vp, u_int64_t flags)
708 {
709 	struct kaudit_record *ar;
710 	struct vattr vattr;
711 	int error;
712 	struct vnode_au_info *vnp;
713 
714 	KASSERT(vp != NULL, ("audit_arg_vnode: vp == NULL"));
715 	KASSERT((flags == ARG_VNODE1) || (flags == ARG_VNODE2),
716 	    ("audit_arg_vnode: flags %jd", (intmax_t)flags));
717 
718 	/*
719 	 * Assume that if the caller is calling audit_arg_vnode() on a
720 	 * non-MPSAFE vnode, then it will have acquired Giant.
721 	 */
722 	VFS_ASSERT_GIANT(vp->v_mount);
723 	ASSERT_VOP_LOCKED(vp, "audit_arg_vnode");
724 
725 	ar = currecord();
726 	if (ar == NULL)
727 		return;
728 
729 	/*
730 	 * XXXAUDIT: The below clears, and then resets the flags for valid
731 	 * arguments.  Ideally, either the new vnode is used, or the old one
732 	 * would be.
733 	 */
734 	if (flags & ARG_VNODE1) {
735 		ar->k_ar.ar_valid_arg &= (ARG_ALL ^ ARG_VNODE1);
736 		vnp = &ar->k_ar.ar_arg_vnode1;
737 	} else {
738 		ar->k_ar.ar_valid_arg &= (ARG_ALL ^ ARG_VNODE2);
739 		vnp = &ar->k_ar.ar_arg_vnode2;
740 	}
741 
742 	error = VOP_GETATTR(vp, &vattr, curthread->td_ucred, curthread);
743 	if (error) {
744 		/* XXX: How to handle this case? */
745 		return;
746 	}
747 
748 	vnp->vn_mode = vattr.va_mode;
749 	vnp->vn_uid = vattr.va_uid;
750 	vnp->vn_gid = vattr.va_gid;
751 	vnp->vn_dev = vattr.va_rdev;
752 	vnp->vn_fsid = vattr.va_fsid;
753 	vnp->vn_fileid = vattr.va_fileid;
754 	vnp->vn_gen = vattr.va_gen;
755 	if (flags & ARG_VNODE1)
756 		ARG_SET_VALID(ar, ARG_VNODE1);
757 	else
758 		ARG_SET_VALID(ar, ARG_VNODE2);
759 }
760 
761 /*
762  * Audit the argument strings passed to exec.
763  */
764 void
765 audit_arg_argv(char *argv, int argc, int length)
766 {
767 	struct kaudit_record *ar;
768 
769 	if (audit_argv == 0)
770 		return;
771 
772 	ar = currecord();
773 	if (ar == NULL)
774 		return;
775 
776 	ar->k_ar.ar_arg_argv = malloc(length, M_AUDITTEXT, M_WAITOK);
777 	bcopy(argv, ar->k_ar.ar_arg_argv, length);
778 	ar->k_ar.ar_arg_argc = argc;
779 	ARG_SET_VALID(ar, ARG_ARGV);
780 }
781 
782 /*
783  * Audit the environment strings passed to exec.
784  */
785 void
786 audit_arg_envv(char *envv, int envc, int length)
787 {
788 	struct kaudit_record *ar;
789 
790 	if (audit_arge == 0)
791 		return;
792 
793 	ar = currecord();
794 	if (ar == NULL)
795 		return;
796 
797 	ar->k_ar.ar_arg_envv = malloc(length, M_AUDITTEXT, M_WAITOK);
798 	bcopy(envv, ar->k_ar.ar_arg_envv, length);
799 	ar->k_ar.ar_arg_envc = envc;
800 	ARG_SET_VALID(ar, ARG_ENVV);
801 }
802 
803 /*
804  * The close() system call uses it's own audit call to capture the path/vnode
805  * information because those pieces are not easily obtained within the system
806  * call itself.
807  */
808 void
809 audit_sysclose(struct thread *td, int fd)
810 {
811 	struct kaudit_record *ar;
812 	struct vnode *vp;
813 	struct file *fp;
814 	int vfslocked;
815 
816 	KASSERT(td != NULL, ("audit_sysclose: td == NULL"));
817 
818 	ar = currecord();
819 	if (ar == NULL)
820 		return;
821 
822 	audit_arg_fd(fd);
823 
824 	if (getvnode(td->td_proc->p_fd, fd, &fp) != 0)
825 		return;
826 
827 	vp = fp->f_vnode;
828 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
829 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
830 	audit_arg_vnode(vp, ARG_VNODE1);
831 	VOP_UNLOCK(vp, 0, td);
832 	VFS_UNLOCK_GIANT(vfslocked);
833 	fdrop(fp, td);
834 }
835