tools/testing/selftests/bpf/prog_tests/test_lsm.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/test_lsm.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/prog_tests/test_lsm.c- Extension
.c- Size
- 2889 bytes
- Lines
- 139
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
test_progs.hsys/wait.hunistd.hlsm.skel.hlsm_tailcall.skel.h
Detected Declarations
function exec_cmdfunction test_lsmfunction test_lsm_basicfunction test_lsm_tailcallfunction test_test_lsm
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2020 Google LLC.
*/
#include <test_progs.h>
#include <sys/wait.h>
#include <unistd.h>
#include "lsm.skel.h"
#include "lsm_tailcall.skel.h"
char *CMD_ARGS[] = {"true", NULL};
int exec_cmd(int *monitored_pid)
{
int child_pid, child_status;
child_pid = fork();
if (child_pid == 0) {
*monitored_pid = getpid();
execvp(CMD_ARGS[0], CMD_ARGS);
return -EINVAL;
} else if (child_pid > 0) {
waitpid(child_pid, &child_status, 0);
return child_status;
}
return -EINVAL;
}
static int test_lsm(struct lsm *skel)
{
struct bpf_link *link;
int buf = 1234;
int err;
err = lsm__attach(skel);
if (!ASSERT_OK(err, "attach"))
return err;
/* Check that already linked program can't be attached again. */
link = bpf_program__attach(skel->progs.test_int_hook);
if (!ASSERT_ERR_PTR(link, "attach_link"))
return -1;
err = exec_cmd(&skel->bss->monitored_pid);
if (!ASSERT_OK(err, "exec_cmd"))
return err;
ASSERT_EQ(skel->bss->bprm_count, 1, "bprm_count");
skel->bss->monitored_pid = getpid();
err = stack_mprotect();
if (!ASSERT_EQ(err, -1, "stack_mprotect") ||
!ASSERT_EQ(errno, EPERM, "stack_mprotect"))
return err;
ASSERT_EQ(skel->bss->mprotect_count, 1, "mprotect_count");
syscall(__NR_setdomainname, &buf, -2L);
syscall(__NR_setdomainname, 0, -3L);
syscall(__NR_setdomainname, ~0L, -4L);
ASSERT_EQ(skel->bss->copy_test, 3, "copy_test");
lsm__detach(skel);
skel->bss->copy_test = 0;
skel->bss->bprm_count = 0;
skel->bss->mprotect_count = 0;
return 0;
}
static void test_lsm_basic(void)
{
struct lsm *skel = NULL;
int err;
skel = lsm__open_and_load();
if (!ASSERT_OK_PTR(skel, "lsm_skel_load"))
goto close_prog;
err = test_lsm(skel);
if (!ASSERT_OK(err, "test_lsm_first_attach"))
goto close_prog;
err = test_lsm(skel);
Annotation
- Immediate include surface: `test_progs.h`, `sys/wait.h`, `unistd.h`, `lsm.skel.h`, `lsm_tailcall.skel.h`.
- Detected declarations: `function exec_cmd`, `function test_lsm`, `function test_lsm_basic`, `function test_lsm_tailcall`, `function test_test_lsm`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.