|
8 | 8 | #include <elf.h> |
9 | 9 | #include <algorithm> |
10 | 10 | #include <cassert> |
| 11 | +#include <iomanip> |
11 | 12 |
|
12 | 13 | // COMPILATION NOTE: Must pass -lseccomp to build |
13 | 14 | #ifndef __NR_rseq |
|
19 | 20 | #endif |
20 | 21 | #include <seccomp.h> |
21 | 22 | #include <set> |
| 23 | +#include <map> |
22 | 24 | #include <string> |
23 | 25 | #include <seccomp.h> |
24 | 26 | #include <iostream> |
25 | 27 | #include <fstream> |
26 | 28 | #include <vector> |
27 | 29 | #include <string> |
28 | 30 |
|
| 31 | + |
29 | 32 | #define SUBMITTY_INSTALL_DIRECTORY std::string("__INSTALL__FILLIN__SUBMITTY_INSTALL_DIR__") |
30 | 33 |
|
31 | | -#define ALLOW_SYSCALL(name) allow_syscall(sc,SCMP_SYS(name),#name,execute_logfile) |
| 34 | +#define ALLOW_SYSCALL(name, which_category) allow_syscall(sc,SCMP_SYS(name),#name,execute_logfile, which_category,categories) |
| 35 | +#define ALLOW_SYSCALL_BY_NUMBER(num, name, which_category) allow_syscall(sc,num,name,execute_logfile, which_category,categories) |
| 36 | + |
| 37 | +static std::map<int,std::pair<std::string,bool> > allowed_system_calls; |
32 | 38 |
|
33 | | -static int total_allowed_system_calls = 0; |
34 | 39 |
|
35 | | -inline void allow_syscall(scmp_filter_ctx sc, int syscall, const std::string &syscall_string, std::ofstream &execute_logfile) { |
36 | | - total_allowed_system_calls++; |
37 | | - //execute_logfile << "allow " << total_allowed_system_calls << " " << syscall_string << std::endl; |
38 | | - int res = seccomp_rule_add(sc, SCMP_ACT_ALLOW, syscall, 0); |
39 | | - if (res < 0) { |
40 | | - execute_logfile << "WARNING: Errno " << res << " installing seccomp rule for " << syscall_string << std::endl; |
| 40 | +inline void allow_syscall(scmp_filter_ctx sc, int syscall, const std::string &syscall_string, std::ofstream &execute_logfile, |
| 41 | + const std::string &which_category, const std::set<std::string> &categories) { |
| 42 | + bool allowed = false; |
| 43 | + if (which_category.find("SAFELIST:") != std::string::npos) |
| 44 | + allowed = true; |
| 45 | + else if (which_category.find("FORBIDDEN:") != std::string::npos) |
| 46 | + allowed = false; |
| 47 | + else { |
| 48 | + assert (which_category.find("RESTRICTED:") != std::string::npos); |
| 49 | + if (categories.find(which_category.substr(11,which_category.size()-11)) != categories.end()) { |
| 50 | + allowed = true; |
| 51 | + } |
| 52 | + } |
| 53 | + allowed_system_calls.insert(std::make_pair(syscall,std::make_pair(syscall_string,allowed))); |
| 54 | +} |
| 55 | + |
| 56 | +void process_allow_system_calls(scmp_filter_ctx sc, std::ofstream &execute_logfile) { |
| 57 | + for (std::map<int,std::pair<std::string,bool> >::iterator itr = allowed_system_calls.begin(); itr != allowed_system_calls.end(); itr++) { |
| 58 | + if (itr->second.second == false) { |
| 59 | + //execute_logfile << " DISALLOWED " << itr->first << " " << itr->second.first << std::endl; |
| 60 | + int res = seccomp_rule_add(sc, SCMP_ACT_KILL, itr->first, 0); |
| 61 | + if (res < 0) { |
| 62 | + //execute_logfile << "WARNING: Errno " << res << " installing seccomp rule for " << itr->first << std::endl; |
| 63 | + } |
| 64 | + } else { |
| 65 | + // do nothing - allowed by default |
| 66 | + //execute_logfile << "allowed " << itr->first << " " << itr->second.first << std::endl; |
| 67 | + } |
| 68 | + } |
| 69 | + //execute_logfile << std::endl; |
| 70 | +} |
| 71 | + |
| 72 | +void scan_allowed_system_calls(scmp_filter_ctx sc, std::ofstream &execute_logfile) { |
| 73 | + for (int i = 0; i < 1100; i++) { |
| 74 | + execute_logfile << "BY NUMBER " << i << " "; |
| 75 | + if (allowed_system_calls.find(i) != allowed_system_calls.end()) { |
| 76 | + execute_logfile << " ... already added " << std::endl; |
| 77 | + } else { |
| 78 | + execute_logfile << " MISSING THIS ONE" << std::endl; |
| 79 | + } |
41 | 80 | } |
42 | 81 | } |
43 | 82 |
|
@@ -220,10 +259,10 @@ std::set<std::string> system_call_categories_based_on_program |
220 | 259 |
|
221 | 260 | int install_syscall_filter(bool is_32, const std::string &my_program, std::ofstream &execute_logfile, |
222 | 261 | const nlohmann::json &whole_config, const nlohmann::json &test_case_config) { |
223 | | - total_allowed_system_calls = 0; |
224 | 262 |
|
225 | 263 | int res; |
226 | | - scmp_filter_ctx sc = seccomp_init(SCMP_ACT_KILL); |
| 264 | + scmp_filter_ctx sc = seccomp_init(SCMP_ACT_ALLOW); |
| 265 | + |
227 | 266 | int target_arch = is_32 ? SCMP_ARCH_X86 : SCMP_ARCH_X86_64; |
228 | 267 | if (seccomp_arch_native() != target_arch) { |
229 | 268 | res = seccomp_arch_add(sc, target_arch); |
@@ -276,7 +315,8 @@ int install_syscall_filter(bool is_32, const std::string &my_program, std::ofstr |
276 | 315 | "COMMUNICATIONS_AND_NETWORKING_KILL", |
277 | 316 | "UNKNOWN", |
278 | 317 | "UNKNOWN_MODULE", |
279 | | - "UNKNOWN_REMAP_PAGES" |
| 318 | + "UNKNOWN_REMAP_PAGES", |
| 319 | + "CUSTOM_SYSTEM_CALLS" |
280 | 320 | }; |
281 | 321 |
|
282 | 322 | std::set<std::string> forbidden_categories = { |
@@ -322,15 +362,14 @@ int install_syscall_filter(bool is_32, const std::string &my_program, std::ofstr |
322 | 362 | } |
323 | 363 | } |
324 | 364 |
|
325 | | - //execute_logfile << "categories " << categories.size() << std::endl; |
326 | | - |
327 | 365 | // make sure all categories are valid |
328 | 366 | for_each(categories.begin(),categories.end(), |
329 | 367 | [restricted_categories](const std::string &s){ |
330 | 368 | assert (restricted_categories.find(s) != restricted_categories.end()); }); |
331 | 369 |
|
332 | 370 | allow_system_calls(sc,categories,execute_logfile); |
333 | | - //execute_logfile << "system call filter configured with " << total_allowed_system_calls << " allowed system calls" << std::endl; |
| 371 | + process_allow_system_calls(sc,execute_logfile); |
| 372 | + //scan_allowed_system_calls(sc,execute_logfile); |
334 | 373 |
|
335 | 374 | if (seccomp_load(sc) < 0) |
336 | 375 | return 1; // failure |
|
0 commit comments