cybersecurity · beginner · ~10 min · safe pentest lab

Audit a file mode for risky bits

Inspect Unix permission bits with bitmasks.

Challenge

Audit a Unix file mode for the three bits every permission and privilege-escalation review looks for: world-writable, setuid, setgid.

Task

Implement int audit_mode(unsigned int mode) that returns a bitmask of the risky bits set in mode.

Input

  • mode: a Unix file mode as an integer (octal in the examples) the grader passes.

Output

Returns an int bitmask:

  • bit 0 (1) — world-writable (0002 set)
  • bit 1 (2) — setuid (04000 set)
  • bit 2 (4) — setgid (02000 set) OR together the bits for whichever are present.

Example

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)

Edge cases

  • World-writable is the others write bit (0002), not the owner or group write bit.
  • A safe mode like 0644 returns 0.

Rules

  • Pure bit inspection of the integer — no filesystem access.

Why this matters

World-writable, setuid, and setgid bits are the trio every Linux permission audit (and privilege-escalation check) looks for.

Input format

A Unix file mode as an unsigned integer.

Output format

An int bitmask: bit0=world-writable, bit1=setuid, bit2=setgid.

Constraints

Bit inspection only — no filesystem access.

Starter code

int audit_mode(unsigned int mode) {
    /* TODO */
    (void)mode;
    return 0;
}

Common mistakes

Confusing owner/group/other write bits. Decimal instead of octal literals.

Edge cases to handle

A safe 0644. All three risky bits at once (06777).

Complexity

O(1).

Background lessons

Up next

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.