2-A Case of Dynamic Program Analysis - CS510 Software Engineering
2-A Case of Dynamic Program Analysis - CS510 Software Engineering
Analysis
CS510 Software Engineering
Outline
Introduction
• Static instrumentation vs. dynamic instrumentation
How to implement a dynamic information flow
system
Max = 0;
for (p = head; p; p = p->next)
{
printf(“In
count[0]++;loop\n”);
if (p->value > max)
{
count[1]++; branch\n”);
printf(“True
max = p->value;
}
}
Disassemble Bundles
gcc g++ asm
Construct ICFG
*.o *.a
Analyses/Optimizations
Serialize ICFG
ld
DIABLO Assemble Bundles
a.out
Write Object Format
b.out
Tool 1
VALGRIND CORE
BB
BB Decoder Tool 2
pc
Binary pc ……
Dispatcher BB Compiler
Code
Tool n
Instrumenter
Trampoline New BB
Input New BB Runtime
state
New pc
Binary 1 ……
Dispatcher BB Compiler
Code
Tool n
Instrumenter
Trampoline
Input
Runtime
OUTPUT:
CS510 Software Engineering
Valgrind Infrastructure
1: do {
2: i=i+1; Tool 1
3: s1; VALGRIND CORE
4: } while (i<2)
5: s2; BB Decoder 1: do {
Tool 2
2: i=i+1;
3: s1;
Binary ……
4: } while (i<2)
Dispatcher BB Compiler
Code
Tool n
Instrumenter
Trampoline
Input
Runtime
OUTPUT:
CS510 Software Engineering
Valgrind Infrastructure
1: do {
2: i=i+1; Tool 1
3: s1; VALGRIND CORE
4: } while (i<2)
5: s2; BB Decoder Tool 2
Binary ……
Dispatcher BB Compiler
Code
Tool n
Instrumenter
Trampoline 1: do {
Input print(“1”)
2: i=i+1; Runtime
3: s1;
4: } while (i<2)
OUTPUT:
CS510 Software Engineering
Valgrind Infrastructure
1: do {
2: i=i+1; Tool 1
3: s1; VALGRIND CORE
4: } while (i<2)
5: s2; BB Decoder Tool 2
Binary ……
Dispatcher BB Compiler
Code
Tool n
Instrumenter
1 Trampoline
Input 1: do { Runtime
print(“1”)
i=i+1;
s1;
} while (i<2) OUTPUT: 1 1
CS510 Software Engineering
Valgrind Infrastructure
1: do {
2: i=i+1; Tool 1
3: s1; VALGRIND CORE
4: } while (i<2)
5: s2; 5 BB Decoder Tool 2
5: s2;
Binary ……
Dispatcher BB Compiler
Code
Tool n
5 Instrumenter
Trampoline
Input 1: do { Runtime
print(“1”)
i=i+1;
s1;
} while (i<2) OUTPUT: 1 1
CS510 Software Engineering
Valgrind Infrastructure
1: do {
2: i=i+1; Tool 1
3: s1; VALGRIND CORE
4: } while (i<2)
5: s2; BB Decoder Tool 2
Binary ……
Dispatcher BB Compiler
Code
Tool n
Instrumenter
Trampoline
Input 1: do { Runtime
print(“1”) 5: print (“5”);
i=i+1; s2;
s1;
} while (i<2) OUTPUT: 1 1
CS510 Software Engineering
Valgrind Infrastructure
1: do {
2: i=i+1; Tool 1
3: s1; VALGRIND CORE
4: } while (i<2)
5: s2; BB Decoder Tool 2
Binary ……
Dispatcher BB Compiler
Code
Tool n
Instrumenter
Trampoline
1: do {
Input print(“1”)
i=i+1; Runtime
s1;
} while (i<2)
5: print (“5”); OUTPUT: 1 1 5
s2;
CS510 Software Engineering
Dynamic Instrumentation Characteristics
A trampoline is required.
Does not require recompiling or relinking
• Save time: compile and link times are significant in real systems.
• Can instrument without linking (relinking is not always possible).
Dynamically turn on/off, change instrumentation
• From t1-t2, I want to execute F’, t3-t4, I want F’’
Can be done by invalidating the mapping in the dispatcher.
Can instrument running programs (such as Web or database
servers)
• Production systems.
Can instrument self-mutating code.
• Obfuscation can be easily get around.
Overhead is high
• Dispatching, indexing;
• Dynamic instrumentation
Usually does not provide program representations at
run time
• Hard to acquire
• Unacceptable runtime overhead
• Simple representations such as BB are provided
• GET AROUND: combine with static tools
Diablo + valgrind
IFS is important
• Confidentiality at runtime = IFS
• Tainted analysis = IFS
• Memory reference errors detection = IFS
• Data lineage system = IFS
• Dynamic slicing is partly an IFS
Essence of an IFS
• A runtime abstract interpretation engine
Driven by the executed program path
Abstract state
• One bit, the security bit (tainted bit)
• Prevent call at tainted value.
Use a template
• The tool lackey is good candidate
• Two parts to fill in Tool n
Instrumenter
Runtime Instrumenter
Instrumenter
• Initialization Runtime
• Instrumentation
• Finalization
• System calls interception
Runtime
• Transfer functions
• Memory management for abstract state
void SK_(pre_clo_init)(void)
{
VG_(details_name) (“CS510 IFS");
…
init_shadow_memory();
…
VG_(needs_shadow_memory) ();
VG_(needs_shadow_regs) ();
…
VG_(register_noncompact_helper)((Addr) & RT_load);
VG_(register_noncompact_helper)((Addr) & …);
…
}
EMPTY
switch (u->opcode) {
case LD:
VG_(ccall_RR_R) (cb, (Addr) RT_load, u->
LD [r1], r2 r1, SHADOW (u->r1), SHADOW(U->r2)
}
switch (u->opcode) {
case ST:
VG_(ccall_RRR_0) (cb, (Addr) RT_store,
ST r1, [r2] u->r2, SHADOW (u->r1), SHADOW(u->r2);
}
switch (u->opcode) {
case MOV:
uInstr2(cb, MOV,…, SHADOW(u->r1), …
MOV r1, r2 SHADOW(u->r2)
}
switch (u->opcode) {
case ST:
VG_(ccall_RR_R) (cb, (Addr) RT_add, SHADOW(u->r1),
ADD r1, r2 SHADOW (u->r2), SHADOW(u->r2);
}
switch (u->opcode) {
case ST:
VG_(ccall_R_0) (cb, (Addr) RT_call, SHADOW(u->r1));
CALL r1
}
void (* F) ();
char A[2];
...
read(B, 256);
i=2;
A[i]=B[i];
...
(*F) ();
A[i]=B[i];
... SM(&i)=SHADOW(r1) 1
LD [&i], r1 1
... MOV &B, r2 …
ADD r1, r2 B 1
LD [r2], r2
MOV &A, r3 r1
ADD r1, r3
r2
ST r2, [r3]
... r3
(*F) (); MOV F, r1
CALL r1
CS510 Software Engineering
void (* F) (); Virtual Space Shadow Space
char A[2];
... ...
read(B, 256); i
MOV &B, r1
MOV 256, r2
... F 1
SYS_Read r1, r2
SHADOW(r2)=SM(r2) | SHADOW (r2) A[1]
...
i=2; r2=&B[2];
MOV 2, r1 A[0]
... ST r1, [&i]
... 1
A[i]=B[i]; LD [&i], r1
... 1
MOV &B, r2 …
ADD r1, r2 B 1
LD [r2], r2
MOV &A, r3 r1
ADD r1, r3
r2 1
ST r2, [r3]
... r3
(*F) (); SM (r3)=SHADOW(r2)
MOV F, r1 | SHADOW (r3)
CALL r1 r3=&A[2]
CS510 Software Engineering
void (* F) (); Virtual Space Shadow Space
char A[2];
... ...
read(B, 256); i
MOV &B, r1
MOV 256, r2
... F 1
SYS_Read r1, r2
... A[1]
i=2; MOV 2, r1 A[0]
... ST r1, [&i]
... 1
A[i]=B[i]; LD [&i], r1
... 1
MOV &B, r2 …
ADD r1, r2 B 1
LD [r2], r2
MOV &A, r3 r1 1
SHADOW(r1)=SM(F);
ADD r1, r3
r2 1
ST r2, [r3]
... r3
(*F) (); MOV F, r1
CALL r1
if (SHADOW(r1)) printf (“Call …”);
CS510 Software Engineering
What Is Not Covered
p=getpassword( );
…
if (p==“zhang”) {
send (m);
}