cybersecurity · intermediate · ~15 min
The classic PATH-hijack audit primitive.
Given a directory's stat-mode bits and owner uid, determine whether the directory is dangerous to have in $PATH for a privileged user.
Implement int dir_is_path_hazard(unsigned mode, int owner_uid).
Mode bits (POSIX st_mode):
0002 = world-writable0020 = group-writableReturn 1 if:
A writable directory in $PATH is a privilege-escalation primitive: drop a binary called ls, wait for a privileged user. The audit step is the cheap defence.
mode (octal) + owner uid.
0/1.
Pure bit math on POSIX mode flags.
int dir_is_path_hazard(unsigned mode, int owner_uid) { /* TODO */ (void)mode; (void)owner_uid; return 0; }
Treating mode as decimal. Forgetting the group-writable+non-root case.
Sticky bit set (01000): irrelevant here. Setuid bit: irrelevant here.
O(1).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.