Commit 4991277d authored by Xu Kuohai's avatar Xu Kuohai Committed by Pu Lehui
Browse files

selftests/bpf: Add test for lsm tail call

mainline inclusion
from mainline-v6.12-rc1
commit d463dd9c9aa24b17ccb8ed76bdd7768baf857b48
category: bugfix
bugzilla: https://gitee.com/src-openeuler/kernel/issues/IAYRIC
CVE: CVE-2024-50063

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=d463dd9c9aa2



--------------------------------

Add test for lsm tail call to ensure tail call can only be used between
bpf lsm progs attached to the same hook.

Signed-off-by: default avatarXu Kuohai <xukuohai@huawei.com>
Link: https://lore.kernel.org/r/20240719110059.797546-9-xukuohai@huaweicloud.com


Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Conflicts:
	tools/testing/selftests/bpf/prog_tests/test_lsm.c
[Adapt for some minor issue]
Signed-off-by: default avatarPu Lehui <pulehui@huawei.com>
parent b05a820d
Loading
Loading
Loading
Loading
+45 −1
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@
#include <unistd.h>

#include "lsm.skel.h"
#include "lsm_tailcall.skel.h"

char *CMD_ARGS[] = {"true", NULL};

@@ -52,7 +53,7 @@ int exec_cmd(int *monitored_pid)
	return -EINVAL;
}

void test_test_lsm(void)
static void test_lsm_basic(void)
{
	struct lsm *skel = NULL;
	int err, duration = 0;
@@ -93,3 +94,46 @@ void test_test_lsm(void)
close_prog:
	lsm__destroy(skel);
}

static void test_lsm_tailcall(void)
{
	struct lsm_tailcall *skel = NULL;
	int map_fd, prog_fd;
	int err, key;

	skel = lsm_tailcall__open_and_load();
	if (!ASSERT_OK_PTR(skel, "lsm_tailcall__skel_load"))
		goto close_prog;

	map_fd = bpf_map__fd(skel->maps.jmp_table);
	if (CHECK_FAIL(map_fd < 0))
		goto close_prog;

	prog_fd = bpf_program__fd(skel->progs.lsm_file_permission_prog);
	if (CHECK_FAIL(prog_fd < 0))
		goto close_prog;

	key = 0;
	err = bpf_map_update_elem(map_fd, &key, &prog_fd, BPF_ANY);
	if (CHECK_FAIL(!err))
		goto close_prog;

	prog_fd = bpf_program__fd(skel->progs.lsm_file_alloc_security_prog);
	if (CHECK_FAIL(prog_fd < 0))
		goto close_prog;

	err = bpf_map_update_elem(map_fd, &key, &prog_fd, BPF_ANY);
	if (CHECK_FAIL(err))
		goto close_prog;

close_prog:
	lsm_tailcall__destroy(skel);
}

void test_test_lsm(void)
{
	if (test__start_subtest("lsm_basic"))
		test_lsm_basic();
	if (test__start_subtest("lsm_tailcall"))
		test_lsm_tailcall();
}
+34 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2024 Huawei Technologies Co., Ltd */

#include "vmlinux.h"
#include <errno.h>
#include <bpf/bpf_helpers.h>

char _license[] SEC("license") = "GPL";

struct {
	__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
	__uint(max_entries, 1);
	__uint(key_size, sizeof(__u32));
	__uint(value_size, sizeof(__u32));
} jmp_table SEC(".maps");

SEC("lsm/file_permission")
int lsm_file_permission_prog(void *ctx)
{
	return 0;
}

SEC("lsm/file_alloc_security")
int lsm_file_alloc_security_prog(void *ctx)
{
	return 0;
}

SEC("lsm/file_alloc_security")
int lsm_file_alloc_security_entry(void *ctx)
{
	bpf_tail_call_static(ctx, &jmp_table, 0);
	return 0;
}