Unsafe cast of uint8 datatype to int8
The smart contract code casts an unsigned 8-bit integer (uint8) to a signed 8-bit integer (int8) without checking the value. If the uint8 value is greater than 127, the cast produces a negative number, which could lead to incorrect calculations in the quote function and in the distribution of excess assets.
Submitted by 0xTaylor
https://github.com/reserve-protocol/protocol/blob/df7ecadc2bae74244ace5e8b39e94bc992903158/contracts/p1/BackingManager.sol#L228 https://github.com/reserve-protocol/protocol/blob/df7ecadc2bae74244ace5e8b39e94bc992903158/contracts/p1/BasketHandler.sol#L421
Converting uint8 to int8 can have unexpected consequences when done unsafely. This issue affects the quote function in BasketHandler.sol and handoutExcessAssets in BackingManager.sol. While there is some risk here, the issue is unlikely to be exploited as ERC-20 tokens generally don’t have a decimals value over 18, nevertheless one over 127.
Proof of Concept
➜ int8(uint8(127))
Type: int
├ Hex: 0x7f
└ Decimal: 127
➜ int8(uint8(128))
Type: int
├ Hex: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80
└ Decimal: -128
Tools Used
Chisel
Recommended Mitigation Steps
Validate that the decimals value is within an acceptable upper-bound before attempting to cast it to a signed integer.
The warden does make a reasonable point re: the cast being done here and others have pointed out concerns over assuming that an ERC20 must have a decimals field.
Will leave open for sponsor review, but I think this would qualify as Medium.
tbrent (Reserve) acknowledged and commented:
Agreed, seems Medium to me.
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 code performs an unchecked cast from uint8 to int8, which can produce a negative value when the input exceeds 127.
Impact
The issue is unlikely to be exploited because ERC-20 tokens generally do not have a decimals value over 18, let alone over 127, but if such a token existed it could cause incorrect calculations.
What the attacker needed
A token with decimals > 127
The fix
Validate that the decimals value is within an acceptable upper bound before casting it to a signed integer.
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.
In programming, numbers are stored in fixed-size containers. A uint8 holds values from 0 to 255, while an int8 holds values from -128 to 127. If you force a uint8 value above 127 into an int8, it wraps around to a negative number. This report points out that the smart contract does this conversion without checking, which could cause wrong results if a token has an unusually high number of decimals.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.