cybersecurity · intermediate · ~15 min
The classic PATH-hijack audit primitive.
Flag a directory that is unsafe to have in a privileged user's $PATH — a writable PATH directory is a privilege-escalation primitive, and this audit is the cheap defence.
Implement int dir_is_path_hazard(unsigned mode, int owner_uid) that returns 1 if the directory is dangerous, else 0.
mode: the directory's POSIX st_mode bits the grader passes. Relevant bits: 0002 = world-writable, 0020 = group-writable.owner_uid: the directory's owner uid (0 = root).Returns int: 1 if the directory is world-writable, OR group-writable and not owned by root (owner_uid != 0); else 0.
dir_is_path_hazard(0777, 1000) -> 1 (world-writable)
dir_is_path_hazard(0775, 1000) -> 1 (group-writable, non-root owner)
dir_is_path_hazard(0775, 0) -> 0 (group-writable but root-owned)
dir_is_path_hazard(0755, 1000) -> 0
01000) and setuid bit do not change the answer here.mode as octal.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.
An unsigned mode (POSIX st_mode bits) and an int owner_uid.
An int: 1 if the directory is a PATH hazard, else 0.
World-writable -> hazard; group-writable + non-root owner -> hazard. Pure bit math.
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.