AUTOMATIC PROGRAM REPAIR
Zhen Huang Penn State University Spring 2019 CMPSC 447, Software Security
1
H OW TO R EPAIR V ULNERABILITIES ? Correcting vulnerable logic, e.g. - - PowerPoint PPT Presentation
A UTOMATIC P ROGRAM R EPAIR Zhen Huang 1 Penn State University Spring 2019 CMPSC 447, Software Security P RE PATCH W INDOW Attackers can leverage the window of time before a vulnerability is addressed. Attackers can exploit the
Zhen Huang Penn State University Spring 2019 CMPSC 447, Software Security
1
2
Discovery of a Vulnerability
pre‐patch window
Vendor Releases a Patch Users Apply the Patch
3
Workaround for Rapid Response. IEEE Symposium on Security & Privacy 2016.
4
Multiple attempts of patching (Quotes from a bug report) The developer: “This updates the previous patch...” .... The developer: “This patch builds on the previous one...” .... The developer: “I’ve just committed more changes...” .... .... The tester: “I’m afraid I found a bug...”
5
Correcting vulnerable logic, e.g. race condition Preventing vulnerable code from being executed Adding checks to detect vulnerability‐triggering
6
Heartbleed Vulnerability: memcpy(bp, pl, payload); Official fix: If (… payload… > ...length) return 0; …. memcpy(bp, pl, payload);
7
8
9
int foo(…) { .... // vulnerable code .... } int foo(...) { return error_code; .... // vulnerable code .... SWRR
10
int foo(...) { return ?; .... // vulnerable code ....
11
apache HTTP server
malicious request request rejected
12
13
Int bar() { if (foo() == NULL) return ‐2; …. Int bar() { …. if (spam() == ‐3) return ‐2; foo: NULL
bar: ‐2
Int ham() { …. return bar(); ….
bar: ‐2 spam: ‐3
bar: ‐2
ham: ‐2
14
int baz() { .… If (error) { log_msg(“ERROR!”); return ‐1; } ….
char *foo() { …. if (error) return NULL; ….
15
16
Int bar() { return ‐2; ….. char *foo() { return NULL; …..
SWRR SWRR
Talos Generates source code SWRRs Uses static program analysis Instruments SWRRs into the source code of a
RVM Generates binary code SWRRs Instruments SWRRs into the binary of a target
17
18
19
20
Found error return value for status_handler status_handler function
21
status_handler function
22
23
24
25
26
27
Positive Tests Negative Tests Before the fix Pass Fail After the fix Pass Pass
// returns x‐y if x > y; 0 if x == y; y‐x if x < y 1 int distance(int x, int y) { 2 int result; 3 if (x >y) 4 result = x ‐ y; 5 else if (x == y) 6 result = 0; 7 else 8 result = x ‐ y; // should be y ‐ x 9 return result; 10 }
28
Input# Label x y distance (expected) distance (actual) 1 Positive 2 1 1 1 2 Positive 3 3 3 Negative 1 4 3 ‐3 4 Negative 5 5 ‐5
29
30
Statement
#failed #passed 8 result = x ‐y 1.0 2 5 else if (x == y) 0.67 2 1 3 if (x > y) 0.5 2 2 4 result = x ‐ y 0.0 1 6 result = 0 0.0 1
31
32
33
34
35
int ReadJPEG(…) { …. // overflow error rgb = malloc(stride * cinfo.height); …. }
FEH Overflow Check
char load(…) { …. if (height>16) { // quit } …. }
CWebP Buffer Overflow
36
37
Checks Seed Input Error Input if (height > 16) pass fail …. …. ….
38
39
40
41
42
int ReadJPEG(…) { …. // patch If (cinfo.height > 16) exit(‐1); rgb = malloc(stride * cinfo.height); …. }
CWebP Overflow Check FEH Overflow Check
char load(…) { …. if (height>16) { // quit } …. }
43
Run patched program example Inputs Correct Patch Incorrect Patch Apply patch to program Synthesize a new patch
44
45
46
47
48
buffer data input
void *p = read_from_file(); struct A *pa = (struct A *)p; p->field_i = 100;
strcpy(buffer, input); field1 field2 field i
short n = strlen(input);
49
50
51
52
53
54
char *foo_malloc(int p, int q) { return malloc(p * q); } char *foo(char *d, int r, int c, int l) { char *out = foo_malloc(r, c); bar(d, out, l); return out; } void bar(char *d, char *out, int len);
55
char *foo_malloc(int p, int q) { return malloc(p * q); } char *foo(char *d, int r, int c, int l) { char *out = foo_malloc(r, c); bar(d, out, l); return out; } void bar(char *d, char *out, int len);
56
char *foo_malloc(int p, int q) { return malloc(p * q); } char *foo(char *d, int r, int c, int l) { if (!(r * c >= l)) return NULL; // patch char *out = foo_malloc(r, c); bar(d, out, l); return out; } void bar(char *d, char *out, int len);
57
58
59
60
Semantic Analysis. International Conference on Software Engineering 2013.
Horizontal Code Transfer across Multiple Applications. ACM SIGPLAN conference on Programming Language Design and Implementation 2015.
Security Workaround for Rapid Response. IEEE Symposium on Security & Privacy 2016.
Workshop on Binary Analysis Research 2019.