CSEC - 9

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play

1/61

encourage image

There are no tags or description

Looks like no tags are added yet.

62 Terms

1
New cards

Order of steps from C source to a running program

Compiler > Assembler > Linker > Loader > CPU

2
New cards

What does the compiler do

Converts C code to assembly code

3
New cards

What does the assembler do

Converts assembly code to machine code

4
New cards

What does the linker do

Deals with dependencies and libraries to produce a standalone binary

5
New cards

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

6
New cards

What does the CPU do with a program

Fetches and interprets the instructions one by one from the text segment

7
New cards

What is %eip

Extended instruction pointer - points to the next instruction to execute

8
New cards

What can change %eip

It is incremented after each instruction and modified by call and ret and jmp and conditional jmp

9
New cards

What is a CPU register

A 32-bit memory located inside the CPU - like a variable built into the processor - fast to access

10
New cards

Name the x86-32 temporary (general purpose) registers

%eax %ebx %ecx %edx %edi %esi - most instructions operate on them

11
New cards

What is %esp

Extended stack pointer - points at the TOP of the stack (the top of the current stack frame)

12
New cards

What is %ebp

Extended base pointer - points to the BASE of the stack frame of the current function call

13
New cards

What is %eax typically used for

Holding the return value of a function call

14
New cards

Why does the course use 32-bit examples

64-bit optimisations complicate the examples but the security concepts are exactly the same

15
New cards

What is a process's address space

The virtual memory each process gets - it thinks it is the whole of the memory

16
New cards

List the regions of a process's address space

Text - Data - BSS - Heap - Stack (plus a region reserved for the kernel)

17
New cards

Which region sits at the very bottom of the address space

The text segment

18
New cards

What is stored in the text segment

The machine code of the program (loaded there by the loader)

19
New cards

What is stored in the data segment

Static initialised global variables

20
New cards

What is stored in the BSS segment

Static uninitialised global variables

21
New cards

What is the heap used for

Dynamically allocated memory (e.g. malloc) - it grows UPWARDS from just above BSS toward higher addresses

22
New cards

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

23
New cards

Which way do the heap and the stack grow

Heap grows up (to higher addresses) - stack grows down (to lower addresses)

24
New cards

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)

25
New cards

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

26
New cards

What is a stack frame

The chunk of stack holding the info for ONE function invocation - created on a call and popped on return

27
New cards

When is a new stack frame created

Every time a function is called (the call instruction) - it is pushed onto the stack

28
New cards

What does a typical stack frame contain

Function arguments - return address - saved frame pointer (%ebp) - local variables

29
New cards

Order in which a frame is built (x86-32 simplified model)

  1. Arguments (in reverse order) 2. Return address 3. Saved old %ebp 4. Local variables


30
New cards

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

31
New cards

In what order are arguments pushed

Reverse order - last argument first (so for f(A B C) you push C then B then A)

32
New cards

What is the return address

The address of the instruction in the caller just AFTER the call - where execution resumes when the callee finishes

33
New cards

Why is the old %ebp saved in the new frame

So the caller's stack frame can be restored when the callee returns

34
New cards

Where are local variables placed and in what order

After the saved %ebp in the frame - the compiler may reorder them due to optimisation

35
New cards

How does a function access its arguments

At %ebp plus a positive offset (higher addresses)

36
New cards

How does a function access its local variables

At %ebp minus an offset (lower addresses)

37
New cards

Where is the return address relative to %ebp

At %ebp + 4 (written 4(%ebp)) - and the saved %ebp is at (%ebp)

38
New cards

Steps the CALLING function performs

  1. Push arguments (in reverse) 2. Push the return address 3. Jump to the function's address
39
New cards

Steps the CALLED function performs on entry

  1. Push the old %ebp 5. Set %ebp to the current %esp (start of new frame) 6. Make space for local variables on the stack
40
New cards

Steps performed when a function RETURNS

  1. Reset the previous frame: %esp = %ebp and %ebp = (%ebp) 8. Jump back to the return address: %eip = 4(%ebp)
41
New cards

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)

42
New cards

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)

43
New cards

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

44
New cards

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

45
New cards

What does C do about array bounds

Nothing - arrays have a fixed size but C does NOT enforce bounds on reads or writes

46
New cards

What is a C string

A character array terminated by a NUL byte (0x00)

47
New cards

How much space does a string need

Number of characters PLUS 1 for the NUL terminator

48
New cards

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

49
New cards

char buffer[8] - how long can the input string be

At most 7 characters plus the NUL terminator

50
New cards

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

51
New cards

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

52
New cards

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

53
New cards

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

54
New cards

Define a stack-based buffer overflow

A program writes beyond the end of a stack-allocated buffer - overwriting adjacent data in the stack frame

55
New cards

What important data can an overflow overwrite

Local variables - the saved %ebp - and the RETURN ADDRESS

56
New cards

Security consequence of a stack buffer overflow

The program may crash or execution may be redirected when the function returns

57
New cards

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

58
New cards

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)

59
New cards

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

60
New cards

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

61
New cards

Plan for the 3 memory safety lectures

  1. Memory layout and stack overflow corrupting adjacent memory 2. How attackers exploit it (privilege escalation) 3. Defence


62
New cards

x86 runtime memory - 3 things to remember as you go through it

1) Argument

2) Return Address

3) previous base pointer