Skip to content
This repository was archived by the owner on Dec 2, 2024. It is now read-only.

ContractModel: Detect Double Satisfaction Problems #501

Merged
merged 7 commits into from
Jun 22, 2022

Conversation

MaximilianAlgehed
Copy link
Contributor

@MaximilianAlgehed MaximilianAlgehed commented Jun 7, 2022

This PR introduces a new property to check for double satisfaction vulnerabilities in any contract with a ContractModel. This property is not complete, but as far as we know it is sound. See the checkDoubleSatisfaction property in Plutus.Contract.Test.ContractModel.

Pre-submit checklist:

  • Branch
    • Tests are provided (if possible)
    • Commit sequence broadly makes sense
    • Key commits have useful messages
    • Formatting, PNG optimization, etc. are updated
  • PR
    • Self-reviewed the diff
    • Useful pull request description
    • Reviewer requested

@MaximilianAlgehed MaximilianAlgehed marked this pull request as draft June 7, 2022 12:31
@MaximilianAlgehed MaximilianAlgehed force-pushed the double-satisfaction branch 2 times, most recently from 290dac6 to 280d271 Compare June 7, 2022 13:17
@MaximilianAlgehed
Copy link
Contributor Author

@sjoerdvisscher we didn't touch this code, is something broken on main?

@MaximilianAlgehed MaximilianAlgehed marked this pull request as ready for review June 7, 2022 14:07
@locallycompact
Copy link
Contributor

What is a double satisfaction problem?

@ThomasArts
Copy link

@sjoerdvisscher
Copy link
Contributor

@MaximilianAlgehed Hmm, it looks like a legitimate failure, and I don't see it on other branches. Did you try to run cabal test plutus-pab-test-full-long-running locally?

@MaximilianAlgehed
Copy link
Contributor Author

... It's a cached failure?!

@sjoerdvisscher
Copy link
Contributor

As far as I can tell it is cached from a previous build of this PR.

@MaximilianAlgehed
Copy link
Contributor Author

This indeed fails locally but I'm absolutely stumped as to why - we haven't touched anything related to the PAB?

@MaximilianAlgehed
Copy link
Contributor Author

@sjoerdvisscher there is a change to Ledger.Index here! But that's because the semantics of the old code are, as far as I can tell, wrong! The semantics of the old code is that you always pay the collateral, when the semantics should be that you only pay if phase 2 validation fails, no?

@sjoerdvisscher
Copy link
Contributor

the semantics should be that you only pay if phase 2 validation fails, no?

Yes, that's definitely how it's supposed to work.

@MaximilianAlgehed
Copy link
Contributor Author

MaximilianAlgehed commented Jun 9, 2022 via email

@MaximilianAlgehed
Copy link
Contributor Author

Then the fact that this test is broken tells us something. Is something broken in the PAB or is something broken elsewhere?

@MaximilianAlgehed
Copy link
Contributor Author

@sjoerdvisscher I'll make a separate PR with that fix and hopefully that means we can dig into the issue in the PAB separately.

@MaximilianAlgehed
Copy link
Contributor Author

Also, @sjoerdvisscher it would be good if you or someone could just have a look over this to make sure there is nothing absolutely insane going on :)

@sjoerdvisscher
Copy link
Contributor

So then the old version was buggy (because of how do and infix notation interact).

😱

it would be good if you or someone could just have a look over this to make sure there is nothing absolutely insane going on :)

As a comment in the code says -- Double satisfaction magic, and as with any magic it is hard to tell if something insane is going on or not! To be honest this is true for most of the ContractModel code. In can review it for code quality (and this is already quite a task since most PRs are quite big) but I can't review the logic.

Don't forget to rebase on main to get the fix for the OOM CI issue.

@UlfNorell UlfNorell force-pushed the double-satisfaction branch from a7051e1 to 0823db7 Compare June 20, 2022 13:16
@UlfNorell
Copy link
Contributor

@sjoerdvisscher: 43b73ac changed runValidation to no longer compute an updated UtxoIndex. What's the best way to compute that now? We need to keep an up-to-date UtxoIndex when traversing an emulator trace looking for double satisfaction vulnerabilities.

@sjoerdvisscher
Copy link
Contributor

Ah sorry, I keep forgetting that we're not the only ones using these functions. You need something like this:

        idx' = case e of
            Just (Index.Phase1, _) -> idx
            Just (Index.Phase2, _) -> Index.insertCollateral txn idx
            Nothing                -> Index.insert txn idx

@UlfNorell
Copy link
Contributor

Does that mean we should to switch to using CardanoTxs instead of the emulator Tx? Currently we're expecting to find an emulator Tx in TxnValidate events, but that will now be a CardanoTx?

@sjoerdvisscher
Copy link
Contributor

That's right.

@UlfNorell UlfNorell force-pushed the double-satisfaction branch from 0823db7 to 570a9d4 Compare June 22, 2022 09:40
@sjoerdvisscher sjoerdvisscher merged commit 29c2c5b into IntersectMBO:main Jun 22, 2022
@UlfNorell UlfNorell deleted the double-satisfaction branch June 22, 2022 12:01
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants