lsm.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SafeSetID Linux Security Module
  4. *
  5. * Author: Micah Morton <mortonm@chromium.org>
  6. *
  7. * Copyright (C) 2018 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. #define pr_fmt(fmt) "SafeSetID: " fmt
  15. #include <linux/lsm_hooks.h>
  16. #include <linux/module.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/sched/task_stack.h>
  19. #include <linux/security.h>
  20. #include "lsm.h"
  21. /* Flag indicating whether initialization completed */
  22. int safesetid_initialized __initdata;
  23. struct setid_ruleset __rcu *safesetid_setuid_rules;
  24. struct setid_ruleset __rcu *safesetid_setgid_rules;
  25. /* Compute a decision for a transition from @src to @dst under @policy. */
  26. enum sid_policy_type _setid_policy_lookup(struct setid_ruleset *policy,
  27. kid_t src, kid_t dst)
  28. {
  29. struct setid_rule *rule;
  30. enum sid_policy_type result = SIDPOL_DEFAULT;
  31. if (policy->type == UID) {
  32. hash_for_each_possible(policy->rules, rule, next, __kuid_val(src.uid)) {
  33. if (!uid_eq(rule->src_id.uid, src.uid))
  34. continue;
  35. if (uid_eq(rule->dst_id.uid, dst.uid))
  36. return SIDPOL_ALLOWED;
  37. result = SIDPOL_CONSTRAINED;
  38. }
  39. } else if (policy->type == GID) {
  40. hash_for_each_possible(policy->rules, rule, next, __kgid_val(src.gid)) {
  41. if (!gid_eq(rule->src_id.gid, src.gid))
  42. continue;
  43. if (gid_eq(rule->dst_id.gid, dst.gid)){
  44. return SIDPOL_ALLOWED;
  45. }
  46. result = SIDPOL_CONSTRAINED;
  47. }
  48. } else {
  49. /* Should not reach here, report the ID as contrainsted */
  50. result = SIDPOL_CONSTRAINED;
  51. }
  52. return result;
  53. }
  54. /*
  55. * Compute a decision for a transition from @src to @dst under the active
  56. * policy.
  57. */
  58. static enum sid_policy_type setid_policy_lookup(kid_t src, kid_t dst, enum setid_type new_type)
  59. {
  60. enum sid_policy_type result = SIDPOL_DEFAULT;
  61. struct setid_ruleset *pol;
  62. rcu_read_lock();
  63. if (new_type == UID)
  64. pol = rcu_dereference(safesetid_setuid_rules);
  65. else if (new_type == GID)
  66. pol = rcu_dereference(safesetid_setgid_rules);
  67. else { /* Should not reach here */
  68. result = SIDPOL_CONSTRAINED;
  69. rcu_read_unlock();
  70. return result;
  71. }
  72. if (pol) {
  73. pol->type = new_type;
  74. result = _setid_policy_lookup(pol, src, dst);
  75. }
  76. rcu_read_unlock();
  77. return result;
  78. }
  79. static int safesetid_security_capable(const struct cred *cred,
  80. struct user_namespace *ns,
  81. int cap,
  82. unsigned int opts)
  83. {
  84. /* We're only interested in CAP_SETUID and CAP_SETGID. */
  85. if (cap != CAP_SETUID && cap != CAP_SETGID)
  86. return 0;
  87. /*
  88. * If CAP_SET{U/G}ID is currently used for a setid or setgroups syscall, we
  89. * want to let it go through here; the real security check happens later, in
  90. * the task_fix_set{u/g}id or task_fix_setgroups hooks.
  91. */
  92. if ((opts & CAP_OPT_INSETID) != 0)
  93. return 0;
  94. switch (cap) {
  95. case CAP_SETUID:
  96. /*
  97. * If no policy applies to this task, allow the use of CAP_SETUID for
  98. * other purposes.
  99. */
  100. if (setid_policy_lookup((kid_t){.uid = cred->uid}, INVALID_ID, UID) == SIDPOL_DEFAULT)
  101. return 0;
  102. /*
  103. * Reject use of CAP_SETUID for functionality other than calling
  104. * set*uid() (e.g. setting up userns uid mappings).
  105. */
  106. pr_warn("Operation requires CAP_SETUID, which is not available to UID %u for operations besides approved set*uid transitions\n",
  107. __kuid_val(cred->uid));
  108. return -EPERM;
  109. case CAP_SETGID:
  110. /*
  111. * If no policy applies to this task, allow the use of CAP_SETGID for
  112. * other purposes.
  113. */
  114. if (setid_policy_lookup((kid_t){.gid = cred->gid}, INVALID_ID, GID) == SIDPOL_DEFAULT)
  115. return 0;
  116. /*
  117. * Reject use of CAP_SETUID for functionality other than calling
  118. * set*gid() (e.g. setting up userns gid mappings).
  119. */
  120. pr_warn("Operation requires CAP_SETGID, which is not available to GID %u for operations besides approved set*gid transitions\n",
  121. __kuid_val(cred->uid));
  122. return -EPERM;
  123. default:
  124. /* Error, the only capabilities were checking for is CAP_SETUID/GID */
  125. return 0;
  126. }
  127. return 0;
  128. }
  129. /*
  130. * Check whether a caller with old credentials @old is allowed to switch to
  131. * credentials that contain @new_id.
  132. */
  133. static bool id_permitted_for_cred(const struct cred *old, kid_t new_id, enum setid_type new_type)
  134. {
  135. bool permitted;
  136. /* If our old creds already had this ID in it, it's fine. */
  137. if (new_type == UID) {
  138. if (uid_eq(new_id.uid, old->uid) || uid_eq(new_id.uid, old->euid) ||
  139. uid_eq(new_id.uid, old->suid))
  140. return true;
  141. } else if (new_type == GID){
  142. if (gid_eq(new_id.gid, old->gid) || gid_eq(new_id.gid, old->egid) ||
  143. gid_eq(new_id.gid, old->sgid))
  144. return true;
  145. } else /* Error, new_type is an invalid type */
  146. return false;
  147. /*
  148. * Transitions to new UIDs require a check against the policy of the old
  149. * RUID.
  150. */
  151. permitted =
  152. setid_policy_lookup((kid_t){.uid = old->uid}, new_id, new_type) != SIDPOL_CONSTRAINED;
  153. if (!permitted) {
  154. if (new_type == UID) {
  155. pr_warn("UID transition ((%d,%d,%d) -> %d) blocked\n",
  156. __kuid_val(old->uid), __kuid_val(old->euid),
  157. __kuid_val(old->suid), __kuid_val(new_id.uid));
  158. } else if (new_type == GID) {
  159. pr_warn("GID transition ((%d,%d,%d) -> %d) blocked\n",
  160. __kgid_val(old->gid), __kgid_val(old->egid),
  161. __kgid_val(old->sgid), __kgid_val(new_id.gid));
  162. } else /* Error, new_type is an invalid type */
  163. return false;
  164. }
  165. return permitted;
  166. }
  167. /*
  168. * Check whether there is either an exception for user under old cred struct to
  169. * set*uid to user under new cred struct, or the UID transition is allowed (by
  170. * Linux set*uid rules) even without CAP_SETUID.
  171. */
  172. static int safesetid_task_fix_setuid(struct cred *new,
  173. const struct cred *old,
  174. int flags)
  175. {
  176. /* Do nothing if there are no setuid restrictions for our old RUID. */
  177. if (setid_policy_lookup((kid_t){.uid = old->uid}, INVALID_ID, UID) == SIDPOL_DEFAULT)
  178. return 0;
  179. if (id_permitted_for_cred(old, (kid_t){.uid = new->uid}, UID) &&
  180. id_permitted_for_cred(old, (kid_t){.uid = new->euid}, UID) &&
  181. id_permitted_for_cred(old, (kid_t){.uid = new->suid}, UID) &&
  182. id_permitted_for_cred(old, (kid_t){.uid = new->fsuid}, UID))
  183. return 0;
  184. /*
  185. * Kill this process to avoid potential security vulnerabilities
  186. * that could arise from a missing allowlist entry preventing a
  187. * privileged process from dropping to a lesser-privileged one.
  188. */
  189. force_sig(SIGKILL);
  190. return -EACCES;
  191. }
  192. static int safesetid_task_fix_setgid(struct cred *new,
  193. const struct cred *old,
  194. int flags)
  195. {
  196. /* Do nothing if there are no setgid restrictions for our old RGID. */
  197. if (setid_policy_lookup((kid_t){.gid = old->gid}, INVALID_ID, GID) == SIDPOL_DEFAULT)
  198. return 0;
  199. if (id_permitted_for_cred(old, (kid_t){.gid = new->gid}, GID) &&
  200. id_permitted_for_cred(old, (kid_t){.gid = new->egid}, GID) &&
  201. id_permitted_for_cred(old, (kid_t){.gid = new->sgid}, GID) &&
  202. id_permitted_for_cred(old, (kid_t){.gid = new->fsgid}, GID))
  203. return 0;
  204. /*
  205. * Kill this process to avoid potential security vulnerabilities
  206. * that could arise from a missing allowlist entry preventing a
  207. * privileged process from dropping to a lesser-privileged one.
  208. */
  209. force_sig(SIGKILL);
  210. return -EACCES;
  211. }
  212. static int safesetid_task_fix_setgroups(struct cred *new, const struct cred *old)
  213. {
  214. int i;
  215. /* Do nothing if there are no setgid restrictions for our old RGID. */
  216. if (setid_policy_lookup((kid_t){.gid = old->gid}, INVALID_ID, GID) == SIDPOL_DEFAULT)
  217. return 0;
  218. get_group_info(new->group_info);
  219. for (i = 0; i < new->group_info->ngroups; i++) {
  220. if (!id_permitted_for_cred(old, (kid_t){.gid = new->group_info->gid[i]}, GID)) {
  221. put_group_info(new->group_info);
  222. /*
  223. * Kill this process to avoid potential security vulnerabilities
  224. * that could arise from a missing allowlist entry preventing a
  225. * privileged process from dropping to a lesser-privileged one.
  226. */
  227. force_sig(SIGKILL);
  228. return -EACCES;
  229. }
  230. }
  231. put_group_info(new->group_info);
  232. return 0;
  233. }
  234. static struct security_hook_list safesetid_security_hooks[] = {
  235. LSM_HOOK_INIT(task_fix_setuid, safesetid_task_fix_setuid),
  236. LSM_HOOK_INIT(task_fix_setgid, safesetid_task_fix_setgid),
  237. LSM_HOOK_INIT(task_fix_setgroups, safesetid_task_fix_setgroups),
  238. LSM_HOOK_INIT(capable, safesetid_security_capable)
  239. };
  240. static int __init safesetid_security_init(void)
  241. {
  242. security_add_hooks(safesetid_security_hooks,
  243. ARRAY_SIZE(safesetid_security_hooks), "safesetid");
  244. /* Report that SafeSetID successfully initialized */
  245. safesetid_initialized = 1;
  246. return 0;
  247. }
  248. DEFINE_LSM(safesetid_security_init) = {
  249. .init = safesetid_security_init,
  250. .name = "safesetid",
  251. };