1/61
Looks like no tags are added yet.
Order of steps from C source to a running program
Compiler > Assembler > Linker > Loader > CPU
What does the compiler do
Converts C code to assembly code
What does the assembler do
Converts assembly code to machine code
What does the linker do
Deals with dependencies and libraries to produce a standalone binary
What does the loader do
Sets up the address space in memory and loads the machine code and jumps to the first instruction of the program
What does the CPU do with a program
Fetches and interprets the instructions one by one from the text segment
What is %eip
Extended instruction pointer - points to the next instruction to execute
What can change %eip
It is incremented after each instruction and modified by call and ret and jmp and conditional jmp
What is a CPU register
A 32-bit memory located inside the CPU - like a variable built into the processor - fast to access
Name the x86-32 temporary (general purpose) registers
%eax %ebx %ecx %edx %edi %esi - most instructions operate on them
What is %esp
Extended stack pointer - points at the TOP of the stack (the top of the current stack frame)
What is %ebp
Extended base pointer - points to the BASE of the stack frame of the current function call
What is %eax typically used for
Holding the return value of a function call
Why does the course use 32-bit examples
64-bit optimisations complicate the examples but the security concepts are exactly the same
What is a process's address space
The virtual memory each process gets - it thinks it is the whole of the memory
List the regions of a process's address space
Text - Data - BSS - Heap - Stack (plus a region reserved for the kernel)
Which region sits at the very bottom of the address space
The text segment
What is stored in the text segment
The machine code of the program (loaded there by the loader)
What is stored in the data segment
Static initialised global variables
What is stored in the BSS segment
Static uninitialised global variables
What is the heap used for
Dynamically allocated memory (e.g. malloc) - it grows UPWARDS from just above BSS toward higher addresses
What is stored on the stack
Local variables and bookkeeping info for each function call - it grows DOWNWARDS from the top of the address space
Which way do the heap and the stack grow
Heap grows up (to higher addresses) - stack grows down (to lower addresses)
Where do low and high memory addresses go in the diagram
Low addresses at the bottom (text segment) and high addresses at the top (stack)
What is a stack overflow (memory exhaustion sense)
The stack and heap collide - you have run out of memory - e.g. from very deeply nested function calls
What is a stack frame
The chunk of stack holding the info for ONE function invocation - created on a call and popped on return
When is a new stack frame created
Every time a function is called (the call instruction) - it is pushed onto the stack
What does a typical stack frame contain
Function arguments - return address - saved frame pointer (%ebp) - local variables
Order in which a frame is built (x86-32 simplified model)
Arguments (in reverse order) 2. Return address 3. Saved old %ebp 4. Local variables
Who pushes the function arguments
The CALLING function (caller) - so some textbooks draw them in the caller's frame and others in the callee's
In what order are arguments pushed
Reverse order - last argument first (so for f(A B C) you push C then B then A)
What is the return address
The address of the instruction in the caller just AFTER the call - where execution resumes when the callee finishes
Why is the old %ebp saved in the new frame
So the caller's stack frame can be restored when the callee returns
Where are local variables placed and in what order
After the saved %ebp in the frame - the compiler may reorder them due to optimisation
How does a function access its arguments
At %ebp plus a positive offset (higher addresses)
How does a function access its local variables
At %ebp minus an offset (lower addresses)
Where is the return address relative to %ebp
At %ebp + 4 (written 4(%ebp)) - and the saved %ebp is at (%ebp)
Steps the CALLING function performs
Steps the CALLED function performs on entry
Steps performed when a function RETURNS
How many frames exist at the deepest point of fact(4) in the lecture example
6 frames - main plus fact(4) fact(3) fact(2) fact(1) fact(0)
Where does the recursive fact example store its return value
In %eax - fact(0)=1 then fact(1)=1 then fact(2)=2 then fact(3)=6 then fact(4)=24 (0x18)
Why is the saved return address the same in all the recursive fact frames
Each recursive call returns to the same instruction inside fact - only the first call returns to main
What happens to a frame on return
It is popped - %ebp and %esp are restored to the caller's values so the callee's frame becomes inactive
What does C do about array bounds
Nothing - arrays have a fixed size but C does NOT enforce bounds on reads or writes
What is a C string
A character array terminated by a NUL byte (0x00)
How much space does a string need
Number of characters PLUS 1 for the NUL terminator
Why is strcpy(buffer input) dangerous
strcpy does not check the destination size - if input is longer than the buffer it writes past the end
char buffer[8] - how long can the input string be
At most 7 characters plus the NUL terminator
What is the problem with gets(buf)
gets reads from stdin until newline or NUL and never checks the size of buf - so input can overflow it
What goes wrong in the demo read_stdi function with char buf[128]
gets(buf) does not limit the input to 128 bytes - a longer input overflows buf and corrupts the stack
In which direction does an overflow write on the stack
Towards HIGHER addresses - up the stack - over adjacent variables then the saved %ebp then the return address
What did the compiler do differently in the demo to the source order
It made space for int i first and then buf (compiler optimisation) - the idea of reserving local space is the same
Define a stack-based buffer overflow
A program writes beyond the end of a stack-allocated buffer - overwriting adjacent data in the stack frame
What important data can an overflow overwrite
Local variables - the saved %ebp - and the RETURN ADDRESS
Security consequence of a stack buffer overflow
The program may crash or execution may be redirected when the function returns
Why did the demo with a string of A's crash
The return address got overwritten with A's (0x41414141) - which is not a valid instruction address so the CPU cannot continue
Why is an attacker who controls the input dangerous
They can craft the stack contents - redirect the return address or change variable values - and aim for privilege escalation (e.g. a root shell)
Root causes of stack buffer overflows
C has no automatic bounds checking AND the stack stores control data (return addresses and saved %ebp) right next to buffers
Why is memory safety taught under OS security
The bugs come from programming errors (software security) but the defences are provided at least partly by the operating system
Plan for the 3 memory safety lectures
Memory layout and stack overflow corrupting adjacent memory 2. How attackers exploit it (privilege escalation) 3. Defence
x86 runtime memory - 3 things to remember as you go through it
1) Argument
2) Return Address
3) previous base pointer