audit_arg.c revision 178186
190792Sgshapiro/*
290792Sgshapiro * Copyright (c) 1999-2005 Apple Computer, Inc.
390792Sgshapiro * All rights reserved.
490792Sgshapiro *
590792Sgshapiro * Redistribution and use in source and binary forms, with or without
690792Sgshapiro * modification, are permitted provided that the following conditions
790792Sgshapiro * are met:
890792Sgshapiro * 1.  Redistributions of source code must retain the above copyright
990792Sgshapiro *     notice, this list of conditions and the following disclaimer.
1090792Sgshapiro * 2.  Redistributions in binary form must reproduce the above copyright
1190792Sgshapiro *     notice, this list of conditions and the following disclaimer in the
1290792Sgshapiro *     documentation and/or other materials provided with the distribution.
1390792Sgshapiro * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
1490792Sgshapiro *     its contributors may be used to endorse or promote products derived
1590792Sgshapiro *     from this software without specific prior written permission.
1698121Sgshapiro *
1790792Sgshapiro * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
1890792Sgshapiro * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1990792Sgshapiro * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2090792Sgshapiro * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
2190792Sgshapiro * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2290792Sgshapiro * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2390792Sgshapiro * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2490792Sgshapiro * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2590792Sgshapiro * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
2690792Sgshapiro * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2790792Sgshapiro * POSSIBILITY OF SUCH DAMAGE.
2890792Sgshapiro */
2990792Sgshapiro
3090792Sgshapiro#include <sys/cdefs.h>
3190792Sgshapiro__FBSDID("$FreeBSD: head/sys/security/audit/audit_arg.c 178186 2008-04-13 22:06:56Z rwatson $");
3290792Sgshapiro
3390792Sgshapiro#include <sys/param.h>
3490792Sgshapiro#include <sys/filedesc.h>
3590792Sgshapiro#include <sys/ipc.h>
3690792Sgshapiro#include <sys/mount.h>
3790792Sgshapiro#include <sys/proc.h>
3890792Sgshapiro#include <sys/socket.h>
3990792Sgshapiro#include <sys/socketvar.h>
4090792Sgshapiro#include <sys/protosw.h>
4190792Sgshapiro#include <sys/domain.h>
4290792Sgshapiro#include <sys/sbuf.h>
4390792Sgshapiro#include <sys/systm.h>
4490792Sgshapiro#include <sys/un.h>
4590792Sgshapiro#include <sys/vnode.h>
4690792Sgshapiro
4790792Sgshapiro#include <netinet/in.h>
4890792Sgshapiro#include <netinet/in_pcb.h>
4990792Sgshapiro
5090792Sgshapiro#include <security/audit/audit.h>
5190792Sgshapiro#include <security/audit/audit_private.h>
5290792Sgshapiro
5390792Sgshapiro/*
5490792Sgshapiro * Calls to manipulate elements of the audit record structure from system
5590792Sgshapiro * call code.  Macro wrappers will prevent this functions from being entered
5690792Sgshapiro * if auditing is disabled, avoiding the function call cost.  We check the
5790792Sgshapiro * thread audit record pointer anyway, as the audit condition could change,
5890792Sgshapiro * and pre-selection may not have allocated an audit record for this event.
5990792Sgshapiro *
6090792Sgshapiro * XXXAUDIT: Should we assert, in each case, that this field of the record
6190792Sgshapiro * hasn't already been filled in?
6290792Sgshapiro */
6390792Sgshapirovoid
6490792Sgshapiroaudit_arg_addr(void *addr)
6590792Sgshapiro{
6690792Sgshapiro	struct kaudit_record *ar;
6790792Sgshapiro
6890792Sgshapiro	ar = currecord();
6990792Sgshapiro	if (ar == NULL)
7090792Sgshapiro		return;
7190792Sgshapiro
7290792Sgshapiro	ar->k_ar.ar_arg_addr = addr;
7390792Sgshapiro	ARG_SET_VALID(ar, ARG_ADDR);
7490792Sgshapiro}
7590792Sgshapiro
7690792Sgshapirovoid
7790792Sgshapiroaudit_arg_exit(int status, int retval)
7890792Sgshapiro{
7990792Sgshapiro	struct kaudit_record *ar;
8090792Sgshapiro
8190792Sgshapiro	ar = currecord();
8290792Sgshapiro	if (ar == NULL)
8390792Sgshapiro		return;
8490792Sgshapiro
8590792Sgshapiro	ar->k_ar.ar_arg_exitstatus = status;
8690792Sgshapiro	ar->k_ar.ar_arg_exitretval = retval;
8790792Sgshapiro	ARG_SET_VALID(ar, ARG_EXIT);
8890792Sgshapiro}
8990792Sgshapiro
9090792Sgshapirovoid
9190792Sgshapiroaudit_arg_len(int len)
9290792Sgshapiro{
9390792Sgshapiro	struct kaudit_record *ar;
9490792Sgshapiro
9590792Sgshapiro	ar = currecord();
9690792Sgshapiro	if (ar == NULL)
9790792Sgshapiro		return;
9890792Sgshapiro
9990792Sgshapiro	ar->k_ar.ar_arg_len = len;
10090792Sgshapiro	ARG_SET_VALID(ar, ARG_LEN);
10190792Sgshapiro}
10290792Sgshapiro
10390792Sgshapirovoid
10490792Sgshapiroaudit_arg_fd(int fd)
10590792Sgshapiro{
10690792Sgshapiro	struct kaudit_record *ar;
10790792Sgshapiro
10890792Sgshapiro	ar = currecord();
10990792Sgshapiro	if (ar == NULL)
11090792Sgshapiro		return;
11190792Sgshapiro
11290792Sgshapiro	ar->k_ar.ar_arg_fd = fd;
11390792Sgshapiro	ARG_SET_VALID(ar, ARG_FD);
11490792Sgshapiro}
11590792Sgshapiro
11690792Sgshapirovoid
11790792Sgshapiroaudit_arg_fflags(int fflags)
11890792Sgshapiro{
11990792Sgshapiro	struct kaudit_record *ar;
12090792Sgshapiro
12190792Sgshapiro	ar = currecord();
12290792Sgshapiro	if (ar == NULL)
12390792Sgshapiro		return;
12490792Sgshapiro
12590792Sgshapiro	ar->k_ar.ar_arg_fflags = fflags;
12690792Sgshapiro	ARG_SET_VALID(ar, ARG_FFLAGS);
12790792Sgshapiro}
12890792Sgshapiro
12990792Sgshapirovoid
13090792Sgshapiroaudit_arg_gid(gid_t gid)
13190792Sgshapiro{
13290792Sgshapiro	struct kaudit_record *ar;
13390792Sgshapiro
13490792Sgshapiro	ar = currecord();
13590792Sgshapiro	if (ar == NULL)
13690792Sgshapiro		return;
13790792Sgshapiro
13890792Sgshapiro	ar->k_ar.ar_arg_gid = gid;
13990792Sgshapiro	ARG_SET_VALID(ar, ARG_GID);
14090792Sgshapiro}
14190792Sgshapiro
14290792Sgshapirovoid
14390792Sgshapiroaudit_arg_uid(uid_t uid)
14490792Sgshapiro{
14590792Sgshapiro	struct kaudit_record *ar;
14690792Sgshapiro
14790792Sgshapiro	ar = currecord();
14890792Sgshapiro	if (ar == NULL)
14990792Sgshapiro		return;
15090792Sgshapiro
15190792Sgshapiro	ar->k_ar.ar_arg_uid = uid;
15290792Sgshapiro	ARG_SET_VALID(ar, ARG_UID);
15390792Sgshapiro}
15490792Sgshapiro
15590792Sgshapirovoid
15690792Sgshapiroaudit_arg_egid(gid_t egid)
15790792Sgshapiro{
15890792Sgshapiro	struct kaudit_record *ar;
15990792Sgshapiro
16090792Sgshapiro	ar = currecord();
16190792Sgshapiro	if (ar == NULL)
16290792Sgshapiro		return;
16390792Sgshapiro
16490792Sgshapiro	ar->k_ar.ar_arg_egid = egid;
16590792Sgshapiro	ARG_SET_VALID(ar, ARG_EGID);
16690792Sgshapiro}
16790792Sgshapiro
16890792Sgshapirovoid
16990792Sgshapiroaudit_arg_euid(uid_t euid)
17090792Sgshapiro{
17190792Sgshapiro	struct kaudit_record *ar;
17290792Sgshapiro
17390792Sgshapiro	ar = currecord();
17490792Sgshapiro	if (ar == NULL)
17590792Sgshapiro		return;
17690792Sgshapiro
17790792Sgshapiro	ar->k_ar.ar_arg_euid = euid;
17890792Sgshapiro	ARG_SET_VALID(ar, ARG_EUID);
17990792Sgshapiro}
18090792Sgshapiro
18190792Sgshapirovoid
18290792Sgshapiroaudit_arg_rgid(gid_t rgid)
18390792Sgshapiro{
18490792Sgshapiro	struct kaudit_record *ar;
18590792Sgshapiro
18690792Sgshapiro	ar = currecord();
18790792Sgshapiro	if (ar == NULL)
18890792Sgshapiro		return;
18990792Sgshapiro
19090792Sgshapiro	ar->k_ar.ar_arg_rgid = rgid;
19190792Sgshapiro	ARG_SET_VALID(ar, ARG_RGID);
19290792Sgshapiro}
19390792Sgshapiro
19490792Sgshapirovoid
19590792Sgshapiroaudit_arg_ruid(uid_t ruid)
19690792Sgshapiro{
19790792Sgshapiro	struct kaudit_record *ar;
19890792Sgshapiro
19990792Sgshapiro	ar = currecord();
20090792Sgshapiro	if (ar == NULL)
20190792Sgshapiro		return;
20290792Sgshapiro
20390792Sgshapiro	ar->k_ar.ar_arg_ruid = ruid;
20490792Sgshapiro	ARG_SET_VALID(ar, ARG_RUID);
20590792Sgshapiro}
20690792Sgshapiro
20790792Sgshapirovoid
20890792Sgshapiroaudit_arg_sgid(gid_t sgid)
20990792Sgshapiro{
21090792Sgshapiro	struct kaudit_record *ar;
21190792Sgshapiro
21290792Sgshapiro	ar = currecord();
21390792Sgshapiro	if (ar == NULL)
21490792Sgshapiro		return;
21590792Sgshapiro
21690792Sgshapiro	ar->k_ar.ar_arg_sgid = sgid;
21790792Sgshapiro	ARG_SET_VALID(ar, ARG_SGID);
21890792Sgshapiro}
21990792Sgshapiro
22090792Sgshapirovoid
22190792Sgshapiroaudit_arg_suid(uid_t suid)
22290792Sgshapiro{
22390792Sgshapiro	struct kaudit_record *ar;
22490792Sgshapiro
22590792Sgshapiro	ar = currecord();
22690792Sgshapiro	if (ar == NULL)
22790792Sgshapiro		return;
22890792Sgshapiro
22990792Sgshapiro	ar->k_ar.ar_arg_suid = suid;
23090792Sgshapiro	ARG_SET_VALID(ar, ARG_SUID);
23190792Sgshapiro}
23290792Sgshapiro
23390792Sgshapirovoid
23490792Sgshapiroaudit_arg_groupset(gid_t *gidset, u_int gidset_size)
23590792Sgshapiro{
23690792Sgshapiro	int i;
23790792Sgshapiro	struct kaudit_record *ar;
23890792Sgshapiro
23990792Sgshapiro	ar = currecord();
24090792Sgshapiro	if (ar == NULL)
24190792Sgshapiro		return;
24290792Sgshapiro
24390792Sgshapiro	for (i = 0; i < gidset_size; i++)
24490792Sgshapiro		ar->k_ar.ar_arg_groups.gidset[i] = gidset[i];
24590792Sgshapiro	ar->k_ar.ar_arg_groups.gidset_size = gidset_size;
24690792Sgshapiro	ARG_SET_VALID(ar, ARG_GROUPSET);
24790792Sgshapiro}
24890792Sgshapiro
24990792Sgshapirovoid
25090792Sgshapiroaudit_arg_login(char *login)
25190792Sgshapiro{
25290792Sgshapiro	struct kaudit_record *ar;
25390792Sgshapiro
25490792Sgshapiro	ar = currecord();
25590792Sgshapiro	if (ar == NULL)
25690792Sgshapiro		return;
25790792Sgshapiro
25890792Sgshapiro	strlcpy(ar->k_ar.ar_arg_login, login, MAXLOGNAME);
25990792Sgshapiro	ARG_SET_VALID(ar, ARG_LOGIN);
26090792Sgshapiro}
26190792Sgshapiro
26290792Sgshapirovoid
26390792Sgshapiroaudit_arg_ctlname(int *name, int namelen)
26490792Sgshapiro{
26590792Sgshapiro	struct kaudit_record *ar;
26690792Sgshapiro
26790792Sgshapiro	ar = currecord();
26890792Sgshapiro	if (ar == NULL)
26990792Sgshapiro		return;
27090792Sgshapiro
27190792Sgshapiro	bcopy(name, &ar->k_ar.ar_arg_ctlname, namelen * sizeof(int));
27290792Sgshapiro	ar->k_ar.ar_arg_len = namelen;
27390792Sgshapiro	ARG_SET_VALID(ar, ARG_CTLNAME | ARG_LEN);
27490792Sgshapiro}
27590792Sgshapiro
27690792Sgshapirovoid
27790792Sgshapiroaudit_arg_mask(int mask)
27890792Sgshapiro{
27990792Sgshapiro	struct kaudit_record *ar;
280
281	ar = currecord();
282	if (ar == NULL)
283		return;
284
285	ar->k_ar.ar_arg_mask = mask;
286	ARG_SET_VALID(ar, ARG_MASK);
287}
288
289void
290audit_arg_mode(mode_t mode)
291{
292	struct kaudit_record *ar;
293
294	ar = currecord();
295	if (ar == NULL)
296		return;
297
298	ar->k_ar.ar_arg_mode = mode;
299	ARG_SET_VALID(ar, ARG_MODE);
300}
301
302void
303audit_arg_dev(int dev)
304{
305	struct kaudit_record *ar;
306
307	ar = currecord();
308	if (ar == NULL)
309		return;
310
311	ar->k_ar.ar_arg_dev = dev;
312	ARG_SET_VALID(ar, ARG_DEV);
313}
314
315void
316audit_arg_value(long value)
317{
318	struct kaudit_record *ar;
319
320	ar = currecord();
321	if (ar == NULL)
322		return;
323
324	ar->k_ar.ar_arg_value = value;
325	ARG_SET_VALID(ar, ARG_VALUE);
326}
327
328void
329audit_arg_owner(uid_t uid, gid_t gid)
330{
331	struct kaudit_record *ar;
332
333	ar = currecord();
334	if (ar == NULL)
335		return;
336
337	ar->k_ar.ar_arg_uid = uid;
338	ar->k_ar.ar_arg_gid = gid;
339	ARG_SET_VALID(ar, ARG_UID | ARG_GID);
340}
341
342void
343audit_arg_pid(pid_t pid)
344{
345	struct kaudit_record *ar;
346
347	ar = currecord();
348	if (ar == NULL)
349		return;
350
351	ar->k_ar.ar_arg_pid = pid;
352	ARG_SET_VALID(ar, ARG_PID);
353}
354
355void
356audit_arg_process(struct proc *p)
357{
358	struct kaudit_record *ar;
359
360	KASSERT(p != NULL, ("audit_arg_process: p == NULL"));
361
362	PROC_LOCK_ASSERT(p, MA_OWNED);
363
364	ar = currecord();
365	if (ar == NULL)
366		return;
367
368	ar->k_ar.ar_arg_auid = p->p_ucred->cr_audit.ai_auid;
369	ar->k_ar.ar_arg_euid = p->p_ucred->cr_uid;
370	ar->k_ar.ar_arg_egid = p->p_ucred->cr_groups[0];
371	ar->k_ar.ar_arg_ruid = p->p_ucred->cr_ruid;
372	ar->k_ar.ar_arg_rgid = p->p_ucred->cr_rgid;
373	ar->k_ar.ar_arg_asid = p->p_ucred->cr_audit.ai_asid;
374	ar->k_ar.ar_arg_termid_addr = p->p_ucred->cr_audit.ai_termid;
375	ar->k_ar.ar_arg_pid = p->p_pid;
376	ARG_SET_VALID(ar, ARG_AUID | ARG_EUID | ARG_EGID | ARG_RUID |
377	    ARG_RGID | ARG_ASID | ARG_TERMID_ADDR | ARG_PID | ARG_PROCESS);
378}
379
380void
381audit_arg_signum(u_int signum)
382{
383	struct kaudit_record *ar;
384
385	ar = currecord();
386	if (ar == NULL)
387		return;
388
389	ar->k_ar.ar_arg_signum = signum;
390	ARG_SET_VALID(ar, ARG_SIGNUM);
391}
392
393void
394audit_arg_socket(int sodomain, int sotype, int soprotocol)
395{
396	struct kaudit_record *ar;
397
398	ar = currecord();
399	if (ar == NULL)
400		return;
401
402	ar->k_ar.ar_arg_sockinfo.so_domain = sodomain;
403	ar->k_ar.ar_arg_sockinfo.so_type = sotype;
404	ar->k_ar.ar_arg_sockinfo.so_protocol = soprotocol;
405	ARG_SET_VALID(ar, ARG_SOCKINFO);
406}
407
408void
409audit_arg_sockaddr(struct thread *td, struct sockaddr *sa)
410{
411	struct kaudit_record *ar;
412
413	KASSERT(td != NULL, ("audit_arg_sockaddr: td == NULL"));
414	KASSERT(sa != NULL, ("audit_arg_sockaddr: sa == NULL"));
415
416	ar = currecord();
417	if (ar == NULL)
418		return;
419
420	bcopy(sa, &ar->k_ar.ar_arg_sockaddr, sa->sa_len);
421	switch (sa->sa_family) {
422	case AF_INET:
423		ARG_SET_VALID(ar, ARG_SADDRINET);
424		break;
425
426	case AF_INET6:
427		ARG_SET_VALID(ar, ARG_SADDRINET6);
428		break;
429
430	case AF_UNIX:
431		audit_arg_upath(td, ((struct sockaddr_un *)sa)->sun_path,
432				ARG_UPATH1);
433		ARG_SET_VALID(ar, ARG_SADDRUNIX);
434		break;
435	/* XXXAUDIT: default:? */
436	}
437}
438
439void
440audit_arg_auid(uid_t auid)
441{
442	struct kaudit_record *ar;
443
444	ar = currecord();
445	if (ar == NULL)
446		return;
447
448	ar->k_ar.ar_arg_auid = auid;
449	ARG_SET_VALID(ar, ARG_AUID);
450}
451
452void
453audit_arg_auditinfo(struct auditinfo *au_info)
454{
455	struct kaudit_record *ar;
456
457	ar = currecord();
458	if (ar == NULL)
459		return;
460
461	ar->k_ar.ar_arg_auid = au_info->ai_auid;
462	ar->k_ar.ar_arg_asid = au_info->ai_asid;
463	ar->k_ar.ar_arg_amask.am_success = au_info->ai_mask.am_success;
464	ar->k_ar.ar_arg_amask.am_failure = au_info->ai_mask.am_failure;
465	ar->k_ar.ar_arg_termid.port = au_info->ai_termid.port;
466	ar->k_ar.ar_arg_termid.machine = au_info->ai_termid.machine;
467	ARG_SET_VALID(ar, ARG_AUID | ARG_ASID | ARG_AMASK | ARG_TERMID);
468}
469
470void
471audit_arg_auditinfo_addr(struct auditinfo_addr *au_info)
472{
473	struct kaudit_record *ar;
474
475	ar = currecord();
476	if (ar == NULL)
477		return;
478
479	ar->k_ar.ar_arg_auid = au_info->ai_auid;
480	ar->k_ar.ar_arg_asid = au_info->ai_asid;
481	ar->k_ar.ar_arg_amask.am_success = au_info->ai_mask.am_success;
482	ar->k_ar.ar_arg_amask.am_failure = au_info->ai_mask.am_failure;
483	ar->k_ar.ar_arg_termid_addr.at_type = au_info->ai_termid.at_type;
484	ar->k_ar.ar_arg_termid_addr.at_port = au_info->ai_termid.at_port;
485	ar->k_ar.ar_arg_termid_addr.at_addr[0] = au_info->ai_termid.at_addr[0];
486	ar->k_ar.ar_arg_termid_addr.at_addr[1] = au_info->ai_termid.at_addr[1];
487	ar->k_ar.ar_arg_termid_addr.at_addr[2] = au_info->ai_termid.at_addr[2];
488	ar->k_ar.ar_arg_termid_addr.at_addr[3] = au_info->ai_termid.at_addr[3];
489	ARG_SET_VALID(ar, ARG_AUID | ARG_ASID | ARG_AMASK | ARG_TERMID_ADDR);
490}
491
492void
493audit_arg_text(char *text)
494{
495	struct kaudit_record *ar;
496
497	KASSERT(text != NULL, ("audit_arg_text: text == NULL"));
498
499	ar = currecord();
500	if (ar == NULL)
501		return;
502
503	/* Invalidate the text string */
504	ar->k_ar.ar_valid_arg &= (ARG_ALL ^ ARG_TEXT);
505
506	if (ar->k_ar.ar_arg_text == NULL)
507		ar->k_ar.ar_arg_text = malloc(MAXPATHLEN, M_AUDITTEXT,
508		    M_WAITOK);
509
510	strncpy(ar->k_ar.ar_arg_text, text, MAXPATHLEN);
511	ARG_SET_VALID(ar, ARG_TEXT);
512}
513
514void
515audit_arg_cmd(int cmd)
516{
517	struct kaudit_record *ar;
518
519	ar = currecord();
520	if (ar == NULL)
521		return;
522
523	ar->k_ar.ar_arg_cmd = cmd;
524	ARG_SET_VALID(ar, ARG_CMD);
525}
526
527void
528audit_arg_svipc_cmd(int cmd)
529{
530	struct kaudit_record *ar;
531
532	ar = currecord();
533	if (ar == NULL)
534		return;
535
536	ar->k_ar.ar_arg_svipc_cmd = cmd;
537	ARG_SET_VALID(ar, ARG_SVIPC_CMD);
538}
539
540void
541audit_arg_svipc_perm(struct ipc_perm *perm)
542{
543	struct kaudit_record *ar;
544
545	ar = currecord();
546	if (ar == NULL)
547		return;
548
549	bcopy(perm, &ar->k_ar.ar_arg_svipc_perm,
550	    sizeof(ar->k_ar.ar_arg_svipc_perm));
551	ARG_SET_VALID(ar, ARG_SVIPC_PERM);
552}
553
554void
555audit_arg_svipc_id(int id)
556{
557	struct kaudit_record *ar;
558
559	ar = currecord();
560	if (ar == NULL)
561		return;
562
563	ar->k_ar.ar_arg_svipc_id = id;
564	ARG_SET_VALID(ar, ARG_SVIPC_ID);
565}
566
567void
568audit_arg_svipc_addr(void * addr)
569{
570	struct kaudit_record *ar;
571
572	ar = currecord();
573	if (ar == NULL)
574		return;
575
576	ar->k_ar.ar_arg_svipc_addr = addr;
577	ARG_SET_VALID(ar, ARG_SVIPC_ADDR);
578}
579
580void
581audit_arg_posix_ipc_perm(uid_t uid, gid_t gid, mode_t mode)
582{
583	struct kaudit_record *ar;
584
585	ar = currecord();
586	if (ar == NULL)
587		return;
588
589	ar->k_ar.ar_arg_pipc_perm.pipc_uid = uid;
590	ar->k_ar.ar_arg_pipc_perm.pipc_gid = gid;
591	ar->k_ar.ar_arg_pipc_perm.pipc_mode = mode;
592	ARG_SET_VALID(ar, ARG_POSIX_IPC_PERM);
593}
594
595void
596audit_arg_auditon(union auditon_udata *udata)
597{
598	struct kaudit_record *ar;
599
600	ar = currecord();
601	if (ar == NULL)
602		return;
603
604	bcopy((void *)udata, &ar->k_ar.ar_arg_auditon,
605	    sizeof(ar->k_ar.ar_arg_auditon));
606	ARG_SET_VALID(ar, ARG_AUDITON);
607}
608
609/*
610 * Audit information about a file, either the file's vnode info, or its
611 * socket address info.
612 */
613void
614audit_arg_file(struct proc *p, struct file *fp)
615{
616	struct kaudit_record *ar;
617	struct socket *so;
618	struct inpcb *pcb;
619	struct vnode *vp;
620	int vfslocked;
621
622	ar = currecord();
623	if (ar == NULL)
624		return;
625
626	switch (fp->f_type) {
627	case DTYPE_VNODE:
628	case DTYPE_FIFO:
629		/*
630		 * XXXAUDIT: Only possibly to record as first vnode?
631		 */
632		vp = fp->f_vnode;
633		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
634		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
635		audit_arg_vnode(vp, ARG_VNODE1);
636		VOP_UNLOCK(vp, 0);
637		VFS_UNLOCK_GIANT(vfslocked);
638		break;
639
640	case DTYPE_SOCKET:
641		so = (struct socket *)fp->f_data;
642		if (INP_CHECK_SOCKAF(so, PF_INET)) {
643			SOCK_LOCK(so);
644			ar->k_ar.ar_arg_sockinfo.so_type =
645			    so->so_type;
646			ar->k_ar.ar_arg_sockinfo.so_domain =
647			    INP_SOCKAF(so);
648			ar->k_ar.ar_arg_sockinfo.so_protocol =
649			    so->so_proto->pr_protocol;
650			SOCK_UNLOCK(so);
651			pcb = (struct inpcb *)so->so_pcb;
652			INP_LOCK(pcb);
653			ar->k_ar.ar_arg_sockinfo.so_raddr =
654			    pcb->inp_faddr.s_addr;
655			ar->k_ar.ar_arg_sockinfo.so_laddr =
656			    pcb->inp_laddr.s_addr;
657			ar->k_ar.ar_arg_sockinfo.so_rport =
658			    pcb->inp_fport;
659			ar->k_ar.ar_arg_sockinfo.so_lport =
660			    pcb->inp_lport;
661			INP_UNLOCK(pcb);
662			ARG_SET_VALID(ar, ARG_SOCKINFO);
663		}
664		break;
665
666	default:
667		/* XXXAUDIT: else? */
668		break;
669	}
670}
671
672/*
673 * Store a path as given by the user process for auditing into the audit
674 * record stored on the user thread. This function will allocate the memory
675 * to store the path info if not already available. This memory will be freed
676 * when the audit record is freed.
677 *
678 * XXXAUDIT: Possibly assert that the memory isn't already allocated?
679 */
680void
681audit_arg_upath(struct thread *td, char *upath, u_int64_t flag)
682{
683	struct kaudit_record *ar;
684	char **pathp;
685
686	KASSERT(td != NULL, ("audit_arg_upath: td == NULL"));
687	KASSERT(upath != NULL, ("audit_arg_upath: upath == NULL"));
688
689	ar = currecord();
690	if (ar == NULL)
691		return;
692
693	KASSERT((flag == ARG_UPATH1) || (flag == ARG_UPATH2),
694	    ("audit_arg_upath: flag %llu", (unsigned long long)flag));
695	KASSERT((flag != ARG_UPATH1) || (flag != ARG_UPATH2),
696	    ("audit_arg_upath: flag %llu", (unsigned long long)flag));
697
698	if (flag == ARG_UPATH1)
699		pathp = &ar->k_ar.ar_arg_upath1;
700	else
701		pathp = &ar->k_ar.ar_arg_upath2;
702
703	if (*pathp == NULL)
704		*pathp = malloc(MAXPATHLEN, M_AUDITPATH, M_WAITOK);
705
706	audit_canon_path(td, upath, *pathp);
707
708	ARG_SET_VALID(ar, flag);
709}
710
711/*
712 * Function to save the path and vnode attr information into the audit
713 * record.
714 *
715 * It is assumed that the caller will hold any vnode locks necessary to
716 * perform a VOP_GETATTR() on the passed vnode.
717 *
718 * XXX: The attr code is very similar to vfs_vnops.c:vn_stat(), but always
719 * provides access to the generation number as we need that to construct the
720 * BSM file ID.
721 *
722 * XXX: We should accept the process argument from the caller, since it's
723 * very likely they already have a reference.
724 *
725 * XXX: Error handling in this function is poor.
726 *
727 * XXXAUDIT: Possibly KASSERT the path pointer is NULL?
728 */
729void
730audit_arg_vnode(struct vnode *vp, u_int64_t flags)
731{
732	struct kaudit_record *ar;
733	struct vattr vattr;
734	int error;
735	struct vnode_au_info *vnp;
736
737	KASSERT(vp != NULL, ("audit_arg_vnode: vp == NULL"));
738	KASSERT((flags == ARG_VNODE1) || (flags == ARG_VNODE2),
739	    ("audit_arg_vnode: flags %jd", (intmax_t)flags));
740
741	/*
742	 * Assume that if the caller is calling audit_arg_vnode() on a
743	 * non-MPSAFE vnode, then it will have acquired Giant.
744	 */
745	VFS_ASSERT_GIANT(vp->v_mount);
746	ASSERT_VOP_LOCKED(vp, "audit_arg_vnode");
747
748	ar = currecord();
749	if (ar == NULL)
750		return;
751
752	/*
753	 * XXXAUDIT: The below clears, and then resets the flags for valid
754	 * arguments.  Ideally, either the new vnode is used, or the old one
755	 * would be.
756	 */
757	if (flags & ARG_VNODE1) {
758		ar->k_ar.ar_valid_arg &= (ARG_ALL ^ ARG_VNODE1);
759		vnp = &ar->k_ar.ar_arg_vnode1;
760	} else {
761		ar->k_ar.ar_valid_arg &= (ARG_ALL ^ ARG_VNODE2);
762		vnp = &ar->k_ar.ar_arg_vnode2;
763	}
764
765	error = VOP_GETATTR(vp, &vattr, curthread->td_ucred, curthread);
766	if (error) {
767		/* XXX: How to handle this case? */
768		return;
769	}
770
771	vnp->vn_mode = vattr.va_mode;
772	vnp->vn_uid = vattr.va_uid;
773	vnp->vn_gid = vattr.va_gid;
774	vnp->vn_dev = vattr.va_rdev;
775	vnp->vn_fsid = vattr.va_fsid;
776	vnp->vn_fileid = vattr.va_fileid;
777	vnp->vn_gen = vattr.va_gen;
778	if (flags & ARG_VNODE1)
779		ARG_SET_VALID(ar, ARG_VNODE1);
780	else
781		ARG_SET_VALID(ar, ARG_VNODE2);
782}
783
784/*
785 * Audit the argument strings passed to exec.
786 */
787void
788audit_arg_argv(char *argv, int argc, int length)
789{
790	struct kaudit_record *ar;
791
792	if (audit_argv == 0)
793		return;
794
795	ar = currecord();
796	if (ar == NULL)
797		return;
798
799	ar->k_ar.ar_arg_argv = malloc(length, M_AUDITTEXT, M_WAITOK);
800	bcopy(argv, ar->k_ar.ar_arg_argv, length);
801	ar->k_ar.ar_arg_argc = argc;
802	ARG_SET_VALID(ar, ARG_ARGV);
803}
804
805/*
806 * Audit the environment strings passed to exec.
807 */
808void
809audit_arg_envv(char *envv, int envc, int length)
810{
811	struct kaudit_record *ar;
812
813	if (audit_arge == 0)
814		return;
815
816	ar = currecord();
817	if (ar == NULL)
818		return;
819
820	ar->k_ar.ar_arg_envv = malloc(length, M_AUDITTEXT, M_WAITOK);
821	bcopy(envv, ar->k_ar.ar_arg_envv, length);
822	ar->k_ar.ar_arg_envc = envc;
823	ARG_SET_VALID(ar, ARG_ENVV);
824}
825
826/*
827 * The close() system call uses it's own audit call to capture the path/vnode
828 * information because those pieces are not easily obtained within the system
829 * call itself.
830 */
831void
832audit_sysclose(struct thread *td, int fd)
833{
834	struct kaudit_record *ar;
835	struct vnode *vp;
836	struct file *fp;
837	int vfslocked;
838
839	KASSERT(td != NULL, ("audit_sysclose: td == NULL"));
840
841	ar = currecord();
842	if (ar == NULL)
843		return;
844
845	audit_arg_fd(fd);
846
847	if (getvnode(td->td_proc->p_fd, fd, &fp) != 0)
848		return;
849
850	vp = fp->f_vnode;
851	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
852	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
853	audit_arg_vnode(vp, ARG_VNODE1);
854	VOP_UNLOCK(vp, 0);
855	VFS_UNLOCK_GIANT(vfslocked);
856	fdrop(fp, td);
857}
858