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

Internal _withdraw, reading from storage twice. #26

Open
code423n4 opened this issue Aug 8, 2021 · 4 comments
Open

Internal _withdraw, reading from storage twice. #26

code423n4 opened this issue Aug 8, 2021 · 4 comments
Labels
bug Something isn't working duplicate This issue or pull request already exists G (Gas Optimization) sponsor disputed

Comments

@code423n4
Copy link
Contributor

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

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Aug 8, 2021
code423n4 added a commit that referenced this issue Aug 8, 2021
@JasoonS
Copy link
Collaborator

JasoonS commented Aug 11, 2021

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

@JasoonS
Copy link
Collaborator

JasoonS commented Aug 13, 2021

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

@JasoonS JasoonS added the duplicate This issue or pull request already exists label Aug 13, 2021
@JasoonS
Copy link
Collaborator

JasoonS commented Aug 13, 2021

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

@JasoonS JasoonS closed this as completed Aug 13, 2021
@0xean
Copy link
Collaborator

0xean commented Aug 25, 2021

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.

@0xean 0xean reopened this Aug 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working duplicate This issue or pull request already exists G (Gas Optimization) sponsor disputed
Projects
None yet
Development

No branches or pull requests

3 participants