1cd324d7aSKP Singh // SPDX-License-Identifier: GPL-2.0
2cd324d7aSKP Singh
3cd324d7aSKP Singh /*
4cd324d7aSKP Singh * Copyright (C) 2020 Google LLC.
5cd324d7aSKP Singh */
6cd324d7aSKP Singh
79cde3beeSKP Singh #include <asm-generic/errno-base.h>
89cde3beeSKP Singh #include <sys/stat.h>
9cd324d7aSKP Singh #include <test_progs.h>
10cd324d7aSKP Singh #include <linux/limits.h>
11cd324d7aSKP Singh
12cd324d7aSKP Singh #include "local_storage.skel.h"
13cd324d7aSKP Singh #include "network_helpers.h"
14c710136eSHou Tao #include "task_local_storage_helpers.h"
15cd324d7aSKP Singh
169cde3beeSKP Singh #define TEST_STORAGE_VALUE 0xbeefdead
179cde3beeSKP Singh
189cde3beeSKP Singh struct storage {
199cde3beeSKP Singh void *inode;
209cde3beeSKP Singh unsigned int value;
219cde3beeSKP Singh };
229cde3beeSKP Singh
239cde3beeSKP Singh /* Fork and exec the provided rm binary and return the exit code of the
249cde3beeSKP Singh * forked process and its pid.
259cde3beeSKP Singh */
run_self_unlink(struct local_storage * skel,const char * rm_path)26*d8db84d7SMartin KaFai Lau static int run_self_unlink(struct local_storage *skel, const char *rm_path)
279cde3beeSKP Singh {
289cde3beeSKP Singh int child_pid, child_status, ret;
299cde3beeSKP Singh int null_fd;
309cde3beeSKP Singh
319cde3beeSKP Singh child_pid = fork();
329cde3beeSKP Singh if (child_pid == 0) {
339cde3beeSKP Singh null_fd = open("/dev/null", O_WRONLY);
349cde3beeSKP Singh dup2(null_fd, STDOUT_FILENO);
359cde3beeSKP Singh dup2(null_fd, STDERR_FILENO);
369cde3beeSKP Singh close(null_fd);
379cde3beeSKP Singh
38*d8db84d7SMartin KaFai Lau skel->bss->monitored_pid = getpid();
399cde3beeSKP Singh /* Use the copied /usr/bin/rm to delete itself
409cde3beeSKP Singh * /tmp/copy_of_rm /tmp/copy_of_rm.
419cde3beeSKP Singh */
429cde3beeSKP Singh ret = execlp(rm_path, rm_path, rm_path, NULL);
439cde3beeSKP Singh if (ret)
449cde3beeSKP Singh exit(errno);
459cde3beeSKP Singh } else if (child_pid > 0) {
469cde3beeSKP Singh waitpid(child_pid, &child_status, 0);
47*d8db84d7SMartin KaFai Lau ASSERT_EQ(skel->data->task_storage_result, 0, "task_storage_result");
489cde3beeSKP Singh return WEXITSTATUS(child_status);
499cde3beeSKP Singh }
509cde3beeSKP Singh
519cde3beeSKP Singh return -EINVAL;
529cde3beeSKP Singh }
539cde3beeSKP Singh
check_syscall_operations(int map_fd,int obj_fd)549cde3beeSKP Singh static bool check_syscall_operations(int map_fd, int obj_fd)
559cde3beeSKP Singh {
560ae6eff2SKP Singh struct storage val = { .value = TEST_STORAGE_VALUE },
570ae6eff2SKP Singh lookup_val = { .value = 0 };
589cde3beeSKP Singh int err;
599cde3beeSKP Singh
609cde3beeSKP Singh /* Looking up an existing element should fail initially */
610ae6eff2SKP Singh err = bpf_map_lookup_elem_flags(map_fd, &obj_fd, &lookup_val, 0);
6257ef7715SMartin KaFai Lau if (!ASSERT_EQ(err, -ENOENT, "bpf_map_lookup_elem"))
639cde3beeSKP Singh return false;
649cde3beeSKP Singh
659cde3beeSKP Singh /* Create a new element */
660ae6eff2SKP Singh err = bpf_map_update_elem(map_fd, &obj_fd, &val, BPF_NOEXIST);
6757ef7715SMartin KaFai Lau if (!ASSERT_OK(err, "bpf_map_update_elem"))
689cde3beeSKP Singh return false;
699cde3beeSKP Singh
709cde3beeSKP Singh /* Lookup the newly created element */
710ae6eff2SKP Singh err = bpf_map_lookup_elem_flags(map_fd, &obj_fd, &lookup_val, 0);
7257ef7715SMartin KaFai Lau if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
739cde3beeSKP Singh return false;
749cde3beeSKP Singh
759cde3beeSKP Singh /* Check the value of the newly created element */
7657ef7715SMartin KaFai Lau if (!ASSERT_EQ(lookup_val.value, val.value, "bpf_map_lookup_elem"))
779cde3beeSKP Singh return false;
789cde3beeSKP Singh
799cde3beeSKP Singh err = bpf_map_delete_elem(map_fd, &obj_fd);
8057ef7715SMartin KaFai Lau if (!ASSERT_OK(err, "bpf_map_delete_elem()"))
819cde3beeSKP Singh return false;
829cde3beeSKP Singh
839cde3beeSKP Singh /* The lookup should fail, now that the element has been deleted */
840ae6eff2SKP Singh err = bpf_map_lookup_elem_flags(map_fd, &obj_fd, &lookup_val, 0);
8557ef7715SMartin KaFai Lau if (!ASSERT_EQ(err, -ENOENT, "bpf_map_lookup_elem"))
869cde3beeSKP Singh return false;
879cde3beeSKP Singh
889cde3beeSKP Singh return true;
89cd324d7aSKP Singh }
90cd324d7aSKP Singh
test_test_local_storage(void)91cd324d7aSKP Singh void test_test_local_storage(void)
92cd324d7aSKP Singh {
9315075bb7SKP Singh char tmp_dir_path[] = "/tmp/local_storageXXXXXX";
944170bc6bSKP Singh int err, serv_sk = -1, task_fd = -1, rm_fd = -1;
95cd324d7aSKP Singh struct local_storage *skel = NULL;
962f94ac19SKP Singh char tmp_exec_path[64];
972f94ac19SKP Singh char cmd[256];
98cd324d7aSKP Singh
99cd324d7aSKP Singh skel = local_storage__open_and_load();
10057ef7715SMartin KaFai Lau if (!ASSERT_OK_PTR(skel, "skel_load"))
101cd324d7aSKP Singh goto close_prog;
102cd324d7aSKP Singh
103cd324d7aSKP Singh err = local_storage__attach(skel);
10457ef7715SMartin KaFai Lau if (!ASSERT_OK(err, "attach"))
105cd324d7aSKP Singh goto close_prog;
106cd324d7aSKP Singh
1079cde3beeSKP Singh task_fd = sys_pidfd_open(getpid(), 0);
10857ef7715SMartin KaFai Lau if (!ASSERT_GE(task_fd, 0, "pidfd_open"))
1099cde3beeSKP Singh goto close_prog;
1109cde3beeSKP Singh
1119cde3beeSKP Singh if (!check_syscall_operations(bpf_map__fd(skel->maps.task_storage_map),
1129cde3beeSKP Singh task_fd))
1139cde3beeSKP Singh goto close_prog;
1149cde3beeSKP Singh
11557ef7715SMartin KaFai Lau if (!ASSERT_OK_PTR(mkdtemp(tmp_dir_path), "mkdtemp"))
1169cde3beeSKP Singh goto close_prog;
1179cde3beeSKP Singh
1182f94ac19SKP Singh snprintf(tmp_exec_path, sizeof(tmp_exec_path), "%s/copy_of_rm",
1192f94ac19SKP Singh tmp_dir_path);
1202f94ac19SKP Singh snprintf(cmd, sizeof(cmd), "cp /bin/rm %s", tmp_exec_path);
12157ef7715SMartin KaFai Lau if (!ASSERT_OK(system(cmd), "system(cp)"))
1222f94ac19SKP Singh goto close_prog_rmdir;
1232f94ac19SKP Singh
1244170bc6bSKP Singh rm_fd = open(tmp_exec_path, O_RDONLY);
12557ef7715SMartin KaFai Lau if (!ASSERT_GE(rm_fd, 0, "open(tmp_exec_path)"))
1262f94ac19SKP Singh goto close_prog_rmdir;
1274170bc6bSKP Singh
1284170bc6bSKP Singh if (!check_syscall_operations(bpf_map__fd(skel->maps.inode_storage_map),
1294170bc6bSKP Singh rm_fd))
1302f94ac19SKP Singh goto close_prog_rmdir;
1314170bc6bSKP Singh
1329cde3beeSKP Singh /* Sets skel->bss->monitored_pid to the pid of the forked child
1339cde3beeSKP Singh * forks a child process that executes tmp_exec_path and tries to
1349cde3beeSKP Singh * unlink its executable. This operation should be denied by the loaded
1359cde3beeSKP Singh * LSM program.
1369cde3beeSKP Singh */
137*d8db84d7SMartin KaFai Lau err = run_self_unlink(skel, tmp_exec_path);
13857ef7715SMartin KaFai Lau if (!ASSERT_EQ(err, EPERM, "run_self_unlink"))
1392f94ac19SKP Singh goto close_prog_rmdir;
1409cde3beeSKP Singh
1419cde3beeSKP Singh /* Set the process being monitored to be the current process */
142cd324d7aSKP Singh skel->bss->monitored_pid = getpid();
143cd324d7aSKP Singh
1442f94ac19SKP Singh /* Move copy_of_rm to a new location so that it triggers the
1452f94ac19SKP Singh * inode_rename LSM hook with a new_dentry that has a NULL inode ptr.
1462f94ac19SKP Singh */
1472f94ac19SKP Singh snprintf(cmd, sizeof(cmd), "mv %s/copy_of_rm %s/check_null_ptr",
1482f94ac19SKP Singh tmp_dir_path, tmp_dir_path);
14957ef7715SMartin KaFai Lau if (!ASSERT_OK(system(cmd), "system(mv)"))
1502f94ac19SKP Singh goto close_prog_rmdir;
151cd324d7aSKP Singh
15257ef7715SMartin KaFai Lau ASSERT_EQ(skel->data->inode_storage_result, 0, "inode_storage_result");
153cd324d7aSKP Singh
154cd324d7aSKP Singh serv_sk = start_server(AF_INET6, SOCK_STREAM, NULL, 0, 0);
15557ef7715SMartin KaFai Lau if (!ASSERT_GE(serv_sk, 0, "start_server"))
1562f94ac19SKP Singh goto close_prog_rmdir;
157cd324d7aSKP Singh
15857ef7715SMartin KaFai Lau ASSERT_EQ(skel->data->sk_storage_result, 0, "sk_storage_result");
159cd324d7aSKP Singh
1604170bc6bSKP Singh if (!check_syscall_operations(bpf_map__fd(skel->maps.sk_storage_map),
1614170bc6bSKP Singh serv_sk))
1622f94ac19SKP Singh goto close_prog_rmdir;
163cd324d7aSKP Singh
1642f94ac19SKP Singh close_prog_rmdir:
1652f94ac19SKP Singh snprintf(cmd, sizeof(cmd), "rm -rf %s", tmp_dir_path);
1662f94ac19SKP Singh system(cmd);
167cd324d7aSKP Singh close_prog:
1684170bc6bSKP Singh close(serv_sk);
1694170bc6bSKP Singh close(rm_fd);
1709cde3beeSKP Singh close(task_fd);
171cd324d7aSKP Singh local_storage__destroy(skel);
172cd324d7aSKP Singh }
173