Linux Security Modules development
 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 3/3] selftests/namespaces: test the CAP_SETFCAP rule for user namespaces
Date: Tue, 10 Nov 2026 23:52:57 +0000	[thread overview]
Message-ID: <sashiko-outbox-207958@kernel.org> (raw)
In-Reply-To: <20261109-b4-setfcap-userns-v9-3-0d3f00976ce2@toxicpanda.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] selftests/namespaces: host /mnt creation and false skip in
  idmap_tmp()
- [Low] selftests/namespaces: add setfcap_test to .gitignore
--

commit 38eae5440ed3d06c4f6383c08c8a0e11e61d724f
Author: Josef Bacik <josef@toxicpanda.com>

selftests/namespaces: test the CAP_SETFCAP rule for user namespaces

This commit adds kselftests verifying that a uid 0 task without CAP_SETFCAP
cannot regain the ability to set root file capabilities via setns() or
ptrace into a user namespace, while preserving valid namespace mappings,
idmapped mounts, and file capability operations.

> diff --git a/tools/testing/selftests/namespaces/Makefile b/tools/testing/selftests/namespaces/Makefile
> index 9b22d12708e1a..0455b6544869d 100644
> --- a/tools/testing/selftests/namespaces/Makefile
> +++ b/tools/testing/selftests/namespaces/Makefile
> @@ -14,7 +14,8 @@ TEST_GEN_PROGS := nsid_test \
>  		  stress_test \
>  		  listns_pagination_bug \
>  		  regression_pidfd_setns_test \
> -		  uid_map_range_test
> +		  uid_map_range_test \
> +		  setfcap_test
>  
>  include ../lib.mk
>  

[Severity: Low]
Should setfcap_test also be added to
tools/testing/selftests/namespaces/.gitignore so in-tree builds do not leave
an untracked binary?

> diff --git a/tools/testing/selftests/namespaces/setfcap_test.c b/tools/testing/selftests/namespaces/setfcap_test.c
> new file mode 100644
> index 0000000000000..aee0b1d6f329a
> --- /dev/null
> +++ b/tools/testing/selftests/namespaces/setfcap_test.c

[ ... ]

> +/* /tmp idmapped with the user namespace of pid, at /mnt */
> +static int idmap_tmp(int pid)
> +{
> +	struct mount_attr attr = { .attr_set = MOUNT_ATTR_IDMAP };
> +	char path[64];
> +	int tfd, nsfd;
> +
> +	mkdir("/mnt", 0755);
> +	snprintf(path, sizeof(path), "/proc/%d/ns/user", pid);
> +	nsfd = open(path, O_RDONLY);
> +	tfd = syscall(__NR_open_tree, AT_FDCWD, "/tmp",
> +		      OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
> +	if (nsfd < 0 || tfd < 0)
> +		return -1;
> +	attr.userns_fd = nsfd;
> +	if (syscall(__NR_mount_setattr, tfd, "", AT_EMPTY_PATH, &attr,
> +		    sizeof(attr)))
> +		return -1;
> +	if (syscall(__NR_move_mount, tfd, "", AT_FDCWD, "/mnt",
> +		    MOVE_MOUNT_F_EMPTY_PATH))
> +		return -1;
> +	close(tfd);
> +	close(nsfd);
> +	return 0;
> +}
> +
> +/* Each test: private mounts, a tmpfs on /tmp with copies of this program */
> +static void prepare(struct __test_metadata *_metadata)
> +{
> +	ASSERT_EQ(unshare(CLONE_NEWNS), 0);
> +	ASSERT_EQ(mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL), 0);
> +	ASSERT_EQ(mount("tmpfs", "/tmp", "tmpfs", 0, "mode=1777"), 0);
> +	ASSERT_EQ(new_file("/tmp/x", 0), 0);
> +	ASSERT_EQ(new_file("/tmp/u", 1000), 0);
> +	mkdir("/mnt", 0755);
> +}

[ ... ]

> +/* returns 1 to skip: no idmapped mounts on tmpfs */
> +static int idmap_case(const struct idmap_case *c, long *r)
> +{
> +	struct holder ns, im = { -1, -1 };
> +	struct idmap_join j;
> +	int ret = -1;
> +
> +	ns = spawn_mapped(HOLDER_ROOT, c->ns_map);
> +	if (ns.pid <= 0)
> +		return -1;
> +	if (c->idmap_map) {
> +		im = spawn_mapped(HOLDER_ROOT, c->idmap_map);
> +		if (im.pid <= 0)
> +			goto out;
> +	}
> +	if (new_file("/tmp/x", c->file_uid))
> +		goto out;
> +	if (idmap_tmp(c->idmap_map ? im.pid : ns.pid)) {
> +		ret = 1;
> +		goto out;
> +	}

[Severity: Medium]
Should prepare() and idmap_tmp() place the mountpoint inside the private
/tmp tmpfs instead of at /mnt, and should idmap_tmp() distinguish
mount_setattr() failures from other setup errors?

prepare() only mounts a tmpfs on /tmp, so mkdir("/mnt", 0755) creates /mnt
on the host root filesystem, or fails on a read-only root filesystem where
/mnt does not exist.  In addition, idmap_case() treats any error from
idmap_tmp() as ret = 1, causing TEST_F(idmap, fcap) to report a skip ("no
idmapped mounts on tmpfs") when open(), open_tree(), or move_mount() fails.

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

      reply	other threads:[~2026-11-10 23:52 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
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 [this message]

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-207958@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox