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

L2Sender reinit fix #8797

Merged
merged 7 commits into from
Jan 5, 2024
Merged

Conversation

maurelian
Copy link
Contributor

@maurelian maurelian commented Jan 2, 2024

Description

Fixes an issue which would allow for a single messages to be replayed twice by intercepting an upgrade.

This is addressed in two ways:

  • Do not over the xDomainMsgSender if it is not the default value.
  • Added another check that successfulMessages for the message is still false after the
    external call.

Exploit Description

To perform the exploit, the following steps must be taken by the attacker:

  1. Wait for a signed upgrade transaction.
  2. Once the signed upgrade transaction is available, front-run by running it inside a
    withdrawal transaction.
  3. The attacker's withdrawal payload will call their own contract which would run the
    upgrade transaction and then re-enter relayMessage() with their own same
    withdrawal message.
  4. The reentrancy will succeed since xDomainMsgSender has been reset by the upgrade.

The attacker must also satisfy the following with their withdrawal message:

  1. It must be a failed withdrawal so they can re-enter relayMessage(). A fresh withdrawal
    cannot reenter as it checks failedMessages[versionedHash] is true.
  2. It must have a value set. The amount for value is the amount that they will able to
    drain from the contract.

Copy link
Contributor

semgrep-app bot commented Jan 2, 2024

Semgrep found 1 writable-filesystem-service finding:

  • indexer/docker-compose.yml: L120

Service 'backend-goerli' is running with a writable root filesystem. This may allow malicious applications to download and run additional payloads, or modify container files. If an application inside a container has to save something temporarily consider using a tmpfs. Add 'read_only: true' to this service to prevent this.

Ignore this finding from writable-filesystem-service.

Semgrep found 1 no-new-privileges finding:

  • indexer/docker-compose.yml: L120

Service 'backend-goerli' allows for privilege escalation via setuid or setgid binaries. Add 'no-new-privileges:true' in 'security_opt' to prevent this.

Ignore this finding from no-new-privileges.

Copy link

codecov bot commented Jan 2, 2024

Codecov Report

Attention: 5 lines in your changes are missing coverage. Please review.

Comparison is base (8b197bc) 34.52% compared to head (3f9d045) 25.67%.
Report is 95 commits behind head on develop.

❗ Current head 3f9d045 differs from pull request most recent head f80e4b9. Consider uploading reports for the commit f80e4b9 to get more accurate results

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8797      +/-   ##
===========================================
- Coverage    34.52%   25.67%   -8.86%     
===========================================
  Files          167      119      -48     
  Lines         7171     4892    -2279     
  Branches      1214     1066     -148     
===========================================
- Hits          2476     1256    -1220     
+ Misses        4545     3530    -1015     
+ Partials       150      106      -44     
Flag Coverage Δ
cannon-go-tests ?
chain-mon-tests 27.14% <ø> (ø)
common-ts-tests ?
contracts-bedrock-tests 20.20% <0.00%> (+0.11%) ⬆️
contracts-ts-tests 12.25% <ø> (ø)
core-utils-tests ?
sdk-next-tests 42.18% <ø> (ø)
sdk-tests 42.18% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
...ontracts-bedrock/src/L1/L1CrossDomainMessenger.sol 0.00% <ø> (ø)
...ontracts-bedrock/src/L2/L2CrossDomainMessenger.sol 0.00% <ø> (ø)
...ckages/contracts-bedrock/src/L1/OptimismPortal.sol 0.00% <0.00%> (ø)
...cts-bedrock/src/universal/CrossDomainMessenger.sol 0.00% <0.00%> (ø)

... and 51 files with indirect coverage changes

@tynes
Copy link
Contributor

tynes commented Jan 4, 2024

Do we also need the same fix in the optimism portal?

@maurelian
Copy link
Contributor Author

Do we also need the same fix in the optimism portal?

It's not critical, because we write the withdraw to the mapping before sending, but we should make the fix anyways.

// Mark the withdrawal as finalized so it can't be replayed.
finalizedWithdrawals[withdrawalHash] = true;

I will add that to this PR.

@maurelian maurelian force-pushed the jm/fix-l2sender-reinit branch from 18097d2 to 4580aa7 Compare January 4, 2024 20:13
Copy link
Contributor

semgrep-app bot commented Jan 4, 2024

Semgrep found 7 sol-style-notice-over-dev-natspec findings:

  • packages/contracts-bedrock/src/dispute/interfaces/IOutputBisectionGame.sol: L57, L38, L12
  • packages/contracts-bedrock/src/dispute/interfaces/IBondManager.sol: L48, L41, L36, L28

Prefer @notice over @dev in natspec comments

Ignore this finding from sol-style-notice-over-dev-natspec.

Semgrep found 1 no-printf-in-responsewriter finding:

  • op-exporter/main.go: L83

Detected 'printf' or similar in 'http.ResponseWriter.write()'. This bypasses HTML escaping that prevents cross-site scripting vulnerabilities. Instead, use the 'html/template' package to render data to users.

Ignore this finding from no-printf-in-responsewriter.

Semgrep found 1 no-direct-write-to-responsewriter finding:

  • op-exporter/main.go: L83

Detected directly writing or similar in 'http.ResponseWriter.write()'. This bypasses HTML escaping that prevents cross-site scripting vulnerabilities. Instead, use the 'html/template' package and render data using 'template.Execute()'.

Ignore this finding from no-direct-write-to-responsewriter.

Semgrep found 1 use-tls finding:

  • op-exporter/main.go: L138

Found an HTTP server without TLS. Use 'http.ListenAndServeTLS' instead. See https://golang.org/pkg/net/http/#ListenAndServeTLS for more information.

Ignore this finding from use-tls.

Semgrep found 1 context-todo finding:

  • op-exporter/main.go: L156

Consider to use well-defined context

Ignore this finding from context-todo.

Semgrep found 1 use-fprintf-not-write-fsprint finding:

  • op-exporter/main.go: L83

use fmt.Fprintf(w, { "healthy": "%t", "version": "%s" }, health.healthy, *health.version) instead of fmt.Sprintf and []byte conversion

Ignore this finding from use-fprintf-not-write-fsprint.

@maurelian maurelian marked this pull request as ready for review January 4, 2024 20:35
@maurelian maurelian requested a review from a team as a code owner January 4, 2024 20:35
@maurelian maurelian requested review from Inphi and tynes and removed request for Inphi January 4, 2024 20:35
@maurelian maurelian enabled auto-merge January 4, 2024 20:36
@maurelian maurelian force-pushed the jm/fix-l2sender-reinit branch 2 times, most recently from 3f8faa9 to 3f9d045 Compare January 4, 2024 20:41
Copy link
Contributor

coderabbitai bot commented Jan 4, 2024

Warning

Rate Limit Exceeded

@maurelian has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 4 minutes and 56 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.

Commits Files that changed from the base of the PR and between 19c79fe and f80e4b9.

Walkthrough

The recent updates to the contracts involve incrementing semantic version constants, enhancing initialization checks, and adding reentrancy protection. The L1CrossDomainMessenger and L2CrossDomainMessenger contracts have their versions updated, reflecting new features or fixes. The OptimismPortal contract introduces a default sender initialization. The universal CrossDomainMessenger contract adds reentrancy guards. Test contracts are updated to include checks for reentrancy vulnerabilities during initialization and message relaying.

Changes

File Path Change Summary
.../L1/L1CrossDomainMessenger.sol
.../L2/L2CrossDomainMessenger.sol
Updated semantic version constants.
.../L1/OptimismPortal.sol Version bumped from "2.3.0" to "2.4.0"; added default l2Sender initialization.
.../universal/CrossDomainMessenger.sol Enhanced reentrancy protection and conditional xDomainMsgSender initialization.
.../test/L1/L1CrossDomainMessenger.t.sol Added tests for reentrancy vulnerability.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit's AI:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid.
    • @coderabbitai read the files in the src/scheduler package and generate README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@maurelian maurelian force-pushed the jm/fix-l2sender-reinit branch from 3f9d045 to f80e4b9 Compare January 5, 2024 19:35
Copy link
Contributor

semgrep-app bot commented Jan 5, 2024

Semgrep found 1 sol-style-notice-over-dev-natspec finding:

  • packages/contracts-bedrock/src/dispute/interfaces/IFaultDisputeGame.sol: L12

Prefer @notice over @dev in natspec comments

Ignore this finding from sol-style-notice-over-dev-natspec.

@maurelian maurelian changed the base branch from develop to jm/reinit-attack-demo January 5, 2024 19:37
@maurelian maurelian merged commit 478ba42 into jm/reinit-attack-demo Jan 5, 2024
17 of 26 checks passed
@maurelian maurelian deleted the jm/fix-l2sender-reinit branch January 5, 2024 19:37
@maurelian maurelian restored the jm/fix-l2sender-reinit branch January 5, 2024 19:39
@maurelian maurelian mentioned this pull request Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants