2020-08-21 13:36:07 -07:00
|
|
|
/* This program requires CAP_SYS_ADMIN */
|
2020-06-18 03:21:55 -07:00
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <sched.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/capability.h>
|
|
|
|
|
2020-08-21 13:36:07 -07:00
|
|
|
static char *allowed_netns[] = {
|
2020-07-07 03:30:08 -07:00
|
|
|
"nb-lightning-loop",
|
2020-04-23 09:18:47 -07:00
|
|
|
"nb-liquidd",
|
|
|
|
"nb-joinmarket"
|
2020-06-18 03:21:55 -07:00
|
|
|
};
|
|
|
|
|
2020-08-21 13:36:07 -07:00
|
|
|
int is_netns_allowed(char *netns) {
|
|
|
|
int n_allowed_netns = sizeof(allowed_netns) / sizeof(allowed_netns[0]);
|
|
|
|
for (int i = 0; i < n_allowed_netns; i++) {
|
|
|
|
if (strcmp(allowed_netns[i], netns) == 0) {
|
2020-06-18 03:21:55 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_capabilities() {
|
|
|
|
cap_t caps = cap_get_proc();
|
|
|
|
printf("Capabilities: %s\n", cap_to_text(caps, NULL));
|
|
|
|
cap_free(caps);
|
|
|
|
}
|
2020-08-21 13:36:07 -07:00
|
|
|
|
2020-06-18 03:21:55 -07:00
|
|
|
void drop_capabilities() {
|
|
|
|
cap_t caps = cap_get_proc();
|
|
|
|
cap_clear(caps);
|
|
|
|
cap_set_proc(caps);
|
|
|
|
cap_free(caps);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
char netns_path[256];
|
|
|
|
|
|
|
|
if (argc < 3) {
|
2020-08-21 13:36:07 -07:00
|
|
|
printf("usage: %s <netns> <command>\n", argv[0]);
|
2020-06-18 03:21:55 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-08-21 13:36:07 -07:00
|
|
|
if (!is_netns_allowed(argv[1])) {
|
|
|
|
printf("%s is not an allowed netns.\n", argv[1]);
|
2020-06-18 03:21:55 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(snprintf(netns_path, sizeof(netns_path), "/var/run/netns/%s", argv[1]) < 0) {
|
2020-08-21 13:36:07 -07:00
|
|
|
printf("Path length exceeded for netns %s.\n", argv[1]);
|
2020-06-18 03:21:55 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-08-21 13:36:07 -07:00
|
|
|
int fd = open(netns_path, O_RDONLY);
|
2020-06-18 03:21:55 -07:00
|
|
|
if (fd < 0) {
|
|
|
|
printf("Failed opening netns %s: %d, %s \n", netns_path, errno, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setns(fd, CLONE_NEWNET) < 0) {
|
|
|
|
printf("Failed setns %d, %s \n", errno, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Drop capabilities */
|
|
|
|
#ifdef DEBUG
|
|
|
|
print_capabilities();
|
|
|
|
#endif
|
|
|
|
drop_capabilities();
|
|
|
|
#ifdef DEBUG
|
|
|
|
print_capabilities();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
execvp(argv[2], &argv[2]);
|
|
|
|
return 0;
|
|
|
|
}
|