fix(settlement): add cross-contract reentrancy guard (#8)#42
Merged
elizabetheonoja-art merged 1 commit intoJun 25, 2026
Merged
Conversation
) Settlement entry points yield control to untrusted contracts at the cross-contract call boundary (oracle get_price_value, token transfer via collect_fee and the net transfer). A malicious token or oracle could re-enter settle/finalize_settlement before the first invocation completes and manipulate intermediate state. - Add DataKey::ReentrancyLock in storage.rs (temporary storage) - Add ReentrancyGuard RAII mutex in reentrancy.rs: acquire-before-call, release on Drop; strict (any re-entry panics with ReentrantCall). Panic path is covered by the host's storage revert (wasm uses panic=abort, so Drop does not run on unwind). - Acquire the guard at the top of settle() and finalize_settlement() - Add SettlementError::ReentrantCall - Tests: malicious-token reentry via settle and via finalize_settlement are both rejected; legitimate single settle still succeeds
elizabetheonoja-art
approved these changes
Jun 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix(settlement): cross-contract reentrancy guard
Closes #8
Solution
A strict RAII reentrancy mutex acquired before any external call and released on
scope exit.
storage.rs— newDataKey::ReentrancyLock, held in temporary storage.reentrancy.rs—ReentrancyGuard:new(&env)panics withSettlementError::ReentrantCallif the lock is alreadyheld, otherwise sets it (TTL 1 ledger).
Dropclears the lock on normal return.automatically — necessary because Wasm builds use
panic = "abort", whereDropdoes not run during unwinding.lib.rs—let _guard = ReentrancyGuard::new(&env);at the top ofsettleand
finalize_settlement; addedSettlementError::ReentrantCall = 4.Invariant: while any external call is in flight, the lock is set; any re-entry
into a guarded entry point aborts the transaction.
Files
contracts/settlement/src/storage.rs(new)contracts/settlement/src/reentrancy.rs(new)contracts/settlement/src/lib.rs(guards + error variant)contracts/settlement/src/test.rs(attack + regression tests)