This source file includes following definitions.
- _setuid_policy_lookup
- setuid_policy_lookup
- safesetid_security_capable
- uid_permitted_for_cred
- safesetid_task_fix_setuid
- safesetid_security_init
1
2
3
4
5
6
7
8
9
10
11
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
25 int safesetid_initialized;
26
27 struct setuid_ruleset __rcu *safesetid_setuid_rules;
28
29
30 enum sid_policy_type _setuid_policy_lookup(struct setuid_ruleset *policy,
31 kuid_t src, kuid_t dst)
32 {
33 struct setuid_rule *rule;
34 enum sid_policy_type result = SIDPOL_DEFAULT;
35
36 hash_for_each_possible(policy->rules, rule, next, __kuid_val(src)) {
37 if (!uid_eq(rule->src_uid, src))
38 continue;
39 if (uid_eq(rule->dst_uid, dst))
40 return SIDPOL_ALLOWED;
41 result = SIDPOL_CONSTRAINED;
42 }
43 return result;
44 }
45
46
47
48
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);
59 rcu_read_unlock();
60 return result;
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 {
68
69 if (cap != CAP_SETUID)
70 return 0;
71
72
73
74
75
76
77 if ((opts & CAP_OPT_INSETID) != 0)
78 return 0;
79
80
81
82
83
84 if (setuid_policy_lookup(cred->uid, INVALID_UID) == SIDPOL_DEFAULT)
85 return 0;
86
87
88
89
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));
93 return -EPERM;
94 }
95
96
97
98
99
100 static bool uid_permitted_for_cred(const struct cred *old, kuid_t new_uid)
101 {
102 bool permitted;
103
104
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
109
110
111
112
113 permitted =
114 setuid_policy_lookup(old->uid, new_uid) != SIDPOL_CONSTRAINED;
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;
121 }
122
123
124
125
126
127
128 static int safesetid_task_fix_setuid(struct cred *new,
129 const struct cred *old,
130 int flags)
131 {
132
133
134 if (setuid_policy_lookup(old->uid, INVALID_UID) == SIDPOL_DEFAULT)
135 return 0;
136
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
145
146
147
148 force_sig(SIGKILL);
149 return -EACCES;
150 }
151
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
163 safesetid_initialized = 1;
164
165 return 0;
166 }
167
168 DEFINE_LSM(safesetid_security_init) = {
169 .init = safesetid_security_init,
170 .name = "safesetid",
171 };