From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ed1-f51.google.com (mail-ed1-f51.google.com [209.85.208.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BD23E39E165 for ; Tue, 24 Feb 2026 13:35:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771940120; cv=none; b=D3fSSq/SPZ0edcUcP2kAqCOrMl8t2wPUgZwETt5Oq3uQak4aKtZCBjxBitGktOHb46gsqJRxLi/MPtI4sAtZkrtnElbB/jlHpdrMBokrg1eUOHuz0aV9ADjiJ7/C7LuGgceUW3SN9DPkmqjEsT62P482g8/mQ7cPLLqK6nm0kmU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771940120; c=relaxed/simple; bh=qhbJpmZITKjbEAcPdtlGm6W8/Qffyxn/CjLw4UREa1I=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=TlZMM67aDf9vVkF212ejfqyPbGu+42Yuiq5lKly/sir2oDmFQIe8+tCf2FtdjoRZuogR6ztokZLVCgK0paKc8QpPTC60XzgJT+Ib6OoylNm7v74kc9gEvpcKBa8GvjeIzwOiI0aVfQplwz8KW4YeEo1CAkeunRrcX0yjVn8r6Xk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=TTmOl1Wg; arc=none smtp.client-ip=209.85.208.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="TTmOl1Wg" Received: by mail-ed1-f51.google.com with SMTP id 4fb4d7f45d1cf-65f771c6b89so11914a12.3 for ; Tue, 24 Feb 2026 05:35:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771940116; x=1772544916; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=017Au0WagCuxotQHTbuPbnn7OOhD26VlW9T9YwI+bmw=; b=TTmOl1WgO9ipDMNDDj3wD89bpFoluL+wCTIJLb4MqoavlpDQ7xDtodnvIGGDqUtyoV 4RuHCPw2s1t1N3q7xh+MDGSRsMf2rHME6KoikPe0TAyvLoQ2PpIpze0jjE2xrJ1zLSxg lSiEZ0xAr3QCnDNChTXHZBsW60fDZ+7HIvIN19yVqKesbgtur9ChCDTEh/FEi/Wunz1D eSRz7a9cqDMe9Z2lGY1nPkhl0rL0NUbLZXbw3DCCismTWLKcz4H0yL6aFBSVNFViPnFZ dU5OygsDP70fjKg+dUuf9sLk/Cfc7KSLLOTxK/nT29jkbd8Yg0GgNshE+3zb3RZMh5Vs 2RUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771940116; x=1772544916; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=017Au0WagCuxotQHTbuPbnn7OOhD26VlW9T9YwI+bmw=; b=pyKC7CxK7Hue6/pw5vKtUGEeREzj4nSJKCXsDUr4BdVZHhWsBm+0Gmc5LJzageSg0R ljKdoTomWHhpjW4LQ4ZS4FfWKa5ejDY16Y0gpaFQvmcZU1WcsVqG8BPEJLwOHkuOOESw vKaWfzr1r4E7b1z8hhh6N9/KG9MUKgT/pTn5mmJC2W2NH76ylzZbUuyT8ypnyiV0pgRU +wJxKf6jZ/+0N7MwQPf2P0THEFsp3W3mOhtrY9MXWUrNBvpzQuxK2jK2NGc0Qz20D4NB eL4KHg5Ke2KIXQaqm6s/L8yHzl35irOLNlttpz4jhWqCFuKSB0y+OdW8D/cy/QbKeUgy kx3A== X-Forwarded-Encrypted: i=1; AJvYcCXMvahNekTyYEqwQAvfWSszPptcC2+Uh1AFKD+dygfttDl+1Eldzki5HYqNNIMDuCDokcYawKgIet5s6p4=@vger.kernel.org X-Gm-Message-State: AOJu0Ywuq9cQv61wx2aQ1KHNwRaDCQFMBlWn32vh2brHVWGqmgCySJsq 6V9WPA7Jrmjv1ajgAh5k19l4h3NAeXxJfOVWevSAcFCPF8D2703yvmTLTGWxciMNBA== X-Gm-Gg: AZuq6aIr612lwqXdS1FeEsbmcQhnu8xRj/6TJG91WjF7LHZupTg6zKbApHRo6zQMM4j a70cpnOCpeobKi1I5yeBXw3sysSyHolT4ybLxJ5luDoGUAUByLrnixL6pB/9wWHErMwz5kzo8tL xRJcOz+mYYLKM/T1Lf1dMTaml7wD9ClbuRd2Y/F9mqL3W9LxjZ6/jcBe9W9yem7hI76NFFPRXKA 7WpnlXKi2V/A/2oaimvVrE1RCDrPQDdfLK5ybaOF3Rwm/qdHKmCNASz6op5VtDrs72n5U+k3RNt rIwBLwAUjDxX4pTTjxPK3WXzO5ZYwbIvBOtsdA+BKO3nQ3wI24tSg0oSH5yVXfMwRQKStF4UQdh o9Yyk/TcU4IufPTGDxItWvO0WuoEl9+4+r4QXFU6JMxDrUI7DHvfoBvr4JTFdHKnQs69+L6nKwD BmL2OO2QAR3DZKpRcEf69fbUlQmUqm52Y332Ocrd/UGt+EGKUm5ZvNRCODd5mGais= X-Received: by 2002:a17:906:eec7:b0:b87:117f:b6f0 with SMTP id a640c23a62f3a-b9081b23d01mr691501366b.30.1771940115631; Tue, 24 Feb 2026 05:35:15 -0800 (PST) Received: from google.com (93.50.90.34.bc.googleusercontent.com. [34.90.50.93]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b9084a4d6e1sm426835366b.0.2026.02.24.05.35.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Feb 2026 05:35:15 -0800 (PST) Date: Tue, 24 Feb 2026 13:35:11 +0000 From: Matt Bobrowski To: Christian Brauner Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Tejun Heo , KP Singh , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, Lennart Poettering Subject: Re: [PATCH 1/4] ns: add bpf hooks Message-ID: References: <20260220-work-bpf-namespace-v1-0-866207db7b83@kernel.org> <20260220-work-bpf-namespace-v1-1-866207db7b83@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260220-work-bpf-namespace-v1-1-866207db7b83@kernel.org> On Fri, Feb 20, 2026 at 01:38:29AM +0100, Christian Brauner wrote: > Add the three namespace lifecycle hooks and make them available to bpf > lsm program types. This allows bpf to supervise namespace creation. I'm > in the process of adding various "universal truth" bpf programs to > systemd that will make use of this. This e.g., allows to lock in a > program into a given set of namespaces. > > Signed-off-by: Christian Brauner > --- > include/linux/bpf_lsm.h | 21 +++++++++++++++++++++ > kernel/bpf/bpf_lsm.c | 25 +++++++++++++++++++++++++ > kernel/nscommon.c | 9 ++++++++- > kernel/nsproxy.c | 7 +++++++ > 4 files changed, 61 insertions(+), 1 deletion(-) > > diff --git a/include/linux/bpf_lsm.h b/include/linux/bpf_lsm.h > index 643809cc78c3..5ae438fdf567 100644 > --- a/include/linux/bpf_lsm.h > +++ b/include/linux/bpf_lsm.h > @@ -12,6 +12,9 @@ > #include > #include > > +struct ns_common; > +struct nsset; > + > #ifdef CONFIG_BPF_LSM > > #define LSM_HOOK(RET, DEFAULT, NAME, ...) \ > @@ -48,6 +51,11 @@ void bpf_lsm_find_cgroup_shim(const struct bpf_prog *prog, bpf_func_t *bpf_func) > > int bpf_lsm_get_retval_range(const struct bpf_prog *prog, > struct bpf_retval_range *range); > + > +int bpf_lsm_namespace_alloc(struct ns_common *ns); > +void bpf_lsm_namespace_free(struct ns_common *ns); > +int bpf_lsm_namespace_install(struct nsset *nsset, struct ns_common *ns); > + > int bpf_set_dentry_xattr_locked(struct dentry *dentry, const char *name__str, > const struct bpf_dynptr *value_p, int flags); > int bpf_remove_dentry_xattr_locked(struct dentry *dentry, const char *name__str); > @@ -104,6 +112,19 @@ static inline bool bpf_lsm_has_d_inode_locked(const struct bpf_prog *prog) > { > return false; > } > + > +static inline int bpf_lsm_namespace_alloc(struct ns_common *ns) > +{ > + return 0; > +} > +static inline void bpf_lsm_namespace_free(struct ns_common *ns) > +{ > +} > +static inline int bpf_lsm_namespace_install(struct nsset *nsset, > + struct ns_common *ns) > +{ > + return 0; > +} > #endif /* CONFIG_BPF_LSM */ > > #endif /* _LINUX_BPF_LSM_H */ > diff --git a/kernel/bpf/bpf_lsm.c b/kernel/bpf/bpf_lsm.c > index 0c4a0c8e6f70..f6378db46220 100644 > --- a/kernel/bpf/bpf_lsm.c > +++ b/kernel/bpf/bpf_lsm.c > @@ -30,10 +30,32 @@ __weak noinline RET bpf_lsm_##NAME(__VA_ARGS__) \ > #include > #undef LSM_HOOK > > +__bpf_hook_start(); > + > +__weak noinline int bpf_lsm_namespace_alloc(struct ns_common *ns) > +{ > + return 0; > +} > + > +__weak noinline void bpf_lsm_namespace_free(struct ns_common *ns) > +{ > +} I'm wondering how you foresee this hook functioning in a scenario where the BPF LSM program is attached to this new hook point, although with its attachment type being set to BPF_LSM_CGROUP instead of BPF_LSM_MAC? You probably wouldn't want to utilize something like BPF_LSM_CGROUP for your specific use case, but as things stand currently I don't believe there's anyhthing preventing you from using BPF_LSM_CGROUP with a hook like bpf_lsm_namespace_free(). Notably, the BPF_LSM_CGROUP infrastructure is designed to execute BPF programs based on the cgroup of the currently executing task. There could be some surprises if the bpf_lsm_namespace_free() hook were to ever be called from a context (e.g, kworker) other than the one specified whilst attaching the BPF LSM program with type BPF_LSM_CGROUP. > +__weak noinline int bpf_lsm_namespace_install(struct nsset *nsset, > + struct ns_common *ns) > +{ > + return 0; > +} > + > +__bpf_hook_end(); > + > #define LSM_HOOK(RET, DEFAULT, NAME, ...) BTF_ID(func, bpf_lsm_##NAME) > BTF_SET_START(bpf_lsm_hooks) > #include > #undef LSM_HOOK > +BTF_ID(func, bpf_lsm_namespace_alloc) > +BTF_ID(func, bpf_lsm_namespace_free) > +BTF_ID(func, bpf_lsm_namespace_install) > BTF_SET_END(bpf_lsm_hooks) > > BTF_SET_START(bpf_lsm_disabled_hooks) > @@ -383,6 +405,8 @@ BTF_ID(func, bpf_lsm_task_prctl) > BTF_ID(func, bpf_lsm_task_setscheduler) > BTF_ID(func, bpf_lsm_task_to_inode) > BTF_ID(func, bpf_lsm_userns_create) > +BTF_ID(func, bpf_lsm_namespace_alloc) > +BTF_ID(func, bpf_lsm_namespace_install) > BTF_SET_END(sleepable_lsm_hooks) > > BTF_SET_START(untrusted_lsm_hooks) > @@ -395,6 +419,7 @@ BTF_ID(func, bpf_lsm_sk_alloc_security) > BTF_ID(func, bpf_lsm_sk_free_security) > #endif /* CONFIG_SECURITY_NETWORK */ > BTF_ID(func, bpf_lsm_task_free) > +BTF_ID(func, bpf_lsm_namespace_free) > BTF_SET_END(untrusted_lsm_hooks) > > bool bpf_lsm_is_sleepable_hook(u32 btf_id) > diff --git a/kernel/nscommon.c b/kernel/nscommon.c > index bdc3c86231d3..c3613cab3d41 100644 > --- a/kernel/nscommon.c > +++ b/kernel/nscommon.c > @@ -1,6 +1,7 @@ > // SPDX-License-Identifier: GPL-2.0-only > /* Copyright (c) 2025 Christian Brauner */ > > +#include > #include > #include > #include > @@ -77,6 +78,7 @@ int __ns_common_init(struct ns_common *ns, u32 ns_type, const struct proc_ns_ope > ret = proc_alloc_inum(&ns->inum); > if (ret) > return ret; > + > /* > * Tree ref starts at 0. It's incremented when namespace enters > * active use (installed in nsproxy) and decremented when all > @@ -86,11 +88,16 @@ int __ns_common_init(struct ns_common *ns, u32 ns_type, const struct proc_ns_ope > atomic_set(&ns->__ns_ref_active, 1); > else > atomic_set(&ns->__ns_ref_active, 0); > - return 0; > + > + ret = bpf_lsm_namespace_alloc(ns); > + if (ret && !inum) > + proc_free_inum(ns->inum); > + return ret; > } > > void __ns_common_free(struct ns_common *ns) > { > + bpf_lsm_namespace_free(ns); > proc_free_inum(ns->inum); > } > > diff --git a/kernel/nsproxy.c b/kernel/nsproxy.c > index 259c4b4f1eeb..5742f9664dbb 100644 > --- a/kernel/nsproxy.c > +++ b/kernel/nsproxy.c > @@ -9,6 +9,7 @@ > * Pavel Emelianov > */ > > +#include > #include > #include > #include > @@ -379,6 +380,12 @@ static int prepare_nsset(unsigned flags, struct nsset *nsset) > > static inline int validate_ns(struct nsset *nsset, struct ns_common *ns) > { > + int ret; > + > + ret = bpf_lsm_namespace_install(nsset, ns); > + if (ret) > + return ret; > + > return ns->ops->install(nsset, ns); > } > > > -- > 2.47.3 >