···
#include <linux/capability.h>
···
-
// Make sure assertions are not compiled out, we use them to codify
-
// invariants about this program and we want it to fail fast and
-
// loudly if they are violated.
···
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])
-
assert(!strncmp(self_path, wrapper_dir, len));
-
assert('/' == wrapper_dir[0]);
-
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
-
assert(lstat(self_path, &st) != -1);
-
assert(!(st.st_mode & S_ISUID) || (st.st_uid == geteuid()));
-
assert(!(st.st_mode & S_ISGID) || (st.st_gid == getegid()));
// And, of course, we shouldn't be writable.
-
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);
-
assert(real_fn_size < sizeof(real_fn));
int fd_self = open(real_fn, O_RDONLY);
char source_prog[PATH_MAX];
len = read(fd_self, source_prog, PATH_MAX);
-
assert(len < sizeof(source_prog));
···
+
#include <stdnoreturn.h>
#include <linux/capability.h>
···
+
#define ASSERT(expr) ((expr) ? (void) 0 : assert_failure(#expr))
···
+
static noreturn void assert_failure(const char *assertion) {
+
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])
+
ASSERT(!strncmp(self_path, wrapper_dir, len));
+
ASSERT('/' == wrapper_dir[0]);
+
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
+
ASSERT(lstat(self_path, &st) != -1);
+
ASSERT(!(st.st_mode & S_ISUID) || (st.st_uid == geteuid()));
+
ASSERT(!(st.st_mode & S_ISGID) || (st.st_gid == getegid()));
// And, of course, we shouldn't be writable.
+
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);
+
ASSERT(real_fn_size < sizeof(real_fn));
int fd_self = open(real_fn, O_RDONLY);
char source_prog[PATH_MAX];
len = read(fd_self, source_prog, PATH_MAX);
+
ASSERT(len < sizeof(source_prog));