mac_process.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*-
  2. * Copyright (c) 1999-2002, 2008-2009 Robert N. M. Watson
  3. * Copyright (c) 2001 Ilmar S. Habibulin
  4. * Copyright (c) 2001-2003 Networks Associates Technology, Inc.
  5. * Copyright (c) 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/condvar.h>
  48. #include <sys/imgact.h>
  49. #include <sys/kernel.h>
  50. #include <sys/lock.h>
  51. #include <sys/malloc.h>
  52. #include <sys/mac.h>
  53. #include <sys/proc.h>
  54. #include <sys/rwlock.h>
  55. #include <sys/sbuf.h>
  56. #include <sys/sdt.h>
  57. #include <sys/systm.h>
  58. #include <sys/vnode.h>
  59. #include <sys/mount.h>
  60. #include <sys/file.h>
  61. #include <sys/namei.h>
  62. #include <sys/sysctl.h>
  63. #include <vm/vm.h>
  64. #include <vm/pmap.h>
  65. #include <vm/vm_map.h>
  66. #include <vm/vm_object.h>
  67. #include <security/mac/mac_framework.h>
  68. #include <security/mac/mac_internal.h>
  69. #include <security/mac/mac_policy.h>
  70. static int mac_mmap_revocation = 1;
  71. SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation, CTLFLAG_RW,
  72. &mac_mmap_revocation, 0, "Revoke mmap access to files on subject "
  73. "relabel");
  74. static int mac_mmap_revocation_via_cow = 0;
  75. SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation_via_cow, CTLFLAG_RW,
  76. &mac_mmap_revocation_via_cow, 0, "Revoke mmap access to files via "
  77. "copy-on-write semantics, or by removing all write access");
  78. static void mac_proc_vm_revoke_recurse(struct thread *td,
  79. struct ucred *cred, struct vm_map *map);
  80. static struct label *
  81. mac_proc_label_alloc(void)
  82. {
  83. struct label *label;
  84. label = mac_labelzone_alloc(M_WAITOK);
  85. MAC_POLICY_PERFORM(proc_init_label, label);
  86. return (label);
  87. }
  88. void
  89. mac_proc_init(struct proc *p)
  90. {
  91. if (mac_labeled & MPC_OBJECT_PROC)
  92. p->p_label = mac_proc_label_alloc();
  93. else
  94. p->p_label = NULL;
  95. }
  96. static void
  97. mac_proc_label_free(struct label *label)
  98. {
  99. MAC_POLICY_PERFORM_NOSLEEP(proc_destroy_label, label);
  100. mac_labelzone_free(label);
  101. }
  102. void
  103. mac_proc_destroy(struct proc *p)
  104. {
  105. if (p->p_label != NULL) {
  106. mac_proc_label_free(p->p_label);
  107. p->p_label = NULL;
  108. }
  109. }
  110. void
  111. mac_thread_userret(struct thread *td)
  112. {
  113. MAC_POLICY_PERFORM(thread_userret, td);
  114. }
  115. int
  116. mac_execve_enter(struct image_params *imgp, struct mac *mac_p)
  117. {
  118. struct label *label;
  119. struct mac mac;
  120. char *buffer;
  121. int error;
  122. if (mac_p == NULL)
  123. return (0);
  124. if (!(mac_labeled & MPC_OBJECT_CRED))
  125. return (EINVAL);
  126. error = copyin(mac_p, &mac, sizeof(mac));
  127. if (error)
  128. return (error);
  129. error = mac_check_structmac_consistent(&mac);
  130. if (error)
  131. return (error);
  132. buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
  133. error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
  134. if (error) {
  135. free(buffer, M_MACTEMP);
  136. return (error);
  137. }
  138. label = mac_cred_label_alloc();
  139. error = mac_cred_internalize_label(label, buffer);
  140. free(buffer, M_MACTEMP);
  141. if (error) {
  142. mac_cred_label_free(label);
  143. return (error);
  144. }
  145. imgp->execlabel = label;
  146. return (0);
  147. }
  148. void
  149. mac_execve_exit(struct image_params *imgp)
  150. {
  151. if (imgp->execlabel != NULL) {
  152. mac_cred_label_free(imgp->execlabel);
  153. imgp->execlabel = NULL;
  154. }
  155. }
  156. void
  157. mac_execve_interpreter_enter(struct vnode *interpvp,
  158. struct label **interpvplabel)
  159. {
  160. if (mac_labeled & MPC_OBJECT_VNODE) {
  161. *interpvplabel = mac_vnode_label_alloc();
  162. mac_vnode_copy_label(interpvp->v_label, *interpvplabel);
  163. } else
  164. *interpvplabel = NULL;
  165. }
  166. void
  167. mac_execve_interpreter_exit(struct label *interpvplabel)
  168. {
  169. if (interpvplabel != NULL)
  170. mac_vnode_label_free(interpvplabel);
  171. }
  172. /*
  173. * When relabeling a process, call out to the policies for the maximum
  174. * permission allowed for each object type we know about in its memory space,
  175. * and revoke access (in the least surprising ways we know) when necessary.
  176. * The process lock is not held here.
  177. */
  178. void
  179. mac_proc_vm_revoke(struct thread *td)
  180. {
  181. struct ucred *cred;
  182. PROC_LOCK(td->td_proc);
  183. cred = crhold(td->td_proc->p_ucred);
  184. PROC_UNLOCK(td->td_proc);
  185. /* XXX freeze all other threads */
  186. mac_proc_vm_revoke_recurse(td, cred,
  187. &td->td_proc->p_vmspace->vm_map);
  188. /* XXX allow other threads to continue */
  189. crfree(cred);
  190. }
  191. static __inline const char *
  192. prot2str(vm_prot_t prot)
  193. {
  194. switch (prot & VM_PROT_ALL) {
  195. case VM_PROT_READ:
  196. return ("r--");
  197. case VM_PROT_READ | VM_PROT_WRITE:
  198. return ("rw-");
  199. case VM_PROT_READ | VM_PROT_EXECUTE:
  200. return ("r-x");
  201. case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
  202. return ("rwx");
  203. case VM_PROT_WRITE:
  204. return ("-w-");
  205. case VM_PROT_EXECUTE:
  206. return ("--x");
  207. case VM_PROT_WRITE | VM_PROT_EXECUTE:
  208. return ("-wx");
  209. default:
  210. return ("---");
  211. }
  212. }
  213. static void
  214. mac_proc_vm_revoke_recurse(struct thread *td, struct ucred *cred,
  215. struct vm_map *map)
  216. {
  217. vm_map_entry_t prev, vme;
  218. int result;
  219. vm_prot_t revokeperms;
  220. vm_object_t backing_object, object;
  221. vm_ooffset_t offset;
  222. struct vnode *vp;
  223. struct mount *mp;
  224. if (!mac_mmap_revocation)
  225. return;
  226. prev = &map->header;
  227. vm_map_lock(map);
  228. for (vme = vm_map_entry_first(map); vme != &map->header;
  229. prev = vme, vme = vm_map_entry_succ(prev)) {
  230. if (vme->eflags & MAP_ENTRY_IS_SUB_MAP) {
  231. mac_proc_vm_revoke_recurse(td, cred,
  232. vme->object.sub_map);
  233. continue;
  234. }
  235. /*
  236. * Skip over entries that obviously are not shared.
  237. */
  238. if (vme->eflags & (MAP_ENTRY_COW | MAP_ENTRY_NOSYNC) ||
  239. !vme->max_protection)
  240. continue;
  241. /*
  242. * Drill down to the deepest backing object.
  243. */
  244. offset = vme->offset;
  245. object = vme->object.vm_object;
  246. if (object == NULL)
  247. continue;
  248. VM_OBJECT_RLOCK(object);
  249. while ((backing_object = object->backing_object) != NULL) {
  250. VM_OBJECT_RLOCK(backing_object);
  251. offset += object->backing_object_offset;
  252. VM_OBJECT_RUNLOCK(object);
  253. object = backing_object;
  254. }
  255. VM_OBJECT_RUNLOCK(object);
  256. /*
  257. * At the moment, vm_maps and objects aren't considered by
  258. * the MAC system, so only things with backing by a normal
  259. * object (read: vnodes) are checked.
  260. */
  261. if (object->type != OBJT_VNODE)
  262. continue;
  263. vp = (struct vnode *)object->handle;
  264. vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  265. result = vme->max_protection;
  266. mac_vnode_check_mmap_downgrade(cred, vp, &result);
  267. VOP_UNLOCK(vp);
  268. /*
  269. * Find out what maximum protection we may be allowing now
  270. * but a policy needs to get removed.
  271. */
  272. revokeperms = vme->max_protection & ~result;
  273. if (!revokeperms)
  274. continue;
  275. printf("pid %ld: revoking %s perms from %#lx:%ld "
  276. "(max %s/cur %s)\n", (long)td->td_proc->p_pid,
  277. prot2str(revokeperms), (u_long)vme->start,
  278. (long)(vme->end - vme->start),
  279. prot2str(vme->max_protection), prot2str(vme->protection));
  280. /*
  281. * This is the really simple case: if a map has more
  282. * max_protection than is allowed, but it's not being
  283. * actually used (that is, the current protection is still
  284. * allowed), we can just wipe it out and do nothing more.
  285. */
  286. if ((vme->protection & revokeperms) == 0) {
  287. vme->max_protection -= revokeperms;
  288. } else {
  289. if (revokeperms & VM_PROT_WRITE) {
  290. /*
  291. * In the more complicated case, flush out all
  292. * pending changes to the object then turn it
  293. * copy-on-write.
  294. */
  295. vm_object_reference(object);
  296. (void) vn_start_write(vp, &mp, V_WAIT);
  297. vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  298. VM_OBJECT_WLOCK(object);
  299. vm_object_page_clean(object, offset, offset +
  300. vme->end - vme->start, OBJPC_SYNC);
  301. VM_OBJECT_WUNLOCK(object);
  302. VOP_UNLOCK(vp);
  303. vn_finished_write(mp);
  304. vm_object_deallocate(object);
  305. /*
  306. * Why bother if there's no read permissions
  307. * anymore? For the rest, we need to leave
  308. * the write permissions on for COW, or
  309. * remove them entirely if configured to.
  310. */
  311. if (!mac_mmap_revocation_via_cow) {
  312. vme->max_protection &= ~VM_PROT_WRITE;
  313. vme->protection &= ~VM_PROT_WRITE;
  314. } if ((revokeperms & VM_PROT_READ) == 0)
  315. vme->eflags |= MAP_ENTRY_COW |
  316. MAP_ENTRY_NEEDS_COPY;
  317. }
  318. if (revokeperms & VM_PROT_EXECUTE) {
  319. vme->max_protection &= ~VM_PROT_EXECUTE;
  320. vme->protection &= ~VM_PROT_EXECUTE;
  321. }
  322. if (revokeperms & VM_PROT_READ) {
  323. vme->max_protection = 0;
  324. vme->protection = 0;
  325. }
  326. pmap_protect(map->pmap, vme->start, vme->end,
  327. vme->protection & ~revokeperms);
  328. vm_map_try_merge_entries(map, prev, vme);
  329. }
  330. }
  331. vm_map_unlock(map);
  332. }
  333. MAC_CHECK_PROBE_DEFINE2(proc_check_debug, "struct ucred *", "struct proc *");
  334. int
  335. mac_proc_check_debug(struct ucred *cred, struct proc *p)
  336. {
  337. int error;
  338. PROC_LOCK_ASSERT(p, MA_OWNED);
  339. MAC_POLICY_CHECK_NOSLEEP(proc_check_debug, cred, p);
  340. MAC_CHECK_PROBE2(proc_check_debug, error, cred, p);
  341. return (error);
  342. }
  343. MAC_CHECK_PROBE_DEFINE2(proc_check_sched, "struct ucred *", "struct proc *");
  344. int
  345. mac_proc_check_sched(struct ucred *cred, struct proc *p)
  346. {
  347. int error;
  348. PROC_LOCK_ASSERT(p, MA_OWNED);
  349. MAC_POLICY_CHECK_NOSLEEP(proc_check_sched, cred, p);
  350. MAC_CHECK_PROBE2(proc_check_sched, error, cred, p);
  351. return (error);
  352. }
  353. MAC_CHECK_PROBE_DEFINE3(proc_check_signal, "struct ucred *", "struct proc *",
  354. "int");
  355. int
  356. mac_proc_check_signal(struct ucred *cred, struct proc *p, int signum)
  357. {
  358. int error;
  359. PROC_LOCK_ASSERT(p, MA_OWNED);
  360. MAC_POLICY_CHECK_NOSLEEP(proc_check_signal, cred, p, signum);
  361. MAC_CHECK_PROBE3(proc_check_signal, error, cred, p, signum);
  362. return (error);
  363. }
  364. MAC_CHECK_PROBE_DEFINE2(proc_check_wait, "struct ucred *", "struct proc *");
  365. int
  366. mac_proc_check_wait(struct ucred *cred, struct proc *p)
  367. {
  368. int error;
  369. PROC_LOCK_ASSERT(p, MA_OWNED);
  370. MAC_POLICY_CHECK_NOSLEEP(proc_check_wait, cred, p);
  371. MAC_CHECK_PROBE2(proc_check_wait, error, cred, p);
  372. return (error);
  373. }