I am working on an online judge to conduct ACM-ICPCs, such as competitions at a local college school. For this, I demand that the judge can be safe enough to prevent malware from running on my server. (An example of such a program would be)
int main(){
while(1) fork();
}
Allows you to call the executable file of this program test code .
This program will force my server to freeze the judge. Obviously, I do not want this to happen. Therefore, so that I do not try to use ptrace.I came up with the following code: (Allows the monitor to execute the executable file of this code )
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/user.h>
#include <sys/syscall.h>
#include <sys/reg.h>
#include<stdio.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/user.h>
#include <sys/syscall.h>
#include <sys/reg.h>
#include<stdio.h>
#include<signal.h>
#include<sys/prctl.h>
#include<stdlib.h>
#define NOBANNEDSYS 40
int bannedsys[50]={2,14,12,15,26,37,38,39,39,40,41,42,46,47,48,49,50,60,61,63,72,83,88,120,102,182,183,190};
int main(int argc,char **argv) {
int insyscall=0;
if(argc!=2) {
fprintf(stderr,"Usage: %s <prog name> ",argv[0]);
exit(-1);
}
int status = 0;
int syscall_n = 0;
int entering = 1;
int amp;
struct user_regs_struct regs;
int pid = fork();
if ( !pid ) {
prctl(PR_SET_PDEATHSIG, SIGKILL);
ptrace( PTRACE_TRACEME, 0, 0, 0 );
execlp( argv[1],argv[1], 0 );
}
else {
while (1) {
wait( &);
if ( WIFEXITED( amp ) ) break;
if(insyscall==0){
ptrace( PTRACE_GETREGS, pid, 0,®s );
int i=0;
for(i=0;i<NOBANNEDSYS;i++) if(regs.orig_eax==bannedsys[i]) { kill(pid,SIGKILL);
printf("%d killed due to illegal system call\n",pid);
abort();
}
insyscall=1;
}
else insyscall=0;
ptrace( PTRACE_SYSCALL, pid, 0, 0 );
}
}
return 0;
}
, . , , fork , testcode, - . , , .
, ?
PS: . Linux.
EDIT:
setrlimit 0 exec. . , .