*sshole, or when are we finally going to get rid of C?

*sshole, or when are we finally going to get rid of C?

混蛋,或者我们到底什么时候才能摆脱 C 语言?

2026-11-06 · 4 min read types shortsOriginal art by Genildo Ronchi. Yet another day, yet another vulnerability in core C code. This one happened due to two different conditions using the same sentinel value:

2026-11-06 · 4 分钟阅读 · 类型:短文 · 原图作者:Genildo Ronchi。又是一天,C 语言核心代码中又出现了一个漏洞。这次漏洞的起因是两个不同的条件使用了同一个哨兵值:

On Unix, a password hash of "" has meant a locked account since the very early days. Since 2012, musl libc’s crypt function has returned the string "" for a password that’s too long for it to hash.

在 Unix 系统中,自早期以来,密码哈希值 "" 一直意味着账户被锁定。自 2012 年起,musl libc 的 crypt 函数在处理过长的密码时,会返回字符串 "" 作为哈希结果。

The two points combined, along with sshd hashing login attempts for locked accounts to avoid timing leaks, mean that on musl systems without PAM (like, oh, the official Alpine AWS image), an unauthenticated user now gets a (possibly root!) shell on your computer. See this Alpine advisory for more details.

这两点结合起来,再加上 sshd 为了避免计时攻击而对锁定账户的登录尝试进行哈希处理,意味着在没有 PAM 的 musl 系统上(比如官方的 Alpine AWS 镜像),未经身份验证的用户现在可以在你的计算机上获得一个(可能是 root 权限的!)shell。详情请参阅 Alpine 的安全公告。

This is not a memory safety vulnerability; rather, it is one of those issues that, people often say, languages with better type systems won’t save you from. This is true! You can very well write a Rust function that returns ”*” if the password is too long. But the big difference between C and Rust in this regard is that the norms of the Rust community do not allow that.

这并非内存安全漏洞;相反,它是人们常说的那种“即使使用类型系统更好的语言也无法避免”的问题。确实如此!你完全可以用 Rust 写出一个在密码过长时返回 ”*” 的函数。但在这方面,C 和 Rust 的巨大区别在于,Rust 社区的规范不允许这样做。

The POSIX spec tries to account for error cases. It says: Upon successful completion, crypt() shall return a pointer to the hashed password; […] Otherwise, it shall return a null pointer and set errno to indicate the error. But the C type system can’t force developers to check that the return value is NULL. The musl libc developers saw that some binaries weren’t checking for it, and as a result deviated from the spec, returning the sentinel ”*” value instead.

POSIX 规范试图考虑错误情况。它规定:成功完成后,crypt() 应返回指向哈希密码的指针;[…] 否则,它应返回空指针并设置 errno 以指示错误。但 C 语言的类型系统无法强制开发者检查返回值是否为 NULL。musl libc 的开发者发现一些二进制程序没有进行检查,因此偏离了规范,转而返回哨兵值 ”*”。

As I’ve written about before, type systems have a strong effect on engineering culture. Because C doesn’t have algebraic data types or result types, the engineering norms that form around C communities lead to this kind of in-band signaling. Meanwhile, in Rust, a password that’s too long would be returned as an error variant, and the caller would be compelled to handle it. What if there isn’t an error variant today? Then a breaking change would add an error variant, and a RustSec advisory would probably be published as well.

正如我之前写过的,类型系统对工程文化有很强的影响。由于 C 语言没有代数数据类型或结果类型,围绕 C 社区形成的工程规范导致了这种带内信号传输。与此同时,在 Rust 中,过长的密码会作为错误变体返回,调用者将被迫处理它。如果现在还没有错误变体怎么办?那么一个破坏性的变更会增加一个错误变体,并且很可能会发布一份 RustSec 安全公告。

As an aside, it remains surprising to me when Linux distro maintainers complain about projects introducing Rust dependencies. Rather than adapting to the new world where the engineering norms are likely to serve users better, these maintainers insist on the world adapting to them. Yes, compile times and portability to niche architectures are real costs, but are you here to serve your own interests or the bulk of your users’? There’s a real principal-agent problem going on here.

顺便提一下,当 Linux 发行版维护者抱怨项目引入 Rust 依赖时,我仍然感到惊讶。这些维护者没有去适应那种工程规范可能更好地服务于用户的新世界,反而坚持让世界去适应他们。是的,编译时间和对小众架构的可移植性确实是成本,但你是为了服务自己的利益,还是为了服务广大用户?这里确实存在严重的委托代理问题。

Agents won’t save you. Speaking of agents! There’s a recent strain of argument that goes roughly like this: Rust is admittedly useful for humans to write correct software today. LLMs can now, or in the near future will be able to, reason about memory safety and other error cases directly. So in an agent-heavy development lifecycle there’s no difference in software rigor between Rust and C (or even assembly). But C has certain advantages over Rust in terms of build speed, portability, etc. So we might as well write software in C (or even assembly).

代理无法拯救你。说到代理!最近有一种观点大致如下:不可否认,Rust 对人类编写正确的软件很有用。LLM 现在或在不久的将来将能够直接推理内存安全和其他错误情况。因此,在以代理为主导的开发生命周期中,Rust 和 C(甚至汇编语言)在软件严谨性上没有区别。但 C 在构建速度、可移植性等方面比 Rust 有一定优势。所以我们不妨用 C(甚至汇编语言)编写软件。

There is plenty that’s wrong about the argument, including the dubious decision to trust a fuzzy process when we have already shown that a far more deterministic approach works. But the big missing piece is the norms of the respective communities! When you (or your agent) write Rust software, you’re probably going to use Rust libraries, and your agent is probably going to pattern-match on existing Rust code in its training set. As a result, even if you aren’t already a skilled Rust developer, there will likely be more invariants encoded in the type system.

这种论点有很多错误之处,包括当我们已经证明一种更具确定性的方法有效时,却选择信任一个模糊过程的这种可疑决定。但缺失的关键部分是各自社区的规范!当你(或你的代理)编写 Rust 软件时,你很可能会使用 Rust 库,而你的代理很可能会根据其训练集中的现有 Rust 代码进行模式匹配。因此,即使你还不是一名熟练的 Rust 开发者,类型系统中也可能会编码更多的不变性。

The engineering norms of C developers allow for ”*” to be an acceptable sentinel value indicating an error; the engineering norms of Rust developers simply do not. These norms are the result of the accumulated contributions of a community, and part of the responsibility of a software professional is to think about the kinds of communities one’s work is a part of. But some people have decided that none of this really matters in this shiny new world, and that the contributions of other people can mostly be ignored. Their hubris will be their downfall.

C 语言开发者的工程规范允许 ”*” 作为表示错误的哨兵值;而 Rust 开发者的工程规范则完全不允许。这些规范是一个社区长期贡献积累的结果,软件专业人员的责任之一就是思考自己的工作属于什么样的社区。但有些人认为,在这个光鲜亮丽的新世界里,这一切都不重要,他人的贡献大多可以被忽略。他们的傲慢将导致他们的失败。