cybersecurity · beginner · ~10 min · safe pentest lab

Audit a file mode for risky bits

Inspect Unix permission bits with bitmasks.

Challenge

Your job

int audit_mode(unsigned int mode);

Return a bitmask of risky bits in a Unix file mode:

  • bit 0 (1) — world-writable (0002)
  • bit 1 (2) — setuid (04000)
  • bit 2 (4) — setgid (02000)

Examples: 06440, 06661, 047552, 067777.

Hints

  1. mode & 02 → world-writable.
  2. mode & 04000 → setuid; mode & 02000 → setgid.

Why this matters

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

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.