mac_syscalls.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /*-
  2. * Copyright (c) 1999-2002, 2006, 2009 Robert N. M. Watson
  3. * Copyright (c) 2001 Ilmar S. Habibulin
  4. * Copyright (c) 2001-2005 Networks Associates Technology, Inc.
  5. * Copyright (c) 2005-2006 SPARTA, Inc.
  6. * Copyright (c) 2008 Apple Inc.
  7. * All rights reserved.
  8. *
  9. * This software was developed by Robert Watson and Ilmar Habibulin for the
  10. * TrustedBSD Project.
  11. *
  12. * This software was developed for the FreeBSD Project in part by Network
  13. * Associates Laboratories, the Security Research Division of Network
  14. * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
  15. * as part of the DARPA CHATS research program.
  16. *
  17. * This software was enhanced by SPARTA ISSO under SPAWAR contract
  18. * N66001-04-C-6019 ("SEFOS").
  19. *
  20. * This software was developed at the University of Cambridge Computer
  21. * Laboratory with support from a grant from Google, Inc.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the above copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  33. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  36. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  37. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  38. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  40. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  41. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  42. * SUCH DAMAGE.
  43. */
  44. #include <sys/cdefs.h>
  45. #include "opt_mac.h"
  46. #include <sys/param.h>
  47. #include <sys/capsicum.h>
  48. #include <sys/fcntl.h>
  49. #include <sys/kernel.h>
  50. #include <sys/lock.h>
  51. #include <sys/malloc.h>
  52. #include <sys/mutex.h>
  53. #include <sys/mac.h>
  54. #include <sys/proc.h>
  55. #include <sys/systm.h>
  56. #include <sys/sysctl.h>
  57. #include <sys/sysproto.h>
  58. #include <sys/vnode.h>
  59. #include <sys/mount.h>
  60. #include <sys/file.h>
  61. #include <sys/namei.h>
  62. #include <sys/socket.h>
  63. #include <sys/pipe.h>
  64. #include <sys/socketvar.h>
  65. #include <security/mac/mac_framework.h>
  66. #include <security/mac/mac_internal.h>
  67. #include <security/mac/mac_policy.h>
  68. #ifdef MAC
  69. FEATURE(security_mac, "Mandatory Access Control Framework support");
  70. static int kern___mac_get_path(struct thread *td, const char *path_p,
  71. struct mac *mac_p, int follow);
  72. static int kern___mac_set_path(struct thread *td, const char *path_p,
  73. struct mac *mac_p, int follow);
  74. int
  75. sys___mac_get_pid(struct thread *td, struct __mac_get_pid_args *uap)
  76. {
  77. char *elements, *buffer;
  78. struct mac mac;
  79. struct proc *tproc;
  80. struct ucred *tcred;
  81. int error;
  82. error = copyin(uap->mac_p, &mac, sizeof(mac));
  83. if (error)
  84. return (error);
  85. error = mac_check_structmac_consistent(&mac);
  86. if (error)
  87. return (error);
  88. tproc = pfind(uap->pid);
  89. if (tproc == NULL)
  90. return (ESRCH);
  91. tcred = NULL; /* Satisfy gcc. */
  92. error = p_cansee(td, tproc);
  93. if (error == 0)
  94. tcred = crhold(tproc->p_ucred);
  95. PROC_UNLOCK(tproc);
  96. if (error)
  97. return (error);
  98. elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
  99. error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
  100. if (error) {
  101. free(elements, M_MACTEMP);
  102. crfree(tcred);
  103. return (error);
  104. }
  105. buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
  106. error = mac_cred_externalize_label(tcred->cr_label, elements,
  107. buffer, mac.m_buflen);
  108. if (error == 0)
  109. error = copyout(buffer, mac.m_string, strlen(buffer)+1);
  110. free(buffer, M_MACTEMP);
  111. free(elements, M_MACTEMP);
  112. crfree(tcred);
  113. return (error);
  114. }
  115. int
  116. sys___mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
  117. {
  118. char *elements, *buffer;
  119. struct mac mac;
  120. int error;
  121. error = copyin(uap->mac_p, &mac, sizeof(mac));
  122. if (error)
  123. return (error);
  124. error = mac_check_structmac_consistent(&mac);
  125. if (error)
  126. return (error);
  127. elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
  128. error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
  129. if (error) {
  130. free(elements, M_MACTEMP);
  131. return (error);
  132. }
  133. buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
  134. error = mac_cred_externalize_label(td->td_ucred->cr_label,
  135. elements, buffer, mac.m_buflen);
  136. if (error == 0)
  137. error = copyout(buffer, mac.m_string, strlen(buffer)+1);
  138. free(buffer, M_MACTEMP);
  139. free(elements, M_MACTEMP);
  140. return (error);
  141. }
  142. int
  143. sys___mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
  144. {
  145. struct ucred *newcred, *oldcred;
  146. struct label *intlabel;
  147. struct proc *p;
  148. struct mac mac;
  149. char *buffer;
  150. int error;
  151. if (!(mac_labeled & MPC_OBJECT_CRED))
  152. return (EINVAL);
  153. error = copyin(uap->mac_p, &mac, sizeof(mac));
  154. if (error)
  155. return (error);
  156. error = mac_check_structmac_consistent(&mac);
  157. if (error)
  158. return (error);
  159. buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
  160. error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
  161. if (error) {
  162. free(buffer, M_MACTEMP);
  163. return (error);
  164. }
  165. intlabel = mac_cred_label_alloc();
  166. error = mac_cred_internalize_label(intlabel, buffer);
  167. free(buffer, M_MACTEMP);
  168. if (error)
  169. goto out;
  170. newcred = crget();
  171. p = td->td_proc;
  172. PROC_LOCK(p);
  173. oldcred = p->p_ucred;
  174. error = mac_cred_check_relabel(oldcred, intlabel);
  175. if (error) {
  176. PROC_UNLOCK(p);
  177. crfree(newcred);
  178. goto out;
  179. }
  180. setsugid(p);
  181. crcopy(newcred, oldcred);
  182. mac_cred_relabel(newcred, intlabel);
  183. proc_set_cred(p, newcred);
  184. PROC_UNLOCK(p);
  185. crfree(oldcred);
  186. mac_proc_vm_revoke(td);
  187. out:
  188. mac_cred_label_free(intlabel);
  189. return (error);
  190. }
  191. int
  192. sys___mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
  193. {
  194. char *elements, *buffer;
  195. struct label *intlabel;
  196. struct file *fp;
  197. struct mac mac;
  198. struct vnode *vp;
  199. struct pipe *pipe;
  200. struct socket *so;
  201. cap_rights_t rights;
  202. int error;
  203. error = copyin(uap->mac_p, &mac, sizeof(mac));
  204. if (error)
  205. return (error);
  206. error = mac_check_structmac_consistent(&mac);
  207. if (error)
  208. return (error);
  209. elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
  210. error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
  211. if (error) {
  212. free(elements, M_MACTEMP);
  213. return (error);
  214. }
  215. buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
  216. error = fget(td, uap->fd, cap_rights_init_one(&rights, CAP_MAC_GET),
  217. &fp);
  218. if (error)
  219. goto out;
  220. switch (fp->f_type) {
  221. case DTYPE_FIFO:
  222. case DTYPE_VNODE:
  223. if (!(mac_labeled & MPC_OBJECT_VNODE)) {
  224. error = EINVAL;
  225. goto out_fdrop;
  226. }
  227. vp = fp->f_vnode;
  228. intlabel = mac_vnode_label_alloc();
  229. vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  230. mac_vnode_copy_label(vp->v_label, intlabel);
  231. VOP_UNLOCK(vp);
  232. error = mac_vnode_externalize_label(intlabel, elements,
  233. buffer, mac.m_buflen);
  234. mac_vnode_label_free(intlabel);
  235. break;
  236. case DTYPE_PIPE:
  237. if (!(mac_labeled & MPC_OBJECT_PIPE)) {
  238. error = EINVAL;
  239. goto out_fdrop;
  240. }
  241. pipe = fp->f_data;
  242. intlabel = mac_pipe_label_alloc();
  243. PIPE_LOCK(pipe);
  244. mac_pipe_copy_label(pipe->pipe_pair->pp_label, intlabel);
  245. PIPE_UNLOCK(pipe);
  246. error = mac_pipe_externalize_label(intlabel, elements,
  247. buffer, mac.m_buflen);
  248. mac_pipe_label_free(intlabel);
  249. break;
  250. case DTYPE_SOCKET:
  251. if (!(mac_labeled & MPC_OBJECT_SOCKET)) {
  252. error = EINVAL;
  253. goto out_fdrop;
  254. }
  255. so = fp->f_data;
  256. intlabel = mac_socket_label_alloc(M_WAITOK);
  257. SOCK_LOCK(so);
  258. mac_socket_copy_label(so->so_label, intlabel);
  259. SOCK_UNLOCK(so);
  260. error = mac_socket_externalize_label(intlabel, elements,
  261. buffer, mac.m_buflen);
  262. mac_socket_label_free(intlabel);
  263. break;
  264. default:
  265. error = EINVAL;
  266. }
  267. if (error == 0)
  268. error = copyout(buffer, mac.m_string, strlen(buffer)+1);
  269. out_fdrop:
  270. fdrop(fp, td);
  271. out:
  272. free(buffer, M_MACTEMP);
  273. free(elements, M_MACTEMP);
  274. return (error);
  275. }
  276. int
  277. sys___mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
  278. {
  279. return (kern___mac_get_path(td, uap->path_p, uap->mac_p, FOLLOW));
  280. }
  281. int
  282. sys___mac_get_link(struct thread *td, struct __mac_get_link_args *uap)
  283. {
  284. return (kern___mac_get_path(td, uap->path_p, uap->mac_p, NOFOLLOW));
  285. }
  286. static int
  287. kern___mac_get_path(struct thread *td, const char *path_p, struct mac *mac_p,
  288. int follow)
  289. {
  290. char *elements, *buffer;
  291. struct nameidata nd;
  292. struct label *intlabel;
  293. struct mac mac;
  294. int error;
  295. if (!(mac_labeled & MPC_OBJECT_VNODE))
  296. return (EINVAL);
  297. error = copyin(mac_p, &mac, sizeof(mac));
  298. if (error)
  299. return (error);
  300. error = mac_check_structmac_consistent(&mac);
  301. if (error)
  302. return (error);
  303. elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
  304. error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
  305. if (error) {
  306. free(elements, M_MACTEMP);
  307. return (error);
  308. }
  309. buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
  310. NDINIT(&nd, LOOKUP, LOCKLEAF | follow, UIO_USERSPACE, path_p);
  311. error = namei(&nd);
  312. if (error)
  313. goto out;
  314. intlabel = mac_vnode_label_alloc();
  315. mac_vnode_copy_label(nd.ni_vp->v_label, intlabel);
  316. error = mac_vnode_externalize_label(intlabel, elements, buffer,
  317. mac.m_buflen);
  318. vput(nd.ni_vp);
  319. NDFREE_PNBUF(&nd);
  320. mac_vnode_label_free(intlabel);
  321. if (error == 0)
  322. error = copyout(buffer, mac.m_string, strlen(buffer)+1);
  323. out:
  324. free(buffer, M_MACTEMP);
  325. free(elements, M_MACTEMP);
  326. return (error);
  327. }
  328. int
  329. sys___mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
  330. {
  331. struct label *intlabel;
  332. struct pipe *pipe;
  333. struct socket *so;
  334. struct file *fp;
  335. struct mount *mp;
  336. struct vnode *vp;
  337. struct mac mac;
  338. cap_rights_t rights;
  339. char *buffer;
  340. int error;
  341. error = copyin(uap->mac_p, &mac, sizeof(mac));
  342. if (error)
  343. return (error);
  344. error = mac_check_structmac_consistent(&mac);
  345. if (error)
  346. return (error);
  347. buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
  348. error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
  349. if (error) {
  350. free(buffer, M_MACTEMP);
  351. return (error);
  352. }
  353. error = fget(td, uap->fd, cap_rights_init_one(&rights, CAP_MAC_SET),
  354. &fp);
  355. if (error)
  356. goto out;
  357. switch (fp->f_type) {
  358. case DTYPE_FIFO:
  359. case DTYPE_VNODE:
  360. if (!(mac_labeled & MPC_OBJECT_VNODE)) {
  361. error = EINVAL;
  362. goto out_fdrop;
  363. }
  364. intlabel = mac_vnode_label_alloc();
  365. error = mac_vnode_internalize_label(intlabel, buffer);
  366. if (error) {
  367. mac_vnode_label_free(intlabel);
  368. break;
  369. }
  370. vp = fp->f_vnode;
  371. error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH);
  372. if (error != 0) {
  373. mac_vnode_label_free(intlabel);
  374. break;
  375. }
  376. vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  377. error = vn_setlabel(vp, intlabel, td->td_ucred);
  378. VOP_UNLOCK(vp);
  379. vn_finished_write(mp);
  380. mac_vnode_label_free(intlabel);
  381. break;
  382. case DTYPE_PIPE:
  383. if (!(mac_labeled & MPC_OBJECT_PIPE)) {
  384. error = EINVAL;
  385. goto out_fdrop;
  386. }
  387. intlabel = mac_pipe_label_alloc();
  388. error = mac_pipe_internalize_label(intlabel, buffer);
  389. if (error == 0) {
  390. pipe = fp->f_data;
  391. PIPE_LOCK(pipe);
  392. error = mac_pipe_label_set(td->td_ucred,
  393. pipe->pipe_pair, intlabel);
  394. PIPE_UNLOCK(pipe);
  395. }
  396. mac_pipe_label_free(intlabel);
  397. break;
  398. case DTYPE_SOCKET:
  399. if (!(mac_labeled & MPC_OBJECT_SOCKET)) {
  400. error = EINVAL;
  401. goto out_fdrop;
  402. }
  403. intlabel = mac_socket_label_alloc(M_WAITOK);
  404. error = mac_socket_internalize_label(intlabel, buffer);
  405. if (error == 0) {
  406. so = fp->f_data;
  407. error = mac_socket_label_set(td->td_ucred, so,
  408. intlabel);
  409. }
  410. mac_socket_label_free(intlabel);
  411. break;
  412. default:
  413. error = EINVAL;
  414. }
  415. out_fdrop:
  416. fdrop(fp, td);
  417. out:
  418. free(buffer, M_MACTEMP);
  419. return (error);
  420. }
  421. int
  422. sys___mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
  423. {
  424. return (kern___mac_set_path(td, uap->path_p, uap->mac_p, FOLLOW));
  425. }
  426. int
  427. sys___mac_set_link(struct thread *td, struct __mac_set_link_args *uap)
  428. {
  429. return (kern___mac_set_path(td, uap->path_p, uap->mac_p, NOFOLLOW));
  430. }
  431. static int
  432. kern___mac_set_path(struct thread *td, const char *path_p, struct mac *mac_p,
  433. int follow)
  434. {
  435. struct label *intlabel;
  436. struct nameidata nd;
  437. struct mount *mp;
  438. struct mac mac;
  439. char *buffer;
  440. int error;
  441. if (!(mac_labeled & MPC_OBJECT_VNODE))
  442. return (EINVAL);
  443. error = copyin(mac_p, &mac, sizeof(mac));
  444. if (error)
  445. return (error);
  446. error = mac_check_structmac_consistent(&mac);
  447. if (error)
  448. return (error);
  449. buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
  450. error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
  451. if (error) {
  452. free(buffer, M_MACTEMP);
  453. return (error);
  454. }
  455. intlabel = mac_vnode_label_alloc();
  456. error = mac_vnode_internalize_label(intlabel, buffer);
  457. free(buffer, M_MACTEMP);
  458. if (error)
  459. goto out;
  460. NDINIT(&nd, LOOKUP, LOCKLEAF | follow, UIO_USERSPACE, path_p);
  461. error = namei(&nd);
  462. if (error == 0) {
  463. error = vn_start_write(nd.ni_vp, &mp, V_WAIT | V_PCATCH);
  464. if (error == 0) {
  465. error = vn_setlabel(nd.ni_vp, intlabel,
  466. td->td_ucred);
  467. vn_finished_write(mp);
  468. }
  469. vput(nd.ni_vp);
  470. NDFREE_PNBUF(&nd);
  471. }
  472. out:
  473. mac_vnode_label_free(intlabel);
  474. return (error);
  475. }
  476. int
  477. sys_mac_syscall(struct thread *td, struct mac_syscall_args *uap)
  478. {
  479. struct mac_policy_conf *mpc;
  480. char target[MAC_MAX_POLICY_NAME];
  481. int error;
  482. error = copyinstr(uap->policy, target, sizeof(target), NULL);
  483. if (error)
  484. return (error);
  485. error = ENOSYS;
  486. LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {
  487. if (strcmp(mpc->mpc_name, target) == 0 &&
  488. mpc->mpc_ops->mpo_syscall != NULL) {
  489. error = mpc->mpc_ops->mpo_syscall(td,
  490. uap->call, uap->arg);
  491. goto out;
  492. }
  493. }
  494. if (!LIST_EMPTY(&mac_policy_list)) {
  495. mac_policy_slock_sleep();
  496. LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {
  497. if (strcmp(mpc->mpc_name, target) == 0 &&
  498. mpc->mpc_ops->mpo_syscall != NULL) {
  499. error = mpc->mpc_ops->mpo_syscall(td,
  500. uap->call, uap->arg);
  501. break;
  502. }
  503. }
  504. mac_policy_sunlock_sleep();
  505. }
  506. out:
  507. return (error);
  508. }
  509. #else /* !MAC */
  510. int
  511. sys___mac_get_pid(struct thread *td, struct __mac_get_pid_args *uap)
  512. {
  513. return (ENOSYS);
  514. }
  515. int
  516. sys___mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
  517. {
  518. return (ENOSYS);
  519. }
  520. int
  521. sys___mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
  522. {
  523. return (ENOSYS);
  524. }
  525. int
  526. sys___mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
  527. {
  528. return (ENOSYS);
  529. }
  530. int
  531. sys___mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
  532. {
  533. return (ENOSYS);
  534. }
  535. int
  536. sys___mac_get_link(struct thread *td, struct __mac_get_link_args *uap)
  537. {
  538. return (ENOSYS);
  539. }
  540. int
  541. sys___mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
  542. {
  543. return (ENOSYS);
  544. }
  545. int
  546. sys___mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
  547. {
  548. return (ENOSYS);
  549. }
  550. int
  551. sys___mac_set_link(struct thread *td, struct __mac_set_link_args *uap)
  552. {
  553. return (ENOSYS);
  554. }
  555. int
  556. sys_mac_syscall(struct thread *td, struct mac_syscall_args *uap)
  557. {
  558. return (ENOSYS);
  559. }
  560. #endif /* !MAC */