| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <unistd.h> |
| 6 |
|
| 7 |
#include "log.h" |
| 8 |
#include "namespace.h" |
| 9 |
#include "tests.h" |
| 10 |
|
| 11 |
int main(int argc, char *argv[]) { |
| 12 |
const char * const writable[] = { |
| 13 |
"/home", |
| 14 |
"-/home/lennart/projects/foobar", /* this should be masked automatically */ |
| 15 |
NULL |
| 16 |
}; |
| 17 |
|
| 18 |
const char * const readonly[] = { |
| 19 |
/* "/", */ |
| 20 |
/* "/usr", */ |
| 21 |
"/boot", |
| 22 |
"/lib", |
| 23 |
"/usr/lib", |
| 24 |
"-/lib64", |
| 25 |
"-/usr/lib64", |
| 26 |
NULL |
| 27 |
}; |
| 28 |
|
| 29 |
const char * const exec[] = { |
| 30 |
"/lib", |
| 31 |
"/usr", |
| 32 |
"-/lib64", |
| 33 |
"-/usr/lib64", |
| 34 |
NULL |
| 35 |
}; |
| 36 |
|
| 37 |
const char * const no_exec[] = { |
| 38 |
"/var", |
| 39 |
NULL |
| 40 |
}; |
| 41 |
|
| 42 |
const char *inaccessible[] = { |
| 43 |
"/home/lennart/projects", |
| 44 |
NULL |
| 45 |
}; |
| 46 |
|
| 47 |
static const BindMount bind_mount = { |
| 48 |
.source = (char*) "/usr/bin", |
| 49 |
.destination = (char*) "/etc/systemd", |
| 50 |
.read_only = true, |
| 51 |
}; |
| 52 |
|
| 53 |
static const TemporaryFileSystem tmpfs = { |
| 54 |
.path = (char*) "/var", |
| 55 |
.options = (char*) "ro", |
| 56 |
}; |
| 57 |
|
| 58 |
char *root_directory; |
| 59 |
char *projects_directory; |
| 60 |
int r; |
| 61 |
char tmp_dir[] = "/tmp/systemd-private-XXXXXX", |
| 62 |
var_tmp_dir[] = "/var/tmp/systemd-private-XXXXXX"; |
| 63 |
|
| 64 |
test_setup_logging(LOG_DEBUG); |
| 65 |
|
| 66 |
assert_se(mkdtemp(tmp_dir)); |
| 67 |
assert_se(mkdtemp(var_tmp_dir)); |
| 68 |
|
| 69 |
root_directory = getenv("TEST_NS_CHROOT"); |
| 70 |
projects_directory = getenv("TEST_NS_PROJECTS"); |
| 71 |
|
| 72 |
if (projects_directory) |
| 73 |
inaccessible[0] = projects_directory; |
| 74 |
|
| 75 |
log_info("Inaccessible directory: '%s'", inaccessible[0]); |
| 76 |
if (root_directory) |
| 77 |
log_info("Chroot: '%s'", root_directory); |
| 78 |
else |
| 79 |
log_info("Not chrooted"); |
| 80 |
|
| 81 |
_cleanup_(pinned_resource_done) PinnedResource pr = PINNED_RESOURCE_NULL; |
| 82 |
|
| 83 |
if (root_directory) { |
| 84 |
pr.directory_fd = open(root_directory, O_PATH|O_CLOEXEC|O_DIRECTORY); |
| 85 |
assert_se(pr.directory_fd >= 0); |
| 86 |
|
| 87 |
pr.directory = strdup(root_directory); |
| 88 |
assert_se(pr.directory); |
| 89 |
} |
| 90 |
|
| 91 |
NamespaceParameters p = { |
| 92 |
.runtime_scope = RUNTIME_SCOPE_SYSTEM, |
| 93 |
|
| 94 |
.rootfs = &pr, |
| 95 |
|
| 96 |
.read_write_paths = (char**) writable, |
| 97 |
.read_only_paths = (char**) readonly, |
| 98 |
.inaccessible_paths = (char**) inaccessible, |
| 99 |
|
| 100 |
.exec_paths = (char**) exec, |
| 101 |
.no_exec_paths = (char**) no_exec, |
| 102 |
|
| 103 |
.tmp_dir = tmp_dir, |
| 104 |
.var_tmp_dir = var_tmp_dir, |
| 105 |
|
| 106 |
.bind_mounts = &bind_mount, |
| 107 |
.n_bind_mounts = 1, |
| 108 |
|
| 109 |
.temporary_filesystems = &tmpfs, |
| 110 |
.n_temporary_filesystems = 1, |
| 111 |
|
| 112 |
.private_dev = true, |
| 113 |
.protect_control_groups = true, |
| 114 |
.protect_kernel_tunables = true, |
| 115 |
.protect_kernel_modules = true, |
| 116 |
.protect_proc = PROTECT_PROC_NOACCESS, |
| 117 |
.proc_subset = PROC_SUBSET_PID, |
| 118 |
}; |
| 119 |
|
| 120 |
r = setup_namespace(&p, NULL); |
| 121 |
if (r < 0) { |
| 122 |
log_error_errno(r, "Failed to set up namespace: %m"); |
| 123 |
|
| 124 |
log_info("Usage:\n" |
| 125 |
" sudo TEST_NS_PROJECTS=/home/lennart/projects ./test-ns\n" |
| 126 |
" sudo TEST_NS_CHROOT=/home/alban/debian-tree TEST_NS_PROJECTS=/home/alban/debian-tree/home/alban/Documents ./test-ns"); |
| 127 |
|
| 128 |
return 1; |
| 129 |
} |
| 130 |
|
| 131 |
execl("/bin/sh", "/bin/sh", NULL); |
| 132 |
log_error_errno(errno, "execl(): %m"); |
| 133 |
|
| 134 |
return 1; |
| 135 |
} |