cybersecurity · intermediate · ~15 min

Does it create atomically?

Recognise the atomic-create defence.

Challenge

Check whether an open call requests atomic creation — O_CREAT|O_EXCL fails if the file already exists, closing the create-time TOCTOU window.

Task

Implement int uses_excl(const char *flags) that returns 1 if the open-flags string contains the substring "O_EXCL", and 0 otherwise.

Input

  • flags: a NUL-terminated open-flags string the grader provides (e.g. "O_CREAT|O_EXCL|O_WRONLY").

Output

Returns 1 if O_EXCL is present, 0 otherwise.

Example

uses_excl("O_CREAT|O_EXCL|O_WRONLY")   ->   1
uses_excl("O_CREAT|O_WRONLY")          ->   0

Edge cases

  • A flags string without O_EXCL returns 0.

Input format

A NUL-terminated open-flags string flags.

Output format

1 if flags contains "O_EXCL"; otherwise 0.

Constraints

Substring test for "O_EXCL".

Starter code

#include <string.h>

int uses_excl(const char *flags) {
    /* TODO */
    return 0;
}

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