Skip to main content
QUICK REVIEW

[论文解读] Narcissus: Deriving Correct-By-Construction Decoders and Encoders from Binary Formats

Benjamin Delaware, Sorawit Suriyakarn|arXiv (Cornell University)|Mar 13, 2018
Advanced Malware Detection Techniques参考文献 42被引用 3
一句话总结

Narcissus 是一个基于 Coq 的框架,使用类似解析器组合子的语法,自动推导出复杂、非上下文无关的二进制格式的正确性保证的解码器和编码器。通过利用证明自动化与双向推导,它确保编码器-解码器的可逆性,并提供机器可检查的正确性证明,从而在 MirageOS 等系统中实现安全、高效的实现,性能开销极低。

ABSTRACT

It is a neat result from functional programming that libraries of parser combinators can support rapid construction of decoders for quite a range of formats. With a little more work, the same combinator program can denote both a decoder and an encoder. Unfortunately, the real world is full of gnarly formats, as with the packet formats that make up the standard Internet protocol stack. Most past parser-combinator approaches cannot handle these formats, and the few exceptions require redundancy -- one part of the natural grammar needs to be hand-translated into hints in multiple parts of a parser program. We show how to recover very natural and nonredundant format specifications, covering all popular network packet formats and generating both decoders and encoders automatically. The catch is that we use the Coq proof assistant to derive both kinds of artifacts using tactics, automatically, in a way that guarantees that they form inverses of each other. We used our approach to reimplement packet processing for a full Internet protocol stack, inserting our replacement into the OCaml-based MirageOS unikernel, resulting in minimal performance degradation.

研究动机与目标

  • 解决现实系统中复杂二进制格式的编码器和解码器缺乏正确性保证、无冗余规格说明与实现的问题。
  • 通过集成 Coq 的证明自动化进行形式化验证,消除用户引入的编码器/解码器生成错误。
  • 支持细粒度、可扩展的格式规格说明,处理网络中常见的上下文敏感、非上下文无关协议。
  • 通过生成编码和解码逻辑的机器可检查正确性证明,实现与已验证系统的无缝集成。
  • 通过在 MirageOS 通用内核中用其替换数据包处理模块,证明其在实际应用中的可行性,性能影响极小。

提出的方法

  • 在 Coq 中使用领域特定嵌入式语言(EDSL)通过组合子(如 `format_word`、`++`(序列)和 `format_unused_word`(填充))指定二进制格式。
  • 使用自定义策略 `derive_encoder_decoder_pair`,从单一格式规格自动生成编码器和解码器函数。
  • 通过在推导过程中传递类型和位级信息,保持双向一致性,确保编码和解码互为精确逆操作。
  • 在带有 `GetCurrentByte` 和 `SetCurrentByte` 操作的状态异常单子中生成代码,以建模字节流 I/O。
  • 应用位级操作原语(如 `high_bits` 和 `low_bits`)处理多字节字的序列化与重组。
  • 利用 Coq 的证明自动化生成并消解正确性证明,确保每个生成的编码器-解码器对在数学上均成立。

实验结果

研究问题

  • RQ1是否可以通过函数式编程方式的格式规格说明,生成无冗余的正确性保证的编码器和解码器?
  • RQ2如何通过单一格式规格,形式化保证地推导出编码器和解码器函数?
  • RQ3该框架能否处理复杂、非上下文无关的二进制格式,如标准网络协议中的格式?
  • RQ4在真实世界通用内核系统中使用形式化验证的编码器-解码器栈时,性能开销如何?
  • RQ5该框架在多大程度上可通过用户自定义的格式组合子和实现策略进行扩展?

主要发现

  • Narcissus 框架成功为所有主要网络数据包格式(包括具有对齐、填充和复杂字段依赖关系的格式)推导出正确性保证的编码器和解码器。
  • 该框架利用 Coq 的证明自动化,确保每个生成的编码器-解码器对在数学上被证明互为逆操作,彻底消除编码-解码不匹配的可能性。
  • 在 MirageOS 通用内核中替换数据包处理模块时,该方法仅造成极小的性能下降,证明了其实际效率。
  • 通过用户自定义的格式组合子,该框架支持可扩展性,并可与任意 Coq 数据类型互操作,而不仅限于固定大小的 C 风格结构体。
  • 生成的代码无冗余,因为同一格式规格直接驱动编码器和解码器的生成,无需重复或手动转换。
  • 与现有代码生成工具和解析器组合子工具相比,该框架在正确性保证方面表现更优,因其提供机器可检查的证明,而非非正式或临时的验证。

更好的研究,从现在开始

从阅读论文到最终审阅,大幅缩短您的研究时间。

无需绑定信用卡

本解读由 AI 生成,并经人工编辑审核。