Implement file posix capabilities
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / security / commoncap.c
1 /* Common capabilities, needed by capability.o and root_plug.o
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
10 #include <linux/capability.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/security.h>
15 #include <linux/file.h>
16 #include <linux/mm.h>
17 #include <linux/mman.h>
18 #include <linux/pagemap.h>
19 #include <linux/swap.h>
20 #include <linux/skbuff.h>
21 #include <linux/netlink.h>
22 #include <linux/ptrace.h>
23 #include <linux/xattr.h>
24 #include <linux/hugetlb.h>
25 #include <linux/mount.h>
26
27 int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
28 {
29 NETLINK_CB(skb).eff_cap = current->cap_effective;
30 return 0;
31 }
32
33 EXPORT_SYMBOL(cap_netlink_send);
34
35 int cap_netlink_recv(struct sk_buff *skb, int cap)
36 {
37 if (!cap_raised(NETLINK_CB(skb).eff_cap, cap))
38 return -EPERM;
39 return 0;
40 }
41
42 EXPORT_SYMBOL(cap_netlink_recv);
43
44 int cap_capable (struct task_struct *tsk, int cap)
45 {
46 /* Derived from include/linux/sched.h:capable. */
47 if (cap_raised(tsk->cap_effective, cap))
48 return 0;
49 return -EPERM;
50 }
51
52 int cap_settime(struct timespec *ts, struct timezone *tz)
53 {
54 if (!capable(CAP_SYS_TIME))
55 return -EPERM;
56 return 0;
57 }
58
59 int cap_ptrace (struct task_struct *parent, struct task_struct *child)
60 {
61 /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */
62 if (!cap_issubset(child->cap_permitted, parent->cap_permitted) &&
63 !__capable(parent, CAP_SYS_PTRACE))
64 return -EPERM;
65 return 0;
66 }
67
68 int cap_capget (struct task_struct *target, kernel_cap_t *effective,
69 kernel_cap_t *inheritable, kernel_cap_t *permitted)
70 {
71 /* Derived from kernel/capability.c:sys_capget. */
72 *effective = cap_t (target->cap_effective);
73 *inheritable = cap_t (target->cap_inheritable);
74 *permitted = cap_t (target->cap_permitted);
75 return 0;
76 }
77
78 int cap_capset_check (struct task_struct *target, kernel_cap_t *effective,
79 kernel_cap_t *inheritable, kernel_cap_t *permitted)
80 {
81 /* Derived from kernel/capability.c:sys_capset. */
82 /* verify restrictions on target's new Inheritable set */
83 if (!cap_issubset (*inheritable,
84 cap_combine (target->cap_inheritable,
85 current->cap_permitted))) {
86 return -EPERM;
87 }
88
89 /* verify restrictions on target's new Permitted set */
90 if (!cap_issubset (*permitted,
91 cap_combine (target->cap_permitted,
92 current->cap_permitted))) {
93 return -EPERM;
94 }
95
96 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
97 if (!cap_issubset (*effective, *permitted)) {
98 return -EPERM;
99 }
100
101 return 0;
102 }
103
104 void cap_capset_set (struct task_struct *target, kernel_cap_t *effective,
105 kernel_cap_t *inheritable, kernel_cap_t *permitted)
106 {
107 target->cap_effective = *effective;
108 target->cap_inheritable = *inheritable;
109 target->cap_permitted = *permitted;
110 }
111
112 static inline void bprm_clear_caps(struct linux_binprm *bprm)
113 {
114 cap_clear(bprm->cap_inheritable);
115 cap_clear(bprm->cap_permitted);
116 bprm->cap_effective = false;
117 }
118
119 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
120
121 int cap_inode_need_killpriv(struct dentry *dentry)
122 {
123 struct inode *inode = dentry->d_inode;
124 int error;
125
126 if (!inode->i_op || !inode->i_op->getxattr)
127 return 0;
128
129 error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
130 if (error <= 0)
131 return 0;
132 return 1;
133 }
134
135 int cap_inode_killpriv(struct dentry *dentry)
136 {
137 struct inode *inode = dentry->d_inode;
138
139 if (!inode->i_op || !inode->i_op->removexattr)
140 return 0;
141
142 return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
143 }
144
145 static inline int cap_from_disk(__le32 *caps, struct linux_binprm *bprm,
146 int size)
147 {
148 __u32 magic_etc;
149
150 if (size != XATTR_CAPS_SZ)
151 return -EINVAL;
152
153 magic_etc = le32_to_cpu(caps[0]);
154
155 switch ((magic_etc & VFS_CAP_REVISION_MASK)) {
156 case VFS_CAP_REVISION:
157 if (magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
158 bprm->cap_effective = true;
159 else
160 bprm->cap_effective = false;
161 bprm->cap_permitted = to_cap_t( le32_to_cpu(caps[1]) );
162 bprm->cap_inheritable = to_cap_t( le32_to_cpu(caps[2]) );
163 return 0;
164 default:
165 return -EINVAL;
166 }
167 }
168
169 /* Locate any VFS capabilities: */
170 static int get_file_caps(struct linux_binprm *bprm)
171 {
172 struct dentry *dentry;
173 int rc = 0;
174 __le32 v1caps[XATTR_CAPS_SZ];
175 struct inode *inode;
176
177 if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID) {
178 bprm_clear_caps(bprm);
179 return 0;
180 }
181
182 dentry = dget(bprm->file->f_dentry);
183 inode = dentry->d_inode;
184 if (!inode->i_op || !inode->i_op->getxattr)
185 goto out;
186
187 rc = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, &v1caps,
188 XATTR_CAPS_SZ);
189 if (rc == -ENODATA || rc == -EOPNOTSUPP) {
190 /* no data, that's ok */
191 rc = 0;
192 goto out;
193 }
194 if (rc < 0)
195 goto out;
196
197 rc = cap_from_disk(v1caps, bprm, rc);
198 if (rc)
199 printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
200 __FUNCTION__, rc, bprm->filename);
201
202 out:
203 dput(dentry);
204 if (rc)
205 bprm_clear_caps(bprm);
206
207 return rc;
208 }
209
210 #else
211 int cap_inode_need_killpriv(struct dentry *dentry)
212 {
213 return 0;
214 }
215
216 int cap_inode_killpriv(struct dentry *dentry)
217 {
218 return 0;
219 }
220
221 static inline int get_file_caps(struct linux_binprm *bprm)
222 {
223 bprm_clear_caps(bprm);
224 return 0;
225 }
226 #endif
227
228 int cap_bprm_set_security (struct linux_binprm *bprm)
229 {
230 int ret;
231
232 ret = get_file_caps(bprm);
233 if (ret)
234 printk(KERN_NOTICE "%s: get_file_caps returned %d for %s\n",
235 __FUNCTION__, ret, bprm->filename);
236
237 /* To support inheritance of root-permissions and suid-root
238 * executables under compatibility mode, we raise all three
239 * capability sets for the file.
240 *
241 * If only the real uid is 0, we only raise the inheritable
242 * and permitted sets of the executable file.
243 */
244
245 if (!issecure (SECURE_NOROOT)) {
246 if (bprm->e_uid == 0 || current->uid == 0) {
247 cap_set_full (bprm->cap_inheritable);
248 cap_set_full (bprm->cap_permitted);
249 }
250 if (bprm->e_uid == 0)
251 bprm->cap_effective = true;
252 }
253
254 return ret;
255 }
256
257 void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
258 {
259 /* Derived from fs/exec.c:compute_creds. */
260 kernel_cap_t new_permitted, working;
261
262 new_permitted = cap_intersect (bprm->cap_permitted, cap_bset);
263 working = cap_intersect (bprm->cap_inheritable,
264 current->cap_inheritable);
265 new_permitted = cap_combine (new_permitted, working);
266
267 if (bprm->e_uid != current->uid || bprm->e_gid != current->gid ||
268 !cap_issubset (new_permitted, current->cap_permitted)) {
269 set_dumpable(current->mm, suid_dumpable);
270 current->pdeath_signal = 0;
271
272 if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
273 if (!capable(CAP_SETUID)) {
274 bprm->e_uid = current->uid;
275 bprm->e_gid = current->gid;
276 }
277 if (!capable (CAP_SETPCAP)) {
278 new_permitted = cap_intersect (new_permitted,
279 current->cap_permitted);
280 }
281 }
282 }
283
284 current->suid = current->euid = current->fsuid = bprm->e_uid;
285 current->sgid = current->egid = current->fsgid = bprm->e_gid;
286
287 /* For init, we want to retain the capabilities set
288 * in the init_task struct. Thus we skip the usual
289 * capability rules */
290 if (!is_init(current)) {
291 current->cap_permitted = new_permitted;
292 current->cap_effective = bprm->cap_effective ?
293 new_permitted : 0;
294 }
295
296 /* AUD: Audit candidate if current->cap_effective is set */
297
298 current->keep_capabilities = 0;
299 }
300
301 int cap_bprm_secureexec (struct linux_binprm *bprm)
302 {
303 if (current->uid != 0) {
304 if (bprm->cap_effective)
305 return 1;
306 if (!cap_isclear(bprm->cap_permitted))
307 return 1;
308 if (!cap_isclear(bprm->cap_inheritable))
309 return 1;
310 }
311
312 return (current->euid != current->uid ||
313 current->egid != current->gid);
314 }
315
316 int cap_inode_setxattr(struct dentry *dentry, char *name, void *value,
317 size_t size, int flags)
318 {
319 if (!strcmp(name, XATTR_NAME_CAPS)) {
320 if (!capable(CAP_SETFCAP))
321 return -EPERM;
322 return 0;
323 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
324 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
325 !capable(CAP_SYS_ADMIN))
326 return -EPERM;
327 return 0;
328 }
329
330 int cap_inode_removexattr(struct dentry *dentry, char *name)
331 {
332 if (!strcmp(name, XATTR_NAME_CAPS)) {
333 if (!capable(CAP_SETFCAP))
334 return -EPERM;
335 return 0;
336 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
337 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
338 !capable(CAP_SYS_ADMIN))
339 return -EPERM;
340 return 0;
341 }
342
343 /* moved from kernel/sys.c. */
344 /*
345 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
346 * a process after a call to setuid, setreuid, or setresuid.
347 *
348 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
349 * {r,e,s}uid != 0, the permitted and effective capabilities are
350 * cleared.
351 *
352 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
353 * capabilities of the process are cleared.
354 *
355 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
356 * capabilities are set to the permitted capabilities.
357 *
358 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
359 * never happen.
360 *
361 * -astor
362 *
363 * cevans - New behaviour, Oct '99
364 * A process may, via prctl(), elect to keep its capabilities when it
365 * calls setuid() and switches away from uid==0. Both permitted and
366 * effective sets will be retained.
367 * Without this change, it was impossible for a daemon to drop only some
368 * of its privilege. The call to setuid(!=0) would drop all privileges!
369 * Keeping uid 0 is not an option because uid 0 owns too many vital
370 * files..
371 * Thanks to Olaf Kirch and Peter Benie for spotting this.
372 */
373 static inline void cap_emulate_setxuid (int old_ruid, int old_euid,
374 int old_suid)
375 {
376 if ((old_ruid == 0 || old_euid == 0 || old_suid == 0) &&
377 (current->uid != 0 && current->euid != 0 && current->suid != 0) &&
378 !current->keep_capabilities) {
379 cap_clear (current->cap_permitted);
380 cap_clear (current->cap_effective);
381 }
382 if (old_euid == 0 && current->euid != 0) {
383 cap_clear (current->cap_effective);
384 }
385 if (old_euid != 0 && current->euid == 0) {
386 current->cap_effective = current->cap_permitted;
387 }
388 }
389
390 int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid,
391 int flags)
392 {
393 switch (flags) {
394 case LSM_SETID_RE:
395 case LSM_SETID_ID:
396 case LSM_SETID_RES:
397 /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
398 if (!issecure (SECURE_NO_SETUID_FIXUP)) {
399 cap_emulate_setxuid (old_ruid, old_euid, old_suid);
400 }
401 break;
402 case LSM_SETID_FS:
403 {
404 uid_t old_fsuid = old_ruid;
405
406 /* Copied from kernel/sys.c:setfsuid. */
407
408 /*
409 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
410 * if not, we might be a bit too harsh here.
411 */
412
413 if (!issecure (SECURE_NO_SETUID_FIXUP)) {
414 if (old_fsuid == 0 && current->fsuid != 0) {
415 cap_t (current->cap_effective) &=
416 ~CAP_FS_MASK;
417 }
418 if (old_fsuid != 0 && current->fsuid == 0) {
419 cap_t (current->cap_effective) |=
420 (cap_t (current->cap_permitted) &
421 CAP_FS_MASK);
422 }
423 }
424 break;
425 }
426 default:
427 return -EINVAL;
428 }
429
430 return 0;
431 }
432
433 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
434 /*
435 * Rationale: code calling task_setscheduler, task_setioprio, and
436 * task_setnice, assumes that
437 * . if capable(cap_sys_nice), then those actions should be allowed
438 * . if not capable(cap_sys_nice), but acting on your own processes,
439 * then those actions should be allowed
440 * This is insufficient now since you can call code without suid, but
441 * yet with increased caps.
442 * So we check for increased caps on the target process.
443 */
444 static inline int cap_safe_nice(struct task_struct *p)
445 {
446 if (!cap_issubset(p->cap_permitted, current->cap_permitted) &&
447 !__capable(current, CAP_SYS_NICE))
448 return -EPERM;
449 return 0;
450 }
451
452 int cap_task_setscheduler (struct task_struct *p, int policy,
453 struct sched_param *lp)
454 {
455 return cap_safe_nice(p);
456 }
457
458 int cap_task_setioprio (struct task_struct *p, int ioprio)
459 {
460 return cap_safe_nice(p);
461 }
462
463 int cap_task_setnice (struct task_struct *p, int nice)
464 {
465 return cap_safe_nice(p);
466 }
467
468 int cap_task_kill(struct task_struct *p, struct siginfo *info,
469 int sig, u32 secid)
470 {
471 if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info)))
472 return 0;
473
474 if (secid)
475 /*
476 * Signal sent as a particular user.
477 * Capabilities are ignored. May be wrong, but it's the
478 * only thing we can do at the moment.
479 * Used only by usb drivers?
480 */
481 return 0;
482 if (cap_issubset(p->cap_permitted, current->cap_permitted))
483 return 0;
484 if (capable(CAP_KILL))
485 return 0;
486
487 return -EPERM;
488 }
489 #else
490 int cap_task_setscheduler (struct task_struct *p, int policy,
491 struct sched_param *lp)
492 {
493 return 0;
494 }
495 int cap_task_setioprio (struct task_struct *p, int ioprio)
496 {
497 return 0;
498 }
499 int cap_task_setnice (struct task_struct *p, int nice)
500 {
501 return 0;
502 }
503 int cap_task_kill(struct task_struct *p, struct siginfo *info,
504 int sig, u32 secid)
505 {
506 return 0;
507 }
508 #endif
509
510 void cap_task_reparent_to_init (struct task_struct *p)
511 {
512 p->cap_effective = CAP_INIT_EFF_SET;
513 p->cap_inheritable = CAP_INIT_INH_SET;
514 p->cap_permitted = CAP_FULL_SET;
515 p->keep_capabilities = 0;
516 return;
517 }
518
519 int cap_syslog (int type)
520 {
521 if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
522 return -EPERM;
523 return 0;
524 }
525
526 int cap_vm_enough_memory(struct mm_struct *mm, long pages)
527 {
528 int cap_sys_admin = 0;
529
530 if (cap_capable(current, CAP_SYS_ADMIN) == 0)
531 cap_sys_admin = 1;
532 return __vm_enough_memory(mm, pages, cap_sys_admin);
533 }
534
535 EXPORT_SYMBOL(cap_capable);
536 EXPORT_SYMBOL(cap_settime);
537 EXPORT_SYMBOL(cap_ptrace);
538 EXPORT_SYMBOL(cap_capget);
539 EXPORT_SYMBOL(cap_capset_check);
540 EXPORT_SYMBOL(cap_capset_set);
541 EXPORT_SYMBOL(cap_bprm_set_security);
542 EXPORT_SYMBOL(cap_bprm_apply_creds);
543 EXPORT_SYMBOL(cap_bprm_secureexec);
544 EXPORT_SYMBOL(cap_inode_setxattr);
545 EXPORT_SYMBOL(cap_inode_removexattr);
546 EXPORT_SYMBOL(cap_task_post_setuid);
547 EXPORT_SYMBOL(cap_task_kill);
548 EXPORT_SYMBOL(cap_task_setscheduler);
549 EXPORT_SYMBOL(cap_task_setioprio);
550 EXPORT_SYMBOL(cap_task_setnice);
551 EXPORT_SYMBOL(cap_task_reparent_to_init);
552 EXPORT_SYMBOL(cap_syslog);
553 EXPORT_SYMBOL(cap_vm_enough_memory);