Security Principles (continued) and x86 Assembly
CS 161 Spring 2022 - Lecture 2
Heads up: These slides won’t exactly match the recording, sorry, but the differences are all fairly minor, and both have the exact same content.
Computer Science 161
Nicholas Weaver
Announcements
Computer Science 161
Nicholas Weaver
Next: Security Principles (continued)
3
Computer Science 161
Nicholas Weaver
Defense in Depth
Textbook Chapter 1.5
4
Computer Science 161
Nicholas Weaver
Defense in Depth
5
Computer Science 161
Nicholas Weaver
The Theodosian Walls of Constantinople
6
Computer Science 161
Nicholas Weaver
Least Privilege
Textbook Chapter 1.6
7
Computer Science 161
Nicholas Weaver
Least Privilege
8
Computer Science 161
Nicholas Weaver
uTorrent
9
Computer Science 161
Nicholas Weaver
uTorrent
10
Computer Science 161
Nicholas Weaver
uTorrent
11
Computer Science 161
Nicholas Weaver
uTorrent
12
Computer Science 161
Nicholas Weaver
uTorrent
13
Computer Science 161
Nicholas Weaver
Browser Design with Poor Least Privilege
“Drive-by malware”: A malicious web page exploits a browser bug to infect local files
14
Web Site
The contents of the web site come from an untrusted source (possibly malicious)
Trusted Computing Base
Browser Kernel
Web Browser
Rendering Engine
Website displayed to user
User Files
Computer Science 161
Nicholas Weaver
Google Chrome Design: Apply Least Privilege
Prevent "drive-by malware," where a malicious webpage exploits a browser bug to infect local files
15
Web Site
Trusted Computing Base
Browser Kernel
Rendering Engine
Website displayed to user
Sandbox
70% of vulnerabilities are in the rendering engine.
Sandbox: An isolated environment to run unsafe code. Damage in the sandbox will not spread elsewhere.
Computer Science 161
Nicholas Weaver
Enabling Least Privilege: Access Control
Computer Science 161
Nicholas Weaver
Access Control for Systems: The Operating System
Computer Science 161
Nicholas Weaver
Separation of Responsibility
Textbook Chapter 1.7
18
Computer Science 161
Nicholas Weaver
Separation of Responsibility
19
Computer Science 161
Nicholas Weaver
Welcome to a Nuclear Bunker
20
Computer Science 161
Nicholas Weaver
Welcome to a Movie Theater
21
Computer Science 161
Nicholas Weaver
Ensure Complete Mediation
Textbook Chapter 1.8 & 1.13
22
Computer Science 161
Nicholas Weaver
Security Principle: Ensure Complete Mediation
23
Computer Science 161
Nicholas Weaver
Time-of-Check to Time-of-Use
24
procedure withdrawal(w)
// contact central server to get balance
1. let b := balance
2. if b < w, abort
// contact server to set balance
3. set balance := b - w
4. give w dollars to user
Suppose you have $5 in your account. How can you trick this system into giving you more than $5?
Computer Science 161
Nicholas Weaver
Time-of-Check to Time-of-Use
withdrawal(5)�1. let b := balance�2. if b < w, abort
withdrawal(5)�1. let b := balance�2. if b < w, abort�
// contact server to set balance�3. set balance := b - w��4. give w dollars to user
25
// contact server to set balance�3. set balance := b - w��4. give w dollars to user
The machine gives you $10!
Time
Computer Science 161
Nicholas Weaver
Don’t Rely on Security Through Obscurity
Textbook Chapter 1.9
26
Computer Science 161
Nicholas Weaver
Don’t Rely on Security Through Obscurity
27
Computer Science 161
Nicholas Weaver
Highway Signs
28
Here’s the hidden computer inside the sign.
Here’s a highway sign.
Here’s the control panel. Most signs use the default password, DOTS.
Computer Science 161
Nicholas Weaver
Highway Signs
Note/Takeaway: Do not ever do this. Yes, some former CS 161 students did it once.
29
Computer Science 161
Nicholas Weaver
Highway Signs
Takeaway: Don’t rely on security through obscurity
30
Computer Science 161
Nicholas Weaver
Don’t Rely on Security Through Obscurity
31
Assume the attacker knows where the “secret” control panel is located, and knows the default password.
Computer Science 161
Nicholas Weaver
Use Fail-Safe Defaults
Textbook Chapter 1.10
32
Computer Science 161
Nicholas Weaver
Soda Hall
33
Computer Science 161
Nicholas Weaver
Use Fail-Safe Defaults
34
Computer Science 161
Nicholas Weaver
Design in Security from the Start
Textbook Chapter 1.11
35
Computer Science 161
Nicholas Weaver
Design in Security from the Start
36
Computer Science 161
Nicholas Weaver
Summary: Security Principles
37
Computer Science 161
Nicholas Weaver
Next: x86 Assembly and Call Stack
38
Computer Science 161
Nicholas Weaver
Number Representation
Textbook Chapter 2.1
39
Computer Science 161
Nicholas Weaver
Units of Measurement
40
Computer Science 161
Nicholas Weaver
Hexadecimal
41
Binary | Hexadecimal |
0000 | 0 |
0001 | 1 |
0010 | 2 |
0011 | 3 |
0100 | 4 |
0101 | 5 |
0110 | 6 |
0111 | 7 |
Binary | Hexadecimal |
1000 | 8 |
1001 | 9 |
1010 | A |
1011 | B |
1100 | C |
1101 | D |
1110 | E |
1111 | F |
Computer Science 161
Nicholas Weaver
Running C Programs
Textbook Chapter 2.2
42
Computer Science 161
Nicholas Weaver
CALL (Compiler, Assembler, Linker, Loader)
43
int add_one(int a) {� int added = a + 1;� return added;�}
C code
add_one:� push %ebp� mov %esp, %ebp� sub $4, %esp� mov 8(%ebp), %eax� mov %eax, -4(%ebp)� inc -4(%ebp)� mov -4(%ebp), %eax� leave� ret
Assembly code
(RISC-V, x86)
0x55 0x89 0xe5 0x83 0xec 0x04 0x8b 0x45 0x08 0x89 0x45 0xfc 0x45 0x89 0xe8 0xc9 0xc3
Machine code
(raw bits)
Compiler
Assembler
Computer Science 161
Nicholas Weaver
CALL (Compiler, Assembler, Linker, Loader)
44
Computer Science 161
Nicholas Weaver
Memory Layout
Textbook Chapter 2.3 & 2.5
45
Computer Science 161
Nicholas Weaver
C Memory Layout
46
address 0x00000000
address 0xFFFFFFFF
Computer Science 161
Nicholas Weaver
C Memory Layout
47
|
address 0x00000000
address 0xFFFFFFFF
Higher addresses
Lower addresses
4 bytes
Computer Science 161
Nicholas Weaver
x86 Memory Layout
48
Higher addresses
Lower addresses
Stack |
|
Heap |
Data |
Code |
Grows downwards
Grows upwards
Computer Science 161
Nicholas Weaver
Registers
49
Higher addresses
Lower addresses
Stack |
|
Heap |
Data |
Code |
Grows downwards
Grows upwards
Computer Science 161
Nicholas Weaver
x86 Architecture
Textbook Chapter 2.4 & 2.7
50
Computer Science 161
Nicholas Weaver
Why x86?
51
Computer Science 161
Nicholas Weaver
What is x86?
52
Computer Science 161
Nicholas Weaver
x86 Fact Sheet
53
int main(void) {� uint32_t num = 0xdeadbeef;
// This prints "deadbeef".
printf("%x", num);
// This prints "ef be ad de".
uint8_t *bytes = (uint8_t *) #
for (size_t i = 0; i < 4; i++) {
printf("%x ", bytes[i]);
}
}
Computer Science 161
Nicholas Weaver
x86 Registers
54
Computer Science 161
Nicholas Weaver
x86 Syntax
55
Computer Science 161
Nicholas Weaver
x86 Assembly
56
Opcode
Source
Destination
Computer Science 161
Nicholas Weaver
x86 Assembly
57
Opcode
Source
Destination
Computer Science 161
Nicholas Weaver
Stack Layout
Textbook Chapter 2.6
58
Computer Science 161
Nicholas Weaver
Stack Frames
59
Computer Science 161
Nicholas Weaver
Stack Frames
60
... |
... |
... |
... |
|
|
|
|
|
|
|
|
|
|
|
EBP
ESP
Current stack frame
Computer Science 161
Nicholas Weaver
Pushing and Popping
61
|
|
|
|
|
|
|
EBP
ESP
Current stack frame
|
|
|
|
|
0xcafef00d |
|
EBP
ESP
Current stack frame
Before push %eax
After push %eax
EAX = 0xcafef00d
EBX = ...
EAX = 0xcafef00d
EBX = ...
Computer Science 161
Nicholas Weaver
Pushing and Popping
62
|
|
|
|
|
0xcafef00d |
|
EBP
ESP
Current stack frame
|
|
|
|
|
0xcafef00d |
|
EBP
ESP
Current stack frame
Before pop %eax
After pop %eax
EAX = 0x00000000
EBX = ...
EAX = 0xcafef00d
EBX = ...
Computer Science 161
Nicholas Weaver
Why push and pop?
Computer Science 161
Nicholas Weaver
x86 Stack Layout
64
Computer Science 161
Nicholas Weaver
Stack Layout
struct foo {� long long f1; // 8 bytes� int f2; // 4 bytes� int f3; // 4 bytes�};��void func(void) {� int a; // 4 bytes� struct foo b;� int c; // 4 bytes�}
65
|
|
|
|
|
|
a |
b.f3 |
b.f2 |
b.f1 |
b.f1 |
c |
How would you fill out the boxes in this stack diagram?
Options:
a b.f1 b.f2 b.f3 c
Higher addresses
Lower addresses
4 bytes
Computer Science 161
Nicholas Weaver