Skip to content

Commit 7c05aa4

Browse files
laoarKernel Patches Daemon
authored and
Kernel Patches Daemon
committed
selftests/bpf: Add selftests for set_mempolicy with a lsm prog
In the straightforward LSM prog, it denies the use of mbind(2) with the mode MPOL_BIND and permits other modes. Consequently: - Absent the LSM prog, mbind(2) should invariably succeed regardless of the mode #263/1 set_mempolicy/MPOL_BIND_without_lsm:OK #263/2 set_mempolicy/MPOL_DEFAULT_without_lsm:OK - With the LSM prog - mbind(2) with the mode MPOL_BIND should result in failure #263/3 set_mempolicy/MPOL_BIND_with_lsm:OK - mbind(2) with the mode MPOL_DEFAULT should succeed #263/4 set_mempolicy/MPOL_DEFAULT_with_lsm:OK - Summary #263 set_mempolicy:OK Summary: 1/4 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Yafang Shao <[email protected]>
1 parent da7729e commit 7c05aa4

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (C) 2023 Yafang Shao <[email protected]> */
3+
4+
#include <unistd.h>
5+
#include <sys/types.h>
6+
#include <sys/mman.h>
7+
#include <linux/mempolicy.h>
8+
#include <test_progs.h>
9+
#include "test_set_mempolicy.skel.h"
10+
11+
#define SIZE 4096
12+
13+
static void mempolicy_bind(bool success)
14+
{
15+
unsigned long mask = 1;
16+
char *addr;
17+
int err;
18+
19+
addr = mmap(NULL, SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
20+
if (!ASSERT_OK_PTR(addr, "mmap"))
21+
return;
22+
23+
/* -lnuma is required by mbind(2), so use __NR_mbind to avoid the dependency. */
24+
err = syscall(__NR_mbind, addr, SIZE, MPOL_BIND, &mask, sizeof(mask), 0);
25+
if (success)
26+
ASSERT_OK(err, "mbind_success");
27+
else
28+
ASSERT_ERR(err, "mbind_fail");
29+
30+
munmap(addr, SIZE);
31+
}
32+
33+
static void mempolicy_default(void)
34+
{
35+
char *addr;
36+
int err;
37+
38+
addr = mmap(NULL, SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
39+
if (!ASSERT_OK_PTR(addr, "mmap"))
40+
return;
41+
42+
err = syscall(__NR_mbind, addr, SIZE, MPOL_DEFAULT, NULL, 0, 0);
43+
ASSERT_OK(err, "mbind_success");
44+
45+
munmap(addr, SIZE);
46+
}
47+
48+
void test_set_mempolicy(void)
49+
{
50+
struct test_set_mempolicy *skel;
51+
int err;
52+
53+
skel = test_set_mempolicy__open();
54+
if (!ASSERT_OK_PTR(skel, "open"))
55+
return;
56+
57+
skel->bss->target_pid = getpid();
58+
59+
err = test_set_mempolicy__load(skel);
60+
if (!ASSERT_OK(err, "load"))
61+
goto destroy;
62+
63+
/* Without LSM, mbind(2) should succeed regardless of the mode. */
64+
if (test__start_subtest("MPOL_BIND_without_lsm"))
65+
mempolicy_bind(true);
66+
if (test__start_subtest("MPOL_DEFAULT_without_lsm"))
67+
mempolicy_default();
68+
69+
/* Attach LSM prog, in which it will deny MPOL_BIND */
70+
err = test_set_mempolicy__attach(skel);
71+
if (!ASSERT_OK(err, "attach"))
72+
goto destroy;
73+
74+
/* MPOL_BIND should fail. */
75+
if (test__start_subtest("MPOL_BIND_with_lsm"))
76+
mempolicy_bind(false);
77+
78+
/* MPOL_DEFAULT should succeed. */
79+
if (test__start_subtest("MPOL_DEFAULT_with_lsm"))
80+
mempolicy_default();
81+
82+
destroy:
83+
test_set_mempolicy__destroy(skel);
84+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (C) 2023 Yafang Shao <[email protected]> */
3+
4+
#include "vmlinux.h"
5+
#include <bpf/bpf_helpers.h>
6+
#include <bpf/bpf_tracing.h>
7+
8+
int target_pid;
9+
10+
static int mem_policy_adjustment(u64 mode)
11+
{
12+
struct task_struct *task = bpf_get_current_task_btf();
13+
14+
if (task->pid != target_pid)
15+
return 0;
16+
17+
if (mode != MPOL_BIND)
18+
return 0;
19+
return -1;
20+
}
21+
22+
SEC("lsm/set_mempolicy")
23+
int BPF_PROG(setmempolicy, u64 mode, u16 mode_flags, nodemask_t *nmask, u32 flags)
24+
{
25+
return mem_policy_adjustment(mode);
26+
}
27+
28+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)