Description:
added .gitignore to hold empty dirs, fixed deprecation warnings on const char* for box.cc git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@379 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r177:5f8c24384d1c - - 4 files changed: 6 inserted, 2 deleted

@@ -0,0 +1,2
1 + [^.]*
2 +
@@ -0,0 +1,1
1 + [^.]*
@@ -0,0 +1,1
1 + [^.]*
@@ -16,203 +16,203
16 #include <unistd.h>
16 #include <unistd.h>
17 #include <getopt.h>
17 #include <getopt.h>
18 #include <time.h>
18 #include <time.h>
19 #include <sys/wait.h>
19 #include <sys/wait.h>
20 #include <sys/user.h>
20 #include <sys/user.h>
21 #include <sys/time.h>
21 #include <sys/time.h>
22 #include <sys/ptrace.h>
22 #include <sys/ptrace.h>
23 #include <sys/signal.h>
23 #include <sys/signal.h>
24 #include <sys/sysinfo.h>
24 #include <sys/sysinfo.h>
25 #include <sys/syscall.h>
25 #include <sys/syscall.h>
26 #include <sys/resource.h>
26 #include <sys/resource.h>
27
27
28 #define NONRET __attribute__((noreturn))
28 #define NONRET __attribute__((noreturn))
29 #define UNUSED __attribute__((unused))
29 #define UNUSED __attribute__((unused))
30
30
31 static int filter_syscalls; /* 0=off, 1=liberal, 2=totalitarian */
31 static int filter_syscalls; /* 0=off, 1=liberal, 2=totalitarian */
32 static double timeout;
32 static double timeout;
33 static int pass_environ;
33 static int pass_environ;
34 static int use_wall_clock;
34 static int use_wall_clock;
35 static int file_access;
35 static int file_access;
36 static int verbose;
36 static int verbose;
37 static int memory_limit;
37 static int memory_limit;
38 static int allow_times;
38 static int allow_times;
39 static char *redir_stdin, *redir_stdout;
39 static char *redir_stdin, *redir_stdout;
40 static char *set_cwd;
40 static char *set_cwd;
41
41
42 static pid_t box_pid;
42 static pid_t box_pid;
43 static int is_ptraced;
43 static int is_ptraced;
44 static volatile int timer_tick;
44 static volatile int timer_tick;
45 static time_t start_time;
45 static time_t start_time;
46 static int ticks_per_sec;
46 static int ticks_per_sec;
47 static int page_size;
47 static int page_size;
48
48
49 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ > 0
49 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ > 0
50 /* glibc 2.1 or newer -> has lseek64 */
50 /* glibc 2.1 or newer -> has lseek64 */
51 #define long_seek(f,o,w) lseek64(f,o,w)
51 #define long_seek(f,o,w) lseek64(f,o,w)
52 #else
52 #else
53 /* Touching clandestine places in glibc */
53 /* Touching clandestine places in glibc */
54 extern loff_t llseek(int fd, loff_t pos, int whence);
54 extern loff_t llseek(int fd, loff_t pos, int whence);
55 #define long_seek(f,o,w) llseek(f,o,w)
55 #define long_seek(f,o,w) llseek(f,o,w)
56 #endif
56 #endif
57
57
58 int max_mem_used = 0;
58 int max_mem_used = 0;
59
59
60 void print_running_stat(double wall_time,
60 void print_running_stat(double wall_time,
61 double user_time,
61 double user_time,
62 double system_time,
62 double system_time,
63 int mem_usage)
63 int mem_usage)
64 {
64 {
65 fprintf(stderr,"%.4lfr%.4lfu%.4lfs%dm\n",
65 fprintf(stderr,"%.4lfr%.4lfu%.4lfs%dm\n",
66 wall_time, user_time, system_time, mem_usage);
66 wall_time, user_time, system_time, mem_usage);
67 }
67 }
68
68
69 static void NONRET
69 static void NONRET
70 box_exit(void)
70 box_exit(void)
71 {
71 {
72 if (box_pid > 0) {
72 if (box_pid > 0) {
73 if (is_ptraced)
73 if (is_ptraced)
74 ptrace(PTRACE_KILL, box_pid);
74 ptrace(PTRACE_KILL, box_pid);
75 kill(-box_pid, SIGKILL);
75 kill(-box_pid, SIGKILL);
76 kill(box_pid, SIGKILL);
76 kill(box_pid, SIGKILL);
77 }
77 }
78
78
79 struct timeval total;
79 struct timeval total;
80 int wall;
80 int wall;
81 struct rusage rus;
81 struct rusage rus;
82 int stat;
82 int stat;
83 pid_t p;
83 pid_t p;
84
84
85 // wait so that we can get information
85 // wait so that we can get information
86 p = wait4(box_pid, &stat, WUNTRACED, &rus);
86 p = wait4(box_pid, &stat, WUNTRACED, &rus);
87 if (p < 0) {
87 if (p < 0) {
88 fprintf(stderr,"wait4: error\n");
88 fprintf(stderr,"wait4: error\n");
89 print_running_stat(0,0,0,max_mem_used);
89 print_running_stat(0,0,0,max_mem_used);
90 } else if (p != box_pid) {
90 } else if (p != box_pid) {
91 fprintf(stderr,"wait4: unknown pid %d exited!\n", p);
91 fprintf(stderr,"wait4: unknown pid %d exited!\n", p);
92 print_running_stat(0,0,0,max_mem_used);
92 print_running_stat(0,0,0,max_mem_used);
93 } else {
93 } else {
94 if (!WIFEXITED(stat))
94 if (!WIFEXITED(stat))
95 fprintf(stderr,"wait4: unknown status\n");
95 fprintf(stderr,"wait4: unknown status\n");
96 struct timeval total;
96 struct timeval total;
97 int wall;
97 int wall;
98 wall = time(NULL) - start_time;
98 wall = time(NULL) - start_time;
99 timeradd(&rus.ru_utime, &rus.ru_stime, &total);
99 timeradd(&rus.ru_utime, &rus.ru_stime, &total);
100
100
101 print_running_stat((double)wall,
101 print_running_stat((double)wall,
102 (double) rus.ru_utime.tv_sec +
102 (double) rus.ru_utime.tv_sec +
103 ((double) rus.ru_utime.tv_usec/1000000.0),
103 ((double) rus.ru_utime.tv_usec/1000000.0),
104 (double) rus.ru_stime.tv_sec +
104 (double) rus.ru_stime.tv_sec +
105 ((double) rus.ru_stime.tv_usec/1000000.0),
105 ((double) rus.ru_stime.tv_usec/1000000.0),
106 max_mem_used);
106 max_mem_used);
107 }
107 }
108 exit(1);
108 exit(1);
109 }
109 }
110
110
111 static void NONRET __attribute__((format(printf,1,2)))
111 static void NONRET __attribute__((format(printf,1,2)))
112 - die(char *msg, ...)
112 + die(const char *msg, ...)
113 {
113 {
114 va_list args;
114 va_list args;
115 va_start(args, msg);
115 va_start(args, msg);
116 vfprintf(stderr, msg, args);
116 vfprintf(stderr, msg, args);
117 fputc('\n', stderr);
117 fputc('\n', stderr);
118 box_exit();
118 box_exit();
119 }
119 }
120
120
121 static void __attribute__((format(printf,1,2)))
121 static void __attribute__((format(printf,1,2)))
122 - log(char *msg, ...)
122 + log(const char *msg, ...)
123 {
123 {
124 va_list args;
124 va_list args;
125 va_start(args, msg);
125 va_start(args, msg);
126 if (verbose)
126 if (verbose)
127 {
127 {
128 vfprintf(stderr, msg, args);
128 vfprintf(stderr, msg, args);
129 fflush(stderr);
129 fflush(stderr);
130 }
130 }
131 va_end(args);
131 va_end(args);
132 }
132 }
133
133
134 static void
134 static void
135 valid_filename(unsigned long addr)
135 valid_filename(unsigned long addr)
136 {
136 {
137 char namebuf[4096], *p, *end;
137 char namebuf[4096], *p, *end;
138 static int mem_fd;
138 static int mem_fd;
139
139
140 if (!file_access)
140 if (!file_access)
141 die("File access forbidden.");
141 die("File access forbidden.");
142 if (file_access >= 9)
142 if (file_access >= 9)
143 return;
143 return;
144
144
145 if (!mem_fd)
145 if (!mem_fd)
146 {
146 {
147 sprintf(namebuf, "/proc/%d/mem", (int) box_pid);
147 sprintf(namebuf, "/proc/%d/mem", (int) box_pid);
148 mem_fd = open(namebuf, O_RDONLY);
148 mem_fd = open(namebuf, O_RDONLY);
149 if (mem_fd < 0)
149 if (mem_fd < 0)
150 die("open(%s): %m", namebuf);
150 die("open(%s): %m", namebuf);
151 }
151 }
152 p = end = namebuf;
152 p = end = namebuf;
153 do
153 do
154 {
154 {
155 if (p >= end)
155 if (p >= end)
156 {
156 {
157 int remains = PAGE_SIZE - (addr & (PAGE_SIZE-1));
157 int remains = PAGE_SIZE - (addr & (PAGE_SIZE-1));
158 int l = namebuf + sizeof(namebuf) - end;
158 int l = namebuf + sizeof(namebuf) - end;
159 if (l > remains)
159 if (l > remains)
160 l = remains;
160 l = remains;
161 if (!l)
161 if (!l)
162 die("Access to file with name too long.");
162 die("Access to file with name too long.");
163 if (long_seek(mem_fd, addr, SEEK_SET) < 0)
163 if (long_seek(mem_fd, addr, SEEK_SET) < 0)
164 die("long_seek(mem): %m");
164 die("long_seek(mem): %m");
165 remains = read(mem_fd, end, l);
165 remains = read(mem_fd, end, l);
166 if (remains < 0)
166 if (remains < 0)
167 die("read(mem): %m");
167 die("read(mem): %m");
168 if (!remains)
168 if (!remains)
169 die("Access to file with name out of memory.");
169 die("Access to file with name out of memory.");
170 end += l;
170 end += l;
171 addr += l;
171 addr += l;
172 }
172 }
173 }
173 }
174 while (*p++);
174 while (*p++);
175
175
176 log("[%s] ", namebuf);
176 log("[%s] ", namebuf);
177 if (file_access >= 3)
177 if (file_access >= 3)
178 return;
178 return;
179 if (!strchr(namebuf, '/') && strcmp(namebuf, ".."))
179 if (!strchr(namebuf, '/') && strcmp(namebuf, ".."))
180 return;
180 return;
181 if (file_access >= 2)
181 if (file_access >= 2)
182 {
182 {
183 if ((!strncmp(namebuf, "/etc/", 5) ||
183 if ((!strncmp(namebuf, "/etc/", 5) ||
184 !strncmp(namebuf, "/lib/", 5) ||
184 !strncmp(namebuf, "/lib/", 5) ||
185 !strncmp(namebuf, "/usr/lib/", 9))
185 !strncmp(namebuf, "/usr/lib/", 9))
186 && !strstr(namebuf, ".."))
186 && !strstr(namebuf, ".."))
187 return;
187 return;
188 if (!strcmp(namebuf, "/dev/null") ||
188 if (!strcmp(namebuf, "/dev/null") ||
189 !strcmp(namebuf, "/dev/zero") ||
189 !strcmp(namebuf, "/dev/zero") ||
190 !strcmp(namebuf, "/proc/meminfo") ||
190 !strcmp(namebuf, "/proc/meminfo") ||
191 !strcmp(namebuf, "/proc/self/stat") ||
191 !strcmp(namebuf, "/proc/self/stat") ||
192 !strncmp(namebuf, "/usr/share/zoneinfo/", 20))
192 !strncmp(namebuf, "/usr/share/zoneinfo/", 20))
193 return;
193 return;
194 }
194 }
195 die("Forbidden access to file `%s'.", namebuf);
195 die("Forbidden access to file `%s'.", namebuf);
196 }
196 }
197
197
198 static int
198 static int
199 valid_syscall(struct user *u)
199 valid_syscall(struct user *u)
200 {
200 {
201 switch (u->regs.orig_eax)
201 switch (u->regs.orig_eax)
202 {
202 {
203 case __NR_execve:
203 case __NR_execve:
204 {
204 {
205 static int exec_counter;
205 static int exec_counter;
206 return !exec_counter++;
206 return !exec_counter++;
207 }
207 }
208 case __NR_open:
208 case __NR_open:
209 case __NR_creat:
209 case __NR_creat:
210 case __NR_unlink:
210 case __NR_unlink:
211 case __NR_oldstat:
211 case __NR_oldstat:
212 case __NR_access:
212 case __NR_access:
213 case __NR_oldlstat:
213 case __NR_oldlstat:
214 case __NR_truncate:
214 case __NR_truncate:
215 case __NR_stat:
215 case __NR_stat:
216 case __NR_lstat:
216 case __NR_lstat:
217 case __NR_truncate64:
217 case __NR_truncate64:
218 case __NR_stat64:
218 case __NR_stat64:
You need to be logged in to leave comments. Login now