bsm_token.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 2004-2009 Apple Inc.
  5. * Copyright (c) 2005 SPARTA, Inc.
  6. * All rights reserved.
  7. *
  8. * This code was developed in part by Robert N. M. Watson, Senior Principal
  9. * Scientist, SPARTA, Inc.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of Apple Inc. ("Apple") nor the names of
  20. * its contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  31. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  32. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <sys/param.h>
  36. #include <sys/types.h>
  37. #include <sys/endian.h>
  38. #include <sys/queue.h>
  39. #include <sys/socket.h>
  40. #include <sys/time.h>
  41. #include <sys/ipc.h>
  42. #include <sys/libkern.h>
  43. #include <sys/malloc.h>
  44. #include <sys/un.h>
  45. #include <netinet/in.h>
  46. #include <netinet/in_systm.h>
  47. #include <netinet/ip.h>
  48. #include <bsm/audit.h>
  49. #include <bsm/audit_internal.h>
  50. #include <bsm/audit_record.h>
  51. #include <security/audit/audit.h>
  52. #include <security/audit/audit_private.h>
  53. #define GET_TOKEN_AREA(t, dptr, length) do { \
  54. t = malloc(sizeof(token_t), M_AUDITBSM, M_WAITOK); \
  55. t->t_data = malloc(length, M_AUDITBSM, M_WAITOK | M_ZERO); \
  56. t->len = length; \
  57. dptr = t->t_data; \
  58. } while (0)
  59. /*
  60. * token ID 1 byte
  61. * success/failure 1 byte
  62. * privstrlen 2 bytes
  63. * privstr N bytes + 1 (\0 byte)
  64. */
  65. token_t *
  66. au_to_upriv(char sorf, char *priv)
  67. {
  68. u_int16_t textlen;
  69. u_char *dptr;
  70. token_t *t;
  71. textlen = strlen(priv) + 1;
  72. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_char) +
  73. sizeof(u_int16_t) + textlen);
  74. ADD_U_CHAR(dptr, AUT_UPRIV);
  75. ADD_U_CHAR(dptr, sorf);
  76. ADD_U_INT16(dptr, textlen);
  77. ADD_STRING(dptr, priv, textlen);
  78. return (t);
  79. }
  80. /*
  81. * token ID 1 byte
  82. * privtstrlen 2 bytes
  83. * privtstr N bytes + 1
  84. * privstrlen 2 bytes
  85. * privstr N bytes + 1
  86. */
  87. token_t *
  88. au_to_privset(char *privtypestr, char *privstr)
  89. {
  90. u_int16_t type_len, priv_len;
  91. u_char *dptr;
  92. token_t *t;
  93. type_len = strlen(privtypestr) + 1;
  94. priv_len = strlen(privstr) + 1;
  95. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) +
  96. sizeof(u_int16_t) + type_len + priv_len);
  97. ADD_U_CHAR(dptr, AUT_PRIV);
  98. ADD_U_INT16(dptr, type_len);
  99. ADD_STRING(dptr, privtypestr, type_len);
  100. ADD_U_INT16(dptr, priv_len);
  101. ADD_STRING(dptr, privstr, priv_len);
  102. return (t);
  103. }
  104. /*
  105. * token ID 1 byte
  106. * argument # 1 byte
  107. * argument value 4 bytes/8 bytes (32-bit/64-bit value)
  108. * text length 2 bytes
  109. * text N bytes + 1 terminating NULL byte
  110. */
  111. token_t *
  112. au_to_arg32(char n, const char *text, u_int32_t v)
  113. {
  114. token_t *t;
  115. u_char *dptr = NULL;
  116. u_int16_t textlen;
  117. textlen = strlen(text);
  118. textlen += 1;
  119. GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int32_t) +
  120. sizeof(u_int16_t) + textlen);
  121. ADD_U_CHAR(dptr, AUT_ARG32);
  122. ADD_U_CHAR(dptr, n);
  123. ADD_U_INT32(dptr, v);
  124. ADD_U_INT16(dptr, textlen);
  125. ADD_STRING(dptr, text, textlen);
  126. return (t);
  127. }
  128. token_t *
  129. au_to_arg64(char n, const char *text, u_int64_t v)
  130. {
  131. token_t *t;
  132. u_char *dptr = NULL;
  133. u_int16_t textlen;
  134. textlen = strlen(text);
  135. textlen += 1;
  136. GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int64_t) +
  137. sizeof(u_int16_t) + textlen);
  138. ADD_U_CHAR(dptr, AUT_ARG64);
  139. ADD_U_CHAR(dptr, n);
  140. ADD_U_INT64(dptr, v);
  141. ADD_U_INT16(dptr, textlen);
  142. ADD_STRING(dptr, text, textlen);
  143. return (t);
  144. }
  145. token_t *
  146. au_to_arg(char n, const char *text, u_int32_t v)
  147. {
  148. return (au_to_arg32(n, text, v));
  149. }
  150. #if defined(_KERNEL) || defined(KERNEL)
  151. /*
  152. * token ID 1 byte
  153. * file access mode 4 bytes
  154. * owner user ID 4 bytes
  155. * owner group ID 4 bytes
  156. * file system ID 4 bytes
  157. * node ID 8 bytes
  158. * device 4 bytes/8 bytes (32-bit/64-bit)
  159. */
  160. token_t *
  161. au_to_attr32(struct vnode_au_info *vni)
  162. {
  163. token_t *t;
  164. u_char *dptr = NULL;
  165. u_int16_t pad0_16 = 0;
  166. u_int32_t pad0_32 = 0;
  167. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(u_int16_t) +
  168. 3 * sizeof(u_int32_t) + sizeof(u_int64_t) + sizeof(u_int32_t));
  169. ADD_U_CHAR(dptr, AUT_ATTR32);
  170. /*
  171. * BSD defines the size for the file mode as 2 bytes; BSM defines 4
  172. * so pad with 0.
  173. *
  174. * XXXRW: Possibly should be conditionally compiled.
  175. *
  176. * XXXRW: Should any conversions take place on the mode?
  177. */
  178. ADD_U_INT16(dptr, pad0_16);
  179. ADD_U_INT16(dptr, vni->vn_mode);
  180. ADD_U_INT32(dptr, vni->vn_uid);
  181. ADD_U_INT32(dptr, vni->vn_gid);
  182. ADD_U_INT32(dptr, vni->vn_fsid);
  183. /*
  184. * Some systems use 32-bit file ID's, others use 64-bit file IDs.
  185. * Attempt to handle both, and let the compiler sort it out. If we
  186. * could pick this out at compile-time, it would be better, so as to
  187. * avoid the else case below.
  188. */
  189. if (sizeof(vni->vn_fileid) == sizeof(uint32_t)) {
  190. ADD_U_INT32(dptr, pad0_32);
  191. ADD_U_INT32(dptr, vni->vn_fileid);
  192. } else if (sizeof(vni->vn_fileid) == sizeof(uint64_t))
  193. ADD_U_INT64(dptr, vni->vn_fileid);
  194. else
  195. ADD_U_INT64(dptr, 0LL);
  196. ADD_U_INT32(dptr, vni->vn_dev);
  197. return (t);
  198. }
  199. token_t *
  200. au_to_attr64(struct vnode_au_info *vni)
  201. {
  202. token_t *t;
  203. u_char *dptr = NULL;
  204. u_int16_t pad0_16 = 0;
  205. u_int32_t pad0_32 = 0;
  206. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(u_int16_t) +
  207. 3 * sizeof(u_int32_t) + sizeof(u_int64_t) * 2);
  208. ADD_U_CHAR(dptr, AUT_ATTR64);
  209. /*
  210. * BSD defines the size for the file mode as 2 bytes; BSM defines 4
  211. * so pad with 0.
  212. *
  213. * XXXRW: Possibly should be conditionally compiled.
  214. *
  215. * XXXRW: Should any conversions take place on the mode?
  216. */
  217. ADD_U_INT16(dptr, pad0_16);
  218. ADD_U_INT16(dptr, vni->vn_mode);
  219. ADD_U_INT32(dptr, vni->vn_uid);
  220. ADD_U_INT32(dptr, vni->vn_gid);
  221. ADD_U_INT32(dptr, vni->vn_fsid);
  222. /*
  223. * Some systems use 32-bit file ID's, other's use 64-bit file IDs.
  224. * Attempt to handle both, and let the compiler sort it out. If we
  225. * could pick this out at compile-time, it would be better, so as to
  226. * avoid the else case below.
  227. */
  228. if (sizeof(vni->vn_fileid) == sizeof(uint32_t)) {
  229. ADD_U_INT32(dptr, pad0_32);
  230. ADD_U_INT32(dptr, vni->vn_fileid);
  231. } else if (sizeof(vni->vn_fileid) == sizeof(uint64_t))
  232. ADD_U_INT64(dptr, vni->vn_fileid);
  233. else
  234. ADD_U_INT64(dptr, 0LL);
  235. ADD_U_INT64(dptr, vni->vn_dev);
  236. return (t);
  237. }
  238. token_t *
  239. au_to_attr(struct vnode_au_info *vni)
  240. {
  241. return (au_to_attr32(vni));
  242. }
  243. #endif /* !(defined(_KERNEL) || defined(KERNEL) */
  244. /*
  245. * token ID 1 byte
  246. * how to print 1 byte
  247. * basic unit 1 byte
  248. * unit count 1 byte
  249. * data items (depends on basic unit)
  250. */
  251. token_t *
  252. au_to_data(char unit_print, char unit_type, char unit_count, const char *p)
  253. {
  254. token_t *t;
  255. u_char *dptr = NULL;
  256. size_t datasize, totdata;
  257. /* Determine the size of the basic unit. */
  258. switch (unit_type) {
  259. case AUR_BYTE:
  260. /* case AUR_CHAR: */
  261. datasize = AUR_BYTE_SIZE;
  262. break;
  263. case AUR_SHORT:
  264. datasize = AUR_SHORT_SIZE;
  265. break;
  266. case AUR_INT32:
  267. /* case AUR_INT: */
  268. datasize = AUR_INT32_SIZE;
  269. break;
  270. case AUR_INT64:
  271. datasize = AUR_INT64_SIZE;
  272. break;
  273. default:
  274. return (NULL);
  275. }
  276. totdata = datasize * unit_count;
  277. GET_TOKEN_AREA(t, dptr, 4 * sizeof(u_char) + totdata);
  278. /*
  279. * XXXRW: We should be byte-swapping each data item for multi-byte
  280. * types.
  281. */
  282. ADD_U_CHAR(dptr, AUT_DATA);
  283. ADD_U_CHAR(dptr, unit_print);
  284. ADD_U_CHAR(dptr, unit_type);
  285. ADD_U_CHAR(dptr, unit_count);
  286. ADD_MEM(dptr, p, totdata);
  287. return (t);
  288. }
  289. /*
  290. * token ID 1 byte
  291. * status 4 bytes
  292. * return value 4 bytes
  293. */
  294. token_t *
  295. au_to_exit(int retval, int err)
  296. {
  297. token_t *t;
  298. u_char *dptr = NULL;
  299. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(u_int32_t));
  300. ADD_U_CHAR(dptr, AUT_EXIT);
  301. ADD_U_INT32(dptr, err);
  302. ADD_U_INT32(dptr, retval);
  303. return (t);
  304. }
  305. /*
  306. */
  307. token_t *
  308. au_to_groups(int *groups)
  309. {
  310. return (au_to_newgroups(AUDIT_MAX_GROUPS, (gid_t *)groups));
  311. }
  312. /*
  313. * token ID 1 byte
  314. * number groups 2 bytes
  315. * group list count * 4 bytes
  316. */
  317. token_t *
  318. au_to_newgroups(u_int16_t n, gid_t *groups)
  319. {
  320. token_t *t;
  321. u_char *dptr = NULL;
  322. int i;
  323. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) +
  324. n * sizeof(u_int32_t));
  325. ADD_U_CHAR(dptr, AUT_NEWGROUPS);
  326. ADD_U_INT16(dptr, n);
  327. for (i = 0; i < n; i++)
  328. ADD_U_INT32(dptr, groups[i]);
  329. return (t);
  330. }
  331. /*
  332. * token ID 1 byte
  333. * internet address 4 bytes
  334. */
  335. token_t *
  336. au_to_in_addr(struct in_addr *internet_addr)
  337. {
  338. token_t *t;
  339. u_char *dptr = NULL;
  340. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(uint32_t));
  341. ADD_U_CHAR(dptr, AUT_IN_ADDR);
  342. ADD_MEM(dptr, &internet_addr->s_addr, sizeof(uint32_t));
  343. return (t);
  344. }
  345. /*
  346. * token ID 1 byte
  347. * address type/length 4 bytes
  348. * address 16 bytes
  349. */
  350. token_t *
  351. au_to_in_addr_ex(struct in6_addr *internet_addr)
  352. {
  353. token_t *t;
  354. u_char *dptr = NULL;
  355. u_int32_t type = AU_IPv6;
  356. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 5 * sizeof(uint32_t));
  357. ADD_U_CHAR(dptr, AUT_IN_ADDR_EX);
  358. ADD_U_INT32(dptr, type);
  359. ADD_MEM(dptr, internet_addr, 4 * sizeof(uint32_t));
  360. return (t);
  361. }
  362. /*
  363. * token ID 1 byte
  364. * ip header 20 bytes
  365. *
  366. * The IP header should be submitted in network byte order.
  367. */
  368. token_t *
  369. au_to_ip(struct ip *ip)
  370. {
  371. token_t *t;
  372. u_char *dptr = NULL;
  373. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(struct ip));
  374. ADD_U_CHAR(dptr, AUT_IP);
  375. ADD_MEM(dptr, ip, sizeof(struct ip));
  376. return (t);
  377. }
  378. /*
  379. * token ID 1 byte
  380. * object ID type 1 byte
  381. * object ID 4 bytes
  382. */
  383. token_t *
  384. au_to_ipc(char type, int id)
  385. {
  386. token_t *t;
  387. u_char *dptr = NULL;
  388. GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int32_t));
  389. ADD_U_CHAR(dptr, AUT_IPC);
  390. ADD_U_CHAR(dptr, type);
  391. ADD_U_INT32(dptr, id);
  392. return (t);
  393. }
  394. /*
  395. * token ID 1 byte
  396. * owner user ID 4 bytes
  397. * owner group ID 4 bytes
  398. * creator user ID 4 bytes
  399. * creator group ID 4 bytes
  400. * access mode 4 bytes
  401. * slot sequence # 4 bytes
  402. * key 4 bytes
  403. */
  404. token_t *
  405. au_to_ipc_perm(struct ipc_perm *perm)
  406. {
  407. token_t *t;
  408. u_char *dptr = NULL;
  409. u_int16_t pad0 = 0;
  410. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 12 * sizeof(u_int16_t) +
  411. sizeof(u_int32_t));
  412. ADD_U_CHAR(dptr, AUT_IPC_PERM);
  413. /*
  414. * Systems vary significantly in what types they use in struct
  415. * ipc_perm; at least a few still use 16-bit uid's and gid's, so
  416. * allow for that, as BSM define 32-bit values here.
  417. * Some systems define the sizes for ipc_perm members as 2 bytes;
  418. * BSM defines 4 so pad with 0.
  419. *
  420. * XXXRW: Possibly shoulid be conditionally compiled, and more cases
  421. * need to be handled.
  422. */
  423. if (sizeof(perm->uid) != sizeof(u_int32_t)) {
  424. ADD_U_INT16(dptr, pad0);
  425. ADD_U_INT16(dptr, perm->uid);
  426. ADD_U_INT16(dptr, pad0);
  427. ADD_U_INT16(dptr, perm->gid);
  428. ADD_U_INT16(dptr, pad0);
  429. ADD_U_INT16(dptr, perm->cuid);
  430. ADD_U_INT16(dptr, pad0);
  431. ADD_U_INT16(dptr, perm->cgid);
  432. } else {
  433. ADD_U_INT32(dptr, perm->uid);
  434. ADD_U_INT32(dptr, perm->gid);
  435. ADD_U_INT32(dptr, perm->cuid);
  436. ADD_U_INT32(dptr, perm->cgid);
  437. }
  438. ADD_U_INT16(dptr, pad0);
  439. ADD_U_INT16(dptr, perm->mode);
  440. ADD_U_INT16(dptr, pad0);
  441. ADD_U_INT16(dptr, perm->seq);
  442. ADD_U_INT32(dptr, perm->key);
  443. return (t);
  444. }
  445. /*
  446. * token ID 1 byte
  447. * port IP address 2 bytes
  448. */
  449. token_t *
  450. au_to_iport(u_int16_t iport)
  451. {
  452. token_t *t;
  453. u_char *dptr = NULL;
  454. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t));
  455. ADD_U_CHAR(dptr, AUT_IPORT);
  456. ADD_U_INT16(dptr, iport);
  457. return (t);
  458. }
  459. /*
  460. * token ID 1 byte
  461. * size 2 bytes
  462. * data size bytes
  463. */
  464. token_t *
  465. au_to_opaque(const char *data, u_int16_t bytes)
  466. {
  467. token_t *t;
  468. u_char *dptr = NULL;
  469. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) + bytes);
  470. ADD_U_CHAR(dptr, AUT_OPAQUE);
  471. ADD_U_INT16(dptr, bytes);
  472. ADD_MEM(dptr, data, bytes);
  473. return (t);
  474. }
  475. /*
  476. * token ID 1 byte
  477. * seconds of time 4 bytes
  478. * milliseconds of time 4 bytes
  479. * file name len 2 bytes
  480. * file pathname N bytes + 1 terminating NULL byte
  481. */
  482. token_t *
  483. au_to_file(const char *file, struct timeval tm)
  484. {
  485. token_t *t;
  486. u_char *dptr = NULL;
  487. u_int16_t filelen;
  488. u_int32_t timems;
  489. filelen = strlen(file);
  490. filelen += 1;
  491. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(u_int32_t) +
  492. sizeof(u_int16_t) + filelen);
  493. timems = tm.tv_usec/1000;
  494. ADD_U_CHAR(dptr, AUT_OTHER_FILE32);
  495. ADD_U_INT32(dptr, tm.tv_sec);
  496. ADD_U_INT32(dptr, timems); /* We need time in ms. */
  497. ADD_U_INT16(dptr, filelen);
  498. ADD_STRING(dptr, file, filelen);
  499. return (t);
  500. }
  501. /*
  502. * token ID 1 byte
  503. * text length 2 bytes
  504. * text N bytes + 1 terminating NULL byte
  505. */
  506. token_t *
  507. au_to_text(const char *text)
  508. {
  509. token_t *t;
  510. u_char *dptr = NULL;
  511. u_int16_t textlen;
  512. textlen = strlen(text);
  513. textlen += 1;
  514. /* XXXRW: Should validate length against token size limit. */
  515. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) + textlen);
  516. ADD_U_CHAR(dptr, AUT_TEXT);
  517. ADD_U_INT16(dptr, textlen);
  518. ADD_STRING(dptr, text, textlen);
  519. return (t);
  520. }
  521. /*
  522. * token ID 1 byte
  523. * path length 2 bytes
  524. * path N bytes + 1 terminating NULL byte
  525. */
  526. token_t *
  527. au_to_path(const char *text)
  528. {
  529. token_t *t;
  530. u_char *dptr = NULL;
  531. u_int16_t textlen;
  532. textlen = strlen(text);
  533. textlen += 1;
  534. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) + textlen);
  535. ADD_U_CHAR(dptr, AUT_PATH);
  536. ADD_U_INT16(dptr, textlen);
  537. ADD_STRING(dptr, text, textlen);
  538. return (t);
  539. }
  540. /*
  541. * token ID 1 byte
  542. * audit ID 4 bytes
  543. * effective user ID 4 bytes
  544. * effective group ID 4 bytes
  545. * real user ID 4 bytes
  546. * real group ID 4 bytes
  547. * process ID 4 bytes
  548. * session ID 4 bytes
  549. * terminal ID
  550. * port ID 4 bytes/8 bytes (32-bit/64-bit value)
  551. * machine address 4 bytes
  552. */
  553. token_t *
  554. au_to_process32(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
  555. pid_t pid, au_asid_t sid, au_tid_t *tid)
  556. {
  557. token_t *t;
  558. u_char *dptr = NULL;
  559. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 9 * sizeof(u_int32_t));
  560. ADD_U_CHAR(dptr, AUT_PROCESS32);
  561. ADD_U_INT32(dptr, auid);
  562. ADD_U_INT32(dptr, euid);
  563. ADD_U_INT32(dptr, egid);
  564. ADD_U_INT32(dptr, ruid);
  565. ADD_U_INT32(dptr, rgid);
  566. ADD_U_INT32(dptr, pid);
  567. ADD_U_INT32(dptr, sid);
  568. ADD_U_INT32(dptr, tid->port);
  569. /*
  570. * Note: Solaris will write out IPv6 addresses here as a 32-bit
  571. * address type and 16 bytes of address, but for IPv4 addresses it
  572. * simply writes the 4-byte address directly. We support only IPv4
  573. * addresses for process32 tokens.
  574. */
  575. ADD_MEM(dptr, &tid->machine, sizeof(u_int32_t));
  576. return (t);
  577. }
  578. token_t *
  579. au_to_process64(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
  580. pid_t pid, au_asid_t sid, au_tid_t *tid)
  581. {
  582. token_t *t;
  583. u_char *dptr = NULL;
  584. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 8 * sizeof(u_int32_t) +
  585. sizeof(u_int64_t));
  586. ADD_U_CHAR(dptr, AUT_PROCESS64);
  587. ADD_U_INT32(dptr, auid);
  588. ADD_U_INT32(dptr, euid);
  589. ADD_U_INT32(dptr, egid);
  590. ADD_U_INT32(dptr, ruid);
  591. ADD_U_INT32(dptr, rgid);
  592. ADD_U_INT32(dptr, pid);
  593. ADD_U_INT32(dptr, sid);
  594. ADD_U_INT64(dptr, tid->port);
  595. /*
  596. * Note: Solaris will write out IPv6 addresses here as a 32-bit
  597. * address type and 16 bytes of address, but for IPv4 addresses it
  598. * simply writes the 4-byte address directly. We support only IPv4
  599. * addresses for process64 tokens.
  600. */
  601. ADD_MEM(dptr, &tid->machine, sizeof(u_int32_t));
  602. return (t);
  603. }
  604. token_t *
  605. au_to_process(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
  606. pid_t pid, au_asid_t sid, au_tid_t *tid)
  607. {
  608. return (au_to_process32(auid, euid, egid, ruid, rgid, pid, sid,
  609. tid));
  610. }
  611. /*
  612. * token ID 1 byte
  613. * audit ID 4 bytes
  614. * effective user ID 4 bytes
  615. * effective group ID 4 bytes
  616. * real user ID 4 bytes
  617. * real group ID 4 bytes
  618. * process ID 4 bytes
  619. * session ID 4 bytes
  620. * terminal ID
  621. * port ID 4 bytes/8 bytes (32-bit/64-bit value)
  622. * address type-len 4 bytes
  623. * machine address 16 bytes
  624. */
  625. token_t *
  626. au_to_process32_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
  627. gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
  628. {
  629. token_t *t;
  630. u_char *dptr = NULL;
  631. KASSERT((tid->at_type == AU_IPv4) || (tid->at_type == AU_IPv6),
  632. ("au_to_process32_ex: type %u", (unsigned int)tid->at_type));
  633. if (tid->at_type == AU_IPv4)
  634. GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
  635. 10 * sizeof(u_int32_t));
  636. else
  637. GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
  638. 13 * sizeof(u_int32_t));
  639. ADD_U_CHAR(dptr, AUT_PROCESS32_EX);
  640. ADD_U_INT32(dptr, auid);
  641. ADD_U_INT32(dptr, euid);
  642. ADD_U_INT32(dptr, egid);
  643. ADD_U_INT32(dptr, ruid);
  644. ADD_U_INT32(dptr, rgid);
  645. ADD_U_INT32(dptr, pid);
  646. ADD_U_INT32(dptr, sid);
  647. ADD_U_INT32(dptr, tid->at_port);
  648. ADD_U_INT32(dptr, tid->at_type);
  649. ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
  650. if (tid->at_type == AU_IPv6) {
  651. ADD_MEM(dptr, &tid->at_addr[1], sizeof(u_int32_t));
  652. ADD_MEM(dptr, &tid->at_addr[2], sizeof(u_int32_t));
  653. ADD_MEM(dptr, &tid->at_addr[3], sizeof(u_int32_t));
  654. }
  655. return (t);
  656. }
  657. token_t *
  658. au_to_process64_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
  659. gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
  660. {
  661. token_t *t;
  662. u_char *dptr = NULL;
  663. if (tid->at_type == AU_IPv4)
  664. GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
  665. 7 * sizeof(u_int32_t) + sizeof(u_int64_t) +
  666. 2 * sizeof(u_int32_t));
  667. else if (tid->at_type == AU_IPv6)
  668. GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
  669. 7 * sizeof(u_int32_t) + sizeof(u_int64_t) +
  670. 5 * sizeof(u_int32_t));
  671. else
  672. panic("au_to_process64_ex: invalidate at_type (%d)",
  673. tid->at_type);
  674. ADD_U_CHAR(dptr, AUT_PROCESS64_EX);
  675. ADD_U_INT32(dptr, auid);
  676. ADD_U_INT32(dptr, euid);
  677. ADD_U_INT32(dptr, egid);
  678. ADD_U_INT32(dptr, ruid);
  679. ADD_U_INT32(dptr, rgid);
  680. ADD_U_INT32(dptr, pid);
  681. ADD_U_INT32(dptr, sid);
  682. ADD_U_INT64(dptr, tid->at_port);
  683. ADD_U_INT32(dptr, tid->at_type);
  684. ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
  685. if (tid->at_type == AU_IPv6) {
  686. ADD_MEM(dptr, &tid->at_addr[1], sizeof(u_int32_t));
  687. ADD_MEM(dptr, &tid->at_addr[2], sizeof(u_int32_t));
  688. ADD_MEM(dptr, &tid->at_addr[3], sizeof(u_int32_t));
  689. }
  690. return (t);
  691. }
  692. token_t *
  693. au_to_process_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
  694. gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
  695. {
  696. return (au_to_process32_ex(auid, euid, egid, ruid, rgid, pid, sid,
  697. tid));
  698. }
  699. token_t *
  700. au_to_rights(cap_rights_t *rightsp)
  701. {
  702. token_t *t;
  703. u_char *dptr;
  704. int i;
  705. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(*rightsp));
  706. ADD_U_CHAR(dptr, AUT_RIGHTS);
  707. for (i = 0; i < nitems(rightsp->cr_rights); i++)
  708. ADD_U_INT64(dptr, rightsp->cr_rights[i]);
  709. return (t);
  710. }
  711. /*
  712. * token ID 1 byte
  713. * error status 1 byte
  714. * return value 4 bytes/8 bytes (32-bit/64-bit value)
  715. */
  716. token_t *
  717. au_to_return32(char status, u_int32_t ret)
  718. {
  719. token_t *t;
  720. u_char *dptr = NULL;
  721. GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int32_t));
  722. ADD_U_CHAR(dptr, AUT_RETURN32);
  723. ADD_U_CHAR(dptr, status);
  724. ADD_U_INT32(dptr, ret);
  725. return (t);
  726. }
  727. token_t *
  728. au_to_return64(char status, u_int64_t ret)
  729. {
  730. token_t *t;
  731. u_char *dptr = NULL;
  732. GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int64_t));
  733. ADD_U_CHAR(dptr, AUT_RETURN64);
  734. ADD_U_CHAR(dptr, status);
  735. ADD_U_INT64(dptr, ret);
  736. return (t);
  737. }
  738. token_t *
  739. au_to_return(char status, u_int32_t ret)
  740. {
  741. return (au_to_return32(status, ret));
  742. }
  743. /*
  744. * token ID 1 byte
  745. * sequence number 4 bytes
  746. */
  747. token_t *
  748. au_to_seq(long audit_count)
  749. {
  750. token_t *t;
  751. u_char *dptr = NULL;
  752. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t));
  753. ADD_U_CHAR(dptr, AUT_SEQ);
  754. ADD_U_INT32(dptr, audit_count);
  755. return (t);
  756. }
  757. /*
  758. * token ID 1 byte
  759. * socket domain 2 bytes
  760. * socket type 2 bytes
  761. * address type 2 byte
  762. * local port 2 bytes
  763. * local address 4 bytes/16 bytes (IPv4/IPv6 address)
  764. * remote port 2 bytes
  765. * remote address 4 bytes/16 bytes (IPv4/IPv6 address)
  766. *
  767. * Domain and type arguments to this routine are assumed to already have been
  768. * converted to the BSM constant space, so we don't do that here.
  769. */
  770. token_t *
  771. au_to_socket_ex(u_short so_domain, u_short so_type,
  772. struct sockaddr *sa_local, struct sockaddr *sa_remote)
  773. {
  774. token_t *t;
  775. u_char *dptr = NULL;
  776. struct sockaddr_in *sin;
  777. struct sockaddr_in6 *sin6;
  778. if (so_domain == AF_INET)
  779. GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
  780. 5 * sizeof(u_int16_t) + 2 * sizeof(u_int32_t));
  781. else if (so_domain == AF_INET6)
  782. GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
  783. 5 * sizeof(u_int16_t) + 8 * sizeof(u_int32_t));
  784. else
  785. return (NULL);
  786. ADD_U_CHAR(dptr, AUT_SOCKET_EX);
  787. ADD_U_INT16(dptr, au_domain_to_bsm(so_domain));
  788. ADD_U_INT16(dptr, au_socket_type_to_bsm(so_type));
  789. if (so_domain == AF_INET) {
  790. ADD_U_INT16(dptr, AU_IPv4);
  791. sin = (struct sockaddr_in *)sa_local;
  792. ADD_MEM(dptr, &sin->sin_port, sizeof(uint16_t));
  793. ADD_MEM(dptr, &sin->sin_addr.s_addr, sizeof(uint32_t));
  794. sin = (struct sockaddr_in *)sa_remote;
  795. ADD_MEM(dptr, &sin->sin_port, sizeof(uint16_t));
  796. ADD_MEM(dptr, &sin->sin_addr.s_addr, sizeof(uint32_t));
  797. } else {
  798. ADD_U_INT16(dptr, AU_IPv6);
  799. sin6 = (struct sockaddr_in6 *)sa_local;
  800. ADD_MEM(dptr, &sin6->sin6_port, sizeof(uint16_t));
  801. ADD_MEM(dptr, &sin6->sin6_addr, 4 * sizeof(uint32_t));
  802. sin6 = (struct sockaddr_in6 *)sa_remote;
  803. ADD_MEM(dptr, &sin6->sin6_port, sizeof(uint16_t));
  804. ADD_MEM(dptr, &sin6->sin6_addr, 4 * sizeof(uint32_t));
  805. }
  806. return (t);
  807. }
  808. /*
  809. * Kernel-specific version of the above function.
  810. *
  811. * XXXRW: Should now use au_to_socket_ex() here.
  812. */
  813. #ifdef _KERNEL
  814. token_t *
  815. kau_to_socket(struct socket_au_info *soi)
  816. {
  817. token_t *t;
  818. u_char *dptr;
  819. u_int16_t so_type;
  820. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(u_int16_t) +
  821. sizeof(u_int32_t) + sizeof(u_int16_t) + sizeof(u_int32_t));
  822. ADD_U_CHAR(dptr, AUT_SOCKET);
  823. /* Coerce the socket type into a short value */
  824. so_type = soi->so_type;
  825. ADD_U_INT16(dptr, so_type);
  826. ADD_U_INT16(dptr, soi->so_lport);
  827. ADD_U_INT32(dptr, soi->so_laddr);
  828. ADD_U_INT16(dptr, soi->so_rport);
  829. ADD_U_INT32(dptr, soi->so_raddr);
  830. return (t);
  831. }
  832. #endif
  833. /*
  834. * token ID 1 byte
  835. * socket family 2 bytes
  836. * path (up to) 104 bytes + NULL (NULL terminated string)
  837. */
  838. token_t *
  839. au_to_sock_unix(struct sockaddr_un *so)
  840. {
  841. token_t *t;
  842. u_char *dptr;
  843. GET_TOKEN_AREA(t, dptr, 3 * sizeof(u_char) + strlen(so->sun_path) + 1);
  844. ADD_U_CHAR(dptr, AUT_SOCKUNIX);
  845. /* BSM token has two bytes for family */
  846. ADD_U_CHAR(dptr, 0);
  847. ADD_U_CHAR(dptr, so->sun_family);
  848. ADD_STRING(dptr, so->sun_path, strlen(so->sun_path) + 1);
  849. return (t);
  850. }
  851. /*
  852. * token ID 1 byte
  853. * socket family 2 bytes
  854. * local port 2 bytes
  855. * socket address 4 bytes
  856. */
  857. token_t *
  858. au_to_sock_inet32(struct sockaddr_in *so)
  859. {
  860. token_t *t;
  861. u_char *dptr = NULL;
  862. uint16_t family;
  863. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(uint16_t) +
  864. sizeof(uint32_t));
  865. ADD_U_CHAR(dptr, AUT_SOCKINET32);
  866. /*
  867. * BSM defines the family field as 16 bits, but many operating
  868. * systems have an 8-bit sin_family field. Extend to 16 bits before
  869. * writing into the token. Assume that both the port and the address
  870. * in the sockaddr_in are already in network byte order, but family
  871. * is in local byte order.
  872. *
  873. * XXXRW: Should a name space conversion be taking place on the value
  874. * of sin_family?
  875. */
  876. family = so->sin_family;
  877. ADD_U_INT16(dptr, family);
  878. ADD_MEM(dptr, &so->sin_port, sizeof(uint16_t));
  879. ADD_MEM(dptr, &so->sin_addr.s_addr, sizeof(uint32_t));
  880. return (t);
  881. }
  882. token_t *
  883. au_to_sock_inet128(struct sockaddr_in6 *so)
  884. {
  885. token_t *t;
  886. u_char *dptr = NULL;
  887. GET_TOKEN_AREA(t, dptr, 3 * sizeof(u_char) + sizeof(u_int16_t) +
  888. 4 * sizeof(u_int32_t));
  889. ADD_U_CHAR(dptr, AUT_SOCKINET128);
  890. /*
  891. * In BSD, sin6_family is one octet, but BSM defines the token to
  892. * store two. So we copy in a 0 first. XXXRW: Possibly should be
  893. * conditionally compiled.
  894. */
  895. ADD_U_CHAR(dptr, 0);
  896. ADD_U_CHAR(dptr, so->sin6_family);
  897. ADD_U_INT16(dptr, so->sin6_port);
  898. ADD_MEM(dptr, &so->sin6_addr, 4 * sizeof(uint32_t));
  899. return (t);
  900. }
  901. token_t *
  902. au_to_sock_inet(struct sockaddr_in *so)
  903. {
  904. return (au_to_sock_inet32(so));
  905. }
  906. /*
  907. * token ID 1 byte
  908. * audit ID 4 bytes
  909. * effective user ID 4 bytes
  910. * effective group ID 4 bytes
  911. * real user ID 4 bytes
  912. * real group ID 4 bytes
  913. * process ID 4 bytes
  914. * session ID 4 bytes
  915. * terminal ID
  916. * port ID 4 bytes/8 bytes (32-bit/64-bit value)
  917. * machine address 4 bytes
  918. */
  919. token_t *
  920. au_to_subject32(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
  921. pid_t pid, au_asid_t sid, au_tid_t *tid)
  922. {
  923. token_t *t;
  924. u_char *dptr = NULL;
  925. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 9 * sizeof(u_int32_t));
  926. ADD_U_CHAR(dptr, AUT_SUBJECT32);
  927. ADD_U_INT32(dptr, auid);
  928. ADD_U_INT32(dptr, euid);
  929. ADD_U_INT32(dptr, egid);
  930. ADD_U_INT32(dptr, ruid);
  931. ADD_U_INT32(dptr, rgid);
  932. ADD_U_INT32(dptr, pid);
  933. ADD_U_INT32(dptr, sid);
  934. ADD_U_INT32(dptr, tid->port);
  935. ADD_MEM(dptr, &tid->machine, sizeof(u_int32_t));
  936. return (t);
  937. }
  938. token_t *
  939. au_to_subject64(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
  940. pid_t pid, au_asid_t sid, au_tid_t *tid)
  941. {
  942. token_t *t;
  943. u_char *dptr = NULL;
  944. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 7 * sizeof(u_int32_t) +
  945. sizeof(u_int64_t) + sizeof(u_int32_t));
  946. ADD_U_CHAR(dptr, AUT_SUBJECT64);
  947. ADD_U_INT32(dptr, auid);
  948. ADD_U_INT32(dptr, euid);
  949. ADD_U_INT32(dptr, egid);
  950. ADD_U_INT32(dptr, ruid);
  951. ADD_U_INT32(dptr, rgid);
  952. ADD_U_INT32(dptr, pid);
  953. ADD_U_INT32(dptr, sid);
  954. ADD_U_INT64(dptr, tid->port);
  955. ADD_MEM(dptr, &tid->machine, sizeof(u_int32_t));
  956. return (t);
  957. }
  958. token_t *
  959. au_to_subject(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
  960. pid_t pid, au_asid_t sid, au_tid_t *tid)
  961. {
  962. return (au_to_subject32(auid, euid, egid, ruid, rgid, pid, sid,
  963. tid));
  964. }
  965. /*
  966. * token ID 1 byte
  967. * audit ID 4 bytes
  968. * effective user ID 4 bytes
  969. * effective group ID 4 bytes
  970. * real user ID 4 bytes
  971. * real group ID 4 bytes
  972. * process ID 4 bytes
  973. * session ID 4 bytes
  974. * terminal ID
  975. * port ID 4 bytes/8 bytes (32-bit/64-bit value)
  976. * address type/length 4 bytes
  977. * machine address 16 bytes
  978. */
  979. token_t *
  980. au_to_subject32_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
  981. gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
  982. {
  983. token_t *t;
  984. u_char *dptr = NULL;
  985. KASSERT((tid->at_type == AU_IPv4) || (tid->at_type == AU_IPv6),
  986. ("au_to_subject32_ex: type %u", (unsigned int)tid->at_type));
  987. if (tid->at_type == AU_IPv4)
  988. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 10 *
  989. sizeof(u_int32_t));
  990. else
  991. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 13 *
  992. sizeof(u_int32_t));
  993. ADD_U_CHAR(dptr, AUT_SUBJECT32_EX);
  994. ADD_U_INT32(dptr, auid);
  995. ADD_U_INT32(dptr, euid);
  996. ADD_U_INT32(dptr, egid);
  997. ADD_U_INT32(dptr, ruid);
  998. ADD_U_INT32(dptr, rgid);
  999. ADD_U_INT32(dptr, pid);
  1000. ADD_U_INT32(dptr, sid);
  1001. ADD_U_INT32(dptr, tid->at_port);
  1002. ADD_U_INT32(dptr, tid->at_type);
  1003. if (tid->at_type == AU_IPv6)
  1004. ADD_MEM(dptr, &tid->at_addr[0], 4 * sizeof(u_int32_t));
  1005. else
  1006. ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
  1007. return (t);
  1008. }
  1009. token_t *
  1010. au_to_subject64_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
  1011. gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
  1012. {
  1013. token_t *t;
  1014. u_char *dptr = NULL;
  1015. KASSERT((tid->at_type == AU_IPv4) || (tid->at_type == AU_IPv6),
  1016. ("au_to_subject64_ex: type %u", (unsigned int)tid->at_type));
  1017. if (tid->at_type == AU_IPv4)
  1018. GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
  1019. 7 * sizeof(u_int32_t) + sizeof(u_int64_t) +
  1020. 2 * sizeof(u_int32_t));
  1021. else
  1022. GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
  1023. 7 * sizeof(u_int32_t) + sizeof(u_int64_t) +
  1024. 5 * sizeof(u_int32_t));
  1025. ADD_U_CHAR(dptr, AUT_SUBJECT64_EX);
  1026. ADD_U_INT32(dptr, auid);
  1027. ADD_U_INT32(dptr, euid);
  1028. ADD_U_INT32(dptr, egid);
  1029. ADD_U_INT32(dptr, ruid);
  1030. ADD_U_INT32(dptr, rgid);
  1031. ADD_U_INT32(dptr, pid);
  1032. ADD_U_INT32(dptr, sid);
  1033. ADD_U_INT64(dptr, tid->at_port);
  1034. ADD_U_INT32(dptr, tid->at_type);
  1035. if (tid->at_type == AU_IPv6)
  1036. ADD_MEM(dptr, &tid->at_addr[0], 4 * sizeof(u_int32_t));
  1037. else
  1038. ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
  1039. return (t);
  1040. }
  1041. token_t *
  1042. au_to_subject_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
  1043. gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
  1044. {
  1045. return (au_to_subject32_ex(auid, euid, egid, ruid, rgid, pid, sid,
  1046. tid));
  1047. }
  1048. #if !defined(_KERNEL) && !defined(KERNEL) && defined(HAVE_AUDIT_SYSCALLS)
  1049. /*
  1050. * Collects audit information for the current process and creates a subject
  1051. * token from it.
  1052. */
  1053. token_t *
  1054. au_to_me(void)
  1055. {
  1056. auditinfo_t auinfo;
  1057. auditinfo_addr_t aia;
  1058. /*
  1059. * Try to use getaudit_addr(2) first. If this kernel does not support
  1060. * it, then fall back on to getaudit(2).
  1061. */
  1062. if (getaudit_addr(&aia, sizeof(aia)) != 0) {
  1063. if (errno == ENOSYS) {
  1064. if (getaudit(&auinfo) != 0)
  1065. return (NULL);
  1066. return (au_to_subject32(auinfo.ai_auid, geteuid(),
  1067. getegid(), getuid(), getgid(), getpid(),
  1068. auinfo.ai_asid, &auinfo.ai_termid));
  1069. } else {
  1070. /* getaudit_addr(2) failed for some other reason. */
  1071. return (NULL);
  1072. }
  1073. }
  1074. return (au_to_subject32_ex(aia.ai_auid, geteuid(), getegid(), getuid(),
  1075. getgid(), getpid(), aia.ai_asid, &aia.ai_termid));
  1076. }
  1077. #endif
  1078. #if defined(_KERNEL) || defined(KERNEL)
  1079. static token_t *
  1080. au_to_exec_strings(char *strs, int count, u_char type)
  1081. {
  1082. token_t *t;
  1083. u_char *dptr = NULL;
  1084. u_int32_t totlen;
  1085. int ctr;
  1086. char *p;
  1087. totlen = 0;
  1088. ctr = count;
  1089. p = strs;
  1090. while (ctr-- > 0) {
  1091. totlen += strlen(p) + 1;
  1092. p = strs + totlen;
  1093. }
  1094. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) + totlen);
  1095. ADD_U_CHAR(dptr, type);
  1096. ADD_U_INT32(dptr, count);
  1097. ADD_STRING(dptr, strs, totlen);
  1098. return (t);
  1099. }
  1100. /*
  1101. * token ID 1 byte
  1102. * count 4 bytes
  1103. * text count null-terminated strings
  1104. */
  1105. token_t *
  1106. au_to_exec_args(char *args, int argc)
  1107. {
  1108. return (au_to_exec_strings(args, argc, AUT_EXEC_ARGS));
  1109. }
  1110. /*
  1111. * token ID 1 byte
  1112. * count 4 bytes
  1113. * text count null-terminated strings
  1114. */
  1115. token_t *
  1116. au_to_exec_env(char *envs, int envc)
  1117. {
  1118. return (au_to_exec_strings(envs, envc, AUT_EXEC_ENV));
  1119. }
  1120. #else
  1121. /*
  1122. * token ID 1 byte
  1123. * count 4 bytes
  1124. * text count null-terminated strings
  1125. */
  1126. token_t *
  1127. au_to_exec_args(char **argv)
  1128. {
  1129. token_t *t;
  1130. u_char *dptr = NULL;
  1131. const char *nextarg;
  1132. int i, count = 0;
  1133. size_t totlen = 0;
  1134. nextarg = *argv;
  1135. while (nextarg != NULL) {
  1136. int nextlen;
  1137. nextlen = strlen(nextarg);
  1138. totlen += nextlen + 1;
  1139. count++;
  1140. nextarg = *(argv + count);
  1141. }
  1142. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) + totlen);
  1143. ADD_U_CHAR(dptr, AUT_EXEC_ARGS);
  1144. ADD_U_INT32(dptr, count);
  1145. for (i = 0; i < count; i++) {
  1146. nextarg = *(argv + i);
  1147. ADD_MEM(dptr, nextarg, strlen(nextarg) + 1);
  1148. }
  1149. return (t);
  1150. }
  1151. /*
  1152. * token ID 1 byte
  1153. * count 4 bytes
  1154. * text count null-terminated strings
  1155. */
  1156. token_t *
  1157. au_to_exec_env(char **envp)
  1158. {
  1159. token_t *t;
  1160. u_char *dptr = NULL;
  1161. int i, count = 0;
  1162. size_t totlen = 0;
  1163. const char *nextenv;
  1164. nextenv = *envp;
  1165. while (nextenv != NULL) {
  1166. int nextlen;
  1167. nextlen = strlen(nextenv);
  1168. totlen += nextlen + 1;
  1169. count++;
  1170. nextenv = *(envp + count);
  1171. }
  1172. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) + totlen);
  1173. ADD_U_CHAR(dptr, AUT_EXEC_ENV);
  1174. ADD_U_INT32(dptr, count);
  1175. for (i = 0; i < count; i++) {
  1176. nextenv = *(envp + i);
  1177. ADD_MEM(dptr, nextenv, strlen(nextenv) + 1);
  1178. }
  1179. return (t);
  1180. }
  1181. #endif
  1182. /*
  1183. * token ID 1 byte
  1184. * zonename length 2 bytes
  1185. * zonename N bytes + 1 terminating NULL byte
  1186. */
  1187. token_t *
  1188. au_to_zonename(const char *zonename)
  1189. {
  1190. u_char *dptr = NULL;
  1191. u_int16_t textlen;
  1192. token_t *t;
  1193. textlen = strlen(zonename) + 1;
  1194. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) + textlen);
  1195. ADD_U_CHAR(dptr, AUT_ZONENAME);
  1196. ADD_U_INT16(dptr, textlen);
  1197. ADD_STRING(dptr, zonename, textlen);
  1198. return (t);
  1199. }
  1200. /*
  1201. * token ID 1 byte
  1202. * record byte count 4 bytes
  1203. * version # 1 byte [2]
  1204. * event type 2 bytes
  1205. * event modifier 2 bytes
  1206. * seconds of time 4 bytes/8 bytes (32-bit/64-bit value)
  1207. * milliseconds of time 4 bytes/8 bytes (32-bit/64-bit value)
  1208. */
  1209. token_t *
  1210. au_to_header32_tm(int rec_size, au_event_t e_type, au_emod_t e_mod,
  1211. struct timeval tm)
  1212. {
  1213. token_t *t;
  1214. u_char *dptr = NULL;
  1215. u_int32_t timems;
  1216. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) +
  1217. sizeof(u_char) + 2 * sizeof(u_int16_t) + 2 * sizeof(u_int32_t));
  1218. ADD_U_CHAR(dptr, AUT_HEADER32);
  1219. ADD_U_INT32(dptr, rec_size);
  1220. ADD_U_CHAR(dptr, AUDIT_HEADER_VERSION_OPENBSM);
  1221. ADD_U_INT16(dptr, e_type);
  1222. ADD_U_INT16(dptr, e_mod);
  1223. timems = tm.tv_usec/1000;
  1224. /* Add the timestamp */
  1225. ADD_U_INT32(dptr, tm.tv_sec);
  1226. ADD_U_INT32(dptr, timems); /* We need time in ms. */
  1227. return (t);
  1228. }
  1229. /*
  1230. * token ID 1 byte
  1231. * record byte count 4 bytes
  1232. * version # 1 byte [2]
  1233. * event type 2 bytes
  1234. * event modifier 2 bytes
  1235. * address type/length 4 bytes
  1236. * machine address 4 bytes/16 bytes (IPv4/IPv6 address)
  1237. * seconds of time 4 bytes/8 bytes (32-bit/64-bit value)
  1238. * milliseconds of time 4 bytes/8 bytes (32-bit/64-bit value)
  1239. */
  1240. token_t *
  1241. au_to_header32_ex_tm(int rec_size, au_event_t e_type, au_emod_t e_mod,
  1242. struct timeval tm, struct auditinfo_addr *aia)
  1243. {
  1244. token_t *t;
  1245. u_char *dptr = NULL;
  1246. u_int32_t timems;
  1247. au_tid_addr_t *tid;
  1248. tid = &aia->ai_termid;
  1249. KASSERT(tid->at_type == AU_IPv4 || tid->at_type == AU_IPv6,
  1250. ("au_to_header32_ex_tm: invalid address family"));
  1251. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) +
  1252. sizeof(u_char) + 2 * sizeof(u_int16_t) + 3 *
  1253. sizeof(u_int32_t) + tid->at_type);
  1254. ADD_U_CHAR(dptr, AUT_HEADER32_EX);
  1255. ADD_U_INT32(dptr, rec_size);
  1256. ADD_U_CHAR(dptr, AUDIT_HEADER_VERSION_OPENBSM);
  1257. ADD_U_INT16(dptr, e_type);
  1258. ADD_U_INT16(dptr, e_mod);
  1259. ADD_U_INT32(dptr, tid->at_type);
  1260. if (tid->at_type == AU_IPv6)
  1261. ADD_MEM(dptr, &tid->at_addr[0], 4 * sizeof(u_int32_t));
  1262. else
  1263. ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
  1264. timems = tm.tv_usec/1000;
  1265. /* Add the timestamp */
  1266. ADD_U_INT32(dptr, tm.tv_sec);
  1267. ADD_U_INT32(dptr, timems); /* We need time in ms. */
  1268. return (t);
  1269. }
  1270. token_t *
  1271. au_to_header64_tm(int rec_size, au_event_t e_type, au_emod_t e_mod,
  1272. struct timeval tm)
  1273. {
  1274. token_t *t;
  1275. u_char *dptr = NULL;
  1276. u_int32_t timems;
  1277. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) +
  1278. sizeof(u_char) + 2 * sizeof(u_int16_t) + 2 * sizeof(u_int64_t));
  1279. ADD_U_CHAR(dptr, AUT_HEADER64);
  1280. ADD_U_INT32(dptr, rec_size);
  1281. ADD_U_CHAR(dptr, AUDIT_HEADER_VERSION_OPENBSM);
  1282. ADD_U_INT16(dptr, e_type);
  1283. ADD_U_INT16(dptr, e_mod);
  1284. timems = tm.tv_usec/1000;
  1285. /* Add the timestamp */
  1286. ADD_U_INT64(dptr, tm.tv_sec);
  1287. ADD_U_INT64(dptr, timems); /* We need time in ms. */
  1288. return (t);
  1289. }
  1290. #if !defined(KERNEL) && !defined(_KERNEL)
  1291. #ifdef HAVE_AUDIT_SYSCALLS
  1292. token_t *
  1293. au_to_header32_ex(int rec_size, au_event_t e_type, au_emod_t e_mod)
  1294. {
  1295. struct timeval tm;
  1296. struct auditinfo_addr aia;
  1297. if (gettimeofday(&tm, NULL) == -1)
  1298. return (NULL);
  1299. if (audit_get_kaudit(&aia, sizeof(aia)) != 0) {
  1300. if (errno != ENOSYS)
  1301. return (NULL);
  1302. return (au_to_header32_tm(rec_size, e_type, e_mod, tm));
  1303. }
  1304. return (au_to_header32_ex_tm(rec_size, e_type, e_mod, tm, &aia));
  1305. }
  1306. #endif /* HAVE_AUDIT_SYSCALLS */
  1307. token_t *
  1308. au_to_header32(int rec_size, au_event_t e_type, au_emod_t e_mod)
  1309. {
  1310. struct timeval tm;
  1311. if (gettimeofday(&tm, NULL) == -1)
  1312. return (NULL);
  1313. return (au_to_header32_tm(rec_size, e_type, e_mod, tm));
  1314. }
  1315. token_t *
  1316. au_to_header64(__unused int rec_size, __unused au_event_t e_type,
  1317. __unused au_emod_t e_mod)
  1318. {
  1319. struct timeval tm;
  1320. if (gettimeofday(&tm, NULL) == -1)
  1321. return (NULL);
  1322. return (au_to_header64_tm(rec_size, e_type, e_mod, tm));
  1323. }
  1324. token_t *
  1325. au_to_header(int rec_size, au_event_t e_type, au_emod_t e_mod)
  1326. {
  1327. return (au_to_header32(rec_size, e_type, e_mod));
  1328. }
  1329. #ifdef HAVE_AUDIT_SYSCALLS
  1330. token_t *
  1331. au_to_header_ex(int rec_size, au_event_t e_type, au_emod_t e_mod)
  1332. {
  1333. return (au_to_header32_ex(rec_size, e_type, e_mod));
  1334. }
  1335. #endif /* HAVE_AUDIT_SYSCALLS */
  1336. #endif /* !defined(KERNEL) && !defined(_KERNEL) */
  1337. /*
  1338. * token ID 1 byte
  1339. * trailer magic number 2 bytes
  1340. * record byte count 4 bytes
  1341. */
  1342. token_t *
  1343. au_to_trailer(int rec_size)
  1344. {
  1345. token_t *t;
  1346. u_char *dptr = NULL;
  1347. u_int16_t magic = AUT_TRAILER_MAGIC;
  1348. GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) +
  1349. sizeof(u_int32_t));
  1350. ADD_U_CHAR(dptr, AUT_TRAILER);
  1351. ADD_U_INT16(dptr, magic);
  1352. ADD_U_INT32(dptr, rec_size);
  1353. return (t);
  1354. }