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