Part 6 of 6 · 2 min read
Exercises
Easy
Easy 1: Mask values (20 minutes)
What hexadecimal mask has only CAP_KILL (5) set, and only CAP_SYS_TIME (25)?
Done when: 0x20 and 0x2000000.
Hint
bit n has value 1 << n.
Easy 2: Decode (20 minutes)
Which capability is in 0x400? Run capsh --decode=0000000000000400 to check.
Done when: cap_net_bind_service, bit 10.
Hint
0x400 is 1 << 10.
Easy 3: Two names (20 minutes)
Give one act that needs CAP_CHOWN and one that needs CAP_KILL.
Done when: changing a file's owner to another user, and sending a signal to another user's process.
Hint
think of the table in section 2.
Medium
Medium 1: Combine (45 minutes)
A process needs CAP_NET_BIND_SERVICE and CAP_KILL. What mask is that?
Done when: 0x400 + 0x20 = 0x420.
Hint
OR the two masks.
Medium 2: Read your own set (45 minutes)
Run grep Cap /proc/self/status as a normal user and as root (with sudo). Report the five lines and the number of capabilities in each effective set.
Done when: all zeros for the normal user, and a mask with many bits for root, with a count from capsh --decode.
Hint
use sudo grep Cap /proc/self/status.
Medium 3: Run the program (45 minutes)
Compile the program of section 3 and run it as root. Change CAP_NET_BIND_SERVICE to CAP_DAC_OVERRIDE in the capset line and run again. Which act succeeds in the third state?
Done when: the shadow file opens and the bind fails.
Hint
edit the 1u << CAP_... expression in both places.
Hard
Hard 1: Verify a drop (90 minutes)
Extend the program so that the third process, after capset, tries setuid(0) and chown on a file it owns to user 0, and prints both results. Explain each result.
Done when: setuid(0) fails because the process has no CAP_SETUID, chown to another owner fails because it has no CAP_CHOWN, with a paragraph on why.
Hint
call setuid(0) and chown(path, 0, -1) and print strerror(errno).
Hard 2: no_new_privs (90 minutes)
Write a program that sets PR_SET_NO_NEW_PRIVS, then runs a setuid-root program (compile the whoami2 program of page 2 and make it setuid root) and prints the effective ID it sees. Compare with the run without the flag.
Done when: 65534 with the flag and 0 without, and a sentence on what execve did.
Hint
prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) before execl.
Hard 3: Choose the capabilities (90 minutes)
A backup tool must read every file and set no other rights, and a time-sync daemon must set the clock only. Name the single capability for each and say what the broad alternative would be and why it is worse.
Done when: CAP_DAC_READ_SEARCH (read access) or CAP_DAC_OVERRIDE (also write) for the backup tool, CAP_SYS_TIME for the clock, and the alternative is running as root or with CAP_SYS_ADMIN.
Hint
see the table in section 2 and the header file /usr/include/linux/capability.h.