cybersecurity · intermediate · ~15 min · safe pentest lab

Check local file permissions

Read POSIX permission bits via stat.

Challenge

Catch a secret file that is readable by more than its owner — a common cause of credential leaks in misconfigured deployments.

Task

Implement int is_secret_file_safe(const char *path) that inspects the permission bits of the file at path.

Input

  • path: a NUL-terminated path. The grader creates the test files locally and chmods them to known modes before each check.

Output

Returns an int:

  • 1 if the file is exactly mode 600 (read+write for the owner only).
  • 0 if it exists but has any other permission bits set.
  • -1 if it cannot be stat'd (e.g. missing).

Example

file chmod 0600   ->   1
file chmod 0644   ->   0   (world/group readable)
missing path      ->   -1

Edge cases

  • Compare only the low 9 permission bits (st_mode & 0777); ignore the file-type bits.
  • A nonexistent path returns -1, not 0.

Input format

A NUL-terminated path to a local file the grader created.

Output format

An int: 1 if mode 600, 0 if other bits set, -1 if it can't be stat'd.

Constraints

Compare the low 9 permission bits (st_mode & 0777) against 0600.

Starter code

#include <sys/stat.h>
#include <stdio.h>

int is_secret_file_safe(const char *path) {
    /* TODO */
    return -1;
}

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