1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2020 Google LLC.
7 #include <asm-generic/errno-base.h>
9 #include <test_progs.h>
10 #include <linux/limits.h>
12 #include "local_storage.skel.h"
13 #include "network_helpers.h"
14 #include "task_local_storage_helpers.h"
16 #define TEST_STORAGE_VALUE 0xbeefdead
23 /* Fork and exec the provided rm binary and return the exit code of the
24 * forked process and its pid.
26 static int run_self_unlink(struct local_storage *skel, const char *rm_path)
28 int child_pid, child_status, ret;
33 null_fd = open("/dev/null", O_WRONLY);
34 dup2(null_fd, STDOUT_FILENO);
35 dup2(null_fd, STDERR_FILENO);
38 skel->bss->monitored_pid = getpid();
39 /* Use the copied /usr/bin/rm to delete itself
40 * /tmp/copy_of_rm /tmp/copy_of_rm.
42 ret = execlp(rm_path, rm_path, rm_path, NULL);
45 } else if (child_pid > 0) {
46 waitpid(child_pid, &child_status, 0);
47 ASSERT_EQ(skel->data->task_storage_result, 0, "task_storage_result");
48 return WEXITSTATUS(child_status);
54 static bool check_syscall_operations(int map_fd, int obj_fd)
56 struct storage val = { .value = TEST_STORAGE_VALUE },
57 lookup_val = { .value = 0 };
60 /* Looking up an existing element should fail initially */
61 err = bpf_map_lookup_elem_flags(map_fd, &obj_fd, &lookup_val, 0);
62 if (!ASSERT_EQ(err, -ENOENT, "bpf_map_lookup_elem"))
65 /* Create a new element */
66 err = bpf_map_update_elem(map_fd, &obj_fd, &val, BPF_NOEXIST);
67 if (!ASSERT_OK(err, "bpf_map_update_elem"))
70 /* Lookup the newly created element */
71 err = bpf_map_lookup_elem_flags(map_fd, &obj_fd, &lookup_val, 0);
72 if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
75 /* Check the value of the newly created element */
76 if (!ASSERT_EQ(lookup_val.value, val.value, "bpf_map_lookup_elem"))
79 err = bpf_map_delete_elem(map_fd, &obj_fd);
80 if (!ASSERT_OK(err, "bpf_map_delete_elem()"))
83 /* The lookup should fail, now that the element has been deleted */
84 err = bpf_map_lookup_elem_flags(map_fd, &obj_fd, &lookup_val, 0);
85 if (!ASSERT_EQ(err, -ENOENT, "bpf_map_lookup_elem"))
91 void test_test_local_storage(void)
93 char tmp_dir_path[] = "/tmp/local_storageXXXXXX";
94 int err, serv_sk = -1, task_fd = -1, rm_fd = -1;
95 struct local_storage *skel = NULL;
96 char tmp_exec_path[64];
99 skel = local_storage__open_and_load();
100 if (!ASSERT_OK_PTR(skel, "skel_load"))
103 err = local_storage__attach(skel);
104 if (!ASSERT_OK(err, "attach"))
107 task_fd = sys_pidfd_open(getpid(), 0);
108 if (!ASSERT_GE(task_fd, 0, "pidfd_open"))
111 if (!check_syscall_operations(bpf_map__fd(skel->maps.task_storage_map),
115 if (!ASSERT_OK_PTR(mkdtemp(tmp_dir_path), "mkdtemp"))
118 snprintf(tmp_exec_path, sizeof(tmp_exec_path), "%s/copy_of_rm",
120 snprintf(cmd, sizeof(cmd), "cp /bin/rm %s", tmp_exec_path);
121 if (!ASSERT_OK(system(cmd), "system(cp)"))
122 goto close_prog_rmdir;
124 rm_fd = open(tmp_exec_path, O_RDONLY);
125 if (!ASSERT_GE(rm_fd, 0, "open(tmp_exec_path)"))
126 goto close_prog_rmdir;
128 if (!check_syscall_operations(bpf_map__fd(skel->maps.inode_storage_map),
130 goto close_prog_rmdir;
132 /* Sets skel->bss->monitored_pid to the pid of the forked child
133 * forks a child process that executes tmp_exec_path and tries to
134 * unlink its executable. This operation should be denied by the loaded
137 err = run_self_unlink(skel, tmp_exec_path);
138 if (!ASSERT_EQ(err, EPERM, "run_self_unlink"))
139 goto close_prog_rmdir;
141 /* Set the process being monitored to be the current process */
142 skel->bss->monitored_pid = getpid();
144 /* Move copy_of_rm to a new location so that it triggers the
145 * inode_rename LSM hook with a new_dentry that has a NULL inode ptr.
147 snprintf(cmd, sizeof(cmd), "mv %s/copy_of_rm %s/check_null_ptr",
148 tmp_dir_path, tmp_dir_path);
149 if (!ASSERT_OK(system(cmd), "system(mv)"))
150 goto close_prog_rmdir;
152 ASSERT_EQ(skel->data->inode_storage_result, 0, "inode_storage_result");
154 serv_sk = start_server(AF_INET6, SOCK_STREAM, NULL, 0, 0);
155 if (!ASSERT_GE(serv_sk, 0, "start_server"))
156 goto close_prog_rmdir;
158 ASSERT_EQ(skel->data->sk_storage_result, 0, "sk_storage_result");
160 if (!check_syscall_operations(bpf_map__fd(skel->maps.sk_storage_map),
162 goto close_prog_rmdir;
165 snprintf(cmd, sizeof(cmd), "rm -rf %s", tmp_dir_path);
171 local_storage__destroy(skel);