Skip to content

Commit ed51eee

Browse files
sinkapkernel-patches-bot
authored andcommitted
bpf: Add tests for task_local_storage
The test implements a simple MAC policy which denies an executable from unlinking itself. The LSM program bprm_committed_creds sets a task_local_storage with a pointer to the inode. This is then used to detect if the task is trying to unlink itself in the inode_unlink LSM hook. The test copies /bin/rm to /tmp and executes it in a child thread with the intention of deleting itself. A successful test should prevent the the running executable from deleting itself. The temporary file is cleaned up later in the test. Signed-off-by: KP Singh <[email protected]>
1 parent 66cb532 commit ed51eee

File tree

2 files changed

+116
-17
lines changed

2 files changed

+116
-17
lines changed

tools/testing/selftests/bpf/prog_tests/test_local_storage.c

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,83 @@
44
* Copyright (C) 2020 Google LLC.
55
*/
66

7+
#define _GNU_SOURCE
8+
9+
#include <asm-generic/errno-base.h>
10+
#include <unistd.h>
11+
#include <sys/stat.h>
712
#include <test_progs.h>
813
#include <linux/limits.h>
914

1015
#include "local_storage.skel.h"
1116
#include "network_helpers.h"
1217

13-
int create_and_unlink_file(void)
18+
/* Copies an rm binary to a temp file. dest is a mkstemp template */
19+
int copy_rm(char *dest)
1420
{
15-
char fname[PATH_MAX] = "/tmp/fileXXXXXX";
16-
int fd;
21+
int ret, fd_in, fd_out;
22+
struct stat stat;
23+
24+
fd_in = open("/bin/rm", O_RDONLY);
25+
if (fd_in < 0)
26+
return fd_in;
27+
28+
fd_out = mkstemp(dest);
29+
if (fd_out < 0)
30+
return fd_out;
1731

18-
fd = mkstemp(fname);
19-
if (fd < 0)
20-
return fd;
32+
ret = fstat(fd_in, &stat);
33+
if (ret == -1)
34+
return errno;
2135

22-
close(fd);
23-
unlink(fname);
36+
ret = copy_file_range(fd_in, NULL, fd_out, NULL, stat.st_size, 0);
37+
if (ret == -1)
38+
return errno;
39+
40+
/* Set executable permission on the copied file */
41+
ret = chmod(dest, 0100);
42+
if (ret == -1)
43+
return errno;
44+
45+
close(fd_in);
46+
close(fd_out);
2447
return 0;
2548
}
49+
/* Fork and exec the provided rm binary and return the exit code of the
50+
* forked process and its pid.
51+
*/
52+
int run_self_unlink(int *monitored_pid, const char *rm_path)
53+
{
54+
int child_pid, child_status, ret;
55+
int null_fd;
56+
57+
child_pid = fork();
58+
if (child_pid == 0) {
59+
null_fd = open("/dev/null", O_WRONLY);
60+
dup2(null_fd, STDOUT_FILENO);
61+
dup2(null_fd, STDERR_FILENO);
62+
close(null_fd);
63+
64+
*monitored_pid = getpid();
65+
/* Use the copied /usr/bin/rm to delete itself
66+
* /tmp/copy_of_rm /tmp/copy_of_rm.
67+
*/
68+
ret = execlp(rm_path, rm_path, rm_path, NULL);
69+
if (ret)
70+
exit(errno);
71+
} else if (child_pid > 0) {
72+
waitpid(child_pid, &child_status, 0);
73+
return WEXITSTATUS(child_status);
74+
}
75+
76+
return -EINVAL;
77+
}
2678

2779
void test_test_local_storage(void)
2880
{
2981
struct local_storage *skel = NULL;
3082
int err, duration = 0, serv_sk = -1;
83+
char tmp_exec_path[PATH_MAX] = "/tmp/copy_of_rmXXXXXX";
3184

3285
skel = local_storage__open_and_load();
3386
if (CHECK(!skel, "skel_load", "lsm skeleton failed\n"))
@@ -37,10 +90,26 @@ void test_test_local_storage(void)
3790
if (CHECK(err, "attach", "lsm attach failed: %d\n", err))
3891
goto close_prog;
3992

93+
err = copy_rm(tmp_exec_path);
94+
if (CHECK(err < 0, "copy_executable", "err %d errno %d\n", err, errno))
95+
goto close_prog;
96+
97+
/* Sets skel->bss->monitored_pid to the pid of the forked child
98+
* forks a child process that executes tmp_exec_path and tries to
99+
* unlink its executable. This operation should be denied by the loaded
100+
* LSM program.
101+
*/
102+
err = run_self_unlink(&skel->bss->monitored_pid, tmp_exec_path);
103+
if (CHECK(err != EPERM, "run_self_unlink", "err %d want EPERM\n", err))
104+
goto close_prog;
105+
106+
/* Set the process being monitored to be the current process */
40107
skel->bss->monitored_pid = getpid();
41108

42-
err = create_and_unlink_file();
43-
if (CHECK(err < 0, "exec_cmd", "err %d errno %d\n", err, errno))
109+
/* Remove the temporary created executable */
110+
err = unlink(tmp_exec_path);
111+
if (CHECK(err != 0, "unlink", "unable to unlink %s: %d", tmp_exec_path,
112+
errno))
44113
goto close_prog;
45114

46115
CHECK(skel->data->inode_storage_result != 0, "inode_storage_result",

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

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,49 @@ int monitored_pid = 0;
1717
int inode_storage_result = -1;
1818
int sk_storage_result = -1;
1919

20-
struct dummy_storage {
20+
struct local_storage {
21+
struct inode *exec_inode;
2122
__u32 value;
2223
};
2324

2425
struct {
2526
__uint(type, BPF_MAP_TYPE_INODE_STORAGE);
2627
__uint(map_flags, BPF_F_NO_PREALLOC);
2728
__type(key, int);
28-
__type(value, struct dummy_storage);
29+
__type(value, struct local_storage);
2930
} inode_storage_map SEC(".maps");
3031

3132
struct {
3233
__uint(type, BPF_MAP_TYPE_SK_STORAGE);
3334
__uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_CLONE);
3435
__type(key, int);
35-
__type(value, struct dummy_storage);
36+
__type(value, struct local_storage);
3637
} sk_storage_map SEC(".maps");
3738

39+
struct {
40+
__uint(type, BPF_MAP_TYPE_TASK_STORAGE);
41+
__uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_CLONE);
42+
__type(key, int);
43+
__type(value, struct local_storage);
44+
} task_storage_map SEC(".maps");
45+
3846
SEC("lsm/inode_unlink")
3947
int BPF_PROG(unlink_hook, struct inode *dir, struct dentry *victim)
4048
{
4149
__u32 pid = bpf_get_current_pid_tgid() >> 32;
42-
struct dummy_storage *storage;
50+
struct local_storage *storage;
4351
int err;
4452

4553
if (pid != monitored_pid)
4654
return 0;
4755

56+
storage = bpf_task_storage_get(&task_storage_map,
57+
bpf_get_current_task_btf(), 0, 0);
58+
59+
/* Don't let an executable delete itself */
60+
if (storage && storage->exec_inode == victim->d_inode)
61+
return -EPERM;
62+
4863
storage = bpf_inode_storage_get(&inode_storage_map, victim->d_inode, 0,
4964
BPF_SK_STORAGE_GET_F_CREATE);
5065
if (!storage)
@@ -65,7 +80,7 @@ int BPF_PROG(socket_bind, struct socket *sock, struct sockaddr *address,
6580
int addrlen)
6681
{
6782
__u32 pid = bpf_get_current_pid_tgid() >> 32;
68-
struct dummy_storage *storage;
83+
struct local_storage *storage;
6984
int err;
7085

7186
if (pid != monitored_pid)
@@ -91,7 +106,7 @@ int BPF_PROG(socket_post_create, struct socket *sock, int family, int type,
91106
int protocol, int kern)
92107
{
93108
__u32 pid = bpf_get_current_pid_tgid() >> 32;
94-
struct dummy_storage *storage;
109+
struct local_storage *storage;
95110

96111
if (pid != monitored_pid)
97112
return 0;
@@ -110,7 +125,7 @@ SEC("lsm/file_open")
110125
int BPF_PROG(file_open, struct file *file)
111126
{
112127
__u32 pid = bpf_get_current_pid_tgid() >> 32;
113-
struct dummy_storage *storage;
128+
struct local_storage *storage;
114129

115130
if (pid != monitored_pid)
116131
return 0;
@@ -126,3 +141,18 @@ int BPF_PROG(file_open, struct file *file)
126141
storage->value = DUMMY_STORAGE_VALUE;
127142
return 0;
128143
}
144+
145+
/* This uses the local storage to remember the inode of the binary that a
146+
* process was originally executing.
147+
*/
148+
SEC("lsm/bprm_committed_creds")
149+
void BPF_PROG(exec, struct linux_binprm *bprm)
150+
{
151+
struct local_storage *storage;
152+
153+
storage = bpf_task_storage_get(&task_storage_map,
154+
bpf_get_current_task_btf(), 0,
155+
BPF_LOCAL_STORAGE_GET_F_CREATE);
156+
if (storage)
157+
storage->exec_inode = bprm->file->f_inode;
158+
}

0 commit comments

Comments
 (0)