Linux Security Modules development
 help / color / mirror / Atom feed
From: Paul Moore <paul@paul-moore.com>
To: sashiko-bot@kernel.org
Cc: linux-security-module@vger.kernel.org
Subject: Re: [PATCH v2 1/7] lsm: always call the namespace_free hook
Date: Mon, 23 Nov 2026 18:12:35 -0500	[thread overview]
Message-ID: <20a5e5372530caa583c57003dfcd009d@paul-moore.com> (raw)
In-Reply-To: <20261029-work-security-namespace_free-v2-1-65591dbba7b3@kernel.org>

On Oct 29, 2026 sashiko-bot@kernel.org wrote:
> 
> security_namespace_free() returns early if ns->ns_security is NULL and
> skips the namespace_free hook. But ns->ns_security is only allocated if
> an LSM requests a namespace blob via lbs_ns and currently no LSM does.
> So the namespace_free hook is never called at all.
> 
> Landlock only uses namespace_init and namespace_install so it doesn't
> care. But the bpf LSM attaches to every hook and a program attached to
> namespace_free never runs. We want to use it in systemd-nsresourced to
> get notified when a user namespace goes away. Right now it has to kprobe
> retire_userns_sysctls() for that.
> 
> Note that a bpf program can't work around this. The size of the
> namespace blob is fixed at boot and the bpf LSM doesn't request one.
> And security_namespace_init() allocates the blob before the
> namespace_init hooks are called anyway.
> 
> The check isn't needed. __ns_common_free() is only ever called after
> __ns_common_init() succeeded and security_namespace_init() only calls
> security_namespace_free() after the blob has been allocated. So if an
> LSM requests a namespace blob it is always there when the hook runs.
> Call the hook unconditionally like security_sb_free(),
> security_file_free() and security_bpf_map_free() do.
> 
> The bpf LSM attaches programs to its bpf_lsm_*() stubs so fentry on the
> stubs shows what a bpf LSM program sees. Booted with lsm.debug, which
> prints "LSM: blob(ns) size 0", and created 100 user and uts namespaces:
> 
>   bpftrace -e 'fentry:bpf_lsm_namespace_init { @init = count(); }
>                fentry:bpf_lsm_namespace_free { @free = count(); }' &
>   for i in $(seq 100); do unshare -Uu true; done
> 
>   before: @init: 200 @free: 0
>   after:  @init: 200 @free: 200
> 
> Fixes: f675d2e95569 ("lsm: add LSM blob and hooks for namespaces")
> Cc: stable@kernel.org # mainline only
> Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
> ---
>  security/security.c | 3 ---
>  1 file changed, 3 deletions(-)

I just merged this into the lsm/topic-7.5-02-lsm_ns_free_change branch
with a corrected subject line (namespace_free is the callback to
security_namespace_free()) and the stable tag removed; both changes are
annotated in the metadata section.  I'll be pushing that branch up to
kernel.org shortly.

--
paul-moore.com

  parent reply	other threads:[~2026-11-23 23:12 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-29 16:40 [PATCH v2 0/7] ns: fixes for the namespace LSM hooks Christian Brauner
2026-10-29 16:40 ` [PATCH v2 1/7] lsm: always call the namespace_free hook Christian Brauner
2026-10-29 16:48   ` sashiko-bot
2026-11-23 23:12   ` Paul Moore [this message]
2026-11-23 23:17     ` Paul Moore
2026-10-29 16:40 ` [PATCH v2 2/7] bpf: don't mark namespace_init and namespace_free arguments as trusted Christian Brauner
2026-10-29 16:47   ` sashiko-bot
2026-10-29 16:40 ` [PATCH v2 3/7] selftests/bpf: add verifier tests for the namespace LSM hooks Christian Brauner
2026-10-29 16:46   ` sashiko-bot
2026-10-29 16:40 ` [PATCH v2 4/7] ns: allocate the namespace id before the namespace_init hook Christian Brauner
2026-10-29 16:51   ` sashiko-bot
2026-10-29 22:13   ` Mickaël Salaün
2026-11-23 23:19   ` Paul Moore
2026-10-29 16:41 ` [PATCH v2 5/7] nstree: drop ns_tree_add_raw() Christian Brauner
2026-10-29 16:49   ` sashiko-bot
2026-10-29 16:41 ` [PATCH v2 6/7] lsm: fix the security_namespace_init() kernel-doc Christian Brauner
2026-10-29 16:50   ` sashiko-bot
2026-11-23 23:19   ` Paul Moore
2026-10-29 16:41 ` [PATCH v2 7/7] selftests/bpf: add a lifecycle test for the namespace LSM hooks Christian Brauner
2026-10-29 16:55   ` sashiko-bot
2026-10-29 21:06 ` [PATCH v2 0/7] ns: fixes " Paul Moore

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=20a5e5372530caa583c57003dfcd009d@paul-moore.com \
    --to=paul@paul-moore.com \
    --cc=linux-security-module@vger.kernel.org \
    --cc=sashiko-bot@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