yama_lsm.c 12 KB

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