···
5
+
#include <stdnoreturn.h>
#include <linux/capability.h>
···
19
-
// Make sure assertions are not compiled out, we use them to codify
20
-
// invariants about this program and we want it to fail fast and
21
-
// loudly if they are violated.
19
+
#define ASSERT(expr) ((expr) ? (void) 0 : assert_failure(#expr))
···
38
+
static noreturn void assert_failure(const char *assertion) {
39
+
fprintf(stderr, "Assertion `%s` in NixOS's wrapper.c failed.\n", assertion);
int get_last_cap(unsigned *last_cap) {
FILE* file = fopen("/proc/sys/kernel/cap_last_cap", "r");
···
int len = strlen(wrapper_dir);
if (len > 0 && '/' == wrapper_dir[len - 1])
184
-
assert(!strncmp(self_path, wrapper_dir, len));
185
-
assert('/' == wrapper_dir[0]);
186
-
assert('/' == self_path[len]);
187
+
ASSERT(!strncmp(self_path, wrapper_dir, len));
188
+
ASSERT('/' == wrapper_dir[0]);
189
+
ASSERT('/' == self_path[len]);
// Make *really* *really* sure that we were executed as
// `self_path', and not, say, as some other setuid program. That
// is, our effective uid/gid should match the uid/gid of
193
-
assert(lstat(self_path, &st) != -1);
196
+
ASSERT(lstat(self_path, &st) != -1);
195
-
assert(!(st.st_mode & S_ISUID) || (st.st_uid == geteuid()));
196
-
assert(!(st.st_mode & S_ISGID) || (st.st_gid == getegid()));
198
+
ASSERT(!(st.st_mode & S_ISUID) || (st.st_uid == geteuid()));
199
+
ASSERT(!(st.st_mode & S_ISGID) || (st.st_gid == getegid()));
// And, of course, we shouldn't be writable.
199
-
assert(!(st.st_mode & (S_IWGRP | S_IWOTH)));
202
+
ASSERT(!(st.st_mode & (S_IWGRP | S_IWOTH)));
// Read the path of the real (wrapped) program from <self>.real.
char real_fn[PATH_MAX + 10];
int real_fn_size = snprintf(real_fn, sizeof(real_fn), "%s.real", self_path);
204
-
assert(real_fn_size < sizeof(real_fn));
207
+
ASSERT(real_fn_size < sizeof(real_fn));
int fd_self = open(real_fn, O_RDONLY);
207
-
assert(fd_self != -1);
210
+
ASSERT(fd_self != -1);
char source_prog[PATH_MAX];
len = read(fd_self, source_prog, PATH_MAX);
212
-
assert(len < sizeof(source_prog));
215
+
ASSERT(len < sizeof(source_prog));