-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcheck_security.sh
More file actions
executable file
·245 lines (224 loc) · 7.8 KB
/
check_security.sh
File metadata and controls
executable file
·245 lines (224 loc) · 7.8 KB
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
#!/bin/bash
# Physical directory where the script is located
_base=$(e=$0;while test -L "$e";do d=$(dirname "$e");e=$(readlink "$e");\
cd "$d";done;cd "$(dirname "$e")";pwd -P)
function usage () {
printf "Usage: check_security [<options>...]
-t TIMEOUT
-m COMPILER intel, microsoft, clang, gcc
-q OPTIMIZATIONS
-p TARGETS
-s IGNORE Don't analyze the specified targets
-d SUITE test, benchmarks, new, all
-o DIR Output directory
-f FLAG Pass all the flags to spectector
-r ANALYZE A folder o file with all the assembly files
wanted to analyze
-i Analyze all the functions of the files
-z JOBS Number of parallel jobs launched
-j File where the jobs are going to be written
By default it will be executed with all the compilers, all
the Paul Kocher examples and a timeout of 30 seconds
To pass a list as an option argument, all the elements
must be quoted and separated by spaces.
"
exit 0
}
function produce_output () { # 1st parameter is target, 2nd outjson, 3rd func, 4th ret
[ "$4" -eq 124 ] && printf "{\"name\":\"%s\",\"status\":\"timeout\",\"file\":\"%s\",\"entry\":\"%s\"}," "$1" "$2" "$3" > "$2" # timeout
[ "$4" -eq 139 ] && printf "{\"name\":\"%s\",\"status\":\"segfault\",\"file\":\"%s\",\"entry\":\"%s\"}," "$1" "$2" "$3" > "$2" # segfault
}
export -f produce_output
function summarize_results () {
printf "results=[" > "$jsonfile"
cat "$outdir"/*.json >> "$jsonfile"
printf "{\"name\":\"summary\",\"flags\":\"%s\"}]" "${flags[@]}" >> "$jsonfile" # TODO: Fix
}
function clean_up { # TODO: Kill parallel instances
kill $LAST 2> /dev/null
summarize_results
exit 1
}
gen=(microsoft "microsoft-19" intel clang gcc)
timeout=30
IFS=' '
results=results
opts=(.o0 .o2)
# Suites
source suites
tests="${old[@]} ${new[@]}"
all="${tests[@]} ${benchs[@]}"
cases=${old[@]}
jobs_file=/tmp/jobs
# Parsing of the arguments
while getopts ":m:p:t:d:o:s:f:q:r:i:z:j:" option; do
case "${option}" in
m) gen=($OPTARG) ;;
p) cases=($OPTARG) ;;
t) timeout=${OPTARG} ;;
d) suite=${OPTARG[@]}[@]
cases=${!suite} ;;
q) opts=($OPTARG) ;;
o) results=$OPTARG;;
s) delete=($OPTARG);;
f) flags=$OPTARG;;
i) get_entry=$OPTARG;;
r) raw=$OPTARG
gen=();;
z) n_parallel=$OPTARG;;
j) jobs_file=$OPTARG;;
* ) usage ;;
esac
done
trap clean_up SIGINT SIGTERM EXIT
cd "$_base"
# Change the path to run spectector
# This works if you are in spectector folder or one of it's subfolders
if [ -x ./spectector ]; then
spectector=./spectector
elif [ -x ../bin/spectector ]; then
spectector=../bin/spectector
elif [ -x bin/spectector ]; then
spectector=bin/spectector
elif which spectector > /dev/null 2>&1; then
# spectector in PATH
spectector=spectector
else
cat <<EOF
ERROR: `spectector` not found
EOF
exit 1
fi
for del in ${delete[@]}; do
cases=("${cases[@]/$del}")
done
# Check if the output is a directory
if ! [ -d $results ]; then
printf "$results is not a directory\n"
[[ "$(read -e -p 'Do you want to create results folder? [y/N]> '; echo $REPLY)" == [Yy]* ]] && mkdir results || exit 1
fi
jsonfile=$results/stats.json
# folder with the results for each benchmark
outdir=$results/out
# Store old results
old_folder=$results/$(date "+%Y.%m.%d-%H.%M.%S")
mkdir -p $old_folder
find $results -maxdepth 1 -type f -exec mv {} $old_folder \;
mv $outdir $old_folder 2> /dev/null
mkdir -p "$outdir" # Write the output files
if which gtimeout > /dev/null 2>&1; then
runtimeout=gtimeout # for macOS (GNU coreutils)
else
runtimeout=timeout
fi
if ! [ -z $raw ]; then
if [ -d $get_entry ]; then
printf "Analyzing all the functions defined in %s\n" "$raw"
dir=$(dirname $raw)
type=$(basename $dir)
echo "" > $jobs_file
for assfile in $get_entry/*.s; do
name=$(basename $assfile)
if ! [ -f $raw ]; then
target=$raw
else
target=$raw/$name
fi
while read -r func; do
outf="$outdir/$type.$name.$func.out"
outjson="$outdir/$type.$name.$func.json"
outerr="$outdir/$type.$name.$func.err"
single_func="[$func]"
# Check of parsing
echo "printf \"%s\t%s\n\" \"$target\" \"$func\"; $spectector $target $flags -e $single_func -a none > $outf 2> $outerr; if [ $? -ne 0 ]; then printf \"{\\\"name\\\":\\\"%s\\\",\\\"status\\\":\\\"parsing\\\",\\\"file\\\":\\\"%s\\\",\\\"entry\\\":\\\"%s\\\"},\" \"$target\" \"$outjson\" \"$func\" > \"$outjson\"; exit; fi; $runtimeout $timeout $spectector $target $flags --stats \"$outjson\" -e $single_func > $outf 2> $outerr; ret=\$?; if [ \$ret -eq 124 ]; then printf \"{\\\"name\\\":\\\"%s\\\",\\\"status\\\":\\\"timeout\\\",\\\"file\\\":\\\"%s\\\",\\\"entry\\\":\\\"%s\\\"},\" \"$target\" \"$outjson\" \"$func\" > \"$outjson\"; elif [ \$ret -eq 139 ]; then printf \"{\\\"name\\\":\\\"%s\\\",\\\"status\\\":\\\"segfault\\\",\\\"file\\\":\\\"%s\\\",\\\"entry\\\":\\\"%s\\\"},\" \"$target\" \"$outjson\" \"$func\" > \"$outjson\"; elif [ \$ret -ne 0 ]; then printf \"{\\\"name\\\":\\\"%s\\\",\\\"status\\\":\\\"unknown_error\\\",\\\"file\\\":\\\"%s\\\",\\\"entry\\\":\\\"%s\\\"},\" \"$target\" \"$outjson\" \"$func\" > \"$outjson\"; fi" >> $jobs_file
done < <(./scripts/get_function_names.sh $assfile)
done
if [ -z $n_parallel ]; then parallel :::: $jobs_file; else parallel -j$n_parallel :::: $jobs_file; fi
summarize_results
exit 0
elif [ -f $raw ]; then
dir=$(dirname $raw)
type=$(basename $dir)
while read -r line || [[ -n "$line" ]]; do
to_analyze=($line)
target=$dir/${to_analyze[0]}
func=${to_analyze[1]}
if [ -f $target ] && grep "\<$func:" $target > /dev/null; then
outf="$outdir/$type.${to_analyze[0]}.$func.out"
outjson="$outdir/$type.${to_analyze[0]}.$func.json"
outerr="$outdir/$type.${to_analyze[0]}.$func.err"
printf "%s\t%s\n" "$target" "$func"
$runtimeout $timeout $spectector $target $flags --stats "$outjson"\
-e $func > $outf 2> $outerr &
LAST=$!
wait $LAST
ret=$?
produce_output
# else
# printf "$target doesn't exist or $func doesn't exist\n"
fi
done < $raw
summarize_results
exit 0
else
printf "Review the parameters passed\n"
exit 1
fi
fi
for app in ${cases[@]}; do
if [ $get_entry ]; then
# TODO: Change source?
entries="./scripts/get_function_names.sh target/clang/$app/any.o2.s"
else
entries="echo no-entry"
fi
$entries | while read func; do
if [ $get_entry ]; then entry_flag="-e $func";
fi
for comp in ${gen[@]}; do
folder=target/$comp/$app
experiments=(any lfence slh)
extension=s
case "$comp" in
"intel") experiments=(any lfence);;
"gcc") experiments=(any slh);;
"microsoft") experiments=(any lfence) && extension=asm;;
"microsoft-19") experiments=(any lfence) && extension=asm;;
esac
for ex in ${experiments[@]}; do
for opt in ${opts[@]}; do
target=$folder/$ex$opt.$extension
if ! [ -f $target ]; then
printf "%s doesn't exist\n" "$target"
else
f_target=$(basename $target)
name="${f_target%.*}"
mitigation="${name%.*}"
type="${name##*.}"
if [ $get_entry ]; then
printf "%s\n" "$comp-$app-$f_target-$func"
outf="$outdir/${comp}.${app}.${f_target}.$func.out"
outjson="$outdir/${comp}.${app}.${f_target}.$func.json"
outerr="$outdir/${comp}.${app}.${f_target}.$func.err"
else
printf "%s\n" "$comp-$app-$f_target"
outf="$outdir/${comp}.${app}.${f_target}.out"
outjson="$outdir/${comp}.${app}.${f_target}.json"
outerr="$outdir/${comp}.${app}.${f_target}.err"
fi # (show progress)
$runtimeout $timeout $spectector "$target" $flags\
--stats $outjson $entry_flag > "$outf" 2> $outerr &
LAST=$!
wait $LAST
ret=$?
produce_output $target $outjson $func $ret
fi
done
done
done
done
done
summarize_results
exit 0
# echo "Comparison with saved results: "
# diff ../results/summary.txt ../results/summary.txt-ok && echo ok