]> asedeno.scripts.mit.edu Git - linux.git/blob - security/safesetid/lsm.c
LSM: SafeSetID: refactor policy hash table
[linux.git] / security / safesetid / lsm.c
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
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>
22 #include "lsm.h"
23
24 /* Flag indicating whether initialization completed */
25 int safesetid_initialized;
26
27 #define NUM_BITS 8 /* 256 buckets in hash table */
28
29 static DEFINE_HASHTABLE(safesetid_whitelist_hashtable, NUM_BITS);
30
31 static DEFINE_SPINLOCK(safesetid_whitelist_hashtable_spinlock);
32
33 static enum sid_policy_type setuid_policy_lookup(kuid_t src, kuid_t dst)
34 {
35         struct entry *entry;
36         enum sid_policy_type result = SIDPOL_DEFAULT;
37
38         rcu_read_lock();
39         hash_for_each_possible_rcu(safesetid_whitelist_hashtable,
40                                    entry, next, __kuid_val(src)) {
41                 if (!uid_eq(entry->src_uid, src))
42                         continue;
43                 if (uid_eq(entry->dst_uid, dst)) {
44                         rcu_read_unlock();
45                         return SIDPOL_ALLOWED;
46                 }
47                 result = SIDPOL_CONSTRAINED;
48         }
49         rcu_read_unlock();
50         return result;
51 }
52
53 static int safesetid_security_capable(const struct cred *cred,
54                                       struct user_namespace *ns,
55                                       int cap,
56                                       unsigned int opts)
57 {
58         if (cap == CAP_SETUID &&
59             setuid_policy_lookup(cred->uid, INVALID_UID) != SIDPOL_DEFAULT) {
60                 if (!(opts & CAP_OPT_INSETID)) {
61                         /*
62                          * Deny if we're not in a set*uid() syscall to avoid
63                          * giving powers gated by CAP_SETUID that are related
64                          * to functionality other than calling set*uid() (e.g.
65                          * allowing user to set up userns uid mappings).
66                          */
67                         pr_warn("Operation requires CAP_SETUID, which is not available to UID %u for operations besides approved set*uid transitions\n",
68                                 __kuid_val(cred->uid));
69                         return -1;
70                 }
71         }
72         return 0;
73 }
74
75 /*
76  * Check whether a caller with old credentials @old is allowed to switch to
77  * credentials that contain @new_uid.
78  */
79 static bool uid_permitted_for_cred(const struct cred *old, kuid_t new_uid)
80 {
81         bool permitted;
82
83         /* If our old creds already had this UID in it, it's fine. */
84         if (uid_eq(new_uid, old->uid) || uid_eq(new_uid, old->euid) ||
85             uid_eq(new_uid, old->suid))
86                 return true;
87
88         /*
89          * Transitions to new UIDs require a check against the policy of the old
90          * RUID.
91          */
92         permitted =
93             setuid_policy_lookup(old->uid, new_uid) != SIDPOL_CONSTRAINED;
94         if (!permitted) {
95                 pr_warn("UID transition ((%d,%d,%d) -> %d) blocked\n",
96                         __kuid_val(old->uid), __kuid_val(old->euid),
97                         __kuid_val(old->suid), __kuid_val(new_uid));
98         }
99         return permitted;
100 }
101
102 /*
103  * Check whether there is either an exception for user under old cred struct to
104  * set*uid to user under new cred struct, or the UID transition is allowed (by
105  * Linux set*uid rules) even without CAP_SETUID.
106  */
107 static int safesetid_task_fix_setuid(struct cred *new,
108                                      const struct cred *old,
109                                      int flags)
110 {
111
112         /* Do nothing if there are no setuid restrictions for our old RUID. */
113         if (setuid_policy_lookup(old->uid, INVALID_UID) == SIDPOL_DEFAULT)
114                 return 0;
115
116         if (uid_permitted_for_cred(old, new->uid) &&
117             uid_permitted_for_cred(old, new->euid) &&
118             uid_permitted_for_cred(old, new->suid) &&
119             uid_permitted_for_cred(old, new->fsuid))
120                 return 0;
121
122         /*
123          * Kill this process to avoid potential security vulnerabilities
124          * that could arise from a missing whitelist entry preventing a
125          * privileged process from dropping to a lesser-privileged one.
126          */
127         force_sig(SIGKILL);
128         return -EACCES;
129 }
130
131 int add_safesetid_whitelist_entry(kuid_t parent, kuid_t child)
132 {
133         struct entry *new;
134
135         /* Return if entry already exists */
136         if (setuid_policy_lookup(parent, child) == SIDPOL_ALLOWED)
137                 return 0;
138
139         new = kzalloc(sizeof(struct entry), GFP_KERNEL);
140         if (!new)
141                 return -ENOMEM;
142         new->src_uid = parent;
143         new->dst_uid = child;
144         spin_lock(&safesetid_whitelist_hashtable_spinlock);
145         hash_add_rcu(safesetid_whitelist_hashtable,
146                      &new->next,
147                      __kuid_val(parent));
148         spin_unlock(&safesetid_whitelist_hashtable_spinlock);
149         return 0;
150 }
151
152 void flush_safesetid_whitelist_entries(void)
153 {
154         struct entry *entry;
155         struct hlist_node *hlist_node;
156         unsigned int bkt_loop_cursor;
157         HLIST_HEAD(free_list);
158
159         /*
160          * Could probably use hash_for_each_rcu here instead, but this should
161          * be fine as well.
162          */
163         spin_lock(&safesetid_whitelist_hashtable_spinlock);
164         hash_for_each_safe(safesetid_whitelist_hashtable, bkt_loop_cursor,
165                            hlist_node, entry, next) {
166                 hash_del_rcu(&entry->next);
167                 hlist_add_head(&entry->dlist, &free_list);
168         }
169         spin_unlock(&safesetid_whitelist_hashtable_spinlock);
170         synchronize_rcu();
171         hlist_for_each_entry_safe(entry, hlist_node, &free_list, dlist) {
172                 hlist_del(&entry->dlist);
173                 kfree(entry);
174         }
175 }
176
177 static struct security_hook_list safesetid_security_hooks[] = {
178         LSM_HOOK_INIT(task_fix_setuid, safesetid_task_fix_setuid),
179         LSM_HOOK_INIT(capable, safesetid_security_capable)
180 };
181
182 static int __init safesetid_security_init(void)
183 {
184         security_add_hooks(safesetid_security_hooks,
185                            ARRAY_SIZE(safesetid_security_hooks), "safesetid");
186
187         /* Report that SafeSetID successfully initialized */
188         safesetid_initialized = 1;
189
190         return 0;
191 }
192
193 DEFINE_LSM(safesetid_security_init) = {
194         .init = safesetid_security_init,
195         .name = "safesetid",
196 };