Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  2
  3/*
  4 * Copyright (C) 2020 Google LLC.
  5 */
  6
  7#include <asm-generic/errno-base.h>
  8#include <sys/stat.h>
  9#include <test_progs.h>
 10#include <linux/limits.h>
 11
 12#include "local_storage.skel.h"
 13#include "network_helpers.h"
 14#include "task_local_storage_helpers.h"
 15
 
 
 16#define TEST_STORAGE_VALUE 0xbeefdead
 17
 18struct storage {
 19	void *inode;
 20	unsigned int value;
 21};
 22
 23/* Fork and exec the provided rm binary and return the exit code of the
 24 * forked process and its pid.
 25 */
 26static int run_self_unlink(struct local_storage *skel, const char *rm_path)
 27{
 28	int child_pid, child_status, ret;
 29	int null_fd;
 30
 31	child_pid = fork();
 32	if (child_pid == 0) {
 33		null_fd = open("/dev/null", O_WRONLY);
 34		dup2(null_fd, STDOUT_FILENO);
 35		dup2(null_fd, STDERR_FILENO);
 36		close(null_fd);
 37
 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.
 41		 */
 42		ret = execlp(rm_path, rm_path, rm_path, NULL);
 43		if (ret)
 44			exit(errno);
 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);
 49	}
 50
 51	return -EINVAL;
 52}
 53
 54static bool check_syscall_operations(int map_fd, int obj_fd)
 55{
 56	struct storage val = { .value = TEST_STORAGE_VALUE },
 57		       lookup_val = { .value = 0 };
 58	int err;
 59
 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"))
 
 63		return false;
 64
 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"))
 
 68		return false;
 69
 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"))
 
 73		return false;
 74
 75	/* Check the value of the newly created element */
 76	if (!ASSERT_EQ(lookup_val.value, val.value, "bpf_map_lookup_elem"))
 
 77		return false;
 78
 79	err = bpf_map_delete_elem(map_fd, &obj_fd);
 80	if (!ASSERT_OK(err, "bpf_map_delete_elem()"))
 
 81		return false;
 82
 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"))
 
 86		return false;
 87
 88	return true;
 89}
 90
 91void test_test_local_storage(void)
 92{
 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];
 97	char cmd[256];
 98
 99	skel = local_storage__open_and_load();
100	if (!ASSERT_OK_PTR(skel, "skel_load"))
101		goto close_prog;
102
103	err = local_storage__attach(skel);
104	if (!ASSERT_OK(err, "attach"))
105		goto close_prog;
106
107	task_fd = sys_pidfd_open(getpid(), 0);
108	if (!ASSERT_GE(task_fd, 0, "pidfd_open"))
 
109		goto close_prog;
110
111	if (!check_syscall_operations(bpf_map__fd(skel->maps.task_storage_map),
112				      task_fd))
113		goto close_prog;
114
115	if (!ASSERT_OK_PTR(mkdtemp(tmp_dir_path), "mkdtemp"))
 
116		goto close_prog;
117
118	snprintf(tmp_exec_path, sizeof(tmp_exec_path), "%s/copy_of_rm",
119		 tmp_dir_path);
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;
123
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;
127
128	if (!check_syscall_operations(bpf_map__fd(skel->maps.inode_storage_map),
129				      rm_fd))
130		goto close_prog_rmdir;
131
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
135	 * LSM program.
136	 */
137	err = run_self_unlink(skel, tmp_exec_path);
138	if (!ASSERT_EQ(err, EPERM, "run_self_unlink"))
139		goto close_prog_rmdir;
140
141	/* Set the process being monitored to be the current process */
142	skel->bss->monitored_pid = getpid();
143
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.
146	 */
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;
151
152	ASSERT_EQ(skel->data->inode_storage_result, 0, "inode_storage_result");
 
153
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;
157
158	ASSERT_EQ(skel->data->sk_storage_result, 0, "sk_storage_result");
 
159
160	if (!check_syscall_operations(bpf_map__fd(skel->maps.sk_storage_map),
161				      serv_sk))
162		goto close_prog_rmdir;
163
164close_prog_rmdir:
165	snprintf(cmd, sizeof(cmd), "rm -rf %s", tmp_dir_path);
166	system(cmd);
167close_prog:
168	close(serv_sk);
169	close(rm_fd);
170	close(task_fd);
171	local_storage__destroy(skel);
172}
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2
  3/*
  4 * Copyright (C) 2020 Google LLC.
  5 */
  6
  7#include <asm-generic/errno-base.h>
  8#include <sys/stat.h>
  9#include <test_progs.h>
 10#include <linux/limits.h>
 11
 12#include "local_storage.skel.h"
 13#include "network_helpers.h"
 14#include "task_local_storage_helpers.h"
 15
 16static unsigned int duration;
 17
 18#define TEST_STORAGE_VALUE 0xbeefdead
 19
 20struct storage {
 21	void *inode;
 22	unsigned int value;
 23};
 24
 25/* Fork and exec the provided rm binary and return the exit code of the
 26 * forked process and its pid.
 27 */
 28static int run_self_unlink(int *monitored_pid, const char *rm_path)
 29{
 30	int child_pid, child_status, ret;
 31	int null_fd;
 32
 33	child_pid = fork();
 34	if (child_pid == 0) {
 35		null_fd = open("/dev/null", O_WRONLY);
 36		dup2(null_fd, STDOUT_FILENO);
 37		dup2(null_fd, STDERR_FILENO);
 38		close(null_fd);
 39
 40		*monitored_pid = getpid();
 41		/* Use the copied /usr/bin/rm to delete itself
 42		 * /tmp/copy_of_rm /tmp/copy_of_rm.
 43		 */
 44		ret = execlp(rm_path, rm_path, rm_path, NULL);
 45		if (ret)
 46			exit(errno);
 47	} else if (child_pid > 0) {
 48		waitpid(child_pid, &child_status, 0);
 
 49		return WEXITSTATUS(child_status);
 50	}
 51
 52	return -EINVAL;
 53}
 54
 55static bool check_syscall_operations(int map_fd, int obj_fd)
 56{
 57	struct storage val = { .value = TEST_STORAGE_VALUE },
 58		       lookup_val = { .value = 0 };
 59	int err;
 60
 61	/* Looking up an existing element should fail initially */
 62	err = bpf_map_lookup_elem_flags(map_fd, &obj_fd, &lookup_val, 0);
 63	if (CHECK(!err || errno != ENOENT, "bpf_map_lookup_elem",
 64		  "err:%d errno:%d\n", err, errno))
 65		return false;
 66
 67	/* Create a new element */
 68	err = bpf_map_update_elem(map_fd, &obj_fd, &val, BPF_NOEXIST);
 69	if (CHECK(err < 0, "bpf_map_update_elem", "err:%d errno:%d\n", err,
 70		  errno))
 71		return false;
 72
 73	/* Lookup the newly created element */
 74	err = bpf_map_lookup_elem_flags(map_fd, &obj_fd, &lookup_val, 0);
 75	if (CHECK(err < 0, "bpf_map_lookup_elem", "err:%d errno:%d", err,
 76		  errno))
 77		return false;
 78
 79	/* Check the value of the newly created element */
 80	if (CHECK(lookup_val.value != val.value, "bpf_map_lookup_elem",
 81		  "value got = %x errno:%d", lookup_val.value, val.value))
 82		return false;
 83
 84	err = bpf_map_delete_elem(map_fd, &obj_fd);
 85	if (CHECK(err, "bpf_map_delete_elem()", "err:%d errno:%d\n", err,
 86		  errno))
 87		return false;
 88
 89	/* The lookup should fail, now that the element has been deleted */
 90	err = bpf_map_lookup_elem_flags(map_fd, &obj_fd, &lookup_val, 0);
 91	if (CHECK(!err || errno != ENOENT, "bpf_map_lookup_elem",
 92		  "err:%d errno:%d\n", err, errno))
 93		return false;
 94
 95	return true;
 96}
 97
 98void test_test_local_storage(void)
 99{
100	char tmp_dir_path[] = "/tmp/local_storageXXXXXX";
101	int err, serv_sk = -1, task_fd = -1, rm_fd = -1;
102	struct local_storage *skel = NULL;
103	char tmp_exec_path[64];
104	char cmd[256];
105
106	skel = local_storage__open_and_load();
107	if (CHECK(!skel, "skel_load", "lsm skeleton failed\n"))
108		goto close_prog;
109
110	err = local_storage__attach(skel);
111	if (CHECK(err, "attach", "lsm attach failed: %d\n", err))
112		goto close_prog;
113
114	task_fd = sys_pidfd_open(getpid(), 0);
115	if (CHECK(task_fd < 0, "pidfd_open",
116		  "failed to get pidfd err:%d, errno:%d", task_fd, errno))
117		goto close_prog;
118
119	if (!check_syscall_operations(bpf_map__fd(skel->maps.task_storage_map),
120				      task_fd))
121		goto close_prog;
122
123	if (CHECK(!mkdtemp(tmp_dir_path), "mkdtemp",
124		  "unable to create tmpdir: %d\n", errno))
125		goto close_prog;
126
127	snprintf(tmp_exec_path, sizeof(tmp_exec_path), "%s/copy_of_rm",
128		 tmp_dir_path);
129	snprintf(cmd, sizeof(cmd), "cp /bin/rm %s", tmp_exec_path);
130	if (CHECK_FAIL(system(cmd)))
131		goto close_prog_rmdir;
132
133	rm_fd = open(tmp_exec_path, O_RDONLY);
134	if (CHECK(rm_fd < 0, "open", "failed to open %s err:%d, errno:%d",
135		  tmp_exec_path, rm_fd, errno))
136		goto close_prog_rmdir;
137
138	if (!check_syscall_operations(bpf_map__fd(skel->maps.inode_storage_map),
139				      rm_fd))
140		goto close_prog_rmdir;
141
142	/* Sets skel->bss->monitored_pid to the pid of the forked child
143	 * forks a child process that executes tmp_exec_path and tries to
144	 * unlink its executable. This operation should be denied by the loaded
145	 * LSM program.
146	 */
147	err = run_self_unlink(&skel->bss->monitored_pid, tmp_exec_path);
148	if (CHECK(err != EPERM, "run_self_unlink", "err %d want EPERM\n", err))
149		goto close_prog_rmdir;
150
151	/* Set the process being monitored to be the current process */
152	skel->bss->monitored_pid = getpid();
153
154	/* Move copy_of_rm to a new location so that it triggers the
155	 * inode_rename LSM hook with a new_dentry that has a NULL inode ptr.
156	 */
157	snprintf(cmd, sizeof(cmd), "mv %s/copy_of_rm %s/check_null_ptr",
158		 tmp_dir_path, tmp_dir_path);
159	if (CHECK_FAIL(system(cmd)))
160		goto close_prog_rmdir;
161
162	CHECK(skel->data->inode_storage_result != 0, "inode_storage_result",
163	      "inode_local_storage not set\n");
164
165	serv_sk = start_server(AF_INET6, SOCK_STREAM, NULL, 0, 0);
166	if (CHECK(serv_sk < 0, "start_server", "failed to start server\n"))
167		goto close_prog_rmdir;
168
169	CHECK(skel->data->sk_storage_result != 0, "sk_storage_result",
170	      "sk_local_storage not set\n");
171
172	if (!check_syscall_operations(bpf_map__fd(skel->maps.sk_storage_map),
173				      serv_sk))
174		goto close_prog_rmdir;
175
176close_prog_rmdir:
177	snprintf(cmd, sizeof(cmd), "rm -rf %s", tmp_dir_path);
178	system(cmd);
179close_prog:
180	close(serv_sk);
181	close(rm_fd);
182	close(task_fd);
183	local_storage__destroy(skel);
184}