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/param.h>
40 #include <sys/types.h>
41 #include <sys/endian.h>
42 #include <sys/queue.h>
43 #include <sys/socket.h>
47 #include <sys/libkern.h>
48 #include <sys/malloc.h>
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/ip.h>
56 #include <bsm/audit.h>
57 #include <bsm/audit_internal.h>
58 #include <bsm/audit_record.h>
59 #include <security/audit/audit.h>
60 #include <security/audit/audit_private.h>
62 #define GET_TOKEN_AREA(t, dptr, length) do { \
63 t = malloc(sizeof(token_t), M_AUDITBSM, M_WAITOK); \
64 t->t_data = malloc(length, M_AUDITBSM, M_WAITOK | M_ZERO); \
72 * argument value 4 bytes/8 bytes (32-bit/64-bit value)
74 * text N bytes + 1 terminating NULL byte
77 au_to_arg32(char n, const char *text, u_int32_t v)
83 textlen = strlen(text);
86 GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int32_t) +
87 sizeof(u_int16_t) + textlen);
89 ADD_U_CHAR(dptr, AUT_ARG32);
92 ADD_U_INT16(dptr, textlen);
93 ADD_STRING(dptr, text, textlen);
99 au_to_arg64(char n, const char *text, u_int64_t v)
105 textlen = strlen(text);
108 GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int64_t) +
109 sizeof(u_int16_t) + textlen);
111 ADD_U_CHAR(dptr, AUT_ARG64);
113 ADD_U_INT64(dptr, v);
114 ADD_U_INT16(dptr, textlen);
115 ADD_STRING(dptr, text, textlen);
121 au_to_arg(char n, const char *text, u_int32_t v)
124 return (au_to_arg32(n, text, v));
127 #if defined(_KERNEL) || defined(KERNEL)
130 * file access mode 4 bytes
131 * owner user ID 4 bytes
132 * owner group ID 4 bytes
133 * file system ID 4 bytes
135 * device 4 bytes/8 bytes (32-bit/64-bit)
138 au_to_attr32(struct vnode_au_info *vni)
142 u_int16_t pad0_16 = 0;
143 u_int32_t pad0_32 = 0;
145 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(u_int16_t) +
146 3 * sizeof(u_int32_t) + sizeof(u_int64_t) + sizeof(u_int32_t));
148 ADD_U_CHAR(dptr, AUT_ATTR32);
151 * BSD defines the size for the file mode as 2 bytes; BSM defines 4
154 * XXXRW: Possibly should be conditionally compiled.
156 * XXXRW: Should any conversions take place on the mode?
158 ADD_U_INT16(dptr, pad0_16);
159 ADD_U_INT16(dptr, vni->vn_mode);
161 ADD_U_INT32(dptr, vni->vn_uid);
162 ADD_U_INT32(dptr, vni->vn_gid);
163 ADD_U_INT32(dptr, vni->vn_fsid);
166 * Some systems use 32-bit file ID's, others use 64-bit file IDs.
167 * Attempt to handle both, and let the compiler sort it out. If we
168 * could pick this out at compile-time, it would be better, so as to
169 * avoid the else case below.
171 if (sizeof(vni->vn_fileid) == sizeof(uint32_t)) {
172 ADD_U_INT32(dptr, pad0_32);
173 ADD_U_INT32(dptr, vni->vn_fileid);
174 } else if (sizeof(vni->vn_fileid) == sizeof(uint64_t))
175 ADD_U_INT64(dptr, vni->vn_fileid);
177 ADD_U_INT64(dptr, 0LL);
179 ADD_U_INT32(dptr, vni->vn_dev);
185 au_to_attr64(struct vnode_au_info *vni)
189 u_int16_t pad0_16 = 0;
190 u_int32_t pad0_32 = 0;
192 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(u_int16_t) +
193 3 * sizeof(u_int32_t) + sizeof(u_int64_t) * 2);
195 ADD_U_CHAR(dptr, AUT_ATTR64);
198 * BSD defines the size for the file mode as 2 bytes; BSM defines 4
201 * XXXRW: Possibly should be conditionally compiled.
203 * XXXRW: Should any conversions take place on the mode?
205 ADD_U_INT16(dptr, pad0_16);
206 ADD_U_INT16(dptr, vni->vn_mode);
208 ADD_U_INT32(dptr, vni->vn_uid);
209 ADD_U_INT32(dptr, vni->vn_gid);
210 ADD_U_INT32(dptr, vni->vn_fsid);
213 * Some systems use 32-bit file ID's, other's use 64-bit file IDs.
214 * Attempt to handle both, and let the compiler sort it out. If we
215 * could pick this out at compile-time, it would be better, so as to
216 * avoid the else case below.
218 if (sizeof(vni->vn_fileid) == sizeof(uint32_t)) {
219 ADD_U_INT32(dptr, pad0_32);
220 ADD_U_INT32(dptr, vni->vn_fileid);
221 } else if (sizeof(vni->vn_fileid) == sizeof(uint64_t))
222 ADD_U_INT64(dptr, vni->vn_fileid);
224 ADD_U_INT64(dptr, 0LL);
226 ADD_U_INT64(dptr, vni->vn_dev);
232 au_to_attr(struct vnode_au_info *vni)
235 return (au_to_attr32(vni));
237 #endif /* !(defined(_KERNEL) || defined(KERNEL) */
241 * how to print 1 byte
244 * data items (depends on basic unit)
247 au_to_data(char unit_print, char unit_type, char unit_count, const char *p)
251 size_t datasize, totdata;
253 /* Determine the size of the basic unit. */
257 datasize = AUR_BYTE_SIZE;
261 datasize = AUR_SHORT_SIZE;
266 datasize = AUR_INT32_SIZE;
270 datasize = AUR_INT64_SIZE;
277 totdata = datasize * unit_count;
279 GET_TOKEN_AREA(t, dptr, 4 * sizeof(u_char) + totdata);
282 * XXXRW: We should be byte-swapping each data item for multi-byte
285 ADD_U_CHAR(dptr, AUT_DATA);
286 ADD_U_CHAR(dptr, unit_print);
287 ADD_U_CHAR(dptr, unit_type);
288 ADD_U_CHAR(dptr, unit_count);
289 ADD_MEM(dptr, p, totdata);
298 * return value 4 bytes
301 au_to_exit(int retval, int err)
306 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(u_int32_t));
308 ADD_U_CHAR(dptr, AUT_EXIT);
309 ADD_U_INT32(dptr, err);
310 ADD_U_INT32(dptr, retval);
318 au_to_groups(int *groups)
321 return (au_to_newgroups(AUDIT_MAX_GROUPS, (gid_t *)groups));
326 * number groups 2 bytes
327 * group list count * 4 bytes
330 au_to_newgroups(u_int16_t n, gid_t *groups)
336 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) +
337 n * sizeof(u_int32_t));
339 ADD_U_CHAR(dptr, AUT_NEWGROUPS);
340 ADD_U_INT16(dptr, n);
341 for (i = 0; i < n; i++)
342 ADD_U_INT32(dptr, groups[i]);
349 * internet address 4 bytes
352 au_to_in_addr(struct in_addr *internet_addr)
357 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(uint32_t));
359 ADD_U_CHAR(dptr, AUT_IN_ADDR);
360 ADD_MEM(dptr, &internet_addr->s_addr, sizeof(uint32_t));
367 * address type/length 4 bytes
371 au_to_in_addr_ex(struct in6_addr *internet_addr)
375 u_int32_t type = AU_IPv6;
377 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 5 * sizeof(uint32_t));
379 ADD_U_CHAR(dptr, AUT_IN_ADDR_EX);
380 ADD_U_INT32(dptr, type);
381 ADD_MEM(dptr, internet_addr, 4 * sizeof(uint32_t));
390 * The IP header should be submitted in network byte order.
393 au_to_ip(struct ip *ip)
398 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(struct ip));
400 ADD_U_CHAR(dptr, AUT_IP);
401 ADD_MEM(dptr, ip, sizeof(struct ip));
408 * object ID type 1 byte
412 au_to_ipc(char type, int id)
417 GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int32_t));
419 ADD_U_CHAR(dptr, AUT_IPC);
420 ADD_U_CHAR(dptr, type);
421 ADD_U_INT32(dptr, id);
428 * owner user ID 4 bytes
429 * owner group ID 4 bytes
430 * creator user ID 4 bytes
431 * creator group ID 4 bytes
432 * access mode 4 bytes
433 * slot sequence # 4 bytes
437 au_to_ipc_perm(struct ipc_perm *perm)
443 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 12 * sizeof(u_int16_t) +
446 ADD_U_CHAR(dptr, AUT_IPC_PERM);
449 * Systems vary significantly in what types they use in struct
450 * ipc_perm; at least a few still use 16-bit uid's and gid's, so
451 * allow for that, as BSM define 32-bit values here.
452 * Some systems define the sizes for ipc_perm members as 2 bytes;
453 * BSM defines 4 so pad with 0.
455 * XXXRW: Possibly shoulid be conditionally compiled, and more cases
456 * need to be handled.
458 if (sizeof(perm->uid) != sizeof(u_int32_t)) {
459 ADD_U_INT16(dptr, pad0);
460 ADD_U_INT16(dptr, perm->uid);
461 ADD_U_INT16(dptr, pad0);
462 ADD_U_INT16(dptr, perm->gid);
463 ADD_U_INT16(dptr, pad0);
464 ADD_U_INT16(dptr, perm->cuid);
465 ADD_U_INT16(dptr, pad0);
466 ADD_U_INT16(dptr, perm->cgid);
468 ADD_U_INT32(dptr, perm->uid);
469 ADD_U_INT32(dptr, perm->gid);
470 ADD_U_INT32(dptr, perm->cuid);
471 ADD_U_INT32(dptr, perm->cgid);
474 ADD_U_INT16(dptr, pad0);
475 ADD_U_INT16(dptr, perm->mode);
477 ADD_U_INT16(dptr, pad0);
479 ADD_U_INT16(dptr, perm->seq);
481 ADD_U_INT32(dptr, perm->key);
488 * port IP address 2 bytes
491 au_to_iport(u_int16_t iport)
496 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t));
498 ADD_U_CHAR(dptr, AUT_IPORT);
499 ADD_U_INT16(dptr, iport);
510 au_to_opaque(const char *data, u_int16_t bytes)
515 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) + bytes);
517 ADD_U_CHAR(dptr, AUT_OPAQUE);
518 ADD_U_INT16(dptr, bytes);
519 ADD_MEM(dptr, data, bytes);
526 * seconds of time 4 bytes
527 * milliseconds of time 4 bytes
528 * file name len 2 bytes
529 * file pathname N bytes + 1 terminating NULL byte
532 au_to_file(const char *file, struct timeval tm)
539 filelen = strlen(file);
542 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(u_int32_t) +
543 sizeof(u_int16_t) + filelen);
545 timems = tm.tv_usec/1000;
547 ADD_U_CHAR(dptr, AUT_OTHER_FILE32);
548 ADD_U_INT32(dptr, tm.tv_sec);
549 ADD_U_INT32(dptr, timems); /* We need time in ms. */
550 ADD_U_INT16(dptr, filelen);
551 ADD_STRING(dptr, file, filelen);
558 * text length 2 bytes
559 * text N bytes + 1 terminating NULL byte
562 au_to_text(const char *text)
568 textlen = strlen(text);
571 /* XXXRW: Should validate length against token size limit. */
573 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) + textlen);
575 ADD_U_CHAR(dptr, AUT_TEXT);
576 ADD_U_INT16(dptr, textlen);
577 ADD_STRING(dptr, text, textlen);
584 * path length 2 bytes
585 * path N bytes + 1 terminating NULL byte
588 au_to_path(const char *text)
594 textlen = strlen(text);
597 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) + textlen);
599 ADD_U_CHAR(dptr, AUT_PATH);
600 ADD_U_INT16(dptr, textlen);
601 ADD_STRING(dptr, text, textlen);
609 * effective user ID 4 bytes
610 * effective group ID 4 bytes
611 * real user ID 4 bytes
612 * real group ID 4 bytes
616 * port ID 4 bytes/8 bytes (32-bit/64-bit value)
617 * machine address 4 bytes
620 au_to_process32(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
621 pid_t pid, au_asid_t sid, au_tid_t *tid)
626 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 9 * sizeof(u_int32_t));
628 ADD_U_CHAR(dptr, AUT_PROCESS32);
629 ADD_U_INT32(dptr, auid);
630 ADD_U_INT32(dptr, euid);
631 ADD_U_INT32(dptr, egid);
632 ADD_U_INT32(dptr, ruid);
633 ADD_U_INT32(dptr, rgid);
634 ADD_U_INT32(dptr, pid);
635 ADD_U_INT32(dptr, sid);
636 ADD_U_INT32(dptr, tid->port);
639 * Note: Solaris will write out IPv6 addresses here as a 32-bit
640 * address type and 16 bytes of address, but for IPv4 addresses it
641 * simply writes the 4-byte address directly. We support only IPv4
642 * addresses for process32 tokens.
644 ADD_MEM(dptr, &tid->machine, sizeof(u_int32_t));
650 au_to_process64(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
651 pid_t pid, au_asid_t sid, au_tid_t *tid)
656 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 8 * sizeof(u_int32_t) +
659 ADD_U_CHAR(dptr, AUT_PROCESS64);
660 ADD_U_INT32(dptr, auid);
661 ADD_U_INT32(dptr, euid);
662 ADD_U_INT32(dptr, egid);
663 ADD_U_INT32(dptr, ruid);
664 ADD_U_INT32(dptr, rgid);
665 ADD_U_INT32(dptr, pid);
666 ADD_U_INT32(dptr, sid);
667 ADD_U_INT64(dptr, tid->port);
670 * Note: Solaris will write out IPv6 addresses here as a 32-bit
671 * address type and 16 bytes of address, but for IPv4 addresses it
672 * simply writes the 4-byte address directly. We support only IPv4
673 * addresses for process64 tokens.
675 ADD_MEM(dptr, &tid->machine, sizeof(u_int32_t));
681 au_to_process(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
682 pid_t pid, au_asid_t sid, au_tid_t *tid)
685 return (au_to_process32(auid, euid, egid, ruid, rgid, pid, sid,
692 * effective user ID 4 bytes
693 * effective group ID 4 bytes
694 * real user ID 4 bytes
695 * real group ID 4 bytes
699 * port ID 4 bytes/8 bytes (32-bit/64-bit value)
700 * address type-len 4 bytes
701 * machine address 16 bytes
704 au_to_process32_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
705 gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
710 KASSERT((tid->at_type == AU_IPv4) || (tid->at_type == AU_IPv6),
711 ("au_to_process32_ex: type %u", (unsigned int)tid->at_type));
712 if (tid->at_type == AU_IPv4)
713 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
714 10 * sizeof(u_int32_t));
716 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
717 13 * sizeof(u_int32_t));
719 ADD_U_CHAR(dptr, AUT_PROCESS32_EX);
720 ADD_U_INT32(dptr, auid);
721 ADD_U_INT32(dptr, euid);
722 ADD_U_INT32(dptr, egid);
723 ADD_U_INT32(dptr, ruid);
724 ADD_U_INT32(dptr, rgid);
725 ADD_U_INT32(dptr, pid);
726 ADD_U_INT32(dptr, sid);
727 ADD_U_INT32(dptr, tid->at_port);
728 ADD_U_INT32(dptr, tid->at_type);
729 ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
730 if (tid->at_type == AU_IPv6) {
731 ADD_MEM(dptr, &tid->at_addr[1], sizeof(u_int32_t));
732 ADD_MEM(dptr, &tid->at_addr[2], sizeof(u_int32_t));
733 ADD_MEM(dptr, &tid->at_addr[3], sizeof(u_int32_t));
740 au_to_process64_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
741 gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
746 if (tid->at_type == AU_IPv4)
747 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
748 7 * sizeof(u_int32_t) + sizeof(u_int64_t) +
749 2 * sizeof(u_int32_t));
750 else if (tid->at_type == AU_IPv6)
751 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
752 7 * sizeof(u_int32_t) + sizeof(u_int64_t) +
753 5 * sizeof(u_int32_t));
755 panic("au_to_process64_ex: invalidate at_type (%d)",
758 ADD_U_CHAR(dptr, AUT_PROCESS64_EX);
759 ADD_U_INT32(dptr, auid);
760 ADD_U_INT32(dptr, euid);
761 ADD_U_INT32(dptr, egid);
762 ADD_U_INT32(dptr, ruid);
763 ADD_U_INT32(dptr, rgid);
764 ADD_U_INT32(dptr, pid);
765 ADD_U_INT32(dptr, sid);
766 ADD_U_INT64(dptr, tid->at_port);
767 ADD_U_INT32(dptr, tid->at_type);
768 ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
769 if (tid->at_type == AU_IPv6) {
770 ADD_MEM(dptr, &tid->at_addr[1], sizeof(u_int32_t));
771 ADD_MEM(dptr, &tid->at_addr[2], sizeof(u_int32_t));
772 ADD_MEM(dptr, &tid->at_addr[3], sizeof(u_int32_t));
779 au_to_process_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
780 gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
783 return (au_to_process32_ex(auid, euid, egid, ruid, rgid, pid, sid,
789 * error status 1 byte
790 * return value 4 bytes/8 bytes (32-bit/64-bit value)
793 au_to_return32(char status, u_int32_t ret)
798 GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int32_t));
800 ADD_U_CHAR(dptr, AUT_RETURN32);
801 ADD_U_CHAR(dptr, status);
802 ADD_U_INT32(dptr, ret);
808 au_to_return64(char status, u_int64_t ret)
813 GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int64_t));
815 ADD_U_CHAR(dptr, AUT_RETURN64);
816 ADD_U_CHAR(dptr, status);
817 ADD_U_INT64(dptr, ret);
823 au_to_return(char status, u_int32_t ret)
826 return (au_to_return32(status, ret));
831 * sequence number 4 bytes
834 au_to_seq(long audit_count)
839 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t));
841 ADD_U_CHAR(dptr, AUT_SEQ);
842 ADD_U_INT32(dptr, audit_count);
849 * socket domain 2 bytes
850 * socket type 2 bytes
851 * address type 2 byte
853 * local address 4 bytes/16 bytes (IPv4/IPv6 address)
854 * remote port 2 bytes
855 * remote address 4 bytes/16 bytes (IPv4/IPv6 address)
857 * Domain and type arguments to this routine are assumed to already have been
858 * converted to the BSM constant space, so we don't do that here.
861 au_to_socket_ex(u_short so_domain, u_short so_type,
862 struct sockaddr *sa_local, struct sockaddr *sa_remote)
866 struct sockaddr_in *sin;
867 struct sockaddr_in6 *sin6;
869 if (so_domain == AF_INET)
870 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
871 5 * sizeof(u_int16_t) + 2 * sizeof(u_int32_t));
872 else if (so_domain == AF_INET6)
873 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
874 5 * sizeof(u_int16_t) + 8 * sizeof(u_int32_t));
878 ADD_U_CHAR(dptr, AUT_SOCKET_EX);
879 ADD_U_INT16(dptr, au_domain_to_bsm(so_domain));
880 ADD_U_INT16(dptr, au_socket_type_to_bsm(so_type));
881 if (so_domain == AF_INET) {
882 ADD_U_INT16(dptr, AU_IPv4);
883 sin = (struct sockaddr_in *)sa_local;
884 ADD_MEM(dptr, &sin->sin_port, sizeof(uint16_t));
885 ADD_MEM(dptr, &sin->sin_addr.s_addr, sizeof(uint32_t));
886 sin = (struct sockaddr_in *)sa_remote;
887 ADD_MEM(dptr, &sin->sin_port, sizeof(uint16_t));
888 ADD_MEM(dptr, &sin->sin_addr.s_addr, sizeof(uint32_t));
890 ADD_U_INT16(dptr, AU_IPv6);
891 sin6 = (struct sockaddr_in6 *)sa_local;
892 ADD_MEM(dptr, &sin6->sin6_port, sizeof(uint16_t));
893 ADD_MEM(dptr, &sin6->sin6_addr, 4 * sizeof(uint32_t));
894 sin6 = (struct sockaddr_in6 *)sa_remote;
895 ADD_MEM(dptr, &sin6->sin6_port, sizeof(uint16_t));
896 ADD_MEM(dptr, &sin6->sin6_addr, 4 * sizeof(uint32_t));
903 * Kernel-specific version of the above function.
905 * XXXRW: Should now use au_to_socket_ex() here.
909 kau_to_socket(struct socket_au_info *soi)
915 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(u_int16_t) +
916 sizeof(u_int32_t) + sizeof(u_int16_t) + sizeof(u_int32_t));
918 ADD_U_CHAR(dptr, AUT_SOCKET);
919 /* Coerce the socket type into a short value */
920 so_type = soi->so_type;
921 ADD_U_INT16(dptr, so_type);
922 ADD_U_INT16(dptr, soi->so_lport);
923 ADD_U_INT32(dptr, soi->so_laddr);
924 ADD_U_INT16(dptr, soi->so_rport);
925 ADD_U_INT32(dptr, soi->so_raddr);
933 * socket family 2 bytes
934 * path (up to) 104 bytes + NULL (NULL terminated string)
937 au_to_sock_unix(struct sockaddr_un *so)
942 GET_TOKEN_AREA(t, dptr, 3 * sizeof(u_char) + strlen(so->sun_path) + 1);
944 ADD_U_CHAR(dptr, AUT_SOCKUNIX);
945 /* BSM token has two bytes for family */
947 ADD_U_CHAR(dptr, so->sun_family);
948 ADD_STRING(dptr, so->sun_path, strlen(so->sun_path) + 1);
955 * socket family 2 bytes
957 * socket address 4 bytes
960 au_to_sock_inet32(struct sockaddr_in *so)
966 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(uint16_t) +
969 ADD_U_CHAR(dptr, AUT_SOCKINET32);
971 * BSM defines the family field as 16 bits, but many operating
972 * systems have an 8-bit sin_family field. Extend to 16 bits before
973 * writing into the token. Assume that both the port and the address
974 * in the sockaddr_in are already in network byte order, but family
975 * is in local byte order.
977 * XXXRW: Should a name space conversion be taking place on the value
980 family = so->sin_family;
981 ADD_U_INT16(dptr, family);
982 ADD_MEM(dptr, &so->sin_port, sizeof(uint16_t));
983 ADD_MEM(dptr, &so->sin_addr.s_addr, sizeof(uint32_t));
989 au_to_sock_inet128(struct sockaddr_in6 *so)
994 GET_TOKEN_AREA(t, dptr, 3 * sizeof(u_char) + sizeof(u_int16_t) +
995 4 * sizeof(u_int32_t));
997 ADD_U_CHAR(dptr, AUT_SOCKINET128);
999 * In BSD, sin6_family is one octet, but BSM defines the token to
1000 * store two. So we copy in a 0 first. XXXRW: Possibly should be
1001 * conditionally compiled.
1003 ADD_U_CHAR(dptr, 0);
1004 ADD_U_CHAR(dptr, so->sin6_family);
1006 ADD_U_INT16(dptr, so->sin6_port);
1007 ADD_MEM(dptr, &so->sin6_addr, 4 * sizeof(uint32_t));
1013 au_to_sock_inet(struct sockaddr_in *so)
1016 return (au_to_sock_inet32(so));
1022 * effective user ID 4 bytes
1023 * effective group ID 4 bytes
1024 * real user ID 4 bytes
1025 * real group ID 4 bytes
1026 * process ID 4 bytes
1027 * session ID 4 bytes
1029 * port ID 4 bytes/8 bytes (32-bit/64-bit value)
1030 * machine address 4 bytes
1033 au_to_subject32(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
1034 pid_t pid, au_asid_t sid, au_tid_t *tid)
1037 u_char *dptr = NULL;
1039 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 9 * sizeof(u_int32_t));
1041 ADD_U_CHAR(dptr, AUT_SUBJECT32);
1042 ADD_U_INT32(dptr, auid);
1043 ADD_U_INT32(dptr, euid);
1044 ADD_U_INT32(dptr, egid);
1045 ADD_U_INT32(dptr, ruid);
1046 ADD_U_INT32(dptr, rgid);
1047 ADD_U_INT32(dptr, pid);
1048 ADD_U_INT32(dptr, sid);
1049 ADD_U_INT32(dptr, tid->port);
1050 ADD_MEM(dptr, &tid->machine, sizeof(u_int32_t));
1056 au_to_subject64(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
1057 pid_t pid, au_asid_t sid, au_tid_t *tid)
1060 u_char *dptr = NULL;
1062 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 7 * sizeof(u_int32_t) +
1063 sizeof(u_int64_t) + sizeof(u_int32_t));
1065 ADD_U_CHAR(dptr, AUT_SUBJECT64);
1066 ADD_U_INT32(dptr, auid);
1067 ADD_U_INT32(dptr, euid);
1068 ADD_U_INT32(dptr, egid);
1069 ADD_U_INT32(dptr, ruid);
1070 ADD_U_INT32(dptr, rgid);
1071 ADD_U_INT32(dptr, pid);
1072 ADD_U_INT32(dptr, sid);
1073 ADD_U_INT64(dptr, tid->port);
1074 ADD_MEM(dptr, &tid->machine, sizeof(u_int32_t));
1080 au_to_subject(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
1081 pid_t pid, au_asid_t sid, au_tid_t *tid)
1084 return (au_to_subject32(auid, euid, egid, ruid, rgid, pid, sid,
1091 * effective user ID 4 bytes
1092 * effective group ID 4 bytes
1093 * real user ID 4 bytes
1094 * real group ID 4 bytes
1095 * process ID 4 bytes
1096 * session ID 4 bytes
1098 * port ID 4 bytes/8 bytes (32-bit/64-bit value)
1099 * address type/length 4 bytes
1100 * machine address 16 bytes
1103 au_to_subject32_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
1104 gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
1107 u_char *dptr = NULL;
1109 KASSERT((tid->at_type == AU_IPv4) || (tid->at_type == AU_IPv6),
1110 ("au_to_subject32_ex: type %u", (unsigned int)tid->at_type));
1112 if (tid->at_type == AU_IPv4)
1113 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 10 *
1116 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 13 *
1119 ADD_U_CHAR(dptr, AUT_SUBJECT32_EX);
1120 ADD_U_INT32(dptr, auid);
1121 ADD_U_INT32(dptr, euid);
1122 ADD_U_INT32(dptr, egid);
1123 ADD_U_INT32(dptr, ruid);
1124 ADD_U_INT32(dptr, rgid);
1125 ADD_U_INT32(dptr, pid);
1126 ADD_U_INT32(dptr, sid);
1127 ADD_U_INT32(dptr, tid->at_port);
1128 ADD_U_INT32(dptr, tid->at_type);
1129 if (tid->at_type == AU_IPv6)
1130 ADD_MEM(dptr, &tid->at_addr[0], 4 * sizeof(u_int32_t));
1132 ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
1138 au_to_subject64_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
1139 gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
1142 u_char *dptr = NULL;
1144 KASSERT((tid->at_type == AU_IPv4) || (tid->at_type == AU_IPv6),
1145 ("au_to_subject64_ex: type %u", (unsigned int)tid->at_type));
1147 if (tid->at_type == AU_IPv4)
1148 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
1149 7 * sizeof(u_int32_t) + sizeof(u_int64_t) +
1150 2 * sizeof(u_int32_t));
1152 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
1153 7 * sizeof(u_int32_t) + sizeof(u_int64_t) +
1154 5 * sizeof(u_int32_t));
1156 ADD_U_CHAR(dptr, AUT_SUBJECT64_EX);
1157 ADD_U_INT32(dptr, auid);
1158 ADD_U_INT32(dptr, euid);
1159 ADD_U_INT32(dptr, egid);
1160 ADD_U_INT32(dptr, ruid);
1161 ADD_U_INT32(dptr, rgid);
1162 ADD_U_INT32(dptr, pid);
1163 ADD_U_INT32(dptr, sid);
1164 ADD_U_INT64(dptr, tid->at_port);
1165 ADD_U_INT32(dptr, tid->at_type);
1166 if (tid->at_type == AU_IPv6)
1167 ADD_MEM(dptr, &tid->at_addr[0], 4 * sizeof(u_int32_t));
1169 ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
1175 au_to_subject_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
1176 gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
1179 return (au_to_subject32_ex(auid, euid, egid, ruid, rgid, pid, sid,
1183 #if !defined(_KERNEL) && !defined(KERNEL) && defined(HAVE_AUDIT_SYSCALLS)
1185 * Collects audit information for the current process and creates a subject
1192 auditinfo_addr_t aia;
1195 * Try to use getaudit_addr(2) first. If this kernel does not support
1196 * it, then fall back on to getaudit(2).
1198 if (getaudit_addr(&aia, sizeof(aia)) != 0) {
1199 if (errno == ENOSYS) {
1200 if (getaudit(&auinfo) != 0)
1202 return (au_to_subject32(auinfo.ai_auid, geteuid(),
1203 getegid(), getuid(), getgid(), getpid(),
1204 auinfo.ai_asid, &auinfo.ai_termid));
1206 /* getaudit_addr(2) failed for some other reason. */
1211 return (au_to_subject32_ex(aia.ai_auid, geteuid(), getegid(), getuid(),
1212 getgid(), getpid(), aia.ai_asid, &aia.ai_termid));
1216 #if defined(_KERNEL) || defined(KERNEL)
1218 au_to_exec_strings(char *strs, int count, u_char type)
1221 u_char *dptr = NULL;
1230 totlen += strlen(p) + 1;
1233 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) + totlen);
1234 ADD_U_CHAR(dptr, type);
1235 ADD_U_INT32(dptr, count);
1236 ADD_STRING(dptr, strs, totlen);
1244 * text count null-terminated strings
1247 au_to_exec_args(char *args, int argc)
1250 return (au_to_exec_strings(args, argc, AUT_EXEC_ARGS));
1256 * text count null-terminated strings
1259 au_to_exec_env(char *envs, int envc)
1262 return (au_to_exec_strings(envs, envc, AUT_EXEC_ENV));
1268 * text count null-terminated strings
1271 au_to_exec_args(char **argv)
1274 u_char *dptr = NULL;
1275 const char *nextarg;
1281 while (nextarg != NULL) {
1284 nextlen = strlen(nextarg);
1285 totlen += nextlen + 1;
1287 nextarg = *(argv + count);
1290 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) + totlen);
1292 ADD_U_CHAR(dptr, AUT_EXEC_ARGS);
1293 ADD_U_INT32(dptr, count);
1295 for (i = 0; i < count; i++) {
1296 nextarg = *(argv + i);
1297 ADD_MEM(dptr, nextarg, strlen(nextarg) + 1);
1306 * text count null-terminated strings
1309 au_to_exec_env(char **envp)
1312 u_char *dptr = NULL;
1315 const char *nextenv;
1319 while (nextenv != NULL) {
1322 nextlen = strlen(nextenv);
1323 totlen += nextlen + 1;
1325 nextenv = *(envp + count);
1328 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) + totlen);
1330 ADD_U_CHAR(dptr, AUT_EXEC_ENV);
1331 ADD_U_INT32(dptr, count);
1333 for (i = 0; i < count; i++) {
1334 nextenv = *(envp + i);
1335 ADD_MEM(dptr, nextenv, strlen(nextenv) + 1);
1344 * zonename length 2 bytes
1345 * zonename N bytes + 1 terminating NULL byte
1348 au_to_zonename(const char *zonename)
1350 u_char *dptr = NULL;
1354 textlen = strlen(zonename) + 1;
1355 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) + textlen);
1357 ADD_U_CHAR(dptr, AUT_ZONENAME);
1358 ADD_U_INT16(dptr, textlen);
1359 ADD_STRING(dptr, zonename, textlen);
1365 * record byte count 4 bytes
1366 * version # 1 byte [2]
1367 * event type 2 bytes
1368 * event modifier 2 bytes
1369 * seconds of time 4 bytes/8 bytes (32-bit/64-bit value)
1370 * milliseconds of time 4 bytes/8 bytes (32-bit/64-bit value)
1373 au_to_header32_tm(int rec_size, au_event_t e_type, au_emod_t e_mod,
1377 u_char *dptr = NULL;
1380 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) +
1381 sizeof(u_char) + 2 * sizeof(u_int16_t) + 2 * sizeof(u_int32_t));
1383 ADD_U_CHAR(dptr, AUT_HEADER32);
1384 ADD_U_INT32(dptr, rec_size);
1385 ADD_U_CHAR(dptr, AUDIT_HEADER_VERSION_OPENBSM);
1386 ADD_U_INT16(dptr, e_type);
1387 ADD_U_INT16(dptr, e_mod);
1389 timems = tm.tv_usec/1000;
1390 /* Add the timestamp */
1391 ADD_U_INT32(dptr, tm.tv_sec);
1392 ADD_U_INT32(dptr, timems); /* We need time in ms. */
1399 * record byte count 4 bytes
1400 * version # 1 byte [2]
1401 * event type 2 bytes
1402 * event modifier 2 bytes
1403 * address type/length 4 bytes
1404 * machine address 4 bytes/16 bytes (IPv4/IPv6 address)
1405 * seconds of time 4 bytes/8 bytes (32-bit/64-bit value)
1406 * milliseconds of time 4 bytes/8 bytes (32-bit/64-bit value)
1409 au_to_header32_ex_tm(int rec_size, au_event_t e_type, au_emod_t e_mod,
1410 struct timeval tm, struct auditinfo_addr *aia)
1413 u_char *dptr = NULL;
1417 tid = &aia->ai_termid;
1418 KASSERT(tid->at_type == AU_IPv4 || tid->at_type == AU_IPv6,
1419 ("au_to_header32_ex_tm: invalid address family"));
1421 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) +
1422 sizeof(u_char) + 2 * sizeof(u_int16_t) + 3 *
1423 sizeof(u_int32_t) + tid->at_type);
1425 ADD_U_CHAR(dptr, AUT_HEADER32_EX);
1426 ADD_U_INT32(dptr, rec_size);
1427 ADD_U_CHAR(dptr, AUDIT_HEADER_VERSION_OPENBSM);
1428 ADD_U_INT16(dptr, e_type);
1429 ADD_U_INT16(dptr, e_mod);
1431 ADD_U_INT32(dptr, tid->at_type);
1432 if (tid->at_type == AU_IPv6)
1433 ADD_MEM(dptr, &tid->at_addr[0], 4 * sizeof(u_int32_t));
1435 ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
1436 timems = tm.tv_usec/1000;
1437 /* Add the timestamp */
1438 ADD_U_INT32(dptr, tm.tv_sec);
1439 ADD_U_INT32(dptr, timems); /* We need time in ms. */
1445 au_to_header64_tm(int rec_size, au_event_t e_type, au_emod_t e_mod,
1449 u_char *dptr = NULL;
1452 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) +
1453 sizeof(u_char) + 2 * sizeof(u_int16_t) + 2 * sizeof(u_int64_t));
1455 ADD_U_CHAR(dptr, AUT_HEADER64);
1456 ADD_U_INT32(dptr, rec_size);
1457 ADD_U_CHAR(dptr, AUDIT_HEADER_VERSION_OPENBSM);
1458 ADD_U_INT16(dptr, e_type);
1459 ADD_U_INT16(dptr, e_mod);
1461 timems = tm.tv_usec/1000;
1462 /* Add the timestamp */
1463 ADD_U_INT64(dptr, tm.tv_sec);
1464 ADD_U_INT64(dptr, timems); /* We need time in ms. */
1469 #if !defined(KERNEL) && !defined(_KERNEL)
1470 #ifdef HAVE_AUDIT_SYSCALLS
1472 au_to_header32_ex(int rec_size, au_event_t e_type, au_emod_t e_mod)
1475 struct auditinfo_addr aia;
1477 if (gettimeofday(&tm, NULL) == -1)
1479 if (audit_get_kaudit(&aia, sizeof(aia)) != 0) {
1480 if (errno != ENOSYS)
1482 return (au_to_header32_tm(rec_size, e_type, e_mod, tm));
1484 return (au_to_header32_ex_tm(rec_size, e_type, e_mod, tm, &aia));
1486 #endif /* HAVE_AUDIT_SYSCALLS */
1489 au_to_header32(int rec_size, au_event_t e_type, au_emod_t e_mod)
1493 if (gettimeofday(&tm, NULL) == -1)
1495 return (au_to_header32_tm(rec_size, e_type, e_mod, tm));
1499 au_to_header64(__unused int rec_size, __unused au_event_t e_type,
1500 __unused au_emod_t e_mod)
1504 if (gettimeofday(&tm, NULL) == -1)
1506 return (au_to_header64_tm(rec_size, e_type, e_mod, tm));
1510 au_to_header(int rec_size, au_event_t e_type, au_emod_t e_mod)
1513 return (au_to_header32(rec_size, e_type, e_mod));
1516 #ifdef HAVE_AUDIT_SYSCALLS
1518 au_to_header_ex(int rec_size, au_event_t e_type, au_emod_t e_mod)
1521 return (au_to_header32_ex(rec_size, e_type, e_mod));
1523 #endif /* HAVE_AUDIT_SYSCALLS */
1524 #endif /* !defined(KERNEL) && !defined(_KERNEL) */
1528 * trailer magic number 2 bytes
1529 * record byte count 4 bytes
1532 au_to_trailer(int rec_size)
1535 u_char *dptr = NULL;
1536 u_int16_t magic = AUT_TRAILER_MAGIC;
1538 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) +
1541 ADD_U_CHAR(dptr, AUT_TRAILER);
1542 ADD_U_INT16(dptr, magic);
1543 ADD_U_INT32(dptr, rec_size);