yama_lsm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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/lsm_hooks.h>
  15. #include <linux/sysctl.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/prctl.h>
  18. #include <linux/ratelimit.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/string_helpers.h>
  21. #include <linux/task_work.h>
  22. #include <linux/sched.h>
  23. #include <linux/spinlock.h>
  24. #define YAMA_SCOPE_DISABLED 0
  25. #define YAMA_SCOPE_RELATIONAL 1
  26. #define YAMA_SCOPE_CAPABILITY 2
  27. #define YAMA_SCOPE_NO_ATTACH 3
  28. static int ptrace_scope = YAMA_SCOPE_DISABLED;
  29. /* describe a ptrace relationship for potential exception */
  30. struct ptrace_relation {
  31. struct task_struct *tracer;
  32. struct task_struct *tracee;
  33. bool invalid;
  34. struct list_head node;
  35. struct rcu_head rcu;
  36. };
  37. static LIST_HEAD(ptracer_relations);
  38. static DEFINE_SPINLOCK(ptracer_relations_lock);
  39. static void yama_relation_cleanup(struct work_struct *work);
  40. static DECLARE_WORK(yama_relation_work, yama_relation_cleanup);
  41. struct access_report_info {
  42. struct callback_head work;
  43. const char *access;
  44. struct task_struct *target;
  45. struct task_struct *agent;
  46. };
  47. static void __report_access(struct callback_head *work)
  48. {
  49. struct access_report_info *info =
  50. container_of(work, struct access_report_info, work);
  51. char *target_cmd, *agent_cmd;
  52. target_cmd = kstrdup_quotable_cmdline(info->target, GFP_KERNEL);
  53. agent_cmd = kstrdup_quotable_cmdline(info->agent, GFP_KERNEL);
  54. pr_notice_ratelimited(
  55. "ptrace %s of \"%s\"[%d] was attempted by \"%s\"[%d]\n",
  56. info->access, target_cmd, info->target->pid, agent_cmd,
  57. info->agent->pid);
  58. kfree(agent_cmd);
  59. kfree(target_cmd);
  60. put_task_struct(info->agent);
  61. put_task_struct(info->target);
  62. kfree(info);
  63. }
  64. /* defers execution because cmdline access can sleep */
  65. static void report_access(const char *access, struct task_struct *target,
  66. struct task_struct *agent)
  67. {
  68. struct access_report_info *info;
  69. char agent_comm[sizeof(agent->comm)];
  70. assert_spin_locked(&target->alloc_lock); /* for target->comm */
  71. if (current->flags & PF_KTHREAD) {
  72. /* I don't think kthreads call task_work_run() before exiting.
  73. * Imagine angry ranting about procfs here.
  74. */
  75. pr_notice_ratelimited(
  76. "ptrace %s of \"%s\"[%d] was attempted by \"%s\"[%d]\n",
  77. access, target->comm, target->pid,
  78. get_task_comm(agent_comm, agent), agent->pid);
  79. return;
  80. }
  81. info = kmalloc(sizeof(*info), GFP_ATOMIC);
  82. if (!info)
  83. return;
  84. init_task_work(&info->work, __report_access);
  85. get_task_struct(target);
  86. get_task_struct(agent);
  87. info->access = access;
  88. info->target = target;
  89. info->agent = agent;
  90. if (task_work_add(current, &info->work, true) == 0)
  91. return; /* success */
  92. WARN(1, "report_access called from exiting task");
  93. put_task_struct(target);
  94. put_task_struct(agent);
  95. kfree(info);
  96. }
  97. /**
  98. * yama_relation_cleanup - remove invalid entries from the relation list
  99. *
  100. */
  101. static void yama_relation_cleanup(struct work_struct *work)
  102. {
  103. struct ptrace_relation *relation;
  104. spin_lock(&ptracer_relations_lock);
  105. rcu_read_lock();
  106. list_for_each_entry_rcu(relation, &ptracer_relations, node) {
  107. if (relation->invalid) {
  108. list_del_rcu(&relation->node);
  109. kfree_rcu(relation, rcu);
  110. }
  111. }
  112. rcu_read_unlock();
  113. spin_unlock(&ptracer_relations_lock);
  114. }
  115. /**
  116. * yama_ptracer_add - add/replace an exception for this tracer/tracee pair
  117. * @tracer: the task_struct of the process doing the ptrace
  118. * @tracee: the task_struct of the process to be ptraced
  119. *
  120. * Each tracee can have, at most, one tracer registered. Each time this
  121. * is called, the prior registered tracer will be replaced for the tracee.
  122. *
  123. * Returns 0 if relationship was added, -ve on error.
  124. */
  125. static int yama_ptracer_add(struct task_struct *tracer,
  126. struct task_struct *tracee)
  127. {
  128. struct ptrace_relation *relation, *added;
  129. added = kmalloc(sizeof(*added), GFP_KERNEL);
  130. if (!added)
  131. return -ENOMEM;
  132. added->tracee = tracee;
  133. added->tracer = tracer;
  134. added->invalid = false;
  135. spin_lock(&ptracer_relations_lock);
  136. rcu_read_lock();
  137. list_for_each_entry_rcu(relation, &ptracer_relations, node) {
  138. if (relation->invalid)
  139. continue;
  140. if (relation->tracee == tracee) {
  141. list_replace_rcu(&relation->node, &added->node);
  142. kfree_rcu(relation, rcu);
  143. goto out;
  144. }
  145. }
  146. list_add_rcu(&added->node, &ptracer_relations);
  147. out:
  148. rcu_read_unlock();
  149. spin_unlock(&ptracer_relations_lock);
  150. return 0;
  151. }
  152. /**
  153. * yama_ptracer_del - remove exceptions related to the given tasks
  154. * @tracer: remove any relation where tracer task matches
  155. * @tracee: remove any relation where tracee task matches
  156. */
  157. static void yama_ptracer_del(struct task_struct *tracer,
  158. struct task_struct *tracee)
  159. {
  160. struct ptrace_relation *relation;
  161. bool marked = false;
  162. rcu_read_lock();
  163. list_for_each_entry_rcu(relation, &ptracer_relations, node) {
  164. if (relation->invalid)
  165. continue;
  166. if (relation->tracee == tracee ||
  167. (tracer && relation->tracer == tracer)) {
  168. relation->invalid = true;
  169. marked = true;
  170. }
  171. }
  172. rcu_read_unlock();
  173. if (marked)
  174. schedule_work(&yama_relation_work);
  175. }
  176. /**
  177. * yama_task_free - check for task_pid to remove from exception list
  178. * @task: task being removed
  179. */
  180. void yama_task_free(struct task_struct *task)
  181. {
  182. yama_ptracer_del(task, task);
  183. }
  184. /**
  185. * yama_task_prctl - check for Yama-specific prctl operations
  186. * @option: operation
  187. * @arg2: argument
  188. * @arg3: argument
  189. * @arg4: argument
  190. * @arg5: argument
  191. *
  192. * Return 0 on success, -ve on error. -ENOSYS is returned when Yama
  193. * does not handle the given option.
  194. */
  195. int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
  196. unsigned long arg4, unsigned long arg5)
  197. {
  198. int rc = -ENOSYS;
  199. struct task_struct *myself = current;
  200. switch (option) {
  201. case PR_SET_PTRACER:
  202. /* Since a thread can call prctl(), find the group leader
  203. * before calling _add() or _del() on it, since we want
  204. * process-level granularity of control. The tracer group
  205. * leader checking is handled later when walking the ancestry
  206. * at the time of PTRACE_ATTACH check.
  207. */
  208. rcu_read_lock();
  209. if (!thread_group_leader(myself))
  210. myself = rcu_dereference(myself->group_leader);
  211. get_task_struct(myself);
  212. rcu_read_unlock();
  213. if (arg2 == 0) {
  214. yama_ptracer_del(NULL, myself);
  215. rc = 0;
  216. } else if (arg2 == PR_SET_PTRACER_ANY || (int)arg2 == -1) {
  217. rc = yama_ptracer_add(NULL, myself);
  218. } else {
  219. struct task_struct *tracer;
  220. tracer = find_get_task_by_vpid(arg2);
  221. if (!tracer) {
  222. rc = -EINVAL;
  223. } else {
  224. rc = yama_ptracer_add(tracer, myself);
  225. put_task_struct(tracer);
  226. }
  227. }
  228. put_task_struct(myself);
  229. break;
  230. }
  231. return rc;
  232. }
  233. /**
  234. * task_is_descendant - walk up a process family tree looking for a match
  235. * @parent: the process to compare against while walking up from child
  236. * @child: the process to start from while looking upwards for parent
  237. *
  238. * Returns 1 if child is a descendant of parent, 0 if not.
  239. */
  240. static int task_is_descendant(struct task_struct *parent,
  241. struct task_struct *child)
  242. {
  243. int rc = 0;
  244. struct task_struct *walker = child;
  245. if (!parent || !child)
  246. return 0;
  247. rcu_read_lock();
  248. if (!thread_group_leader(parent))
  249. parent = rcu_dereference(parent->group_leader);
  250. while (walker->pid > 0) {
  251. if (!thread_group_leader(walker))
  252. walker = rcu_dereference(walker->group_leader);
  253. if (walker == parent) {
  254. rc = 1;
  255. break;
  256. }
  257. walker = rcu_dereference(walker->real_parent);
  258. }
  259. rcu_read_unlock();
  260. return rc;
  261. }
  262. /**
  263. * ptracer_exception_found - tracer registered as exception for this tracee
  264. * @tracer: the task_struct of the process attempting ptrace
  265. * @tracee: the task_struct of the process to be ptraced
  266. *
  267. * Returns 1 if tracer has a ptracer exception ancestor for tracee.
  268. */
  269. static int ptracer_exception_found(struct task_struct *tracer,
  270. struct task_struct *tracee)
  271. {
  272. int rc = 0;
  273. struct ptrace_relation *relation;
  274. struct task_struct *parent = NULL;
  275. bool found = false;
  276. rcu_read_lock();
  277. /*
  278. * If there's already an active tracing relationship, then make an
  279. * exception for the sake of other accesses, like process_vm_rw().
  280. */
  281. parent = ptrace_parent(tracee);
  282. if (parent != NULL && same_thread_group(parent, tracer)) {
  283. rc = 1;
  284. goto unlock;
  285. }
  286. /* Look for a PR_SET_PTRACER relationship. */
  287. if (!thread_group_leader(tracee))
  288. tracee = rcu_dereference(tracee->group_leader);
  289. list_for_each_entry_rcu(relation, &ptracer_relations, node) {
  290. if (relation->invalid)
  291. continue;
  292. if (relation->tracee == tracee) {
  293. parent = relation->tracer;
  294. found = true;
  295. break;
  296. }
  297. }
  298. if (found && (parent == NULL || task_is_descendant(parent, tracer)))
  299. rc = 1;
  300. unlock:
  301. rcu_read_unlock();
  302. return rc;
  303. }
  304. /**
  305. * yama_ptrace_access_check - validate PTRACE_ATTACH calls
  306. * @child: task that current task is attempting to ptrace
  307. * @mode: ptrace attach mode
  308. *
  309. * Returns 0 if following the ptrace is allowed, -ve on error.
  310. */
  311. static int yama_ptrace_access_check(struct task_struct *child,
  312. unsigned int mode)
  313. {
  314. int rc = 0;
  315. /* require ptrace target be a child of ptracer on attach */
  316. if (mode & PTRACE_MODE_ATTACH) {
  317. switch (ptrace_scope) {
  318. case YAMA_SCOPE_DISABLED:
  319. /* No additional restrictions. */
  320. break;
  321. case YAMA_SCOPE_RELATIONAL:
  322. rcu_read_lock();
  323. if (!pid_alive(child))
  324. rc = -EPERM;
  325. if (!rc && !task_is_descendant(current, child) &&
  326. !ptracer_exception_found(current, child) &&
  327. !ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE))
  328. rc = -EPERM;
  329. rcu_read_unlock();
  330. break;
  331. case YAMA_SCOPE_CAPABILITY:
  332. rcu_read_lock();
  333. if (!ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE))
  334. rc = -EPERM;
  335. rcu_read_unlock();
  336. break;
  337. case YAMA_SCOPE_NO_ATTACH:
  338. default:
  339. rc = -EPERM;
  340. break;
  341. }
  342. }
  343. if (rc && (mode & PTRACE_MODE_NOAUDIT) == 0)
  344. report_access("attach", child, current);
  345. return rc;
  346. }
  347. /**
  348. * yama_ptrace_traceme - validate PTRACE_TRACEME calls
  349. * @parent: task that will become the ptracer of the current task
  350. *
  351. * Returns 0 if following the ptrace is allowed, -ve on error.
  352. */
  353. int yama_ptrace_traceme(struct task_struct *parent)
  354. {
  355. int rc = 0;
  356. /* Only disallow PTRACE_TRACEME on more aggressive settings. */
  357. switch (ptrace_scope) {
  358. case YAMA_SCOPE_CAPABILITY:
  359. if (!has_ns_capability(parent, current_user_ns(), CAP_SYS_PTRACE))
  360. rc = -EPERM;
  361. break;
  362. case YAMA_SCOPE_NO_ATTACH:
  363. rc = -EPERM;
  364. break;
  365. }
  366. if (rc) {
  367. task_lock(current);
  368. report_access("traceme", current, parent);
  369. task_unlock(current);
  370. }
  371. return rc;
  372. }
  373. static struct security_hook_list yama_hooks[] __lsm_ro_after_init = {
  374. LSM_HOOK_INIT(ptrace_access_check, yama_ptrace_access_check),
  375. LSM_HOOK_INIT(ptrace_traceme, yama_ptrace_traceme),
  376. LSM_HOOK_INIT(task_prctl, yama_task_prctl),
  377. LSM_HOOK_INIT(task_free, yama_task_free),
  378. };
  379. #ifdef CONFIG_SYSCTL
  380. static int yama_dointvec_minmax(struct ctl_table *table, int write,
  381. void __user *buffer, size_t *lenp, loff_t *ppos)
  382. {
  383. struct ctl_table table_copy;
  384. if (write && !capable(CAP_SYS_PTRACE))
  385. return -EPERM;
  386. /* Lock the max value if it ever gets set. */
  387. table_copy = *table;
  388. if (*(int *)table_copy.data == *(int *)table_copy.extra2)
  389. table_copy.extra1 = table_copy.extra2;
  390. return proc_dointvec_minmax(&table_copy, write, buffer, lenp, ppos);
  391. }
  392. static int zero;
  393. static int max_scope = YAMA_SCOPE_NO_ATTACH;
  394. struct ctl_path yama_sysctl_path[] = {
  395. { .procname = "kernel", },
  396. { .procname = "yama", },
  397. { }
  398. };
  399. static struct ctl_table yama_sysctl_table[] = {
  400. {
  401. .procname = "ptrace_scope",
  402. .data = &ptrace_scope,
  403. .maxlen = sizeof(int),
  404. .mode = 0644,
  405. .proc_handler = yama_dointvec_minmax,
  406. .extra1 = &zero,
  407. .extra2 = &max_scope,
  408. },
  409. { }
  410. };
  411. static void __init yama_init_sysctl(void)
  412. {
  413. if (!register_sysctl_paths(yama_sysctl_path, yama_sysctl_table))
  414. panic("Yama: sysctl registration failed.\n");
  415. }
  416. #else
  417. static inline void yama_init_sysctl(void) { }
  418. #endif /* CONFIG_SYSCTL */
  419. void __init yama_add_hooks(void)
  420. {
  421. pr_info("Yama: disabled by default; enable with sysctl kernel.yama.*\n");
  422. security_add_hooks(yama_hooks, ARRAY_SIZE(yama_hooks), "yama");
  423. yama_init_sysctl();
  424. }