Computer Science 161 Fall 2016 Nicholas Weaver
"Secure" Coding Practices Nicholas Weaver
based on David Wagner’s slides from Sp 2016
1
"Secure" Coding Practices Nicholas Weaver based on David - - PowerPoint PPT Presentation
Computer Science 161 Fall 2016 Nicholas Weaver "Secure" Coding Practices Nicholas Weaver based on David Wagners slides from Sp 2016 1 Administrivia Computer Science 161 Fall 2016 Nicholas Weaver 2 Computer Science 161 Fall
Computer Science 161 Fall 2016 Nicholas Weaver
1
Computer Science 161 Fall 2016 Nicholas Weaver
2
Computer Science 161 Fall 2016 Nicholas Weaver
3
Computer Science 161 Fall 2016 Nicholas Weaver
was flagged as anomalous
4
if ((options == (__WCLONE|__WALL)) && (current->uid = 0)) retval = -EINVAL;
Computer Science 161 Fall 2016 Nicholas Weaver
5
Computer Science 161 Fall 2016 Nicholas Weaver
6
Computer Science 161 Fall 2016 Nicholas Weaver
7
Computer Science 161 Fall 2016 Nicholas Weaver
8
Computer Science 161 Fall 2016 Nicholas Weaver
9
Computer Science 161 Fall 2016 Nicholas Weaver
10
Computer Science 161 Fall 2016 Nicholas Weaver
11
Computer Science 161 Fall 2016 Nicholas Weaver
13
Computer Science 161 Fall 2016 Nicholas Weaver
ideally) fashion?
basis
what holds after execution)
15
Computer Science 161 Fall 2016 Nicholas Weaver
16
Precondition?
Computer Science 161 Fall 2016 Nicholas Weaver
17
Precondition: what needs to hold for function to operate correctly
Computer Science 161 Fall 2016 Nicholas Weaver
18
Postcondition?
Computer Science 161 Fall 2016 Nicholas Weaver
19
/* ensures: retval != NULL (and a valid pointer) */
Postcondition: what the function promises will hold upon its return
Computer Science 161 Fall 2016 Nicholas Weaver
20
Precondition?
Computer Science 161 Fall 2016 Nicholas Weaver
21
General correctness proof strategy for memory safety: (1) Identify each point of memory access (2) Write down precondition it requires (3) Propagate requirement up to beginning of function
Computer Science 161 Fall 2016 Nicholas Weaver
22
General correctness proof strategy for memory safety: (1) Identify each point of memory access? (2) Write down precondition it requires (3) Propagate requirement up to beginning of function
Computer Science 161 Fall 2016 Nicholas Weaver
23
General correctness proof strategy for memory safety: (1) Identify each point of memory access (2) Write down precondition it requires (3) Propagate requirement up to beginning of function
Computer Science 161 Fall 2016 Nicholas Weaver
24
General correctness proof strategy for memory safety: (1) Identify each point of memory access (2) Write down precondition it requires? (3) Propagate requirement up to beginning of function
Computer Science 161 Fall 2016 Nicholas Weaver
25
General correctness proof strategy for memory safety: (1) Identify each point of memory access (2) Write down precondition it requires (3) Propagate requirement up to beginning of function
Computer Science 161 Fall 2016 Nicholas Weaver
26
General correctness proof strategy for memory safety: (1) Identify each point of memory access (2) Write down precondition it requires (3) Propagate requirement up to beginning of function?
Computer Science 161 Fall 2016 Nicholas Weaver
27
Let’s simplify, given that a never changes. (It gets much worse when we have multiple threads)
Computer Science 161 Fall 2016 Nicholas Weaver
28
Computer Science 161 Fall 2016 Nicholas Weaver
29
General correctness proof strategy for memory safety: (1) Identify each point of memory access (2) Write down precondition it requires (3) Propagate requirement up to beginning of function?
Computer Science 161 Fall 2016 Nicholas Weaver
30
General correctness proof strategy for memory safety: (1) Identify each point of memory access (2) Write down precondition it requires (3) Propagate requirement up to beginning of function?
Computer Science 161 Fall 2016 Nicholas Weaver
31
General correctness proof strategy for memory safety: (1) Identify each point of memory access (2) Write down precondition it requires (3) Propagate requirement up to beginning of function?
Computer Science 161 Fall 2016 Nicholas Weaver
32
The 0 <= i part is clear, so let’s focus for now on the rest.
Computer Science 161 Fall 2016 Nicholas Weaver
33
Computer Science 161 Fall 2016 Nicholas Weaver
34
General correctness proof strategy for memory safety: (1) Identify each point of memory access (2) Write down precondition it requires (3) Propagate requirement up to beginning of function?
Computer Science 161 Fall 2016 Nicholas Weaver
35
General correctness proof strategy for memory safety: (1) Identify each point of memory access (2) Write down precondition it requires (3) Propagate requirement up to beginning of function?
Computer Science 161 Fall 2016 Nicholas Weaver
36
How to prove our candidate invariant? n <= size(a) is straightforward because n never changes.
Computer Science 161 Fall 2016 Nicholas Weaver
37
Computer Science 161 Fall 2016 Nicholas Weaver
38
What about i < n ?
Computer Science 161 Fall 2016 Nicholas Weaver
39
What about i < n ? That follows from the loop condition.
Computer Science 161 Fall 2016 Nicholas Weaver
40
At this point we know the proposed invariant will always hold...
Computer Science 161 Fall 2016 Nicholas Weaver
41
… and we’re done!
Computer Science 161 Fall 2016 Nicholas Weaver
42
A more complicated loop might need us to use induction: Base case: first entrance into loop. Induction: show that postcondition of last statement of loop plus loop test condition implies invariant.
Computer Science 161 Fall 2016 Nicholas Weaver
43
Computer Science 161 Fall 2016 Nicholas Weaver
44
Computer Science 161 Fall 2016 Nicholas Weaver
45
Computer Science 161 Fall 2016 Nicholas Weaver
46
Computer Science 161 Fall 2016 Nicholas Weaver
47
Computer Science 161 Fall 2016 Nicholas Weaver
48
Computer Science 161 Fall 2016 Nicholas Weaver
49
Computer Science 161 Fall 2016 Nicholas Weaver
50
Computer Science 161 Fall 2016 Nicholas Weaver
51
Computer Science 161 Fall 2016 Nicholas Weaver
52
Computer Science 161 Fall 2016 Nicholas Weaver
53
Computer Science 161 Fall 2016 Nicholas Weaver
54
Computer Science 161 Fall 2016 Nicholas Weaver
55
Computer Science 161 Fall 2016 Nicholas Weaver
56
Computer Science 161 Fall 2016 Nicholas Weaver
57
Computer Science 161 Fall 2016 Nicholas Weaver
58
void foo(int *a){ int i, j, sum; sum = 0; j = 0; for(i = 1; i < 10; ++i){ sum += a[j]; j = a[j]; } }
Computer Science 161 Fall 2016 Nicholas Weaver
errors
students;--??!?"
59
Computer Science 161 Fall 2016 Nicholas Weaver
60
Computer Science 161 Fall 2016 Nicholas Weaver
61