cybersecurity · intermediate · ~15 min

Strip dangerous environment variables before exec

The env-scrubber pattern used by every setuid program.

Challenge

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_PRELOAD
  • LD_LIBRARY_PATH
  • LD_AUDIT
  • DYLD_INSERT_LIBRARIES
  • DYLD_LIBRARY_PATH
  • DYLD_FORCE_FLAT_NAMESPACE
  • IFS

Else return 0.

Why this matters

Setuid binaries that inherit LD_PRELOAD or LD_LIBRARY_PATH are owned. Stripping the dangerous variables BEFORE execve is the standard defence.

Input format

One NAME=VALUE string (no newline).

Output format

0/1.

Constraints

Match name followed by =. Never partial-match.

Starter code

int is_dangerous_env(const char *kv) { /* TODO */ (void)kv; return 0; }

Common mistakes

Using strstr — matches MY_LD_PRELOAD=... too. Use strncmp + '=' check.

Edge cases to handle

No '=' in input. Name without value (LD_PRELOAD=). Empty string.

Complexity

O(N * len) where N is list length.

Background lessons

Up next

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