2 * Copyright (c) 2004-2009 Apple Inc.
3 * Copyright (c) 2005 SPARTA, Inc.
6 * This code was developed in part by Robert N. M. Watson, Senior Principal
7 * Scientist, SPARTA, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
18 * its contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
25 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
33 * P4: //depot/projects/trustedbsd/openbsm/libbsm/bsm_token.c#93
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/types.h>
40 #include <sys/endian.h>
41 #include <sys/queue.h>
42 #include <sys/socket.h>
46 #include <sys/libkern.h>
47 #include <sys/malloc.h>
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
55 #include <bsm/audit.h>
56 #include <bsm/audit_internal.h>
57 #include <bsm/audit_record.h>
58 #include <security/audit/audit.h>
59 #include <security/audit/audit_private.h>
61 #define GET_TOKEN_AREA(t, dptr, length) do { \
62 t = malloc(sizeof(token_t), M_AUDITBSM, M_WAITOK); \
63 t->t_data = malloc(length, M_AUDITBSM, M_WAITOK | M_ZERO); \
71 * argument value 4 bytes/8 bytes (32-bit/64-bit value)
73 * text N bytes + 1 terminating NULL byte
76 au_to_arg32(char n, const char *text, u_int32_t v)
82 textlen = strlen(text);
85 GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int32_t) +
86 sizeof(u_int16_t) + textlen);
88 ADD_U_CHAR(dptr, AUT_ARG32);
91 ADD_U_INT16(dptr, textlen);
92 ADD_STRING(dptr, text, textlen);
98 au_to_arg64(char n, const char *text, u_int64_t v)
104 textlen = strlen(text);
107 GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int64_t) +
108 sizeof(u_int16_t) + textlen);
110 ADD_U_CHAR(dptr, AUT_ARG64);
112 ADD_U_INT64(dptr, v);
113 ADD_U_INT16(dptr, textlen);
114 ADD_STRING(dptr, text, textlen);
120 au_to_arg(char n, const char *text, u_int32_t v)
123 return (au_to_arg32(n, text, v));
126 #if defined(_KERNEL) || defined(KERNEL)
129 * file access mode 4 bytes
130 * owner user ID 4 bytes
131 * owner group ID 4 bytes
132 * file system ID 4 bytes
134 * device 4 bytes/8 bytes (32-bit/64-bit)
137 au_to_attr32(struct vnode_au_info *vni)
141 u_int16_t pad0_16 = 0;
142 u_int32_t pad0_32 = 0;
144 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(u_int16_t) +
145 3 * sizeof(u_int32_t) + sizeof(u_int64_t) + sizeof(u_int32_t));
147 ADD_U_CHAR(dptr, AUT_ATTR32);
150 * BSD defines the size for the file mode as 2 bytes; BSM defines 4
153 * XXXRW: Possibly should be conditionally compiled.
155 * XXXRW: Should any conversions take place on the mode?
157 ADD_U_INT16(dptr, pad0_16);
158 ADD_U_INT16(dptr, vni->vn_mode);
160 ADD_U_INT32(dptr, vni->vn_uid);
161 ADD_U_INT32(dptr, vni->vn_gid);
162 ADD_U_INT32(dptr, vni->vn_fsid);
165 * Some systems use 32-bit file ID's, others use 64-bit file IDs.
166 * Attempt to handle both, and let the compiler sort it out. If we
167 * could pick this out at compile-time, it would be better, so as to
168 * avoid the else case below.
170 if (sizeof(vni->vn_fileid) == sizeof(uint32_t)) {
171 ADD_U_INT32(dptr, pad0_32);
172 ADD_U_INT32(dptr, vni->vn_fileid);
173 } else if (sizeof(vni->vn_fileid) == sizeof(uint64_t))
174 ADD_U_INT64(dptr, vni->vn_fileid);
176 ADD_U_INT64(dptr, 0LL);
178 ADD_U_INT32(dptr, vni->vn_dev);
184 au_to_attr64(struct vnode_au_info *vni)
188 u_int16_t pad0_16 = 0;
189 u_int32_t pad0_32 = 0;
191 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(u_int16_t) +
192 3 * sizeof(u_int32_t) + sizeof(u_int64_t) * 2);
194 ADD_U_CHAR(dptr, AUT_ATTR64);
197 * BSD defines the size for the file mode as 2 bytes; BSM defines 4
200 * XXXRW: Possibly should be conditionally compiled.
202 * XXXRW: Should any conversions take place on the mode?
204 ADD_U_INT16(dptr, pad0_16);
205 ADD_U_INT16(dptr, vni->vn_mode);
207 ADD_U_INT32(dptr, vni->vn_uid);
208 ADD_U_INT32(dptr, vni->vn_gid);
209 ADD_U_INT32(dptr, vni->vn_fsid);
212 * Some systems use 32-bit file ID's, other's use 64-bit file IDs.
213 * Attempt to handle both, and let the compiler sort it out. If we
214 * could pick this out at compile-time, it would be better, so as to
215 * avoid the else case below.
217 if (sizeof(vni->vn_fileid) == sizeof(uint32_t)) {
218 ADD_U_INT32(dptr, pad0_32);
219 ADD_U_INT32(dptr, vni->vn_fileid);
220 } else if (sizeof(vni->vn_fileid) == sizeof(uint64_t))
221 ADD_U_INT64(dptr, vni->vn_fileid);
223 ADD_U_INT64(dptr, 0LL);
225 ADD_U_INT64(dptr, vni->vn_dev);
231 au_to_attr(struct vnode_au_info *vni)
234 return (au_to_attr32(vni));
236 #endif /* !(defined(_KERNEL) || defined(KERNEL) */
240 * how to print 1 byte
243 * data items (depends on basic unit)
246 au_to_data(char unit_print, char unit_type, char unit_count, const char *p)
250 size_t datasize, totdata;
252 /* Determine the size of the basic unit. */
256 datasize = AUR_BYTE_SIZE;
260 datasize = AUR_SHORT_SIZE;
265 datasize = AUR_INT32_SIZE;
269 datasize = AUR_INT64_SIZE;
276 totdata = datasize * unit_count;
278 GET_TOKEN_AREA(t, dptr, 4 * sizeof(u_char) + totdata);
281 * XXXRW: We should be byte-swapping each data item for multi-byte
284 ADD_U_CHAR(dptr, AUT_DATA);
285 ADD_U_CHAR(dptr, unit_print);
286 ADD_U_CHAR(dptr, unit_type);
287 ADD_U_CHAR(dptr, unit_count);
288 ADD_MEM(dptr, p, totdata);
297 * return value 4 bytes
300 au_to_exit(int retval, int err)
305 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(u_int32_t));
307 ADD_U_CHAR(dptr, AUT_EXIT);
308 ADD_U_INT32(dptr, err);
309 ADD_U_INT32(dptr, retval);
317 au_to_groups(int *groups)
320 return (au_to_newgroups(AUDIT_MAX_GROUPS, (gid_t *)groups));
325 * number groups 2 bytes
326 * group list count * 4 bytes
329 au_to_newgroups(u_int16_t n, gid_t *groups)
335 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) +
336 n * sizeof(u_int32_t));
338 ADD_U_CHAR(dptr, AUT_NEWGROUPS);
339 ADD_U_INT16(dptr, n);
340 for (i = 0; i < n; i++)
341 ADD_U_INT32(dptr, groups[i]);
348 * internet address 4 bytes
351 au_to_in_addr(struct in_addr *internet_addr)
356 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(uint32_t));
358 ADD_U_CHAR(dptr, AUT_IN_ADDR);
359 ADD_MEM(dptr, &internet_addr->s_addr, sizeof(uint32_t));
366 * address type/length 4 bytes
370 au_to_in_addr_ex(struct in6_addr *internet_addr)
374 u_int32_t type = AU_IPv6;
376 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 5 * sizeof(uint32_t));
378 ADD_U_CHAR(dptr, AUT_IN_ADDR_EX);
379 ADD_U_INT32(dptr, type);
380 ADD_MEM(dptr, internet_addr, 4 * sizeof(uint32_t));
389 * The IP header should be submitted in network byte order.
392 au_to_ip(struct ip *ip)
397 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(struct ip));
399 ADD_U_CHAR(dptr, AUT_IP);
400 ADD_MEM(dptr, ip, sizeof(struct ip));
407 * object ID type 1 byte
411 au_to_ipc(char type, int id)
416 GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int32_t));
418 ADD_U_CHAR(dptr, AUT_IPC);
419 ADD_U_CHAR(dptr, type);
420 ADD_U_INT32(dptr, id);
427 * owner user ID 4 bytes
428 * owner group ID 4 bytes
429 * creator user ID 4 bytes
430 * creator group ID 4 bytes
431 * access mode 4 bytes
432 * slot sequence # 4 bytes
436 au_to_ipc_perm(struct ipc_perm *perm)
442 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 12 * sizeof(u_int16_t) +
445 ADD_U_CHAR(dptr, AUT_IPC_PERM);
448 * Systems vary significantly in what types they use in struct
449 * ipc_perm; at least a few still use 16-bit uid's and gid's, so
450 * allow for that, as BSM define 32-bit values here.
451 * Some systems define the sizes for ipc_perm members as 2 bytes;
452 * BSM defines 4 so pad with 0.
454 * XXXRW: Possibly shoulid be conditionally compiled, and more cases
455 * need to be handled.
457 if (sizeof(perm->uid) != sizeof(u_int32_t)) {
458 ADD_U_INT16(dptr, pad0);
459 ADD_U_INT16(dptr, perm->uid);
460 ADD_U_INT16(dptr, pad0);
461 ADD_U_INT16(dptr, perm->gid);
462 ADD_U_INT16(dptr, pad0);
463 ADD_U_INT16(dptr, perm->cuid);
464 ADD_U_INT16(dptr, pad0);
465 ADD_U_INT16(dptr, perm->cgid);
467 ADD_U_INT32(dptr, perm->uid);
468 ADD_U_INT32(dptr, perm->gid);
469 ADD_U_INT32(dptr, perm->cuid);
470 ADD_U_INT32(dptr, perm->cgid);
473 ADD_U_INT16(dptr, pad0);
474 ADD_U_INT16(dptr, perm->mode);
476 ADD_U_INT16(dptr, pad0);
478 ADD_U_INT16(dptr, perm->seq);
480 ADD_U_INT32(dptr, perm->key);
487 * port IP address 2 bytes
490 au_to_iport(u_int16_t iport)
495 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t));
497 ADD_U_CHAR(dptr, AUT_IPORT);
498 ADD_U_INT16(dptr, iport);
509 au_to_opaque(const char *data, u_int16_t bytes)
514 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) + bytes);
516 ADD_U_CHAR(dptr, AUT_OPAQUE);
517 ADD_U_INT16(dptr, bytes);
518 ADD_MEM(dptr, data, bytes);
525 * seconds of time 4 bytes
526 * milliseconds of time 4 bytes
527 * file name len 2 bytes
528 * file pathname N bytes + 1 terminating NULL byte
531 au_to_file(const char *file, struct timeval tm)
538 filelen = strlen(file);
541 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(u_int32_t) +
542 sizeof(u_int16_t) + filelen);
544 timems = tm.tv_usec/1000;
546 ADD_U_CHAR(dptr, AUT_OTHER_FILE32);
547 ADD_U_INT32(dptr, tm.tv_sec);
548 ADD_U_INT32(dptr, timems); /* We need time in ms. */
549 ADD_U_INT16(dptr, filelen);
550 ADD_STRING(dptr, file, filelen);
557 * text length 2 bytes
558 * text N bytes + 1 terminating NULL byte
561 au_to_text(const char *text)
567 textlen = strlen(text);
570 /* XXXRW: Should validate length against token size limit. */
572 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) + textlen);
574 ADD_U_CHAR(dptr, AUT_TEXT);
575 ADD_U_INT16(dptr, textlen);
576 ADD_STRING(dptr, text, textlen);
583 * path length 2 bytes
584 * path N bytes + 1 terminating NULL byte
587 au_to_path(const char *text)
593 textlen = strlen(text);
596 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) + textlen);
598 ADD_U_CHAR(dptr, AUT_PATH);
599 ADD_U_INT16(dptr, textlen);
600 ADD_STRING(dptr, text, textlen);
608 * effective user ID 4 bytes
609 * effective group ID 4 bytes
610 * real user ID 4 bytes
611 * real group ID 4 bytes
615 * port ID 4 bytes/8 bytes (32-bit/64-bit value)
616 * machine address 4 bytes
619 au_to_process32(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
620 pid_t pid, au_asid_t sid, au_tid_t *tid)
625 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 9 * sizeof(u_int32_t));
627 ADD_U_CHAR(dptr, AUT_PROCESS32);
628 ADD_U_INT32(dptr, auid);
629 ADD_U_INT32(dptr, euid);
630 ADD_U_INT32(dptr, egid);
631 ADD_U_INT32(dptr, ruid);
632 ADD_U_INT32(dptr, rgid);
633 ADD_U_INT32(dptr, pid);
634 ADD_U_INT32(dptr, sid);
635 ADD_U_INT32(dptr, tid->port);
638 * Note: Solaris will write out IPv6 addresses here as a 32-bit
639 * address type and 16 bytes of address, but for IPv4 addresses it
640 * simply writes the 4-byte address directly. We support only IPv4
641 * addresses for process32 tokens.
643 ADD_MEM(dptr, &tid->machine, sizeof(u_int32_t));
649 au_to_process64(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
650 pid_t pid, au_asid_t sid, au_tid_t *tid)
655 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 8 * sizeof(u_int32_t) +
658 ADD_U_CHAR(dptr, AUT_PROCESS64);
659 ADD_U_INT32(dptr, auid);
660 ADD_U_INT32(dptr, euid);
661 ADD_U_INT32(dptr, egid);
662 ADD_U_INT32(dptr, ruid);
663 ADD_U_INT32(dptr, rgid);
664 ADD_U_INT32(dptr, pid);
665 ADD_U_INT32(dptr, sid);
666 ADD_U_INT64(dptr, tid->port);
669 * Note: Solaris will write out IPv6 addresses here as a 32-bit
670 * address type and 16 bytes of address, but for IPv4 addresses it
671 * simply writes the 4-byte address directly. We support only IPv4
672 * addresses for process64 tokens.
674 ADD_MEM(dptr, &tid->machine, sizeof(u_int32_t));
680 au_to_process(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
681 pid_t pid, au_asid_t sid, au_tid_t *tid)
684 return (au_to_process32(auid, euid, egid, ruid, rgid, pid, sid,
691 * effective user ID 4 bytes
692 * effective group ID 4 bytes
693 * real user ID 4 bytes
694 * real group ID 4 bytes
698 * port ID 4 bytes/8 bytes (32-bit/64-bit value)
699 * address type-len 4 bytes
700 * machine address 16 bytes
703 au_to_process32_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
704 gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
709 KASSERT((tid->at_type == AU_IPv4) || (tid->at_type == AU_IPv6),
710 ("au_to_process32_ex: type %u", (unsigned int)tid->at_type));
711 if (tid->at_type == AU_IPv4)
712 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
713 10 * sizeof(u_int32_t));
715 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
716 13 * sizeof(u_int32_t));
718 ADD_U_CHAR(dptr, AUT_PROCESS32_EX);
719 ADD_U_INT32(dptr, auid);
720 ADD_U_INT32(dptr, euid);
721 ADD_U_INT32(dptr, egid);
722 ADD_U_INT32(dptr, ruid);
723 ADD_U_INT32(dptr, rgid);
724 ADD_U_INT32(dptr, pid);
725 ADD_U_INT32(dptr, sid);
726 ADD_U_INT32(dptr, tid->at_port);
727 ADD_U_INT32(dptr, tid->at_type);
728 ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
729 if (tid->at_type == AU_IPv6) {
730 ADD_MEM(dptr, &tid->at_addr[1], sizeof(u_int32_t));
731 ADD_MEM(dptr, &tid->at_addr[2], sizeof(u_int32_t));
732 ADD_MEM(dptr, &tid->at_addr[3], sizeof(u_int32_t));
739 au_to_process64_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
740 gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
745 if (tid->at_type == AU_IPv4)
746 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
747 7 * sizeof(u_int32_t) + sizeof(u_int64_t) +
748 2 * sizeof(u_int32_t));
749 else if (tid->at_type == AU_IPv6)
750 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
751 7 * sizeof(u_int32_t) + sizeof(u_int64_t) +
752 5 * sizeof(u_int32_t));
754 panic("au_to_process64_ex: invalidate at_type (%d)",
757 ADD_U_CHAR(dptr, AUT_PROCESS64_EX);
758 ADD_U_INT32(dptr, auid);
759 ADD_U_INT32(dptr, euid);
760 ADD_U_INT32(dptr, egid);
761 ADD_U_INT32(dptr, ruid);
762 ADD_U_INT32(dptr, rgid);
763 ADD_U_INT32(dptr, pid);
764 ADD_U_INT32(dptr, sid);
765 ADD_U_INT64(dptr, tid->at_port);
766 ADD_U_INT32(dptr, tid->at_type);
767 ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
768 if (tid->at_type == AU_IPv6) {
769 ADD_MEM(dptr, &tid->at_addr[1], sizeof(u_int32_t));
770 ADD_MEM(dptr, &tid->at_addr[2], sizeof(u_int32_t));
771 ADD_MEM(dptr, &tid->at_addr[3], sizeof(u_int32_t));
778 au_to_process_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
779 gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
782 return (au_to_process32_ex(auid, euid, egid, ruid, rgid, pid, sid,
788 * error status 1 byte
789 * return value 4 bytes/8 bytes (32-bit/64-bit value)
792 au_to_return32(char status, u_int32_t ret)
797 GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int32_t));
799 ADD_U_CHAR(dptr, AUT_RETURN32);
800 ADD_U_CHAR(dptr, status);
801 ADD_U_INT32(dptr, ret);
807 au_to_return64(char status, u_int64_t ret)
812 GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int64_t));
814 ADD_U_CHAR(dptr, AUT_RETURN64);
815 ADD_U_CHAR(dptr, status);
816 ADD_U_INT64(dptr, ret);
822 au_to_return(char status, u_int32_t ret)
825 return (au_to_return32(status, ret));
830 * sequence number 4 bytes
833 au_to_seq(long audit_count)
838 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t));
840 ADD_U_CHAR(dptr, AUT_SEQ);
841 ADD_U_INT32(dptr, audit_count);
848 * socket domain 2 bytes
849 * socket type 2 bytes
850 * address type 2 byte
852 * local address 4 bytes/16 bytes (IPv4/IPv6 address)
853 * remote port 2 bytes
854 * remote address 4 bytes/16 bytes (IPv4/IPv6 address)
856 * Domain and type arguments to this routine are assumed to already have been
857 * converted to the BSM constant space, so we don't do that here.
860 au_to_socket_ex(u_short so_domain, u_short so_type,
861 struct sockaddr *sa_local, struct sockaddr *sa_remote)
865 struct sockaddr_in *sin;
866 struct sockaddr_in6 *sin6;
868 if (so_domain == AF_INET)
869 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
870 5 * sizeof(u_int16_t) + 2 * sizeof(u_int32_t));
871 else if (so_domain == AF_INET6)
872 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
873 5 * sizeof(u_int16_t) + 8 * sizeof(u_int32_t));
877 ADD_U_CHAR(dptr, AUT_SOCKET_EX);
878 ADD_U_INT16(dptr, au_domain_to_bsm(so_domain));
879 ADD_U_INT16(dptr, au_socket_type_to_bsm(so_type));
880 if (so_domain == AF_INET) {
881 ADD_U_INT16(dptr, AU_IPv4);
882 sin = (struct sockaddr_in *)sa_local;
883 ADD_MEM(dptr, &sin->sin_port, sizeof(uint16_t));
884 ADD_MEM(dptr, &sin->sin_addr.s_addr, sizeof(uint32_t));
885 sin = (struct sockaddr_in *)sa_remote;
886 ADD_MEM(dptr, &sin->sin_port, sizeof(uint16_t));
887 ADD_MEM(dptr, &sin->sin_addr.s_addr, sizeof(uint32_t));
889 ADD_U_INT16(dptr, AU_IPv6);
890 sin6 = (struct sockaddr_in6 *)sa_local;
891 ADD_MEM(dptr, &sin6->sin6_port, sizeof(uint16_t));
892 ADD_MEM(dptr, &sin6->sin6_addr, 4 * sizeof(uint32_t));
893 sin6 = (struct sockaddr_in6 *)sa_remote;
894 ADD_MEM(dptr, &sin6->sin6_port, sizeof(uint16_t));
895 ADD_MEM(dptr, &sin6->sin6_addr, 4 * sizeof(uint32_t));
902 * Kernel-specific version of the above function.
904 * XXXRW: Should now use au_to_socket_ex() here.
908 kau_to_socket(struct socket_au_info *soi)
914 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(u_int16_t) +
915 sizeof(u_int32_t) + sizeof(u_int16_t) + sizeof(u_int32_t));
917 ADD_U_CHAR(dptr, AUT_SOCKET);
918 /* Coerce the socket type into a short value */
919 so_type = soi->so_type;
920 ADD_U_INT16(dptr, so_type);
921 ADD_U_INT16(dptr, soi->so_lport);
922 ADD_U_INT32(dptr, soi->so_laddr);
923 ADD_U_INT16(dptr, soi->so_rport);
924 ADD_U_INT32(dptr, soi->so_raddr);
932 * socket family 2 bytes
933 * path (up to) 104 bytes + NULL (NULL terminated string)
936 au_to_sock_unix(struct sockaddr_un *so)
941 GET_TOKEN_AREA(t, dptr, 3 * sizeof(u_char) + strlen(so->sun_path) + 1);
943 ADD_U_CHAR(dptr, AUT_SOCKUNIX);
944 /* BSM token has two bytes for family */
946 ADD_U_CHAR(dptr, so->sun_family);
947 ADD_STRING(dptr, so->sun_path, strlen(so->sun_path) + 1);
954 * socket family 2 bytes
956 * socket address 4 bytes
959 au_to_sock_inet32(struct sockaddr_in *so)
965 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(uint16_t) +
968 ADD_U_CHAR(dptr, AUT_SOCKINET32);
970 * BSM defines the family field as 16 bits, but many operating
971 * systems have an 8-bit sin_family field. Extend to 16 bits before
972 * writing into the token. Assume that both the port and the address
973 * in the sockaddr_in are already in network byte order, but family
974 * is in local byte order.
976 * XXXRW: Should a name space conversion be taking place on the value
979 family = so->sin_family;
980 ADD_U_INT16(dptr, family);
981 ADD_MEM(dptr, &so->sin_port, sizeof(uint16_t));
982 ADD_MEM(dptr, &so->sin_addr.s_addr, sizeof(uint32_t));
988 au_to_sock_inet128(struct sockaddr_in6 *so)
993 GET_TOKEN_AREA(t, dptr, 3 * sizeof(u_char) + sizeof(u_int16_t) +
994 4 * sizeof(u_int32_t));
996 ADD_U_CHAR(dptr, AUT_SOCKINET128);
998 * In BSD, sin6_family is one octet, but BSM defines the token to
999 * store two. So we copy in a 0 first. XXXRW: Possibly should be
1000 * conditionally compiled.
1002 ADD_U_CHAR(dptr, 0);
1003 ADD_U_CHAR(dptr, so->sin6_family);
1005 ADD_U_INT16(dptr, so->sin6_port);
1006 ADD_MEM(dptr, &so->sin6_addr, 4 * sizeof(uint32_t));
1012 au_to_sock_inet(struct sockaddr_in *so)
1015 return (au_to_sock_inet32(so));
1021 * effective user ID 4 bytes
1022 * effective group ID 4 bytes
1023 * real user ID 4 bytes
1024 * real group ID 4 bytes
1025 * process ID 4 bytes
1026 * session ID 4 bytes
1028 * port ID 4 bytes/8 bytes (32-bit/64-bit value)
1029 * machine address 4 bytes
1032 au_to_subject32(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
1033 pid_t pid, au_asid_t sid, au_tid_t *tid)
1036 u_char *dptr = NULL;
1038 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 9 * sizeof(u_int32_t));
1040 ADD_U_CHAR(dptr, AUT_SUBJECT32);
1041 ADD_U_INT32(dptr, auid);
1042 ADD_U_INT32(dptr, euid);
1043 ADD_U_INT32(dptr, egid);
1044 ADD_U_INT32(dptr, ruid);
1045 ADD_U_INT32(dptr, rgid);
1046 ADD_U_INT32(dptr, pid);
1047 ADD_U_INT32(dptr, sid);
1048 ADD_U_INT32(dptr, tid->port);
1049 ADD_MEM(dptr, &tid->machine, sizeof(u_int32_t));
1055 au_to_subject64(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
1056 pid_t pid, au_asid_t sid, au_tid_t *tid)
1059 u_char *dptr = NULL;
1061 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 7 * sizeof(u_int32_t) +
1062 sizeof(u_int64_t) + sizeof(u_int32_t));
1064 ADD_U_CHAR(dptr, AUT_SUBJECT64);
1065 ADD_U_INT32(dptr, auid);
1066 ADD_U_INT32(dptr, euid);
1067 ADD_U_INT32(dptr, egid);
1068 ADD_U_INT32(dptr, ruid);
1069 ADD_U_INT32(dptr, rgid);
1070 ADD_U_INT32(dptr, pid);
1071 ADD_U_INT32(dptr, sid);
1072 ADD_U_INT64(dptr, tid->port);
1073 ADD_MEM(dptr, &tid->machine, sizeof(u_int32_t));
1079 au_to_subject(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
1080 pid_t pid, au_asid_t sid, au_tid_t *tid)
1083 return (au_to_subject32(auid, euid, egid, ruid, rgid, pid, sid,
1090 * effective user ID 4 bytes
1091 * effective group ID 4 bytes
1092 * real user ID 4 bytes
1093 * real group ID 4 bytes
1094 * process ID 4 bytes
1095 * session ID 4 bytes
1097 * port ID 4 bytes/8 bytes (32-bit/64-bit value)
1098 * address type/length 4 bytes
1099 * machine address 16 bytes
1102 au_to_subject32_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
1103 gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
1106 u_char *dptr = NULL;
1108 KASSERT((tid->at_type == AU_IPv4) || (tid->at_type == AU_IPv6),
1109 ("au_to_subject32_ex: type %u", (unsigned int)tid->at_type));
1111 if (tid->at_type == AU_IPv4)
1112 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 10 *
1115 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 13 *
1118 ADD_U_CHAR(dptr, AUT_SUBJECT32_EX);
1119 ADD_U_INT32(dptr, auid);
1120 ADD_U_INT32(dptr, euid);
1121 ADD_U_INT32(dptr, egid);
1122 ADD_U_INT32(dptr, ruid);
1123 ADD_U_INT32(dptr, rgid);
1124 ADD_U_INT32(dptr, pid);
1125 ADD_U_INT32(dptr, sid);
1126 ADD_U_INT32(dptr, tid->at_port);
1127 ADD_U_INT32(dptr, tid->at_type);
1128 if (tid->at_type == AU_IPv6)
1129 ADD_MEM(dptr, &tid->at_addr[0], 4 * sizeof(u_int32_t));
1131 ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
1137 au_to_subject64_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
1138 gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
1141 u_char *dptr = NULL;
1143 KASSERT((tid->at_type == AU_IPv4) || (tid->at_type == AU_IPv6),
1144 ("au_to_subject64_ex: type %u", (unsigned int)tid->at_type));
1146 if (tid->at_type == AU_IPv4)
1147 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
1148 7 * sizeof(u_int32_t) + sizeof(u_int64_t) +
1149 2 * sizeof(u_int32_t));
1151 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
1152 7 * sizeof(u_int32_t) + sizeof(u_int64_t) +
1153 5 * sizeof(u_int32_t));
1155 ADD_U_CHAR(dptr, AUT_SUBJECT64_EX);
1156 ADD_U_INT32(dptr, auid);
1157 ADD_U_INT32(dptr, euid);
1158 ADD_U_INT32(dptr, egid);
1159 ADD_U_INT32(dptr, ruid);
1160 ADD_U_INT32(dptr, rgid);
1161 ADD_U_INT32(dptr, pid);
1162 ADD_U_INT32(dptr, sid);
1163 ADD_U_INT64(dptr, tid->at_port);
1164 ADD_U_INT32(dptr, tid->at_type);
1165 if (tid->at_type == AU_IPv6)
1166 ADD_MEM(dptr, &tid->at_addr[0], 4 * sizeof(u_int32_t));
1168 ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
1174 au_to_subject_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
1175 gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
1178 return (au_to_subject32_ex(auid, euid, egid, ruid, rgid, pid, sid,
1182 #if !defined(_KERNEL) && !defined(KERNEL) && defined(HAVE_AUDIT_SYSCALLS)
1184 * Collects audit information for the current process and creates a subject
1191 auditinfo_addr_t aia;
1194 * Try to use getaudit_addr(2) first. If this kernel does not support
1195 * it, then fall back on to getaudit(2).
1197 if (getaudit_addr(&aia, sizeof(aia)) != 0) {
1198 if (errno == ENOSYS) {
1199 if (getaudit(&auinfo) != 0)
1201 return (au_to_subject32(auinfo.ai_auid, geteuid(),
1202 getegid(), getuid(), getgid(), getpid(),
1203 auinfo.ai_asid, &auinfo.ai_termid));
1205 /* getaudit_addr(2) failed for some other reason. */
1210 return (au_to_subject32_ex(aia.ai_auid, geteuid(), getegid(), getuid(),
1211 getgid(), getpid(), aia.ai_asid, &aia.ai_termid));
1215 #if defined(_KERNEL) || defined(KERNEL)
1217 au_to_exec_strings(char *strs, int count, u_char type)
1220 u_char *dptr = NULL;
1229 totlen += strlen(p) + 1;
1232 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) + totlen);
1233 ADD_U_CHAR(dptr, type);
1234 ADD_U_INT32(dptr, count);
1235 ADD_STRING(dptr, strs, totlen);
1243 * text count null-terminated strings
1246 au_to_exec_args(char *args, int argc)
1249 return (au_to_exec_strings(args, argc, AUT_EXEC_ARGS));
1255 * text count null-terminated strings
1258 au_to_exec_env(char *envs, int envc)
1261 return (au_to_exec_strings(envs, envc, AUT_EXEC_ENV));
1267 * text count null-terminated strings
1270 au_to_exec_args(char **argv)
1273 u_char *dptr = NULL;
1274 const char *nextarg;
1280 while (nextarg != NULL) {
1283 nextlen = strlen(nextarg);
1284 totlen += nextlen + 1;
1286 nextarg = *(argv + count);
1289 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) + totlen);
1291 ADD_U_CHAR(dptr, AUT_EXEC_ARGS);
1292 ADD_U_INT32(dptr, count);
1294 for (i = 0; i < count; i++) {
1295 nextarg = *(argv + i);
1296 ADD_MEM(dptr, nextarg, strlen(nextarg) + 1);
1305 * text count null-terminated strings
1308 au_to_exec_env(char **envp)
1311 u_char *dptr = NULL;
1314 const char *nextenv;
1318 while (nextenv != NULL) {
1321 nextlen = strlen(nextenv);
1322 totlen += nextlen + 1;
1324 nextenv = *(envp + count);
1327 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) + totlen);
1329 ADD_U_CHAR(dptr, AUT_EXEC_ENV);
1330 ADD_U_INT32(dptr, count);
1332 for (i = 0; i < count; i++) {
1333 nextenv = *(envp + i);
1334 ADD_MEM(dptr, nextenv, strlen(nextenv) + 1);
1343 * zonename length 2 bytes
1344 * zonename N bytes + 1 terminating NULL byte
1347 au_to_zonename(const char *zonename)
1349 u_char *dptr = NULL;
1353 textlen = strlen(zonename) + 1;
1354 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) + textlen);
1356 ADD_U_CHAR(dptr, AUT_ZONENAME);
1357 ADD_U_INT16(dptr, textlen);
1358 ADD_STRING(dptr, zonename, textlen);
1364 * record byte count 4 bytes
1365 * version # 1 byte [2]
1366 * event type 2 bytes
1367 * event modifier 2 bytes
1368 * seconds of time 4 bytes/8 bytes (32-bit/64-bit value)
1369 * milliseconds of time 4 bytes/8 bytes (32-bit/64-bit value)
1372 au_to_header32_tm(int rec_size, au_event_t e_type, au_emod_t e_mod,
1376 u_char *dptr = NULL;
1379 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) +
1380 sizeof(u_char) + 2 * sizeof(u_int16_t) + 2 * sizeof(u_int32_t));
1382 ADD_U_CHAR(dptr, AUT_HEADER32);
1383 ADD_U_INT32(dptr, rec_size);
1384 ADD_U_CHAR(dptr, AUDIT_HEADER_VERSION_OPENBSM);
1385 ADD_U_INT16(dptr, e_type);
1386 ADD_U_INT16(dptr, e_mod);
1388 timems = tm.tv_usec/1000;
1389 /* Add the timestamp */
1390 ADD_U_INT32(dptr, tm.tv_sec);
1391 ADD_U_INT32(dptr, timems); /* We need time in ms. */
1398 * record byte count 4 bytes
1399 * version # 1 byte [2]
1400 * event type 2 bytes
1401 * event modifier 2 bytes
1402 * address type/length 4 bytes
1403 * machine address 4 bytes/16 bytes (IPv4/IPv6 address)
1404 * seconds of time 4 bytes/8 bytes (32-bit/64-bit value)
1405 * milliseconds of time 4 bytes/8 bytes (32-bit/64-bit value)
1408 au_to_header32_ex_tm(int rec_size, au_event_t e_type, au_emod_t e_mod,
1409 struct timeval tm, struct auditinfo_addr *aia)
1412 u_char *dptr = NULL;
1416 tid = &aia->ai_termid;
1417 KASSERT(tid->at_type == AU_IPv4 || tid->at_type == AU_IPv6,
1418 ("au_to_header32_ex_tm: invalid address family"));
1420 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) +
1421 sizeof(u_char) + 2 * sizeof(u_int16_t) + 3 *
1422 sizeof(u_int32_t) + tid->at_type);
1424 ADD_U_CHAR(dptr, AUT_HEADER32_EX);
1425 ADD_U_INT32(dptr, rec_size);
1426 ADD_U_CHAR(dptr, AUDIT_HEADER_VERSION_OPENBSM);
1427 ADD_U_INT16(dptr, e_type);
1428 ADD_U_INT16(dptr, e_mod);
1430 ADD_U_INT32(dptr, tid->at_type);
1431 if (tid->at_type == AU_IPv6)
1432 ADD_MEM(dptr, &tid->at_addr[0], 4 * sizeof(u_int32_t));
1434 ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
1435 timems = tm.tv_usec/1000;
1436 /* Add the timestamp */
1437 ADD_U_INT32(dptr, tm.tv_sec);
1438 ADD_U_INT32(dptr, timems); /* We need time in ms. */
1444 au_to_header64_tm(int rec_size, au_event_t e_type, au_emod_t e_mod,
1448 u_char *dptr = NULL;
1451 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) +
1452 sizeof(u_char) + 2 * sizeof(u_int16_t) + 2 * sizeof(u_int64_t));
1454 ADD_U_CHAR(dptr, AUT_HEADER64);
1455 ADD_U_INT32(dptr, rec_size);
1456 ADD_U_CHAR(dptr, AUDIT_HEADER_VERSION_OPENBSM);
1457 ADD_U_INT16(dptr, e_type);
1458 ADD_U_INT16(dptr, e_mod);
1460 timems = tm.tv_usec/1000;
1461 /* Add the timestamp */
1462 ADD_U_INT64(dptr, tm.tv_sec);
1463 ADD_U_INT64(dptr, timems); /* We need time in ms. */
1468 #if !defined(KERNEL) && !defined(_KERNEL)
1469 #ifdef HAVE_AUDIT_SYSCALLS
1471 au_to_header32_ex(int rec_size, au_event_t e_type, au_emod_t e_mod)
1474 struct auditinfo_addr aia;
1476 if (gettimeofday(&tm, NULL) == -1)
1478 if (audit_get_kaudit(&aia, sizeof(aia)) != 0) {
1479 if (errno != ENOSYS)
1481 return (au_to_header32_tm(rec_size, e_type, e_mod, tm));
1483 return (au_to_header32_ex_tm(rec_size, e_type, e_mod, tm, &aia));
1485 #endif /* HAVE_AUDIT_SYSCALLS */
1488 au_to_header32(int rec_size, au_event_t e_type, au_emod_t e_mod)
1492 if (gettimeofday(&tm, NULL) == -1)
1494 return (au_to_header32_tm(rec_size, e_type, e_mod, tm));
1498 au_to_header64(__unused int rec_size, __unused au_event_t e_type,
1499 __unused au_emod_t e_mod)
1503 if (gettimeofday(&tm, NULL) == -1)
1505 return (au_to_header64_tm(rec_size, e_type, e_mod, tm));
1509 au_to_header(int rec_size, au_event_t e_type, au_emod_t e_mod)
1512 return (au_to_header32(rec_size, e_type, e_mod));
1515 #ifdef HAVE_AUDIT_SYSCALLS
1517 au_to_header_ex(int rec_size, au_event_t e_type, au_emod_t e_mod)
1520 return (au_to_header32_ex(rec_size, e_type, e_mod));
1522 #endif /* HAVE_AUDIT_SYSCALLS */
1523 #endif /* !defined(KERNEL) && !defined(_KERNEL) */
1527 * trailer magic number 2 bytes
1528 * record byte count 4 bytes
1531 au_to_trailer(int rec_size)
1534 u_char *dptr = NULL;
1535 u_int16_t magic = AUT_TRAILER_MAGIC;
1537 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) +
1540 ADD_U_CHAR(dptr, AUT_TRAILER);
1541 ADD_U_INT16(dptr, magic);
1542 ADD_U_INT32(dptr, rec_size);