]> git.saurik.com Git - apple/xnu.git/blame - bsd/security/audit/audit_arg.c
xnu-3247.10.11.tar.gz
[apple/xnu.git] / bsd / security / audit / audit_arg.c
CommitLineData
b0d623f7 1/*-
39236c6e 2 * Copyright (c) 1999-2012 Apple Inc.
b0d623f7
A
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 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 */
30/*
31 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
32 * support for mandatory and extensible security protections. This notice
33 * is included in support of clause 2.2 (b) of the Apple Public License,
34 * Version 2.0.
35 */
36
37#include <sys/param.h>
38#include <sys/fcntl.h>
39#include <sys/kernel.h>
40#include <sys/lock.h>
41#include <sys/namei.h>
42#include <sys/proc_internal.h>
43#include <sys/kauth.h>
44#include <sys/queue.h>
45#include <sys/systm.h>
46#include <sys/time.h>
47#include <sys/ucred.h>
48#include <sys/uio.h>
49#include <sys/unistd.h>
50#include <sys/file_internal.h>
51#include <sys/vnode_internal.h>
52#include <sys/user.h>
53#include <sys/syscall.h>
54#include <sys/malloc.h>
55#include <sys/un.h>
56#include <sys/sysent.h>
57#include <sys/sysproto.h>
58#include <sys/vfs_context.h>
59#include <sys/domain.h>
60#include <sys/protosw.h>
61#include <sys/socketvar.h>
62
63#include <bsm/audit.h>
64#include <bsm/audit_internal.h>
65#include <bsm/audit_kevents.h>
66
67#include <security/audit/audit.h>
68#include <security/audit/audit_bsd.h>
69#include <security/audit/audit_private.h>
70
71#include <mach/host_priv.h>
72#include <mach/host_special_ports.h>
73#include <mach/audit_triggers_server.h>
74
75#include <kern/host.h>
76#include <kern/kalloc.h>
77#include <kern/zalloc.h>
b0d623f7
A
78#include <kern/sched_prim.h>
79
80#if CONFIG_MACF
81#include <bsm/audit_record.h>
82#include <security/mac.h>
83#include <security/mac_framework.h>
84#include <security/mac_policy.h>
85extern zone_t audit_mac_label_zone;
86#endif
87
88#include <net/route.h>
89
90#include <netinet/in.h>
91#include <netinet/in_pcb.h>
92
93#if CONFIG_AUDIT
94/*
95 * Calls to manipulate elements of the audit record structure from system
96 * call code. Macro wrappers will prevent this functions from being entered
97 * if auditing is disabled, avoiding the function call cost. We check the
98 * thread audit record pointer anyway, as the audit condition could change,
99 * and pre-selection may not have allocated an audit record for this event.
100 *
101 * XXXAUDIT: Should we assert, in each case, that this field of the record
102 * hasn't already been filled in?
103 */
104void
105audit_arg_addr(struct kaudit_record *ar, user_addr_t addr)
106{
107 struct proc *p = current_proc();
108
109 ar->k_ar.ar_arg_addr = addr;
110
111 /*
112 * If the process is 64-bit then flag the address as such.
113 */
114 if (proc_is64bit(p))
115 ARG_SET_VALID(ar, ARG_ADDR64);
116 else
117 ARG_SET_VALID(ar, ARG_ADDR32);
118}
119
120void
121audit_arg_exit(struct kaudit_record *ar, int status, int retval)
122{
123
124 ar->k_ar.ar_arg_exitstatus = status;
125 ar->k_ar.ar_arg_exitretval = retval;
126 ARG_SET_VALID(ar, ARG_EXIT);
127}
128
129void
130audit_arg_len(struct kaudit_record *ar, user_size_t len)
131{
132
133 ar->k_ar.ar_arg_len = len;
134 ARG_SET_VALID(ar, ARG_LEN);
135}
136
137void
138audit_arg_fd(struct kaudit_record *ar, int fd)
139{
140
141 ar->k_ar.ar_arg_fd = fd;
142 ARG_SET_VALID(ar, ARG_FD);
143}
144
145void
146audit_arg_fflags(struct kaudit_record *ar, int fflags)
147{
148
149 ar->k_ar.ar_arg_fflags = fflags;
150 ARG_SET_VALID(ar, ARG_FFLAGS);
151}
152
153void
154audit_arg_gid(struct kaudit_record *ar, gid_t gid)
155{
156
157 ar->k_ar.ar_arg_gid = gid;
158 ARG_SET_VALID(ar, ARG_GID);
159}
160
161void
162audit_arg_uid(struct kaudit_record *ar, uid_t uid)
163{
164
165 ar->k_ar.ar_arg_uid = uid;
166 ARG_SET_VALID(ar, ARG_UID);
167}
168
169void
170audit_arg_egid(struct kaudit_record *ar, gid_t egid)
171{
172
173 ar->k_ar.ar_arg_egid = egid;
174 ARG_SET_VALID(ar, ARG_EGID);
175}
176
177void
178audit_arg_euid(struct kaudit_record *ar, uid_t euid)
179{
180
181 ar->k_ar.ar_arg_euid = euid;
182 ARG_SET_VALID(ar, ARG_EUID);
183}
184
185void
186audit_arg_rgid(struct kaudit_record *ar, gid_t rgid)
187{
188
189 ar->k_ar.ar_arg_rgid = rgid;
190 ARG_SET_VALID(ar, ARG_RGID);
191}
192
193void
194audit_arg_ruid(struct kaudit_record *ar, uid_t ruid)
195{
196
197 ar->k_ar.ar_arg_ruid = ruid;
198 ARG_SET_VALID(ar, ARG_RUID);
199}
200
201void
202audit_arg_sgid(struct kaudit_record *ar, gid_t sgid)
203{
204
205 ar->k_ar.ar_arg_sgid = sgid;
206 ARG_SET_VALID(ar, ARG_SGID);
207}
208
209void
210audit_arg_suid(struct kaudit_record *ar, uid_t suid)
211{
212
213 ar->k_ar.ar_arg_suid = suid;
214 ARG_SET_VALID(ar, ARG_SUID);
215}
216
217void
218audit_arg_groupset(struct kaudit_record *ar, gid_t *gidset, u_int gidset_size)
219{
220 u_int i;
221
222 for (i = 0; i < gidset_size; i++)
223 ar->k_ar.ar_arg_groups.gidset[i] = gidset[i];
224 ar->k_ar.ar_arg_groups.gidset_size = gidset_size;
225 ARG_SET_VALID(ar, ARG_GROUPSET);
226}
227
228void
229audit_arg_login(struct kaudit_record *ar, char *login)
230{
231
232 strlcpy(ar->k_ar.ar_arg_login, login, MAXLOGNAME);
233 ARG_SET_VALID(ar, ARG_LOGIN);
234}
235
236void
237audit_arg_ctlname(struct kaudit_record *ar, int *name, int namelen)
238{
239
240 bcopy(name, &ar->k_ar.ar_arg_ctlname, namelen * sizeof(int));
241 ar->k_ar.ar_arg_len = namelen;
242 ARG_SET_VALID(ar, ARG_CTLNAME | ARG_LEN);
243}
244
245void
246audit_arg_mask(struct kaudit_record *ar, int mask)
247{
248
249 ar->k_ar.ar_arg_mask = mask;
250 ARG_SET_VALID(ar, ARG_MASK);
251}
252
253void
254audit_arg_mode(struct kaudit_record *ar, mode_t mode)
255{
256
257 ar->k_ar.ar_arg_mode = mode;
258 ARG_SET_VALID(ar, ARG_MODE);
259}
260
261void
262audit_arg_value32(struct kaudit_record *ar, uint32_t value32)
263{
264
265 ar->k_ar.ar_arg_value32 = value32;
266 ARG_SET_VALID(ar, ARG_VALUE32);
267}
268
269void
270audit_arg_value64(struct kaudit_record *ar, uint64_t value64)
271{
272
273 ar->k_ar.ar_arg_value64 = value64;
274 ARG_SET_VALID(ar, ARG_VALUE64);
275}
276
277void
278audit_arg_owner(struct kaudit_record *ar, uid_t uid, gid_t gid)
279{
280
281 ar->k_ar.ar_arg_uid = uid;
282 ar->k_ar.ar_arg_gid = gid;
283 ARG_SET_VALID(ar, ARG_UID | ARG_GID);
284}
285
286void
287audit_arg_pid(struct kaudit_record *ar, pid_t pid)
288{
289
290 ar->k_ar.ar_arg_pid = pid;
291 ARG_SET_VALID(ar, ARG_PID);
292}
293
294void
295audit_arg_process(struct kaudit_record *ar, proc_t p)
296{
297 kauth_cred_t my_cred;
298
299 KASSERT(p != NULL, ("audit_arg_process: p == NULL"));
300
301 if ( p == NULL)
302 return;
303
304 my_cred = kauth_cred_proc_ref(p);
305 ar->k_ar.ar_arg_auid = my_cred->cr_audit.as_aia_p->ai_auid;
306 ar->k_ar.ar_arg_asid = my_cred->cr_audit.as_aia_p->ai_asid;
307 bcopy(&my_cred->cr_audit.as_aia_p->ai_termid,
308 &ar->k_ar.ar_arg_termid_addr, sizeof(au_tid_addr_t));
6d2010ae
A
309 ar->k_ar.ar_arg_euid = kauth_cred_getuid(my_cred);
310 ar->k_ar.ar_arg_egid = kauth_cred_getgid(my_cred);
311 ar->k_ar.ar_arg_ruid = kauth_cred_getruid(my_cred);
312 ar->k_ar.ar_arg_rgid = kauth_cred_getrgid(my_cred);
b0d623f7
A
313 kauth_cred_unref(&my_cred);
314 ar->k_ar.ar_arg_pid = p->p_pid;
315 ARG_SET_VALID(ar, ARG_AUID | ARG_EUID | ARG_EGID | ARG_RUID |
316 ARG_RGID | ARG_ASID | ARG_TERMID_ADDR | ARG_PID | ARG_PROCESS);
317}
318
319void
320audit_arg_signum(struct kaudit_record *ar, u_int signum)
321{
322
323 ar->k_ar.ar_arg_signum = signum;
324 ARG_SET_VALID(ar, ARG_SIGNUM);
325}
326
327void
328audit_arg_socket(struct kaudit_record *ar, int sodomain, int sotype,
329 int soprotocol)
330{
331
332 ar->k_ar.ar_arg_sockinfo.sai_domain = sodomain;
333 ar->k_ar.ar_arg_sockinfo.sai_type = sotype;
334 ar->k_ar.ar_arg_sockinfo.sai_protocol = soprotocol;
335 ARG_SET_VALID(ar, ARG_SOCKINFO);
336}
337
338/*
339 * Note that the current working directory vp must be supplied at the audit
340 * call site to permit per thread current working directories, and that it
341 * must take a upath starting with '/' into account for chroot if the path
342 * is absolute. This results in the real (non-chroot) path being recorded
343 * in the audit record.
344 */
345void
346audit_arg_sockaddr(struct kaudit_record *ar, struct vnode *cwd_vp,
347 struct sockaddr *sa)
348{
349 int slen;
350 struct sockaddr_un *sun;
351 char path[SOCK_MAXADDRLEN - offsetof(struct sockaddr_un, sun_path) + 1];
352
353 KASSERT(sa != NULL, ("audit_arg_sockaddr: sa == NULL"));
354
355 if (cwd_vp == NULL || sa == NULL)
356 return;
357
358 bcopy(sa, &ar->k_ar.ar_arg_sockaddr, sa->sa_len);
359 switch (sa->sa_family) {
360 case AF_INET:
361 ARG_SET_VALID(ar, ARG_SADDRINET);
362 break;
363
364 case AF_INET6:
365 ARG_SET_VALID(ar, ARG_SADDRINET6);
366 break;
367
368 case AF_UNIX:
369 sun = (struct sockaddr_un *)sa;
370 slen = sun->sun_len - offsetof(struct sockaddr_un, sun_path);
371
372 if (slen >= 0) {
373 /*
374 * Make sure the path is NULL-terminated
375 */
376 if (sun->sun_path[slen] != 0) {
377 bcopy(sun->sun_path, path, slen);
378 path[slen] = 0;
379 audit_arg_upath(ar, cwd_vp, path, ARG_UPATH1);
380 } else {
381 audit_arg_upath(ar, cwd_vp, sun->sun_path,
382 ARG_UPATH1);
383 }
384 }
385 ARG_SET_VALID(ar, ARG_SADDRUNIX);
386 break;
387 /* XXXAUDIT: default:? */
388 }
389}
390
391void
392audit_arg_auid(struct kaudit_record *ar, uid_t auid)
393{
394
395 ar->k_ar.ar_arg_auid = auid;
396 ARG_SET_VALID(ar, ARG_AUID);
397}
398
399void
400audit_arg_auditinfo(struct kaudit_record *ar, struct auditinfo *au_info)
401{
402
403 ar->k_ar.ar_arg_auid = au_info->ai_auid;
404 ar->k_ar.ar_arg_asid = au_info->ai_asid;
405 ar->k_ar.ar_arg_amask.am_success = au_info->ai_mask.am_success;
406 ar->k_ar.ar_arg_amask.am_failure = au_info->ai_mask.am_failure;
407 ar->k_ar.ar_arg_termid.port = au_info->ai_termid.port;
408 ar->k_ar.ar_arg_termid.machine = au_info->ai_termid.machine;
409 ARG_SET_VALID(ar, ARG_AUID | ARG_ASID | ARG_AMASK | ARG_TERMID);
410}
411
412void
413audit_arg_auditinfo_addr(struct kaudit_record *ar,
414 struct auditinfo_addr *au_info)
415{
416
417 ar->k_ar.ar_arg_auid = au_info->ai_auid;
418 ar->k_ar.ar_arg_asid = au_info->ai_asid;
419 ar->k_ar.ar_arg_amask.am_success = au_info->ai_mask.am_success;
420 ar->k_ar.ar_arg_amask.am_failure = au_info->ai_mask.am_failure;
421 ar->k_ar.ar_arg_termid_addr.at_type = au_info->ai_termid.at_type;
422 ar->k_ar.ar_arg_termid_addr.at_port = au_info->ai_termid.at_port;
423 ar->k_ar.ar_arg_termid_addr.at_addr[0] = au_info->ai_termid.at_addr[0];
424 ar->k_ar.ar_arg_termid_addr.at_addr[1] = au_info->ai_termid.at_addr[1];
425 ar->k_ar.ar_arg_termid_addr.at_addr[2] = au_info->ai_termid.at_addr[2];
426 ar->k_ar.ar_arg_termid_addr.at_addr[3] = au_info->ai_termid.at_addr[3];
427 ARG_SET_VALID(ar, ARG_AUID | ARG_ASID | ARG_AMASK | ARG_TERMID_ADDR);
428}
429
430void
431audit_arg_text(struct kaudit_record *ar, char *text)
432{
433
434 KASSERT(text != NULL, ("audit_arg_text: text == NULL"));
435
436 /* Invalidate the text string */
437 ar->k_ar.ar_valid_arg &= (ARG_ALL ^ ARG_TEXT);
438 if (text == NULL)
439 return;
440
441 if (ar->k_ar.ar_arg_text == NULL)
442 ar->k_ar.ar_arg_text = malloc(MAXPATHLEN, M_AUDITTEXT,
443 M_WAITOK);
444
445 strncpy(ar->k_ar.ar_arg_text, text, MAXPATHLEN);
446 ARG_SET_VALID(ar, ARG_TEXT);
447}
448
449void
450audit_arg_opaque(struct kaudit_record *ar, void *data, size_t size)
451{
452
453 KASSERT(data != NULL, ("audit_arg_opaque: data == NULL"));
454 KASSERT(size <= UINT16_MAX, ("audit_arg_opaque: size > UINT16_MAX"));
455
456 if (data == NULL || size > UINT16_MAX)
457 return;
458
459 if (ar->k_ar.ar_arg_opaque == NULL)
460 ar->k_ar.ar_arg_opaque = malloc(size, M_AUDITDATA, M_WAITOK);
461 else
462 return;
463
464 memcpy(ar->k_ar.ar_arg_opaque, data, size);
465 ar->k_ar.ar_arg_opq_size = (u_int16_t) size;
466 ARG_SET_VALID(ar, ARG_OPAQUE);
467}
468
469void
470audit_arg_data(struct kaudit_record *ar, void *data, size_t size, size_t number)
471{
472 size_t sz;
473
474 KASSERT(data != NULL, ("audit_arg_data: data == NULL"));
475 KASSERT(size >= AUR_BYTE_SIZE && size <= AUR_INT64_SIZE,
476 ("audit_arg_data: size < AUR_BYTE_SIZE or size > AUR_INT64_SIZE"));
477 KASSERT(number <= UINT8_MAX,
478 ("audit_arg_data: number > UINT8_MAX"));
479
480 if (data == NULL || size < AUR_BYTE_SIZE || size > AUR_INT64_SIZE ||
481 number > UINT8_MAX)
482 return;
483
484 sz = size * number;
485
486 if (ar->k_ar.ar_arg_data == NULL)
487 ar->k_ar.ar_arg_data = malloc(sz, M_AUDITDATA, M_WAITOK);
488 else
489 return;
490
491 memcpy(ar->k_ar.ar_arg_data, data, sz);
492
493 switch(size) {
494 case AUR_BYTE_SIZE:
495 ar->k_ar.ar_arg_data_type = AUR_BYTE;
496 break;
497
498 case AUR_SHORT_SIZE:
499 ar->k_ar.ar_arg_data_type = AUR_SHORT;
500 break;
501
502 case AUR_INT32_SIZE:
503 ar->k_ar.ar_arg_data_type = AUR_INT32;
504 break;
505
506 case AUR_INT64_SIZE:
507 ar->k_ar.ar_arg_data_type = AUR_INT64;
508 break;
509
510 default:
511 free(ar->k_ar.ar_arg_data, M_AUDITDATA);
512 ar->k_ar.ar_arg_data = NULL;
513 return;
514 }
515
516 ar->k_ar.ar_arg_data_count = (u_char)number;
517
518 ARG_SET_VALID(ar, ARG_DATA);
519}
520
521void
522audit_arg_cmd(struct kaudit_record *ar, int cmd)
523{
524
525 ar->k_ar.ar_arg_cmd = cmd;
526 ARG_SET_VALID(ar, ARG_CMD);
527}
528
529void
530audit_arg_svipc_cmd(struct kaudit_record *ar, int cmd)
531{
532
533 ar->k_ar.ar_arg_svipc_cmd = cmd;
534 ARG_SET_VALID(ar, ARG_SVIPC_CMD);
535}
536
537void
538audit_arg_svipc_perm(struct kaudit_record *ar, struct ipc_perm *perm)
539{
540
541 bcopy(perm, &ar->k_ar.ar_arg_svipc_perm,
542 sizeof(ar->k_ar.ar_arg_svipc_perm));
543 ARG_SET_VALID(ar, ARG_SVIPC_PERM);
544}
545
546void
547audit_arg_svipc_id(struct kaudit_record *ar, int id)
548{
549
550 ar->k_ar.ar_arg_svipc_id = id;
551 ARG_SET_VALID(ar, ARG_SVIPC_ID);
552}
553
554void
555audit_arg_svipc_addr(struct kaudit_record *ar, user_addr_t addr)
556{
557
558 ar->k_ar.ar_arg_svipc_addr = addr;
559 ARG_SET_VALID(ar, ARG_SVIPC_ADDR);
560}
561
562void
563audit_arg_posix_ipc_perm(struct kaudit_record *ar, uid_t uid, gid_t gid,
564 mode_t mode)
565{
566
567 ar->k_ar.ar_arg_pipc_perm.pipc_uid = uid;
568 ar->k_ar.ar_arg_pipc_perm.pipc_gid = gid;
569 ar->k_ar.ar_arg_pipc_perm.pipc_mode = mode;
570 ARG_SET_VALID(ar, ARG_POSIX_IPC_PERM);
571}
572
573void
574audit_arg_auditon(struct kaudit_record *ar, union auditon_udata *udata)
575{
576
577 bcopy((void *)udata, &ar->k_ar.ar_arg_auditon,
578 sizeof(ar->k_ar.ar_arg_auditon));
579 ARG_SET_VALID(ar, ARG_AUDITON);
580}
581
582/*
583 * Audit information about a file, either the file's vnode info, or its
584 * socket address info.
585 */
586void
587audit_arg_file(struct kaudit_record *ar, __unused proc_t p,
588 struct fileproc *fp)
589{
590 struct socket *so;
591 struct inpcb *pcb;
592 struct sockaddr_in *sin;
593 struct sockaddr_in6 *sin6;
594
39236c6e 595 switch (FILEGLOB_DTYPE(fp->f_fglob)) {
b0d623f7
A
596 case DTYPE_VNODE:
597 /* case DTYPE_FIFO: */
598 audit_arg_vnpath_withref(ar,
599 (struct vnode *)fp->f_fglob->fg_data, ARG_VNODE1);
600 break;
601
602 case DTYPE_SOCKET:
603 so = (struct socket *)fp->f_fglob->fg_data;
39236c6e 604 if (SOCK_CHECK_DOM(so, PF_INET)) {
b0d623f7
A
605 if (so->so_pcb == NULL)
606 break;
607 ar->k_ar.ar_arg_sockinfo.sai_type =
608 so->so_type;
39236c6e
A
609 ar->k_ar.ar_arg_sockinfo.sai_domain = SOCK_DOM(so);
610 ar->k_ar.ar_arg_sockinfo.sai_protocol = SOCK_PROTO(so);
b0d623f7
A
611 pcb = (struct inpcb *)so->so_pcb;
612 sin = (struct sockaddr_in *)
613 &ar->k_ar.ar_arg_sockinfo.sai_faddr;
614 sin->sin_addr.s_addr = pcb->inp_faddr.s_addr;
615 sin->sin_port = pcb->inp_fport;
616 sin = (struct sockaddr_in *)
617 &ar->k_ar.ar_arg_sockinfo.sai_laddr;
618 sin->sin_addr.s_addr = pcb->inp_laddr.s_addr;
619 sin->sin_port = pcb->inp_lport;
620 ARG_SET_VALID(ar, ARG_SOCKINFO);
621 }
39236c6e 622 if (SOCK_CHECK_DOM(so, PF_INET6)) {
b0d623f7
A
623 if (so->so_pcb == NULL)
624 break;
625 ar->k_ar.ar_arg_sockinfo.sai_type =
626 so->so_type;
39236c6e
A
627 ar->k_ar.ar_arg_sockinfo.sai_domain = SOCK_DOM(so);
628 ar->k_ar.ar_arg_sockinfo.sai_protocol = SOCK_PROTO(so);
b0d623f7
A
629 pcb = (struct inpcb *)so->so_pcb;
630 sin6 = (struct sockaddr_in6 *)
631 &ar->k_ar.ar_arg_sockinfo.sai_faddr;
632 sin6->sin6_addr = pcb->in6p_faddr;
633 sin6->sin6_port = pcb->in6p_fport;
634 sin6 = (struct sockaddr_in6 *)
635 &ar->k_ar.ar_arg_sockinfo.sai_laddr;
636 sin6->sin6_addr = pcb->in6p_laddr;
637 sin6->sin6_port = pcb->in6p_lport;
638 ARG_SET_VALID(ar, ARG_SOCKINFO);
639 }
640 break;
641
642 default:
643 /* XXXAUDIT: else? */
644 break;
645 }
646}
647
648/*
649 * Store a path as given by the user process for auditing into the audit
650 * record stored on the user thread. This function will allocate the memory
651 * to store the path info if not already available. This memory will be
652 * freed when the audit record is freed.
653 *
654 * Note that the current working directory vp must be supplied at the audit call
655 * site to permit per thread current working directories, and that it must take
656 * a upath starting with '/' into account for chroot if the path is absolute.
657 * This results in the real (non-chroot) path being recorded in the audit
658 * record.
659 *
660 * XXXAUDIT: Possibly assert that the memory isn't already allocated?
661 */
662void
663audit_arg_upath(struct kaudit_record *ar, struct vnode *cwd_vp, char *upath, u_int64_t flag)
664{
665 char **pathp;
666
667 KASSERT(upath != NULL, ("audit_arg_upath: upath == NULL"));
668 KASSERT((flag == ARG_UPATH1) || (flag == ARG_UPATH2),
669 ("audit_arg_upath: flag %llu", (unsigned long long)flag));
670 KASSERT((flag != ARG_UPATH1) || (flag != ARG_UPATH2),
671 ("audit_arg_upath: flag %llu", (unsigned long long)flag));
672
673 if (flag == ARG_UPATH1)
674 pathp = &ar->k_ar.ar_arg_upath1;
675 else
676 pathp = &ar->k_ar.ar_arg_upath2;
677
678 if (*pathp == NULL)
679 *pathp = malloc(MAXPATHLEN, M_AUDITPATH, M_WAITOK);
680 else
681 return;
682
683 if (audit_canon_path(cwd_vp, upath, *pathp) == 0)
684 ARG_SET_VALID(ar, flag);
685 else {
686 free(*pathp, M_AUDITPATH);
687 *pathp = NULL;
688 }
689}
690
691/*
692 * Function to save the path and vnode attr information into the audit
693 * record.
694 *
695 * It is assumed that the caller will hold any vnode locks necessary to
696 * perform a VNOP_GETATTR() on the passed vnode.
697 *
698 * XXX: The attr code is very similar to vfs_vnops.c:vn_stat(), but always
699 * provides access to the generation number as we need that to construct the
700 * BSM file ID.
701 *
702 * XXX: We should accept the process argument from the caller, since it's
703 * very likely they already have a reference.
704 *
705 * XXX: Error handling in this function is poor.
706 *
707 * XXXAUDIT: Possibly KASSERT the path pointer is NULL?
708 */
709void
710audit_arg_vnpath(struct kaudit_record *ar, struct vnode *vp, u_int64_t flags)
711{
712 struct vnode_attr va;
713 int error;
714 int len;
715 char **pathp;
716 struct vnode_au_info *vnp;
717 proc_t p;
718#if CONFIG_MACF
719 char **vnode_mac_labelp;
720 struct mac mac;
721#endif
722
723 KASSERT(vp != NULL, ("audit_arg_vnpath: vp == NULL"));
724 KASSERT((flags == ARG_VNODE1) || (flags == ARG_VNODE2),
725 ("audit_arg_vnpath: flags != ARG_VNODE[1,2]"));
726
727 p = current_proc();
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_KPATH1);
736 ar->k_ar.ar_valid_arg &= (ARG_ALL ^ ARG_VNODE1);
737 pathp = &ar->k_ar.ar_arg_kpath1;
738 vnp = &ar->k_ar.ar_arg_vnode1;
739#if CONFIG_MACF
740 vnode_mac_labelp = &ar->k_ar.ar_vnode1_mac_labels;
741#endif
742 } else {
743 ar->k_ar.ar_valid_arg &= (ARG_ALL ^ ARG_KPATH2);
744 ar->k_ar.ar_valid_arg &= (ARG_ALL ^ ARG_VNODE2);
745 pathp = &ar->k_ar.ar_arg_kpath2;
746 vnp = &ar->k_ar.ar_arg_vnode2;
747#if CONFIG_MACF
748 vnode_mac_labelp = &ar->k_ar.ar_vnode2_mac_labels;
749#endif
750 }
751
752 if (*pathp == NULL)
753 *pathp = malloc(MAXPATHLEN, M_AUDITPATH, M_WAITOK);
754 else
755 return;
756
757 /*
758 * If vn_getpath() succeeds, place it in a string buffer
759 * attached to the audit record, and set a flag indicating
760 * it is present.
761 */
762 len = MAXPATHLEN;
763 if (vn_getpath(vp, *pathp, &len) == 0) {
764 if (flags & ARG_VNODE1)
765 ARG_SET_VALID(ar, ARG_KPATH1);
766 else
767 ARG_SET_VALID(ar, ARG_KPATH2);
768 } else {
769 free(*pathp, M_AUDITPATH);
770 *pathp = NULL;
771 }
772
773 VATTR_INIT(&va);
774 VATTR_WANTED(&va, va_mode);
775 VATTR_WANTED(&va, va_uid);
776 VATTR_WANTED(&va, va_gid);
777 VATTR_WANTED(&va, va_rdev);
778 VATTR_WANTED(&va, va_fsid);
779 VATTR_WANTED(&va, va_fileid);
780 VATTR_WANTED(&va, va_gen);
781 error = vnode_getattr(vp, &va, vfs_context_current());
782 if (error) {
783 /* XXX: How to handle this case? */
784 return;
785 }
786
787#if CONFIG_MACF
788 if (*vnode_mac_labelp == NULL && (vp->v_lflag & VL_LABELED) == VL_LABELED) {
789 *vnode_mac_labelp = (char *)zalloc(audit_mac_label_zone);
790 if (*vnode_mac_labelp != NULL) {
791 mac.m_buflen = MAC_AUDIT_LABEL_LEN;
792 mac.m_string = *vnode_mac_labelp;
793 mac_vnode_label_externalize_audit(vp, &mac);
794 }
795 }
796#endif
797
798 /*
799 * XXX do we want to fall back here when these aren't supported?
800 */
801 vnp->vn_mode = va.va_mode;
802 vnp->vn_uid = va.va_uid;
803 vnp->vn_gid = va.va_gid;
804 vnp->vn_dev = va.va_rdev;
805 vnp->vn_fsid = va.va_fsid;
806 vnp->vn_fileid = (u_int32_t)va.va_fileid;
807 vnp->vn_gen = va.va_gen;
808 if (flags & ARG_VNODE1)
809 ARG_SET_VALID(ar, ARG_VNODE1);
810 else
811 ARG_SET_VALID(ar, ARG_VNODE2);
812}
813
814void
815audit_arg_vnpath_withref(struct kaudit_record *ar, struct vnode *vp, u_int64_t flags)
816{
817 if (vp == NULL || vnode_getwithref(vp))
818 return;
819 audit_arg_vnpath(ar, vp, flags);
820 (void)vnode_put(vp);
821}
822
823void
824audit_arg_mach_port1(struct kaudit_record *ar, mach_port_name_t port)
825{
826
827 ar->k_ar.ar_arg_mach_port1 = port;
828 ARG_SET_VALID(ar, ARG_MACHPORT1);
829}
830
831void
832audit_arg_mach_port2(struct kaudit_record *ar, mach_port_name_t port)
833{
834
835 ar->k_ar.ar_arg_mach_port2 = port;
836 ARG_SET_VALID(ar, ARG_MACHPORT2);
837}
838
839
840/*
841 * Audit the argument strings passed to exec.
842 */
843void
844audit_arg_argv(struct kaudit_record *ar, char *argv, int argc, int length)
845{
846
847 if (audit_argv == 0 || argc == 0)
848 return;
849
850 if (ar->k_ar.ar_arg_argv == NULL)
851 ar->k_ar.ar_arg_argv = malloc(length, M_AUDITTEXT, M_WAITOK);
852 bcopy(argv, ar->k_ar.ar_arg_argv, length);
853 ar->k_ar.ar_arg_argc = argc;
854 ARG_SET_VALID(ar, ARG_ARGV);
855}
856
857/*
858 * Audit the environment strings passed to exec.
859 */
860void
861audit_arg_envv(struct kaudit_record *ar, char *envv, int envc, int length)
862{
863
864 if (audit_arge == 0 || envc == 0)
865 return;
866
867 if (ar->k_ar.ar_arg_envv == NULL)
868 ar->k_ar.ar_arg_envv = malloc(length, M_AUDITTEXT, M_WAITOK);
869 bcopy(envv, ar->k_ar.ar_arg_envv, length);
870 ar->k_ar.ar_arg_envc = envc;
871 ARG_SET_VALID(ar, ARG_ENVV);
872}
873
874/*
875 * The close() system call uses it's own audit call to capture the path/vnode
876 * information because those pieces are not easily obtained within the system
877 * call itself.
878 */
879void
880audit_sysclose(struct kaudit_record *ar, proc_t p, int fd)
881{
882 struct fileproc *fp;
883 struct vnode *vp;
884
885 KASSERT(p != NULL, ("audit_sysclose: p == NULL"));
886
887 audit_arg_fd(ar, fd);
888
889 if (fp_getfvp(p, fd, &fp, &vp) != 0)
890 return;
891
892 audit_arg_vnpath_withref(ar, (struct vnode *)fp->f_fglob->fg_data,
893 ARG_VNODE1);
894 fp_drop(p, fd, fp, 0);
895}
896
897#endif /* CONFIG_AUDIT */