selftests/bpf: Test forbidden bpf_ksock_send() LSM attach

The bpf_ksock_send() kfunc eventually calls security_socket_sendmsg(),
thus creating a possible recursion if a program calling the kfunc is
attached on that specific hook. A filter is added on the kfunc
registration to prevent that at load time from the verifier. This test
exercises that the verifier will reject such program on that attach
point.

Signed-off-by: Mahe Tardy <mahe.tardy@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Stanislav Fomichev <sdf@fomichev.me>
Acked-by: Song Liu <song@kernel.org>
Link: https://lore.kernel.org/bpf/20260813110540.103550-5-mahe.tardy@gmail.com
This commit is contained in:
Mahe Tardy
2026-08-13 11:05:39 +00:00
committed by Daniel Borkmann
parent c7838e3dc6
commit 7b0dfbf577
2 changed files with 41 additions and 0 deletions

View File

@@ -6,6 +6,7 @@
#include "test_progs.h"
#include "network_helpers.h"
#include "ksock_lsm.skel.h"
#include "ksock_lsm_verifier.skel.h"
#define NS_TEST "ksock_lsm_ns"
#define RECV_PORT 7777
@@ -122,3 +123,8 @@ void test_ksock_lsm(void)
remove_netns(NS_TEST);
ksock_lsm__destroy(skel);
}
void test_ksock_lsm_verifier(void)
{
RUN_TESTS(ksock_lsm_verifier);
}

View File

@@ -0,0 +1,35 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2026 Isovalent */
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include "bpf_misc.h"
#include "ksock_common.h"
char send_data[11] = "dummy data";
SEC("lsm.s/socket_sendmsg")
__description("bpf_ksock_send is rejected from socket_sendmsg LSM hook")
__failure __msg("calling kernel function bpf_ksock_send is not allowed")
int BPF_PROG(ksock_socket_sendmsg, struct socket *sock, struct msghdr *msg,
int size, int ret)
{
struct __ksock_ctx_value *v;
struct bpf_ksock *ks;
v = ksock_ctx_value_lookup();
if (!v)
return ret;
ks = bpf_kptr_xchg(&v->ctx, NULL);
if (!ks)
return ret;
bpf_ksock_send(ks, send_data, sizeof(send_data));
bpf_ksock_release(ks);
return ret;
}
char __license[] SEC("license") = "GPL";