Repository navigation
Conversation
SafeMul and caclAVGPrice compute with big.Int and then silently truncate the result with Int64(). For a large amount/price combination the wrapped value can be a small positive number, so a malicious passive buy order only freezes a tiny amount and the matching engine pays the taker seller the wrapped value while transferring the full token amount. The average price bookkeeping is corrupted the same way. - SafeMul/caclAVGPrice now return ErrOverflow when the result does not fit in int64. - Order creation, frozen remainder, matching settlement and average-price updates reject the overflow behind ForkFixSafeMulOverflow. - Pre-fork behavior is unchanged (the truncated value is still used) so the fix can be activated at a fork height.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
SafeMul用 big.Int 计算后再Int64()截断,结果超出 int64 时静默回绕(可能变成一个小正数)且上层不感知。攻击:挂一笔被动买单,
Amount=28999941890838049、Price=1e16(均在CheckAmount/CheckPrice允许范围),名义成本约为 2.9e24,截断后恰好为 256,买单只冻结 256 个单位。taker 卖单卖出数量不小于该挂单数量时,matchModel用SafeMul(matched, 1e16, 1e8)计算付款同样回绕成 256,卖方交出大量 token 只收到 0.00000256 枚。caclAVGPrice也会同样截断,损坏订单均价/市场深度。修复
SafeMul、caclAVGPrice改为返回(int64, error),!IsInt64()时返回ErrOverflow。ForkFixSafeMulOverflow,在下单余额检查、撤单、未成交冻结、撮合付款、均价计算所有路径分叉后拒绝溢出;分叉前仍沿用旧的截断值,保证历史区块可重放。CalcActualCost调用,并新增TestSafeMul溢出用例。验证
go build ./plugin/dapp/exchange/...通过go test ./plugin/dapp/exchange/executor/ -count=1通过