Linux Security Modules development
 help / color / mirror / Atom feed
From: Christian Brauner <brauner@kernel.org>
To: linux-fsdevel@vger.kernel.org
Cc: linux-security-module@vger.kernel.org,
	 Alexander Viro <viro@zeniv.linux.org.uk>,
	Jan Kara <jack@suse.cz>,
	 bpf@vger.kernel.org, Zhiqi Zhang <zhiqizhangg@gmail.com>,
	 "Christian Brauner (Amutable)" <brauner@kernel.org>
Subject: [PATCH 0/2] ns: don't call the namespace LSM hooks for kernel-internal namespaces
Date: Fri, 20 Nov 2026 13:29:57 +0100	[thread overview]
Message-ID: <20261120-work-ns-mount_subtree-v1-0-06337377f94a@kernel.org> (raw)

mount_subtree() allocates a private anonymous mount namespace so that
automounts during the path lookup have somewhere to go. btrfs goes
through it on every mount and NFS on every NFSv4 mount and whenever it
follows a referral.

Since the namespace LSM hooks went in that namespace goes through
security_namespace_init() like any other. Landlock enforces
LANDLOCK_PERMISSION_NAMESPACE_USE in its namespace_init hook. So a
sandbox that handles that permission without allowing CLONE_NEWNS can't
mount btrfs or NFSv4 anymore. mount(2) fails with EPERM even though
nobody asked for a mount namespace. Zhiqi reported that and sent a fix
that skips namespace_init for this namespace (cf. [1]).

But namespace_free needs to be skipped as well. Otherwise a bpf program
attached to namespace_free would see these namespaces go away without
ever having seen them being created. Right now that can't happen but
only because security_namespace_free() skips the hook if no LSM
requested a namespace blob. So mark such namespaces as kernel-internal
and keep them away from both hooks. The first patch turns the anon
argument of alloc_mnt_ns() into flags so we don't end up with two
booleans.

The anonymous mount namespaces created by open_tree(OPEN_TREE_CLONE)
and fsmount() are handed to userspace and keep going through the hooks.
Landlock covers them on purpose.

I tested this on v7.4-rc3 with a btrfs loop mount and an NFSv4 mount
in a Landlock domain that handles LANDLOCK_PERMISSION_NAMESPACE_USE but
doesn't allow CLONE_NEWNS. Both fail with EPERM without this series and
work with it. unshare(CLONE_NEWNS), clone(CLONE_NEWNS),
open_tree(OPEN_TREE_CLONE), fsmount() and setns() are still denied and
the Landlock namespace selftests pass.

[1]: https://lore.kernel.org/20261114141323.19029-1-zhiqizhangg@gmail.com

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
Christian Brauner (2):
      mount: pass flags to alloc_mnt_ns()
      ns: don't call the namespace LSM hooks for kernel-internal namespaces

 fs/namespace.c                     | 31 +++++++++++++++++++++----------
 include/linux/ns/ns_common_types.h |  2 ++
 include/linux/ns_common.h          | 20 +++++++++++++-------
 kernel/nscommon.c                  |  9 ++++++---
 4 files changed, 42 insertions(+), 20 deletions(-)
---
base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4
change-id: 20261120-work-ns-mount_subtree-d429ad72f8ac


             reply	other threads:[~2026-11-20 12:30 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 12:29 Christian Brauner [this message]
2026-11-20 12:29 ` [PATCH 1/2] mount: pass flags to alloc_mnt_ns() Christian Brauner
2026-11-20 12:32   ` sashiko-bot
2026-11-20 12:29 ` [PATCH 2/2] ns: don't call the namespace LSM hooks for kernel-internal namespaces Christian Brauner
2026-11-20 12:33   ` 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=20261120-work-ns-mount_subtree-v1-0-06337377f94a@kernel.org \
    --to=brauner@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=zhiqizhangg@gmail.com \
    /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