Part 7 of 7 · 3 min read
Exercises
Easy
Easy 1: Octal (20 minutes)
Write rwxr-x--- and rw-r--r-- in octal.
Done when: 750 and 644.
Hint
each class is r = 4, w = 2, x = 1.
Easy 2: umask (20 minutes)
A program creates a file with 0666 and a directory with 0777 under umask 027. What modes result?
Done when: 640 and 750.
Hint
clear the bits that are set in the umask.
Easy 3: Which class (20 minutes)
A file is owned by alice, group staff, mode 0470. Bob is in staff. Can Bob write it? Can alice read it?
Done when: yes (the group bits are 7, rwx) and yes (the owner bits are 4, r--), and the remark that alice cannot write although Bob can.
Hint
0470 means owner 4, group 7, other 0. Bob matches the group.
Medium
Medium 1: Trace the rule (45 minutes)
A file has owner 100, group 200, mode 0604. Give read, write and execute for: uid 100 gid 300; uid 300 gid 200; uid 300 gid 300.
Done when: uid 100: r w -; uid 300 gid 200: - - - (group bits are 0); uid 300 gid 300: r - -.
Hint
0604 means owner 6 (rw-), group 0, other 4 (r--).
Medium 2: Run the program (45 minutes)
Compile the program of section 2 and test your own file with mode 0407 for two different user IDs. You need root to change users, or use sudo.
Done when: a listing in which kernel and rule agree, with a sentence on what surprised you.
Hint
use chmod 0407 file and chown.
Medium 3: Setuid in practice (45 minutes)
Compile the program of section 4. As root, chown root it and chmod u+s. Run it as an ordinary user, then with setpriv --no-new-privs (or unshare --no-new-privs if available).
Done when: three outputs like section 4, and a note that the filesystem must not be mounted nosuid.
Hint
copy the program, then chown root prog && chmod u+s prog.
Hard
Hard 1: ls -l in C (90 minutes)
Write a program that takes a path, calls stat, and prints the mode as ten characters like -rwsr-xr-x, including setuid, setgid and sticky bits (s, S, t, T), and the owner and group IDs.
Done when: the output matches ls -ld for /usr/bin/passwd, /tmp, /etc/shadow and a file of your own.
Hint
use S_IRUSR and the other constants of <sys/stat.h>.
Hard 2: Prove the drop (90 minutes)
Write a program that, as root, drops privilege to uid 65534 with the right call order and then tries setuid(0). Do it again with only seteuid(65534). Report what each attempt returns.
Done when: the first setuid(0) fails with EPERM and the second succeeds, with an explanation using the saved user ID.
Hint
after a seteuid, the saved ID is still 0.
Hard 3: Audit setuid files (90 minutes)
Write a program (or a find command) that lists all setuid and setgid files on your machine, with owner and size. Choose three and say why each needs the bit.
Done when: a list, and three reasons that name what each program does with its extra rights.
Hint
find / -xdev -perm /6000 -type f lists the files.