tools/testing/selftests/bpf/progs/lsm.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/lsm.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/progs/lsm.c
Extension
.c
Size
4051 bytes
Lines
184
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.

Dependency Surface

Detected Declarations

Annotated Snippet

struct inner_map {
	__uint(type, BPF_MAP_TYPE_ARRAY);
	__uint(max_entries, 1);
	__type(key, int);
	__type(value, __u64);
} inner_map SEC(".maps");

struct outer_arr {
	__uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
	__uint(max_entries, 1);
	__uint(key_size, sizeof(int));
	__uint(value_size, sizeof(int));
	__array(values, struct inner_map);
} outer_arr SEC(".maps") = {
	.values = { [0] = &inner_map },
};

struct outer_hash {
	__uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
	__uint(max_entries, 1);
	__uint(key_size, sizeof(int));
	__array(values, struct inner_map);
} outer_hash SEC(".maps") = {
	.values = { [0] = &inner_map },
};

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

int monitored_pid = 0;
int mprotect_count = 0;
int bprm_count = 0;

SEC("lsm/file_mprotect")
int BPF_PROG(test_int_hook, struct vm_area_struct *vma,
	     unsigned long reqprot, unsigned long prot, int ret)
{
	struct mm_struct *mm = vma->vm_mm;

	if (ret != 0 || !mm)
		return ret;

	__s32 pid = bpf_get_current_pid_tgid() >> 32;
	int is_stack = 0;

	is_stack = (vma->vm_start <= mm->start_stack &&
		    vma->vm_end >= mm->start_stack);

	if (is_stack && monitored_pid == pid) {
		mprotect_count++;
		ret = -EPERM;
	}

	return ret;
}

SEC("lsm.s/bprm_committed_creds")
int BPF_PROG(test_void_hook, struct linux_binprm *bprm)
{
	__u32 pid = bpf_get_current_pid_tgid() >> 32;
	struct inner_map *inner_map;
	char args[64];
	__u32 key = 0;
	__u64 *value;

	if (monitored_pid == pid)
		bprm_count++;

	bpf_copy_from_user(args, sizeof(args), (void *)bprm->vma->vm_mm->arg_start);
	bpf_copy_from_user(args, sizeof(args), (void *)bprm->mm->arg_start);

	value = bpf_map_lookup_elem(&array, &key);
	if (value)
		*value = 0;
	value = bpf_map_lookup_elem(&hash, &key);
	if (value)
		*value = 0;
	value = bpf_map_lookup_elem(&lru_hash, &key);
	if (value)
		*value = 0;
	value = bpf_map_lookup_elem(&percpu_array, &key);
	if (value)
		*value = 0;
	value = bpf_map_lookup_elem(&percpu_hash, &key);
	if (value)
		*value = 0;
	value = bpf_map_lookup_elem(&lru_percpu_hash, &key);
	if (value)
		*value = 0;
	inner_map = bpf_map_lookup_elem(&outer_arr, &key);
	if (inner_map) {

Annotation

Implementation Notes