audit_syscalls.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 1999-2009 Apple Inc.
  5. * Copyright (c) 2016, 2018 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/mount.h>
  39. #include <sys/namei.h>
  40. #include <sys/priv.h>
  41. #include <sys/proc.h>
  42. #include <sys/sysproto.h>
  43. #include <sys/systm.h>
  44. #include <sys/vnode.h>
  45. #include <sys/jail.h>
  46. #include <bsm/audit.h>
  47. #include <bsm/audit_kevents.h>
  48. #include <security/audit/audit.h>
  49. #include <security/audit/audit_private.h>
  50. #include <security/mac/mac_framework.h>
  51. #ifdef AUDIT
  52. /*
  53. * System call to allow a user space application to submit a BSM audit record
  54. * to the kernel for inclusion in the audit log. This function does little
  55. * verification on the audit record that is submitted.
  56. *
  57. * XXXAUDIT: Audit preselection for user records does not currently work,
  58. * since we pre-select only based on the AUE_audit event type, not the event
  59. * type submitted as part of the user audit data.
  60. */
  61. /* ARGSUSED */
  62. int
  63. sys_audit(struct thread *td, struct audit_args *uap)
  64. {
  65. int error;
  66. void * rec;
  67. struct kaudit_record *ar;
  68. if (jailed(td->td_ucred))
  69. return (ENOSYS);
  70. error = priv_check(td, PRIV_AUDIT_SUBMIT);
  71. if (error)
  72. return (error);
  73. if ((uap->length <= 0) || (uap->length > audit_qctrl.aq_bufsz))
  74. return (EINVAL);
  75. ar = currecord();
  76. /*
  77. * If there's no current audit record (audit() itself not audited)
  78. * commit the user audit record.
  79. */
  80. if (ar == NULL) {
  81. /*
  82. * This is not very efficient; we're required to allocate a
  83. * complete kernel audit record just so the user record can
  84. * tag along.
  85. *
  86. * XXXAUDIT: Maybe AUE_AUDIT in the system call context and
  87. * special pre-select handling?
  88. */
  89. td->td_ar = audit_new(AUE_NULL, td);
  90. if (td->td_ar == NULL)
  91. return (ENOTSUP);
  92. td->td_pflags |= TDP_AUDITREC;
  93. ar = td->td_ar;
  94. }
  95. if (uap->length > MAX_AUDIT_RECORD_SIZE)
  96. return (EINVAL);
  97. rec = malloc(uap->length, M_AUDITDATA, M_WAITOK);
  98. error = copyin(uap->record, rec, uap->length);
  99. if (error)
  100. goto free_out;
  101. /* Verify the record. */
  102. if (bsm_rec_verify(rec) == 0) {
  103. error = EINVAL;
  104. goto free_out;
  105. }
  106. #ifdef MAC
  107. error = mac_system_check_audit(td->td_ucred, rec, uap->length);
  108. if (error)
  109. goto free_out;
  110. #endif
  111. /*
  112. * Attach the user audit record to the kernel audit record. Because
  113. * this system call is an auditable event, we will write the user
  114. * record along with the record for this audit event.
  115. *
  116. * XXXAUDIT: KASSERT appropriate starting values of k_udata, k_ulen,
  117. * k_ar_commit & AR_COMMIT_USER?
  118. */
  119. ar->k_udata = rec;
  120. ar->k_ulen = uap->length;
  121. ar->k_ar_commit |= AR_COMMIT_USER;
  122. /*
  123. * Currently we assume that all preselection has been performed in
  124. * userspace. We unconditionally set these masks so that the records
  125. * get committed both to the trail and pipe. In the future we will
  126. * want to setup kernel based preselection.
  127. */
  128. ar->k_ar_commit |= (AR_PRESELECT_USER_TRAIL | AR_PRESELECT_USER_PIPE);
  129. return (0);
  130. free_out:
  131. /*
  132. * audit_syscall_exit() will free the audit record on the thread even
  133. * if we allocated it above.
  134. */
  135. free(rec, M_AUDITDATA);
  136. return (error);
  137. }
  138. /*
  139. * System call to manipulate auditing.
  140. */
  141. /* ARGSUSED */
  142. int
  143. sys_auditon(struct thread *td, struct auditon_args *uap)
  144. {
  145. struct ucred *cred, *newcred, *oldcred;
  146. int error;
  147. union auditon_udata udata;
  148. struct proc *tp;
  149. if (jailed(td->td_ucred))
  150. return (ENOSYS);
  151. AUDIT_ARG_CMD(uap->cmd);
  152. #ifdef MAC
  153. error = mac_system_check_auditon(td->td_ucred, uap->cmd);
  154. if (error)
  155. return (error);
  156. #endif
  157. error = priv_check(td, PRIV_AUDIT_CONTROL);
  158. if (error)
  159. return (error);
  160. if ((uap->length <= 0) || (uap->length > sizeof(union auditon_udata)))
  161. return (EINVAL);
  162. memset((void *)&udata, 0, sizeof(udata));
  163. /*
  164. * Some of the GET commands use the arguments too.
  165. */
  166. switch (uap->cmd) {
  167. case A_SETPOLICY:
  168. case A_OLDSETPOLICY:
  169. case A_SETKMASK:
  170. case A_SETQCTRL:
  171. case A_OLDSETQCTRL:
  172. case A_SETSTAT:
  173. case A_SETUMASK:
  174. case A_SETSMASK:
  175. case A_SETCOND:
  176. case A_OLDSETCOND:
  177. case A_SETCLASS:
  178. case A_SETEVENT:
  179. case A_SETPMASK:
  180. case A_SETFSIZE:
  181. case A_SETKAUDIT:
  182. case A_GETCLASS:
  183. case A_GETEVENT:
  184. case A_GETPINFO:
  185. case A_GETPINFO_ADDR:
  186. case A_SENDTRIGGER:
  187. error = copyin(uap->data, (void *)&udata, uap->length);
  188. if (error)
  189. return (error);
  190. AUDIT_ARG_AUDITON(&udata);
  191. break;
  192. }
  193. /*
  194. * XXXAUDIT: Locking?
  195. */
  196. switch (uap->cmd) {
  197. case A_OLDGETPOLICY:
  198. case A_GETPOLICY:
  199. if (uap->length == sizeof(udata.au_policy64)) {
  200. if (!audit_fail_stop)
  201. udata.au_policy64 |= AUDIT_CNT;
  202. if (audit_panic_on_write_fail)
  203. udata.au_policy64 |= AUDIT_AHLT;
  204. if (audit_argv)
  205. udata.au_policy64 |= AUDIT_ARGV;
  206. if (audit_arge)
  207. udata.au_policy64 |= AUDIT_ARGE;
  208. break;
  209. }
  210. if (uap->length != sizeof(udata.au_policy))
  211. return (EINVAL);
  212. if (!audit_fail_stop)
  213. udata.au_policy |= AUDIT_CNT;
  214. if (audit_panic_on_write_fail)
  215. udata.au_policy |= AUDIT_AHLT;
  216. if (audit_argv)
  217. udata.au_policy |= AUDIT_ARGV;
  218. if (audit_arge)
  219. udata.au_policy |= AUDIT_ARGE;
  220. break;
  221. case A_OLDSETPOLICY:
  222. case A_SETPOLICY:
  223. if (uap->length == sizeof(udata.au_policy64)) {
  224. if (udata.au_policy & ~(AUDIT_CNT|AUDIT_AHLT|
  225. AUDIT_ARGV|AUDIT_ARGE))
  226. return (EINVAL);
  227. audit_fail_stop = ((udata.au_policy64 & AUDIT_CNT) ==
  228. 0);
  229. audit_panic_on_write_fail = (udata.au_policy64 &
  230. AUDIT_AHLT);
  231. audit_argv = (udata.au_policy64 & AUDIT_ARGV);
  232. audit_arge = (udata.au_policy64 & AUDIT_ARGE);
  233. break;
  234. }
  235. if (uap->length != sizeof(udata.au_policy))
  236. return (EINVAL);
  237. if (udata.au_policy & ~(AUDIT_CNT|AUDIT_AHLT|AUDIT_ARGV|
  238. AUDIT_ARGE))
  239. return (EINVAL);
  240. /*
  241. * XXX - Need to wake up waiters if the policy relaxes?
  242. */
  243. audit_fail_stop = ((udata.au_policy & AUDIT_CNT) == 0);
  244. audit_panic_on_write_fail = (udata.au_policy & AUDIT_AHLT);
  245. audit_argv = (udata.au_policy & AUDIT_ARGV);
  246. audit_arge = (udata.au_policy & AUDIT_ARGE);
  247. break;
  248. case A_GETKMASK:
  249. if (uap->length != sizeof(udata.au_mask))
  250. return (EINVAL);
  251. udata.au_mask = audit_nae_mask;
  252. break;
  253. case A_SETKMASK:
  254. if (uap->length != sizeof(udata.au_mask))
  255. return (EINVAL);
  256. audit_nae_mask = udata.au_mask;
  257. break;
  258. case A_OLDGETQCTRL:
  259. case A_GETQCTRL:
  260. if (uap->length == sizeof(udata.au_qctrl64)) {
  261. udata.au_qctrl64.aq64_hiwater =
  262. (u_int64_t)audit_qctrl.aq_hiwater;
  263. udata.au_qctrl64.aq64_lowater =
  264. (u_int64_t)audit_qctrl.aq_lowater;
  265. udata.au_qctrl64.aq64_bufsz =
  266. (u_int64_t)audit_qctrl.aq_bufsz;
  267. udata.au_qctrl64.aq64_minfree =
  268. (u_int64_t)audit_qctrl.aq_minfree;
  269. break;
  270. }
  271. if (uap->length != sizeof(udata.au_qctrl))
  272. return (EINVAL);
  273. udata.au_qctrl = audit_qctrl;
  274. break;
  275. case A_OLDSETQCTRL:
  276. case A_SETQCTRL:
  277. if (uap->length == sizeof(udata.au_qctrl64)) {
  278. /* NB: aq64_minfree is unsigned unlike aq_minfree. */
  279. if ((udata.au_qctrl64.aq64_hiwater > AQ_MAXHIGH) ||
  280. (udata.au_qctrl64.aq64_lowater >=
  281. udata.au_qctrl.aq_hiwater) ||
  282. (udata.au_qctrl64.aq64_bufsz > AQ_MAXBUFSZ) ||
  283. (udata.au_qctrl64.aq64_minfree > 100))
  284. return (EINVAL);
  285. audit_qctrl.aq_hiwater =
  286. (int)udata.au_qctrl64.aq64_hiwater;
  287. audit_qctrl.aq_lowater =
  288. (int)udata.au_qctrl64.aq64_lowater;
  289. audit_qctrl.aq_bufsz =
  290. (int)udata.au_qctrl64.aq64_bufsz;
  291. audit_qctrl.aq_minfree =
  292. (int)udata.au_qctrl64.aq64_minfree;
  293. audit_qctrl.aq_delay = -1; /* Not used. */
  294. break;
  295. }
  296. if (uap->length != sizeof(udata.au_qctrl))
  297. return (EINVAL);
  298. if ((udata.au_qctrl.aq_hiwater > AQ_MAXHIGH) ||
  299. (udata.au_qctrl.aq_lowater >= udata.au_qctrl.aq_hiwater) ||
  300. (udata.au_qctrl.aq_bufsz > AQ_MAXBUFSZ) ||
  301. (udata.au_qctrl.aq_minfree < 0) ||
  302. (udata.au_qctrl.aq_minfree > 100))
  303. return (EINVAL);
  304. audit_qctrl = udata.au_qctrl;
  305. /* XXX The queue delay value isn't used with the kernel. */
  306. audit_qctrl.aq_delay = -1;
  307. break;
  308. case A_GETCWD:
  309. return (ENOSYS);
  310. break;
  311. case A_GETCAR:
  312. return (ENOSYS);
  313. break;
  314. case A_GETSTAT:
  315. return (ENOSYS);
  316. break;
  317. case A_SETSTAT:
  318. return (ENOSYS);
  319. break;
  320. case A_SETUMASK:
  321. return (ENOSYS);
  322. break;
  323. case A_SETSMASK:
  324. return (ENOSYS);
  325. break;
  326. case A_OLDGETCOND:
  327. case A_GETCOND:
  328. if (uap->length == sizeof(udata.au_cond64)) {
  329. if (audit_trail_enabled && !audit_trail_suspended)
  330. udata.au_cond64 = AUC_AUDITING;
  331. else
  332. udata.au_cond64 = AUC_NOAUDIT;
  333. break;
  334. }
  335. if (uap->length != sizeof(udata.au_cond))
  336. return (EINVAL);
  337. if (audit_trail_enabled && !audit_trail_suspended)
  338. udata.au_cond = AUC_AUDITING;
  339. else
  340. udata.au_cond = AUC_NOAUDIT;
  341. break;
  342. case A_OLDSETCOND:
  343. case A_SETCOND:
  344. if (uap->length == sizeof(udata.au_cond64)) {
  345. if (udata.au_cond64 == AUC_NOAUDIT)
  346. audit_trail_suspended = 1;
  347. if (udata.au_cond64 == AUC_AUDITING)
  348. audit_trail_suspended = 0;
  349. if (udata.au_cond64 == AUC_DISABLED) {
  350. audit_trail_suspended = 1;
  351. audit_shutdown(NULL, 0);
  352. }
  353. audit_syscalls_enabled_update();
  354. break;
  355. }
  356. if (uap->length != sizeof(udata.au_cond))
  357. return (EINVAL);
  358. if (udata.au_cond == AUC_NOAUDIT)
  359. audit_trail_suspended = 1;
  360. if (udata.au_cond == AUC_AUDITING)
  361. audit_trail_suspended = 0;
  362. if (udata.au_cond == AUC_DISABLED) {
  363. audit_trail_suspended = 1;
  364. audit_shutdown(NULL, 0);
  365. }
  366. audit_syscalls_enabled_update();
  367. break;
  368. case A_GETCLASS:
  369. if (uap->length != sizeof(udata.au_evclass))
  370. return (EINVAL);
  371. udata.au_evclass.ec_class = au_event_class(
  372. udata.au_evclass.ec_number);
  373. break;
  374. case A_GETEVENT:
  375. if (uap->length != sizeof(udata.au_evname))
  376. return (EINVAL);
  377. error = au_event_name(udata.au_evname.en_number,
  378. udata.au_evname.en_name);
  379. if (error != 0)
  380. return (error);
  381. break;
  382. case A_SETCLASS:
  383. if (uap->length != sizeof(udata.au_evclass))
  384. return (EINVAL);
  385. au_evclassmap_insert(udata.au_evclass.ec_number,
  386. udata.au_evclass.ec_class);
  387. break;
  388. case A_SETEVENT:
  389. if (uap->length != sizeof(udata.au_evname))
  390. return (EINVAL);
  391. /* Ensure nul termination from userspace. */
  392. udata.au_evname.en_name[sizeof(udata.au_evname.en_name) - 1]
  393. = 0;
  394. au_evnamemap_insert(udata.au_evname.en_number,
  395. udata.au_evname.en_name);
  396. break;
  397. case A_GETPINFO:
  398. if (uap->length != sizeof(udata.au_aupinfo))
  399. return (EINVAL);
  400. if (udata.au_aupinfo.ap_pid < 1)
  401. return (ESRCH);
  402. if ((tp = pfind(udata.au_aupinfo.ap_pid)) == NULL)
  403. return (ESRCH);
  404. if ((error = p_cansee(td, tp)) != 0) {
  405. PROC_UNLOCK(tp);
  406. return (error);
  407. }
  408. cred = tp->p_ucred;
  409. if (cred->cr_audit.ai_termid.at_type == AU_IPv6) {
  410. PROC_UNLOCK(tp);
  411. return (EINVAL);
  412. }
  413. udata.au_aupinfo.ap_auid = cred->cr_audit.ai_auid;
  414. udata.au_aupinfo.ap_mask.am_success =
  415. cred->cr_audit.ai_mask.am_success;
  416. udata.au_aupinfo.ap_mask.am_failure =
  417. cred->cr_audit.ai_mask.am_failure;
  418. udata.au_aupinfo.ap_termid.machine =
  419. cred->cr_audit.ai_termid.at_addr[0];
  420. udata.au_aupinfo.ap_termid.port =
  421. (dev_t)cred->cr_audit.ai_termid.at_port;
  422. udata.au_aupinfo.ap_asid = cred->cr_audit.ai_asid;
  423. PROC_UNLOCK(tp);
  424. break;
  425. case A_SETPMASK:
  426. if (uap->length != sizeof(udata.au_aupinfo))
  427. return (EINVAL);
  428. if (udata.au_aupinfo.ap_pid < 1)
  429. return (ESRCH);
  430. newcred = crget();
  431. if ((tp = pfind(udata.au_aupinfo.ap_pid)) == NULL) {
  432. crfree(newcred);
  433. return (ESRCH);
  434. }
  435. if ((error = p_cansee(td, tp)) != 0) {
  436. PROC_UNLOCK(tp);
  437. crfree(newcred);
  438. return (error);
  439. }
  440. oldcred = tp->p_ucred;
  441. crcopy(newcred, oldcred);
  442. newcred->cr_audit.ai_mask.am_success =
  443. udata.au_aupinfo.ap_mask.am_success;
  444. newcred->cr_audit.ai_mask.am_failure =
  445. udata.au_aupinfo.ap_mask.am_failure;
  446. proc_set_cred(tp, newcred);
  447. PROC_UNLOCK(tp);
  448. crfree(oldcred);
  449. break;
  450. case A_SETFSIZE:
  451. if (uap->length != sizeof(udata.au_fstat))
  452. return (EINVAL);
  453. if ((udata.au_fstat.af_filesz != 0) &&
  454. (udata.au_fstat.af_filesz < MIN_AUDIT_FILE_SIZE))
  455. return (EINVAL);
  456. audit_fstat.af_filesz = udata.au_fstat.af_filesz;
  457. break;
  458. case A_GETFSIZE:
  459. if (uap->length != sizeof(udata.au_fstat))
  460. return (EINVAL);
  461. udata.au_fstat.af_filesz = audit_fstat.af_filesz;
  462. udata.au_fstat.af_currsz = audit_fstat.af_currsz;
  463. break;
  464. case A_GETPINFO_ADDR:
  465. if (uap->length != sizeof(udata.au_aupinfo_addr))
  466. return (EINVAL);
  467. if (udata.au_aupinfo_addr.ap_pid < 1)
  468. return (ESRCH);
  469. if ((tp = pfind(udata.au_aupinfo_addr.ap_pid)) == NULL)
  470. return (ESRCH);
  471. cred = tp->p_ucred;
  472. udata.au_aupinfo_addr.ap_auid = cred->cr_audit.ai_auid;
  473. udata.au_aupinfo_addr.ap_mask.am_success =
  474. cred->cr_audit.ai_mask.am_success;
  475. udata.au_aupinfo_addr.ap_mask.am_failure =
  476. cred->cr_audit.ai_mask.am_failure;
  477. udata.au_aupinfo_addr.ap_termid = cred->cr_audit.ai_termid;
  478. udata.au_aupinfo_addr.ap_asid = cred->cr_audit.ai_asid;
  479. PROC_UNLOCK(tp);
  480. break;
  481. case A_GETKAUDIT:
  482. if (uap->length != sizeof(udata.au_kau_info))
  483. return (EINVAL);
  484. audit_get_kinfo(&udata.au_kau_info);
  485. break;
  486. case A_SETKAUDIT:
  487. if (uap->length != sizeof(udata.au_kau_info))
  488. return (EINVAL);
  489. if (udata.au_kau_info.ai_termid.at_type != AU_IPv4 &&
  490. udata.au_kau_info.ai_termid.at_type != AU_IPv6)
  491. return (EINVAL);
  492. audit_set_kinfo(&udata.au_kau_info);
  493. break;
  494. case A_SENDTRIGGER:
  495. if (uap->length != sizeof(udata.au_trigger))
  496. return (EINVAL);
  497. if ((udata.au_trigger < AUDIT_TRIGGER_MIN) ||
  498. (udata.au_trigger > AUDIT_TRIGGER_MAX))
  499. return (EINVAL);
  500. return (audit_send_trigger(udata.au_trigger));
  501. default:
  502. return (EINVAL);
  503. }
  504. /*
  505. * Copy data back to userspace for the GET comands.
  506. */
  507. switch (uap->cmd) {
  508. case A_GETPOLICY:
  509. case A_OLDGETPOLICY:
  510. case A_GETKMASK:
  511. case A_GETQCTRL:
  512. case A_OLDGETQCTRL:
  513. case A_GETCWD:
  514. case A_GETCAR:
  515. case A_GETSTAT:
  516. case A_GETCOND:
  517. case A_OLDGETCOND:
  518. case A_GETCLASS:
  519. case A_GETPINFO:
  520. case A_GETFSIZE:
  521. case A_GETPINFO_ADDR:
  522. case A_GETKAUDIT:
  523. error = copyout((void *)&udata, uap->data, uap->length);
  524. if (error)
  525. return (error);
  526. break;
  527. }
  528. return (0);
  529. }
  530. /*
  531. * System calls to manage the user audit information.
  532. */
  533. /* ARGSUSED */
  534. int
  535. sys_getauid(struct thread *td, struct getauid_args *uap)
  536. {
  537. int error;
  538. if (jailed(td->td_ucred))
  539. return (ENOSYS);
  540. error = priv_check(td, PRIV_AUDIT_GETAUDIT);
  541. if (error)
  542. return (error);
  543. return (copyout(&td->td_ucred->cr_audit.ai_auid, uap->auid,
  544. sizeof(td->td_ucred->cr_audit.ai_auid)));
  545. }
  546. /* ARGSUSED */
  547. int
  548. sys_setauid(struct thread *td, struct setauid_args *uap)
  549. {
  550. struct ucred *newcred, *oldcred;
  551. au_id_t id;
  552. int error;
  553. if (jailed(td->td_ucred))
  554. return (ENOSYS);
  555. error = copyin(uap->auid, &id, sizeof(id));
  556. if (error)
  557. return (error);
  558. audit_arg_auid(id);
  559. newcred = crget();
  560. PROC_LOCK(td->td_proc);
  561. oldcred = td->td_proc->p_ucred;
  562. crcopy(newcred, oldcred);
  563. #ifdef MAC
  564. error = mac_cred_check_setauid(oldcred, id);
  565. if (error)
  566. goto fail;
  567. #endif
  568. error = priv_check_cred(oldcred, PRIV_AUDIT_SETAUDIT);
  569. if (error)
  570. goto fail;
  571. newcred->cr_audit.ai_auid = id;
  572. proc_set_cred(td->td_proc, newcred);
  573. PROC_UNLOCK(td->td_proc);
  574. crfree(oldcred);
  575. return (0);
  576. fail:
  577. PROC_UNLOCK(td->td_proc);
  578. crfree(newcred);
  579. return (error);
  580. }
  581. /*
  582. * System calls to get and set process audit information.
  583. */
  584. /* ARGSUSED */
  585. int
  586. sys_getaudit(struct thread *td, struct getaudit_args *uap)
  587. {
  588. struct auditinfo ai;
  589. struct ucred *cred;
  590. int error;
  591. cred = td->td_ucred;
  592. if (jailed(cred))
  593. return (ENOSYS);
  594. error = priv_check(td, PRIV_AUDIT_GETAUDIT);
  595. if (error)
  596. return (error);
  597. if (cred->cr_audit.ai_termid.at_type == AU_IPv6)
  598. return (E2BIG);
  599. bzero(&ai, sizeof(ai));
  600. ai.ai_auid = cred->cr_audit.ai_auid;
  601. ai.ai_mask = cred->cr_audit.ai_mask;
  602. ai.ai_asid = cred->cr_audit.ai_asid;
  603. ai.ai_termid.machine = cred->cr_audit.ai_termid.at_addr[0];
  604. ai.ai_termid.port = cred->cr_audit.ai_termid.at_port;
  605. return (copyout(&ai, uap->auditinfo, sizeof(ai)));
  606. }
  607. /* ARGSUSED */
  608. int
  609. sys_setaudit(struct thread *td, struct setaudit_args *uap)
  610. {
  611. struct ucred *newcred, *oldcred;
  612. struct auditinfo ai;
  613. int error;
  614. if (jailed(td->td_ucred))
  615. return (ENOSYS);
  616. error = copyin(uap->auditinfo, &ai, sizeof(ai));
  617. if (error)
  618. return (error);
  619. audit_arg_auditinfo(&ai);
  620. newcred = crget();
  621. PROC_LOCK(td->td_proc);
  622. oldcred = td->td_proc->p_ucred;
  623. crcopy(newcred, oldcred);
  624. #ifdef MAC
  625. error = mac_cred_check_setaudit(oldcred, &ai);
  626. if (error)
  627. goto fail;
  628. #endif
  629. error = priv_check_cred(oldcred, PRIV_AUDIT_SETAUDIT);
  630. if (error)
  631. goto fail;
  632. bzero(&newcred->cr_audit, sizeof(newcred->cr_audit));
  633. newcred->cr_audit.ai_auid = ai.ai_auid;
  634. newcred->cr_audit.ai_mask = ai.ai_mask;
  635. newcred->cr_audit.ai_asid = ai.ai_asid;
  636. newcred->cr_audit.ai_termid.at_addr[0] = ai.ai_termid.machine;
  637. newcred->cr_audit.ai_termid.at_port = ai.ai_termid.port;
  638. newcred->cr_audit.ai_termid.at_type = AU_IPv4;
  639. proc_set_cred(td->td_proc, newcred);
  640. PROC_UNLOCK(td->td_proc);
  641. crfree(oldcred);
  642. return (0);
  643. fail:
  644. PROC_UNLOCK(td->td_proc);
  645. crfree(newcred);
  646. return (error);
  647. }
  648. /* ARGSUSED */
  649. int
  650. sys_getaudit_addr(struct thread *td, struct getaudit_addr_args *uap)
  651. {
  652. int error;
  653. if (jailed(td->td_ucred))
  654. return (ENOSYS);
  655. if (uap->length < sizeof(*uap->auditinfo_addr))
  656. return (EOVERFLOW);
  657. error = priv_check(td, PRIV_AUDIT_GETAUDIT);
  658. if (error)
  659. return (error);
  660. return (copyout(&td->td_ucred->cr_audit, uap->auditinfo_addr,
  661. sizeof(*uap->auditinfo_addr)));
  662. }
  663. /* ARGSUSED */
  664. int
  665. sys_setaudit_addr(struct thread *td, struct setaudit_addr_args *uap)
  666. {
  667. struct ucred *newcred, *oldcred;
  668. struct auditinfo_addr aia;
  669. int error;
  670. if (jailed(td->td_ucred))
  671. return (ENOSYS);
  672. error = copyin(uap->auditinfo_addr, &aia, sizeof(aia));
  673. if (error)
  674. return (error);
  675. audit_arg_auditinfo_addr(&aia);
  676. if (aia.ai_termid.at_type != AU_IPv6 &&
  677. aia.ai_termid.at_type != AU_IPv4)
  678. return (EINVAL);
  679. newcred = crget();
  680. PROC_LOCK(td->td_proc);
  681. oldcred = td->td_proc->p_ucred;
  682. crcopy(newcred, oldcred);
  683. #ifdef MAC
  684. error = mac_cred_check_setaudit_addr(oldcred, &aia);
  685. if (error)
  686. goto fail;
  687. #endif
  688. error = priv_check_cred(oldcred, PRIV_AUDIT_SETAUDIT);
  689. if (error)
  690. goto fail;
  691. newcred->cr_audit = aia;
  692. proc_set_cred(td->td_proc, newcred);
  693. PROC_UNLOCK(td->td_proc);
  694. crfree(oldcred);
  695. return (0);
  696. fail:
  697. PROC_UNLOCK(td->td_proc);
  698. crfree(newcred);
  699. return (error);
  700. }
  701. /*
  702. * Syscall to manage audit files.
  703. */
  704. /* ARGSUSED */
  705. int
  706. sys_auditctl(struct thread *td, struct auditctl_args *uap)
  707. {
  708. struct nameidata nd;
  709. struct ucred *cred;
  710. struct vnode *vp;
  711. int error = 0;
  712. int flags;
  713. if (jailed(td->td_ucred))
  714. return (ENOSYS);
  715. error = priv_check(td, PRIV_AUDIT_CONTROL);
  716. if (error)
  717. return (error);
  718. vp = NULL;
  719. cred = NULL;
  720. /*
  721. * If a path is specified, open the replacement vnode, perform
  722. * validity checks, and grab another reference to the current
  723. * credential.
  724. *
  725. * On Darwin, a NULL path argument is also used to disable audit.
  726. */
  727. if (uap->path == NULL)
  728. return (EINVAL);
  729. NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
  730. uap->path);
  731. flags = AUDIT_OPEN_FLAGS;
  732. error = vn_open(&nd, &flags, 0, NULL);
  733. if (error)
  734. return (error);
  735. vp = nd.ni_vp;
  736. #ifdef MAC
  737. error = mac_system_check_auditctl(td->td_ucred, vp);
  738. VOP_UNLOCK(vp);
  739. if (error) {
  740. vn_close(vp, AUDIT_CLOSE_FLAGS, td->td_ucred, td);
  741. return (error);
  742. }
  743. #else
  744. VOP_UNLOCK(vp);
  745. #endif
  746. NDFREE_PNBUF(&nd);
  747. if (vp->v_type != VREG) {
  748. vn_close(vp, AUDIT_CLOSE_FLAGS, td->td_ucred, td);
  749. return (EINVAL);
  750. }
  751. cred = td->td_ucred;
  752. crhold(cred);
  753. /*
  754. * XXXAUDIT: Should audit_trail_suspended actually be cleared by
  755. * audit_worker?
  756. */
  757. audit_trail_suspended = 0;
  758. audit_syscalls_enabled_update();
  759. audit_rotate_vnode(cred, vp);
  760. return (error);
  761. }
  762. #else /* !AUDIT */
  763. int
  764. sys_audit(struct thread *td, struct audit_args *uap)
  765. {
  766. return (ENOSYS);
  767. }
  768. int
  769. sys_auditon(struct thread *td, struct auditon_args *uap)
  770. {
  771. return (ENOSYS);
  772. }
  773. int
  774. sys_getauid(struct thread *td, struct getauid_args *uap)
  775. {
  776. return (ENOSYS);
  777. }
  778. int
  779. sys_setauid(struct thread *td, struct setauid_args *uap)
  780. {
  781. return (ENOSYS);
  782. }
  783. int
  784. sys_getaudit(struct thread *td, struct getaudit_args *uap)
  785. {
  786. return (ENOSYS);
  787. }
  788. int
  789. sys_setaudit(struct thread *td, struct setaudit_args *uap)
  790. {
  791. return (ENOSYS);
  792. }
  793. int
  794. sys_getaudit_addr(struct thread *td, struct getaudit_addr_args *uap)
  795. {
  796. return (ENOSYS);
  797. }
  798. int
  799. sys_setaudit_addr(struct thread *td, struct setaudit_addr_args *uap)
  800. {
  801. return (ENOSYS);
  802. }
  803. int
  804. sys_auditctl(struct thread *td, struct auditctl_args *uap)
  805. {
  806. return (ENOSYS);
  807. }
  808. #endif /* AUDIT */