From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Andrea Parri" <parri.andrea@gmail.com>, <kpsingh@kernel.org>,
<matt@bobrowski.net>, <daniel@iogearbox.net>, <andrii@kernel.org>,
<eddyz87@gmail.com>, <memxor@gmail.com>
Cc: <martin.lau@linux.dev>, <song@kernel.org>,
<yonghong.song@linux.dev>, <jolsa@kernel.org>,
<emil@etsalapatis.com>, <ihor.solodrai@linux.dev>,
<sdf@fomichev.me>, <bpf@vger.kernel.org>,
<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH bpf] bpf, lsm: Fix cgroup shim selection for socket hooks
Date: Tue, 03 Nov 2026 02:29:16 +0000 [thread overview]
Message-ID: <DML7ZWOLYTYS.2BQ9L223JO46Z@gmail.com> (raw)
In-Reply-To: <20261102152913.366237-1-parri.andrea@gmail.com>
On Mon, Nov 02, 2026 at 07:29 AM Andrea Parri <parri.andrea@gmail.com> wrote:
> Fixes: 69fd337a975c ("bpf: per-cgroup lsm flavor")
> Cc: stable@vger.kernel.org
Drop it and target bpf-next.
> + sk = sock ? sock->sk : NULL;
> + /* NULL from ip_cmsg_recv_security(), no sk yet in sock_create_lite() */
> + if (unlikely(!sk))
> + return __cgroup_bpf_run_lsm_current(ctx, insn);
With this hunk one hook uses the cgroup of the socket or the current
cgroup. It depends on the caller.
bpf_lsm_current_hooks is for such hooks. Add the two hooks there.
In __sock_create() the current task allocates the sk. So
socket_post_create gets the same cgroup both ways.
Then there is no need to touch cgroup.c.
Something like this (compile tested only):
diff --git a/kernel/bpf/bpf_lsm.c b/kernel/bpf/bpf_lsm.c
index f58dce8..485e8fe 100644
--- a/kernel/bpf/bpf_lsm.c
+++ b/kernel/bpf/bpf_lsm.c
@@ -66,6 +66,10 @@ BTF_SET_START(bpf_lsm_current_hooks)
#ifdef CONFIG_SECURITY_NETWORK
BTF_ID(func, bpf_lsm_sk_alloc_security)
BTF_ID(func, bpf_lsm_sk_free_security)
+/* sock->sk is NULL in sock_create_lite() */
+BTF_ID(func, bpf_lsm_socket_post_create)
+/* sock is NULL in ip_cmsg_recv_security() */
+BTF_ID(func, bpf_lsm_socket_getpeersec_dgram)
#endif
BTF_SET_END(bpf_lsm_current_hooks)
@@ -95,6 +99,7 @@ void bpf_lsm_find_cgroup_shim(const struct bpf_prog *prog,
bpf_func_t *bpf_func)
{
const struct btf_param *args __maybe_unused;
+ u32 id __maybe_unused = 0;
if (btf_type_vlen(prog->aux->attach_func_proto) < 1 ||
btf_id_set_contains(&bpf_lsm_current_hooks,
@@ -105,10 +110,12 @@ void bpf_lsm_find_cgroup_shim(const struct bpf_prog *prog,
#ifdef CONFIG_NET
args = btf_params(prog->aux->attach_func_proto);
+ /* args[0].type is the id of the pointer. btf_sock_ids[] has structs. */
+ btf_type_resolve_ptr(prog->aux->attach_btf, args[0].type, &id);
- if (args[0].type == btf_sock_ids[BTF_SOCK_TYPE_SOCKET])
+ if (id == btf_sock_ids[BTF_SOCK_TYPE_SOCKET])
*bpf_func = __cgroup_bpf_run_lsm_socket;
- else if (args[0].type == btf_sock_ids[BTF_SOCK_TYPE_SOCK])
+ else if (id == btf_sock_ids[BTF_SOCK_TYPE_SOCK])
*bpf_func = __cgroup_bpf_run_lsm_sock;
else
#endif
pw-bot: cr
prev parent reply other threads:[~2026-11-03 2:29 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-02 15:29 [PATCH bpf] bpf, lsm: Fix cgroup shim selection for socket hooks Andrea Parri
2026-11-03 2:29 ` Alexei Starovoitov [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=DML7ZWOLYTYS.2BQ9L223JO46Z@gmail.com \
--to=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=ihor.solodrai@linux.dev \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=matt@bobrowski.net \
--cc=memxor@gmail.com \
--cc=parri.andrea@gmail.com \
--cc=sdf@fomichev.me \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/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.