All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Josef Bacik" <josef@toxicpanda.com>,
	"Serge Hallyn" <serge@hallyn.com>,
	"Paul Moore" <paul@paul-moore.com>,
	"Christian Brauner" <brauner@kernel.org>
Cc: "James Morris" <jmorris@namei.org>,
	"David Howells" <dhowells@redhat.com>,
	"Jarkko Sakkinen" <jarkko@kernel.org>,
	"Andrew G. Morgan" <morgan@kernel.org>,
	"Serge Hallyn" <sergeh@kernel.org>,
	linux-security-module@vger.kernel.org, keyrings@vger.kernel.org,
	"Shuah Khan" <shuah@kernel.org>,
	"Bradley Morgan" <brads@mainlining.org>
Subject: Re: [PATCH v9 2/3] capabilities: don't let ptrace borrow CAP_SETFCAP
Date: Tue, 10 Nov 2026 23:35:34 +0000	[thread overview]
Message-ID: <sashiko-outbox-207942@kernel.org> (raw)
In-Reply-To: <20261109-b4-setfcap-userns-v9-2-0d3f00976ce2@toxicpanda.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] capabilities: bypass of cap_ptrace_borrows_setfcap() via target
  task
--

commit 13b53bed949001ddba3ad97bb6d9dfe1459dfe6f
Author: Josef Bacik <josef@toxicpanda.com>

capabilities: don't let ptrace borrow CAP_SETFCAP

This commit denies cross-namespace PTRACE_MODE_ATTACH and PTRACE_TRACEME
when a tracer lacking CAP_SETFCAP and CAP_SYS_PTRACE attempts to trace a
target task that holds CAP_SETFCAP or can regain it from its bounding set
in a user namespace where setns() would refuse the tracer.

Link: https://lore.kernel.org/all/fd159e68-c613-4c7d-bfba-8af0a3f3a96e@hallyn.com/

> diff --git a/security/commoncap.c b/security/commoncap.c
> index 0ece141c68a69..d550a4e5d36a3 100644
> --- a/security/commoncap.c
> +++ b/security/commoncap.c
> @@ -176,14 +176,35 @@ int cap_settime(const struct timespec64 *ts, const struct timezone *tz)
>  	return 0;
>  }
>  
> +/*
> + * See cred_borrows_setfcap().  Within one namespace the usual rules apply: a
> + * tracer with CAP_SYS_PTRACE can take control of tasks with CAP_SETFCAP
> + * anyway.  A target that still has CAP_SETFCAP in its bounding set gets it
> + * back from an execve() the tracer can make it do.
> + */
> +static bool cap_ptrace_borrows_setfcap(const struct cred *cred,
> +				       const struct cred *child_cred)
> +{
> +	if (cred->user_ns == child_cred->user_ns)
> +		return false;
> +	if (!cap_raised(child_cred->cap_permitted, CAP_SETFCAP) &&
> +	    !cap_raised(child_cred->cap_bset, CAP_SETFCAP))
> +		return false;
> +	return !cap_raised(cred->cap_permitted, CAP_SYS_PTRACE) &&
> +	       cred_borrows_setfcap(cred, child_cred->user_ns);
> +}

[Severity: High]
Can a tracer still make the target task obtain or exercise CAP_SETFCAP
over the tracer's root user when CAP_SETFCAP is cleared in both
child_cred->cap_permitted and child_cred->cap_bset?

If CAP_SETFCAP remains raised in child_cred->cap_inheritable, making a
root target task in child_cred->user_ns call execve() restores CAP_SETFCAP
in new->cap_permitted via handle_privileged_root(), which sets
new->cap_permitted = cap_combine(old->cap_bset, old->cap_inheritable).

Even if child_cred->cap_inheritable is empty, the tracer can make the
target task:
- call unshare(CLONE_NEWUSER), where set_cred_user_ns() resets
  cap_permitted, cap_effective and cap_bset to CAP_FULL_SET (and map a
  non-zero UID mapped to the tracer's root kuid to 0 in the new
  namespace),
- write "0 0 1" to /proc/self/uid_map using CAP_SYS_ADMIN if
  child_cred->user_ns is still unmapped and parent_could_setfcap is true,
  or
- ptrace another task in child_cred->user_ns that still holds CAP_SETFCAP,
  since cap_ptrace_borrows_setfcap() allows same-namespace ptrace.

In each case, the tracer can still write file capabilities valid for its
root user.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261109-b4-setfcap-userns-v9-0-0d3f00976ce2@toxicpanda.com?part=2

  reply	other threads:[~2026-11-10 23:35 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-09 19:49 [PATCH v9 0/3] capabilities: close the ways around the CAP_SETFCAP rule for uid 0 Josef Bacik
2026-11-09 19:49 ` [PATCH v9 1/3] userns: don't let setns() lend CAP_SETFCAP Josef Bacik
2026-11-10 23:20   ` sashiko-bot
2026-11-09 19:49 ` [PATCH v9 2/3] capabilities: don't let ptrace borrow CAP_SETFCAP Josef Bacik
2026-11-10 23:35   ` sashiko-bot [this message]
2026-11-09 19:49 ` [PATCH v9 3/3] selftests/namespaces: test the CAP_SETFCAP rule for user namespaces Josef Bacik
2026-11-10 23:52   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-207942@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=brads@mainlining.org \
    --cc=brauner@kernel.org \
    --cc=dhowells@redhat.com \
    --cc=jarkko@kernel.org \
    --cc=jmorris@namei.org \
    --cc=josef@toxicpanda.com \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=morgan@kernel.org \
    --cc=paul@paul-moore.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=serge@hallyn.com \
    --cc=sergeh@kernel.org \
    --cc=shuah@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.