cybersecurity · intermediate · ~15 min
Encode the symlink + traversal refusal rule.
Implement
int can_open_path_safely(const char *path, int last_is_symlink, int has_traversal).
Return 1 (safe) only if:
last_is_symlink == 0 (the final component is NOT a symlink), ANDhas_traversal == 0 (no .. segments).Else return 0 (refused).
This models the result of using openat + O_NOFOLLOW after a
path-traversal pre-check.
openat(dirfd, name, O_NOFOLLOW) returns ELOOP when the path is a symlink. Encoding that in a small policy lets you reason about the defence without a real filesystem.
path + two boolean flags from prior checks.
0/1.
Pure boolean logic.
int can_open_path_safely(const char *path, int last_is_symlink, int has_traversal) { /* TODO */ (void)path; (void)last_is_symlink; (void)has_traversal; return 0; }
Returning 1 even when traversal is detected.
NULL path. Both flags set.
O(1).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.