cybersecurity · beginner · ~10 min · safe pentest lab
Inspect Unix permission bits with bitmasks.
Audit a Unix file mode for the three bits every permission and privilege-escalation review looks for: world-writable, setuid, setgid.
Implement int audit_mode(unsigned int mode) that returns a bitmask of the risky bits set in mode.
mode: a Unix file mode as an integer (octal in the examples) the grader passes.Returns an int bitmask:
1) — world-writable (0002 set)2) — setuid (04000 set)4) — setgid (02000 set)
OR together the bits for whichever are present.audit_mode(0644) -> 0
audit_mode(0666) -> 1 (world-writable)
audit_mode(04755) -> 2 (setuid)
audit_mode(02755) -> 4 (setgid)
audit_mode(06777) -> 7 (all three)
0002), not the owner or group write bit.0644 returns 0.World-writable, setuid, and setgid bits are the trio every Linux permission audit (and privilege-escalation check) looks for.
A Unix file mode as an unsigned integer.
An int bitmask: bit0=world-writable, bit1=setuid, bit2=setgid.
Bit inspection only — no filesystem access.
int audit_mode(unsigned int mode) {
/* TODO */
(void)mode;
return 0;
}
Confusing owner/group/other write bits. Decimal instead of octal literals.
A safe 0644. All three risky bits at once (06777).
O(1).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.