Skip to content

Commit c2ceb29

Browse files
authored
Fix bugs on subprocesses killing
Fix bugs on subprocesses killing
2 parents ab1745d + c4079db commit c2ceb29

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+174
-83
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ tmp/
3636

3737
.vscode/
3838
.idea/
39+
sick/

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ Below we list these programs and the conditions to trigger each bomb.
6060
| Buffer Overflow | stack_bo_l1.c | expected stdin: \`python -c 'print "AAAAAAAA\x01\x00\x00\x00"'\`|
6161
| | stack_bo_l2.c | expected stdin: TO FIGURE OUT |
6262
| | heap_bo_l1.c | expected stdin: TO FIGURE OUT|
63-
| External Function Call | rand_ef.c | rand()%100 == 7 |
64-
| | pow_ef.c | pow(i, 2) == 49 |
65-
| | sin_ef.c | sin(i * PI / 180) == 0.5 |
66-
| | ln_ef.c | 1.94 < log(i) && log(i) < 1.95 |
63+
| External Function Call | printint_ef_l1.c | expected stdin: 196 |
64+
| | printfloat_ef_l1.c | expected stdin: 196 |
65+
| | atoi_ef_l2.c | expected stdin: 199 |
66+
| | atof_ef_l2.c | expected stdin: 199 |
67+
| | rand_ef_l2.c | rand()%100 == 7 |
68+
| | pow_ef_l2.c | pow(i, 2) == 49 |
69+
| | sin_ef_l2.c | sin(i * PI / 180) == 0.5 |
70+
| | ln_ef_l2.c | 1.94 < log(i) && log(i) < 1.95 |
6771
| Crypto Function | sha_cf.c | if sha1(i) equals to a predefined value |
6872
| | aes_cf.c | if aes(i, plaintext) equals to a ciphertext |
6973
| Loop | collaz_lo_l1.c | if it loops 25 times (example stdin:101) |

config/test_settings.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@
22
FUNC_NAME = 'logic_bomb'
33

44
src_dirs = [
5-
'src/buffer_overflow',
5+
# 'src/buffer_overflow',
66
'src/contextual_symbolic_value',
7-
'src/covert_propogation',
8-
'src/crypto_functions',
9-
'src/data_overflow',
10-
'src/external_functions',
11-
'src/floating_point',
12-
'src/loop',
13-
'src/parallel_program',
14-
'src/symbolic_jump',
15-
'src/symbolic_memory',
7+
# 'src/covert_propogation',
8+
# 'src/crypto_functions',
9+
# 'src/data_overflow',
10+
# 'src/external_functions',
11+
# 'src/floating_point',
12+
# 'src/loop',
13+
# 'src/parallel_program',
14+
# 'src/symbolic_jump',
15+
# 'src/symbolic_memory',
1616

1717
# 'src_cpp/covert_propagation',
1818
# 'src_cpp/symbolic_jump',
1919
# 'src_cpp/symbolic_memory',
2020

21+
# 'sick/'
2122
# 'src/symbolic_variable_declaration',
2223
]
2324

@@ -49,8 +50,8 @@
4950
"python script/triton_caller.py -l%d -m%d -f%s -i%s -p triton/%s.out"
5051
]
5152

52-
angr_tp_path = 'templates/default.c'
53-
triton_tp_path = 'templates/default.c'
53+
angr_tp_path = 'templates/default_no_printf.c'
54+
triton_tp_path = 'templates/default_no_printf.c'
5455
klee_tp_path = 'templates/klee.c'
5556

5657
switches = {

run_tests.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@
55
import script_runner as sr
66
import shutil
77
import json
8+
import psutil
89

910
from config.test_settings import TRITON_INSTALLATION_PATH, FUNC_NAME
1011

1112

13+
def kill_all(process):
14+
parent = psutil.Process(process.pid)
15+
for child in parent.children(recursive=True):
16+
child.kill()
17+
parent.kill()
18+
19+
1220
def ATKrun(target , src_dirs, func_name='logic_bomb', default_stdin_len=10):
1321
def params_list_parser(params):
1422
if len(params.strip()) == 0:
@@ -41,7 +49,7 @@ def params_list_parser(params):
4149
TLE = 4
4250
RUNTIME_ERROR = 255
4351

44-
MAX_TIME = 1800
52+
MAX_TIME = 60
4553
test_results = {}
4654

4755
func_pattern = re.compile(r'int[ \t\n]+%s\(([^)]*)\);*' % func_name)
@@ -101,12 +109,13 @@ def params_list_parser(params):
101109
continue
102110
# Run test
103111
p = subprocess.Popen(cmds[1].split(' '))
112+
print(p.pid)
104113
try:
105114
rt_vale = p.wait(timeout=MAX_TIME)
106115
test_results[fp] = rt_vale
107116
except subprocess.TimeoutExpired:
108117
test_results[fp] = TLE
109-
p.kill()
118+
kill_all(p)
110119

111120
elif prefix == 'klee':
112121
if not os.path.exists('klee'):
@@ -137,7 +146,7 @@ def params_list_parser(params):
137146
rt_vale = p.wait(timeout=MAX_TIME)
138147
except subprocess.TimeoutExpired:
139148
test_results[fp] = TLE
140-
p.kill()
149+
kill_all(p)
141150
continue
142151

143152
p = subprocess.Popen(cmds[2].split(' '))
@@ -146,7 +155,7 @@ def params_list_parser(params):
146155
test_results[fp] = rt_vale
147156
except subprocess.TimeoutExpired:
148157
test_results[fp] = TLE
149-
p.kill()
158+
kill_all(p)
150159
shutil.rmtree('klee')
151160
elif prefix == 'triton':
152161
cmds.append(cmds_tp[0] % outname)
@@ -163,6 +172,7 @@ def params_list_parser(params):
163172
continue
164173

165174
# Run test
175+
print("=== Run test!", outname, "===")
166176
p = subprocess.Popen(cmds[1].split(' '))
167177
rt_vale = p.wait()
168178
test_results[fp] = rt_vale

script/triton_caller.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import re
55
import sys
66
import time
7+
import signal
8+
import psutil
9+
10+
from threading import Timer
711

812

913
parser = argparse.ArgumentParser()
@@ -32,7 +36,8 @@
3236

3337
print(' '.join([TRITON_INSTALLATION_PATH, 'triton/triton_run.py', prog]))
3438

35-
p = subprocess.Popen([TRITON_INSTALLATION_PATH, 'triton/triton_run.py', prog, '0' * args.length], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
39+
p = subprocess.Popen([TRITON_INSTALLATION_PATH, 'triton/triton_run.py', prog, '0' * args.length], stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setsid)
40+
print(p.pid)
3641
start = time.time()
3742
while time.time() - start < args.max_time:
3843
rt_value = p.poll()
@@ -42,7 +47,11 @@
4247
time.sleep(0.1)
4348
print(time.time() - start)
4449
if time.time() - start > args.max_time:
45-
print(p.kill())
50+
# p.kill()
51+
parent = psutil.Process(p.pid)
52+
for child in parent.children(recursive=True):
53+
child.kill()
54+
parent.kill()
4655
print('timeout!!!!')
4756
exit(4)
4857

@@ -56,28 +65,34 @@
5665
for testcase in pt.finditer(out):
5766
tmp = case_pt.findall(out)
5867
tmp = ''.join(list(map(chr, map(int, tmp))))
59-
print(repr(list(tmp)))
68+
print("New test case:", repr(list(tmp)))
6069
tmp = tmp.replace('\x00', '')
6170
reses.append(tmp)
6271

63-
print(reses)
72+
print "%d test case(s) generated" % len(reses)
6473

6574
tests = set()
6675
for res in reses:
67-
p = subprocess.Popen([prog, res])
76+
p = subprocess.Popen([prog, res], preexec_fn=os.setsid)
6877
start = time.time()
6978
while time.time() - start < args.max_time:
7079
rt_value = p.poll()
7180
if rt_value is not None:
72-
print(rt_value)
81+
print("Return Value:", rt_value)
7382
break
7483
time.sleep(0.1)
7584
if time.time() - start > args.max_time:
7685
tests.add(0)
77-
p.kill()
86+
print(p.pid)
87+
# p.kill()
88+
parent = psutil.Process(p.pid)
89+
for child in parent.children(recursive=True):
90+
child.kill()
91+
parent.kill()
92+
7893
print('\nTest case timeout!!!!\n')
7994
else:
80-
tests.add(0)
95+
tests.add(rt_value)
8196

8297
if args.expected is None:
8398
standard = {0, 1}

src/contextual_symbolic_value/pid_csv.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
int logic_bomb(int symvar) {
99
int pid = (int) getpid();
10-
printf ("current pid is %d\n%", pid);
1110
if(pid == symvar)
1211
return BOMB_ENDING;
1312
else

src/contextual_symbolic_value/ping_csv.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ int ping_it(struct in_addr *dst)
6565
if (rc <= 0) {
6666
perror("recvfrom");
6767
} else if (rc < sizeof rcv_hdr) {
68-
printf("Error, got short ICMP packet, %d bytes\n", rc);
68+
//printf("Error, got short ICMP packet, %d bytes\n", rc);
6969
}
7070
memcpy(&rcv_hdr, data, sizeof rcv_hdr);
7171
if (rcv_hdr.type == ICMP_ECHOREPLY) {
72-
printf("ICMP Reply, id=0x%x, sequence = 0x%x\n",
73-
icmp_hdr.un.echo.id, icmp_hdr.un.echo.sequence);
72+
//printf("ICMP Reply, id=0x%x, sequence = 0x%x\n",
73+
//icmp_hdr.un.echo.id, icmp_hdr.un.echo.sequence);
7474
} else {
75-
printf("Got ICMP packet with type 0x%x ?!?\n", rcv_hdr.type);
75+
//printf("Got ICMP packet with type 0x%x ?!?\n", rcv_hdr.type);
7676
}
7777
return 1;
7878
}
@@ -85,7 +85,7 @@ int logic_bomb(char* s) {
8585

8686
if (inet_aton(s, &dst) == 0) {
8787
perror("inet_aton");
88-
printf("%s isn't a valid IP address\n", s);
88+
//printf("%s isn't a valid IP address\n", s);
8989
return NORMAL_ENDING;
9090
}
9191

src/contextual_symbolic_value/syscall_csv.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88

99
// {"s":{"length": 16}}
1010
int logic_bomb(char* s) {
11-
int trigger = 0;
11+
if(s == NULL)
12+
return NORMAL_ENDING;
13+
if(s[0]=='\0')
14+
return NORMAL_ENDING;
15+
int trigger = -1;
1216
trigger = system(s);
1317
if(trigger == 0) {
1418
return BOMB_ENDING;

src/covert_propogation/file_cp.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@ int logic_bomb(int i) {
1414
FILE *fp = fopen(file, "ab+");
1515
if(fp == NULL)
1616
{
17-
printf("Error!");
17+
//printf("Error!");
1818
exit(1);
1919
}
2020
fprintf(fp,"%d",i);
2121
fclose(fp);
2222

2323
fp = fopen("tmp.covpro", "r");
2424
fscanf(fp,"%d",&j);
25-
printf("i = %d, j = %d\n", i,j);
2625
fclose(fp);
2726
remove(file);
2827
if(j == 7){

src/covert_propogation/socket_cp.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ int server(){
2323
bind(server_sockfd,(struct sockaddr *)&server_address,server_len);
2424

2525
listen(server_sockfd,5);
26-
printf("server waiting for connect\n");
26+
//printf("server waiting for connect\n");
2727

2828
client_len = sizeof(client_address);
2929
client_sockfd = accept(server_sockfd,(struct sockaddr *)&client_address,(socklen_t *)&client_len);
@@ -33,7 +33,7 @@ int server(){
3333
perror("recv");
3434
exit(EXIT_FAILURE);
3535
}
36-
printf("receive from client is %c\n",char_recv);
36+
//printf("receive from client is %c\n",char_recv);
3737

3838
char_send = char_recv;
3939
if(btye = send(client_sockfd,&char_send,1,0) == -1)
@@ -47,7 +47,7 @@ int server(){
4747
}
4848

4949
int client_send(char char_send){
50-
printf("client start\n");
50+
//printf("client start\n");
5151
int sockfd;
5252
int len;
5353
struct sockaddr_in address;
@@ -79,7 +79,7 @@ int client_send(char char_send){
7979
perror("recv");
8080
exit(EXIT_FAILURE);
8181
}
82-
printf("receive from server %c\n",char_recv);
82+
//printf("receive from server %c\n",char_recv);
8383
close(sockfd);
8484
return atoi(char_recv);
8585
}
@@ -97,7 +97,7 @@ int logic_bomb(char* s) {
9797
if(pid2 == 0){
9898
sleep(1);
9999
i=client_send(s[0]);
100-
printf("i=%d\n",i);
100+
//printf("i=%d\n",i);
101101
if(i==7){
102102
return BOMB_ENDING;
103103
}else{

src/covert_propogation/stack_cp.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ int logic_bomb(int i) {
66
int j;
77
__asm__ __volatile__("push %0" :: "m"(i));
88
__asm__ __volatile__("pop %0" :: "m"(j));
9-
printf("%d\n", j);
109
if(j == 7){
1110
return BOMB_ENDING;
1211
} else{

src/crypto_functions/aes_cf.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void aes_print(uint8_t* str) {
1818
// {"s":{"length": 32}}
1919
int logic_bomb(char* s) {
2020
if(strlen(s) != 32){
21-
printf("please input the 128-bit keys\n");
21+
//printf("please input the 128-bit keys\n");
2222
return NORMAL_ENDING;
2323
}
2424

@@ -42,15 +42,15 @@ int logic_bomb(char* s) {
4242
&key[12],&key[13],
4343
&key[14],&key[15]);
4444

45-
aes_print(key);
45+
//aes_print(key);
4646

4747
uint8_t decodetext[16];
4848
uint8_t ciphertext[] = {0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60, 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97};
4949
uint8_t plaintext[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
5050

5151
AES128_ECB_decrypt(ciphertext, key, decodetext);
5252

53-
aes_print(decodetext);
53+
//aes_print(decodetext);
5454
if(0 == memcmp((char*) plaintext, (char*) decodetext, 16)){
5555
return BOMB_ENDING;
5656
}else{

src/external_functions/atof_ef_l2.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
TOY:
3+
*/
4+
#include <string.h>
5+
#include <math.h>
6+
#include "utils.h"
7+
#include "a_tester.h"
8+
9+
// {"s":{"length": 3}}
10+
int logic_bomb(char* symvar) {
11+
float i = atof(symvar);
12+
if(i - 199 == 0){
13+
return BOMB_ENDING;
14+
}else{
15+
return NORMAL_ENDING;
16+
}
17+
}

src/external_functions/atoi_ef_l2.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
TOY:
3+
*/
4+
#include <string.h>
5+
#include <math.h>
6+
#include "utils.h"
7+
#include "a_tester.h"
8+
9+
// {"s":{"length": 3}}
10+
int logic_bomb(char* symvar) {
11+
int i = atoi(symvar);
12+
if(i==199){
13+
return BOMB_ENDING;
14+
}else{
15+
return NORMAL_ENDING;
16+
}
17+
}

0 commit comments

Comments
 (0)