POSIX wordexp(3) is dangerous

Lobsters Hottest News

Summary

一篇 TIL 博客指出 POSIX 的 wordexp(3) 默认启用命令替换,即使传入 WRDE_NOCMD,musl 和 macOS 的 libc 实现也可能被绕过,建议不要在不可信输入上使用它。

<p><a href="https://lobste.rs/s/tbbiqb/posix_wordexp_3_is_dangerous">Comments</a></p>
Original Article
View Cached Full Text

Cached at: 10/27/26, 01:59 AM

# TIL: POSIX wordexp(3) is dangerous Source: [https://yossarian.net/til/post/posix-wordexp-3-is-dangerous/](https://yossarian.net/til/post/posix-wordexp-3-is-dangerous/) [`wordexp\(3\)`](https://pubs.opengroup.org/onlinepubs/9799919799/functions/wordexp.html)is a POSIX API for performing shell\-style expansions from C\. It's often cited/recommended as a way to do tilde expansion \(e\.g\.`~/foo`to`/home/me/foo`\): ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <wordexp.h> int expand_tilde(const char *input, char **output) { wordexp_t p; int ret = wordexp(input, &p, 0); if (ret == 0) { *output = strdup(p.we_wordv[0]); wordfree(&p); } return ret; } int main(void) { char *expanded = NULL; expand_tilde("~/foo", &expanded); printf("expanded: %s\n", expanded); return 0; } ``` However,`wordexp\(3\)`is also dangerous\! That last`0`argument is for the*flags*parameter\. By default, all expansion features are enabled,**including command expansion**: ``` expand_tilde("$(whoami)/foo", &expanded); ``` or: ``` expand_tilde("$(cat /etc/passwd)/foo", &expanded); ``` Disabling this requires passing[`WRDE\_NOCMD`](https://pubs.opengroup.org/onlinepubs/9799919799/functions/wordexp.html#:~:text=WRDE_NOCMD,Substitution%2C%20is%20requested.)as a flag\. Unfortunately, this palliative at best in some libc implementations: - [musl implements`wordexp\(3\)`](https://git.musl-libc.org/cgit/musl/tree/src/misc/wordexp.c)by delegating to the system shell\. It adds some perfunctory scanning for command substitutions before delegating, but fails to match some patterns that the underlying shell \(like GNU Bash\) can treat as valid command substitutions\. - [macOS's libc implements`wordexp\(3\)`](https://github.com/apple-oss-distributions/Libc/blob/71bbe350ab79eef58113991d817ccc6165061a64/gen/FreeBSD/wordexp.c#L201)in a similar manner, delegating to`/usr/lib/system/wordexp\-helper`[1](https://yossarian.net/til/post/posix-wordexp-3-is-dangerous/#fn-perl)\. According to[Julian Squires](https://www.cipht.net/)this had the same bypass as musl[as recently as 2023](https://www.cipht.net/2023/11/21/worried-by-wordexp.html), although I didn't bother testing whether it's still effective now that the default macOS shell is`zsh`instead of`bash`\. Bottom line: don't use`wordexp\(3\)`on untrusted inputs, even with`WRDE\_NOCMD`\. 1. Previously, macOS's`wordexp\(3\)`would shell out to Perl\.`wordexp\-helper`appears to be the "modern" solution\.[↩](https://yossarian.net/til/post/posix-wordexp-3-is-dangerous/#fnref-perl)

Similar Articles

stack unwinding can lead to leakless code execution

Lobsters Hottest

The article discusses how stack unwinding using DWARF bytecode in glibc can be exploited for code execution, illustrated through CTF challenges and analysis of thread cancellation in POSIX systems.

Don't Put Tilde In Your Path

Hacker News Top

一篇技术博文指出在 shell 配置中用引号包裹的 `PATH="$PATH:~/.local/bin"` 不会展开波浪号,导致当前目录下的字面 `./~/` 路径被加入 PATH,带来潜在劫持风险;建议改用 `$HOME`,并介绍了 nono 沙箱工具的相关警告与排查方法。

@apivixtls: Saw an open-source project called Codex5.5 for bypassing restrictions today. The approach is quite straightforward. It stuffs a set of unrestricted instructions into GPT-5.5 via model_instructions_file, making Codex CLI directly run in [MODE: UNRESTRICTED] mode…

X AI KOLs Timeline

An open-source project named Codex5.5 bypasses GPT-5.5's security restrictions by modifying the model_instructions_file. It supports sensitive operations like SQL injection testing, but carries a risk of account suspension. It is recommended to use a throwaway account.

@VincentLogic: Found a pretty cool CLI tool! OfficeCLI lets you work with Word, Excel, and PPT files right in the terminal — no Office installation needed. Create, read, and modify files with ease, making it super handy for automation scripts. The best part? Once installed, Claude Code, Cursor, and other AI coding assistants can …

X AI KOLs Timeline

OfficeCLI is an open-source command-line tool that lets you create, read, and modify Word, Excel, and PPT files in the terminal without installing Office. It integrates with AI coding assistants like Claude Code and Cursor, making it ideal for automation scripts and batch file processing.

Termaxa

Product Hunt

Termaxa 是一个开源 Rust 工具,作为 Claude Code、Codex、Cursor 和 Copilot 等 AI 代理命令执行路径中的安全闸门,在命令运行前预览其将删除的内容、先进行备份,并按用户策略拒绝或放行。