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 */ |
| 25 | int safesetid_initialized; |
| 26 | |
Jann Horn | 03638e6 | 2019-04-10 09:56:05 -0700 | [diff] [blame] | 27 | struct setuid_ruleset __rcu *safesetid_setuid_rules; |
Micah Morton | aeca4e2 | 2019-01-16 07:46:06 -0800 | [diff] [blame] | 28 | |
Jann Horn | 03638e6 | 2019-04-10 09:56:05 -0700 | [diff] [blame] | 29 | /* Compute a decision for a transition from @src to @dst under @policy. */ |
| 30 | enum sid_policy_type _setuid_policy_lookup(struct setuid_ruleset *policy, |
| 31 | kuid_t src, kuid_t dst) |
Micah Morton | aeca4e2 | 2019-01-16 07:46:06 -0800 | [diff] [blame] | 32 | { |
Jann Horn | 03638e6 | 2019-04-10 09:56:05 -0700 | [diff] [blame] | 33 | struct setuid_rule *rule; |
Jann Horn | 1cd02a2 | 2019-04-10 09:55:34 -0700 | [diff] [blame] | 34 | enum sid_policy_type result = SIDPOL_DEFAULT; |
Micah Morton | aeca4e2 | 2019-01-16 07:46:06 -0800 | [diff] [blame] | 35 | |
Jann Horn | 03638e6 | 2019-04-10 09:56:05 -0700 | [diff] [blame] | 36 | hash_for_each_possible(policy->rules, rule, next, __kuid_val(src)) { |
| 37 | if (!uid_eq(rule->src_uid, src)) |
Jann Horn | 1cd02a2 | 2019-04-10 09:55:34 -0700 | [diff] [blame] | 38 | continue; |
Jann Horn | 03638e6 | 2019-04-10 09:56:05 -0700 | [diff] [blame] | 39 | if (uid_eq(rule->dst_uid, dst)) |
Jann Horn | 1cd02a2 | 2019-04-10 09:55:34 -0700 | [diff] [blame] | 40 | return SIDPOL_ALLOWED; |
Jann Horn | 1cd02a2 | 2019-04-10 09:55:34 -0700 | [diff] [blame] | 41 | result = SIDPOL_CONSTRAINED; |
Micah Morton | aeca4e2 | 2019-01-16 07:46:06 -0800 | [diff] [blame] | 42 | } |
Jann Horn | 03638e6 | 2019-04-10 09:56:05 -0700 | [diff] [blame] | 43 | return result; |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * Compute a decision for a transition from @src to @dst under the active |
| 48 | * policy. |
| 49 | */ |
| 50 | static enum sid_policy_type setuid_policy_lookup(kuid_t src, kuid_t dst) |
| 51 | { |
| 52 | enum sid_policy_type result = SIDPOL_DEFAULT; |
| 53 | struct setuid_ruleset *pol; |
| 54 | |
| 55 | rcu_read_lock(); |
| 56 | pol = rcu_dereference(safesetid_setuid_rules); |
| 57 | if (pol) |
| 58 | result = _setuid_policy_lookup(pol, src, dst); |
Micah Morton | aeca4e2 | 2019-01-16 07:46:06 -0800 | [diff] [blame] | 59 | rcu_read_unlock(); |
Jann Horn | 1cd02a2 | 2019-04-10 09:55:34 -0700 | [diff] [blame] | 60 | return result; |
Micah Morton | aeca4e2 | 2019-01-16 07:46:06 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static int safesetid_security_capable(const struct cred *cred, |
| 64 | struct user_namespace *ns, |
| 65 | int cap, |
| 66 | unsigned int opts) |
| 67 | { |
Jann Horn | 8068866 | 2019-04-10 09:55:41 -0700 | [diff] [blame] | 68 | /* We're only interested in CAP_SETUID. */ |
| 69 | if (cap != CAP_SETUID) |
| 70 | return 0; |
| 71 | |
| 72 | /* |
| 73 | * If CAP_SETUID is currently used for a set*uid() syscall, we want to |
| 74 | * let it go through here; the real security check happens later, in the |
| 75 | * task_fix_setuid hook. |
| 76 | */ |
| 77 | if ((opts & CAP_OPT_INSETID) != 0) |
| 78 | return 0; |
| 79 | |
| 80 | /* |
| 81 | * If no policy applies to this task, allow the use of CAP_SETUID for |
| 82 | * other purposes. |
| 83 | */ |
| 84 | if (setuid_policy_lookup(cred->uid, INVALID_UID) == SIDPOL_DEFAULT) |
| 85 | return 0; |
| 86 | |
| 87 | /* |
| 88 | * Reject use of CAP_SETUID for functionality other than calling |
| 89 | * set*uid() (e.g. setting up userns uid mappings). |
| 90 | */ |
| 91 | pr_warn("Operation requires CAP_SETUID, which is not available to UID %u for operations besides approved set*uid transitions\n", |
| 92 | __kuid_val(cred->uid)); |
Jann Horn | e10337d | 2019-04-10 09:56:27 -0700 | [diff] [blame] | 93 | return -EPERM; |
Micah Morton | aeca4e2 | 2019-01-16 07:46:06 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Jann Horn | 7ef6b30 | 2019-04-10 09:55:19 -0700 | [diff] [blame] | 96 | /* |
| 97 | * Check whether a caller with old credentials @old is allowed to switch to |
| 98 | * credentials that contain @new_uid. |
| 99 | */ |
| 100 | static bool uid_permitted_for_cred(const struct cred *old, kuid_t new_uid) |
Micah Morton | aeca4e2 | 2019-01-16 07:46:06 -0800 | [diff] [blame] | 101 | { |
Jann Horn | 7ef6b30 | 2019-04-10 09:55:19 -0700 | [diff] [blame] | 102 | bool permitted; |
| 103 | |
| 104 | /* If our old creds already had this UID in it, it's fine. */ |
| 105 | if (uid_eq(new_uid, old->uid) || uid_eq(new_uid, old->euid) || |
| 106 | uid_eq(new_uid, old->suid)) |
| 107 | return true; |
| 108 | |
Micah Morton | aeca4e2 | 2019-01-16 07:46:06 -0800 | [diff] [blame] | 109 | /* |
Jann Horn | 7ef6b30 | 2019-04-10 09:55:19 -0700 | [diff] [blame] | 110 | * Transitions to new UIDs require a check against the policy of the old |
| 111 | * RUID. |
Micah Morton | aeca4e2 | 2019-01-16 07:46:06 -0800 | [diff] [blame] | 112 | */ |
Jann Horn | 1cd02a2 | 2019-04-10 09:55:34 -0700 | [diff] [blame] | 113 | permitted = |
| 114 | setuid_policy_lookup(old->uid, new_uid) != SIDPOL_CONSTRAINED; |
Jann Horn | 7ef6b30 | 2019-04-10 09:55:19 -0700 | [diff] [blame] | 115 | if (!permitted) { |
| 116 | pr_warn("UID transition ((%d,%d,%d) -> %d) blocked\n", |
| 117 | __kuid_val(old->uid), __kuid_val(old->euid), |
| 118 | __kuid_val(old->suid), __kuid_val(new_uid)); |
| 119 | } |
| 120 | return permitted; |
Micah Morton | aeca4e2 | 2019-01-16 07:46:06 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Check whether there is either an exception for user under old cred struct to |
| 125 | * set*uid to user under new cred struct, or the UID transition is allowed (by |
| 126 | * Linux set*uid rules) even without CAP_SETUID. |
| 127 | */ |
| 128 | static int safesetid_task_fix_setuid(struct cred *new, |
| 129 | const struct cred *old, |
| 130 | int flags) |
| 131 | { |
| 132 | |
Jann Horn | 7ef6b30 | 2019-04-10 09:55:19 -0700 | [diff] [blame] | 133 | /* Do nothing if there are no setuid restrictions for our old RUID. */ |
Jann Horn | 1cd02a2 | 2019-04-10 09:55:34 -0700 | [diff] [blame] | 134 | if (setuid_policy_lookup(old->uid, INVALID_UID) == SIDPOL_DEFAULT) |
Micah Morton | aeca4e2 | 2019-01-16 07:46:06 -0800 | [diff] [blame] | 135 | return 0; |
| 136 | |
Jann Horn | 7ef6b30 | 2019-04-10 09:55:19 -0700 | [diff] [blame] | 137 | if (uid_permitted_for_cred(old, new->uid) && |
| 138 | uid_permitted_for_cred(old, new->euid) && |
| 139 | uid_permitted_for_cred(old, new->suid) && |
| 140 | uid_permitted_for_cred(old, new->fsuid)) |
| 141 | return 0; |
| 142 | |
| 143 | /* |
| 144 | * Kill this process to avoid potential security vulnerabilities |
| 145 | * that could arise from a missing whitelist entry preventing a |
| 146 | * privileged process from dropping to a lesser-privileged one. |
| 147 | */ |
| 148 | force_sig(SIGKILL); |
| 149 | return -EACCES; |
Micah Morton | aeca4e2 | 2019-01-16 07:46:06 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Micah Morton | aeca4e2 | 2019-01-16 07:46:06 -0800 | [diff] [blame] | 152 | static struct security_hook_list safesetid_security_hooks[] = { |
| 153 | LSM_HOOK_INIT(task_fix_setuid, safesetid_task_fix_setuid), |
| 154 | LSM_HOOK_INIT(capable, safesetid_security_capable) |
| 155 | }; |
| 156 | |
| 157 | static int __init safesetid_security_init(void) |
| 158 | { |
| 159 | security_add_hooks(safesetid_security_hooks, |
| 160 | ARRAY_SIZE(safesetid_security_hooks), "safesetid"); |
| 161 | |
| 162 | /* Report that SafeSetID successfully initialized */ |
| 163 | safesetid_initialized = 1; |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | DEFINE_LSM(safesetid_security_init) = { |
| 169 | .init = safesetid_security_init, |
Micah Morton | f67e20d | 2019-01-28 12:30:56 -0800 | [diff] [blame] | 170 | .name = "safesetid", |
Micah Morton | aeca4e2 | 2019-01-16 07:46:06 -0800 | [diff] [blame] | 171 | }; |