aboutsummaryrefslogtreecommitdiffstats
path: root/security/commoncap.c
diff options
context:
space:
mode:
Diffstat (limited to 'security/commoncap.c')
-rw-r--r--security/commoncap.c120
1 files changed, 70 insertions, 50 deletions
diff --git a/security/commoncap.c b/security/commoncap.c
index 9d172e6e330c..a93b3b733079 100644
--- a/security/commoncap.c
+++ b/security/commoncap.c
@@ -27,7 +27,7 @@
27#include <linux/sched.h> 27#include <linux/sched.h>
28#include <linux/prctl.h> 28#include <linux/prctl.h>
29#include <linux/securebits.h> 29#include <linux/securebits.h>
30#include <linux/syslog.h> 30#include <linux/user_namespace.h>
31 31
32/* 32/*
33 * If a non-root user executes a setuid-root binary in 33 * If a non-root user executes a setuid-root binary in
@@ -53,13 +53,12 @@ static void warn_setuid_and_fcaps_mixed(const char *fname)
53 53
54int cap_netlink_send(struct sock *sk, struct sk_buff *skb) 54int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
55{ 55{
56 NETLINK_CB(skb).eff_cap = current_cap();
57 return 0; 56 return 0;
58} 57}
59 58
60int cap_netlink_recv(struct sk_buff *skb, int cap) 59int cap_netlink_recv(struct sk_buff *skb, int cap)
61{ 60{
62 if (!cap_raised(NETLINK_CB(skb).eff_cap, cap)) 61 if (!cap_raised(current_cap(), cap))
63 return -EPERM; 62 return -EPERM;
64 return 0; 63 return 0;
65} 64}
@@ -69,6 +68,7 @@ EXPORT_SYMBOL(cap_netlink_recv);
69 * cap_capable - Determine whether a task has a particular effective capability 68 * cap_capable - Determine whether a task has a particular effective capability
70 * @tsk: The task to query 69 * @tsk: The task to query
71 * @cred: The credentials to use 70 * @cred: The credentials to use
71 * @ns: The user namespace in which we need the capability
72 * @cap: The capability to check for 72 * @cap: The capability to check for
73 * @audit: Whether to write an audit message or not 73 * @audit: Whether to write an audit message or not
74 * 74 *
@@ -80,10 +80,30 @@ EXPORT_SYMBOL(cap_netlink_recv);
80 * cap_has_capability() returns 0 when a task has a capability, but the 80 * cap_has_capability() returns 0 when a task has a capability, but the
81 * kernel's capable() and has_capability() returns 1 for this case. 81 * kernel's capable() and has_capability() returns 1 for this case.
82 */ 82 */
83int cap_capable(struct task_struct *tsk, const struct cred *cred, int cap, 83int cap_capable(struct task_struct *tsk, const struct cred *cred,
84 int audit) 84 struct user_namespace *targ_ns, int cap, int audit)
85{ 85{
86 return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM; 86 for (;;) {
87 /* The creator of the user namespace has all caps. */
88 if (targ_ns != &init_user_ns && targ_ns->creator == cred->user)
89 return 0;
90
91 /* Do we have the necessary capabilities? */
92 if (targ_ns == cred->user->user_ns)
93 return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
94
95 /* Have we tried all of the parent namespaces? */
96 if (targ_ns == &init_user_ns)
97 return -EPERM;
98
99 /*
100 *If you have a capability in a parent user ns, then you have
101 * it over all children user namespaces as well.
102 */
103 targ_ns = targ_ns->creator->user_ns;
104 }
105
106 /* We never get here */
87} 107}
88 108
89/** 109/**
@@ -94,7 +114,7 @@ int cap_capable(struct task_struct *tsk, const struct cred *cred, int cap,
94 * Determine whether the current process may set the system clock and timezone 114 * Determine whether the current process may set the system clock and timezone
95 * information, returning 0 if permission granted, -ve if denied. 115 * information, returning 0 if permission granted, -ve if denied.
96 */ 116 */
97int cap_settime(struct timespec *ts, struct timezone *tz) 117int cap_settime(const struct timespec *ts, const struct timezone *tz)
98{ 118{
99 if (!capable(CAP_SYS_TIME)) 119 if (!capable(CAP_SYS_TIME))
100 return -EPERM; 120 return -EPERM;
@@ -107,18 +127,30 @@ int cap_settime(struct timespec *ts, struct timezone *tz)
107 * @child: The process to be accessed 127 * @child: The process to be accessed
108 * @mode: The mode of attachment. 128 * @mode: The mode of attachment.
109 * 129 *
130 * If we are in the same or an ancestor user_ns and have all the target
131 * task's capabilities, then ptrace access is allowed.
132 * If we have the ptrace capability to the target user_ns, then ptrace
133 * access is allowed.
134 * Else denied.
135 *
110 * Determine whether a process may access another, returning 0 if permission 136 * Determine whether a process may access another, returning 0 if permission
111 * granted, -ve if denied. 137 * granted, -ve if denied.
112 */ 138 */
113int cap_ptrace_access_check(struct task_struct *child, unsigned int mode) 139int cap_ptrace_access_check(struct task_struct *child, unsigned int mode)
114{ 140{
115 int ret = 0; 141 int ret = 0;
142 const struct cred *cred, *child_cred;
116 143
117 rcu_read_lock(); 144 rcu_read_lock();
118 if (!cap_issubset(__task_cred(child)->cap_permitted, 145 cred = current_cred();
119 current_cred()->cap_permitted) && 146 child_cred = __task_cred(child);
120 !capable(CAP_SYS_PTRACE)) 147 if (cred->user->user_ns == child_cred->user->user_ns &&
121 ret = -EPERM; 148 cap_issubset(child_cred->cap_permitted, cred->cap_permitted))
149 goto out;
150 if (ns_capable(child_cred->user->user_ns, CAP_SYS_PTRACE))
151 goto out;
152 ret = -EPERM;
153out:
122 rcu_read_unlock(); 154 rcu_read_unlock();
123 return ret; 155 return ret;
124} 156}
@@ -127,18 +159,30 @@ int cap_ptrace_access_check(struct task_struct *child, unsigned int mode)
127 * cap_ptrace_traceme - Determine whether another process may trace the current 159 * cap_ptrace_traceme - Determine whether another process may trace the current
128 * @parent: The task proposed to be the tracer 160 * @parent: The task proposed to be the tracer
129 * 161 *
162 * If parent is in the same or an ancestor user_ns and has all current's
163 * capabilities, then ptrace access is allowed.
164 * If parent has the ptrace capability to current's user_ns, then ptrace
165 * access is allowed.
166 * Else denied.
167 *
130 * Determine whether the nominated task is permitted to trace the current 168 * Determine whether the nominated task is permitted to trace the current
131 * process, returning 0 if permission is granted, -ve if denied. 169 * process, returning 0 if permission is granted, -ve if denied.
132 */ 170 */
133int cap_ptrace_traceme(struct task_struct *parent) 171int cap_ptrace_traceme(struct task_struct *parent)
134{ 172{
135 int ret = 0; 173 int ret = 0;
174 const struct cred *cred, *child_cred;
136 175
137 rcu_read_lock(); 176 rcu_read_lock();
138 if (!cap_issubset(current_cred()->cap_permitted, 177 cred = __task_cred(parent);
139 __task_cred(parent)->cap_permitted) && 178 child_cred = current_cred();
140 !has_capability(parent, CAP_SYS_PTRACE)) 179 if (cred->user->user_ns == child_cred->user->user_ns &&
141 ret = -EPERM; 180 cap_issubset(child_cred->cap_permitted, cred->cap_permitted))
181 goto out;
182 if (has_ns_capability(parent, child_cred->user->user_ns, CAP_SYS_PTRACE))
183 goto out;
184 ret = -EPERM;
185out:
142 rcu_read_unlock(); 186 rcu_read_unlock();
143 return ret; 187 return ret;
144} 188}
@@ -178,7 +222,8 @@ static inline int cap_inh_is_capped(void)
178 /* they are so limited unless the current task has the CAP_SETPCAP 222 /* they are so limited unless the current task has the CAP_SETPCAP
179 * capability 223 * capability
180 */ 224 */
181 if (cap_capable(current, current_cred(), CAP_SETPCAP, 225 if (cap_capable(current, current_cred(),
226 current_cred()->user->user_ns, CAP_SETPCAP,
182 SECURITY_CAP_AUDIT) == 0) 227 SECURITY_CAP_AUDIT) == 0)
183 return 0; 228 return 0;
184 return 1; 229 return 1;
@@ -484,15 +529,10 @@ skip:
484 new->suid = new->fsuid = new->euid; 529 new->suid = new->fsuid = new->euid;
485 new->sgid = new->fsgid = new->egid; 530 new->sgid = new->fsgid = new->egid;
486 531
487 /* For init, we want to retain the capabilities set in the initial 532 if (effective)
488 * task. Thus we skip the usual capability rules 533 new->cap_effective = new->cap_permitted;
489 */ 534 else
490 if (!is_global_init(current)) { 535 cap_clear(new->cap_effective);
491 if (effective)
492 new->cap_effective = new->cap_permitted;
493 else
494 cap_clear(new->cap_effective);
495 }
496 bprm->cap_effective = effective; 536 bprm->cap_effective = effective;
497 537
498 /* 538 /*
@@ -719,14 +759,11 @@ static int cap_safe_nice(struct task_struct *p)
719/** 759/**
720 * cap_task_setscheduler - Detemine if scheduler policy change is permitted 760 * cap_task_setscheduler - Detemine if scheduler policy change is permitted
721 * @p: The task to affect 761 * @p: The task to affect
722 * @policy: The policy to effect
723 * @lp: The parameters to the scheduling policy
724 * 762 *
725 * Detemine if the requested scheduler policy change is permitted for the 763 * Detemine if the requested scheduler policy change is permitted for the
726 * specified task, returning 0 if permission is granted, -ve if denied. 764 * specified task, returning 0 if permission is granted, -ve if denied.
727 */ 765 */
728int cap_task_setscheduler(struct task_struct *p, int policy, 766int cap_task_setscheduler(struct task_struct *p)
729 struct sched_param *lp)
730{ 767{
731 return cap_safe_nice(p); 768 return cap_safe_nice(p);
732} 769}
@@ -833,7 +870,8 @@ int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
833 & (new->securebits ^ arg2)) /*[1]*/ 870 & (new->securebits ^ arg2)) /*[1]*/
834 || ((new->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/ 871 || ((new->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
835 || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/ 872 || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
836 || (cap_capable(current, current_cred(), CAP_SETPCAP, 873 || (cap_capable(current, current_cred(),
874 current_cred()->user->user_ns, CAP_SETPCAP,
837 SECURITY_CAP_AUDIT) != 0) /*[4]*/ 875 SECURITY_CAP_AUDIT) != 0) /*[4]*/
838 /* 876 /*
839 * [1] no changing of bits that are locked 877 * [1] no changing of bits that are locked
@@ -887,24 +925,6 @@ error:
887} 925}
888 926
889/** 927/**
890 * cap_syslog - Determine whether syslog function is permitted
891 * @type: Function requested
892 * @from_file: Whether this request came from an open file (i.e. /proc)
893 *
894 * Determine whether the current process is permitted to use a particular
895 * syslog function, returning 0 if permission is granted, -ve if not.
896 */
897int cap_syslog(int type, bool from_file)
898{
899 if (type != SYSLOG_ACTION_OPEN && from_file)
900 return 0;
901 if ((type != SYSLOG_ACTION_READ_ALL &&
902 type != SYSLOG_ACTION_SIZE_BUFFER) && !capable(CAP_SYS_ADMIN))
903 return -EPERM;
904 return 0;
905}
906
907/**
908 * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted 928 * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted
909 * @mm: The VM space in which the new mapping is to be made 929 * @mm: The VM space in which the new mapping is to be made
910 * @pages: The size of the mapping 930 * @pages: The size of the mapping
@@ -916,7 +936,7 @@ int cap_vm_enough_memory(struct mm_struct *mm, long pages)
916{ 936{
917 int cap_sys_admin = 0; 937 int cap_sys_admin = 0;
918 938
919 if (cap_capable(current, current_cred(), CAP_SYS_ADMIN, 939 if (cap_capable(current, current_cred(), &init_user_ns, CAP_SYS_ADMIN,
920 SECURITY_CAP_NOAUDIT) == 0) 940 SECURITY_CAP_NOAUDIT) == 0)
921 cap_sys_admin = 1; 941 cap_sys_admin = 1;
922 return __vm_enough_memory(mm, pages, cap_sys_admin); 942 return __vm_enough_memory(mm, pages, cap_sys_admin);
@@ -943,7 +963,7 @@ int cap_file_mmap(struct file *file, unsigned long reqprot,
943 int ret = 0; 963 int ret = 0;
944 964
945 if (addr < dac_mmap_min_addr) { 965 if (addr < dac_mmap_min_addr) {
946 ret = cap_capable(current, current_cred(), CAP_SYS_RAWIO, 966 ret = cap_capable(current, current_cred(), &init_user_ns, CAP_SYS_RAWIO,
947 SECURITY_CAP_AUDIT); 967 SECURITY_CAP_AUDIT);
948 /* set PF_SUPERPRIV if it turns out we allow the low mmap */ 968 /* set PF_SUPERPRIV if it turns out we allow the low mmap */
949 if (ret == 0) 969 if (ret == 0)