Repository navigation
Expand file tree
/
Copy pathTaintTracking.v
More file actions
276 lines (243 loc) · 8.71 KB
/
Copy pathTaintTracking.v
File metadata and controls
276 lines (243 loc) · 8.71 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
Set Warnings "-notation-overridden,-parsing,-deprecated-hint-without-locality".
From Stdlib Require Import Strings.String.
From SECF Require Import Maps.
From Stdlib Require Import Bool.Bool.
From Stdlib Require Import Arith.Arith.
From Stdlib Require Import Arith.EqNat.
From Stdlib Require Import Arith.PeanoNat.
From Stdlib Require Import Lia.
From Stdlib Require Import List. Import ListNotations.
Set Default Goal Selector "!".
From QuickChick Require Import QuickChick Tactics.
Import QcNotation QcDefaultNotation. Open Scope qc_scope.
Require Export ExtLib.Structures.Monads.
Require Import ExtLib.Data.List.
Import MonadNotation.
From Stdlib Require Import String.
From SECF Require Import ListMaps MapsFunctor MiniCET Utils TestingSemantics.
Require Import Stdlib.Classes.EquivDec.
Notation t_apply := ListTotalMap.t_apply.
Definition reg_id := string.
Definition mem_addr := nat.
Definition taint : Type := list (reg_id + mem_addr).
#[export] Instance showTaint : Show (reg_id + mem_addr) :=
{show := fun x =>
match x with
| inl x => show x
| inr a => show a
end}.
Definition sum_eqb (s1 s2 : (reg_id + mem_addr)) : bool :=
match s1, s2 with
| inl x1, inl x2 => String.eqb x1 x2
| inr x1, inr x2 => Nat.eqb x1 x2
| _, _ => false
end.
Definition join_taints t1 t2 := remove_dupes sum_eqb (t1 ++ t2).
Module TaintTracking (Import ST: Semantics ListTotalMap).
Definition tcptr := taint.
Definition treg := ListTotalMap.t taint.
Definition tamem := list taint.
Definition tstk := list tcptr.
Definition tcfg := (tcptr * treg * tamem * tstk)%type.
Definition input_st : Type := ST.cfg * tcfg * taint.
Variant output_st (A : Type) : Type :=
| RStep : A -> obs -> input_st -> output_st A
| RError : obs -> input_st -> output_st A
| RTerm : obs -> input_st -> output_st A.
Record evaluator (A : Type) : Type := mkEvaluator
{ evaluate : input_st -> output_st A }.
Definition interpreter: Type := evaluator unit.
#[export] Instance monadEvaluator: Monad evaluator :=
{ ret := fun A value => mkEvaluator A (fun (ist : input_st) => RStep A value [] ist);
bind := fun A B e f =>
mkEvaluator B (fun (ist : input_st) =>
match evaluate A e ist with
| RStep _ value os1 ist' =>
match evaluate B (f value) ist' with
| RStep _ value os2 ist'' => RStep B value (os1 ++ os2) ist''
| RError _ os2 ist'' => RError B (os1 ++ os2) ist''
| RTerm _ os2 ist'' => RTerm B (os1 ++ os2) ist''
end
| RError _ os ist' => RError B os ist'
| RTerm _ os ist' => RTerm B os ist'
end
)
}.
Fixpoint _calc_taint_exp (e: exp) (tr: treg) : taint :=
match e with
| ANum _ | FPtr _ => []
| AId x => t_apply tr x
| ABin _ e1 e2 => join_taints (_calc_taint_exp e1 tr) (_calc_taint_exp e2 tr)
| ACTIf e1 e2 e3 => join_taints (_calc_taint_exp e1 tr)
(join_taints (_calc_taint_exp e2 tr) (_calc_taint_exp e3 tr))
end.
Variant taint_ctx :=
| CMem (n: nat)
| CDefault.
Definition taint_step (i: inst) (c: ST.cfg) (tc: tcfg) (tobs: taint) (tctx: taint_ctx) : option (tcfg * taint) :=
let '(pc, rs, m, s) := c in
let '(tpc, trs, tm, ts) := tc in
match i with
| <{ skip }> | <{ ctarget }> | <{ jump _ }> =>
match tctx with
| CDefault => Some (tc, tobs)
| _ => None
end
| <{ x := e }> =>
match tctx with
| CDefault =>
let te := (_calc_taint_exp e trs) in
let tx := join_taints te tpc in
let tc' := (tpc, (x !-> tx; trs), tm, ts) in
Some (tc', tobs)
| _ => None
end
(* YF: TBD with Yonghyun *)
| <{ x <- div e1, e2 }> =>
match tctx with
| CDefault =>
let te1 := _calc_taint_exp e1 trs in
let te2 := _calc_taint_exp e2 trs in
let tx := join_taints (join_taints te1 te2) tpc in
let tc' := (tpc, (x !-> tx; trs), tm, ts) in
let tobs' := join_taints tobs (join_taints (join_taints te1 te2) tpc) in (* YF: We add the taints of v1 and v2, since the actual observation would be [ODiv v1 v2], right? *)
Some (tc', tobs')
| _ => None
end
| <{ branch e to l }> =>
match tctx with
| CDefault =>
let te := (_calc_taint_exp e trs) in
let tpc' := join_taints te tpc in
let tc' := (tpc', trs, tm, ts) in
let tobs' := join_taints tobs tpc' in
Some (tc', tobs')
| _ => None
end
| <{ x <- load[e] }> =>
match tctx with
| CMem n =>
let te := (_calc_taint_exp e trs) in
let tv := nth n tm [] in
let tx := (join_taints (join_taints te tv) tpc) in
let tc' := (tpc, (x !-> tx; trs), tm, ts) in
let tobs' := (join_taints (join_taints te tpc) tobs) in
Some (tc', tobs')
| _ => None
end
| <{ store[e] <- e' }> =>
match tctx with
| CMem n =>
let te := (_calc_taint_exp e trs) in (* *)
let te' := (_calc_taint_exp e' trs) in
let tm_val := join_taints (* (join_taints te te') *) te' tpc in (* YH: te is actually need for tm_val? let's remove te and run the tests to see. *)
let tc' := (tpc, trs, upd n tm tm_val, ts) in
let tobs' := join_taints (join_taints te tpc) tobs in
Some (tc', tobs')
| _ => None
end
| <{ call e }> =>
match tctx with
| CDefault =>
let te := (_calc_taint_exp e trs) in
let ts' := tpc :: ts in
let tpc' := join_taints te tpc in
let tc' := (tpc', trs, tm, ts') in
let tobs' := join_taints tobs tpc' in
Some (tc', tobs')
| _ => None
end
(* YF: TBD with Yonghyun *)
| <{ x <- peek }> =>
match tctx with
| CDefault =>
let tx' := hd [] ts in
let tc' := (tpc, (x !-> tx'; trs), tm, ts) in
Some (tc', tobs)
| _ => None
end
| <{ ret }> =>
match tctx with
| CDefault =>
let tpc' := hd [] ts in
let ts' := tl ts in
let tc' := (tpc', trs, tm, ts') in
Some (tc', tobs)
| _ => None
end
end.
Definition get_ctx (rs: ST.reg) (i: inst) : option taint_ctx :=
match i with
| <{ x <- load[e] }> => n <- to_nat (ST.eval rs e);;
Some (CMem n)
| <{ store[e] <- e' }> => n <- to_nat (ST.eval rs e);;
Some (CMem n)
| _ => Some CDefault
end.
Definition final_cfg (p: prog) (c: ST.cfg) : bool :=
let '(pc, rs, m, stk) := c in
match ST.fetch p pc with
| Some i => match i with
| IRet => if seq.nilp stk then true else false
| _ => false
end
| None => false
end.
Definition step_taint_track (p: prog) : evaluator unit :=
mkEvaluator _ (fun (ist : input_st) =>
let '(c, tc, tobs) := ist in
let '(pc, rs, m, s) := c in
let '(tpc, trs, tm, ts) := tc in
match ST.step p (S_Running c) with
| (S_Running c', os) =>
match (ST.fetch p pc) with
| Some i => match get_ctx rs i with
| Some tctx => match taint_step i c tc tobs tctx with
| Some (tc', tobs') => RStep _ tt os (c', tc', tobs')
| _ => RError _ [] ist
end
| _ => RError _ [] ist
end
| _ => RError _ [] ist
end
| (S_Term, []) => RTerm _ [] ist
| _ => RError _ [] ist
end).
Variant exec_result : Type :=
| ETerm (st: input_st) (os: obs)
| EError (st: input_st) (os: obs)
| EOutOfFuel (st: input_st) (os: obs).
Fixpoint steps_taint_track (f: nat) (p: prog) (ist: input_st) (os: obs) : exec_result :=
match f with
| O => EOutOfFuel ist os
| S f' =>
match evaluate unit (step_taint_track p) ist with
| RTerm _ os' ist' => ETerm ist' (os ++ os')
| RError _ os' ist' => EError ist' (os ++ os')
| RStep _ _ os' ist' => steps_taint_track f' p ist' (os ++ os')
end
end.
Fixpoint _init_taint_mem (m: mem) (n: nat) : tamem :=
match m with
| [] => []
| h :: m' => ([inr n]) :: _init_taint_mem m' (S n)
end.
Definition init_taint_mem (m: mem) : tamem :=
_init_taint_mem m 0.
Definition taint_tracking (f : nat) (p : prog) (c: cfg)
: option (obs * list string * list nat) :=
let '(pc, rs, m, ts) := c in
let tpc := [] in
let trs := ([], map (fun x => (x,[@inl reg_id mem_addr x])) (map_dom (snd rs))) in
let tm := init_taint_mem m in
let ts := [] in
let tc := (tpc, trs, tm, ts) in
let ist := (c, tc, []) in
match (steps_taint_track f p ist []) with
| ETerm (_, _, tobs) os | EOutOfFuel (_, _, tobs) os =>
let (ids, mems) := split_sum_list tobs in
Some (os, remove_dupes String.eqb ids,
remove_dupes Nat.eqb mems)
| _ => None
end.
End TaintTracking.