xref: /openbmc/linux/tools/testing/selftests/bpf/prog_tests/test_lsm.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
103e54f10SKP Singh // SPDX-License-Identifier: GPL-2.0
203e54f10SKP Singh 
303e54f10SKP Singh /*
403e54f10SKP Singh  * Copyright (C) 2020 Google LLC.
503e54f10SKP Singh  */
603e54f10SKP Singh 
703e54f10SKP Singh #include <test_progs.h>
803e54f10SKP Singh #include <sys/mman.h>
903e54f10SKP Singh #include <sys/wait.h>
1003e54f10SKP Singh #include <unistd.h>
1103e54f10SKP Singh #include <malloc.h>
1203e54f10SKP Singh #include <stdlib.h>
1303e54f10SKP Singh 
1403e54f10SKP Singh #include "lsm.skel.h"
1503e54f10SKP Singh 
1603e54f10SKP Singh char *CMD_ARGS[] = {"true", NULL};
1703e54f10SKP Singh 
185222d696SKP Singh #define GET_PAGE_ADDR(ADDR, PAGE_SIZE)					\
195222d696SKP Singh 	(char *)(((unsigned long) (ADDR + PAGE_SIZE)) & ~(PAGE_SIZE-1))
205222d696SKP Singh 
stack_mprotect(void)215222d696SKP Singh int stack_mprotect(void)
2203e54f10SKP Singh {
2303e54f10SKP Singh 	void *buf;
2403e54f10SKP Singh 	long sz;
2503e54f10SKP Singh 	int ret;
2603e54f10SKP Singh 
2703e54f10SKP Singh 	sz = sysconf(_SC_PAGESIZE);
2803e54f10SKP Singh 	if (sz < 0)
2903e54f10SKP Singh 		return sz;
3003e54f10SKP Singh 
315222d696SKP Singh 	buf = alloca(sz * 3);
325222d696SKP Singh 	ret = mprotect(GET_PAGE_ADDR(buf, sz), sz,
335222d696SKP Singh 		       PROT_READ | PROT_WRITE | PROT_EXEC);
3403e54f10SKP Singh 	return ret;
3503e54f10SKP Singh }
3603e54f10SKP Singh 
exec_cmd(int * monitored_pid)3703e54f10SKP Singh int exec_cmd(int *monitored_pid)
3803e54f10SKP Singh {
3903e54f10SKP Singh 	int child_pid, child_status;
4003e54f10SKP Singh 
4103e54f10SKP Singh 	child_pid = fork();
4203e54f10SKP Singh 	if (child_pid == 0) {
4303e54f10SKP Singh 		*monitored_pid = getpid();
4403e54f10SKP Singh 		execvp(CMD_ARGS[0], CMD_ARGS);
4503e54f10SKP Singh 		return -EINVAL;
4603e54f10SKP Singh 	} else if (child_pid > 0) {
4703e54f10SKP Singh 		waitpid(child_pid, &child_status, 0);
4803e54f10SKP Singh 		return child_status;
4903e54f10SKP Singh 	}
5003e54f10SKP Singh 
5103e54f10SKP Singh 	return -EINVAL;
5203e54f10SKP Singh }
5303e54f10SKP Singh 
test_lsm(struct lsm * skel)54cede72adSJiri Olsa static int test_lsm(struct lsm *skel)
5503e54f10SKP Singh {
56cede72adSJiri Olsa 	struct bpf_link *link;
57e68a1445SAlexei Starovoitov 	int buf = 1234;
58cede72adSJiri Olsa 	int err;
5903e54f10SKP Singh 
6003e54f10SKP Singh 	err = lsm__attach(skel);
617bb2cc19SJiri Olsa 	if (!ASSERT_OK(err, "attach"))
62cede72adSJiri Olsa 		return err;
63cede72adSJiri Olsa 
64cede72adSJiri Olsa 	/* Check that already linked program can't be attached again. */
65cede72adSJiri Olsa 	link = bpf_program__attach(skel->progs.test_int_hook);
667bb2cc19SJiri Olsa 	if (!ASSERT_ERR_PTR(link, "attach_link"))
67cede72adSJiri Olsa 		return -1;
6803e54f10SKP Singh 
6903e54f10SKP Singh 	err = exec_cmd(&skel->bss->monitored_pid);
707bb2cc19SJiri Olsa 	if (!ASSERT_OK(err, "exec_cmd"))
71cede72adSJiri Olsa 		return err;
7203e54f10SKP Singh 
737bb2cc19SJiri Olsa 	ASSERT_EQ(skel->bss->bprm_count, 1, "bprm_count");
7403e54f10SKP Singh 
7503e54f10SKP Singh 	skel->bss->monitored_pid = getpid();
7603e54f10SKP Singh 
775222d696SKP Singh 	err = stack_mprotect();
78*2934565fSIlya Leoshkevich 	if (!ASSERT_EQ(err, -1, "stack_mprotect") ||
79*2934565fSIlya Leoshkevich 	    !ASSERT_EQ(errno, EPERM, "stack_mprotect"))
80cede72adSJiri Olsa 		return err;
8103e54f10SKP Singh 
827bb2cc19SJiri Olsa 	ASSERT_EQ(skel->bss->mprotect_count, 1, "mprotect_count");
8303e54f10SKP Singh 
84e68a1445SAlexei Starovoitov 	syscall(__NR_setdomainname, &buf, -2L);
85e68a1445SAlexei Starovoitov 	syscall(__NR_setdomainname, 0, -3L);
86e68a1445SAlexei Starovoitov 	syscall(__NR_setdomainname, ~0L, -4L);
87e68a1445SAlexei Starovoitov 
887bb2cc19SJiri Olsa 	ASSERT_EQ(skel->bss->copy_test, 3, "copy_test");
89e68a1445SAlexei Starovoitov 
90cede72adSJiri Olsa 	lsm__detach(skel);
91cede72adSJiri Olsa 
92cede72adSJiri Olsa 	skel->bss->copy_test = 0;
93cede72adSJiri Olsa 	skel->bss->bprm_count = 0;
94cede72adSJiri Olsa 	skel->bss->mprotect_count = 0;
95cede72adSJiri Olsa 	return 0;
96cede72adSJiri Olsa }
97cede72adSJiri Olsa 
test_test_lsm(void)98cede72adSJiri Olsa void test_test_lsm(void)
99cede72adSJiri Olsa {
100cede72adSJiri Olsa 	struct lsm *skel = NULL;
101cede72adSJiri Olsa 	int err;
102cede72adSJiri Olsa 
103cede72adSJiri Olsa 	skel = lsm__open_and_load();
1047bb2cc19SJiri Olsa 	if (!ASSERT_OK_PTR(skel, "lsm_skel_load"))
105cede72adSJiri Olsa 		goto close_prog;
106cede72adSJiri Olsa 
107cede72adSJiri Olsa 	err = test_lsm(skel);
1087bb2cc19SJiri Olsa 	if (!ASSERT_OK(err, "test_lsm_first_attach"))
109cede72adSJiri Olsa 		goto close_prog;
110cede72adSJiri Olsa 
111cede72adSJiri Olsa 	err = test_lsm(skel);
1127bb2cc19SJiri Olsa 	ASSERT_OK(err, "test_lsm_second_attach");
113cede72adSJiri Olsa 
11403e54f10SKP Singh close_prog:
11503e54f10SKP Singh 	lsm__destroy(skel);
11603e54f10SKP Singh }
117