f56407fa6e
Technically the bpf programs can sleep while attached to bpf_lsm_file_mprotect, but such programs need to access user memory. So they're in might_fault() category. Which means they cannot be called from file_mprotect lsm hook that takes write lock on mm->mmap_lock. Adjust the test accordingly. Also add might_fault() to __bpf_prog_enter_sleepable() to catch such deadlocks early. Fixes: 1e6c62a88215 ("bpf: Introduce sleepable BPF programs") Fixes: e68a144547fc ("selftests/bpf: Add sleepable tests") Reported-by: Yonghong Song <yhs@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20200831201651.82447-1-alexei.starovoitov@gmail.com
111 lines
2.3 KiB
C
111 lines
2.3 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
* Copyright 2020 Google LLC.
|
|
*/
|
|
|
|
#include "vmlinux.h"
|
|
#include <bpf/bpf_helpers.h>
|
|
#include <bpf/bpf_tracing.h>
|
|
#include <errno.h>
|
|
|
|
struct {
|
|
__uint(type, BPF_MAP_TYPE_ARRAY);
|
|
__uint(max_entries, 1);
|
|
__type(key, __u32);
|
|
__type(value, __u64);
|
|
} array SEC(".maps");
|
|
|
|
struct {
|
|
__uint(type, BPF_MAP_TYPE_HASH);
|
|
__uint(max_entries, 1);
|
|
__type(key, __u32);
|
|
__type(value, __u64);
|
|
} hash SEC(".maps");
|
|
|
|
struct {
|
|
__uint(type, BPF_MAP_TYPE_LRU_HASH);
|
|
__uint(max_entries, 1);
|
|
__type(key, __u32);
|
|
__type(value, __u64);
|
|
} lru_hash SEC(".maps");
|
|
|
|
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)
|
|
{
|
|
if (ret != 0)
|
|
return ret;
|
|
|
|
__u32 pid = bpf_get_current_pid_tgid() >> 32;
|
|
int is_stack = 0;
|
|
|
|
is_stack = (vma->vm_start <= vma->vm_mm->start_stack &&
|
|
vma->vm_end >= vma->vm_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;
|
|
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;
|
|
|
|
return 0;
|
|
}
|
|
SEC("lsm/task_free") /* lsm/ is ok, lsm.s/ fails */
|
|
int BPF_PROG(test_task_free, struct task_struct *task)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
int copy_test = 0;
|
|
|
|
SEC("fentry.s/__x64_sys_setdomainname")
|
|
int BPF_PROG(test_sys_setdomainname, struct pt_regs *regs)
|
|
{
|
|
void *ptr = (void *)PT_REGS_PARM1(regs);
|
|
int len = PT_REGS_PARM2(regs);
|
|
int buf = 0;
|
|
long ret;
|
|
|
|
ret = bpf_copy_from_user(&buf, sizeof(buf), ptr);
|
|
if (len == -2 && ret == 0 && buf == 1234)
|
|
copy_test++;
|
|
if (len == -3 && ret == -EFAULT)
|
|
copy_test++;
|
|
if (len == -4 && ret == -EFAULT)
|
|
copy_test++;
|
|
return 0;
|
|
}
|