yama_lsm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 is 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. if (!thread_group_leader(tracee))
  282. tracee = rcu_dereference(tracee->group_leader);
  283. list_for_each_entry_rcu(relation, &ptracer_relations, node) {
  284. if (relation->invalid)
  285. continue;
  286. if (relation->tracee == tracee) {
  287. parent = relation->tracer;
  288. found = true;
  289. break;
  290. }
  291. }
  292. if (found && (parent == NULL || task_is_descendant(parent, tracer)))
  293. rc = 1;
  294. rcu_read_unlock();
  295. return rc;
  296. }
  297. /**
  298. * yama_ptrace_access_check - validate PTRACE_ATTACH calls
  299. * @child: task that current task is attempting to ptrace
  300. * @mode: ptrace attach mode
  301. *
  302. * Returns 0 if following the ptrace is allowed, -ve on error.
  303. */
  304. static int yama_ptrace_access_check(struct task_struct *child,
  305. unsigned int mode)
  306. {
  307. int rc = 0;
  308. /* require ptrace target be a child of ptracer on attach */
  309. if (mode & PTRACE_MODE_ATTACH) {
  310. switch (ptrace_scope) {
  311. case YAMA_SCOPE_DISABLED:
  312. /* No additional restrictions. */
  313. break;
  314. case YAMA_SCOPE_RELATIONAL:
  315. rcu_read_lock();
  316. if (!task_is_descendant(current, child) &&
  317. !ptracer_exception_found(current, child) &&
  318. !ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE))
  319. rc = -EPERM;
  320. rcu_read_unlock();
  321. break;
  322. case YAMA_SCOPE_CAPABILITY:
  323. rcu_read_lock();
  324. if (!ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE))
  325. rc = -EPERM;
  326. rcu_read_unlock();
  327. break;
  328. case YAMA_SCOPE_NO_ATTACH:
  329. default:
  330. rc = -EPERM;
  331. break;
  332. }
  333. }
  334. if (rc && (mode & PTRACE_MODE_NOAUDIT) == 0)
  335. report_access("attach", child, current);
  336. return rc;
  337. }
  338. /**
  339. * yama_ptrace_traceme - validate PTRACE_TRACEME calls
  340. * @parent: task that will become the ptracer of the current task
  341. *
  342. * Returns 0 if following the ptrace is allowed, -ve on error.
  343. */
  344. int yama_ptrace_traceme(struct task_struct *parent)
  345. {
  346. int rc = 0;
  347. /* Only disallow PTRACE_TRACEME on more aggressive settings. */
  348. switch (ptrace_scope) {
  349. case YAMA_SCOPE_CAPABILITY:
  350. if (!has_ns_capability(parent, current_user_ns(), CAP_SYS_PTRACE))
  351. rc = -EPERM;
  352. break;
  353. case YAMA_SCOPE_NO_ATTACH:
  354. rc = -EPERM;
  355. break;
  356. }
  357. if (rc) {
  358. task_lock(current);
  359. report_access("traceme", current, parent);
  360. task_unlock(current);
  361. }
  362. return rc;
  363. }
  364. static struct security_hook_list yama_hooks[] = {
  365. LSM_HOOK_INIT(ptrace_access_check, yama_ptrace_access_check),
  366. LSM_HOOK_INIT(ptrace_traceme, yama_ptrace_traceme),
  367. LSM_HOOK_INIT(task_prctl, yama_task_prctl),
  368. LSM_HOOK_INIT(task_free, yama_task_free),
  369. };
  370. #ifdef CONFIG_SYSCTL
  371. static int yama_dointvec_minmax(struct ctl_table *table, int write,
  372. void __user *buffer, size_t *lenp, loff_t *ppos)
  373. {
  374. struct ctl_table table_copy;
  375. if (write && !capable(CAP_SYS_PTRACE))
  376. return -EPERM;
  377. /* Lock the max value if it ever gets set. */
  378. table_copy = *table;
  379. if (*(int *)table_copy.data == *(int *)table_copy.extra2)
  380. table_copy.extra1 = table_copy.extra2;
  381. return proc_dointvec_minmax(&table_copy, write, buffer, lenp, ppos);
  382. }
  383. static int zero;
  384. static int max_scope = YAMA_SCOPE_NO_ATTACH;
  385. struct ctl_path yama_sysctl_path[] = {
  386. { .procname = "kernel", },
  387. { .procname = "yama", },
  388. { }
  389. };
  390. static struct ctl_table yama_sysctl_table[] = {
  391. {
  392. .procname = "ptrace_scope",
  393. .data = &ptrace_scope,
  394. .maxlen = sizeof(int),
  395. .mode = 0644,
  396. .proc_handler = yama_dointvec_minmax,
  397. .extra1 = &zero,
  398. .extra2 = &max_scope,
  399. },
  400. { }
  401. };
  402. static void __init yama_init_sysctl(void)
  403. {
  404. if (!register_sysctl_paths(yama_sysctl_path, yama_sysctl_table))
  405. panic("Yama: sysctl registration failed.\n");
  406. }
  407. #else
  408. static inline void yama_init_sysctl(void) { }
  409. #endif /* CONFIG_SYSCTL */
  410. void __init yama_add_hooks(void)
  411. {
  412. pr_info("Yama: becoming mindful.\n");
  413. security_add_hooks(yama_hooks, ARRAY_SIZE(yama_hooks));
  414. yama_init_sysctl();
  415. }