Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monad attacks #154

Merged
merged 35 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
65a472d
Start experimenting with attacks in an arbitraty monad
carlhammann Aug 26, 2022
8884a28
Try Attack = MockChainSt -> TxSkel -> Maybe (TxSkel, a)
carlhammann Aug 30, 2022
e285b24
Rewrite the dupTokenAttack as a combination of smaller attacks
carlhammann Aug 30, 2022
efc7eb9
Add tests for the modified dupTokenAttack
carlhammann Aug 31, 2022
396cf7b
Rewrite the datumHijackingAttack in the new language
carlhammann Aug 31, 2022
41244bd
Better rewriting of the datumHijackingAttack
carlhammann Sep 1, 2022
400c50b
Make attacks return lists, revert the LTL module to main
carlhammann Sep 1, 2022
0c545fe
Move 'somewhere' and 'everywhere' to MockChain.Monad.Staged, in order…
carlhammann Sep 1, 2022
22c747f
Tests for datumHijackingAttack; weird behaviour in mkSelectAttack
carlhammann Sep 1, 2022
095e22e
Found the culprit in mkSelectAttack, added regression test
carlhammann Sep 1, 2022
5b0d8f3
Redefine mkAttack and the datumTamperingAttack
carlhammann Sep 1, 2022
ed86ecb
Rewrite permutOutAttack and its tests
carlhammann Sep 1, 2022
23e19f7
Reintroduce non-attack tests, remove tests for '<>' on 'Attack's
carlhammann Sep 2, 2022
2f2797e
Status save on doubleSatAttack
carlhammann Sep 2, 2022
418c254
Make an extra module for attacks that add/remove constraints
carlhammann Sep 2, 2022
067a794
Rewrote the doubleSatAttack
carlhammann Sep 2, 2022
801e8ab
Some better comments at the double satisfaction attack
carlhammann Sep 5, 2022
6790089
Rewrite tests for doubleSatAttack
carlhammann Sep 5, 2022
a06d47e
Test the addConstraintsAttack
carlhammann Sep 5, 2022
1bc6e25
Adapt the examples to the new attacks
carlhammann Sep 5, 2022
048d77e
Some attacks to add and remove OutConstraints
carlhammann Sep 5, 2022
0e65615
Make cooked export everything
carlhammann Sep 5, 2022
d762ca2
Merge branch 'main' into monad-attacks
carlhammann Sep 5, 2022
44d7020
Address Facundo's and Lucas' comments, first half
carlhammann Oct 5, 2022
e3ab421
Rework the test for the doubleSatAttack
carlhammann Oct 5, 2022
f3389ac
Address more comments from Facundo an Mathieu
carlhammann Oct 6, 2022
faae62a
Make some tests more structured, as suggested by Facundo
carlhammann Oct 6, 2022
9038925
Merge branch 'main' into monad-attacks
carlhammann Oct 6, 2022
8f7193d
Incorporate suggestions from Georg
carlhammann Oct 7, 2022
37a2a60
Update cooked-validators/src/Cooked/Attack/AddConstraints.hs
carlhammann Oct 7, 2022
00301d1
Cleanup from the last commit
carlhammann Oct 7, 2022
2ea04ff
Repair 'sameConstraints', yet again
carlhammann Oct 10, 2022
a90c621
Merge branch 'main' into monad-attacks
carlhammann Oct 14, 2022
614d0ac
Rename Attack -> Tweak
carlhammann Oct 14, 2022
4d11868
Merge remote-tracking branch 'origin/monad-attacks' into monad-attacks
carlhammann Oct 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions cooked-validators/cooked-validators.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ library
exposed-modules:
Cooked
Cooked.Attack
Cooked.Attack.Common
Cooked.Attack.DatumHijacking
Cooked.Attack.DoubleSat
Cooked.Attack.DupToken
Cooked.Attack.OutPermutations
Cooked.Attack.TamperDatum
Cooked.Attack.Tweak
Cooked.Attack.Tweak.AddConstraints
Cooked.Attack.Tweak.Common
Cooked.Attack.Tweak.OutPermutations
Cooked.Attack.Tweak.TamperDatum
Cooked.Currencies
Cooked.Ltl
Cooked.MockChain
Expand Down Expand Up @@ -94,6 +96,7 @@ test-suite spec
main-is: Spec.hs
other-modules:
Cooked.AttackSpec
Cooked.AttackSpec.AddConstraints
Cooked.AttackSpec.Common
Cooked.AttackSpec.DatumHijacking
Cooked.AttackSpec.DoubleSat
Expand Down
2 changes: 2 additions & 0 deletions cooked-validators/src/Cooked.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
-- when writing large test-suites.
module Cooked (module X) where

import Cooked.Attack as X
import Cooked.Currencies as X
import Cooked.Ltl as X
import Cooked.MockChain as X
import Cooked.Tx.Constraints as X
92 changes: 5 additions & 87 deletions cooked-validators/src/Cooked/Attack.hs
Original file line number Diff line number Diff line change
@@ -1,88 +1,6 @@
module Cooked.Attack
( module Cooked.Attack.Common,
module Cooked.Attack.DatumHijacking,
module Cooked.Attack.DoubleSat,
module Cooked.Attack.DupToken,
module Cooked.Attack.OutPermutations,
module Cooked.Attack.TamperDatum,
)
where
module Cooked.Attack (module X) where
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still unsure that the single steps modifications should be called attacks. Why not

  • call the single steps modifications "Modifications" although there might be conflict there with the meaning of modifications in LTL
  • call actual aggregations of modifications toward a specific goal "Attacks"


import Cooked.Attack.Common
import Cooked.Attack.DatumHijacking
import Cooked.Attack.DoubleSat
import Cooked.Attack.DupToken
import Cooked.Attack.OutPermutations
import Cooked.Attack.TamperDatum

-- The idea of this module: Turning optics into attacks
-------------------------------------------------------
--
-- In cooked-validators, a _single transaction attack_ on a smart contract is a
-- function that modifies one transaction: An attacker applies this function to
-- a transaction in an otherwise normal chain of transactions, somehow fools the
-- validator script(s), and profits. So, attacks in cooked-valiadators should
-- modify 'TxSkel's.
--
-- It would be nice to have a collection of parametric attacks, together with an
-- easy way to add new attacks to that collection. This module and its
-- submodules contain the beginnings of such a collection, and also a hopefully
-- useful mechanisms to extend it.
--
-- Since most attacks are of the form "look deep into the nested data types
-- within a 'TxSkel' and change something", The idea is to use the optics
-- defined in "Cooked.Tx.Constraints.Optics" and turn them into attacks.
--
-- Optic attack example: Token duplication
------------------------------------------
--
-- There's a 'dupTokenAttack' in "Cooked.Attack.DupToken", and its
-- implementation is quite readable; the purpose of the following is not to
-- explain that function in detail, but to motivate how one should write new
-- attacks using this module.
--
-- A _token duplication_ attack consists in trying to increase the amount of
-- tokens a transaction mints and paying the surplus to an attacker. A naive
-- idea to implement the token duplication attack would then be
--
-- > naiveDupTokenAttack :: Wallet -> (Value -> Value) -> TxSkel -> TxSkel
-- > naiveDupTokenAttack attacker increaseValue =
-- > paySurplusTo attacker . over (mintsConstraintT % valueL) increaseValue
--
-- where @paySurplusTo :: Wallet -> TxSkel -> TxSkel@ is a suitable function
-- that modifies a 'TxSkel' to play any extra minted tokens to a given
-- wallet.
--
-- This is idea is almost right, save for the type of @naiveDupTokenAttack@: If
-- @increaseValue@ did not in fact _change_ any of the minted values, or if
-- there were no 'MintsConstraint's in the transaction under consideration, we
-- have no way to detect this failure. Also, depending on the current state of
-- the 'MockChain' there might be more than one possibly interesting way to
-- modify the initial transaction. That's why we set
--
-- > type Attack = MockChainSt -> TxSkel -> [TxSkel]
--
-- and the module "Cooked.Attack.Common" provides functions like
--
-- > mkAttack :: Is k A_Traversal => Optic' k is TxSkel a -> (a -> Maybe a) -> Attack
--
-- which is a kind of "'over' with failure": The list of modified 'TxSkel's
-- returned by @mkAttack optic f state skel@ will be nonempty if and only if
--
-- - the @optic@ being used in the attack has at least one focus on the input
-- @skel@, and
--
-- - the function @f@ returns @Just@ on at least one of the foci.
--
-- In that case, the returned list will contain exactly one modified 'TxSkel',
-- with all foci modified by @f@.
--
-- There are also other functions like 'mkAttack' that return more than one
-- modification in "Cooked.Attack.Common".
--
-- Note that we can use 'mkAttack' (or a similar fuction) to write
-- 'dupTokenAttack' in a contract-agnostic manner. Also note that, despite
-- (because?) of the generality of such attacks, they are relatively easy to
-- implement. Our growing collection of optics in "Cooked.Tx.Constraints.Optics"
-- and other helpers will hopefully mean that it will become easier and easier
-- to write attacks.
import Cooked.Attack.DatumHijacking as X
import Cooked.Attack.DoubleSat as X
import Cooked.Attack.DupToken as X
import Cooked.Attack.Tweak as X
Loading