yama_lsm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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_RELATIONAL;
  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. rcu_read_lock();
  221. tracer = find_task_by_vpid(arg2);
  222. if (tracer)
  223. get_task_struct(tracer);
  224. else
  225. rc = -EINVAL;
  226. rcu_read_unlock();
  227. if (tracer) {
  228. rc = yama_ptracer_add(tracer, myself);
  229. put_task_struct(tracer);
  230. }
  231. }
  232. put_task_struct(myself);
  233. break;
  234. }
  235. return rc;
  236. }
  237. /**
  238. * task_is_descendant - walk up a process family tree looking for a match
  239. * @parent: the process to compare against while walking up from child
  240. * @child: the process to start from while looking upwards for parent
  241. *
  242. * Returns 1 if child is a descendant of parent, 0 if not.
  243. */
  244. static int task_is_descendant(struct task_struct *parent,
  245. struct task_struct *child)
  246. {
  247. int rc = 0;
  248. struct task_struct *walker = child;
  249. if (!parent || !child)
  250. return 0;
  251. rcu_read_lock();
  252. if (!thread_group_leader(parent))
  253. parent = rcu_dereference(parent->group_leader);
  254. while (walker->pid > 0) {
  255. if (!thread_group_leader(walker))
  256. walker = rcu_dereference(walker->group_leader);
  257. if (walker == parent) {
  258. rc = 1;
  259. break;
  260. }
  261. walker = rcu_dereference(walker->real_parent);
  262. }
  263. rcu_read_unlock();
  264. return rc;
  265. }
  266. /**
  267. * ptracer_exception_found - tracer registered as exception for this tracee
  268. * @tracer: the task_struct of the process attempting ptrace
  269. * @tracee: the task_struct of the process to be ptraced
  270. *
  271. * Returns 1 if tracer has a ptracer exception ancestor for tracee.
  272. */
  273. static int ptracer_exception_found(struct task_struct *tracer,
  274. struct task_struct *tracee)
  275. {
  276. int rc = 0;
  277. struct ptrace_relation *relation;
  278. struct task_struct *parent = NULL;
  279. bool found = false;
  280. rcu_read_lock();
  281. /*
  282. * If there's already an active tracing relationship, then make an
  283. * exception for the sake of other accesses, like process_vm_rw().
  284. */
  285. parent = ptrace_parent(tracee);
  286. if (parent != NULL && same_thread_group(parent, tracer)) {
  287. rc = 1;
  288. goto unlock;
  289. }
  290. /* Look for a PR_SET_PTRACER relationship. */
  291. if (!thread_group_leader(tracee))
  292. tracee = rcu_dereference(tracee->group_leader);
  293. list_for_each_entry_rcu(relation, &ptracer_relations, node) {
  294. if (relation->invalid)
  295. continue;
  296. if (relation->tracee == tracee) {
  297. parent = relation->tracer;
  298. found = true;
  299. break;
  300. }
  301. }
  302. if (found && (parent == NULL || task_is_descendant(parent, tracer)))
  303. rc = 1;
  304. unlock:
  305. rcu_read_unlock();
  306. return rc;
  307. }
  308. /**
  309. * yama_ptrace_access_check - validate PTRACE_ATTACH calls
  310. * @child: task that current task is attempting to ptrace
  311. * @mode: ptrace attach mode
  312. *
  313. * Returns 0 if following the ptrace is allowed, -ve on error.
  314. */
  315. static int yama_ptrace_access_check(struct task_struct *child,
  316. unsigned int mode)
  317. {
  318. int rc = 0;
  319. /* require ptrace target be a child of ptracer on attach */
  320. if (mode & PTRACE_MODE_ATTACH) {
  321. switch (ptrace_scope) {
  322. case YAMA_SCOPE_DISABLED:
  323. /* No additional restrictions. */
  324. break;
  325. case YAMA_SCOPE_RELATIONAL:
  326. rcu_read_lock();
  327. if (!pid_alive(child))
  328. rc = -EPERM;
  329. if (!rc && !task_is_descendant(current, child) &&
  330. !ptracer_exception_found(current, child) &&
  331. !ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE))
  332. rc = -EPERM;
  333. rcu_read_unlock();
  334. break;
  335. case YAMA_SCOPE_CAPABILITY:
  336. rcu_read_lock();
  337. if (!ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE))
  338. rc = -EPERM;
  339. rcu_read_unlock();
  340. break;
  341. case YAMA_SCOPE_NO_ATTACH:
  342. default:
  343. rc = -EPERM;
  344. break;
  345. }
  346. }
  347. if (rc && (mode & PTRACE_MODE_NOAUDIT) == 0)
  348. report_access("attach", child, current);
  349. return rc;
  350. }
  351. /**
  352. * yama_ptrace_traceme - validate PTRACE_TRACEME calls
  353. * @parent: task that will become the ptracer of the current task
  354. *
  355. * Returns 0 if following the ptrace is allowed, -ve on error.
  356. */
  357. int yama_ptrace_traceme(struct task_struct *parent)
  358. {
  359. int rc = 0;
  360. /* Only disallow PTRACE_TRACEME on more aggressive settings. */
  361. switch (ptrace_scope) {
  362. case YAMA_SCOPE_CAPABILITY:
  363. if (!has_ns_capability(parent, current_user_ns(), CAP_SYS_PTRACE))
  364. rc = -EPERM;
  365. break;
  366. case YAMA_SCOPE_NO_ATTACH:
  367. rc = -EPERM;
  368. break;
  369. }
  370. if (rc) {
  371. task_lock(current);
  372. report_access("traceme", current, parent);
  373. task_unlock(current);
  374. }
  375. return rc;
  376. }
  377. static struct security_hook_list yama_hooks[] __lsm_ro_after_init = {
  378. LSM_HOOK_INIT(ptrace_access_check, yama_ptrace_access_check),
  379. LSM_HOOK_INIT(ptrace_traceme, yama_ptrace_traceme),
  380. LSM_HOOK_INIT(task_prctl, yama_task_prctl),
  381. LSM_HOOK_INIT(task_free, yama_task_free),
  382. };
  383. #ifdef CONFIG_SYSCTL
  384. static int yama_dointvec_minmax(struct ctl_table *table, int write,
  385. void __user *buffer, size_t *lenp, loff_t *ppos)
  386. {
  387. struct ctl_table table_copy;
  388. if (write && !capable(CAP_SYS_PTRACE))
  389. return -EPERM;
  390. /* Lock the max value if it ever gets set. */
  391. table_copy = *table;
  392. if (*(int *)table_copy.data == *(int *)table_copy.extra2)
  393. table_copy.extra1 = table_copy.extra2;
  394. return proc_dointvec_minmax(&table_copy, write, buffer, lenp, ppos);
  395. }
  396. static int zero;
  397. static int max_scope = YAMA_SCOPE_NO_ATTACH;
  398. struct ctl_path yama_sysctl_path[] = {
  399. { .procname = "kernel", },
  400. { .procname = "yama", },
  401. { }
  402. };
  403. static struct ctl_table yama_sysctl_table[] = {
  404. {
  405. .procname = "ptrace_scope",
  406. .data = &ptrace_scope,
  407. .maxlen = sizeof(int),
  408. .mode = 0644,
  409. .proc_handler = yama_dointvec_minmax,
  410. .extra1 = &zero,
  411. .extra2 = &max_scope,
  412. },
  413. { }
  414. };
  415. static void __init yama_init_sysctl(void)
  416. {
  417. if (!register_sysctl_paths(yama_sysctl_path, yama_sysctl_table))
  418. panic("Yama: sysctl registration failed.\n");
  419. }
  420. #else
  421. static inline void yama_init_sysctl(void) { }
  422. #endif /* CONFIG_SYSCTL */
  423. void __init yama_add_hooks(void)
  424. {
  425. pr_info("Yama: becoming mindful.\n");
  426. security_add_hooks(yama_hooks, ARRAY_SIZE(yama_hooks), "yama");
  427. yama_init_sysctl();
  428. }