Skip to content
Smart contracts Audit finding Intermediate

The check for integer overflow in the function staker.go#checkStake() is incorrect

The checkStake function in the staker code adds three unsigned 64-bit values and tries to detect overflow, but it fails to check the carry flag after the first addition. An overflow in that first addition could go unnoticed and the function could return a wrong total, although the report says all inputs are bounded so this cannot happen in current code paths.

LeoFlint Follow

Originally disclosed on Immunefi

Informational View original
  • Disclosed after under an hour

56761 bc insight the check for integer overflow in the function staker go checkstake is incorrect

Submitted on Oct 20th 2025 at 13:10:36 UTC by @LeoFlint for Attackathon | VeChain Hayabusa Upgrade

Description

The function builtin/staker/staker.go#checkStake() adds three uint64 values and attempts to check for integer overflow. The current implementation misses verifying the carry after the first addition, which can let an overflow go undetected and cause the function to return an incorrect result.

Details

Current code:

Go
func checkStake(valNextPeriodTVL, aggNextPeriodTVL, amount uint64) (uint64, error) {
	total1, carry := bits.Add64(valNextPeriodTVL, aggNextPeriodTVL, 0)
	//@audit no check carry
	total2, carry := bits.Add64(total1, amount, carry)
	if carry != 0 {
		return 0, NewReverts("stake is out of range")
	}
	return total2, nil
}

Because the code does not check carry immediately after the first addition, an overflow occurring there can be missed and the function can proceed incorrectly.

Impact

All values passed to checkStake() were bounded before and overflow should never happen. This makes the issue informational in the current code paths, but the check itself is incorrect and should be fixed to be robust.

Recommendation

Check the carry after the first addition as well. For example:

Go
func checkStake(valNextPeriodTVL, aggNextPeriodTVL, amount uint64) (uint64, error) {
	total1, carry := bits.Add64(valNextPeriodTVL, aggNextPeriodTVL, 0)
	if carry != 0 {
		return 0, NewReverts("stake is out of range")
	}

	total2, carry := bits.Add64(total1, amount, carry)
	if carry != 0 {
		return 0, NewReverts("stake is out of range")
	}
	return total2, nil
}

References

https://github.com/vechain/thor/blob/55dfff0c869801d9c02f50780b15f9b153953970/builtin/staker/staker.go#L739

Proof of Concept

<details> <summary>Test demonstrating missed overflow</summary>

Test to add to builtin/staker/staker_test.go:

Go
func Test_CheckStakeCarry(t *testing.T) {
	var valNextPeriodTVL uint64 = 10
	var aggNextPeriodTVL uint64 = math.MaxUint64
	var amount uint64 = 5
	_, err := checkStake(valNextPeriodTVL, aggNextPeriodTVL, amount)
	assert.NoError(t, err)
}

With the original (incorrect) implementation the test passes even though an integer overflow occurs:

Bash
PASS
ok      github.com/vechain/thor/v2/builtin/staker       0.024s
</details>

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

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

The carry flag from the first 64-bit addition is not checked before the result is reused in the second addition.

Impact

The report states the issue is informational because all values passed to checkStake were bounded before and overflow should never happen, so no real exploit is shown.

What the attacker needed

None in current code paths

The fix

The carry should also be checked after the first addition and the function should revert if it is set.

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.

Computers store whole numbers in fixed-size slots, and when a sum is too big for the slot it wraps around to a small value, which is called an integer overflow. This code tries to spot that by watching a 'carry' flag that the addition produces, but it only looks at the flag after the second addition and ignores it after the first. So a wrap-around in the first step could slip through, though the author says the inputs are limited in practice so it does not happen today.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More from VeChain Hayabusa Upgrade

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.