Skip to content

Commit f56407f

Browse files
Alexei Starovoitovborkmann
authored andcommitted
bpf: Remove bpf_lsm_file_mprotect from sleepable list.
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: 1e6c62a ("bpf: Introduce sleepable BPF programs") Fixes: e68a144 ("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
1 parent b69e56c commit f56407f

3 files changed

Lines changed: 18 additions & 18 deletions

File tree

‎kernel/bpf/trampoline.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start)
409409
void notrace __bpf_prog_enter_sleepable(void)
410410
{
411411
rcu_read_lock_trace();
412+
might_fault();
412413
}
413414

414415
void notrace __bpf_prog_exit_sleepable(void)

‎kernel/bpf/verifier.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11006,7 +11006,6 @@ static int check_attach_modify_return(struct bpf_prog *prog, unsigned long addr)
1100611006
/* non exhaustive list of sleepable bpf_lsm_*() functions */
1100711007
BTF_SET_START(btf_sleepable_lsm_hooks)
1100811008
#ifdef CONFIG_BPF_LSM
11009-
BTF_ID(func, bpf_lsm_file_mprotect)
1101011009
BTF_ID(func, bpf_lsm_bprm_committed_creds)
1101111010
#else
1101211011
BTF_ID_UNUSED

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,10 @@ int monitored_pid = 0;
3636
int mprotect_count = 0;
3737
int bprm_count = 0;
3838

39-
SEC("lsm.s/file_mprotect")
39+
SEC("lsm/file_mprotect")
4040
int BPF_PROG(test_int_hook, struct vm_area_struct *vma,
4141
unsigned long reqprot, unsigned long prot, int ret)
4242
{
43-
char args[64];
44-
__u32 key = 0;
45-
__u64 *value;
46-
4743
if (ret != 0)
4844
return ret;
4945

@@ -53,18 +49,6 @@ int BPF_PROG(test_int_hook, struct vm_area_struct *vma,
5349
is_stack = (vma->vm_start <= vma->vm_mm->start_stack &&
5450
vma->vm_end >= vma->vm_mm->start_stack);
5551

56-
bpf_copy_from_user(args, sizeof(args), (void *)vma->vm_mm->arg_start);
57-
58-
value = bpf_map_lookup_elem(&array, &key);
59-
if (value)
60-
*value = 0;
61-
value = bpf_map_lookup_elem(&hash, &key);
62-
if (value)
63-
*value = 0;
64-
value = bpf_map_lookup_elem(&lru_hash, &key);
65-
if (value)
66-
*value = 0;
67-
6852
if (is_stack && monitored_pid == pid) {
6953
mprotect_count++;
7054
ret = -EPERM;
@@ -77,10 +61,26 @@ SEC("lsm.s/bprm_committed_creds")
7761
int BPF_PROG(test_void_hook, struct linux_binprm *bprm)
7862
{
7963
__u32 pid = bpf_get_current_pid_tgid() >> 32;
64+
char args[64];
65+
__u32 key = 0;
66+
__u64 *value;
8067

8168
if (monitored_pid == pid)
8269
bprm_count++;
8370

71+
bpf_copy_from_user(args, sizeof(args), (void *)bprm->vma->vm_mm->arg_start);
72+
bpf_copy_from_user(args, sizeof(args), (void *)bprm->mm->arg_start);
73+
74+
value = bpf_map_lookup_elem(&array, &key);
75+
if (value)
76+
*value = 0;
77+
value = bpf_map_lookup_elem(&hash, &key);
78+
if (value)
79+
*value = 0;
80+
value = bpf_map_lookup_elem(&lru_hash, &key);
81+
if (value)
82+
*value = 0;
83+
8484
return 0;
8585
}
8686
SEC("lsm/task_free") /* lsm/ is ok, lsm.s/ fails */

0 commit comments

Comments
 (0)