forked from madthanu/alice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalicedefaultexplorer.py
252 lines (215 loc) · 9.46 KB
/
alicedefaultexplorer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env python
# Copyright (c) 2014 Thanumalayan Sankaranarayana Pillai. All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import subprocess
import cProfile
import Queue
import threading
import time
import pprint
import code
import sys
import collections
import gc
from alice import aliceconfig
from alice import Replayer
from alicedefaultfs import defaultfs
__author__ = "Thanumalayan Sankaranarayana Pillai"
__copyright__ = "Copyright 2014, Thanumalayan Sankaranarayana Pillai"
__credits__ = ["Thanumalayan Sankaranarayana Pillai", "Vijay Chidambaram",
"Ramnatthan Alagappan", "Samer Al-Kiswany"]
__license__ = "MIT"
class MultiThreadedChecker(threading.Thread):
queue = Queue.Queue()
outputs = {}
def __init__(self, queue, thread_id='0'):
threading.Thread.__init__(self)
self.queue = MultiThreadedChecker.queue
self.thread_id = str(thread_id)
def __threaded_check(self, dirname, crashid):
assert type(aliceconfig().checker_tool) in [list, str, tuple]
args = [aliceconfig().checker_tool, dirname, dirname + '.input_stdout', self.thread_id]
output_stdout = dirname + '.output_stdout'
output_stderr = dirname + '.output_stderr'
retcode = subprocess.call(args, stdout = open(output_stdout, 'w'), stderr = open(output_stderr, 'w'))
MultiThreadedChecker.outputs[crashid] = retcode
os.system('rm -rf ' + dirname)
def run(self):
while True:
task = self.queue.get()
self.__threaded_check(*task)
self.queue.task_done()
@staticmethod
def check_later(dirname, retcodeid):
MultiThreadedChecker.queue.put((dirname, retcodeid))
@staticmethod
def reset():
assert MultiThreadedChecker.queue.empty()
MultiThreadedChecker.outputs = {}
@staticmethod
def wait_and_get_outputs():
MultiThreadedChecker.queue.join()
return MultiThreadedChecker.outputs
def stack_repr(op):
try:
backtrace = 0
try:
backtrace = op.hidden_backtrace
except:
pass
found = False
#code.interact(local=dict(globals().items() + locals().items()))
for i in range(0, len(backtrace)):
stack_frame = backtrace[i]
if stack_frame.src_filename != None and 'syscall-template' in stack_frame.src_filename:
continue
if '/libc' in stack_frame.binary_filename:
continue
if stack_frame.func_name != None and 'output_stacktrace' in stack_frame.func_name:
continue
found = True
break
if not found:
raise Exception('Standard stack traverse did not work')
if stack_frame.src_filename == None:
return 'B-' + str(stack_frame.binary_filename) + ':' + str(stack_frame.raw_addr) + '[' + str(stack_frame.func_name).replace('(anonymous namespace)', '()') + ']'
return str(stack_frame.src_filename) + ':' + str(stack_frame.src_line_num) + '[' + str(stack_frame.func_name).replace('(anonymous namespace)', '()') + ']'
except Exception as e:
return 'Unknown (stacktraces not traversable for finding static vulnerabilities):' + op.hidden_id
def default_checks(alice_args, threads = 1):
print 'Parsing traces to determine logical operations ...'
replayer = Replayer(alice_args)
replayer.set_fs(defaultfs('count', 1))
print 'Logical operations:'
replayer.print_ops()
assert threads > 0
for i in range(0, threads):
t = MultiThreadedChecker(MultiThreadedChecker.queue, i)
t.setDaemon(True)
t.start()
atomic_patch_middle = set()
print 'Finding vulnerabilities...'
# Finding across-syscall atomicity
for i in range(0, replayer.mops_len()):
dirname = os.path.join(aliceconfig().scratchpad_dir, 'reconstructeddir-' + str(i))
replayer.dops_end_at((i, replayer.dops_len(i) - 1))
replayer.construct_crashed_dir(dirname, dirname + '.input_stdout')
MultiThreadedChecker.check_later(dirname, i)
checker_outputs = MultiThreadedChecker.wait_and_get_outputs()
staticvuls = set()
i = 0
while(i < replayer.mops_len()):
if checker_outputs[i] != 0:
patch_start = i
atomic_patch_middle.add(i)
# Go until the last but one mop
while(i < replayer.mops_len() - 1 and checker_outputs[i + 1] != 0):
atomic_patch_middle.add(i)
i += 1
patch_end = i + 1
if patch_end >= replayer.mops_len():
patch_end = replayer.mops_len() - 1
print 'WARNING: Application found to be inconsistent after the entire workload completes. Recheck workload and checker. Possible bug in ALICE framework if this is not expected.'
print '(Dynamic vulnerability) Across-syscall atomicity, sometimes concerning durability: ' + \
'Operations ' + str(patch_start) + ' until ' + str(patch_end) + ' need to be atomically persisted'
staticvuls.add((stack_repr(replayer.get_op(patch_start)),
stack_repr(replayer.get_op(patch_end))))
i += 1
for vul in staticvuls:
print '(Static vulnerability) Across-syscall atomicity: ' + \
'Operation ' + vul[0] + ' until ' + vul[1]
# Finding ordering vulnerabilities
replayer.load(0)
MultiThreadedChecker.reset()
for i in range(0, replayer.mops_len()):
if replayer.dops_len(i) == 0 or i in atomic_patch_middle or (i - 1) in atomic_patch_middle:
continue
for j in range(0, replayer.dops_len(i)):
replayer.dops_omit((i, j))
for j in range(i + 1, replayer.mops_len()):
if replayer.dops_len(j) == 0 or j in atomic_patch_middle:
continue
replayer.dops_end_at((j, replayer.dops_len(j) - 1))
if replayer.is_legal():
dirname = os.path.join(aliceconfig().scratchpad_dir, 'reconstructeddir-' + str(i) + '-' + str(j))
replayer.construct_crashed_dir(dirname, dirname + '.input_stdout')
MultiThreadedChecker.check_later(dirname, (i, j))
for j in range(0, replayer.dops_len(i)):
replayer.dops_include((i, j))
checker_outputs = MultiThreadedChecker.wait_and_get_outputs()
staticvuls = set()
for i in range(0, replayer.mops_len()):
for j in range(i + 1, replayer.mops_len()):
if (i, j) in checker_outputs and checker_outputs[(i, j)] != 0:
print '(Dynamic vulnerability) Ordering: ' + \
'Operation ' + str(i) + ' needs to be persisted before ' + str(j)
staticvuls.add((stack_repr(replayer.get_op(i)),
stack_repr(replayer.get_op(j))))
break
for vul in staticvuls:
print '(Static vulnerability) Ordering: ' + \
'Operation ' + vul[0] + ' needed before ' + vul[1]
# Finding atomicity vulnerabilities
replayer.load(0)
MultiThreadedChecker.reset()
atomicity_explanations = dict()
for mode in (('count', 1), ('count', 3), ('aligned', 4096)):
replayer.set_fs(defaultfs(*mode))
for i in range(0, replayer.mops_len()):
if i in atomic_patch_middle or (i - 1) in atomic_patch_middle:
continue
for j in range(0, replayer.dops_len(i) - 1):
replayer.dops_end_at((i, j))
if replayer.is_legal():
dirname = os.path.join(aliceconfig().scratchpad_dir, 'reconstructeddir-' + mode[0] + '-' + str(mode[1]) + '-' + str(i) + '-' + str(j))
replayer.construct_crashed_dir(dirname, dirname + '.input_stdout')
MultiThreadedChecker.check_later(dirname, (mode, i, j))
atomicity_explanations[(mode, i, j)] = replayer.get_op(i).hidden_disk_ops[j].atomicity
if mode != ('aligned', '4096'): # Do not do this for the 4096 aligned case, since a large write might contain a huge number of diskops
for k in range(0, j):
replayer.dops_omit((i, k))
if replayer.is_legal():
dirname = os.path.join(aliceconfig().scratchpad_dir, 'reconstructeddir-' + mode[0] + '-' + str(mode[1]) + '-' + str(i) + '-' + str(j) + '-' + str(k))
replayer.construct_crashed_dir(dirname, dirname + '.input_stdout')
MultiThreadedChecker.check_later(dirname, (mode, i, j, k))
replayer.dops_include((i, k))
checker_outputs = MultiThreadedChecker.wait_and_get_outputs()
staticvuls = collections.defaultdict(lambda:set())
for i in range(0, replayer.mops_len()):
dynamicvuls = set()
for j in range(0, replayer.dops_len(i) - 1):
for mode in (('count', 1), ('count', 3), ('aligned', 4096)):
if (mode, i, j) in checker_outputs and checker_outputs[(mode, i, j)] != 0:
dynamicvuls.add(atomicity_explanations[(mode, i, j)])
if len(dynamicvuls) == 0:
for j in range(0, replayer.dops_len(i) - 1):
for mode in (('count', 1), ('count', 3), ('aligned', 4096)):
for k in range(0, j):
if (mode, i, j, k) in checker_outputs and checker_outputs[(mode, i, j, k)] != 0:
dynamicvuls.add('???')
if len(dynamicvuls) > 0:
print '(Dynamic vulnerability) Atomicity: ' + \
'Operation ' + str(i) + '(' + (', '.join(dynamicvuls)) + ')'
staticvuls[stack_repr(replayer.get_op(i))].update(dynamicvuls)
for vul in staticvuls:
print '(Static vulnerability) Atomicity: ' + \
'Operation ' + vul + ' (' + (','.join(staticvuls[vul])) + ')'
print 'Done finding vulnerabilities.'