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