Commit 5222d696 authored by KP Singh's avatar KP Singh Committed by Alexei Starovoitov

bpf, lsm: Fix the file_mprotect LSM test.

The test was previously using an mprotect on the heap memory allocated
using malloc and was expecting the allocation to be always using
sbrk(2). This is, however, not always true and in certain conditions
malloc may end up using anonymous mmaps for heap alloctions. This means
that the following condition that is used in the "lsm/file_mprotect"
program is not sufficent to detect all mprotect calls done on heap
memory:

	is_heap = (vma->vm_start >= vma->vm_mm->start_brk &&
		   vma->vm_end <= vma->vm_mm->brk);

The test is updated to use an mprotect on memory allocated on the stack.
While this would result in the splitting of the vma, this happens only
after the security_file_mprotect hook. So, the condition used in the BPF
program holds true.

Fixes: 03e54f10 ("bpf: lsm: Add selftests for BPF_PROG_TYPE_LSM")
Reported-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarKP Singh <kpsingh@google.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200402200751.26372-1-kpsingh@chromium.org
parent 7a1ca972
...@@ -15,7 +15,10 @@ ...@@ -15,7 +15,10 @@
char *CMD_ARGS[] = {"true", NULL}; char *CMD_ARGS[] = {"true", NULL};
int heap_mprotect(void) #define GET_PAGE_ADDR(ADDR, PAGE_SIZE) \
(char *)(((unsigned long) (ADDR + PAGE_SIZE)) & ~(PAGE_SIZE-1))
int stack_mprotect(void)
{ {
void *buf; void *buf;
long sz; long sz;
...@@ -25,12 +28,9 @@ int heap_mprotect(void) ...@@ -25,12 +28,9 @@ int heap_mprotect(void)
if (sz < 0) if (sz < 0)
return sz; return sz;
buf = memalign(sz, 2 * sz); buf = alloca(sz * 3);
if (buf == NULL) ret = mprotect(GET_PAGE_ADDR(buf, sz), sz,
return -ENOMEM; PROT_READ | PROT_WRITE | PROT_EXEC);
ret = mprotect(buf, sz, PROT_READ | PROT_WRITE | PROT_EXEC);
free(buf);
return ret; return ret;
} }
...@@ -73,8 +73,8 @@ void test_test_lsm(void) ...@@ -73,8 +73,8 @@ void test_test_lsm(void)
skel->bss->monitored_pid = getpid(); skel->bss->monitored_pid = getpid();
err = heap_mprotect(); err = stack_mprotect();
if (CHECK(errno != EPERM, "heap_mprotect", "want errno=EPERM, got %d\n", if (CHECK(errno != EPERM, "stack_mprotect", "want err=EPERM, got %d\n",
errno)) errno))
goto close_prog; goto close_prog;
......
...@@ -23,12 +23,12 @@ int BPF_PROG(test_int_hook, struct vm_area_struct *vma, ...@@ -23,12 +23,12 @@ int BPF_PROG(test_int_hook, struct vm_area_struct *vma,
return ret; return ret;
__u32 pid = bpf_get_current_pid_tgid() >> 32; __u32 pid = bpf_get_current_pid_tgid() >> 32;
int is_heap = 0; int is_stack = 0;
is_heap = (vma->vm_start >= vma->vm_mm->start_brk && is_stack = (vma->vm_start <= vma->vm_mm->start_stack &&
vma->vm_end <= vma->vm_mm->brk); vma->vm_end >= vma->vm_mm->start_stack);
if (is_heap && monitored_pid == pid) { if (is_stack && monitored_pid == pid) {
mprotect_count++; mprotect_count++;
ret = -EPERM; ret = -EPERM;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment