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.
34 #include <sys/types.h>
36 #include <config/config.h>
41 #ifdef USE_SYS_ENDIAN_H
42 #include <sys/endian.h>
44 #ifdef USE_MACHINE_ENDIAN_H
45 #include <machine/endian.h>
47 #ifdef USE_COMPAT_ENDIAN_H
48 #include <compat/endian.h>
50 #ifdef USE_COMPAT_ENDIAN_ENC_H
51 #include <compat/endian_enc.h>
54 #ifdef HAVE_FULL_QUEUE_H
55 #include <sys/queue.h>
56 #else /* !HAVE_FULL_QUEUE_H */
57 #include <compat/queue.h>
58 #endif /* !HAVE_FULL_QUEUE_H */
60 #include <sys/socket.h>
66 #include <netinet/in.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/ip.h>
76 #include <bsm/audit_internal.h>
77 #include <bsm/libbsm.h>
79 #define GET_TOKEN_AREA(t, dptr, length) do { \
80 (t) = malloc(sizeof(token_t)); \
82 (t)->len = (length); \
83 (dptr) = (t->t_data) = calloc((length), sizeof(u_char)); \
84 if ((dptr) == NULL) { \
90 assert((t) == NULL || (dptr) != NULL); \
95 * success/failure 1 byte
97 * privstr N bytes + 1 (\0 byte)
100 au_to_upriv(char sorf, char *priv)
106 textlen = strlen(priv) + 1;
107 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_char) +
108 sizeof(u_int16_t) + textlen);
111 ADD_U_CHAR(dptr, AUT_UPRIV);
112 ADD_U_CHAR(dptr, sorf);
113 ADD_U_INT16(dptr, textlen);
114 ADD_STRING(dptr, priv, textlen);
120 * privtstrlen 2 bytes
121 * privtstr N bytes + 1
123 * privstr N bytes + 1
126 au_to_privset(char *privtypestr, char *privstr)
128 u_int16_t type_len, priv_len;
132 type_len = strlen(privtypestr) + 1;
133 priv_len = strlen(privstr) + 1;
134 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) +
135 sizeof(u_int16_t) + type_len + priv_len);
138 ADD_U_CHAR(dptr, AUT_PRIV);
139 ADD_U_INT16(dptr, type_len);
140 ADD_STRING(dptr, privtypestr, type_len);
141 ADD_U_INT16(dptr, priv_len);
142 ADD_STRING(dptr, privstr, priv_len);
149 * argument value 4 bytes/8 bytes (32-bit/64-bit value)
150 * text length 2 bytes
151 * text N bytes + 1 terminating NULL byte
154 au_to_arg32(char n, const char *text, u_int32_t v)
160 textlen = strlen(text);
163 GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int32_t) +
164 sizeof(u_int16_t) + textlen);
168 ADD_U_CHAR(dptr, AUT_ARG32);
170 ADD_U_INT32(dptr, v);
171 ADD_U_INT16(dptr, textlen);
172 ADD_STRING(dptr, text, textlen);
178 au_to_arg64(char n, const char *text, u_int64_t v)
184 textlen = strlen(text);
187 GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int64_t) +
188 sizeof(u_int16_t) + textlen);
192 ADD_U_CHAR(dptr, AUT_ARG64);
194 ADD_U_INT64(dptr, v);
195 ADD_U_INT16(dptr, textlen);
196 ADD_STRING(dptr, text, textlen);
202 au_to_arg(char n, const char *text, u_int32_t v)
205 return (au_to_arg32(n, text, v));
208 #if defined(_KERNEL) || defined(KERNEL)
211 * file access mode 4 bytes
212 * owner user ID 4 bytes
213 * owner group ID 4 bytes
214 * file system ID 4 bytes
216 * device 4 bytes/8 bytes (32-bit/64-bit)
219 au_to_attr32(struct vnode_au_info *vni)
223 u_int16_t pad0_16 = 0;
224 u_int32_t pad0_32 = 0;
226 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(u_int16_t) +
227 3 * sizeof(u_int32_t) + sizeof(u_int64_t) + sizeof(u_int32_t));
231 ADD_U_CHAR(dptr, AUT_ATTR32);
234 * BSD defines the size for the file mode as 2 bytes; BSM defines 4
237 * XXXRW: Possibly should be conditionally compiled.
239 * XXXRW: Should any conversions take place on the mode?
241 ADD_U_INT16(dptr, pad0_16);
242 ADD_U_INT16(dptr, vni->vn_mode);
244 ADD_U_INT32(dptr, vni->vn_uid);
245 ADD_U_INT32(dptr, vni->vn_gid);
246 ADD_U_INT32(dptr, vni->vn_fsid);
249 * Some systems use 32-bit file ID's, others use 64-bit file IDs.
250 * Attempt to handle both, and let the compiler sort it out. If we
251 * could pick this out at compile-time, it would be better, so as to
252 * avoid the else case below.
254 if (sizeof(vni->vn_fileid) == sizeof(uint32_t)) {
255 ADD_U_INT32(dptr, pad0_32);
256 ADD_U_INT32(dptr, vni->vn_fileid);
257 } else if (sizeof(vni->vn_fileid) == sizeof(uint64_t))
258 ADD_U_INT64(dptr, vni->vn_fileid);
260 ADD_U_INT64(dptr, 0LL);
262 ADD_U_INT32(dptr, vni->vn_dev);
268 au_to_attr64(struct vnode_au_info *vni)
272 u_int16_t pad0_16 = 0;
273 u_int32_t pad0_32 = 0;
275 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(u_int16_t) +
276 3 * sizeof(u_int32_t) + sizeof(u_int64_t) * 2);
280 ADD_U_CHAR(dptr, AUT_ATTR64);
283 * BSD defines the size for the file mode as 2 bytes; BSM defines 4
286 * XXXRW: Possibly should be conditionally compiled.
288 * XXXRW: Should any conversions take place on the mode?
290 ADD_U_INT16(dptr, pad0_16);
291 ADD_U_INT16(dptr, vni->vn_mode);
293 ADD_U_INT32(dptr, vni->vn_uid);
294 ADD_U_INT32(dptr, vni->vn_gid);
295 ADD_U_INT32(dptr, vni->vn_fsid);
298 * Some systems use 32-bit file ID's, other's use 64-bit file IDs.
299 * Attempt to handle both, and let the compiler sort it out. If we
300 * could pick this out at compile-time, it would be better, so as to
301 * avoid the else case below.
303 if (sizeof(vni->vn_fileid) == sizeof(uint32_t)) {
304 ADD_U_INT32(dptr, pad0_32);
305 ADD_U_INT32(dptr, vni->vn_fileid);
306 } else if (sizeof(vni->vn_fileid) == sizeof(uint64_t))
307 ADD_U_INT64(dptr, vni->vn_fileid);
309 ADD_U_INT64(dptr, 0LL);
311 ADD_U_INT64(dptr, vni->vn_dev);
317 au_to_attr(struct vnode_au_info *vni)
320 return (au_to_attr32(vni));
322 #endif /* !(defined(_KERNEL) || defined(KERNEL) */
326 * how to print 1 byte
329 * data items (depends on basic unit)
332 au_to_data(char unit_print, char unit_type, char unit_count, const char *p)
336 size_t datasize, totdata;
338 /* Determine the size of the basic unit. */
342 datasize = AUR_BYTE_SIZE;
346 datasize = AUR_SHORT_SIZE;
351 datasize = AUR_INT32_SIZE;
355 datasize = AUR_INT64_SIZE;
363 totdata = datasize * unit_count;
365 GET_TOKEN_AREA(t, dptr, 4 * sizeof(u_char) + totdata);
370 * XXXRW: We should be byte-swapping each data item for multi-byte
373 ADD_U_CHAR(dptr, AUT_DATA);
374 ADD_U_CHAR(dptr, unit_print);
375 ADD_U_CHAR(dptr, unit_type);
376 ADD_U_CHAR(dptr, unit_count);
377 ADD_MEM(dptr, p, totdata);
386 * return value 4 bytes
389 au_to_exit(int retval, int err)
394 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(u_int32_t));
398 ADD_U_CHAR(dptr, AUT_EXIT);
399 ADD_U_INT32(dptr, err);
400 ADD_U_INT32(dptr, retval);
408 au_to_groups(int *groups)
411 return (au_to_newgroups(AUDIT_MAX_GROUPS, (gid_t *)groups));
416 * number groups 2 bytes
417 * group list count * 4 bytes
420 au_to_newgroups(u_int16_t n, gid_t *groups)
426 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) +
427 n * sizeof(u_int32_t));
431 ADD_U_CHAR(dptr, AUT_NEWGROUPS);
432 ADD_U_INT16(dptr, n);
433 for (i = 0; i < n; i++)
434 ADD_U_INT32(dptr, groups[i]);
441 * internet address 4 bytes
444 au_to_in_addr(struct in_addr *internet_addr)
449 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(uint32_t));
453 ADD_U_CHAR(dptr, AUT_IN_ADDR);
454 ADD_MEM(dptr, &internet_addr->s_addr, sizeof(uint32_t));
461 * address type/length 4 bytes
465 au_to_in_addr_ex(struct in6_addr *internet_addr)
469 u_int32_t type = AU_IPv6;
471 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 5 * sizeof(uint32_t));
475 ADD_U_CHAR(dptr, AUT_IN_ADDR_EX);
476 ADD_U_INT32(dptr, type);
477 ADD_MEM(dptr, internet_addr, 4 * sizeof(uint32_t));
486 * The IP header should be submitted in network byte order.
489 au_to_ip(struct ip *ip)
494 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(struct ip));
498 ADD_U_CHAR(dptr, AUT_IP);
499 ADD_MEM(dptr, ip, sizeof(struct ip));
506 * object ID type 1 byte
510 au_to_ipc(char type, int id)
515 GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int32_t));
519 ADD_U_CHAR(dptr, AUT_IPC);
520 ADD_U_CHAR(dptr, type);
521 ADD_U_INT32(dptr, id);
528 * owner user ID 4 bytes
529 * owner group ID 4 bytes
530 * creator user ID 4 bytes
531 * creator group ID 4 bytes
532 * access mode 4 bytes
533 * slot sequence # 4 bytes
537 au_to_ipc_perm(struct ipc_perm *perm)
543 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 12 * sizeof(u_int16_t) +
548 ADD_U_CHAR(dptr, AUT_IPC_PERM);
551 * Systems vary significantly in what types they use in struct
552 * ipc_perm; at least a few still use 16-bit uid's and gid's, so
553 * allow for that, as BSM define 32-bit values here.
554 * Some systems define the sizes for ipc_perm members as 2 bytes;
555 * BSM defines 4 so pad with 0.
557 * XXXRW: Possibly shoulid be conditionally compiled, and more cases
558 * need to be handled.
560 if (sizeof(perm->uid) != sizeof(u_int32_t)) {
561 ADD_U_INT16(dptr, pad0);
562 ADD_U_INT16(dptr, perm->uid);
563 ADD_U_INT16(dptr, pad0);
564 ADD_U_INT16(dptr, perm->gid);
565 ADD_U_INT16(dptr, pad0);
566 ADD_U_INT16(dptr, perm->cuid);
567 ADD_U_INT16(dptr, pad0);
568 ADD_U_INT16(dptr, perm->cgid);
570 ADD_U_INT32(dptr, perm->uid);
571 ADD_U_INT32(dptr, perm->gid);
572 ADD_U_INT32(dptr, perm->cuid);
573 ADD_U_INT32(dptr, perm->cgid);
576 ADD_U_INT16(dptr, pad0);
577 ADD_U_INT16(dptr, perm->mode);
579 ADD_U_INT16(dptr, pad0);
581 #ifdef HAVE_IPC_PERM___SEQ
582 ADD_U_INT16(dptr, perm->__seq);
583 #else /* HAVE_IPC_PERM___SEQ */
584 #ifdef HAVE_IPC_PERM__SEQ
585 ADD_U_INT16(dptr, perm->_seq);
586 #else /* HAVE_IPC_PERM__SEQ */
587 ADD_U_INT16(dptr, perm->seq);
588 #endif /* HAVE_IPC_PERM__SEQ */
589 #endif /* HAVE_IPC_PERM___SEQ */
591 #ifdef HAVE_IPC_PERM___KEY
592 ADD_U_INT32(dptr, perm->__key);
593 #else /* HAVE_IPC_PERM___KEY */
594 #ifdef HAVE_IPC_PERM__KEY
595 ADD_U_INT32(dptr, perm->_key);
596 #else /* HAVE_IPC_PERM__KEY */
597 ADD_U_INT32(dptr, perm->key);
598 #endif /* HAVE_IPC_PERM__KEY */
599 #endif /* HAVE_IPC_PERM___KEY */
606 * port IP address 2 bytes
609 au_to_iport(u_int16_t iport)
614 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t));
618 ADD_U_CHAR(dptr, AUT_IPORT);
619 ADD_U_INT16(dptr, iport);
630 au_to_opaque(const char *data, u_int16_t bytes)
635 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) + bytes);
639 ADD_U_CHAR(dptr, AUT_OPAQUE);
640 ADD_U_INT16(dptr, bytes);
641 ADD_MEM(dptr, data, bytes);
648 * seconds of time 4 bytes
649 * milliseconds of time 4 bytes
650 * file name len 2 bytes
651 * file pathname N bytes + 1 terminating NULL byte
654 au_to_file(const char *file, struct timeval tm)
661 filelen = strlen(file);
664 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(u_int32_t) +
665 sizeof(u_int16_t) + filelen);
669 timems = tm.tv_usec/1000;
671 ADD_U_CHAR(dptr, AUT_OTHER_FILE32);
672 ADD_U_INT32(dptr, tm.tv_sec);
673 ADD_U_INT32(dptr, timems); /* We need time in ms. */
674 ADD_U_INT16(dptr, filelen);
675 ADD_STRING(dptr, file, filelen);
682 * text length 2 bytes
683 * text N bytes + 1 terminating NULL byte
686 au_to_text(const char *text)
692 textlen = strlen(text);
695 /* XXXRW: Should validate length against token size limit. */
697 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) + textlen);
701 ADD_U_CHAR(dptr, AUT_TEXT);
702 ADD_U_INT16(dptr, textlen);
703 ADD_STRING(dptr, text, textlen);
710 * path length 2 bytes
711 * path N bytes + 1 terminating NULL byte
714 au_to_path(const char *text)
720 textlen = strlen(text);
723 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) + textlen);
727 ADD_U_CHAR(dptr, AUT_PATH);
728 ADD_U_INT16(dptr, textlen);
729 ADD_STRING(dptr, text, textlen);
737 * effective user ID 4 bytes
738 * effective group ID 4 bytes
739 * real user ID 4 bytes
740 * real group ID 4 bytes
744 * port ID 4 bytes/8 bytes (32-bit/64-bit value)
745 * machine address 4 bytes
748 au_to_process32(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
749 pid_t pid, au_asid_t sid, au_tid_t *tid)
754 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 9 * sizeof(u_int32_t));
758 ADD_U_CHAR(dptr, AUT_PROCESS32);
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_INT32(dptr, tid->port);
769 * Note: Solaris will write out IPv6 addresses here as a 32-bit
770 * address type and 16 bytes of address, but for IPv4 addresses it
771 * simply writes the 4-byte address directly. We support only IPv4
772 * addresses for process32 tokens.
774 ADD_MEM(dptr, &tid->machine, sizeof(u_int32_t));
780 au_to_process64(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
781 pid_t pid, au_asid_t sid, au_tid_t *tid)
786 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 8 * sizeof(u_int32_t) +
791 ADD_U_CHAR(dptr, AUT_PROCESS64);
792 ADD_U_INT32(dptr, auid);
793 ADD_U_INT32(dptr, euid);
794 ADD_U_INT32(dptr, egid);
795 ADD_U_INT32(dptr, ruid);
796 ADD_U_INT32(dptr, rgid);
797 ADD_U_INT32(dptr, pid);
798 ADD_U_INT32(dptr, sid);
799 ADD_U_INT64(dptr, tid->port);
802 * Note: Solaris will write out IPv6 addresses here as a 32-bit
803 * address type and 16 bytes of address, but for IPv4 addresses it
804 * simply writes the 4-byte address directly. We support only IPv4
805 * addresses for process64 tokens.
807 ADD_MEM(dptr, &tid->machine, sizeof(u_int32_t));
813 au_to_process(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
814 pid_t pid, au_asid_t sid, au_tid_t *tid)
817 return (au_to_process32(auid, euid, egid, ruid, rgid, pid, sid,
824 * effective user ID 4 bytes
825 * effective group ID 4 bytes
826 * real user ID 4 bytes
827 * real group ID 4 bytes
831 * port ID 4 bytes/8 bytes (32-bit/64-bit value)
832 * address type-len 4 bytes
833 * machine address 16 bytes
836 au_to_process32_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
837 gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
842 if (tid->at_type == AU_IPv4)
843 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
844 10 * sizeof(u_int32_t));
845 else if (tid->at_type == AU_IPv6)
846 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
847 13 * sizeof(u_int32_t));
855 ADD_U_CHAR(dptr, AUT_PROCESS32_EX);
856 ADD_U_INT32(dptr, auid);
857 ADD_U_INT32(dptr, euid);
858 ADD_U_INT32(dptr, egid);
859 ADD_U_INT32(dptr, ruid);
860 ADD_U_INT32(dptr, rgid);
861 ADD_U_INT32(dptr, pid);
862 ADD_U_INT32(dptr, sid);
863 ADD_U_INT32(dptr, tid->at_port);
864 ADD_U_INT32(dptr, tid->at_type);
865 ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
866 if (tid->at_type == AU_IPv6) {
867 ADD_MEM(dptr, &tid->at_addr[1], sizeof(u_int32_t));
868 ADD_MEM(dptr, &tid->at_addr[2], sizeof(u_int32_t));
869 ADD_MEM(dptr, &tid->at_addr[3], sizeof(u_int32_t));
876 au_to_process64_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
877 gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
882 if (tid->at_type == AU_IPv4)
883 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
884 7 * sizeof(u_int32_t) + sizeof(u_int64_t) +
885 2 * sizeof(u_int32_t));
886 else if (tid->at_type == AU_IPv6)
887 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
888 7 * sizeof(u_int32_t) + sizeof(u_int64_t) +
889 5 * sizeof(u_int32_t));
897 ADD_U_CHAR(dptr, AUT_PROCESS64_EX);
898 ADD_U_INT32(dptr, auid);
899 ADD_U_INT32(dptr, euid);
900 ADD_U_INT32(dptr, egid);
901 ADD_U_INT32(dptr, ruid);
902 ADD_U_INT32(dptr, rgid);
903 ADD_U_INT32(dptr, pid);
904 ADD_U_INT32(dptr, sid);
905 ADD_U_INT64(dptr, tid->at_port);
906 ADD_U_INT32(dptr, tid->at_type);
907 ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
908 if (tid->at_type == AU_IPv6) {
909 ADD_MEM(dptr, &tid->at_addr[1], sizeof(u_int32_t));
910 ADD_MEM(dptr, &tid->at_addr[2], sizeof(u_int32_t));
911 ADD_MEM(dptr, &tid->at_addr[3], sizeof(u_int32_t));
918 au_to_process_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
919 gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
922 return (au_to_process32_ex(auid, euid, egid, ruid, rgid, pid, sid,
928 * error status 1 byte
929 * return value 4 bytes/8 bytes (32-bit/64-bit value)
932 au_to_return32(char status, u_int32_t ret)
937 GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int32_t));
941 ADD_U_CHAR(dptr, AUT_RETURN32);
942 ADD_U_CHAR(dptr, status);
943 ADD_U_INT32(dptr, ret);
949 au_to_return64(char status, u_int64_t ret)
954 GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int64_t));
958 ADD_U_CHAR(dptr, AUT_RETURN64);
959 ADD_U_CHAR(dptr, status);
960 ADD_U_INT64(dptr, ret);
966 au_to_return(char status, u_int32_t ret)
969 return (au_to_return32(status, ret));
974 * sequence number 4 bytes
977 au_to_seq(long audit_count)
982 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t));
986 ADD_U_CHAR(dptr, AUT_SEQ);
987 ADD_U_INT32(dptr, audit_count);
994 * socket domain 2 bytes
995 * socket type 2 bytes
996 * address type 2 byte
998 * local address 4 bytes/16 bytes (IPv4/IPv6 address)
999 * remote port 2 bytes
1000 * remote address 4 bytes/16 bytes (IPv4/IPv6 address)
1002 * Domain and type arguments to this routine are assumed to already have been
1003 * converted to the BSM constant space, so we don't do that here.
1006 au_to_socket_ex(u_short so_domain, u_short so_type,
1007 struct sockaddr *sa_local, struct sockaddr *sa_remote)
1010 u_char *dptr = NULL;
1011 struct sockaddr_in *sin;
1012 struct sockaddr_in6 *sin6;
1014 if (so_domain == AF_INET)
1015 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
1016 5 * sizeof(u_int16_t) + 2 * sizeof(u_int32_t));
1017 else if (so_domain == AF_INET6)
1018 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
1019 5 * sizeof(u_int16_t) + 8 * sizeof(u_int32_t));
1027 ADD_U_CHAR(dptr, AUT_SOCKET_EX);
1028 ADD_U_INT16(dptr, au_domain_to_bsm(so_domain));
1029 ADD_U_INT16(dptr, au_socket_type_to_bsm(so_type));
1030 if (so_domain == AF_INET) {
1031 ADD_U_INT16(dptr, AU_IPv4);
1032 sin = (struct sockaddr_in *)sa_local;
1033 ADD_MEM(dptr, &sin->sin_port, sizeof(uint16_t));
1034 ADD_MEM(dptr, &sin->sin_addr.s_addr, sizeof(uint32_t));
1035 sin = (struct sockaddr_in *)sa_remote;
1036 ADD_MEM(dptr, &sin->sin_port, sizeof(uint16_t));
1037 ADD_MEM(dptr, &sin->sin_addr.s_addr, sizeof(uint32_t));
1039 ADD_U_INT16(dptr, AU_IPv6);
1040 sin6 = (struct sockaddr_in6 *)sa_local;
1041 ADD_MEM(dptr, &sin6->sin6_port, sizeof(uint16_t));
1042 ADD_MEM(dptr, &sin6->sin6_addr, 4 * sizeof(uint32_t));
1043 sin6 = (struct sockaddr_in6 *)sa_remote;
1044 ADD_MEM(dptr, &sin6->sin6_port, sizeof(uint16_t));
1045 ADD_MEM(dptr, &sin6->sin6_addr, 4 * sizeof(uint32_t));
1053 * socket family 2 bytes
1054 * path (up to) 104 bytes + NULL (NULL terminated string)
1057 au_to_sock_unix(struct sockaddr_un *so)
1062 GET_TOKEN_AREA(t, dptr, 3 * sizeof(u_char) + strlen(so->sun_path) + 1);
1066 ADD_U_CHAR(dptr, AUT_SOCKUNIX);
1067 /* BSM token has two bytes for family */
1068 ADD_U_CHAR(dptr, 0);
1069 ADD_U_CHAR(dptr, so->sun_family);
1070 ADD_STRING(dptr, so->sun_path, strlen(so->sun_path) + 1);
1077 * socket family 2 bytes
1078 * local port 2 bytes
1079 * socket address 4 bytes
1082 au_to_sock_inet32(struct sockaddr_in *so)
1085 u_char *dptr = NULL;
1088 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(uint16_t) +
1093 ADD_U_CHAR(dptr, AUT_SOCKINET32);
1095 * BSM defines the family field as 16 bits, but many operating
1096 * systems have an 8-bit sin_family field. Extend to 16 bits before
1097 * writing into the token. Assume that both the port and the address
1098 * in the sockaddr_in are already in network byte order, but family
1099 * is in local byte order.
1101 * XXXRW: Should a name space conversion be taking place on the value
1104 family = so->sin_family;
1105 ADD_U_INT16(dptr, family);
1106 ADD_MEM(dptr, &so->sin_port, sizeof(uint16_t));
1107 ADD_MEM(dptr, &so->sin_addr.s_addr, sizeof(uint32_t));
1113 au_to_sock_inet128(struct sockaddr_in6 *so)
1116 u_char *dptr = NULL;
1118 GET_TOKEN_AREA(t, dptr, 3 * sizeof(u_char) + sizeof(u_int16_t) +
1119 4 * sizeof(u_int32_t));
1123 ADD_U_CHAR(dptr, AUT_SOCKINET128);
1125 * In BSD, sin6_family is one octet, but BSM defines the token to
1126 * store two. So we copy in a 0 first. XXXRW: Possibly should be
1127 * conditionally compiled.
1129 ADD_U_CHAR(dptr, 0);
1130 ADD_U_CHAR(dptr, so->sin6_family);
1132 ADD_U_INT16(dptr, so->sin6_port);
1133 ADD_MEM(dptr, &so->sin6_addr, 4 * sizeof(uint32_t));
1139 au_to_sock_inet(struct sockaddr_in *so)
1142 return (au_to_sock_inet32(so));
1148 * effective user ID 4 bytes
1149 * effective group ID 4 bytes
1150 * real user ID 4 bytes
1151 * real group ID 4 bytes
1152 * process ID 4 bytes
1153 * session ID 4 bytes
1155 * port ID 4 bytes/8 bytes (32-bit/64-bit value)
1156 * machine address 4 bytes
1159 au_to_subject32(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
1160 pid_t pid, au_asid_t sid, au_tid_t *tid)
1163 u_char *dptr = NULL;
1165 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 9 * sizeof(u_int32_t));
1169 ADD_U_CHAR(dptr, AUT_SUBJECT32);
1170 ADD_U_INT32(dptr, auid);
1171 ADD_U_INT32(dptr, euid);
1172 ADD_U_INT32(dptr, egid);
1173 ADD_U_INT32(dptr, ruid);
1174 ADD_U_INT32(dptr, rgid);
1175 ADD_U_INT32(dptr, pid);
1176 ADD_U_INT32(dptr, sid);
1177 ADD_U_INT32(dptr, tid->port);
1178 ADD_MEM(dptr, &tid->machine, sizeof(u_int32_t));
1184 au_to_subject64(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
1185 pid_t pid, au_asid_t sid, au_tid_t *tid)
1188 u_char *dptr = NULL;
1190 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 7 * sizeof(u_int32_t) +
1191 sizeof(u_int64_t) + sizeof(u_int32_t));
1195 ADD_U_CHAR(dptr, AUT_SUBJECT64);
1196 ADD_U_INT32(dptr, auid);
1197 ADD_U_INT32(dptr, euid);
1198 ADD_U_INT32(dptr, egid);
1199 ADD_U_INT32(dptr, ruid);
1200 ADD_U_INT32(dptr, rgid);
1201 ADD_U_INT32(dptr, pid);
1202 ADD_U_INT32(dptr, sid);
1203 ADD_U_INT64(dptr, tid->port);
1204 ADD_MEM(dptr, &tid->machine, sizeof(u_int32_t));
1210 au_to_subject(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
1211 pid_t pid, au_asid_t sid, au_tid_t *tid)
1214 return (au_to_subject32(auid, euid, egid, ruid, rgid, pid, sid,
1221 * effective user ID 4 bytes
1222 * effective group ID 4 bytes
1223 * real user ID 4 bytes
1224 * real group ID 4 bytes
1225 * process ID 4 bytes
1226 * session ID 4 bytes
1228 * port ID 4 bytes/8 bytes (32-bit/64-bit value)
1229 * address type/length 4 bytes
1230 * machine address 16 bytes
1233 au_to_subject32_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
1234 gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
1237 u_char *dptr = NULL;
1239 if (tid->at_type == AU_IPv4)
1240 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 10 *
1242 else if (tid->at_type == AU_IPv6)
1243 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 13 *
1252 ADD_U_CHAR(dptr, AUT_SUBJECT32_EX);
1253 ADD_U_INT32(dptr, auid);
1254 ADD_U_INT32(dptr, euid);
1255 ADD_U_INT32(dptr, egid);
1256 ADD_U_INT32(dptr, ruid);
1257 ADD_U_INT32(dptr, rgid);
1258 ADD_U_INT32(dptr, pid);
1259 ADD_U_INT32(dptr, sid);
1260 ADD_U_INT32(dptr, tid->at_port);
1261 ADD_U_INT32(dptr, tid->at_type);
1262 if (tid->at_type == AU_IPv6)
1263 ADD_MEM(dptr, &tid->at_addr[0], 4 * sizeof(u_int32_t));
1265 ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
1271 au_to_subject64_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
1272 gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
1275 u_char *dptr = NULL;
1277 if (tid->at_type == AU_IPv4)
1278 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
1279 7 * sizeof(u_int32_t) + sizeof(u_int64_t) +
1280 2 * sizeof(u_int32_t));
1281 else if (tid->at_type == AU_IPv6)
1282 GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
1283 7 * sizeof(u_int32_t) + sizeof(u_int64_t) +
1284 5 * sizeof(u_int32_t));
1292 ADD_U_CHAR(dptr, AUT_SUBJECT64_EX);
1293 ADD_U_INT32(dptr, auid);
1294 ADD_U_INT32(dptr, euid);
1295 ADD_U_INT32(dptr, egid);
1296 ADD_U_INT32(dptr, ruid);
1297 ADD_U_INT32(dptr, rgid);
1298 ADD_U_INT32(dptr, pid);
1299 ADD_U_INT32(dptr, sid);
1300 ADD_U_INT64(dptr, tid->at_port);
1301 ADD_U_INT32(dptr, tid->at_type);
1302 if (tid->at_type == AU_IPv6)
1303 ADD_MEM(dptr, &tid->at_addr[0], 4 * sizeof(u_int32_t));
1305 ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
1311 au_to_subject_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
1312 gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
1315 return (au_to_subject32_ex(auid, euid, egid, ruid, rgid, pid, sid,
1319 #if !defined(_KERNEL) && !defined(KERNEL) && defined(HAVE_AUDIT_SYSCALLS)
1321 * Collects audit information for the current process and creates a subject
1328 auditinfo_addr_t aia;
1331 * Try to use getaudit_addr(2) first. If this kernel does not support
1332 * it, then fall back on to getaudit(2).
1334 if (getaudit_addr(&aia, sizeof(aia)) != 0) {
1335 if (errno == ENOSYS) {
1336 if (getaudit(&auinfo) != 0)
1338 return (au_to_subject32(auinfo.ai_auid, geteuid(),
1339 getegid(), getuid(), getgid(), getpid(),
1340 auinfo.ai_asid, &auinfo.ai_termid));
1342 /* getaudit_addr(2) failed for some other reason. */
1347 return (au_to_subject32_ex(aia.ai_auid, geteuid(), getegid(), getuid(),
1348 getgid(), getpid(), aia.ai_asid, &aia.ai_termid));
1355 * text count null-terminated strings
1358 au_to_exec_args(char **argv)
1361 u_char *dptr = NULL;
1362 const char *nextarg;
1368 while (nextarg != NULL) {
1371 nextlen = strlen(nextarg);
1372 totlen += nextlen + 1;
1374 nextarg = *(argv + count);
1377 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) + totlen);
1381 ADD_U_CHAR(dptr, AUT_EXEC_ARGS);
1382 ADD_U_INT32(dptr, count);
1384 for (i = 0; i < count; i++) {
1385 nextarg = *(argv + i);
1386 ADD_MEM(dptr, nextarg, strlen(nextarg) + 1);
1395 * text count null-terminated strings
1398 au_to_exec_env(char **envp)
1401 u_char *dptr = NULL;
1404 const char *nextenv;
1408 while (nextenv != NULL) {
1411 nextlen = strlen(nextenv);
1412 totlen += nextlen + 1;
1414 nextenv = *(envp + count);
1417 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) + totlen);
1421 ADD_U_CHAR(dptr, AUT_EXEC_ENV);
1422 ADD_U_INT32(dptr, count);
1424 for (i = 0; i < count; i++) {
1425 nextenv = *(envp + i);
1426 ADD_MEM(dptr, nextenv, strlen(nextenv) + 1);
1434 * zonename length 2 bytes
1435 * zonename N bytes + 1 terminating NULL byte
1438 au_to_zonename(const char *zonename)
1440 u_char *dptr = NULL;
1444 textlen = strlen(zonename) + 1;
1445 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) + textlen);
1449 ADD_U_CHAR(dptr, AUT_ZONENAME);
1450 ADD_U_INT16(dptr, textlen);
1451 ADD_STRING(dptr, zonename, textlen);
1457 * record byte count 4 bytes
1458 * version # 1 byte [2]
1459 * event type 2 bytes
1460 * event modifier 2 bytes
1461 * seconds of time 4 bytes/8 bytes (32-bit/64-bit value)
1462 * milliseconds of time 4 bytes/8 bytes (32-bit/64-bit value)
1465 au_to_header32_tm(int rec_size, au_event_t e_type, au_emod_t e_mod,
1469 u_char *dptr = NULL;
1472 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) +
1473 sizeof(u_char) + 2 * sizeof(u_int16_t) + 2 * sizeof(u_int32_t));
1477 ADD_U_CHAR(dptr, AUT_HEADER32);
1478 ADD_U_INT32(dptr, rec_size);
1479 ADD_U_CHAR(dptr, AUDIT_HEADER_VERSION_OPENBSM);
1480 ADD_U_INT16(dptr, e_type);
1481 ADD_U_INT16(dptr, e_mod);
1483 timems = tm.tv_usec/1000;
1484 /* Add the timestamp */
1485 ADD_U_INT32(dptr, tm.tv_sec);
1486 ADD_U_INT32(dptr, timems); /* We need time in ms. */
1493 * record byte count 4 bytes
1494 * version # 1 byte [2]
1495 * event type 2 bytes
1496 * event modifier 2 bytes
1497 * address type/length 4 bytes
1498 * machine address 4 bytes/16 bytes (IPv4/IPv6 address)
1499 * seconds of time 4 bytes/8 bytes (32-bit/64-bit value)
1500 * milliseconds of time 4 bytes/8 bytes (32-bit/64-bit value)
1503 au_to_header32_ex_tm(int rec_size, au_event_t e_type, au_emod_t e_mod,
1504 struct timeval tm, struct auditinfo_addr *aia)
1507 u_char *dptr = NULL;
1511 tid = &aia->ai_termid;
1512 if (tid->at_type != AU_IPv4 && tid->at_type != AU_IPv6)
1514 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) +
1515 sizeof(u_char) + 2 * sizeof(u_int16_t) + 3 *
1516 sizeof(u_int32_t) + tid->at_type);
1520 ADD_U_CHAR(dptr, AUT_HEADER32_EX);
1521 ADD_U_INT32(dptr, rec_size);
1522 ADD_U_CHAR(dptr, AUDIT_HEADER_VERSION_OPENBSM);
1523 ADD_U_INT16(dptr, e_type);
1524 ADD_U_INT16(dptr, e_mod);
1526 ADD_U_INT32(dptr, tid->at_type);
1527 if (tid->at_type == AU_IPv6)
1528 ADD_MEM(dptr, &tid->at_addr[0], 4 * sizeof(u_int32_t));
1530 ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
1531 timems = tm.tv_usec/1000;
1532 /* Add the timestamp */
1533 ADD_U_INT32(dptr, tm.tv_sec);
1534 ADD_U_INT32(dptr, timems); /* We need time in ms. */
1540 au_to_header64_tm(int rec_size, au_event_t e_type, au_emod_t e_mod,
1544 u_char *dptr = NULL;
1547 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) +
1548 sizeof(u_char) + 2 * sizeof(u_int16_t) + 2 * sizeof(u_int64_t));
1552 ADD_U_CHAR(dptr, AUT_HEADER64);
1553 ADD_U_INT32(dptr, rec_size);
1554 ADD_U_CHAR(dptr, AUDIT_HEADER_VERSION_OPENBSM);
1555 ADD_U_INT16(dptr, e_type);
1556 ADD_U_INT16(dptr, e_mod);
1558 timems = tm.tv_usec/1000;
1559 /* Add the timestamp */
1560 ADD_U_INT64(dptr, tm.tv_sec);
1561 ADD_U_INT64(dptr, timems); /* We need time in ms. */
1566 #if !defined(KERNEL) && !defined(_KERNEL)
1567 #ifdef HAVE_AUDIT_SYSCALLS
1569 au_to_header32_ex(int rec_size, au_event_t e_type, au_emod_t e_mod)
1572 struct auditinfo_addr aia;
1574 if (gettimeofday(&tm, NULL) == -1)
1576 if (audit_get_kaudit(&aia, sizeof(aia)) != 0) {
1577 if (errno != ENOSYS)
1579 return (au_to_header32_tm(rec_size, e_type, e_mod, tm));
1581 return (au_to_header32_ex_tm(rec_size, e_type, e_mod, tm, &aia));
1583 #endif /* HAVE_AUDIT_SYSCALLS */
1586 au_to_header32(int rec_size, au_event_t e_type, au_emod_t e_mod)
1590 if (gettimeofday(&tm, NULL) == -1)
1592 return (au_to_header32_tm(rec_size, e_type, e_mod, tm));
1596 au_to_header64(__unused int rec_size, __unused au_event_t e_type,
1597 __unused au_emod_t e_mod)
1601 if (gettimeofday(&tm, NULL) == -1)
1603 return (au_to_header64_tm(rec_size, e_type, e_mod, tm));
1607 au_to_header(int rec_size, au_event_t e_type, au_emod_t e_mod)
1610 return (au_to_header32(rec_size, e_type, e_mod));
1613 #ifdef HAVE_AUDIT_SYSCALLS
1615 au_to_header_ex(int rec_size, au_event_t e_type, au_emod_t e_mod)
1618 return (au_to_header32_ex(rec_size, e_type, e_mod));
1620 #endif /* HAVE_AUDIT_SYSCALLS */
1621 #endif /* !defined(KERNEL) && !defined(_KERNEL) */
1625 * trailer magic number 2 bytes
1626 * record byte count 4 bytes
1629 au_to_trailer(int rec_size)
1632 u_char *dptr = NULL;
1633 u_int16_t magic = AUT_TRAILER_MAGIC;
1635 GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) +
1640 ADD_U_CHAR(dptr, AUT_TRAILER);
1641 ADD_U_INT16(dptr, magic);
1642 ADD_U_INT32(dptr, rec_size);