addAssetBalance wraps on overflow and subAssetBalance clamps under zero
Report
Summary
addAssetBalance and subAssetBalance are the unexported helpers every asset credit and debit is supposed to share, so a wrapping delta should error instead of persisting a wrong balance. At core/ledger.go:509-529, addAssetBalance does += with no overflow check (MaxUint64+5 becomes 4) and subAssetBalance clamps 9-100 to 0 and does not return an error. Higher-level paths mostly have their own checks, so this is the missing safety net those paths claim to share. Any ledger caller that feeds these helpers a wrapping or underflowing delta hits the gap. GitHub #43 is swap unwind and the lease_cancel wrap is the admitted credit path; these are the unexported helpers every asset credit and debit goes through, without the guards the higher-level paths claim to have.
Affected components
core/ledger.go:509-529 addAssetBalance (+= with no overflow check) and subAssetBalance (clamp to 0 on underflow) at xe 02e9641b0ac64437f57e09d63301f88cce61a802.
Impact
Any caller that feeds these helpers a wrapping or underflowing delta persists a wrong balance. Today the higher-level paths mostly have their own checks, so this is the missing safety net those paths are supposed to share. Worst case is a silent wrap or clamp on a future path that forgets to re-check.
PoC
package core
import (
"fmt"
"math"
"testing"
)
// Overlay into package core. addAssetBalance / subAssetBalance are unexported.
func TestPoC_B6(t *testing.T) {
fmt.Println("PROVENANCE: xe=02e9641b0ac64437f57e09d63301f88cce61a802 file=poc/round3/overlay/b6_asset_balance_test.go")
l := &Ledger{assetBalances: make(map[string]map[string]uint64)}
acc := "acct"
l.addAssetBalance(acc, "XE", 7)
ctrl := l.getAssetBalance(acc, "XE")
l2 := &Ledger{assetBalances: make(map[string]map[string]uint64)}
l2.addAssetBalance(acc, "XE", math.MaxUint64)
l2.addAssetBalance(acc, "XE", 5)
wrapped := l2.getAssetBalance(acc, "XE")
l3 := &Ledger{assetBalances: make(map[string]map[string]uint64)}
l3.setAssetBalance(acc, "XUSD", 9)
l3.subAssetBalance(acc, "XUSD", 100)
clamped := l3.getAssetBalance(acc, "XUSD")
fmt.Printf("CONTROL add 7 -> %d\n", ctrl)
fmt.Printf("ATTACK add MaxUint64+5 -> %d (want 4)\n", wrapped)
fmt.Printf("ATTACK sub 9-100 clamp -> %d\n", clamped)
stake := LeaseStake(1000)
fmt.Printf("LeaseStakeDivisor=%d LeaseStake(1000)=%d (AssetCredit path)\n", LeaseStakeDivisor, stake)
if ctrl == 7 && wrapped == 4 && clamped == 0 {
fmt.Println("PASS: addAssetBalance wraps; subAssetBalance clamps under zero; CONTROL add is exact")
fmt.Println("VERDICT=CONFIRMED")
return
}
fmt.Println("VERDICT=KILLED")
}
ATTACK add MaxUint64+5 -> 4
ATTACK sub 9-100 clamp -> 0
PASS: addAssetBalance wraps; subAssetBalance clamps under zero
Suggested fix
Return error on overflow in addAssetBalance and on underflow in subAssetBalance. Do not wrap and do not clamp to zero.
Thread
Thanks. This is the same finding as an earlier report that is not public, which was filed first; the programme pays the first verifiable submission, so this one closes as a duplicate. If you believe it is a different bug, file a new report that links https://bounty.xe.network/XE-0942 and says what differs; it is read like any other.