0xMiden / 0xMiden/protocol

Smart Contract Diff Audit L-24: Failed Library Linking Leaves `CodeBuilder` In An Inconsistent State

Aberta
#3,683 1 comentário 0 reações 0 responsáveis Ver no GitHub
Linguagem predominante
Rust
Estrelas
132
Forks
167
Merge médio
1d 23h
PRs com merge (30d)
110

Descrição

The [`CodeBuilder`](https://github.com/0xMiden/protocol/blob/8411bf093bde25285708faac152b6d7269009617/crates/miden-standards/src/code_builder/mod.rs#L249-L253) type owns a single `Assembler` for its entire lifetime and exposes [`link_static_library`](https://github.com/0xMiden/protocol/blob/8411bf093bde25285708faac152b6d7269009617/crates/miden-standards/src/code_builder/mod.rs#L340-L346) and [`link_dynamic_library`](https://github.com/0xMiden/protocol/blob/8411bf093bde25285708faac152b6d7269009617/crates/miden-standards/src/code_builder/mod.rs#L361-L367), which take the builder by mutable reference and forward to `Assembler::link_package`. In `miden-assembly` version `0.24.1`, as pinned in `Cargo.lock`, that call records the library entry before registering any module and then registers the library modules one at a time, aborting mid-loop with a duplicate-module error when a module path collides with a path already registered by another linked library. No rollback is performed, so the library entry and any modules registered before the collision remain in the linker.

A caller that handles the returned error and keeps using the same builder therefore continues with linker state that no longer matches the set of successfully linked libraries. Module paths from the failed library stay reserved and can make a later legitimate link fail with a duplicate error that does not correspond to any real conflict, and repeating the identical link reports success, because the already recorded library entry short-circuits the operation, while the module graph remains incomplete. The failure then resurfaces as an unresolved symbol during a subsequent compilation. The effect is confined to off-chain script compilation and produces compilation errors rather than incorrect compiled output. The `# Errors` documentation on both methods does not indicate that a failed call leaves the builder inconsistent, and only these two methods can leave a live inconsistent instance, since the chaining variants [`with_statically_linked_library`](https://github.com/0xMiden/protocol/blob/8411bf093bde25285708faac152b6d7269009617/crates/miden-standards/src/code_builder/mod.rs#L378-L384) and [`with_dynamically_linked_library`](https://github.com/0xMiden/protocol/blob/8411bf093bde25285708faac152b6d7269009617/crates/miden-standards/src/code_builder/mod.rs#L395-L401) consume the builder and drop it on error.

Consider treating a link failure as fatal for the builder instance, for example by recreating the internal `Assembler` or by marking the builder unusable once a link returns an error, and documenting that a failed link does not leave the builder unchanged. Consider also raising the missing rollback with the `miden-assembly` maintainers so that library linking becomes transactional, committing registered modules and the library entry only once the whole package has been linked.

---

_Copied verbatim from finding [L-24](https://audits.openzeppelin.com/miden/miden-01-07-smart-contract-diff-audit-nfts/issues/failed-library-linking-leaves-codebuilder-in-an-inconsistent-state-a3307990) (low severity) of the OpenZeppelin [smart contract diff audit (NFTs)](https://audits.openzeppelin.com/miden/miden-01-07-smart-contract-diff-audit-nfts). The audit was performed against commit `8411bf093bde25285708faac152b6d7269009617`._

Guia de contribuição

Abrir o guia de contribuição

Avaliação

Esta issue ainda não foi avaliada.

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.