Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Commit

Permalink
Exec wshd in context of the container
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy Kalinin and Pieter Noordhuis committed Mar 15, 2013
1 parent 7d7206f commit 8de5d03
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 10 deletions.
3 changes: 3 additions & 0 deletions warden/root/linux/skeleton/lib/hook-parent-before-clone.sh
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ cd $(dirname $0)/../
source ./lib/common.sh source ./lib/common.sh


setup_fs setup_fs

cp bin/wshd mnt/sbin/wshd
chmod 700 mnt/sbin/wshd
8 changes: 5 additions & 3 deletions warden/src/wsh/barrier.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ int barrier_open(barrier_t *bar) {
goto err; goto err;
} }


fcntl_mix_cloexec(aux[0]);
fcntl_mix_cloexec(aux[1]);

bar->fd[0] = aux[0]; bar->fd[0] = aux[0];
bar->fd[1] = aux[1]; bar->fd[1] = aux[1];
return 0; return 0;
Expand All @@ -36,6 +33,11 @@ void barrier_close(barrier_t *bar) {
close(bar->fd[1]); close(bar->fd[1]);
} }


void barrier_mix_cloexec(barrier_t *bar) {
fcntl_mix_cloexec(bar->fd[0]);
fcntl_mix_cloexec(bar->fd[1]);
}

void barrier_close_wait(barrier_t *bar) { void barrier_close_wait(barrier_t *bar) {
close(bar->fd[0]); close(bar->fd[0]);
} }
Expand Down
1 change: 1 addition & 0 deletions warden/src/wsh/barrier.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct barrier_s {


int barrier_open(barrier_t *bar); int barrier_open(barrier_t *bar);
void barrier_close(barrier_t *bar); void barrier_close(barrier_t *bar);
void barrier_mix_cloexec(barrier_t *bar);


void barrier_close_wait(barrier_t *bar); void barrier_close_wait(barrier_t *bar);
void barrier_close_signal(barrier_t *bar); void barrier_close_signal(barrier_t *bar);
Expand Down
3 changes: 0 additions & 3 deletions warden/src/wsh/un.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <unistd.h> #include <unistd.h>


#include "un.h" #include "un.h"
#include "util.h"


int un__socket() { int un__socket() {
int fd; int fd;
Expand All @@ -34,8 +33,6 @@ int un_listen(const char *path) {
strcpy(sa.sun_path, path); strcpy(sa.sun_path, path);
unlink(sa.sun_path); unlink(sa.sun_path);


fcntl_mix_cloexec(fd);

if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) { if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
perror("bind"); perror("bind");
exit(1); exit(1);
Expand Down
91 changes: 87 additions & 4 deletions warden/src/wsh/wshd.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/ipc.h>
#include <sys/param.h> #include <sys/param.h>
#include <sys/shm.h>
#include <sys/signalfd.h> #include <sys/signalfd.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
Expand Down Expand Up @@ -623,6 +625,65 @@ int child_loop(wshd_t *w) {
/* No header defines this */ /* No header defines this */
extern int pivot_root(const char *new_root, const char *put_old); extern int pivot_root(const char *new_root, const char *put_old);


void child_save_to_shm(wshd_t *w) {
int rv;
void *w_;

rv = shmget(0xdeadbeef, sizeof(*w), IPC_CREAT | IPC_EXCL | 0600);
if (rv == -1) {
perror("shmget");
abort();
}

w_ = shmat(rv, NULL, 0);
if (w_ == (void *)-1) {
perror("shmat");
abort();
}

memcpy(w_, w, sizeof(*w));
}

wshd_t *child_load_from_shm(void) {
int rv;
wshd_t *w;
void *w_;

rv = shmget(0xdeadbeef, sizeof(*w), 0600);
if (rv == -1) {
perror("shmget");
abort();
}

w_ = shmat(rv, NULL, 0);
if (w_ == (void *)-1) {
perror("shmat");
abort();
}

w = malloc(sizeof(*w));
if (w == NULL) {
perror("malloc");
abort();
}

memcpy(w, w_, sizeof(*w));

rv = shmdt(w_);
if (w_ == (void *)-1) {
perror("shmdt");
abort();
}

rv = shmctl(0xdeadbeef, IPC_RMID, NULL);
if (w_ == (void *)-1) {
perror("shmctl");
abort();
}

return w;
}

int child_run(void *data) { int child_run(void *data) {
wshd_t *w = (wshd_t *)data; wshd_t *w = (wshd_t *)data;
int rv; int rv;
Expand Down Expand Up @@ -668,6 +729,27 @@ int child_run(void *data) {
rv = run(pivoted_lib_path, "hook-child-after-pivot.sh"); rv = run(pivoted_lib_path, "hook-child-after-pivot.sh");
assert(rv == 0); assert(rv == 0);


child_save_to_shm(w);

execl("/sbin/wshd", "/sbin/wshd", "--continue", NULL);
perror("exec");
abort();
}

int child_continue(int argc, char **argv) {
wshd_t *w;
int rv;

w = child_load_from_shm();

/* Process MUST not leak file descriptors to children */
barrier_mix_cloexec(&w->barrier_child);
fcntl_mix_cloexec(w->fd);

if (strlen(w->title) > 0) {
setproctitle(argv, w->title);
}

rv = mount_umount_pivoted_root("/mnt"); rv = mount_umount_pivoted_root("/mnt");
if (rv == -1) { if (rv == -1) {
exit(1); exit(1);
Expand Down Expand Up @@ -778,6 +860,11 @@ int main(int argc, char **argv) {
wshd_t *w; wshd_t *w;
int rv; int rv;


/* Continue child execution in the context of the container */
if (argc > 1 && strcmp(argv[1], "--continue") == 0) {
return child_continue(argc, argv);
}

w = calloc(1, sizeof(*w)); w = calloc(1, sizeof(*w));
assert(w != NULL); assert(w != NULL);


Expand All @@ -798,10 +885,6 @@ int main(int argc, char **argv) {
strcpy(w->root_path, "root"); strcpy(w->root_path, "root");
} }


if (w->title != NULL) {
setproctitle(argv, w->title);
}

assert_directory(w->run_path); assert_directory(w->run_path);
assert_directory(w->lib_path); assert_directory(w->lib_path);
assert_directory(w->root_path); assert_directory(w->root_path);
Expand Down

0 comments on commit 8de5d03

Please sign in to comment.