cybersecurity · intermediate · ~15 min · safe pentest lab
Read POSIX permission bits via stat.
Catch a secret file that is readable by more than its owner — a common cause of credential leaks in misconfigured deployments.
Implement int is_secret_file_safe(const char *path) that inspects the permission bits of the file at path.
path: a NUL-terminated path. The grader creates the test files locally and chmods them to known modes before each check.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).file chmod 0600 -> 1
file chmod 0644 -> 0 (world/group readable)
missing path -> -1
st_mode & 0777); ignore the file-type bits.A NUL-terminated path to a local file the grader created.
An int: 1 if mode 600, 0 if other bits set, -1 if it can't be stat'd.
Compare the low 9 permission bits (st_mode & 0777) against 0600.
#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.