cybersecurity · beginner · ~15 min
Defensive scan for NUL-byte smuggling.
Some legacy APIs treat strings as 'up to NUL', but the
buffer the caller actually allocated extends further. An attacker passes
a value like safe.txt\0/etc/passwd — the API stops at NUL and opens
safe.txt, but a downstream layer reads the buffer raw and sees the rest.
Implement int has_nul_smuggle(const char *buf, int allocated_len).
Treating buf as a byte buffer of allocated_len bytes, return 1 if there
is any non-NUL byte at any index after the first NUL in buf, else 0.
Some attacker-controlled input crosses a NUL byte through legacy APIs, smuggling one filename and reading another. Detecting it is a defensive first pass.
Byte buffer + size.
0/1.
Read exactly allocated_len bytes.
int has_nul_smuggle(const char *buf, int allocated_len) { /* TODO */ (void)buf; (void)allocated_len; return 0; }
Using strlen — that's the bug being defended against.
No NUL anywhere. NUL at the last byte (no follow-on). All-NUL.
O(allocated_len).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.