yama_lsm.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Yama Linux Security Module
  3. *
  4. * Author: Kees Cook <keescook@chromium.org>
  5. *
  6. * Copyright (C) 2010 Canonical, Ltd.
  7. * Copyright (C) 2011 The Chromium OS Authors.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2, as
  11. * published by the Free Software Foundation.
  12. *
  13. */
  14. #include <linux/security.h>
  15. #include <linux/sysctl.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/prctl.h>
  18. #include <linux/ratelimit.h>
  19. #define YAMA_SCOPE_DISABLED 0
  20. #define YAMA_SCOPE_RELATIONAL 1
  21. #define YAMA_SCOPE_CAPABILITY 2
  22. #define YAMA_SCOPE_NO_ATTACH 3
  23. static int ptrace_scope = YAMA_SCOPE_RELATIONAL;
  24. /* describe a ptrace relationship for potential exception */
  25. struct ptrace_relation {
  26. struct task_struct *tracer;
  27. struct task_struct *tracee;
  28. struct list_head node;
  29. };
  30. static LIST_HEAD(ptracer_relations);
  31. static DEFINE_SPINLOCK(ptracer_relations_lock);
  32. /**
  33. * yama_ptracer_add - add/replace an exception for this tracer/tracee pair
  34. * @tracer: the task_struct of the process doing the ptrace
  35. * @tracee: the task_struct of the process to be ptraced
  36. *
  37. * Each tracee can have, at most, one tracer registered. Each time this
  38. * is called, the prior registered tracer will be replaced for the tracee.
  39. *
  40. * Returns 0 if relationship was added, -ve on error.
  41. */
  42. static int yama_ptracer_add(struct task_struct *tracer,
  43. struct task_struct *tracee)
  44. {
  45. int rc = 0;
  46. struct ptrace_relation *added;
  47. struct ptrace_relation *entry, *relation = NULL;
  48. added = kmalloc(sizeof(*added), GFP_KERNEL);
  49. if (!added)
  50. return -ENOMEM;
  51. spin_lock_bh(&ptracer_relations_lock);
  52. list_for_each_entry(entry, &ptracer_relations, node)
  53. if (entry->tracee == tracee) {
  54. relation = entry;
  55. break;
  56. }
  57. if (!relation) {
  58. relation = added;
  59. relation->tracee = tracee;
  60. list_add(&relation->node, &ptracer_relations);
  61. }
  62. relation->tracer = tracer;
  63. spin_unlock_bh(&ptracer_relations_lock);
  64. if (added != relation)
  65. kfree(added);
  66. return rc;
  67. }
  68. /**
  69. * yama_ptracer_del - remove exceptions related to the given tasks
  70. * @tracer: remove any relation where tracer task matches
  71. * @tracee: remove any relation where tracee task matches
  72. */
  73. static void yama_ptracer_del(struct task_struct *tracer,
  74. struct task_struct *tracee)
  75. {
  76. struct ptrace_relation *relation, *safe;
  77. spin_lock_bh(&ptracer_relations_lock);
  78. list_for_each_entry_safe(relation, safe, &ptracer_relations, node)
  79. if (relation->tracee == tracee ||
  80. (tracer && relation->tracer == tracer)) {
  81. list_del(&relation->node);
  82. kfree(relation);
  83. }
  84. spin_unlock_bh(&ptracer_relations_lock);
  85. }
  86. /**
  87. * yama_task_free - check for task_pid to remove from exception list
  88. * @task: task being removed
  89. */
  90. static void yama_task_free(struct task_struct *task)
  91. {
  92. yama_ptracer_del(task, task);
  93. }
  94. /**
  95. * yama_task_prctl - check for Yama-specific prctl operations
  96. * @option: operation
  97. * @arg2: argument
  98. * @arg3: argument
  99. * @arg4: argument
  100. * @arg5: argument
  101. *
  102. * Return 0 on success, -ve on error. -ENOSYS is returned when Yama
  103. * does not handle the given option.
  104. */
  105. static int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
  106. unsigned long arg4, unsigned long arg5)
  107. {
  108. int rc;
  109. struct task_struct *myself = current;
  110. rc = cap_task_prctl(option, arg2, arg3, arg4, arg5);
  111. if (rc != -ENOSYS)
  112. return rc;
  113. switch (option) {
  114. case PR_SET_PTRACER:
  115. /* Since a thread can call prctl(), find the group leader
  116. * before calling _add() or _del() on it, since we want
  117. * process-level granularity of control. The tracer group
  118. * leader checking is handled later when walking the ancestry
  119. * at the time of PTRACE_ATTACH check.
  120. */
  121. rcu_read_lock();
  122. if (!thread_group_leader(myself))
  123. myself = rcu_dereference(myself->group_leader);
  124. get_task_struct(myself);
  125. rcu_read_unlock();
  126. if (arg2 == 0) {
  127. yama_ptracer_del(NULL, myself);
  128. rc = 0;
  129. } else if (arg2 == PR_SET_PTRACER_ANY) {
  130. rc = yama_ptracer_add(NULL, myself);
  131. } else {
  132. struct task_struct *tracer;
  133. rcu_read_lock();
  134. tracer = find_task_by_vpid(arg2);
  135. if (tracer)
  136. get_task_struct(tracer);
  137. else
  138. rc = -EINVAL;
  139. rcu_read_unlock();
  140. if (tracer) {
  141. rc = yama_ptracer_add(tracer, myself);
  142. put_task_struct(tracer);
  143. }
  144. }
  145. put_task_struct(myself);
  146. break;
  147. }
  148. return rc;
  149. }
  150. /**
  151. * task_is_descendant - walk up a process family tree looking for a match
  152. * @parent: the process to compare against while walking up from child
  153. * @child: the process to start from while looking upwards for parent
  154. *
  155. * Returns 1 if child is a descendant of parent, 0 if not.
  156. */
  157. static int task_is_descendant(struct task_struct *parent,
  158. struct task_struct *child)
  159. {
  160. int rc = 0;
  161. struct task_struct *walker = child;
  162. if (!parent || !child)
  163. return 0;
  164. rcu_read_lock();
  165. if (!thread_group_leader(parent))
  166. parent = rcu_dereference(parent->group_leader);
  167. while (walker->pid > 0) {
  168. if (!thread_group_leader(walker))
  169. walker = rcu_dereference(walker->group_leader);
  170. if (walker == parent) {
  171. rc = 1;
  172. break;
  173. }
  174. walker = rcu_dereference(walker->real_parent);
  175. }
  176. rcu_read_unlock();
  177. return rc;
  178. }
  179. /**
  180. * ptracer_exception_found - tracer registered as exception for this tracee
  181. * @tracer: the task_struct of the process attempting ptrace
  182. * @tracee: the task_struct of the process to be ptraced
  183. *
  184. * Returns 1 if tracer has is ptracer exception ancestor for tracee.
  185. */
  186. static int ptracer_exception_found(struct task_struct *tracer,
  187. struct task_struct *tracee)
  188. {
  189. int rc = 0;
  190. struct ptrace_relation *relation;
  191. struct task_struct *parent = NULL;
  192. bool found = false;
  193. spin_lock_bh(&ptracer_relations_lock);
  194. rcu_read_lock();
  195. if (!thread_group_leader(tracee))
  196. tracee = rcu_dereference(tracee->group_leader);
  197. list_for_each_entry(relation, &ptracer_relations, node)
  198. if (relation->tracee == tracee) {
  199. parent = relation->tracer;
  200. found = true;
  201. break;
  202. }
  203. if (found && (parent == NULL || task_is_descendant(parent, tracer)))
  204. rc = 1;
  205. rcu_read_unlock();
  206. spin_unlock_bh(&ptracer_relations_lock);
  207. return rc;
  208. }
  209. /**
  210. * yama_ptrace_access_check - validate PTRACE_ATTACH calls
  211. * @child: task that current task is attempting to ptrace
  212. * @mode: ptrace attach mode
  213. *
  214. * Returns 0 if following the ptrace is allowed, -ve on error.
  215. */
  216. static int yama_ptrace_access_check(struct task_struct *child,
  217. unsigned int mode)
  218. {
  219. int rc;
  220. /* If standard caps disallows it, so does Yama. We should
  221. * only tighten restrictions further.
  222. */
  223. rc = cap_ptrace_access_check(child, mode);
  224. if (rc)
  225. return rc;
  226. /* require ptrace target be a child of ptracer on attach */
  227. if (mode == PTRACE_MODE_ATTACH) {
  228. switch (ptrace_scope) {
  229. case YAMA_SCOPE_DISABLED:
  230. /* No additional restrictions. */
  231. break;
  232. case YAMA_SCOPE_RELATIONAL:
  233. if (!task_is_descendant(current, child) &&
  234. !ptracer_exception_found(current, child) &&
  235. !ns_capable(task_user_ns(child), CAP_SYS_PTRACE))
  236. rc = -EPERM;
  237. break;
  238. case YAMA_SCOPE_CAPABILITY:
  239. if (!ns_capable(task_user_ns(child), CAP_SYS_PTRACE))
  240. rc = -EPERM;
  241. break;
  242. case YAMA_SCOPE_NO_ATTACH:
  243. default:
  244. rc = -EPERM;
  245. break;
  246. }
  247. }
  248. if (rc) {
  249. char name[sizeof(current->comm)];
  250. printk_ratelimited(KERN_NOTICE
  251. "ptrace of pid %d was attempted by: %s (pid %d)\n",
  252. child->pid,
  253. get_task_comm(name, current),
  254. current->pid);
  255. }
  256. return rc;
  257. }
  258. static struct security_operations yama_ops = {
  259. .name = "yama",
  260. .ptrace_access_check = yama_ptrace_access_check,
  261. .task_prctl = yama_task_prctl,
  262. .task_free = yama_task_free,
  263. };
  264. #ifdef CONFIG_SYSCTL
  265. static int yama_dointvec_minmax(struct ctl_table *table, int write,
  266. void __user *buffer, size_t *lenp, loff_t *ppos)
  267. {
  268. int rc;
  269. if (write && !capable(CAP_SYS_PTRACE))
  270. return -EPERM;
  271. rc = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  272. if (rc)
  273. return rc;
  274. /* Lock the max value if it ever gets set. */
  275. if (write && *(int *)table->data == *(int *)table->extra2)
  276. table->extra1 = table->extra2;
  277. return rc;
  278. }
  279. static int zero;
  280. static int max_scope = YAMA_SCOPE_NO_ATTACH;
  281. struct ctl_path yama_sysctl_path[] = {
  282. { .procname = "kernel", },
  283. { .procname = "yama", },
  284. { }
  285. };
  286. static struct ctl_table yama_sysctl_table[] = {
  287. {
  288. .procname = "ptrace_scope",
  289. .data = &ptrace_scope,
  290. .maxlen = sizeof(int),
  291. .mode = 0644,
  292. .proc_handler = yama_dointvec_minmax,
  293. .extra1 = &zero,
  294. .extra2 = &max_scope,
  295. },
  296. { }
  297. };
  298. #endif /* CONFIG_SYSCTL */
  299. static __init int yama_init(void)
  300. {
  301. if (!security_module_enable(&yama_ops))
  302. return 0;
  303. printk(KERN_INFO "Yama: becoming mindful.\n");
  304. if (register_security(&yama_ops))
  305. panic("Yama: kernel registration failed.\n");
  306. #ifdef CONFIG_SYSCTL
  307. if (!register_sysctl_paths(yama_sysctl_path, yama_sysctl_table))
  308. panic("Yama: sysctl registration failed.\n");
  309. #endif
  310. return 0;
  311. }
  312. security_initcall(yama_init);