ms-runner/starter/starter.c

162 lines
4.7 KiB
C
Raw Normal View History

2024-06-20 16:41:25 +00:00
#define _GNU_SOURCE
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sched.h>
#include <sys/syscall.h>
#include <linux/sched.h>
#include <stdarg.h>
#include <time.h>
2024-06-22 18:54:21 +00:00
#include <sys/time.h>
2024-06-20 19:00:10 +00:00
#include <limits.h>
#include <signal.h>
2024-06-22 18:54:21 +00:00
#include <stdbool.h>
2024-06-20 19:00:10 +00:00
2024-06-20 16:41:25 +00:00
#include "util.h"
struct params {
char* shared_folder;
int fd[2];
int out_fd;
2024-06-20 16:41:25 +00:00
char **argv;
};
struct limits {
size_t memory;
int core;
2024-06-20 19:00:10 +00:00
int time;
int processes;
2024-06-20 19:00:10 +00:00
};
struct killparams{
int time;
int pid;
2024-06-20 16:41:25 +00:00
};
#include "ns_exec.c"
#include "cgroup_prepare.c"
2024-06-22 18:54:21 +00:00
//bool TL_achieve=false;
uint64_t time_start;
//get time in milliseconds
uint64_t getmstime() {
struct timeval tm;
gettimeofday(&tm,NULL);
return (uint64_t)tm.tv_sec*1000+(uint64_t)tm.tv_usec/1000;
}
2024-06-22 16:29:19 +00:00
//time limit realisation
2024-06-20 19:00:10 +00:00
static int killafter(void *arg) {
//die when parent dies
if (prctl(PR_SET_PDEATHSIG, SIGKILL)) die("cannot PR_SET_PDEATHSIG for child process: %m\n");
struct killparams* params = (struct killparams*) arg;
2024-06-22 18:54:21 +00:00
struct timespec tw = {params -> time / 1000, ((params->time) % 1000+5) * 1000000};
2024-06-20 19:00:10 +00:00
struct timespec tr;
2024-06-22 16:29:19 +00:00
nanosleep(&tw, &tr);
kill(params->pid, SIGKILL);
2024-06-22 18:54:21 +00:00
return 0;
2024-06-20 19:00:10 +00:00
}
2024-06-20 16:41:25 +00:00
#define STACK_SIZE 1000000
static char nmstack[STACK_SIZE];
2024-06-20 19:00:10 +00:00
static char killstack[STACK_SIZE];
2024-06-20 16:41:25 +00:00
static void parse_args(int argc, char **argv, struct params *params,struct limits *limits){
if (argc < 7) {
puts("usage:\n starter <max processes number> <core_id> <memory_amount> <time limit in ms> <shared folder> <command, arg1,arg2,...>");
2024-06-20 16:41:25 +00:00
exit(0);
}
2024-06-22 16:29:19 +00:00
argc--; argv++;
limits->processes = atoi(argv[0]);
argc--; argv++;
2024-06-22 16:29:19 +00:00
limits->core = atoi(argv[0]);
argc--; argv++;
limits->memory = atoi(argv[0]);
argc--; argv++;
limits->time = atoi(argv[0]);
argc--; argv++;
params->shared_folder = argv[0];
argc--; argv++;
2024-06-20 16:41:25 +00:00
params->argv = argv;
}
2024-06-22 16:29:19 +00:00
//setup user namespace
2024-06-20 16:41:25 +00:00
static void prepare_userns(int pid) {
char path[100];
char line[100];
2024-06-22 16:29:19 +00:00
int uid = 0;//root
int gid = 0;//root
int unprivileged_uid = 66534;//nobody
int unprivileged_gid = 65534;//nogroup
2024-06-20 16:41:25 +00:00
sprintf(path, "/proc/%d/uid_map", pid);
2024-06-22 16:29:19 +00:00
sprintf(line, "0 %d 1\n1 %d 1000\n", uid, unprivileged_uid);//map root to uid 0,nobody to uid 1000
2024-06-20 16:41:25 +00:00
write_file(path, line);
sprintf(path, "/proc/%d/setgroups", pid);
sprintf(line, "deny");
write_file(path, line);
sprintf(path, "/proc/%d/gid_map", pid);
2024-06-22 16:29:19 +00:00
sprintf(line, "0 %d 1\n1 %d 1000\n", gid, unprivileged_gid);//map root to gid 0,nogroup to gid 1000
2024-06-20 16:41:25 +00:00
write_file(path, line);
}
int main(int argc,char** argv) {
if(setuid(0)) die("need to be run as root");
if(setgid(0)) die("need to be run as root");
2024-06-22 16:29:19 +00:00
//get binary folder
2024-06-20 19:00:10 +00:00
char bin_path[PATH_MAX];
2024-06-22 16:29:19 +00:00
if (realpath (argv[0], bin_path) == 0) die("unable to resolve real path: %m");//get absolute path to executable
for(int i = strlen(bin_path); i > 0 && bin_path[i] != '/';i--) bin_path[i]=0;//cut filename to get directory name
if(chdir(bin_path)) die("unable to chdir to binary path: %m");
2024-06-22 16:29:19 +00:00
//set random seed
2024-06-20 16:41:25 +00:00
srand(time(NULL));
2024-06-22 16:29:19 +00:00
//setup parameters
2024-06-20 16:41:25 +00:00
struct params params;
memset(&params, 0, sizeof(struct params));
struct limits limits;
memset(&limits, 0, sizeof(struct limits));
parse_args(argc, argv, &params, &limits);
prepare_cgroup(&limits);
2024-06-22 16:29:19 +00:00
if (pipe(params.fd) < 0) die("can't open pipe: %m");//a pipe to report readiness
int clone_flags = SIGCHLD | CLONE_NEWUTS | CLONE_NEWUSER | CLONE_NEWNS | CLONE_NEWPID | CLONE_NEWNET | CLONE_NEWIPC | CLONE_NEWCGROUP;
int nsrun_pid = clone(nsrun, nmstack + STACK_SIZE, clone_flags, &params);//make new namespace
2024-06-22 18:54:21 +00:00
//int pipe = params.fd[1];
2024-06-20 16:41:25 +00:00
prepare_userns(nsrun_pid);
2024-06-22 16:29:19 +00:00
if (nsrun_pid < 0) die("faled to clone");
2024-06-20 16:41:25 +00:00
add_to_cgroup(nsrun_pid);
2024-06-22 18:54:21 +00:00
if (write(params.fd[1], "OK", 2) != 2) die("Failed to write to pipe: %m");//report readiness
2024-06-20 19:00:10 +00:00
struct killparams killparams;
2024-06-22 16:29:19 +00:00
killparams.time = limits.time;
killparams.pid = nsrun_pid;
2024-06-22 18:54:21 +00:00
int kill_pid = clone(killafter, killstack + STACK_SIZE, SIGCHLD, &killparams);
time_start = getmstime();
2024-06-20 16:41:25 +00:00
if (waitpid(nsrun_pid, NULL, 0) == -1) die("Failed to wait pid %d: %m\n", nsrun_pid);
2024-06-22 18:54:21 +00:00
kill(kill_pid,SIGKILL);//kill killer
int time = getmstime()-time_start;
if(chdir(params.shared_folder)) die("Failed to chdir to shared folder:%m");
2024-06-22 18:54:21 +00:00
if(time>=limits.time) {
write_file("time", "-1");
}
else {
char* timestr = NULL;
asprintf(&timestr, "%d", time);
write_file("time", timestr);
free(timestr);
}
int memory = check_mem();
char* memstr = NULL;
asprintf(&memstr, "%d", memory);
write_file("memory", memstr);
free(memstr);
2024-06-20 16:41:25 +00:00
remove_cgroup();
return 0;
}