Removing binder.c
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
Removing binder.c
By Daroc AldenOctober 30, 2026 Kangrejos The final talk at Kangrejos 2026 was about the removal of the C implementation of Android’s Binder driver, the primary interprocess communication mechanism used on Android devices. Carlos Llamas announced that he had deleted the C code to general applause, and then shared some insights about what went into replacing the C implementation. Alice Ryhl, who has worked on the Rust implementation for some years, added her own thoughts about what the most difficult and interesting parts of the transition were.
Kangrejos 2026 大会的最后一场演讲主题是移除 Android Binder 驱动的 C 语言实现,这是 Android 设备上主要的进程间通信机制。Carlos Llamas 在全场的掌声中宣布他已经删除了这些 C 代码,并分享了关于替换 C 实现过程中的一些见解。Alice Ryhl 多年来一直致力于 Rust 实现的开发,她也补充了自己对于此次转型中最困难和最有趣部分的看法。
Binder was added to the kernel in 2008, Llamas said, as an interprocess communication mechanism for Android devices. Its design originated in BeOS, but it has grown significantly since then. It provides a rich set of operations for allowing processes to communicate with the minimum amount of overhead. It also, therefore, presents a large attack surface for Android. Many complicated chained attacks against Android have involved Binder at one step or another, Llamas explained. Google’s internal “red team” (the team dedicated to proactive security research) deemed Binder to be the most vulnerability-dense component in the entire operating system, with more CVEs per line than anywhere else. That made it an obvious candidate for porting to Rust, Llamas said.
Llamas 表示,Binder 于 2008 年被加入内核,作为 Android 设备的进程间通信机制。其设计源于 BeOS,但自那时起已有了显著的发展。它提供了一套丰富的操作,允许进程以最小的开销进行通信。因此,它也为 Android 带来了巨大的攻击面。Llamas 解释说,许多针对 Android 的复杂连锁攻击在某个环节都涉及 Binder。Google 内部的“红队”(致力于主动安全研究的团队)认为 Binder 是整个操作系统中漏洞最密集的组件,其每行代码的 CVE 数量高于其他任何地方。Llamas 说,这使其成为移植到 Rust 的首选对象。
Binder was the first user of Rust in the kernel, he added, although Ryhl did not publicly post the first version of what became the Rust implementation until 2023, well after the Rust experiment started in 2021. All told, the effort took more than five years. Binder is used for every single screen touch, permission check, and cross-application message on Android, which runs on more than three billion devices. In total, it works out to hundreds of billions of Binder transactions per second, globally, he added. That scale meant that even the smallest discrepancy between the interface provided by the C version and the one provided by the Rust version would cause problems and regressions for some use case.
他补充说,Binder 是内核中第一个使用 Rust 的组件,尽管 Ryhl 直到 2023 年才公开了后来成为 Rust 实现的第一个版本,这距离 2021 年开始的 Rust 实验已经过去很久了。总而言之,这项工作耗时超过五年。Binder 被用于 Android 上的每一次屏幕触摸、权限检查和跨应用消息传递,而 Android 运行在超过 30 亿台设备上。他补充道,总计在全球范围内,每秒会产生数千亿次 Binder 事务。这种规模意味着,C 版本提供的接口与 Rust 版本提供的接口之间即使存在最微小的差异,也会导致某些用例出现问题和回归。
One particularly tricky aspect has been performance, Ryhl said. Throughput doesn’t matter that much; what matters is the tail latencies. Most screen frames on Android have a Binder transaction in the critical path of the frame, she explained. So, a difference of two microseconds on average is not a real problem, but if Binder is ever twenty milliseconds slower, even once in a while, people will notice and care. The actual algorithms that the C and Rust implementations use are identical, she continued, so there are no large differences in throughput. But, because Rust handles locking differently, there were problems with the tail latency from lock contention that she had to debug.
Ryhl 表示,一个特别棘手的方面是性能。吞吐量并不重要,重要的是尾部延迟。她解释说,Android 上大多数屏幕帧的渲染关键路径中都有一个 Binder 事务。因此,平均两微秒的差异并不是真正的问题,但如果 Binder 有时慢了 20 毫秒,哪怕只是偶尔发生,人们也会注意到并对此感到不满。她继续说道,C 和 Rust 实现所使用的实际算法是相同的,因此在吞吐量上没有大的差异。但是,由于 Rust 处理锁的方式不同,导致了锁竞争带来的尾部延迟问题,她必须对此进行调试。
For example, one user-reported traceback showed Binder attempting to take a mutex that was held by another process. That process was preempted, and did not resume executing for twenty milliseconds, well past the sixteen millisecond budget to render a single frame. It was a classic case of priority inversion. To solve the problem in Binder, Ryhl moved as many operations out from under the lock as possible, and was able to replace it with a spinlock, which made the problem go away. The equivalent lock in the C implementation was already a spinlock: “I deviated from C Binder, and pow. It has eighteen years of accumulated quirks.”
例如,一个用户报告的追踪信息显示,Binder 试图获取一个被另一个进程持有的互斥锁。该进程被抢占,并且在 20 毫秒内没有恢复执行,这远远超过了渲染单帧 16 毫秒的预算。这是一个典型的优先级反转案例。为了解决 Binder 中的这个问题,Ryhl 将尽可能多的操作移出了锁的范围,并将其替换为自旋锁,从而消除了该问题。C 实现中的等效锁已经是自旋锁了:“我偏离了 C Binder 的做法,结果就出事了。它积累了 18 年的各种怪癖。”
Eventually, however, the Rust implementation was good enough for most use cases. And the C implementation was still producing new vulnerabilities every time someone tried to introduce a new feature. Eventually, Llamas and Ryhl decided to only add new features to the Rust implementation, in order to motivate people to switch and to avoid headaches from the C implementation. That policy has resulted in the Rust version becoming faster than the C version, because Ryhl was able to introduce an algorithmic improvement that reduced the number of needed system calls, a change that had been considered too complex for the C implementation. Now that the C implementation has been deleted, there are more of those coming, she promised.
然而,最终 Rust 实现对于大多数用例来说已经足够好了。而每当有人试图引入新功能时,C 实现仍然会产生新的漏洞。最终,Llamas 和 Ryhl 决定只在 Rust 实现中添加新功能,以激励人们进行切换,并避免 C 实现带来的麻烦。这一政策使得 Rust 版本变得比 C 版本更快,因为 Ryhl 能够引入一种算法改进,减少了所需的系统调用次数,而这种改变在 C 实现中曾被认为过于复杂。她承诺,现在 C 实现已经被删除,未来还会有更多这样的改进。
Another area that proved difficult was dealing with tracepoints. Users aren’t supposed to rely on the presence or behavior of most kernel-internal tracepoints, but they do. This meant that Ryhl had to copy the existing tracepoints in Binder as a de facto part of the driver’s API. That even extended into some of the data structures involved in the driver. In one case, she said, there was a reference-counted structure that had a slightly different reference count in the Rust implementation, because the Rust code was holding an additional pointer to the structure. That turned out to be a problem for one particular piece of client code, since it relied on checking the reference count via a tracepoint in order to determine when the C implementation had reached a particular state.
另一个被证明很困难的领域是处理跟踪点(tracepoints)。用户本不应该依赖大多数内核内部跟踪点的存在或行为,但他们确实这样做了。这意味着 Ryhl 不得不将 Binder 中现有的跟踪点复制过来,作为驱动程序 API 的事实组成部分。这甚至延伸到了驱动程序涉及的一些数据结构中。她说,在一个案例中,有一个引用计数结构在 Rust 实现中的引用计数略有不同,因为 Rust 代码持有该结构的一个额外指针。这对于某段特定的客户端代码来说是个问题,因为它依赖于通过跟踪点检查引用计数,以确定 C 实现何时达到了特定状态。
There was also a lot of toolchain support needed, Ryhl continued. The Rust implementation had to work with kernel address-space layout randomization (KASLR), control-flow integrity (CFI), and profile-guided optimization. Production Android kernels are built with all of those enabled, and since they impact code generation, the Rust code needed to account for them. Llamas then went into more detail about the deployment plan that will be used to convert users to the Rust implementation. A major problem with just rolling the Rust implementation out is the existence of vendor hooks: custom tracepoints used to inject vendor-specific behavior into the kernel.
Ryhl 继续说道,还需要大量的工具链支持。Rust 实现必须与内核地址空间布局随机化 (KASLR)、控制流完整性 (CFI) 和配置引导优化 (PGO) 协同工作。生产环境的 Android 内核在构建时都启用了这些功能,由于它们会影响代码生成,因此 Rust 代码必须考虑到它们。随后,Llamas 更详细地介绍了将用户转换为 Rust 实现的部署计划。直接推广 Rust 实现的一个主要问题是供应商钩子 (vendor hooks) 的存在:即用于将供应商特定行为注入内核的自定义跟踪点。
The Rust implementation of Binder was not able to reproduce those in every case, and therefore many vendors are stuck on the C implementation until they update their code. That’s going to be a slow process. Old stable versions of the kernel will continue to have the C Binder, and receive security fixes, which gives the various Android vendors a little bit of leeway until they’re forced to update. After the conference, the patch removing the C implementation was merged.
Binder 的 Rust 实现无法在所有情况下重现这些钩子,因此许多供应商在更新代码之前只能继续使用 C 实现。这将是一个缓慢的过程。旧的稳定版内核将继续保留 C Binder 并接收安全修复,这为各 Android 供应商在被迫更新之前提供了一定的缓冲空间。会议结束后,移除 C 实现的补丁已被合并。