Commit | Line | Data |
---|---|---|
e338d263 | 1 | /* Common capabilities, needed by capability.o and root_plug.o |
1da177e4 LT |
2 | * |
3 | * This program is free software; you can redistribute it and/or modify | |
4 | * it under the terms of the GNU General Public License as published by | |
5 | * the Free Software Foundation; either version 2 of the License, or | |
6 | * (at your option) any later version. | |
7 | * | |
8 | */ | |
9 | ||
c59ede7b | 10 | #include <linux/capability.h> |
3fc689e9 | 11 | #include <linux/audit.h> |
1da177e4 LT |
12 | #include <linux/module.h> |
13 | #include <linux/init.h> | |
14 | #include <linux/kernel.h> | |
15 | #include <linux/security.h> | |
16 | #include <linux/file.h> | |
17 | #include <linux/mm.h> | |
18 | #include <linux/mman.h> | |
19 | #include <linux/pagemap.h> | |
20 | #include <linux/swap.h> | |
1da177e4 LT |
21 | #include <linux/skbuff.h> |
22 | #include <linux/netlink.h> | |
23 | #include <linux/ptrace.h> | |
24 | #include <linux/xattr.h> | |
25 | #include <linux/hugetlb.h> | |
b5376771 | 26 | #include <linux/mount.h> |
b460cbc5 | 27 | #include <linux/sched.h> |
3898b1b4 AM |
28 | #include <linux/prctl.h> |
29 | #include <linux/securebits.h> | |
72c2d582 | 30 | |
1da177e4 LT |
31 | int cap_netlink_send(struct sock *sk, struct sk_buff *skb) |
32 | { | |
33 | NETLINK_CB(skb).eff_cap = current->cap_effective; | |
34 | return 0; | |
35 | } | |
36 | ||
c7bdb545 | 37 | int cap_netlink_recv(struct sk_buff *skb, int cap) |
1da177e4 | 38 | { |
c7bdb545 | 39 | if (!cap_raised(NETLINK_CB(skb).eff_cap, cap)) |
1da177e4 LT |
40 | return -EPERM; |
41 | return 0; | |
42 | } | |
43 | ||
44 | EXPORT_SYMBOL(cap_netlink_recv); | |
45 | ||
a6dbb1ef AM |
46 | /* |
47 | * NOTE WELL: cap_capable() cannot be used like the kernel's capable() | |
48 | * function. That is, it has the reverse semantics: cap_capable() | |
49 | * returns 0 when a task has a capability, but the kernel's capable() | |
50 | * returns 1 for this case. | |
51 | */ | |
06112163 | 52 | int cap_capable(struct task_struct *tsk, int cap, int audit) |
1da177e4 LT |
53 | { |
54 | /* Derived from include/linux/sched.h:capable. */ | |
55 | if (cap_raised(tsk->cap_effective, cap)) | |
56 | return 0; | |
57 | return -EPERM; | |
58 | } | |
59 | ||
60 | int cap_settime(struct timespec *ts, struct timezone *tz) | |
61 | { | |
62 | if (!capable(CAP_SYS_TIME)) | |
63 | return -EPERM; | |
64 | return 0; | |
65 | } | |
66 | ||
5cd9c58f | 67 | int cap_ptrace_may_access(struct task_struct *child, unsigned int mode) |
1da177e4 LT |
68 | { |
69 | /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */ | |
5cd9c58f DH |
70 | if (cap_issubset(child->cap_permitted, current->cap_permitted)) |
71 | return 0; | |
72 | if (capable(CAP_SYS_PTRACE)) | |
73 | return 0; | |
74 | return -EPERM; | |
75 | } | |
76 | ||
77 | int cap_ptrace_traceme(struct task_struct *parent) | |
78 | { | |
79 | /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */ | |
80 | if (cap_issubset(current->cap_permitted, parent->cap_permitted)) | |
81 | return 0; | |
82 | if (has_capability(parent, CAP_SYS_PTRACE)) | |
83 | return 0; | |
84 | return -EPERM; | |
1da177e4 LT |
85 | } |
86 | ||
87 | int cap_capget (struct task_struct *target, kernel_cap_t *effective, | |
88 | kernel_cap_t *inheritable, kernel_cap_t *permitted) | |
89 | { | |
90 | /* Derived from kernel/capability.c:sys_capget. */ | |
e338d263 AM |
91 | *effective = target->cap_effective; |
92 | *inheritable = target->cap_inheritable; | |
93 | *permitted = target->cap_permitted; | |
1da177e4 LT |
94 | return 0; |
95 | } | |
96 | ||
72c2d582 AM |
97 | #ifdef CONFIG_SECURITY_FILE_CAPABILITIES |
98 | ||
72c2d582 AM |
99 | static inline int cap_inh_is_capped(void) |
100 | { | |
101 | /* | |
a6dbb1ef AM |
102 | * Return 1 if changes to the inheritable set are limited |
103 | * to the old permitted set. That is, if the current task | |
104 | * does *not* possess the CAP_SETPCAP capability. | |
72c2d582 | 105 | */ |
06112163 | 106 | return (cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0); |
72c2d582 AM |
107 | } |
108 | ||
1209726c AM |
109 | static inline int cap_limit_ptraced_target(void) { return 1; } |
110 | ||
72c2d582 AM |
111 | #else /* ie., ndef CONFIG_SECURITY_FILE_CAPABILITIES */ |
112 | ||
72c2d582 | 113 | static inline int cap_inh_is_capped(void) { return 1; } |
1209726c AM |
114 | static inline int cap_limit_ptraced_target(void) |
115 | { | |
116 | return !capable(CAP_SETPCAP); | |
117 | } | |
72c2d582 AM |
118 | |
119 | #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */ | |
120 | ||
1cdcbec1 | 121 | int cap_capset_check (kernel_cap_t *effective, |
1da177e4 LT |
122 | kernel_cap_t *inheritable, kernel_cap_t *permitted) |
123 | { | |
72c2d582 AM |
124 | if (cap_inh_is_capped() |
125 | && !cap_issubset(*inheritable, | |
1cdcbec1 | 126 | cap_combine(current->cap_inheritable, |
72c2d582 AM |
127 | current->cap_permitted))) { |
128 | /* incapable of using this inheritable set */ | |
1da177e4 LT |
129 | return -EPERM; |
130 | } | |
3b7391de | 131 | if (!cap_issubset(*inheritable, |
1cdcbec1 | 132 | cap_combine(current->cap_inheritable, |
3b7391de SH |
133 | current->cap_bset))) { |
134 | /* no new pI capabilities outside bounding set */ | |
135 | return -EPERM; | |
136 | } | |
1da177e4 LT |
137 | |
138 | /* verify restrictions on target's new Permitted set */ | |
139 | if (!cap_issubset (*permitted, | |
1cdcbec1 | 140 | cap_combine (current->cap_permitted, |
1da177e4 LT |
141 | current->cap_permitted))) { |
142 | return -EPERM; | |
143 | } | |
144 | ||
145 | /* verify the _new_Effective_ is a subset of the _new_Permitted_ */ | |
146 | if (!cap_issubset (*effective, *permitted)) { | |
147 | return -EPERM; | |
148 | } | |
149 | ||
150 | return 0; | |
151 | } | |
152 | ||
1cdcbec1 | 153 | void cap_capset_set (kernel_cap_t *effective, |
1da177e4 LT |
154 | kernel_cap_t *inheritable, kernel_cap_t *permitted) |
155 | { | |
1cdcbec1 DH |
156 | current->cap_effective = *effective; |
157 | current->cap_inheritable = *inheritable; | |
158 | current->cap_permitted = *permitted; | |
1da177e4 LT |
159 | } |
160 | ||
b5376771 SH |
161 | static inline void bprm_clear_caps(struct linux_binprm *bprm) |
162 | { | |
5459c164 | 163 | cap_clear(bprm->cap_post_exec_permitted); |
b5376771 SH |
164 | bprm->cap_effective = false; |
165 | } | |
166 | ||
167 | #ifdef CONFIG_SECURITY_FILE_CAPABILITIES | |
168 | ||
169 | int cap_inode_need_killpriv(struct dentry *dentry) | |
170 | { | |
171 | struct inode *inode = dentry->d_inode; | |
172 | int error; | |
173 | ||
174 | if (!inode->i_op || !inode->i_op->getxattr) | |
175 | return 0; | |
176 | ||
177 | error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0); | |
178 | if (error <= 0) | |
179 | return 0; | |
180 | return 1; | |
181 | } | |
182 | ||
183 | int cap_inode_killpriv(struct dentry *dentry) | |
184 | { | |
185 | struct inode *inode = dentry->d_inode; | |
186 | ||
187 | if (!inode->i_op || !inode->i_op->removexattr) | |
188 | return 0; | |
189 | ||
190 | return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS); | |
191 | } | |
192 | ||
c0b00441 EP |
193 | static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps, |
194 | struct linux_binprm *bprm) | |
b5376771 | 195 | { |
c0b00441 EP |
196 | unsigned i; |
197 | int ret = 0; | |
198 | ||
199 | if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE) | |
200 | bprm->cap_effective = true; | |
201 | else | |
202 | bprm->cap_effective = false; | |
203 | ||
204 | CAP_FOR_EACH_U32(i) { | |
205 | __u32 permitted = caps->permitted.cap[i]; | |
206 | __u32 inheritable = caps->inheritable.cap[i]; | |
207 | ||
208 | /* | |
209 | * pP' = (X & fP) | (pI & fI) | |
210 | */ | |
211 | bprm->cap_post_exec_permitted.cap[i] = | |
212 | (current->cap_bset.cap[i] & permitted) | | |
213 | (current->cap_inheritable.cap[i] & inheritable); | |
214 | ||
215 | if (permitted & ~bprm->cap_post_exec_permitted.cap[i]) { | |
216 | /* | |
217 | * insufficient to execute correctly | |
218 | */ | |
219 | ret = -EPERM; | |
220 | } | |
221 | } | |
222 | ||
223 | /* | |
224 | * For legacy apps, with no internal support for recognizing they | |
225 | * do not have enough capabilities, we return an error if they are | |
226 | * missing some "forced" (aka file-permitted) capabilities. | |
227 | */ | |
228 | return bprm->cap_effective ? ret : 0; | |
229 | } | |
230 | ||
231 | int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps) | |
232 | { | |
233 | struct inode *inode = dentry->d_inode; | |
b5376771 | 234 | __u32 magic_etc; |
e338d263 | 235 | unsigned tocopy, i; |
c0b00441 EP |
236 | int size; |
237 | struct vfs_cap_data caps; | |
238 | ||
239 | memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data)); | |
240 | ||
241 | if (!inode || !inode->i_op || !inode->i_op->getxattr) | |
242 | return -ENODATA; | |
243 | ||
244 | size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps, | |
245 | XATTR_CAPS_SZ); | |
246 | if (size == -ENODATA || size == -EOPNOTSUPP) { | |
247 | /* no data, that's ok */ | |
248 | return -ENODATA; | |
249 | } | |
250 | if (size < 0) | |
251 | return size; | |
b5376771 | 252 | |
e338d263 | 253 | if (size < sizeof(magic_etc)) |
b5376771 SH |
254 | return -EINVAL; |
255 | ||
c0b00441 | 256 | cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc); |
b5376771 SH |
257 | |
258 | switch ((magic_etc & VFS_CAP_REVISION_MASK)) { | |
e338d263 AM |
259 | case VFS_CAP_REVISION_1: |
260 | if (size != XATTR_CAPS_SZ_1) | |
261 | return -EINVAL; | |
262 | tocopy = VFS_CAP_U32_1; | |
263 | break; | |
264 | case VFS_CAP_REVISION_2: | |
265 | if (size != XATTR_CAPS_SZ_2) | |
266 | return -EINVAL; | |
267 | tocopy = VFS_CAP_U32_2; | |
268 | break; | |
b5376771 SH |
269 | default: |
270 | return -EINVAL; | |
271 | } | |
e338d263 | 272 | |
5459c164 | 273 | CAP_FOR_EACH_U32(i) { |
c0b00441 EP |
274 | if (i >= tocopy) |
275 | break; | |
276 | cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted); | |
277 | cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable); | |
e338d263 | 278 | } |
c0b00441 | 279 | return 0; |
b5376771 SH |
280 | } |
281 | ||
282 | /* Locate any VFS capabilities: */ | |
283 | static int get_file_caps(struct linux_binprm *bprm) | |
284 | { | |
285 | struct dentry *dentry; | |
286 | int rc = 0; | |
c0b00441 | 287 | struct cpu_vfs_cap_data vcaps; |
b5376771 | 288 | |
3318a386 SH |
289 | bprm_clear_caps(bprm); |
290 | ||
1f29fae2 SH |
291 | if (!file_caps_enabled) |
292 | return 0; | |
293 | ||
3318a386 | 294 | if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID) |
b5376771 | 295 | return 0; |
b5376771 SH |
296 | |
297 | dentry = dget(bprm->file->f_dentry); | |
b5376771 | 298 | |
c0b00441 EP |
299 | rc = get_vfs_caps_from_disk(dentry, &vcaps); |
300 | if (rc < 0) { | |
301 | if (rc == -EINVAL) | |
302 | printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n", | |
303 | __func__, rc, bprm->filename); | |
304 | else if (rc == -ENODATA) | |
305 | rc = 0; | |
b5376771 SH |
306 | goto out; |
307 | } | |
b5376771 | 308 | |
c0b00441 | 309 | rc = bprm_caps_from_vfs_caps(&vcaps, bprm); |
b5376771 SH |
310 | |
311 | out: | |
312 | dput(dentry); | |
313 | if (rc) | |
314 | bprm_clear_caps(bprm); | |
315 | ||
316 | return rc; | |
317 | } | |
318 | ||
319 | #else | |
320 | int cap_inode_need_killpriv(struct dentry *dentry) | |
321 | { | |
322 | return 0; | |
323 | } | |
324 | ||
325 | int cap_inode_killpriv(struct dentry *dentry) | |
326 | { | |
327 | return 0; | |
328 | } | |
329 | ||
330 | static inline int get_file_caps(struct linux_binprm *bprm) | |
331 | { | |
332 | bprm_clear_caps(bprm); | |
333 | return 0; | |
334 | } | |
335 | #endif | |
336 | ||
1da177e4 LT |
337 | int cap_bprm_set_security (struct linux_binprm *bprm) |
338 | { | |
b5376771 | 339 | int ret; |
1da177e4 | 340 | |
b5376771 | 341 | ret = get_file_caps(bprm); |
1da177e4 | 342 | |
5459c164 AM |
343 | if (!issecure(SECURE_NOROOT)) { |
344 | /* | |
345 | * To support inheritance of root-permissions and suid-root | |
346 | * executables under compatibility mode, we override the | |
347 | * capability sets for the file. | |
348 | * | |
349 | * If only the real uid is 0, we do not set the effective | |
350 | * bit. | |
351 | */ | |
b103c598 | 352 | if (bprm->e_uid == 0 || current_uid() == 0) { |
5459c164 AM |
353 | /* pP' = (cap_bset & ~0) | (pI & ~0) */ |
354 | bprm->cap_post_exec_permitted = cap_combine( | |
355 | current->cap_bset, current->cap_inheritable | |
356 | ); | |
357 | bprm->cap_effective = (bprm->e_uid == 0); | |
358 | ret = 0; | |
1da177e4 | 359 | } |
1da177e4 | 360 | } |
b5376771 SH |
361 | |
362 | return ret; | |
1da177e4 LT |
363 | } |
364 | ||
365 | void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe) | |
366 | { | |
3fc689e9 EP |
367 | kernel_cap_t pP = current->cap_permitted; |
368 | kernel_cap_t pE = current->cap_effective; | |
b103c598 DH |
369 | uid_t uid; |
370 | gid_t gid; | |
3fc689e9 | 371 | |
b103c598 DH |
372 | current_uid_gid(&uid, &gid); |
373 | ||
374 | if (bprm->e_uid != uid || bprm->e_gid != gid || | |
5459c164 AM |
375 | !cap_issubset(bprm->cap_post_exec_permitted, |
376 | current->cap_permitted)) { | |
6c5d5238 | 377 | set_dumpable(current->mm, suid_dumpable); |
b5376771 | 378 | current->pdeath_signal = 0; |
1da177e4 LT |
379 | |
380 | if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) { | |
381 | if (!capable(CAP_SETUID)) { | |
b103c598 DH |
382 | bprm->e_uid = uid; |
383 | bprm->e_gid = gid; | |
1da177e4 | 384 | } |
1209726c | 385 | if (cap_limit_ptraced_target()) { |
5459c164 AM |
386 | bprm->cap_post_exec_permitted = cap_intersect( |
387 | bprm->cap_post_exec_permitted, | |
388 | current->cap_permitted); | |
1da177e4 LT |
389 | } |
390 | } | |
391 | } | |
392 | ||
393 | current->suid = current->euid = current->fsuid = bprm->e_uid; | |
394 | current->sgid = current->egid = current->fsgid = bprm->e_gid; | |
395 | ||
396 | /* For init, we want to retain the capabilities set | |
397 | * in the init_task struct. Thus we skip the usual | |
398 | * capability rules */ | |
b460cbc5 | 399 | if (!is_global_init(current)) { |
5459c164 | 400 | current->cap_permitted = bprm->cap_post_exec_permitted; |
e338d263 | 401 | if (bprm->cap_effective) |
5459c164 | 402 | current->cap_effective = bprm->cap_post_exec_permitted; |
e338d263 AM |
403 | else |
404 | cap_clear(current->cap_effective); | |
1da177e4 LT |
405 | } |
406 | ||
3fc689e9 EP |
407 | /* |
408 | * Audit candidate if current->cap_effective is set | |
409 | * | |
410 | * We do not bother to audit if 3 things are true: | |
411 | * 1) cap_effective has all caps | |
412 | * 2) we are root | |
413 | * 3) root is supposed to have all caps (SECURE_NOROOT) | |
414 | * Since this is just a normal root execing a process. | |
415 | * | |
416 | * Number 1 above might fail if you don't have a full bset, but I think | |
417 | * that is interesting information to audit. | |
418 | */ | |
419 | if (!cap_isclear(current->cap_effective)) { | |
420 | if (!cap_issubset(CAP_FULL_SET, current->cap_effective) || | |
421 | (bprm->e_uid != 0) || (current->uid != 0) || | |
422 | issecure(SECURE_NOROOT)) | |
423 | audit_log_bprm_fcaps(bprm, &pP, &pE); | |
424 | } | |
1da177e4 | 425 | |
3898b1b4 | 426 | current->securebits &= ~issecure_mask(SECURE_KEEP_CAPS); |
1da177e4 LT |
427 | } |
428 | ||
429 | int cap_bprm_secureexec (struct linux_binprm *bprm) | |
430 | { | |
b103c598 | 431 | if (current_uid() != 0) { |
b5376771 SH |
432 | if (bprm->cap_effective) |
433 | return 1; | |
5459c164 | 434 | if (!cap_isclear(bprm->cap_post_exec_permitted)) |
b5376771 SH |
435 | return 1; |
436 | } | |
437 | ||
b103c598 DH |
438 | return (current_euid() != current_uid() || |
439 | current_egid() != current_gid()); | |
1da177e4 LT |
440 | } |
441 | ||
8f0cfa52 DH |
442 | int cap_inode_setxattr(struct dentry *dentry, const char *name, |
443 | const void *value, size_t size, int flags) | |
1da177e4 | 444 | { |
b5376771 SH |
445 | if (!strcmp(name, XATTR_NAME_CAPS)) { |
446 | if (!capable(CAP_SETFCAP)) | |
447 | return -EPERM; | |
448 | return 0; | |
449 | } else if (!strncmp(name, XATTR_SECURITY_PREFIX, | |
1da177e4 LT |
450 | sizeof(XATTR_SECURITY_PREFIX) - 1) && |
451 | !capable(CAP_SYS_ADMIN)) | |
452 | return -EPERM; | |
453 | return 0; | |
454 | } | |
455 | ||
8f0cfa52 | 456 | int cap_inode_removexattr(struct dentry *dentry, const char *name) |
1da177e4 | 457 | { |
b5376771 SH |
458 | if (!strcmp(name, XATTR_NAME_CAPS)) { |
459 | if (!capable(CAP_SETFCAP)) | |
460 | return -EPERM; | |
461 | return 0; | |
462 | } else if (!strncmp(name, XATTR_SECURITY_PREFIX, | |
1da177e4 LT |
463 | sizeof(XATTR_SECURITY_PREFIX) - 1) && |
464 | !capable(CAP_SYS_ADMIN)) | |
465 | return -EPERM; | |
466 | return 0; | |
467 | } | |
468 | ||
469 | /* moved from kernel/sys.c. */ | |
470 | /* | |
471 | * cap_emulate_setxuid() fixes the effective / permitted capabilities of | |
472 | * a process after a call to setuid, setreuid, or setresuid. | |
473 | * | |
474 | * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of | |
475 | * {r,e,s}uid != 0, the permitted and effective capabilities are | |
476 | * cleared. | |
477 | * | |
478 | * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective | |
479 | * capabilities of the process are cleared. | |
480 | * | |
481 | * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective | |
482 | * capabilities are set to the permitted capabilities. | |
483 | * | |
484 | * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should | |
485 | * never happen. | |
486 | * | |
487 | * -astor | |
488 | * | |
489 | * cevans - New behaviour, Oct '99 | |
490 | * A process may, via prctl(), elect to keep its capabilities when it | |
491 | * calls setuid() and switches away from uid==0. Both permitted and | |
492 | * effective sets will be retained. | |
493 | * Without this change, it was impossible for a daemon to drop only some | |
494 | * of its privilege. The call to setuid(!=0) would drop all privileges! | |
495 | * Keeping uid 0 is not an option because uid 0 owns too many vital | |
496 | * files.. | |
497 | * Thanks to Olaf Kirch and Peter Benie for spotting this. | |
498 | */ | |
499 | static inline void cap_emulate_setxuid (int old_ruid, int old_euid, | |
500 | int old_suid) | |
501 | { | |
b103c598 DH |
502 | uid_t euid = current_euid(); |
503 | ||
1da177e4 | 504 | if ((old_ruid == 0 || old_euid == 0 || old_suid == 0) && |
b103c598 | 505 | (current_uid() != 0 && euid != 0 && current_suid() != 0) && |
3898b1b4 | 506 | !issecure(SECURE_KEEP_CAPS)) { |
1da177e4 LT |
507 | cap_clear (current->cap_permitted); |
508 | cap_clear (current->cap_effective); | |
509 | } | |
b103c598 | 510 | if (old_euid == 0 && euid != 0) { |
1da177e4 LT |
511 | cap_clear (current->cap_effective); |
512 | } | |
b103c598 | 513 | if (old_euid != 0 && euid == 0) { |
1da177e4 LT |
514 | current->cap_effective = current->cap_permitted; |
515 | } | |
516 | } | |
517 | ||
518 | int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid, | |
519 | int flags) | |
520 | { | |
521 | switch (flags) { | |
522 | case LSM_SETID_RE: | |
523 | case LSM_SETID_ID: | |
524 | case LSM_SETID_RES: | |
525 | /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */ | |
526 | if (!issecure (SECURE_NO_SETUID_FIXUP)) { | |
527 | cap_emulate_setxuid (old_ruid, old_euid, old_suid); | |
528 | } | |
529 | break; | |
530 | case LSM_SETID_FS: | |
531 | { | |
532 | uid_t old_fsuid = old_ruid; | |
533 | ||
534 | /* Copied from kernel/sys.c:setfsuid. */ | |
535 | ||
536 | /* | |
537 | * FIXME - is fsuser used for all CAP_FS_MASK capabilities? | |
538 | * if not, we might be a bit too harsh here. | |
539 | */ | |
540 | ||
541 | if (!issecure (SECURE_NO_SETUID_FIXUP)) { | |
b103c598 | 542 | if (old_fsuid == 0 && current_fsuid() != 0) { |
e338d263 AM |
543 | current->cap_effective = |
544 | cap_drop_fs_set( | |
545 | current->cap_effective); | |
1da177e4 | 546 | } |
b103c598 | 547 | if (old_fsuid != 0 && current_fsuid() == 0) { |
e338d263 AM |
548 | current->cap_effective = |
549 | cap_raise_fs_set( | |
550 | current->cap_effective, | |
551 | current->cap_permitted); | |
1da177e4 LT |
552 | } |
553 | } | |
554 | break; | |
555 | } | |
556 | default: | |
557 | return -EINVAL; | |
558 | } | |
559 | ||
560 | return 0; | |
561 | } | |
562 | ||
b5376771 SH |
563 | #ifdef CONFIG_SECURITY_FILE_CAPABILITIES |
564 | /* | |
565 | * Rationale: code calling task_setscheduler, task_setioprio, and | |
566 | * task_setnice, assumes that | |
567 | * . if capable(cap_sys_nice), then those actions should be allowed | |
568 | * . if not capable(cap_sys_nice), but acting on your own processes, | |
569 | * then those actions should be allowed | |
570 | * This is insufficient now since you can call code without suid, but | |
571 | * yet with increased caps. | |
572 | * So we check for increased caps on the target process. | |
573 | */ | |
de45e806 | 574 | static int cap_safe_nice(struct task_struct *p) |
b5376771 SH |
575 | { |
576 | if (!cap_issubset(p->cap_permitted, current->cap_permitted) && | |
5cd9c58f | 577 | !capable(CAP_SYS_NICE)) |
b5376771 SH |
578 | return -EPERM; |
579 | return 0; | |
580 | } | |
581 | ||
582 | int cap_task_setscheduler (struct task_struct *p, int policy, | |
583 | struct sched_param *lp) | |
584 | { | |
585 | return cap_safe_nice(p); | |
586 | } | |
587 | ||
588 | int cap_task_setioprio (struct task_struct *p, int ioprio) | |
589 | { | |
590 | return cap_safe_nice(p); | |
591 | } | |
592 | ||
593 | int cap_task_setnice (struct task_struct *p, int nice) | |
594 | { | |
595 | return cap_safe_nice(p); | |
596 | } | |
597 | ||
3b7391de SH |
598 | /* |
599 | * called from kernel/sys.c for prctl(PR_CABSET_DROP) | |
600 | * done without task_capability_lock() because it introduces | |
601 | * no new races - i.e. only another task doing capget() on | |
602 | * this task could get inconsistent info. There can be no | |
603 | * racing writer bc a task can only change its own caps. | |
604 | */ | |
3898b1b4 | 605 | static long cap_prctl_drop(unsigned long cap) |
3b7391de SH |
606 | { |
607 | if (!capable(CAP_SETPCAP)) | |
608 | return -EPERM; | |
609 | if (!cap_valid(cap)) | |
610 | return -EINVAL; | |
611 | cap_lower(current->cap_bset, cap); | |
612 | return 0; | |
613 | } | |
3898b1b4 | 614 | |
b5376771 SH |
615 | #else |
616 | int cap_task_setscheduler (struct task_struct *p, int policy, | |
617 | struct sched_param *lp) | |
618 | { | |
619 | return 0; | |
620 | } | |
621 | int cap_task_setioprio (struct task_struct *p, int ioprio) | |
622 | { | |
623 | return 0; | |
624 | } | |
625 | int cap_task_setnice (struct task_struct *p, int nice) | |
626 | { | |
627 | return 0; | |
628 | } | |
b5376771 SH |
629 | #endif |
630 | ||
3898b1b4 AM |
631 | int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3, |
632 | unsigned long arg4, unsigned long arg5, long *rc_p) | |
633 | { | |
634 | long error = 0; | |
635 | ||
636 | switch (option) { | |
637 | case PR_CAPBSET_READ: | |
638 | if (!cap_valid(arg2)) | |
639 | error = -EINVAL; | |
640 | else | |
641 | error = !!cap_raised(current->cap_bset, arg2); | |
642 | break; | |
643 | #ifdef CONFIG_SECURITY_FILE_CAPABILITIES | |
644 | case PR_CAPBSET_DROP: | |
645 | error = cap_prctl_drop(arg2); | |
646 | break; | |
647 | ||
648 | /* | |
649 | * The next four prctl's remain to assist with transitioning a | |
650 | * system from legacy UID=0 based privilege (when filesystem | |
651 | * capabilities are not in use) to a system using filesystem | |
652 | * capabilities only - as the POSIX.1e draft intended. | |
653 | * | |
654 | * Note: | |
655 | * | |
656 | * PR_SET_SECUREBITS = | |
657 | * issecure_mask(SECURE_KEEP_CAPS_LOCKED) | |
658 | * | issecure_mask(SECURE_NOROOT) | |
659 | * | issecure_mask(SECURE_NOROOT_LOCKED) | |
660 | * | issecure_mask(SECURE_NO_SETUID_FIXUP) | |
661 | * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED) | |
662 | * | |
663 | * will ensure that the current process and all of its | |
664 | * children will be locked into a pure | |
665 | * capability-based-privilege environment. | |
666 | */ | |
667 | case PR_SET_SECUREBITS: | |
668 | if ((((current->securebits & SECURE_ALL_LOCKS) >> 1) | |
669 | & (current->securebits ^ arg2)) /*[1]*/ | |
670 | || ((current->securebits & SECURE_ALL_LOCKS | |
671 | & ~arg2)) /*[2]*/ | |
672 | || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/ | |
06112163 | 673 | || (cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0)) { /*[4]*/ |
3898b1b4 AM |
674 | /* |
675 | * [1] no changing of bits that are locked | |
676 | * [2] no unlocking of locks | |
677 | * [3] no setting of unsupported bits | |
678 | * [4] doing anything requires privilege (go read about | |
679 | * the "sendmail capabilities bug") | |
680 | */ | |
681 | error = -EPERM; /* cannot change a locked bit */ | |
682 | } else { | |
683 | current->securebits = arg2; | |
684 | } | |
685 | break; | |
686 | case PR_GET_SECUREBITS: | |
687 | error = current->securebits; | |
688 | break; | |
689 | ||
690 | #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */ | |
691 | ||
692 | case PR_GET_KEEPCAPS: | |
693 | if (issecure(SECURE_KEEP_CAPS)) | |
694 | error = 1; | |
695 | break; | |
696 | case PR_SET_KEEPCAPS: | |
697 | if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */ | |
698 | error = -EINVAL; | |
699 | else if (issecure(SECURE_KEEP_CAPS_LOCKED)) | |
700 | error = -EPERM; | |
701 | else if (arg2) | |
702 | current->securebits |= issecure_mask(SECURE_KEEP_CAPS); | |
703 | else | |
704 | current->securebits &= | |
705 | ~issecure_mask(SECURE_KEEP_CAPS); | |
706 | break; | |
707 | ||
708 | default: | |
709 | /* No functionality available - continue with default */ | |
710 | return 0; | |
711 | } | |
712 | ||
713 | /* Functionality provided */ | |
714 | *rc_p = error; | |
715 | return 1; | |
716 | } | |
717 | ||
1da177e4 LT |
718 | void cap_task_reparent_to_init (struct task_struct *p) |
719 | { | |
e338d263 AM |
720 | cap_set_init_eff(p->cap_effective); |
721 | cap_clear(p->cap_inheritable); | |
722 | cap_set_full(p->cap_permitted); | |
3898b1b4 | 723 | p->securebits = SECUREBITS_DEFAULT; |
1da177e4 LT |
724 | return; |
725 | } | |
726 | ||
727 | int cap_syslog (int type) | |
728 | { | |
729 | if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN)) | |
730 | return -EPERM; | |
731 | return 0; | |
732 | } | |
733 | ||
34b4e4aa | 734 | int cap_vm_enough_memory(struct mm_struct *mm, long pages) |
1da177e4 LT |
735 | { |
736 | int cap_sys_admin = 0; | |
737 | ||
06112163 | 738 | if (cap_capable(current, CAP_SYS_ADMIN, SECURITY_CAP_NOAUDIT) == 0) |
1da177e4 | 739 | cap_sys_admin = 1; |
34b4e4aa | 740 | return __vm_enough_memory(mm, pages, cap_sys_admin); |
1da177e4 LT |
741 | } |
742 |