Part 6 of 6 · 3 min read
Exercises
Easy
Easy 1: Translate (20 minutes)
With base 28,000 and limit 4,000, translate the logical addresses 0, 3,999 and 4,000.
Done when: 28,000; 31,999; and a trap for 4,000.
Hint
The check comes first, then the addition.
Easy 2: Which region? (20 minutes)
For each item, name the region of the address space where it lives: a local variable, the code of main, memory from malloc(100), a global with the value 42, the string literal "hello".
Done when: five answers: stack, code, heap, data, read-only data.
Hint
Only malloc memory and the stack change size while the program runs.
Easy 3: Read a maps line (20 minutes)
Explain the line 7ffff7c28000-7ffff7db1000 r-xp ... libc.so.6 field by field.
Done when: a statement of the start, the end, the size in bytes, and the permissions.
Hint
The size is the end minus the start, and the fields are start-end perms offset device inode name.
Medium
Medium 1: Print your own layout (45 minutes)
Write the program of section 3 and run it three times, twice with randomisation and once under setarch -R. Compare the addresses.
Done when: a table of three runs, with a note on which addresses changed and which did not.
Hint
Print with %p, and keep the output of each run in a file.
Medium 2: Read /proc/self/maps (45 minutes)
Make a program that prints its own /proc/self/maps, and mark the lines for code, data, heap, library and stack.
Done when: a labelled list of at least five regions, with permissions.
Hint
Open the file with fopen and copy each line to the output.
Medium 3: Two processes, one address (30 minutes)
Write a program that sets a global variable to 1, forks, and in the child sets it to 2. Print the address and value in both processes.
Done when: the same address in both processes with the values 1 and 2.
Hint
After fork, each process has its own copy of the variable.
Hard
Hard 1: Simulate base and limit (60 minutes)
Write a C function translate(base, limit, addr, &phys) that returns 0 and fills phys, or returns -1 for a trap. Test it on the five rows of the table in section 2.
Done when: all five rows match the table, and the trap row returns -1.
Hint
The limit test must use "less than", not "less than or equal to".
Hard 2: Move a process (60 minutes)
Extend the simulation with an array of three processes in a memory of 64,000 bytes. Write a function that moves a process to a new base, and show that its logical addresses translate to new physical ones while the logical addresses stay the same.
Done when: a before and after table for one process.
Hint
Moving a process changes only its base.
Hard 3: Measure ASLR (90 minutes)
Run a program that prints the address of a stack variable 1,000 times. Count how many different values appear, and the number of bits that vary.
Done when: a count of distinct addresses, and an estimate of the number of random bits, with the method used.
Hint
The number of random bits is about the base-2 logarithm of the number of distinct values.