Skip to content
DoS Audit finding Intermediate

lastUpdatedDay not initialized

The variable lastUpdatedDay in IncentiveDistribution.sol is not initialised, so when updateDayTotals is called it computes a huge day difference and enters a very long loop that runs out of gas. Even if the loop finished, the daily distribution variable would be updated incorrectly, making the incentive distribution inaccurate or non-functional.

Marginswap

Originally disclosed on Code4rena

The variable lastUpdatedDay in IncentiveDistribution.sol is not (properly) initialized. This means the function updateDayTotals will end up in a very large loop which will lead to an out of gas error. Even if the loop would end, the variable currentDailyDistribution would be updated very often. Thus updateDayTotals cannot be performed.

The entire IncentiveDistribution does not work. If the loop would stop, the variable currentDailyDistribution is not accurate, resulting in a far lower incentive distribution than expected.

Recommend initializing lastUpdatedDay with something like block.timestamp / (1 days)

uint256 lastUpdatedDay; # ==> lastUpdatedDay = 0

# When the function updateDayTotals is called:
uint256 public nowDay = block.timestamp / (1 days); #==> ~ 18721
uint256 dayDiff = nowDay - lastUpdatedDay; #==> 18721-0 = 18721

for (uint256 i = 0; i < dayDiff; i++) { # very long loop (18721)
currentDailyDistribution = ....
}
#will result in an out of gas error

## [[H-10] function buyBond charges msg.sender twice](https://github.com/code-423n4/2021-04-marginswap-findings/issues/38)

function buyBond transfers amount from msg.sender twice:
Fund(fund()).depositFor(msg.sender, issuer, amount);
...
collectToken(issuer, msg.sender, amount);

The original report as published on Code4rena. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the state variable lastUpdatedDay is declared but never set to the current day, so it defaults to zero and causes an enormous loop when calculating the day difference.

Impact

The entire IncentiveDistribution does not work, and even if the loop stopped the currentDailyDistribution would be inaccurate, resulting in a far lower incentive distribution than expected.

What the attacker needed

None

The fix

Initialise lastUpdatedDay with a value such as block.timestamp divided by one day.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A smart contract has a counter that tracks the last day it updated. Because that counter starts at zero instead of the current day, the contract tries to catch up on thousands of days at once. That uses too much processing power (gas) and the update fails, so the incentive system stops working.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More from Marginswap

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.