run_program_conf.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. #ifdef __x86_64__
  2. typedef unsigned long long int reg_val_t;
  3. #define REG_SYSCALL orig_rax
  4. #define REG_RET rax
  5. #define REG_ARG0 rdi
  6. #define REG_ARG1 rsi
  7. #define REG_ARG2 rdx
  8. #define REG_ARG3 rcx
  9. #else
  10. typedef long int reg_val_t;
  11. #define REG_SYSCALL orig_eax
  12. #define REG_RET eax
  13. #define REG_ARG0 ebx
  14. #define REG_ARG1 ecx
  15. #define REG_ARG2 edx
  16. #define REG_ARG3 esx
  17. #endif
  18. const size_t MaxPathLen = 200;
  19. set<string> writable_file_name_set;
  20. set<string> readable_file_name_set;
  21. set<string> statable_file_name_set;
  22. set<string> soft_ban_file_name_set;
  23. int syscall_max_cnt[1000];
  24. bool syscall_should_soft_ban[1000];
  25. string basename(const string &path) {
  26. size_t p = path.rfind('/');
  27. if (p == string::npos) {
  28. return path;
  29. } else {
  30. return path.substr(p + 1);
  31. }
  32. }
  33. string dirname(const string &path) {
  34. size_t p = path.rfind('/');
  35. if (p == string::npos) {
  36. return "";
  37. } else {
  38. return path.substr(0, p);
  39. }
  40. }
  41. string getcwdp(pid_t pid) {
  42. char s[20];
  43. char cwd[MaxPathLen + 1];
  44. if (pid != 0) {
  45. sprintf(s, "/proc/%lld/cwd", (long long int)pid);
  46. } else {
  47. sprintf(s, "/proc/self/cwd");
  48. }
  49. int l = readlink(s, cwd, MaxPathLen);
  50. if (l == -1) {
  51. return "";
  52. }
  53. cwd[l] = '\0';
  54. return cwd;
  55. }
  56. string abspath(pid_t pid, const string &path) {
  57. if (path.size() > MaxPathLen) {
  58. return "";
  59. }
  60. if (path.empty()) {
  61. return path;
  62. }
  63. string s;
  64. string b;
  65. size_t st;
  66. if (path[0] == '/') {
  67. s = "/";
  68. st = 1;
  69. } else {
  70. s = getcwdp(pid) + "/";
  71. st = 0;
  72. }
  73. for (size_t i = st; i < path.size(); i++) {
  74. b += path[i];
  75. if (path[i] == '/') {
  76. if (b == "../" && !s.empty()) {
  77. if (s == "./") {
  78. s = "../";
  79. } else if (s != "/") {
  80. size_t p = s.size() - 1;
  81. while (p > 0 && s[p - 1] != '/') {
  82. p--;
  83. }
  84. if (s.size() - p == 3 && s[p] == '.' && s[p + 1] == '.' && s[p + 2] == '/') {
  85. s += b;
  86. } else {
  87. s.resize(p);
  88. }
  89. }
  90. } else if (b != "./" && b != "/") {
  91. s += b;
  92. }
  93. b.clear();
  94. }
  95. }
  96. if (b == ".." && !s.empty()) {
  97. if (s == "./") {
  98. s = "..";
  99. } else if (s != "/") {
  100. size_t p = s.size() - 1;
  101. while (p > 0 && s[p - 1] != '/') {
  102. p--;
  103. }
  104. if (s.size() - p == 3 && s[p] == '.' && s[p + 1] == '.' && s[p + 2] == '/') {
  105. s += b;
  106. } else {
  107. s.resize(p);
  108. }
  109. }
  110. } else if (b != ".") {
  111. s += b;
  112. }
  113. if (s.size() >= 2 && s[s.size() - 1] == '/') {
  114. s.resize(s.size() - 1);
  115. }
  116. return s;
  117. }
  118. string realpath(const string &path) {
  119. char real[PATH_MAX + 1] = {};
  120. if (realpath(path.c_str(), real) == NULL) {
  121. return "";
  122. }
  123. return real;
  124. }
  125. inline bool is_in_set_smart(string name, const set<string> &s) {
  126. if (name.size() > MaxPathLen) {
  127. return false;
  128. }
  129. if (s.count(name)) {
  130. return true;
  131. }
  132. for (size_t i = 0; i + 1 < name.size(); i++) {
  133. if ((i == 0 || name[i - 1] == '/') && name[i] == '.' && name[i + 1] == '.' && (i + 2 == name.size() || name[i + 2] == '.')) {
  134. return false;
  135. }
  136. }
  137. int level;
  138. for (level = 0; !name.empty(); name = dirname(name), level++) {
  139. if (level == 1 && s.count(name + "/*")) {
  140. return true;
  141. }
  142. if (s.count(name + "/")) {
  143. return true;
  144. }
  145. }
  146. if (level == 1 && s.count("/*")) {
  147. return true;
  148. }
  149. if (s.count("/")) {
  150. return true;
  151. }
  152. return false;
  153. }
  154. inline bool is_writable_file(string name) {
  155. if (name == "/") {
  156. return writable_file_name_set.count("system_root");
  157. }
  158. return is_in_set_smart(name, writable_file_name_set) || is_in_set_smart(realpath(name), readable_file_name_set);
  159. }
  160. inline bool is_readable_file(const string &name) {
  161. if (is_writable_file(name)) {
  162. return true;
  163. }
  164. if (name == "/") {
  165. return readable_file_name_set.count("system_root");
  166. }
  167. return is_in_set_smart(name, readable_file_name_set) || is_in_set_smart(realpath(name), readable_file_name_set);
  168. }
  169. inline bool is_statable_file(const string &name) {
  170. if (is_readable_file(name)) {
  171. return true;
  172. }
  173. if (name == "/") {
  174. return statable_file_name_set.count("system_root");
  175. }
  176. return is_in_set_smart(name, statable_file_name_set) || is_in_set_smart(realpath(name), statable_file_name_set);
  177. }
  178. inline bool is_soft_ban_file(const string &name) {
  179. if (name == "/") {
  180. return soft_ban_file_name_set.count("system_root");
  181. }
  182. return is_in_set_smart(name, soft_ban_file_name_set) || is_in_set_smart(realpath(name), soft_ban_file_name_set);
  183. }
  184. #ifdef __x86_64__
  185. int syscall_max_cnt_list_default[][2] = {
  186. {__NR_read , -1},
  187. {__NR_write , -1},
  188. {__NR_readv , -1},
  189. {__NR_writev , -1},
  190. {__NR_open , -1},
  191. {__NR_unlink , -1},
  192. {__NR_close , -1},
  193. {__NR_readlink , -1},
  194. {__NR_openat , -1},
  195. {__NR_unlinkat , -1},
  196. {__NR_readlinkat , -1},
  197. {__NR_stat , -1},
  198. {__NR_fstat , -1},
  199. {__NR_lstat , -1},
  200. {__NR_lseek , -1},
  201. {__NR_access , -1},
  202. {__NR_dup , -1},
  203. {__NR_dup2 , -1},
  204. {__NR_dup3 , -1},
  205. {__NR_ioctl , -1},
  206. {__NR_fcntl , -1},
  207. {__NR_mmap , -1},
  208. {__NR_mprotect , -1},
  209. {__NR_munmap , -1},
  210. {__NR_brk , -1},
  211. {__NR_mremap , -1},
  212. {__NR_msync , -1},
  213. {__NR_mincore , -1},
  214. {__NR_madvise , -1},
  215. {__NR_rt_sigaction , -1},
  216. {__NR_rt_sigprocmask, -1},
  217. {__NR_rt_sigreturn , -1},
  218. {__NR_rt_sigpending , -1},
  219. {__NR_sigaltstack , -1},
  220. {__NR_getcwd , -1},
  221. {__NR_exit , -1},
  222. {__NR_exit_group , -1},
  223. {__NR_arch_prctl , -1},
  224. {__NR_gettimeofday , -1},
  225. {__NR_getrlimit , -1},
  226. {__NR_getrusage , -1},
  227. {__NR_times , -1},
  228. {__NR_time , -1},
  229. {__NR_clock_gettime , -1},
  230. {__NR_restart_syscall, -1},
  231. {__NR_set_tid_address, -1},
  232. {__NR_set_robust_list, -1},
  233. {__NR_prlimit64 , -1},
  234. {__NR_futex , -1},
  235. {-1 , -1}
  236. };
  237. int syscall_soft_ban_list_default[] = {
  238. -1
  239. };
  240. const char *readable_file_name_list_default[] = {
  241. "/etc/ld.so.nohwcap",
  242. "/etc/ld.so.preload",
  243. "/etc/ld.so.cache",
  244. "/lib/x86_64-linux-gnu/",
  245. "/usr/lib/x86_64-linux-gnu/",
  246. "/usr/lib/locale/locale-archive",
  247. "/proc/self/exe",
  248. "/etc/timezone",
  249. "/usr/share/zoneinfo/",
  250. "/dev/random",
  251. "/dev/urandom",
  252. "/proc/meminfo",
  253. "/etc/localtime",
  254. "/proc/self/environ",
  255. "/proc/self/cmdline",
  256. NULL
  257. };
  258. #else
  259. #error T_T
  260. #endif
  261. void add_file_permission(const string &file_name, char mode) {
  262. if (mode == 'w') {
  263. writable_file_name_set.insert(file_name);
  264. } else if (mode == 'r') {
  265. readable_file_name_set.insert(file_name);
  266. } else if (mode == 's') {
  267. statable_file_name_set.insert(file_name);
  268. }
  269. for (string name = dirname(file_name); !name.empty(); name = dirname(name)) {
  270. statable_file_name_set.insert(name);
  271. }
  272. }
  273. void init_conf(const RunProgramConfig &config) {
  274. for (int i = 0; syscall_max_cnt_list_default[i][0] != -1; i++) {
  275. syscall_max_cnt[syscall_max_cnt_list_default[i][0]] = syscall_max_cnt_list_default[i][1];
  276. }
  277. for (int i = 0; syscall_soft_ban_list_default[i] != -1; i++) {
  278. syscall_should_soft_ban[syscall_soft_ban_list_default[i]] = true;
  279. }
  280. for (int i = 0; readable_file_name_list_default[i]; i++) {
  281. readable_file_name_set.insert(readable_file_name_list_default[i]);
  282. }
  283. statable_file_name_set.insert(config.work_path + "/");
  284. if (config.type != "java8" && config.type != "java11") {
  285. add_file_permission(config.program_name, 'r');
  286. } else {
  287. int p = config.program_name.find('.');
  288. if (p == string::npos) {
  289. readable_file_name_set.insert(config.work_path + "/");
  290. } else {
  291. readable_file_name_set.insert(config.work_path + "/" + config.program_name.substr(0, p) + "/");
  292. }
  293. }
  294. add_file_permission(config.work_path, 'r');
  295. for (vector<string>::const_iterator it = config.extra_readable_files.begin(); it != config.extra_readable_files.end(); it++) {
  296. add_file_permission(*it, 'r');
  297. }
  298. for (vector<string>::const_iterator it = config.extra_writable_files.begin(); it != config.extra_writable_files.end(); it++) {
  299. add_file_permission(*it, 'w');
  300. }
  301. writable_file_name_set.insert("/dev/null");
  302. if (config.allow_proc) {
  303. syscall_max_cnt[__NR_clone ] = -1;
  304. syscall_max_cnt[__NR_fork ] = -1;
  305. syscall_max_cnt[__NR_vfork ] = -1;
  306. syscall_max_cnt[__NR_nanosleep ] = -1;
  307. syscall_max_cnt[__NR_execve ] = -1;
  308. }
  309. if (config.type == "python2") {
  310. syscall_max_cnt[__NR_set_tid_address] = 1;
  311. syscall_max_cnt[__NR_set_robust_list] = 1;
  312. syscall_max_cnt[__NR_futex ] = -1;
  313. syscall_max_cnt[__NR_getdents ] = -1;
  314. syscall_max_cnt[__NR_getdents64 ] = -1;
  315. # ifdef UOJ_JUDGER_BASESYSTEM_UBUNTU1804
  316. syscall_max_cnt[__NR_prlimit64 ] = -1;
  317. syscall_max_cnt[__NR_getpid ] = -1;
  318. syscall_max_cnt[__NR_sysinfo ] = -1;
  319. # endif
  320. readable_file_name_set.insert("/usr/bin/python2.7");
  321. readable_file_name_set.insert("/usr/lib/python2.7/");
  322. readable_file_name_set.insert("/usr/bin/lib/python2.7/");
  323. readable_file_name_set.insert("/usr/local/lib/python2.7/");
  324. readable_file_name_set.insert("/usr/lib/pymodules/python2.7/");
  325. readable_file_name_set.insert("/usr/bin/Modules/");
  326. readable_file_name_set.insert("/usr/bin/pybuilddir.txt");
  327. # ifdef UOJ_JUDGER_BASESYSTEM_UBUNTU1804
  328. readable_file_name_set.insert("/usr/lib/locale/");
  329. readable_file_name_set.insert(config.work_path + "/answer.code");
  330. # endif
  331. statable_file_name_set.insert("/usr");
  332. statable_file_name_set.insert("/usr/bin");
  333. } else if (config.type == "python3") {
  334. syscall_max_cnt[__NR_set_tid_address] = 1;
  335. syscall_max_cnt[__NR_set_robust_list] = 1;
  336. syscall_max_cnt[__NR_futex ] = -1;
  337. syscall_max_cnt[__NR_getdents ] = -1;
  338. syscall_max_cnt[__NR_getdents64 ] = -1;
  339. # ifdef UOJ_JUDGER_BASESYSTEM_UBUNTU1804
  340. syscall_max_cnt[__NR_prlimit64 ] = -1;
  341. syscall_max_cnt[__NR_getrandom ] = -1;
  342. syscall_max_cnt[__NR_sysinfo ] = -1;
  343. syscall_max_cnt[__NR_getpid ] = -1;
  344. # endif
  345. readable_file_name_set.insert("/usr/bin/python3");
  346. readable_file_name_set.insert("/usr/lib/python3/");
  347. # ifdef UOJ_JUDGER_PYTHON3_VERSION
  348. readable_file_name_set.insert("/usr/bin/python" UOJ_JUDGER_PYTHON3_VERSION);
  349. readable_file_name_set.insert("/usr/lib/python" UOJ_JUDGER_PYTHON3_VERSION "/");
  350. readable_file_name_set.insert("/usr/bin/lib/python" UOJ_JUDGER_PYTHON3_VERSION "/");
  351. readable_file_name_set.insert("/usr/local/lib/python" UOJ_JUDGER_PYTHON3_VERSION "/");
  352. # else
  353. readable_file_name_set.insert("/usr/bin/python3.4");
  354. readable_file_name_set.insert("/usr/lib/python3.4/");
  355. readable_file_name_set.insert("/usr/bin/lib/python3.4/");
  356. readable_file_name_set.insert("/usr/local/lib/python3.4/");
  357. # endif
  358. readable_file_name_set.insert("/usr/bin/pyvenv.cfg");
  359. readable_file_name_set.insert("/usr/pyvenv.cfg");
  360. readable_file_name_set.insert("/usr/bin/Modules/");
  361. readable_file_name_set.insert("/usr/bin/pybuilddir.txt");
  362. readable_file_name_set.insert("/usr/lib/dist-python");
  363. # ifdef UOJ_JUDGER_BASESYSTEM_UBUNTU1804
  364. readable_file_name_set.insert("/usr/lib/locale/");
  365. readable_file_name_set.insert(config.work_path + "/answer.code");
  366. # endif
  367. statable_file_name_set.insert("/usr");
  368. statable_file_name_set.insert("/usr/bin");
  369. statable_file_name_set.insert("/usr/lib");
  370. # ifdef UOJ_JUDGER_BASESYSTEM_UBUNTU1804
  371. statable_file_name_set.insert("/usr/lib/python36.zip");
  372. # endif
  373. } else if (config.type == "java8") {
  374. syscall_max_cnt[__NR_gettid ] = -1;
  375. syscall_max_cnt[__NR_set_tid_address] = 1;
  376. syscall_max_cnt[__NR_set_robust_list] = 14;
  377. syscall_max_cnt[__NR_futex ] = -1;
  378. syscall_max_cnt[__NR_uname ] = 1;
  379. syscall_max_cnt[__NR_clone ] = 13;
  380. syscall_max_cnt[__NR_getdents ] = 4;
  381. syscall_max_cnt[__NR_clock_getres ] = 2;
  382. syscall_max_cnt[__NR_setrlimit ] = 1;
  383. syscall_max_cnt[__NR_sched_getaffinity] = -1;
  384. syscall_max_cnt[__NR_sched_yield ] = -1;
  385. # ifdef UOJ_JUDGER_BASESYSTEM_UBUNTU1804
  386. syscall_max_cnt[__NR_prlimit64 ] = -1;
  387. syscall_max_cnt[__NR_getpid ] = -1;
  388. syscall_max_cnt[__NR_sysinfo ] = -1;
  389. syscall_max_cnt[__NR_clone ] = -1;
  390. syscall_max_cnt[__NR_set_robust_list] = -1;
  391. syscall_max_cnt[__NR_prctl ] = -1;
  392. # endif
  393. syscall_should_soft_ban[__NR_socket ] = true;
  394. syscall_should_soft_ban[__NR_connect ] = true;
  395. syscall_should_soft_ban[__NR_geteuid ] = true;
  396. syscall_should_soft_ban[__NR_getuid ] = true;
  397. soft_ban_file_name_set.insert("/etc/nsswitch.conf");
  398. soft_ban_file_name_set.insert("/etc/passwd");
  399. add_file_permission("/usr/lib/jvm/java-8-openjdk-amd64/", 'r');
  400. readable_file_name_set.insert("/sys/devices/system/cpu/");
  401. readable_file_name_set.insert("/proc/");
  402. # ifdef UOJ_JUDGER_BASESYSTEM_UBUNTU1804
  403. readable_file_name_set.insert("/sys/fs/cgroup/cpu/");
  404. readable_file_name_set.insert("/sys/fs/cgroup/cpu,cpuacct/");
  405. readable_file_name_set.insert("/sys/fs/cgroup/memory/");
  406. readable_file_name_set.insert("/usr/lib/locale/");
  407. # endif
  408. statable_file_name_set.insert("/usr/java/");
  409. statable_file_name_set.insert("/tmp/");
  410. } else if (config.type == "java11") {
  411. syscall_max_cnt[__NR_gettid ] = -1;
  412. syscall_max_cnt[__NR_set_tid_address] = 1;
  413. syscall_max_cnt[__NR_set_robust_list] = 15;
  414. syscall_max_cnt[__NR_futex ] = -1;
  415. syscall_max_cnt[__NR_uname ] = 1;
  416. syscall_max_cnt[__NR_clone ] = 14;
  417. syscall_max_cnt[__NR_getdents ] = 4;
  418. syscall_max_cnt[__NR_clock_getres ] = 2;
  419. syscall_max_cnt[__NR_setrlimit ] = 1;
  420. syscall_max_cnt[__NR_sched_getaffinity] = -1;
  421. syscall_max_cnt[__NR_sched_yield ] = -1;
  422. # ifdef UOJ_JUDGER_BASESYSTEM_UBUNTU1804
  423. syscall_max_cnt[__NR_prlimit64 ] = -1;
  424. syscall_max_cnt[__NR_getpid ] = -1;
  425. syscall_max_cnt[__NR_sysinfo ] = -1;
  426. syscall_max_cnt[__NR_clone ] = -1;
  427. syscall_max_cnt[__NR_set_robust_list] = -1;
  428. syscall_max_cnt[__NR_uname ] = -1;
  429. syscall_max_cnt[__NR_clock_getres ] = -1;
  430. syscall_max_cnt[__NR_pread64 ] = -1;
  431. syscall_max_cnt[__NR_prctl ] = -1;
  432. syscall_max_cnt[__NR_nanosleep ] = -1;
  433. # endif
  434. syscall_should_soft_ban[__NR_socket ] = true;
  435. syscall_should_soft_ban[__NR_connect ] = true;
  436. syscall_should_soft_ban[__NR_geteuid ] = true;
  437. syscall_should_soft_ban[__NR_getuid ] = true;
  438. soft_ban_file_name_set.insert("/etc/nsswitch.conf");
  439. soft_ban_file_name_set.insert("/etc/passwd");
  440. add_file_permission("/usr/lib/jvm/java-11-openjdk-amd64/", 'r');
  441. readable_file_name_set.insert("/sys/devices/system/cpu/");
  442. readable_file_name_set.insert("/proc/");
  443. # ifdef UOJ_JUDGER_BASESYSTEM_UBUNTU1804
  444. readable_file_name_set.insert("/sys/fs/cgroup/cpu/");
  445. readable_file_name_set.insert("/sys/fs/cgroup/cpu,cpuacct/");
  446. readable_file_name_set.insert("/sys/fs/cgroup/memory/");
  447. readable_file_name_set.insert("/usr/share/java/");
  448. readable_file_name_set.insert("/usr/lib/locale/");
  449. readable_file_name_set.insert("/etc/oracle/java/usagetracker.properties");
  450. # endif
  451. statable_file_name_set.insert("/usr/java/");
  452. statable_file_name_set.insert("/tmp/");
  453. # ifdef UOJ_JUDGER_BASESYSTEM_UBUNTU1804
  454. statable_file_name_set.insert("/usr/share/");
  455. # endif
  456. } else if (config.type == "compiler") {
  457. // syscall_max_cnt[__NR_rseq ] = -1;
  458. // syscall_max_cnt[__NR_newfstatat ] = -1;
  459. // syscall_max_cnt[__NR_clone3 ] = -1;
  460. readable_file_name_set.insert("/etc/ld.so.conf");
  461. readable_file_name_set.insert("/etc/ld.so.conf.d/");
  462. syscall_max_cnt[__NR_gettid ] = -1;
  463. syscall_max_cnt[__NR_set_tid_address] = -1;
  464. syscall_max_cnt[__NR_set_robust_list] = -1;
  465. syscall_max_cnt[__NR_futex ] = -1;
  466. syscall_max_cnt[__NR_getpid ] = -1;
  467. syscall_max_cnt[__NR_vfork ] = -1;
  468. syscall_max_cnt[__NR_fork ] = -1;
  469. syscall_max_cnt[__NR_clone ] = -1;
  470. syscall_max_cnt[__NR_execve ] = -1;
  471. syscall_max_cnt[__NR_wait4 ] = -1;
  472. syscall_max_cnt[__NR_clock_gettime ] = -1;
  473. syscall_max_cnt[__NR_clock_getres ] = -1;
  474. syscall_max_cnt[__NR_setrlimit ] = -1;
  475. syscall_max_cnt[__NR_pipe ] = -1;
  476. syscall_max_cnt[__NR_getdents64 ] = -1;
  477. syscall_max_cnt[__NR_getdents ] = -1;
  478. syscall_max_cnt[__NR_umask ] = -1;
  479. syscall_max_cnt[__NR_rename ] = -1;
  480. syscall_max_cnt[__NR_chmod ] = -1;
  481. syscall_max_cnt[__NR_mkdir ] = -1;
  482. syscall_max_cnt[__NR_chdir ] = -1;
  483. syscall_max_cnt[__NR_fchdir ] = -1;
  484. syscall_max_cnt[__NR_ftruncate ] = -1; // for javac = =
  485. syscall_max_cnt[__NR_sched_getaffinity] = -1; // for javac = =
  486. syscall_max_cnt[__NR_sched_yield ] = -1; // for javac = =
  487. syscall_max_cnt[__NR_uname ] = -1; // for javac = =
  488. syscall_max_cnt[__NR_sysinfo ] = -1; // for javac = =
  489. # ifdef UOJ_JUDGER_BASESYSTEM_UBUNTU1804
  490. syscall_max_cnt[__NR_prlimit64 ] = -1;
  491. syscall_max_cnt[__NR_getrandom ] = -1;
  492. syscall_max_cnt[__NR_pread64 ] = -1;
  493. syscall_max_cnt[__NR_prctl ] = -1;
  494. syscall_max_cnt[__NR_nanosleep ] = -1;
  495. syscall_max_cnt[__NR_socketpair ] = -1;
  496. # endif
  497. syscall_should_soft_ban[__NR_socket ] = true; // for javac
  498. syscall_should_soft_ban[__NR_connect ] = true; // for javac
  499. syscall_should_soft_ban[__NR_geteuid ] = true; // for javac
  500. syscall_should_soft_ban[__NR_getuid ] = true; // for javac
  501. writable_file_name_set.insert("/tmp/");
  502. readable_file_name_set.insert(config.work_path);
  503. writable_file_name_set.insert(config.work_path + "/");
  504. readable_file_name_set.insert(abspath(0, string(self_path) + "/../runtime") + "/");
  505. # ifdef UOJ_JUDGER_BASESYSTEM_UBUNTU1804
  506. readable_file_name_set.insert("/sys/fs/cgroup/cpu/");
  507. readable_file_name_set.insert("/sys/fs/cgroup/cpu,cpuacct/");
  508. readable_file_name_set.insert("/sys/fs/cgroup/memory/");
  509. readable_file_name_set.insert("/etc/oracle/java/usagetracker.properties");
  510. # endif
  511. readable_file_name_set.insert("system_root");
  512. readable_file_name_set.insert("/usr/");
  513. readable_file_name_set.insert("/lib/");
  514. readable_file_name_set.insert("/lib64/");
  515. readable_file_name_set.insert("/bin/");
  516. readable_file_name_set.insert("/sbin/");
  517. // readable_file_name_set.insert("/proc/meminfo");
  518. // readable_file_name_set.insert("/proc/self/");
  519. readable_file_name_set.insert("/sys/devices/system/cpu/");
  520. readable_file_name_set.insert("/proc/");
  521. soft_ban_file_name_set.insert("/etc/nsswitch.conf"); // for javac = =
  522. soft_ban_file_name_set.insert("/etc/passwd"); // for javac = =
  523. readable_file_name_set.insert("/etc/timezone");
  524. # ifdef UOJ_JUDGER_FPC_VERSION
  525. readable_file_name_set.insert("/etc/fpc-" UOJ_JUDGER_FPC_VERSION ".cfg.d/");
  526. # else
  527. readable_file_name_set.insert("/etc/fpc-2.6.2.cfg.d/");
  528. # endif
  529. readable_file_name_set.insert("/etc/fpc.cfg");
  530. statable_file_name_set.insert("/*");
  531. }
  532. }
  533. string read_string_from_regs(reg_val_t addr, pid_t pid) {
  534. char res[MaxPathLen + 1], *ptr = res;
  535. while (ptr != res + MaxPathLen) {
  536. *(reg_val_t*)ptr = ptrace(PTRACE_PEEKDATA, pid, addr, NULL);
  537. for (int i = 0; i < sizeof(reg_val_t); i++, ptr++, addr++) {
  538. if (*ptr == 0) {
  539. return res;
  540. }
  541. }
  542. }
  543. res[MaxPathLen] = 0;
  544. return res;
  545. }
  546. string read_abspath_from_regs(reg_val_t addr, pid_t pid) {
  547. return abspath(pid, read_string_from_regs(addr, pid));
  548. }
  549. inline void soft_ban_syscall(pid_t pid, user_regs_struct reg) {
  550. reg.REG_SYSCALL += 1024;
  551. ptrace(PTRACE_SETREGS, pid, NULL, &reg);
  552. }
  553. inline bool on_dgs_file_detect(pid_t pid, user_regs_struct reg, const string &fn) {
  554. if (is_soft_ban_file(fn)) {
  555. soft_ban_syscall(pid, reg);
  556. return true;
  557. } else {
  558. return false;
  559. }
  560. }
  561. inline bool check_safe_syscall(pid_t pid, bool need_show_trace_details) {
  562. struct user_regs_struct reg;
  563. ptrace(PTRACE_GETREGS, pid, NULL, &reg);
  564. int cur_instruction = ptrace(PTRACE_PEEKTEXT, pid, reg.rip - 2, NULL) & 0xffff;
  565. if (cur_instruction != 0x050f) {
  566. if (need_show_trace_details) {
  567. fprintf(stderr, "informal syscall %d\n", cur_instruction);
  568. }
  569. return false;
  570. }
  571. int syscall = (int)reg.REG_SYSCALL;
  572. if (0 > syscall || syscall >= 1000) {
  573. return false;
  574. }
  575. if (need_show_trace_details) {
  576. fprintf(stderr, "syscall %d\n", (int)syscall);
  577. }
  578. if (syscall_should_soft_ban[syscall]) {
  579. soft_ban_syscall(pid, reg);
  580. } else if (syscall_max_cnt[syscall]-- == 0) {
  581. if (need_show_trace_details) {
  582. fprintf(stderr, "dgs %d\n", (int)syscall);
  583. }
  584. return false;
  585. }
  586. if (syscall == __NR_open || syscall == __NR_openat) {
  587. reg_val_t fn_addr;
  588. reg_val_t flags;
  589. if (syscall == __NR_open) {
  590. fn_addr = reg.REG_ARG0;
  591. flags = reg.REG_ARG1;
  592. } else {
  593. fn_addr = reg.REG_ARG1;
  594. flags = reg.REG_ARG2;
  595. }
  596. string fn = read_abspath_from_regs(fn_addr, pid);
  597. if (need_show_trace_details) {
  598. fprintf(stderr, "open ");
  599. switch (flags & O_ACCMODE) {
  600. case O_RDONLY:
  601. fprintf(stderr, "r ");
  602. break;
  603. case O_WRONLY:
  604. fprintf(stderr, "w ");
  605. break;
  606. case O_RDWR:
  607. fprintf(stderr, "rw");
  608. break;
  609. default:
  610. fprintf(stderr, "??");
  611. break;
  612. }
  613. fprintf(stderr, " %s\n", fn.c_str());
  614. }
  615. bool is_read_only = (flags & O_ACCMODE) == O_RDONLY &&
  616. (flags & O_CREAT) == 0 &&
  617. (flags & O_EXCL) == 0 &&
  618. (flags & O_TRUNC) == 0;
  619. if (is_read_only) {
  620. if (realpath(fn) != "" && !is_readable_file(fn)) {
  621. return on_dgs_file_detect(pid, reg, fn);
  622. }
  623. } else {
  624. if (!is_writable_file(fn)) {
  625. return on_dgs_file_detect(pid, reg, fn);
  626. }
  627. }
  628. } else if (syscall == __NR_readlink || syscall == __NR_readlinkat) {
  629. reg_val_t fn_addr;
  630. if (syscall == __NR_readlink) {
  631. fn_addr = reg.REG_ARG0;
  632. } else {
  633. fn_addr = reg.REG_ARG1;
  634. }
  635. string fn = read_abspath_from_regs(fn_addr, pid);
  636. if (need_show_trace_details) {
  637. fprintf(stderr, "readlink %s\n", fn.c_str());
  638. }
  639. if (!is_readable_file(fn)) {
  640. return on_dgs_file_detect(pid, reg, fn);
  641. }
  642. } else if (syscall == __NR_unlink || syscall == __NR_unlinkat) {
  643. reg_val_t fn_addr;
  644. if (syscall == __NR_unlink) {
  645. fn_addr = reg.REG_ARG0;
  646. } else {
  647. fn_addr = reg.REG_ARG1;
  648. }
  649. string fn = read_abspath_from_regs(fn_addr, pid);
  650. if (need_show_trace_details) {
  651. fprintf(stderr, "unlink %s\n", fn.c_str());
  652. }
  653. if (!is_writable_file(fn)) {
  654. return on_dgs_file_detect(pid, reg, fn);
  655. }
  656. } else if (syscall == __NR_access) {
  657. reg_val_t fn_addr = reg.REG_ARG0;
  658. string fn = read_abspath_from_regs(fn_addr, pid);
  659. if (need_show_trace_details) {
  660. fprintf(stderr, "access %s\n", fn.c_str());
  661. }
  662. if (!is_statable_file(fn)) {
  663. return on_dgs_file_detect(pid, reg, fn);
  664. }
  665. } else if (syscall == __NR_stat || syscall == __NR_lstat) {
  666. reg_val_t fn_addr = reg.REG_ARG0;
  667. string fn = read_abspath_from_regs(fn_addr, pid);
  668. if (need_show_trace_details) {
  669. fprintf(stderr, "stat %s\n", fn.c_str());
  670. }
  671. if (!is_statable_file(fn)) {
  672. return on_dgs_file_detect(pid, reg, fn);
  673. }
  674. } else if (syscall == __NR_execve) {
  675. reg_val_t fn_addr = reg.REG_ARG0;
  676. string fn = read_abspath_from_regs(fn_addr, pid);
  677. if (need_show_trace_details) {
  678. fprintf(stderr, "execve %s\n", fn.c_str());
  679. }
  680. if (!is_readable_file(fn)) {
  681. return on_dgs_file_detect(pid, reg, fn);
  682. }
  683. } else if (syscall == __NR_chmod || syscall == __NR_rename) {
  684. reg_val_t fn_addr = reg.REG_ARG0;
  685. string fn = read_abspath_from_regs(fn_addr, pid);
  686. if (need_show_trace_details) {
  687. fprintf(stderr, "change %s\n", fn.c_str());
  688. }
  689. if (!is_writable_file(fn)) {
  690. return on_dgs_file_detect(pid, reg, fn);
  691. }
  692. }
  693. return true;
  694. }
  695. inline void on_syscall_exit(pid_t pid, bool need_show_trace_details) {
  696. struct user_regs_struct reg;
  697. ptrace(PTRACE_GETREGS, pid, NULL, &reg);
  698. if (need_show_trace_details) {
  699. if ((long long int)reg.REG_SYSCALL >= 1024) {
  700. fprintf(stderr, "ban sys %lld\n", (long long int)reg.REG_SYSCALL - 1024);
  701. } else {
  702. fprintf(stderr, "exitsys %lld (ret %d)\n", (long long int)reg.REG_SYSCALL, (int)reg.REG_RET);
  703. }
  704. }
  705. if ((long long int)reg.REG_SYSCALL >= 1024) {
  706. reg.REG_SYSCALL -= 1024;
  707. reg.REG_RET = -EACCES;
  708. ptrace(PTRACE_SETREGS, pid, NULL, &reg);
  709. }
  710. }