0xMiden / 0xMiden/protocol

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

Đang mở
#3,683 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Rust
Star
132
Fork
167
Merge trung bình
1 ngày 23 giờ
Pull request đã merge (30 ngày)
110

Mô tả

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`._

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.