audit_bsm_klib.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 1999-2009 Apple Inc.
  5. * Copyright (c) 2005, 2016-2017 Robert N. M. Watson
  6. * All rights reserved.
  7. *
  8. * Portions of this software were developed by BAE Systems, the University of
  9. * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL
  10. * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent
  11. * Computing (TC) research program.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. Neither the name of Apple Inc. ("Apple") nor the names of
  22. * its contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
  26. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
  29. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  33. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  34. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <sys/param.h>
  38. #include <sys/capsicum.h>
  39. #include <sys/fcntl.h>
  40. #include <sys/filedesc.h>
  41. #include <sys/libkern.h>
  42. #include <sys/malloc.h>
  43. #include <sys/mount.h>
  44. #include <sys/proc.h>
  45. #include <sys/rwlock.h>
  46. #include <sys/sem.h>
  47. #include <sys/sbuf.h>
  48. #include <sys/sx.h>
  49. #include <sys/syscall.h>
  50. #include <sys/sysctl.h>
  51. #include <sys/sysent.h>
  52. #include <sys/vnode.h>
  53. #include <bsm/audit.h>
  54. #include <bsm/audit_kevents.h>
  55. #include <security/audit/audit.h>
  56. #include <security/audit/audit_private.h>
  57. struct aue_open_event {
  58. int aoe_flags;
  59. au_event_t aoe_event;
  60. };
  61. static const struct aue_open_event aue_open[] = {
  62. { O_RDONLY, AUE_OPEN_R },
  63. { (O_RDONLY | O_CREAT), AUE_OPEN_RC },
  64. { (O_RDONLY | O_CREAT | O_TRUNC), AUE_OPEN_RTC },
  65. { (O_RDONLY | O_TRUNC), AUE_OPEN_RT },
  66. { O_RDWR, AUE_OPEN_RW },
  67. { (O_RDWR | O_CREAT), AUE_OPEN_RWC },
  68. { (O_RDWR | O_CREAT | O_TRUNC), AUE_OPEN_RWTC },
  69. { (O_RDWR | O_TRUNC), AUE_OPEN_RWT },
  70. { O_WRONLY, AUE_OPEN_W },
  71. { (O_WRONLY | O_CREAT), AUE_OPEN_WC },
  72. { (O_WRONLY | O_CREAT | O_TRUNC), AUE_OPEN_WTC },
  73. { (O_WRONLY | O_TRUNC), AUE_OPEN_WT },
  74. };
  75. static const struct aue_open_event aue_openat[] = {
  76. { O_RDONLY, AUE_OPENAT_R },
  77. { (O_RDONLY | O_CREAT), AUE_OPENAT_RC },
  78. { (O_RDONLY | O_CREAT | O_TRUNC), AUE_OPENAT_RTC },
  79. { (O_RDONLY | O_TRUNC), AUE_OPENAT_RT },
  80. { O_RDWR, AUE_OPENAT_RW },
  81. { (O_RDWR | O_CREAT), AUE_OPENAT_RWC },
  82. { (O_RDWR | O_CREAT | O_TRUNC), AUE_OPENAT_RWTC },
  83. { (O_RDWR | O_TRUNC), AUE_OPENAT_RWT },
  84. { O_WRONLY, AUE_OPENAT_W },
  85. { (O_WRONLY | O_CREAT), AUE_OPENAT_WC },
  86. { (O_WRONLY | O_CREAT | O_TRUNC), AUE_OPENAT_WTC },
  87. { (O_WRONLY | O_TRUNC), AUE_OPENAT_WT },
  88. };
  89. static const int aue_msgsys[] = {
  90. /* 0 */ AUE_MSGCTL,
  91. /* 1 */ AUE_MSGGET,
  92. /* 2 */ AUE_MSGSND,
  93. /* 3 */ AUE_MSGRCV,
  94. };
  95. static const int aue_msgsys_count = sizeof(aue_msgsys) / sizeof(int);
  96. static const int aue_semsys[] = {
  97. /* 0 */ AUE_SEMCTL,
  98. /* 1 */ AUE_SEMGET,
  99. /* 2 */ AUE_SEMOP,
  100. };
  101. static const int aue_semsys_count = sizeof(aue_semsys) / sizeof(int);
  102. static const int aue_shmsys[] = {
  103. /* 0 */ AUE_SHMAT,
  104. /* 1 */ AUE_SHMDT,
  105. /* 2 */ AUE_SHMGET,
  106. /* 3 */ AUE_SHMCTL,
  107. };
  108. static const int aue_shmsys_count = sizeof(aue_shmsys) / sizeof(int);
  109. /*
  110. * Check whether an event is auditable by comparing the mask of classes this
  111. * event is part of against the given mask.
  112. */
  113. int
  114. au_preselect(au_event_t event, au_class_t class, au_mask_t *mask_p, int sorf)
  115. {
  116. au_class_t effmask = 0;
  117. if (mask_p == NULL)
  118. return (-1);
  119. /*
  120. * Perform the actual check of the masks against the event.
  121. */
  122. if (sorf & AU_PRS_SUCCESS)
  123. effmask |= (mask_p->am_success & class);
  124. if (sorf & AU_PRS_FAILURE)
  125. effmask |= (mask_p->am_failure & class);
  126. if (effmask)
  127. return (1);
  128. else
  129. return (0);
  130. }
  131. /*
  132. * Convert sysctl names and present arguments to events.
  133. */
  134. au_event_t
  135. audit_ctlname_to_sysctlevent(int name[], uint64_t valid_arg)
  136. {
  137. /* can't parse it - so return the worst case */
  138. if ((valid_arg & (ARG_CTLNAME | ARG_LEN)) != (ARG_CTLNAME | ARG_LEN))
  139. return (AUE_SYSCTL);
  140. switch (name[0]) {
  141. /* non-admin "lookups" treat them special */
  142. case KERN_OSTYPE:
  143. case KERN_OSRELEASE:
  144. case KERN_OSREV:
  145. case KERN_VERSION:
  146. case KERN_ARGMAX:
  147. case KERN_CLOCKRATE:
  148. case KERN_BOOTTIME:
  149. case KERN_POSIX1:
  150. case KERN_NGROUPS:
  151. case KERN_JOB_CONTROL:
  152. case KERN_SAVED_IDS:
  153. case KERN_OSRELDATE:
  154. case KERN_DUMMY:
  155. return (AUE_SYSCTL_NONADMIN);
  156. /* only treat the changeable controls as admin */
  157. case KERN_MAXVNODES:
  158. case KERN_MAXPROC:
  159. case KERN_MAXFILES:
  160. case KERN_MAXPROCPERUID:
  161. case KERN_MAXFILESPERPROC:
  162. case KERN_HOSTID:
  163. case KERN_SECURELVL:
  164. case KERN_HOSTNAME:
  165. case KERN_PROC:
  166. case KERN_FILE:
  167. case KERN_PROF:
  168. case KERN_NISDOMAINNAME:
  169. case KERN_UPDATEINTERVAL:
  170. case KERN_NTP_PLL:
  171. case KERN_BOOTFILE:
  172. case KERN_DUMPDEV:
  173. case KERN_IPC:
  174. case KERN_PS_STRINGS:
  175. case KERN_USRSTACK:
  176. case KERN_LOGSIGEXIT:
  177. case KERN_IOV_MAX:
  178. return ((valid_arg & ARG_VALUE) ?
  179. AUE_SYSCTL : AUE_SYSCTL_NONADMIN);
  180. default:
  181. return (AUE_SYSCTL);
  182. }
  183. /* NOTREACHED */
  184. }
  185. /*
  186. * Convert an open flags specifier into a specific type of open event for
  187. * auditing purposes.
  188. */
  189. au_event_t
  190. audit_flags_and_error_to_openevent(int oflags, int error)
  191. {
  192. int i;
  193. /*
  194. * Need to check only those flags we care about.
  195. */
  196. oflags = oflags & (O_RDONLY | O_CREAT | O_TRUNC | O_RDWR | O_WRONLY);
  197. for (i = 0; i < nitems(aue_open); i++) {
  198. if (aue_open[i].aoe_flags == oflags)
  199. return (aue_open[i].aoe_event);
  200. }
  201. return (AUE_OPEN);
  202. }
  203. au_event_t
  204. audit_flags_and_error_to_openatevent(int oflags, int error)
  205. {
  206. int i;
  207. /*
  208. * Need to check only those flags we care about.
  209. */
  210. oflags = oflags & (O_RDONLY | O_CREAT | O_TRUNC | O_RDWR | O_WRONLY);
  211. for (i = 0; i < nitems(aue_openat); i++) {
  212. if (aue_openat[i].aoe_flags == oflags)
  213. return (aue_openat[i].aoe_event);
  214. }
  215. return (AUE_OPENAT);
  216. }
  217. /*
  218. * Convert a MSGCTL command to a specific event.
  219. */
  220. au_event_t
  221. audit_msgctl_to_event(int cmd)
  222. {
  223. switch (cmd) {
  224. case IPC_RMID:
  225. return (AUE_MSGCTL_RMID);
  226. case IPC_SET:
  227. return (AUE_MSGCTL_SET);
  228. case IPC_STAT:
  229. return (AUE_MSGCTL_STAT);
  230. default:
  231. /* We will audit a bad command. */
  232. return (AUE_MSGCTL);
  233. }
  234. }
  235. /*
  236. * Convert a SEMCTL command to a specific event.
  237. */
  238. au_event_t
  239. audit_semctl_to_event(int cmd)
  240. {
  241. switch (cmd) {
  242. case GETALL:
  243. return (AUE_SEMCTL_GETALL);
  244. case GETNCNT:
  245. return (AUE_SEMCTL_GETNCNT);
  246. case GETPID:
  247. return (AUE_SEMCTL_GETPID);
  248. case GETVAL:
  249. return (AUE_SEMCTL_GETVAL);
  250. case GETZCNT:
  251. return (AUE_SEMCTL_GETZCNT);
  252. case IPC_RMID:
  253. return (AUE_SEMCTL_RMID);
  254. case IPC_SET:
  255. return (AUE_SEMCTL_SET);
  256. case SETALL:
  257. return (AUE_SEMCTL_SETALL);
  258. case SETVAL:
  259. return (AUE_SEMCTL_SETVAL);
  260. case IPC_STAT:
  261. return (AUE_SEMCTL_STAT);
  262. default:
  263. /* We will audit a bad command. */
  264. return (AUE_SEMCTL);
  265. }
  266. }
  267. /*
  268. * Convert msgsys(2), semsys(2), and shmsys(2) system-call variations into
  269. * audit events, if possible.
  270. */
  271. au_event_t
  272. audit_msgsys_to_event(int which)
  273. {
  274. if ((which >= 0) && (which < aue_msgsys_count))
  275. return (aue_msgsys[which]);
  276. /* Audit a bad command. */
  277. return (AUE_MSGSYS);
  278. }
  279. au_event_t
  280. audit_semsys_to_event(int which)
  281. {
  282. if ((which >= 0) && (which < aue_semsys_count))
  283. return (aue_semsys[which]);
  284. /* Audit a bad command. */
  285. return (AUE_SEMSYS);
  286. }
  287. au_event_t
  288. audit_shmsys_to_event(int which)
  289. {
  290. if ((which >= 0) && (which < aue_shmsys_count))
  291. return (aue_shmsys[which]);
  292. /* Audit a bad command. */
  293. return (AUE_SHMSYS);
  294. }
  295. /*
  296. * Convert a command for the auditon() system call to a audit event.
  297. */
  298. au_event_t
  299. auditon_command_event(int cmd)
  300. {
  301. switch(cmd) {
  302. case A_GETPOLICY:
  303. return (AUE_AUDITON_GPOLICY);
  304. case A_SETPOLICY:
  305. return (AUE_AUDITON_SPOLICY);
  306. case A_GETKMASK:
  307. return (AUE_AUDITON_GETKMASK);
  308. case A_SETKMASK:
  309. return (AUE_AUDITON_SETKMASK);
  310. case A_GETQCTRL:
  311. return (AUE_AUDITON_GQCTRL);
  312. case A_SETQCTRL:
  313. return (AUE_AUDITON_SQCTRL);
  314. case A_GETCWD:
  315. return (AUE_AUDITON_GETCWD);
  316. case A_GETCAR:
  317. return (AUE_AUDITON_GETCAR);
  318. case A_GETSTAT:
  319. return (AUE_AUDITON_GETSTAT);
  320. case A_SETSTAT:
  321. return (AUE_AUDITON_SETSTAT);
  322. case A_SETUMASK:
  323. return (AUE_AUDITON_SETUMASK);
  324. case A_SETSMASK:
  325. return (AUE_AUDITON_SETSMASK);
  326. case A_GETCOND:
  327. return (AUE_AUDITON_GETCOND);
  328. case A_SETCOND:
  329. return (AUE_AUDITON_SETCOND);
  330. case A_GETCLASS:
  331. return (AUE_AUDITON_GETCLASS);
  332. case A_SETCLASS:
  333. return (AUE_AUDITON_SETCLASS);
  334. case A_GETPINFO:
  335. case A_SETPMASK:
  336. case A_SETFSIZE:
  337. case A_GETFSIZE:
  338. case A_GETPINFO_ADDR:
  339. case A_GETKAUDIT:
  340. case A_SETKAUDIT:
  341. default:
  342. return (AUE_AUDITON); /* No special record */
  343. }
  344. }
  345. /*
  346. * Create a canonical path from given path by prefixing either the root
  347. * directory, or the current working directory. If the process working
  348. * directory is NULL, we could use 'rootvnode' to obtain the root directory,
  349. * but this results in a volfs name written to the audit log. So we will
  350. * leave the filename starting with '/' in the audit log in this case.
  351. */
  352. void
  353. audit_canon_path_vp(struct thread *td, struct vnode *rdir, struct vnode *cdir,
  354. char *path, char *cpath)
  355. {
  356. struct vnode *vp;
  357. char *rbuf, *fbuf, *copy;
  358. struct sbuf sbf;
  359. int error;
  360. WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "%s: at %s:%d",
  361. __func__, __FILE__, __LINE__);
  362. copy = path;
  363. if (*path == '/') {
  364. vp = rdir;
  365. } else {
  366. if (cdir == NULL) {
  367. cpath[0] = '\0';
  368. return;
  369. }
  370. vp = cdir;
  371. }
  372. MPASS(vp != NULL);
  373. /*
  374. * NB: We require that the supplied array be at least MAXPATHLEN bytes
  375. * long. If this is not the case, then we can run into serious trouble.
  376. */
  377. (void) sbuf_new(&sbf, cpath, MAXPATHLEN, SBUF_FIXEDLEN);
  378. /*
  379. * Strip leading forward slashes.
  380. *
  381. * Note this does nothing to fully canonicalize the path.
  382. */
  383. while (*copy == '/')
  384. copy++;
  385. /*
  386. * Make sure we handle chroot(2) and prepend the global path to these
  387. * environments.
  388. *
  389. * NB: vn_fullpath(9) on FreeBSD is less reliable than vn_getpath(9)
  390. * on Darwin. As a result, this may need some additional attention
  391. * in the future.
  392. */
  393. error = vn_fullpath_global(vp, &rbuf, &fbuf);
  394. if (error) {
  395. cpath[0] = '\0';
  396. return;
  397. }
  398. (void) sbuf_cat(&sbf, rbuf);
  399. /*
  400. * We are going to concatenate the resolved path with the passed path
  401. * with all slashes removed and we want them glued with a single slash.
  402. * However, if the directory is /, the slash is already there.
  403. */
  404. if (rbuf[1] != '\0')
  405. (void) sbuf_putc(&sbf, '/');
  406. free(fbuf, M_TEMP);
  407. /*
  408. * Now that we have processed any alternate root and relative path
  409. * names, add the supplied pathname.
  410. */
  411. (void) sbuf_cat(&sbf, copy);
  412. /*
  413. * One or more of the previous sbuf operations could have resulted in
  414. * the supplied buffer being overflowed. Check to see if this is the
  415. * case.
  416. */
  417. if (sbuf_error(&sbf) != 0) {
  418. cpath[0] = '\0';
  419. return;
  420. }
  421. sbuf_finish(&sbf);
  422. }
  423. void
  424. audit_canon_path(struct thread *td, int dirfd, char *path, char *cpath)
  425. {
  426. struct vnode *cdir, *rdir;
  427. struct pwd *pwd;
  428. cap_rights_t rights;
  429. int error;
  430. bool vrele_cdir;
  431. WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "%s: at %s:%d",
  432. __func__, __FILE__, __LINE__);
  433. pwd = pwd_hold(td);
  434. rdir = pwd->pwd_rdir;
  435. cdir = NULL;
  436. vrele_cdir = false;
  437. if (*path != '/') {
  438. if (dirfd == AT_FDCWD) {
  439. cdir = pwd->pwd_cdir;
  440. } else {
  441. error = fgetvp(td, dirfd, cap_rights_init(&rights), &cdir);
  442. if (error != 0) {
  443. cpath[0] = '\0';
  444. pwd_drop(pwd);
  445. return;
  446. }
  447. vrele_cdir = true;
  448. }
  449. }
  450. audit_canon_path_vp(td, rdir, cdir, path, cpath);
  451. pwd_drop(pwd);
  452. if (vrele_cdir)
  453. vrele(cdir);
  454. }