History

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.

Mahe Tardy committed 3 months ago

Commit 7b0dfbf1 parent c7838e32 files · +41 −0Browse files

Unified · Split

2 files
  1. tools/testing/selftests/bpf/prog_tests/ksock.c +6 −0
  2. tools/testing/selftests/bpf/progs/ksock_lsm_verifier.c +35 −0
tools/testing/selftests/bpf/prog_tests/ksock.c+6−0
@@ -6,6 +6,7 @@ · 5 unmodified lines
6#include "test_progs.h"6#include "test_progs.h"
7#include "network_helpers.h"7#include "network_helpers.h"
8#include "ksock_lsm.skel.h"8#include "ksock_lsm.skel.h"
9#include "ksock_lsm_verifier.skel.h"
9 10
10#define NS_TEST "ksock_lsm_ns"11#define NS_TEST "ksock_lsm_ns"
11#define RECV_PORT 777712#define RECV_PORT 7777
@@ -122,3 +123,8 @@ fail: · 110 unmodified lines
122 remove_netns(NS_TEST);123 remove_netns(NS_TEST);
123 ksock_lsm__destroy(skel);124 ksock_lsm__destroy(skel);
124}125}
126
127void test_ksock_lsm_verifier(void)
128{
129 RUN_TESTS(ksock_lsm_verifier);
130}
tools/testing/selftests/bpf/progs/ksock_lsm_verifier.c+35−0
@@ -0,0 +1,35 @@
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2026 Isovalent */
3
4#include "vmlinux.h"
5#include <bpf/bpf_helpers.h>
6#include <bpf/bpf_tracing.h>
7#include "bpf_misc.h"
8#include "ksock_common.h"
9
10char send_data[11] = "dummy data";
11
12SEC("lsm.s/socket_sendmsg")
13__description("bpf_ksock_send is rejected from socket_sendmsg LSM hook")
14__failure __msg("calling kernel function bpf_ksock_send is not allowed")
15int BPF_PROG(ksock_socket_sendmsg, struct socket *sock, struct msghdr *msg,
16 int size, int ret)
17{
18 struct __ksock_ctx_value *v;
19 struct bpf_ksock *ks;
20
21 v = ksock_ctx_value_lookup();
22 if (!v)
23 return ret;
24
25 ks = bpf_kptr_xchg(&v->ctx, NULL);
26 if (!ks)
27 return ret;
28
29 bpf_ksock_send(ks, send_data, sizeof(send_data));
30 bpf_ksock_release(ks);
31
32 return ret;
33}
34
35char __license[] SEC("license") = "GPL";