cybersecurity · intermediate · ~15 min
The env-scrubber pattern used by every setuid program.
Implement int is_dangerous_env(const char *kv) returning 1
if the NAME=VALUE env string starts with one of these names (followed
by =):
LD_PRELOADLD_LIBRARY_PATHLD_AUDITDYLD_INSERT_LIBRARIESDYLD_LIBRARY_PATHDYLD_FORCE_FLAT_NAMESPACEIFSElse return 0.
Setuid binaries that inherit LD_PRELOAD or LD_LIBRARY_PATH are owned. Stripping the dangerous variables BEFORE execve is the standard defence.
One NAME=VALUE string (no newline).
0/1.
Match name followed by =. Never partial-match.
int is_dangerous_env(const char *kv) { /* TODO */ (void)kv; return 0; }
Using strstr — matches MY_LD_PRELOAD=... too. Use strncmp + '=' check.
No '=' in input. Name without value (LD_PRELOAD=). Empty string.
O(N * len) where N is list length.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.