NewsCrawler.org

POSIX wordexp(3) is dangerous

POSIX wordexp(3) is dangerous
Image: yossarian.net

wordexp(3)[1] is a POSIX API that performs shell-style expansions from C and is often recommended as a way to do tilde expansion[2], turning ~/foo into /home/me/foo. By default, all expansion features are enabled, including command substitution[3]; disabling it requires passing the WRDE_NOCMD[4] flag. Even with WRDE_NOCMD this is unreliable: musl implements wordexp(3) by delegating to the system shell, and its scan for command substitutions misses patterns that the underlying shell, such as GNU Bash, treats as valid, while macOS's libc delegates to /usr/lib/system/wordexp-helper and, according to Julian Squires, had the same bypass as recently as 2023. Bottom line: do not use wordexp(3) on untrusted inputs, even with WRDE_NOCMD; macOS's wordexp(3) previously shelled out to Perl, and wordexp-helper is the newer replacement.

Read the original yossarian.netDiscussion

Discussion

Commenters mostly agree the function is best avoided: one notes that command substitution requires support for the entire shell language, another that wordexp(3) usually forks a shell and is unusable in multithreaded programs. Theo de Raadt wrote in 2010 that wordexp() would never be added to OpenBSD's libc, and the Hare project rejected adding it to its standard library. The i3 window manager, however, deliberately uses wordexp for wildcard expansion and command substitution in its include directive.

Sources

Published: First seen: Читать по-русски

Open as a page