2021-11-30 17:57:48 +01:00
|
|
|
#include "exile.h"
|
2021-06-05 14:07:11 +02:00
|
|
|
#include <stdbool.h>
|
2021-08-09 20:29:18 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2021-09-05 16:48:27 +02:00
|
|
|
#include <sys/wait.h>
|
2021-08-09 20:29:18 +02:00
|
|
|
|
2022-01-17 22:48:29 +01:00
|
|
|
#define LOG(...) do { fprintf(stdout, "%s(): ", __func__); fprintf(stdout, __VA_ARGS__); } while(0)
|
|
|
|
|
2021-11-30 17:57:48 +01:00
|
|
|
int xexile_enable_policy(struct exile_policy *policy)
|
2021-08-16 23:32:27 +02:00
|
|
|
{
|
2021-11-30 17:57:48 +01:00
|
|
|
int ret = exile_enable_policy(policy);
|
2021-08-16 23:32:27 +02:00
|
|
|
if(ret != 0)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("failed: %i\n", ret);
|
2021-08-16 23:32:27 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-05 16:53:39 +02:00
|
|
|
int test_default_main()
|
2021-06-05 14:07:11 +02:00
|
|
|
{
|
2021-11-30 17:57:48 +01:00
|
|
|
struct exile_policy *policy = exile_init_policy();
|
|
|
|
return xexile_enable_policy(policy);
|
2021-06-05 14:07:11 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 16:48:27 +02:00
|
|
|
static int test_expected_kill(int (*f)())
|
2021-06-05 14:07:11 +02:00
|
|
|
{
|
2021-09-05 16:48:27 +02:00
|
|
|
pid_t pid = fork();
|
|
|
|
if(pid == 0)
|
|
|
|
{
|
|
|
|
return f();
|
|
|
|
}
|
|
|
|
int status = 0;
|
|
|
|
waitpid(pid, &status, 0);
|
2021-08-16 23:32:27 +02:00
|
|
|
|
2021-09-05 16:48:27 +02:00
|
|
|
if(WIFSIGNALED(status))
|
|
|
|
{
|
|
|
|
int c = WTERMSIG(status);
|
|
|
|
if(c == SIGSYS)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Got expected signal\n");
|
2021-09-05 16:48:27 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Unexpected status code: %i\n", c);
|
2021-09-05 16:48:27 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int c = WEXITSTATUS(status);
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Process was not killed, test fails. Status code of exit: %i\n", c);
|
2021-09-05 16:48:27 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int test_successful_exit(int (*f)())
|
|
|
|
{
|
|
|
|
pid_t pid = fork();
|
|
|
|
if(pid == 0)
|
|
|
|
{
|
|
|
|
return f();
|
|
|
|
}
|
|
|
|
int status = 0;
|
|
|
|
waitpid(pid, &status, 0);
|
|
|
|
|
|
|
|
if(WIFSIGNALED(status))
|
|
|
|
{
|
|
|
|
int c = WTERMSIG(status);
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Received signal, which was not expected. Signal was: %i\n", c);
|
2021-09-05 16:48:27 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int c = WEXITSTATUS(status);
|
|
|
|
if(c != 0)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Process failed to exit properly. Status code is: %i\n", c);
|
2021-09-05 16:48:27 +02:00
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Process exited sucessfully as expected");
|
2021-09-05 16:48:27 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int do_test_seccomp_blacklisted()
|
|
|
|
{
|
2021-11-30 17:57:48 +01:00
|
|
|
struct exile_policy *policy = exile_init_policy();
|
2021-11-21 15:28:46 +01:00
|
|
|
exile_append_syscall_policy(policy,EXILE_SYS(getuid), EXILE_SYSCALL_DENY_KILL_PROCESS, NULL, 0);
|
2021-11-30 17:57:48 +01:00
|
|
|
exile_append_syscall_default_policy(policy, EXILE_SYSCALL_ALLOW);
|
2021-08-16 23:32:27 +02:00
|
|
|
|
2021-11-30 17:57:48 +01:00
|
|
|
xexile_enable_policy(policy);
|
2021-08-11 20:54:40 +02:00
|
|
|
|
2021-11-21 15:28:46 +01:00
|
|
|
uid_t pid = syscall(EXILE_SYS(geteuid));
|
|
|
|
pid = syscall(EXILE_SYS(getuid));
|
2021-06-05 14:07:11 +02:00
|
|
|
return 0;
|
2021-09-05 16:48:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
}
|
2021-09-05 16:53:39 +02:00
|
|
|
int test_seccomp_blacklisted()
|
2021-09-05 16:48:27 +02:00
|
|
|
{
|
|
|
|
return test_expected_kill(&do_test_seccomp_blacklisted);
|
2021-06-05 14:07:11 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 16:48:27 +02:00
|
|
|
|
|
|
|
static int do_test_seccomp_blacklisted_call_permitted()
|
2021-06-05 14:07:11 +02:00
|
|
|
{
|
2021-11-30 17:57:48 +01:00
|
|
|
struct exile_policy *policy = exile_init_policy();
|
2021-08-11 20:54:40 +02:00
|
|
|
|
2021-11-21 15:28:46 +01:00
|
|
|
exile_append_syscall_policy(policy, EXILE_SYS(getuid), EXILE_SYSCALL_DENY_KILL_PROCESS, NULL, 0);
|
2021-11-30 17:57:48 +01:00
|
|
|
exile_append_syscall_default_policy(policy, EXILE_SYSCALL_ALLOW);
|
2021-08-11 20:54:40 +02:00
|
|
|
|
2021-11-30 17:57:48 +01:00
|
|
|
xexile_enable_policy(policy);
|
2021-06-05 14:07:11 +02:00
|
|
|
//geteuid is not blacklisted, so must succeed
|
2021-11-21 15:28:46 +01:00
|
|
|
uid_t pid = syscall(EXILE_SYS(geteuid));
|
2021-06-05 14:07:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-05 16:48:27 +02:00
|
|
|
|
2021-09-05 16:53:39 +02:00
|
|
|
int test_seccomp_blacklisted_call_permitted()
|
2021-09-05 16:48:27 +02:00
|
|
|
{
|
|
|
|
return test_successful_exit(&do_test_seccomp_blacklisted_call_permitted);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_test_seccomp_x32_kill()
|
2021-08-12 12:25:12 +02:00
|
|
|
{
|
2021-11-30 17:57:48 +01:00
|
|
|
struct exile_policy *policy = exile_init_policy();
|
2021-08-12 12:25:12 +02:00
|
|
|
|
2021-11-21 15:28:46 +01:00
|
|
|
exile_append_syscall_policy(policy, EXILE_SYS(getuid), EXILE_SYSCALL_DENY_KILL_PROCESS, NULL, 0);
|
2021-11-30 17:57:48 +01:00
|
|
|
exile_append_syscall_default_policy(policy, EXILE_SYSCALL_ALLOW);
|
2021-08-12 12:25:12 +02:00
|
|
|
|
2021-11-30 17:57:48 +01:00
|
|
|
xexile_enable_policy(policy);
|
2021-08-12 12:25:12 +02:00
|
|
|
|
|
|
|
/* Attempt to bypass by falling back to x32 should be blocked */
|
2021-11-30 17:57:48 +01:00
|
|
|
syscall(EXILE_SYS(getuid)+__X32_SYSCALL_BIT);
|
2021-08-12 21:58:45 +02:00
|
|
|
|
2021-08-12 12:25:12 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2021-08-16 23:32:27 +02:00
|
|
|
|
2021-09-05 16:53:39 +02:00
|
|
|
int test_seccomp_x32_kill()
|
2021-09-05 16:48:27 +02:00
|
|
|
{
|
|
|
|
return test_expected_kill(&do_test_seccomp_x32_kill);
|
|
|
|
}
|
|
|
|
|
2021-08-16 23:32:27 +02:00
|
|
|
/* Tests whether seccomp rules end with a policy matching all syscalls */
|
2021-09-05 16:53:39 +02:00
|
|
|
int test_seccomp_require_last_matchall()
|
2021-08-16 23:32:27 +02:00
|
|
|
{
|
2021-11-30 17:57:48 +01:00
|
|
|
struct exile_policy *policy = exile_init_policy();
|
2021-08-16 23:32:27 +02:00
|
|
|
|
2021-11-21 15:28:46 +01:00
|
|
|
exile_append_syscall_policy(policy, EXILE_SYS(getuid), EXILE_SYSCALL_DENY_KILL_PROCESS, NULL, 0);
|
2021-08-16 23:32:27 +02:00
|
|
|
|
2021-11-30 17:57:48 +01:00
|
|
|
int status = exile_enable_policy(policy);
|
2021-09-05 16:48:27 +02:00
|
|
|
if(status == 0)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed. Should not have been enabled!");
|
2021-09-05 16:48:27 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2021-08-16 23:32:27 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 16:48:27 +02:00
|
|
|
static int do_test_seccomp_errno()
|
2021-09-05 12:31:16 +02:00
|
|
|
{
|
2021-11-30 17:57:48 +01:00
|
|
|
struct exile_policy *policy = exile_init_policy();
|
2021-09-05 12:31:16 +02:00
|
|
|
|
2021-11-21 15:28:46 +01:00
|
|
|
exile_append_syscall_policy(policy, EXILE_SYS(close),EXILE_SYSCALL_DENY_RET_ERROR, NULL, 0);
|
2021-11-30 17:57:48 +01:00
|
|
|
exile_append_syscall_default_policy(policy, EXILE_SYSCALL_ALLOW);
|
2021-09-05 12:31:16 +02:00
|
|
|
|
2021-11-30 17:57:48 +01:00
|
|
|
xexile_enable_policy(policy);
|
2021-11-21 15:28:46 +01:00
|
|
|
uid_t id = syscall(EXILE_SYS(getuid));
|
2021-09-05 12:31:16 +02:00
|
|
|
|
2021-11-21 15:28:46 +01:00
|
|
|
int fd = syscall(EXILE_SYS(close), 0);
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("close() return code: %i, errno: %s\n", fd, strerror(errno));
|
2021-09-05 12:31:16 +02:00
|
|
|
return fd == -1 ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
2021-09-05 16:48:27 +02:00
|
|
|
|
|
|
|
|
2021-09-05 16:53:39 +02:00
|
|
|
int test_seccomp_errno()
|
2021-09-05 16:48:27 +02:00
|
|
|
{
|
|
|
|
return test_successful_exit(&do_test_seccomp_errno);
|
|
|
|
}
|
|
|
|
|
2021-11-21 15:28:46 +01:00
|
|
|
int test_seccomp_argfilter_allowed()
|
|
|
|
{
|
|
|
|
struct exile_policy *policy = exile_init_policy();
|
|
|
|
|
2021-12-05 17:28:58 +01:00
|
|
|
struct sock_filter argfilter[2] =
|
2021-11-21 15:28:46 +01:00
|
|
|
{
|
|
|
|
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, (offsetof(struct seccomp_data, args[1]))),
|
|
|
|
BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, O_WRONLY, 0, EXILE_SYSCALL_EXIT_BPF_NO_MATCH)
|
|
|
|
};
|
2021-12-05 17:28:58 +01:00
|
|
|
|
2021-11-21 15:28:46 +01:00
|
|
|
exile_append_syscall_policy(policy, EXILE_SYS(open),EXILE_SYSCALL_DENY_RET_ERROR, argfilter, 2);
|
|
|
|
exile_append_syscall_default_policy(policy, EXILE_SYSCALL_ALLOW);
|
|
|
|
xexile_enable_policy(policy);
|
|
|
|
|
|
|
|
|
|
|
|
char *t = "/dev/random";
|
|
|
|
int ret = (int) syscall(EXILE_SYS(open),t, O_RDONLY);
|
|
|
|
|
|
|
|
if(ret == -1)
|
|
|
|
{
|
|
|
|
printf("Failed: open was expected to succeed, but returned %i\n", ret);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int test_seccomp_argfilter_filtered()
|
|
|
|
{
|
|
|
|
struct exile_policy *policy = exile_init_policy();
|
|
|
|
|
2021-12-05 17:28:58 +01:00
|
|
|
struct sock_filter argfilter[2] =
|
2021-11-21 15:28:46 +01:00
|
|
|
{
|
|
|
|
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, (offsetof(struct seccomp_data, args[1]))),
|
|
|
|
BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, O_WRONLY, 0, EXILE_SYSCALL_EXIT_BPF_NO_MATCH)
|
|
|
|
};
|
|
|
|
|
|
|
|
exile_append_syscall_policy(policy, EXILE_SYS(open),EXILE_SYSCALL_DENY_RET_ERROR, argfilter, 2);
|
|
|
|
exile_append_syscall_default_policy(policy, EXILE_SYSCALL_ALLOW);
|
|
|
|
xexile_enable_policy(policy);
|
|
|
|
|
|
|
|
char *t = "/dev/random";
|
|
|
|
int ret = (int) syscall(EXILE_SYS(open),t, O_WRONLY);
|
|
|
|
|
|
|
|
if(ret != -1)
|
|
|
|
{
|
|
|
|
printf("Failed: open was expected to fail, but returned %i\n", ret);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int test_seccomp_argfilter_mixed()
|
|
|
|
{
|
|
|
|
struct exile_policy *policy = exile_init_policy();
|
|
|
|
|
2021-12-05 17:28:58 +01:00
|
|
|
struct sock_filter argfilter[2] =
|
2021-11-21 15:28:46 +01:00
|
|
|
{
|
|
|
|
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, (offsetof(struct seccomp_data, args[1]))),
|
|
|
|
BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, O_WRONLY, 0, EXILE_SYSCALL_EXIT_BPF_NO_MATCH)
|
|
|
|
};
|
|
|
|
|
|
|
|
exile_append_syscall_policy(policy, EXILE_SYS(stat),EXILE_SYSCALL_DENY_RET_ERROR, NULL,0);
|
|
|
|
exile_append_syscall_policy(policy, EXILE_SYS(open),EXILE_SYSCALL_DENY_RET_ERROR, argfilter, 2);
|
|
|
|
exile_append_syscall_policy(policy, EXILE_SYS(getpid),EXILE_SYSCALL_DENY_RET_ERROR, NULL, 0);
|
|
|
|
|
|
|
|
exile_append_syscall_default_policy(policy, EXILE_SYSCALL_ALLOW);
|
|
|
|
xexile_enable_policy(policy);
|
|
|
|
|
|
|
|
struct stat statbuf;
|
|
|
|
int s = (int) syscall(EXILE_SYS(stat), "/dev/urandom", &statbuf);
|
|
|
|
if(s != -1)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: stat was expected to fail, but returned %i\n", s);
|
2021-11-21 15:28:46 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t p = (pid_t) syscall(EXILE_SYS(getpid));
|
|
|
|
if(p != -1)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: getpid was expected to fail, but returned %i\n", p);
|
2021-11-21 15:28:46 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *t = "/dev/random";
|
|
|
|
int ret = (int) syscall(EXILE_SYS(open),t, O_WRONLY);
|
|
|
|
if(ret != -1)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: open was expected to fail, but returned %i\n", ret);
|
2021-11-21 15:28:46 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
ret = (int) syscall(EXILE_SYS(open), t, O_RDONLY);
|
|
|
|
if(ret == -1)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: open with O_RDONLY was expected to succeed, but returned %i\n", ret);
|
2021-09-19 15:23:41 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-05 17:28:58 +01:00
|
|
|
|
2021-12-28 10:56:48 +01:00
|
|
|
int do_test_seccomp_vow_socket()
|
2021-12-05 17:28:58 +01:00
|
|
|
{
|
|
|
|
struct exile_policy *policy = exile_init_policy();
|
2021-12-28 10:56:48 +01:00
|
|
|
policy->vow_promises = EXILE_SYSCALL_VOW_STDIO | EXILE_SYSCALL_VOW_INET | EXILE_SYSCALL_VOW_DENY_ERROR;
|
2021-12-05 17:28:58 +01:00
|
|
|
xexile_enable_policy(policy);
|
|
|
|
|
|
|
|
int s = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if(s == -1)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: socket was expected to succeed, but returned %i\n", s);
|
2021-12-05 17:28:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
s = socket(AF_UNIX, SOCK_DGRAM, 0);
|
|
|
|
if(s != -1)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: socket was expected to fail, but returned %i\n", s);
|
2021-12-05 17:28:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-28 10:56:48 +01:00
|
|
|
int do_test_seccomp_vow_open()
|
2021-12-05 17:28:58 +01:00
|
|
|
{
|
|
|
|
struct exile_policy *policy = exile_init_policy();
|
2021-12-28 10:56:48 +01:00
|
|
|
policy->vow_promises = EXILE_SYSCALL_VOW_STDIO | EXILE_SYSCALL_VOW_RPATH | EXILE_SYSCALL_VOW_DENY_ERROR;
|
2021-12-05 17:28:58 +01:00
|
|
|
xexile_enable_policy(policy);
|
|
|
|
|
|
|
|
int ret = open("/dev/urandom", O_WRONLY | O_APPEND);
|
|
|
|
if(ret != -1)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: open was expected to fail, but returned %i\n", ret);
|
2021-12-05 17:28:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
ret = open("/dev/urandom", O_RDWR);
|
|
|
|
if(ret != -1)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: open O_RDWR was expected to fail, but returned %i\n", ret);
|
2021-12-05 17:28:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
ret = open("/dev/urandom", O_RDONLY);
|
|
|
|
if(ret == -1)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: open was expected to succceed, but returned %i\n", ret);
|
2021-12-05 17:28:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-28 10:56:48 +01:00
|
|
|
int test_seccomp_vow()
|
2021-12-05 17:28:58 +01:00
|
|
|
{
|
2021-12-28 10:56:48 +01:00
|
|
|
int ret = test_successful_exit(&do_test_seccomp_vow_open);
|
2021-12-05 17:28:58 +01:00
|
|
|
if(ret != 0)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: do_test_seccomp_vow_open()\n");
|
2021-12-05 17:28:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2021-12-28 10:56:48 +01:00
|
|
|
ret = test_successful_exit(&do_test_seccomp_vow_socket);
|
2021-12-05 17:28:58 +01:00
|
|
|
if(ret != 0)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: do_test_seccomp_vow_socket()\n");
|
2021-12-05 17:28:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-28 10:56:48 +01:00
|
|
|
int test_seccomp_exile_vow_multiple()
|
2021-12-27 12:00:31 +01:00
|
|
|
{
|
|
|
|
|
2021-12-28 10:56:48 +01:00
|
|
|
int ret = exile_vow(EXILE_SYSCALL_VOW_STDIO | EXILE_SYSCALL_VOW_UNIX | EXILE_SYSCALL_VOW_SECCOMP_INSTALL | EXILE_SYSCALL_VOW_DENY_ERROR);
|
2021-12-27 12:00:31 +01:00
|
|
|
if(ret != 0)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: exile_vow() call 1 failed\n");
|
2021-12-27 12:00:31 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int s = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if(s == -1)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: socket was expected to succeed, but returned %i\n", s);
|
2021-12-27 12:00:31 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Let's take away unix sockets, so it should not be possible anymore */
|
2021-12-28 10:56:48 +01:00
|
|
|
ret = exile_vow(EXILE_SYSCALL_VOW_STDIO | EXILE_SYSCALL_VOW_SECCOMP_INSTALL | EXILE_SYSCALL_VOW_DENY_ERROR);
|
2021-12-27 12:00:31 +01:00
|
|
|
if(ret != 0)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: exile_vow() call 2 failed\n");
|
2021-12-27 12:00:31 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
s = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if(s != -1)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: socket was expected to fail, but returned %i\n", s);
|
2021-12-27 12:00:31 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Let's try to regain unix sockets again */
|
2021-12-28 10:56:48 +01:00
|
|
|
ret = exile_vow(EXILE_SYSCALL_VOW_STDIO | EXILE_SYSCALL_VOW_UNIX | EXILE_SYSCALL_VOW_SECCOMP_INSTALL | EXILE_SYSCALL_VOW_DENY_ERROR);
|
2021-12-27 12:00:31 +01:00
|
|
|
if(ret != 0)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: exile_vow() call 3 failed\n");
|
2021-12-27 12:00:31 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
s = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if(s != -1)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: socket was still expected to fail, but returned %i\n", s);
|
2021-12-27 12:00:31 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-20 17:03:04 +01:00
|
|
|
#if HAVE_LANDLOCK == 1
|
2021-09-05 16:53:39 +02:00
|
|
|
int test_landlock()
|
2021-06-05 14:07:11 +02:00
|
|
|
{
|
2021-12-27 16:51:06 +01:00
|
|
|
if(!exile_landlock_is_available())
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("landlock not available, so cannot test\n");
|
2021-12-27 16:51:06 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2021-11-30 17:57:48 +01:00
|
|
|
struct exile_policy *policy = exile_init_policy();
|
2022-01-15 19:32:12 +01:00
|
|
|
exile_append_path_policies(policy, EXILE_FS_ALLOW_ALL_READ, "/proc/self/fd");
|
2021-11-30 17:57:48 +01:00
|
|
|
xexile_enable_policy(policy);
|
2021-11-20 16:56:19 +01:00
|
|
|
|
2021-06-05 14:07:11 +02:00
|
|
|
int fd = open("/", O_RDONLY | O_CLOEXEC);
|
|
|
|
if(fd < 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-09-05 16:53:39 +02:00
|
|
|
int test_landlock_deny_write()
|
2021-06-05 14:07:11 +02:00
|
|
|
{
|
2021-11-30 17:57:48 +01:00
|
|
|
struct exile_policy *policy = exile_init_policy();
|
2022-01-15 19:32:12 +01:00
|
|
|
exile_append_path_policies(policy, EXILE_FS_ALLOW_ALL_READ, "/tmp/");
|
2021-11-30 17:57:48 +01:00
|
|
|
xexile_enable_policy(policy);
|
2021-11-20 16:56:19 +01:00
|
|
|
|
2021-06-05 14:07:11 +02:00
|
|
|
int fd = open("/tmp/a", O_WRONLY | O_CLOEXEC);
|
|
|
|
if(fd < 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2021-11-20 17:03:04 +01:00
|
|
|
#else
|
|
|
|
int test_landlock()
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int test_landlock_deny_write()
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
#endif
|
2021-06-05 14:07:11 +02:00
|
|
|
|
2021-09-05 16:53:39 +02:00
|
|
|
int test_nofs()
|
2021-08-09 20:29:18 +02:00
|
|
|
{
|
2021-11-30 17:57:48 +01:00
|
|
|
struct exile_policy *policy = exile_init_policy();
|
2021-08-09 20:29:18 +02:00
|
|
|
policy->no_fs = 1;
|
2021-11-30 17:57:48 +01:00
|
|
|
xexile_enable_policy(policy);
|
2021-08-09 20:29:18 +02:00
|
|
|
|
|
|
|
int s = socket(AF_INET,SOCK_STREAM,0);
|
|
|
|
if(s == -1)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed to open socket but this was not requested by policy\n");
|
2021-09-05 16:48:27 +02:00
|
|
|
return 1;
|
2021-08-09 20:29:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Expect seccomp to take care of this */
|
|
|
|
if(open("/test", O_CREAT | O_WRONLY) >= 0)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: We do not expect write access\n");
|
2021-09-05 16:48:27 +02:00
|
|
|
return 1;
|
2021-08-09 20:29:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-05 16:53:39 +02:00
|
|
|
int test_no_new_fds()
|
2021-08-09 20:29:18 +02:00
|
|
|
{
|
2021-11-30 17:57:48 +01:00
|
|
|
struct exile_policy *policy = exile_init_policy();
|
2021-08-09 20:29:18 +02:00
|
|
|
policy->no_new_fds = 1;
|
2021-11-30 17:57:48 +01:00
|
|
|
xexile_enable_policy(policy);
|
2021-08-09 20:29:18 +02:00
|
|
|
|
|
|
|
if(open("/tmp/test", O_CREAT | O_WRONLY) >= 0)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: Could open new file descriptor\n");
|
2021-08-09 20:29:18 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int s = socket(AF_INET,SOCK_STREAM,0);
|
|
|
|
if(s >= 0)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: socket got opened but policy denied\n");
|
2021-08-09 20:29:18 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-03-14 21:31:56 +01:00
|
|
|
extern int mkpath(const char *p, mode_t mode, int baseisfile);
|
2022-01-08 12:51:04 +01:00
|
|
|
int test_mkpath()
|
|
|
|
{
|
2022-01-15 19:39:31 +01:00
|
|
|
system("rm -rf /tmp/.exile.h/");
|
2022-01-08 12:51:04 +01:00
|
|
|
const char *filepath = "/tmp/.exile.h/test_mkpath/some/sub/dir/file";
|
|
|
|
const char *dirpath = "/tmp/.exile.h/test_mkpath/some/other/sub/dir";
|
|
|
|
int ret = mkpath(filepath, 0700, 1);
|
|
|
|
if(ret != 0)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: mkpath(file) returned: %i\n", ret);
|
2022-01-08 12:51:04 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
ret = mkpath(dirpath, 0700, 0);
|
|
|
|
if(ret != 0)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: mkpath(dirpath) returned: %i\n", ret);
|
2022-01-08 12:51:04 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat statbuf;
|
|
|
|
ret = stat(filepath, &statbuf);
|
|
|
|
if(ret != 0)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: stat on filepath returned: %i\n", ret);
|
2022-01-08 12:51:04 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if(!S_ISREG(statbuf.st_mode))
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: mkpath did not create a file: %i\n", ret);
|
2022-01-08 12:51:04 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
ret = stat(dirpath, &statbuf);
|
|
|
|
if(ret != 0)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: stat on dirpath returned: %i\n", ret);
|
2022-01-08 12:51:04 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if(!S_ISDIR(statbuf.st_mode))
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: mkpath did not create a directory: %i\n", ret);
|
2022-01-08 12:51:04 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2022-01-15 19:39:31 +01:00
|
|
|
system("rm -rf /tmp/.exile.h/");
|
2022-01-08 12:51:04 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-08 16:39:12 +01:00
|
|
|
int test_fail_flags()
|
|
|
|
{
|
|
|
|
struct exile_policy *policy = exile_init_policy();
|
2022-01-15 19:32:12 +01:00
|
|
|
exile_append_path_policies(policy, EXILE_FS_ALLOW_ALL_READ, "/nosuchpathexists");
|
2022-01-08 16:39:12 +01:00
|
|
|
int ret = exile_enable_policy(policy);
|
|
|
|
if(ret == 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed: A path that does not exist should have set the error flag %i\n", ret);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2022-01-08 12:51:04 +01:00
|
|
|
|
2022-03-14 22:26:22 +01:00
|
|
|
|
|
|
|
static int *read_pipe = NULL;
|
2022-01-14 23:38:42 +01:00
|
|
|
int do_launch_test(void *arg)
|
|
|
|
{
|
|
|
|
int num = *(int *)(arg);
|
|
|
|
num += 1;
|
2022-01-16 21:18:10 +01:00
|
|
|
char buffer[512] = { 0 };
|
2022-03-14 22:26:22 +01:00
|
|
|
read(*read_pipe, buffer, sizeof(buffer)-1);
|
2022-01-14 23:38:42 +01:00
|
|
|
printf("Sandboxed +1: %i\n", num);
|
2022-01-16 21:18:10 +01:00
|
|
|
printf("Echoing: %s\n", buffer);
|
2022-01-14 23:38:42 +01:00
|
|
|
fflush(stdout);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int test_launch()
|
|
|
|
{
|
|
|
|
struct exile_policy *policy = exile_init_policy();
|
|
|
|
struct exile_launch_params params = { 0 };
|
|
|
|
struct exile_launch_result res = {0};
|
|
|
|
int num = 22;
|
|
|
|
params.func = &do_launch_test;
|
|
|
|
params.funcarg = #
|
|
|
|
params.policy = policy;
|
2022-03-14 22:26:22 +01:00
|
|
|
read_pipe = ¶ms.child_write_pipe[0];
|
2022-01-14 23:38:42 +01:00
|
|
|
int launchfd = exile_launch(¶ms, &res);
|
|
|
|
if(launchfd < 0)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed to launch\n");
|
2022-01-14 23:38:42 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-01-16 21:18:10 +01:00
|
|
|
char buffer[4096] = { 0 };
|
|
|
|
write(res.write_fd, "1234", 4);
|
|
|
|
int s = read(res.read_fd, buffer, sizeof(buffer)-1);
|
2022-01-14 23:38:42 +01:00
|
|
|
write(1, buffer, s);
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Before wait, got: %i\n", s);
|
2022-01-14 23:38:42 +01:00
|
|
|
fflush(stdout);
|
2022-01-16 21:18:10 +01:00
|
|
|
if(strstr(buffer, "Echoing: 1234") == NULL)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Failed: Did not get back what we wrote\n");
|
2022-01-16 21:18:10 +01:00
|
|
|
}
|
2022-01-14 23:38:42 +01:00
|
|
|
int status = 0;
|
|
|
|
waitpid(res.tid, &status, __WALL);
|
|
|
|
if(WIFEXITED(status))
|
|
|
|
{
|
|
|
|
status = WEXITSTATUS(status);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#define LAUNCH_GET_TEST_STR "Control yourself. Take only what you need from it.\n"
|
|
|
|
int do_launch_get_test(void *a)
|
|
|
|
{
|
|
|
|
fprintf(stdout, LAUNCH_GET_TEST_STR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int test_launch_get()
|
|
|
|
{
|
|
|
|
struct exile_policy *policy = exile_init_policy();
|
|
|
|
struct exile_launch_params params = { 0 };
|
|
|
|
params.func = &do_launch_get_test;
|
|
|
|
params.funcarg = NULL;
|
|
|
|
params.policy = policy;
|
|
|
|
|
|
|
|
size_t n = 0;
|
|
|
|
char *content = exile_launch_get(¶ms, &n);
|
|
|
|
unsigned int len = strlen(LAUNCH_GET_TEST_STR);
|
|
|
|
if(n != strlen(LAUNCH_GET_TEST_STR))
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Lenght does does not match: %lu vs %u\n", n, len);
|
2022-01-14 23:38:42 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if(strcmp(content, LAUNCH_GET_TEST_STR) != 0)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Received content differs\n");
|
2022-01-14 23:38:42 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-17 22:42:26 +01:00
|
|
|
int test_vows_from_str()
|
|
|
|
{
|
|
|
|
uint64_t expected = EXILE_SYSCALL_VOW_CHOWN | EXILE_SYSCALL_VOW_WPATH | EXILE_SYSCALL_VOW_INET | EXILE_SYSCALL_VOW_DENY_ERROR;
|
|
|
|
uint64_t actual = exile_vows_from_str("chown wpath inet error");
|
|
|
|
if(expected != actual)
|
|
|
|
{
|
2022-01-17 22:48:29 +01:00
|
|
|
LOG("Masks don't match: %lu vs %lu\n", expected, actual);
|
2022-01-17 22:42:26 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-06-06 10:07:11 +02:00
|
|
|
int test_clone3_nosys()
|
|
|
|
{
|
|
|
|
struct exile_policy *policy = exile_init_policy();
|
|
|
|
policy->vow_promises = exile_vows_from_str("stdio rpath wpath cpath thread error");
|
|
|
|
|
|
|
|
exile_enable_policy(policy);
|
|
|
|
/* While args are invalid, it should never reach clone3 syscall handler, so it's irrelevant for
|
|
|
|
our test*/
|
|
|
|
long ret = syscall(__NR_clone3, NULL, 0);
|
|
|
|
|
|
|
|
if(ret == -1 && errno != ENOSYS)
|
|
|
|
{
|
|
|
|
LOG("clone3() was not allowed but did not return ENOSYS. It returned: %li, errno: %i\n", ret, errno);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-05 14:07:11 +02:00
|
|
|
struct dispatcher
|
|
|
|
{
|
|
|
|
char *name;
|
2021-09-05 16:53:39 +02:00
|
|
|
int (*f)();
|
2021-06-05 14:07:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct dispatcher dispatchers[] = {
|
2021-09-05 16:48:27 +02:00
|
|
|
{ "default", &test_default_main },
|
|
|
|
{ "seccomp-blacklisted", &test_seccomp_blacklisted},
|
|
|
|
{ "seccomp-blacklisted-permitted", &test_seccomp_blacklisted_call_permitted},
|
|
|
|
{ "seccomp-x32-kill", &test_seccomp_x32_kill},
|
|
|
|
{ "seccomp-require-last-matchall", &test_seccomp_require_last_matchall},
|
|
|
|
{ "seccomp-errno", &test_seccomp_errno},
|
2021-11-21 15:28:46 +01:00
|
|
|
{ "seccomp-argfilter-allowed", &test_seccomp_argfilter_allowed},
|
|
|
|
{ "seccomp-argfilter-filtered", &test_seccomp_argfilter_filtered},
|
|
|
|
{ "seccomp-argfilter-mixed", &test_seccomp_argfilter_mixed},
|
2021-12-28 10:56:48 +01:00
|
|
|
{ "seccomp-vow", &test_seccomp_vow},
|
|
|
|
{ "seccomp-vow-exile_vow-multi", &test_seccomp_exile_vow_multiple},
|
2021-09-05 16:48:27 +02:00
|
|
|
{ "landlock", &test_landlock},
|
|
|
|
{ "landlock-deny-write", &test_landlock_deny_write },
|
|
|
|
{ "no_fs", &test_nofs},
|
2022-01-08 12:51:04 +01:00
|
|
|
{ "no_new_fds", &test_no_new_fds},
|
|
|
|
{ "mkpath", &test_mkpath},
|
2022-01-08 16:39:12 +01:00
|
|
|
{ "failflags", &test_fail_flags},
|
2022-01-14 23:38:42 +01:00
|
|
|
{ "launch", &test_launch},
|
|
|
|
{ "launch-get", &test_launch_get},
|
2022-01-17 22:42:26 +01:00
|
|
|
{ "vow_from_str", &test_vows_from_str},
|
2022-06-06 10:07:11 +02:00
|
|
|
{ "clone3_nosys", &test_clone3_nosys},
|
2021-06-05 14:07:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
if(argc < 2)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: %s [testname]\n", argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
char *test = argv[1];
|
|
|
|
if(strcmp(test, "--dumptests") == 0)
|
|
|
|
{
|
|
|
|
for(unsigned int i = 0; i < sizeof(dispatchers)/sizeof(dispatchers[0]); i++)
|
|
|
|
{
|
2021-09-05 16:48:27 +02:00
|
|
|
printf("%s\n", dispatchers[i].name);
|
2021-06-05 14:07:11 +02:00
|
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < sizeof(dispatchers)/sizeof(dispatchers[0]); i++)
|
|
|
|
{
|
|
|
|
struct dispatcher *current = &dispatchers[i];
|
|
|
|
if(strcmp(current->name, test) == 0)
|
|
|
|
{
|
2021-09-05 16:53:39 +02:00
|
|
|
return current->f();
|
2021-06-05 14:07:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(stderr, "Unknown test\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|