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