-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchild.c
176 lines (157 loc) · 5.46 KB
/
child.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#define _DEFAULT_SOURCE
#define _POSIX_SOURCE
#define _GNU_SOURCE
#include <stdio.h>
#include <stdarg.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <grp.h>
#include <dlfcn.h>
#include <errno.h>
#include <sched.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mount.h>
#include "runner.h"
#include "child.h"
#include "logger.h"
#include "seccomp_rules.h"
#include "killer.h"
void close_file(FILE *fp) {
if (fp != NULL) {
fclose(fp);
}
}
void child_process(FILE *log_fp, struct config *_config) {
FILE *input_file = NULL, *output_file = NULL, *error_file = NULL;
if (_config->max_stack != UNLIMITED) {
struct rlimit max_stack;
max_stack.rlim_cur = max_stack.rlim_max = (rlim_t) (_config->max_stack);
if (setrlimit(RLIMIT_STACK, &max_stack) != 0) {
CHILD_ERROR_EXIT(SETRLIMIT_FAILED);
}
}
// set memory limit
// if memory_limit_check_only == 0, we only check memory usage number, because setrlimit(maxrss) will cause some crash issues
if (_config->memory_limit_check_only == 0) {
if (_config->max_memory != UNLIMITED) {
struct rlimit max_memory;
max_memory.rlim_cur = max_memory.rlim_max = (rlim_t) (_config->max_memory) * 2;
if (setrlimit(RLIMIT_AS, &max_memory) != 0) {
CHILD_ERROR_EXIT(SETRLIMIT_FAILED);
}
}
}
// set cpu time limit (in seconds)
if (_config->max_cpu_time != UNLIMITED) {
struct rlimit max_cpu_time;
max_cpu_time.rlim_cur = max_cpu_time.rlim_max = (rlim_t) ((_config->max_cpu_time + 1000) / 1000);
if (setrlimit(RLIMIT_CPU, &max_cpu_time) != 0) {
CHILD_ERROR_EXIT(SETRLIMIT_FAILED);
}
}
// set max process number limit
if (_config->max_process_number != UNLIMITED) {
struct rlimit max_process_number;
max_process_number.rlim_cur = max_process_number.rlim_max = (rlim_t) _config->max_process_number;
if (setrlimit(RLIMIT_NPROC, &max_process_number) != 0) {
CHILD_ERROR_EXIT(SETRLIMIT_FAILED);
}
}
// set max output size limit
if (_config->max_output_size != UNLIMITED) {
struct rlimit max_output_size;
max_output_size.rlim_cur = max_output_size.rlim_max = (rlim_t ) _config->max_output_size;
if (setrlimit(RLIMIT_FSIZE, &max_output_size) != 0) {
CHILD_ERROR_EXIT(SETRLIMIT_FAILED);
}
}
if (_config->input_path != NULL) {
input_file = fopen(_config->input_path, "r");
if (input_file == NULL) {
CHILD_ERROR_EXIT(DUP2_FAILED);
}
// redirect file -> stdin
// On success, these system calls return the new descriptor.
// On error, -1 is returned, and errno is set appropriately.
if (dup2(fileno(input_file), fileno(stdin)) == -1) {
// todo log
CHILD_ERROR_EXIT(DUP2_FAILED);
}
}
if (_config->output_path != NULL) {
output_file = fopen(_config->output_path, "w");
if (output_file == NULL) {
CHILD_ERROR_EXIT(DUP2_FAILED);
}
// redirect stdout -> file
if (dup2(fileno(output_file), fileno(stdout)) == -1) {
CHILD_ERROR_EXIT(DUP2_FAILED);
}
}
if (_config->error_path != NULL) {
// if outfile and error_file is the same path, we use the same file pointer
if (_config->output_path != NULL && strcmp(_config->output_path, _config->error_path) == 0) {
error_file = output_file;
}
else {
error_file = fopen(_config->error_path, "w");
if (error_file == NULL) {
// todo log
CHILD_ERROR_EXIT(DUP2_FAILED);
}
}
// redirect stderr -> file
if (dup2(fileno(error_file), fileno(stderr)) == -1) {
// todo log
CHILD_ERROR_EXIT(DUP2_FAILED);
}
}
FILE* fp = fopen("/proc/self/oom_adj", "w");
if(!fp) {
CHILD_ERROR_EXIT(SET_OOM_FAILED);
}
if(fprintf(fp,"15")!=2){
CHILD_ERROR_EXIT(SET_OOM_FAILED);
}
if(fclose(fp)!=0){
CHILD_ERROR_EXIT(SET_OOM_FAILED);
}
// set gid
gid_t group_list[] = {_config->gid};
if (_config->gid != -1 && (setgid(_config->gid) == -1 || setgroups(sizeof(group_list) / sizeof(gid_t), group_list) == -1)) {
CHILD_ERROR_EXIT(SETUID_FAILED);
}
// set uid
if (_config->uid != -1 && setuid(_config->uid) == -1) {
CHILD_ERROR_EXIT(SETUID_FAILED);
}
// load seccomp
if (_config->seccomp_rule_name != NULL) {
if (strcmp("c_cpp", _config->seccomp_rule_name) == 0) {
if (c_cpp_seccomp_rules(_config) != SUCCESS) {
CHILD_ERROR_EXIT(LOAD_SECCOMP_FAILED);
}
}
else if (strcmp("c_cpp_file_io", _config->seccomp_rule_name) == 0) {
if (c_cpp_file_io_seccomp_rules(_config) != SUCCESS) {
CHILD_ERROR_EXIT(LOAD_SECCOMP_FAILED);
}
}
else if (strcmp("general", _config->seccomp_rule_name) == 0) {
if (general_seccomp_rules(_config) != SUCCESS ) {
CHILD_ERROR_EXIT(LOAD_SECCOMP_FAILED);
}
}
// other rules
else {
// rule does not exist
CHILD_ERROR_EXIT(LOAD_SECCOMP_FAILED);
}
}
execve(_config->exe_path, _config->args, _config->env);
CHILD_ERROR_EXIT(EXECVE_FAILED);
}