Skip to content

Commit b173273

Browse files
committed
[taintAnalysis] merge alarm types for patron
1 parent b2c96ee commit b173273

File tree

3 files changed

+102
-13
lines changed

3 files changed

+102
-13
lines changed

src/instance/taintAnalysis.ml

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,35 @@ let inspect_aexp_bo node aexp itvmem mem queries =
106106
}
107107
:: queries)
108108
taint queries
109+
| BufferOverrunLib ("fread", [ _; _; cnt ], loc) ->
110+
let pid = InterCfg.Node.get_pid node in
111+
let taint = TaintSem.eval pid cnt itvmem mem |> TaintDom.Val.user_input in
112+
TaintDom.UserInput.fold
113+
(fun (src_node, src_loc) queries ->
114+
let desc =
115+
"source = " ^ Node.to_string src_node ^ " @ "
116+
^ CilHelper.s_location src_loc
117+
in
118+
{
119+
node;
120+
exp = aexp;
121+
loc;
122+
allocsite = None;
123+
status = UnProven;
124+
desc;
125+
src = Some (src_node, src_loc);
126+
}
127+
:: queries)
128+
taint queries
109129
| _ -> queries
110130

111-
let inspect_aexp_mul node aexp itvmem mem queries =
131+
let inspect_aexp_io node aexp itvmem mem queries =
112132
match aexp with
113-
| MulExp (e, loc) ->
133+
| PlusIOExp (e, loc)
134+
| MinusIOExp (e, loc)
135+
| MultIOExp (e, loc)
136+
| ShiftIOExp (e, loc)
137+
| CastIOExp (e, loc) ->
114138
let pid = InterCfg.Node.get_pid node in
115139
let tv = TaintSem.eval pid e itvmem mem in
116140
let int_overflow, taint =
@@ -164,6 +188,30 @@ let inspect_aexp_dz node aexp itvmem mem queries =
164188
taint queries
165189
| _ -> queries
166190

191+
let inspect_aexp_nd node aexp itvmem mem queries =
192+
match aexp with
193+
| DerefExp (e, loc) ->
194+
let pid = InterCfg.Node.get_pid node in
195+
let taint = TaintSem.eval pid e itvmem mem |> TaintDom.Val.user_input in
196+
TaintDom.UserInput.fold
197+
(fun (src_node, src_loc) queries ->
198+
let desc =
199+
"source = " ^ Node.to_string src_node ^ " @ "
200+
^ CilHelper.s_location src_loc
201+
in
202+
{
203+
node;
204+
exp = aexp;
205+
loc;
206+
allocsite = None;
207+
status = UnProven;
208+
desc;
209+
src = Some (src_node, src_loc);
210+
}
211+
:: queries)
212+
taint queries
213+
| _ -> queries
214+
167215
let generate spec (global, mem, target) =
168216
let nodes = InterCfg.nodesof global.icfg in
169217
let total = List.length nodes in
@@ -181,9 +229,9 @@ let generate spec (global, mem, target) =
181229
else
182230
match target with
183231
| BO -> inspect_aexp_bo node aexp ptrmem mem
184-
| IO -> inspect_aexp_mul node aexp ptrmem mem
232+
| IO -> inspect_aexp_io node aexp ptrmem mem
185233
| DZ -> inspect_aexp_dz node aexp ptrmem mem
186-
| ND -> Fun.id)
234+
| ND -> inspect_aexp_nd node aexp ptrmem mem)
187235
aexps qs
188236
in
189237
(qs, k + 1))
@@ -192,7 +240,11 @@ let generate spec (global, mem, target) =
192240

193241
let inspect_alarm global spec inputof =
194242
(if !Options.bo then generate spec (global, inputof, Report.BO) else [])
195-
@ (if !Options.mul then generate spec (global, inputof, Report.IO) else [])
243+
@ (if
244+
!Options.mult_io || !Options.plus_io || !Options.minus_io
245+
|| !Options.shift_io || !Options.cast_io
246+
then generate spec (global, inputof, Report.IO)
247+
else [])
196248
@ (if !Options.nd then generate spec (global, inputof, Report.ND) else [])
197249
@ if !Options.dz then generate spec (global, inputof, Report.DZ) else []
198250

@@ -260,6 +312,6 @@ let do_analysis (global, itvdug, itvinputof) =
260312
let _ = Options.pfs := 100 in
261313
let dug = Analysis.generate_dug spec global in
262314
(if !Options.marshal_in then marshal_in global
263-
else Analysis.perform spec global dug)
315+
else Analysis.perform spec global dug)
264316
|> opt !Options.marshal_out marshal_out
265317
|> post_process spec itvdug

src/report/alarmExp.ml

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ module F = Format
1717
type t =
1818
| ArrayExp of lval * exp * location
1919
| DerefExp of exp * location
20-
| MulExp of exp * location
20+
| PlusIOExp of exp * location
21+
| MinusIOExp of exp * location
22+
| MultIOExp of exp * location
23+
| ShiftIOExp of exp * location
24+
| CastIOExp of exp * location
2125
| DivExp of exp * location
2226
| Strcpy of exp * exp * location
2327
| Strcat of exp * exp * location
@@ -32,7 +36,13 @@ let to_string t =
3236
match t with
3337
| ArrayExp (lv, e, _) -> CilHelper.s_lv lv ^ "[" ^ CilHelper.s_exp e ^ "]"
3438
| DerefExp (e, _) -> "*(" ^ CilHelper.s_exp e ^ ")"
35-
| MulExp (e, _) | DivExp (e, _) -> CilHelper.s_exp e
39+
| DivExp (e, _)
40+
| PlusIOExp (e, _)
41+
| MinusIOExp (e, _)
42+
| MultIOExp (e, _)
43+
| ShiftIOExp (e, _)
44+
| CastIOExp (e, _) ->
45+
CilHelper.s_exp e
3646
| Strcpy (e1, e2, _) ->
3747
"strcpy (" ^ CilHelper.s_exp e1 ^ ", " ^ CilHelper.s_exp e2 ^ ")"
3848
| Strncpy (e1, e2, e3, _) ->
@@ -54,7 +64,11 @@ let to_string t =
5464
let location_of = function
5565
| ArrayExp (_, _, l)
5666
| DerefExp (_, l)
57-
| MulExp (_, l)
67+
| PlusIOExp (_, l)
68+
| MinusIOExp (_, l)
69+
| MultIOExp (_, l)
70+
| ShiftIOExp (_, l)
71+
| CastIOExp (_, l)
5872
| DivExp (_, l)
5973
| Strcpy (_, _, l)
6074
| Strncpy (_, _, _, l)
@@ -100,11 +114,18 @@ and c_exp e loc =
100114
| UnOp (_, e, _) -> c_exp e loc
101115
| BinOp (bop, e1, e2, _) -> (
102116
match bop with
103-
| Mult when !Options.mul ->
104-
(MulExp (e, loc) :: c_exp e1 loc) @ c_exp e2 loc
117+
| PlusA when !Options.plus_io ->
118+
(PlusIOExp (e, loc) :: c_exp e1 loc) @ c_exp e2 loc
119+
| MinusA when !Options.minus_io ->
120+
(MinusIOExp (e, loc) :: c_exp e1 loc) @ c_exp e2 loc
121+
| Mult when !Options.mult_io ->
122+
(MultIOExp (e, loc) :: c_exp e1 loc) @ c_exp e2 loc
123+
| (Shiftlt | Shiftrt) when !Options.shift_io ->
124+
(ShiftIOExp (e, loc) :: c_exp e1 loc) @ c_exp e2 loc
105125
| Div | Mod -> (DivExp (e, loc) :: c_exp e1 loc) @ c_exp e2 loc
106126
| _ -> c_exp e1 loc @ c_exp e2 loc)
107-
| CastE (_, e) -> c_exp e loc
127+
| CastE (_, e') when !Options.cast_io -> CastIOExp (e, loc) :: c_exp e' loc
128+
| CastE (_, e') -> c_exp e' loc
108129
| AddrOf lv -> c_lv lv loc
109130
| StartOf lv -> c_lv lv loc
110131
| _ -> []
@@ -121,6 +142,7 @@ let query_lib =
121142
"memchr";
122143
"strncmp";
123144
"sprintf";
145+
"fread";
124146
]
125147

126148
let c_lib f es loc =
@@ -139,6 +161,10 @@ let c_lib f es loc =
139161
BufferOverrunLib
140162
(f.vname, [ List.nth es 0; List.nth es 1; List.nth es 2 ], loc)
141163
:: c_exps es loc
164+
| "fread" ->
165+
BufferOverrunLib
166+
(f.vname, [ List.nth es 0; List.nth es 1; List.nth es 2 ], loc)
167+
:: c_exps es loc
142168
| _ -> []
143169

144170
let c_lib_taint f es loc =
@@ -148,6 +174,11 @@ let c_lib_taint f es loc =
148174
| "calloc" | "g_malloc" | "g_malloc_n" | "g_malloc0" | "g_try_malloc"
149175
| "g_try_malloc_n" | "__builtin_alloca" ->
150176
[ AllocSize (f.vname, List.nth es 0, loc) ]
177+
| "fread" ->
178+
[
179+
BufferOverrunLib
180+
(f.vname, [ List.nth es 0; List.nth es 1; List.nth es 2 ], loc);
181+
]
151182
| "printf" -> [ Printf (f.vname, List.nth es 0, loc) ]
152183
| "fprintf" | "sprintf" | "vfprintf" | "vsprintf" | "vasprintf" | "__asprintf"
153184
| "asprintf" | "vdprintf" | "dprintf" | "easprintf" | "evasprintf" ->

src/report/alarmExp.mli

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ open ProsysCil
1515
type t =
1616
| ArrayExp of Cil.lval * Cil.exp * Cil.location
1717
| DerefExp of Cil.exp * Cil.location
18-
| MulExp of Cil.exp * Cil.location
18+
| PlusIOExp of Cil.exp * Cil.location
19+
| MinusIOExp of Cil.exp * Cil.location
20+
| MultIOExp of Cil.exp * Cil.location
21+
| ShiftIOExp of Cil.exp * Cil.location
22+
| CastIOExp of Cil.exp * Cil.location
1923
| DivExp of Cil.exp * Cil.location
2024
| Strcpy of Cil.exp * Cil.exp * Cil.location
2125
| Strcat of Cil.exp * Cil.exp * Cil.location
@@ -26,6 +30,8 @@ type t =
2630
| AllocSize of string * Cil.exp * Cil.location
2731
| Printf of string * Cil.exp * Cil.location
2832

33+
val location_of : t -> Cil.location
34+
2935
val collect : Spec.analysis -> IntraCfg.cmd -> t list
3036

3137
val to_string : t -> string

0 commit comments

Comments
 (0)