Skip to content

Internal _withdraw, reading from storage twice. #26

Open

Description

@code423n4

Handle

evertkors

Vulnerability details

Impact

userAmountStaked[token][msg.sender] is read from storage twice on Staker.sol:925

Recommended Mitigation Steps

uint256 amountUser = userAmountStaked[token][msg.sender]
require(amountUser > 0, "nothing to withdraw")
_mintAccumulatedFloat(marketIndex, msg.sender);
userAmountStaked[token][msg.sender] = amountUser - amount

Activity

  1. added a commit that references this issue on Aug 8, 2021
  2. JasoonS commented on Aug 11, 2021

    @JasoonS
    Collaborator

    Unfortunately this is still needed. _mintAccumulatedFloatAndExecuteOutstandingShifts(marketIndex, msg.sender); modifies the userAmountStaked[token][msg.sender], so you do need to read that value again.

    See below image:
    image

  3. JasoonS commented on Aug 13, 2021

    @JasoonS
    Collaborator

    Verdict: given the code this optimization introduces a bug. Final code indeed only reads userAmountStaked once, but that is due to other optimizations.

  4. added
    duplicateThis issue or pull request already exists
    on Aug 13, 2021
  5. JasoonS commented on Aug 13, 2021

    @JasoonS
    Collaborator

    Not an exact duplicate of #25, #24 and #18 (different variable - but same optimization suggested -closing)

  6. 0xean commented on Aug 25, 2021

    @0xean
    Collaborator

    I am not sure where the divergence in code is here but the line that was reference by the warden is

    require(userAmountStaked[token][msg.sender] > 0, "nothing to withdraw");
        _mintAccumulatedFloat(marketIndex, msg.sender);
    
        userAmountStaked[token][msg.sender] = userAmountStaked[token][msg.sender] - amount;
    

    where the _mintAccumulatedFloat does not modify the userAmountStaked. Given this information, I am reopening the issue.

  7. reopened this on Aug 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions