github.com/tacshi/go-ethereum@v0.0.0-20230616113857-84a434e20921/docs/postmortems/2021-08-22-split-postmortem.md (about)

     1  # Minority split 2021-08-27 post mortem
     2  
     3  This is a post-mortem concerning the minority split that occurred on Ethereum mainnet on block [13107518](https://etherscan.io/block/13107518), at which a minority chain split occurred.
     4  
     5  ## Timeline
     6  
     7  - 2021-08-17: Guido Vranken submitted a bounty report. Investigation started, root cause identified, patch variations discussed.
     8  - 2021-08-18: Made public announcement over twitter about upcoming security release upcoming Tuesday. Downstream projects were also notified about the upcoming patch-release.
     9  - 2021-08-24: Released [v1.10.8](https://github.com/tacshi/go-ethereum/releases/tag/v1.10.8) containing the fix on Tuesday morning (CET). Erigon released [v2021.08.04](https://github.com/ledgerwatch/erigon/releases/tag/v2021.08.04).
    10  - 2021-08-27: At 12:50:07 UTC, issue exploited. Analysis started roughly 30m later,
    11  
    12  ## Bounty report
    13  
    14  ### 2021-08-17 RETURNDATA corruption via datacopy
    15  
    16  On 2021-08-17, Guido Vranken submitted a report to bounty@ethereum.org. This coincided with a geth-meetup in Berlin, so the geth team could fairly quickly analyse the issue.
    17  
    18  He submitted a proof of concept which called the `dataCopy` precompile, where the input slice and output slice were overlapping but shifted. Doing a `copy` where the `src` and `dest` overlaps is not a problem in itself, however, the `returnData`slice was _also_ using the same memory as a backing-array.
    19  
    20  #### Technical details
    21  
    22  During CALL-variants, `geth` does not copy the input. This was changed at one point, to avoid a DoS attack reported by Hubert Ritzdorf, to avoid copying data a lot on repeated `CALL`s -- essentially combating a DoS via `malloc`. Further, the datacopy precompile also does not copy the data, but just returns the same slice. This is fine so far.
    23  
    24  After the execution of `dataCopy`, we copy the `ret` into the designated memory area, and this is what causes a problem. Because we're copying a slice of memory over a slice of memory, and this operation modifies (shifts) the data in the source -- the `ret`. So this means we wind up with corrupted returndata.
    25  
    26  ```
    27  1. Calling datacopy
    28  
    29    memory: [0, 1, 2, 3, 4]
    30    in (mem[0:4]) : [0,1,2,3]
    31    out (mem[1:5]): [1,2,3,4]
    32  
    33  2. dataCopy returns
    34  
    35    returndata (==in, mem[0:4]): [0,1,2,3]
    36  
    37  3. Copy in -> out
    38  
    39    => memory: [0,0,1,2,3]
    40    => returndata: [0,0,1,2]
    41  ```
    42  
    43  #### Summary
    44  
    45  A memory-corruption bug within the EVM can cause a consensus error, where vulnerable nodes obtain a different `stateRoot` when processing a maliciously crafted transaction. This, in turn, would lead to the chain being split: mainnet splitting in two forks.
    46  
    47  #### Handling
    48  
    49  On the evening of 17th, we discussed options on how to handle it. We made a state test to reproduce the issue, and verified that neither `openethereum`, `nethermind` nor `besu` were affected by the same vulnerability, and started a full-sync with a patched version of `geth`.
    50  
    51  It was decided that in this specific instance, it would be possible to make a public announcement and a patch release:
    52  
    53  - The fix can be made pretty 'generically', e.g. always copying data on input to precompiles.
    54  - The flaw is pretty difficult to find, given a generic fix in the call. The attacker needs to figure out that it concerns the precompiles, specifically the datcopy, and that it concerns the `RETURNDATA` buffer rather than the regular memory, and lastly the special circumstances to trigger it (overlapping but shifted input/output).
    55  
    56  Since we had merged the removal of `ETH65`, if the entire network were to upgrade, then nodes which have not yet implemented `ETH66` would be cut off from the network. After further discussions, we decided to:
    57  
    58  - Announce an upcoming security release on Tuesday (August 24th), via Twitter and official channels, plus reach out to downstream projects.
    59  - Temporarily revert the `ETH65`-removal.
    60  - Place the fix into the PR optimizing the jumpdest analysis [233381](https://github.com/tacshi/go-ethereum/pull/23381).
    61  - After 4-8 weeks, release details about the vulnerability.
    62  
    63  ## Exploit
    64  
    65  At block [13107518](https://etherscan.io/block/13107518), mined at Aug-27-2021 12:50:07 PM +UTC, a minority chain split occurred. The discord user @AlexSSD7 notified the allcoredevs-channel on the Eth R&D discord, on Aug 27 13:09 UTC.
    66  
    67  At 14:09 UTC, it was confirmed that the transaction `0x1cb6fb36633d270edefc04d048145b4298e67b8aa82a9e5ec4aa1435dd770ce4` had triggered the bug, leading to a minority-split of the chain. The term 'minority split' means that the majority of miners continued to mine on the correct chain.
    68  
    69  At 14:17 UTC, @mhswende tweeted out about the issue [2].
    70  
    71  The attack was sent from an account funded from Tornado cash.
    72  
    73  It was also found that the same attack had been carried out on the BSC chain at roughly the same time -- at a block mined [12 minutes earlier](https://bscscan.com/tx/0xf667f820631f6adbd04a4c92274374034a3e41fa9057dc42cb4e787535136dce), at Aug-27-2021 12:38:30 PM +UTC.
    74  
    75  The blocks on the 'bad' chain were investigated, and Tim Beiko reached out to those mining operators on the minority chain who could be identified via block extradata.
    76  
    77  ## Lessons learned
    78  
    79  ### Disclosure decision
    80  
    81  The geth-team have an official policy regarding [vulnerability disclosure](https://geth.ethereum.org/docs/vulnerabilities/vulnerabilities).
    82  
    83  > The primary goal for the Geth team is the health of the Ethereum network as a whole, and the decision whether or not to publish details about a serious vulnerability boils down to minimizing the risk and/or impact of discovery and exploitation.
    84  
    85  In this case, it was decided that public pre-announce + patch would likely lead to sufficient update-window for a critical mass of nodes/miners to upgrade in time before it could be exploited. In hindsight, this was a dangerous decision, and it's unlikely that the same decision would be reached were a similar incident to happen again.
    86  
    87  ### Disclosure path
    88  
    89  Several subprojects were informed about the upcoming security patch:
    90  
    91  - Polygon/Matic
    92  - MEV
    93  - Avalanche
    94  - Erigon
    95  - BSC
    96  - EWF
    97  - Quorum
    98  - ETC
    99  - xDAI
   100  
   101  However, some were 'lost', and only notified later
   102  
   103  - Optimism
   104  - Summa
   105  - Harmony
   106  
   107  Action point: create a low-volume geth-announce@ethereum.org email list where dependent projects/operators can receive public announcements.
   108  
   109  - This has been done. If you wish to receive release- and security announcements, sign up [here](https://groups.google.com/a/ethereum.org/g/geth-announce/about)
   110  
   111  ### Fork monitoring
   112  
   113  The fork monitor behaved 'ok' during the incident, but had to be restarted during the evening.
   114  
   115  Action point: improve the resiliency of the forkmon, which is currently not performing great when many nodes are connected.
   116  
   117  Action point: enable push-based alerts to be sent from the forkmon, to speed up the fork detection.
   118  
   119  ## Links
   120  
   121  - [1] https://twitter.com/go_ethereum/status/1428051458763763721
   122  - [2] https://twitter.com/mhswende/status/1431259601530458112
   123  
   124  ## Appendix
   125  
   126  ### Subprojects
   127  
   128  The projects were sent variations of the following text:
   129  
   130  ```
   131  We have identified a security issue with go-ethereum, and will issue a
   132  new release (v1.10.8) on Tuesday next week.
   133  
   134  At this point, we will not disclose details about the issue, but
   135  recommend downstream/dependent projects to be ready to take actions to
   136  upgrade to the latest go-ethereum codebase. More information about the
   137  issue will be disclosed at a later date.
   138  
   139  https://twitter.com/go_ethereum/status/1428051458763763721
   140  
   141  ```
   142  
   143  ### Patch
   144  
   145  ```diff
   146  diff --git a/core/vm/instructions.go b/core/vm/instructions.go
   147  index f7ef2f900e..6c8c6e6e6f 100644
   148  --- a/core/vm/instructions.go
   149  +++ b/core/vm/instructions.go
   150  @@ -669,6 +669,7 @@ func opCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byt
   151          }
   152          stack.push(&temp)
   153          if err == nil || err == ErrExecutionReverted {
   154  +               ret = common.CopyBytes(ret)
   155                  scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   156          }
   157          scope.Contract.Gas += returnGas
   158  @@ -703,6 +704,7 @@ func opCallCode(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([
   159          }
   160          stack.push(&temp)
   161          if err == nil || err == ErrExecutionReverted {
   162  +               ret = common.CopyBytes(ret)
   163                  scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   164          }
   165          scope.Contract.Gas += returnGas
   166  @@ -730,6 +732,7 @@ func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext
   167          }
   168          stack.push(&temp)
   169          if err == nil || err == ErrExecutionReverted {
   170  +               ret = common.CopyBytes(ret)
   171                  scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   172          }
   173          scope.Contract.Gas += returnGas
   174  @@ -757,6 +760,7 @@ func opStaticCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext)
   175          }
   176          stack.push(&temp)
   177          if err == nil || err == ErrExecutionReverted {
   178  +               ret = common.CopyBytes(ret)
   179                  scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
   180          }
   181          scope.Contract.Gas += returnGas
   182  diff --git a/core/vm/interpreter.go b/core/vm/interpreter.go
   183  index 9cf0c4e2c1..9fb83799c9 100644
   184  --- a/core/vm/interpreter.go
   185  +++ b/core/vm/interpreter.go
   186  @@ -262,7 +262,7 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
   187                  // if the operation clears the return data (e.g. it has returning data)
   188                  // set the last return to the result of the operation.
   189                  if operation.returns {
   190  -                       in.returnData = common.CopyBytes(res)
   191  +                       in.returnData = res
   192                  }
   193  
   194                  switch {
   195  ```
   196  
   197  ### Statetest to test for the issue
   198  
   199  ```json
   200  {
   201    "trigger-issue": {
   202      "env": {
   203        "currentCoinbase": "b94f5374fce5edbc8e2a8697c15331677e6ebf0b",
   204        "currentDifficulty": "0x20000",
   205        "currentGasLimit": "0x26e1f476fe1e22",
   206        "currentNumber": "0x1",
   207        "currentTimestamp": "0x3e8",
   208        "previousHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
   209      },
   210      "pre": {
   211        "0x00000000000000000000000000000000000000bb": {
   212          "code": "0x6001600053600260015360036002536004600353600560045360066005536006600260066000600060047f7ef0367e633852132a0ebbf70eb714015dd44bc82e1e55a96ef1389c999c1bcaf13d600060003e596000208055",
   213          "storage": {},
   214          "balance": "0x5",
   215          "nonce": "0x0"
   216        },
   217        "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b": {
   218          "code": "0x",
   219          "storage": {},
   220          "balance": "0xffffffff",
   221          "nonce": "0x0"
   222        }
   223      },
   224      "transaction": {
   225        "gasPrice": "0x1",
   226        "nonce": "0x0",
   227        "to": "0x00000000000000000000000000000000000000bb",
   228        "data": ["0x"],
   229        "gasLimit": ["0x7a1200"],
   230        "value": ["0x01"],
   231        "secretKey": "0x45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8"
   232      },
   233      "out": "0x",
   234      "post": {
   235        "Berlin": [
   236          {
   237            "hash": "2a38a040bab1e1fa499253d98b2fd363e5756ecc52db47dd59af7116c068368c",
   238            "logs": "1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
   239            "indexes": {
   240              "data": 0,
   241              "gas": 0,
   242              "value": 0
   243            }
   244          }
   245        ]
   246      }
   247    }
   248  }
   249  ```