Skip to content

Commit 520d9ea

Browse files
committed
Fix cap_capable to only allow owners in the parent user namespace to have caps.
Andy Lutomirski pointed out that the current behavior of allowing the owner of a user namespace to have all caps when that owner is not in a parent user namespace is wrong. Add a test to ensure the owner of a user namespace is in the parent of the user namespace to fix this bug. Thankfully this bug did not apply to the initial user namespace, keeping the mischief that can be caused by this bug quite small. This is bug was introduced in v3.5 by commit 783291e "Simplify the user_namespace by making userns->creator a kuid." But did not matter until the permisions required to create a user namespace were relaxed allowing a user namespace to be created inside of a user namespace. The bug made it possible for the owner of a user namespace to be present in a child user namespace. Since the owner of a user nameapce is granted all capabilities it became possible for users in a grandchild user namespace to have all privilges over their parent user namspace. Reorder the checks in cap_capable. This should make the common case faster and make it clear that nothing magic happens in the initial user namespace. The reordering is safe because cred->user_ns can only be in targ_ns or targ_ns->parent but not both. Add a comment a the top of the loop to make the logic of the code clear. Add a distinct variable ns that changes as we walk up the user namespace hierarchy to make it clear which variable is changing. Acked-by: Serge Hallyn <serge.hallyn@canonical.com> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
1 parent 98f842e commit 520d9ea

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

‎security/commoncap.c‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,33 @@ int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
7676
int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
7777
int cap, int audit)
7878
{
79-
for (;;) {
80-
/* The owner of the user namespace has all caps. */
81-
if (targ_ns != &init_user_ns && uid_eq(targ_ns->owner, cred->euid))
82-
return 0;
79+
struct user_namespace *ns = targ_ns;
8380

81+
/* See if cred has the capability in the target user namespace
82+
* by examining the target user namespace and all of the target
83+
* user namespace's parents.
84+
*/
85+
for (;;) {
8486
/* Do we have the necessary capabilities? */
85-
if (targ_ns == cred->user_ns)
87+
if (ns == cred->user_ns)
8688
return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
8789

8890
/* Have we tried all of the parent namespaces? */
89-
if (targ_ns == &init_user_ns)
91+
if (ns == &init_user_ns)
9092
return -EPERM;
9193

94+
/*
95+
* The owner of the user namespace in the parent of the
96+
* user namespace has all caps.
97+
*/
98+
if ((ns->parent == cred->user_ns) && uid_eq(ns->owner, cred->euid))
99+
return 0;
100+
92101
/*
93-
*If you have a capability in a parent user ns, then you have
102+
* If you have a capability in a parent user ns, then you have
94103
* it over all children user namespaces as well.
95104
*/
96-
targ_ns = targ_ns->parent;
105+
ns = ns->parent;
97106
}
98107

99108
/* We never get here */

0 commit comments

Comments
 (0)