Same-namespace ptrace checks only compare cap_permitted (or cap_effective), ignoring the userns capability ceiling. As a result, a task with a lowered ceiling could trace a less-restricted same-UID task in the same user namespace and use it to create a wider child user namespace. Require the tracee's cred_userns_bset() to be a subset of the tracer's in the same-namespace check in cap_ptrace_access_check() and cap_ptrace_traceme(). Cross-namespace implicit CAP_SYS_PTRACE via user namespace ownership is already bounded by cap_capable_helper(), while tracers holding explicit CAP_SYS_PTRACE retain access. Also update cred_cap_issubset() so commit_creds() resets mm->dumpable and pdeath_signal whenever a credential transition widens the effective userns capability ceiling. Suggested-by: Christian Brauner Suggested-by: Andy Lutomirski Signed-off-by: Hallgrimur H. Gunnarsson --- kernel/cred.c | 13 +++++++++---- security/commoncap.c | 12 ++++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/kernel/cred.c b/kernel/cred.c index d01df40a9a38..968122cdbf07 100644 --- a/kernel/cred.c +++ b/kernel/cred.c @@ -335,17 +335,22 @@ static bool cred_cap_issubset(const struct cred *set, const struct cred *subset) * the capabilities of subset are a subset of set. */ if (set_ns == subset_ns) - return cap_issubset(subset->cap_permitted, set->cap_permitted); + return cap_issubset(subset->cap_permitted, + set->cap_permitted) && + cap_issubset(cred_userns_bset(subset), + cred_userns_bset(set)); /* The credentials are in a different user namespaces * therefore one is a subset of the other only if a set is an * ancestor of subset and set->euid is owner of subset or one - * of subsets ancestors. + * of subsets ancestors, and set's userns capability ceiling + * covers subset's capabilities. */ for (;subset_ns != &init_user_ns; subset_ns = subset_ns->parent) { - if ((set_ns == subset_ns->parent) && + if (set_ns == subset_ns->parent && uid_eq(subset_ns->owner, set->euid)) - return true; + return cap_issubset(subset_ns->cap_bset, + cred_userns_bset(set)); } return false; diff --git a/security/commoncap.c b/security/commoncap.c index ddac8e917680..786f99eb6838 100644 --- a/security/commoncap.c +++ b/security/commoncap.c @@ -157,7 +157,8 @@ int cap_settime(const struct timespec64 *ts, const struct timezone *tz) * @mode: The mode of attachment. * * If we are in the same or an ancestor user_ns and have all the target - * task's capabilities, then ptrace access is allowed. + * task's capabilities (including its userns capability ceiling), then + * ptrace access is allowed. * If we have the ptrace capability to the target user_ns, then ptrace * access is allowed. * Else denied. @@ -179,7 +180,8 @@ int cap_ptrace_access_check(struct task_struct *child, unsigned int mode) else caller_caps = &cred->cap_permitted; if (cred->user_ns == child_cred->user_ns && - cap_issubset(child_cred->cap_permitted, *caller_caps)) + cap_issubset(child_cred->cap_permitted, *caller_caps) && + cap_issubset(cred_userns_bset(child_cred), cred_userns_bset(cred))) goto out; if (ns_capable(child_cred->user_ns, CAP_SYS_PTRACE)) goto out; @@ -194,7 +196,8 @@ int cap_ptrace_access_check(struct task_struct *child, unsigned int mode) * @parent: The task proposed to be the tracer * * If parent is in the same or an ancestor user_ns and has all current's - * capabilities, then ptrace access is allowed. + * capabilities (including its userns capability ceiling), then ptrace + * access is allowed. * If parent has the ptrace capability to current's user_ns, then ptrace * access is allowed. * Else denied. @@ -211,7 +214,8 @@ int cap_ptrace_traceme(struct task_struct *parent) cred = __task_cred(parent); child_cred = current_cred(); if (cred->user_ns == child_cred->user_ns && - cap_issubset(child_cred->cap_permitted, cred->cap_permitted)) + cap_issubset(child_cred->cap_permitted, cred->cap_permitted) && + cap_issubset(cred_userns_bset(child_cred), cred_userns_bset(cred))) goto out; if (has_ns_capability(parent, child_cred->user_ns, CAP_SYS_PTRACE)) goto out; -- 2.56.1.708.g77570a6a1d-goog