Unsafe downcast of uint256 to uint8 will lead to silent overflow
The contract casts a week counter from a large integer type to a small one without checking the range, so after 255 weeks the week number wraps to zero. A user winning a jackpot in week 256 would be paid the prize stored for week 0 instead of the intended week 256 prize.
Plume Network
Originally disclosed on Immunefi
51771 sc low unsafe downcast of uint256 to uint8 will lead to silent overflow
- Submitted on: Aug 5th 2025 at 17:09:02 UTC by @vielite for Attackathon | Plume Network
- Report ID: #51771
- Report Type: Smart Contract
- Report severity: Low
- Target: https://github.com/immunefi-team/attackathon-plume-network/blob/main/plume/src/spin/Spin.sol
- Impacts: Contract fails to deliver promised returns, but doesn't lose value
Description
Brief/Intro
The determineReward() function in Spin.sol unsafely downcasts the uint256 week counter into a uint8:
function determineReward(
uint256 randomness,
uint256 streakForReward
) internal view returns (string memory, uint256) {
uint256 probability = randomness % 1_000_000; // Normalize VRF range to 1M
// Determine the current week in the 12-week campaign
uint256 daysSinceStart = (block.timestamp - campaignStartDate) / 1 days;
uint8 weekNumber = uint8(getCurrentWeek());
uint8 dayOfWeek = uint8(daysSinceStart % 7);
While the developer's intent was to enforce a 12-week campaign, the smart contract does not enforce this limit. If the campaign runs longer than 255 weeks, the downcast will truncate the value and cause a silent overflow: weekNumber will wrap and may be 0.
Vulnerability Details
The vulnerability stems from the unsafe downcast of a uint256 value returned by getCurrentWeek() into a uint8 in determineReward(). getCurrentWeek() returns:
function getCurrentWeek() public view returns (uint256) {
return (block.timestamp - campaignStartDate) / 7 days;
}
Because getCurrentWeek() returns a uint256 (number of weeks since campaign start), forcing this into uint8 (uint8 weekNumber = uint8(getCurrentWeek());) will silently truncate for values > 255. When the week count reaches 256, weekNumber becomes 0, causing reward lookups to use the wrong week index.
Impact Details
- Severity: Low (contract fails to deliver promised returns but does not lose value)
- If the campaign runs past 255 weeks (~5 years), reward calculation can reference incorrect week indices (e.g., week 256 truncated to 0), causing users to receive incorrect (typically lower) rewards. No funds are directly lost, but promised returns are not delivered.
References
Proof of Concept
{% stepper %} {% step %}
Step — Prerequisite
The Spin.sol contract has been deployed and is actively running for ≥255 weeks.
{% endstep %}
{% step %}
Step — State
Admin sets jackpot prize for week 256 as 10_000. Assume jackpotPrizes[0] = 100.
function setJackpotPrizes(uint8 week, uint256 prize) external onlyRole(ADMIN_ROLE) {
jackpotPrizes[week] = prize;
}
{% endstep %}
{% step %}
Step — User Action
A user calls startSpin() and the oracle callback provides randomness that results in a "Jackpot" win.
{% endstep %}
{% step %}
Step — Flawed execution
getCurrentWeek()returns256.uint8 weekNumber = uint8(256)truncates to0due to downcast.- The function uses
jackpotPrizes[0]instead ofjackpotPrizes[256]. {% endstep %}
{% step %}
Step — Result
The user receives 100 tokens (the value for week index 0) instead of the intended 10_000 tokens for week 256.
{% endstep %}
{% endstepper %}
Context
The target is the Spin.sol contract in the Plume Network attackathon repository, specifically the determineReward() function. The contract implements a spin-to-win feature with a 12-week campaign: a user calls startSpin(), an oracle callback supplies verifiable randomness, and determineReward() maps that randomness and the user's streak onto a prize. Part of that mapping depends on which week of the campaign the spin happened in, and on which day of the week.
Setup
The only precondition the report names is that the Spin.sol contract has been deployed and has been actively running for at least 255 weeks. The proof of concept also assumes an admin holding ADMIN_ROLE has configured jackpot prizes, and that a user can call startSpin() and receive a randomness result that produces a "Jackpot" win. No tooling, versions or accounts beyond these roles are described.
Finding it
The determineReward() function derives the campaign week with an unsafe narrowing conversion. The function computes the week counter as a uint256 and then forces it into a uint8:
function determineReward(
uint256 randomness,
uint256 streakForReward
) internal view returns (string memory, uint256) {
uint256 probability = randomness % 1_000_000; // Normalize VRF range to 1M
// Determine the current week in the 12-week campaign
uint256 daysSinceStart = (block.timestamp - campaignStartDate) / 1 days;
uint8 weekNumber = uint8(getCurrentWeek());
uint8 dayOfWeek = uint8(daysSinceStart % 7);
The developer's intent, as the comment says, was a 12-week campaign, but the contract does not enforce that limit anywhere. If the campaign runs longer than 255 weeks, the downcast truncates the value and weekNumber wraps, potentially to 0. That is a silent overflow: Solidity does not revert on an explicit narrowing cast, so nothing in the code flags the loss of the high bits.
Exploitation
Step 1: Prerequisite
The Spin.sol contract has been deployed and is actively running for at least 255 weeks.
Step 2: State
The admin sets the jackpot prize for week 256 to 10_000. The proof of concept assumes jackpotPrizes[0] = 100. The setter is:
function setJackpotPrizes(uint8 week, uint256 prize) external onlyRole(ADMIN_ROLE) {
jackpotPrizes[week] = prize;
}
Step 3: User action
A user calls startSpin() and the oracle callback provides randomness that results in a "Jackpot" win.
Step 4: Flawed execution
getCurrentWeek()returns256.uint8 weekNumber = uint8(256)truncates to0due to the downcast.- The function uses
jackpotPrizes[0]instead ofjackpotPrizes[256].
Step 5: Result
The user receives 100 tokens (the value for week index 0) instead of the intended 10_000 tokens for week 256.
Root cause
The week counter is produced by getCurrentWeek(), which returns a uint256:
function getCurrentWeek() public view returns (uint256) {
return (block.timestamp - campaignStartDate) / 7 days;
}
Because the return type is uint256, assigning it to a uint8 with uint8 weekNumber = uint8(getCurrentWeek()); silently truncates any value above 255. When the week count reaches 256, weekNumber becomes 0, and reward lookups use the wrong week index. The 12-week campaign length exists only as a comment; the contract does not enforce it, so nothing stops the counter from growing past the width of the destination type.
Impact
If the campaign runs past 255 weeks (roughly 5 years), reward calculation can reference incorrect week indices, for example week 256 truncated to 0, causing users to receive incorrect, typically lower, rewards. No funds are directly lost, but promised returns are not delivered. The report rates this Low on that basis: the contract fails to deliver promised returns without losing value.
Fix
The report does not state that a fix was applied, nor does it prescribe one. The report does not prescribe a fix. The report does not say which approach the project took.
Variants to test
- Other explicit narrowing casts of counters or timestamps, such as
uint128(x),uint64(x),uint32(x)oruint16(x), where the source value grows with time or usage. A positive result is a value that can plausibly exceed the destination width during the contract's lifetime. - Prize or configuration tables indexed by a narrowed type, as with
setJackpotPrizes(uint8 week, ...). A positive result is an index that wraps to an entry the operator did not intend for that period. - Campaign or epoch counters that are documented as bounded (here, 12 weeks) but not enforced in code. A positive result is a comment or specification stating a limit with no corresponding check.
- Any place where a
uint256from a view function such asgetCurrentWeek()is stored or compared in a smaller type. A positive result is a mismatch between the declared return type and the storage or local type.
Hunting notes
This class of bug hides in time-derived counters and in configuration tables keyed by the same narrowed type. During review, look for explicit casts (uint8(...), uint16(...)) applied to values that come from block.timestamp arithmetic, and for comments that promise a bounded campaign or epoch without a check that enforces the bound. The signal is a view function returning a wide integer whose result is immediately narrowed.
What happened, in plain words
A smart contract is a small program that lives on a blockchain. Once it is deployed, anyone can call it, and it runs exactly as written. This report is about one such program, called Spin.sol, which belongs to a project called Plume Network. The program implements a spin-to-win game: a user spins, some random number decides the outcome, and the user may win a prize.
The game was designed as a 12-week campaign. Each week can have its own jackpot prize, and the prize a user gets depends on which week the spin happened in. To find out the current week, the program counts how many weeks have passed since the campaign started.
The problem is a type conversion. In Solidity, the language smart contracts are written in, numbers have fixed sizes. A uint256 is an unsigned whole number that can hold values from 0 up to a very large number (2 to the power of 256 minus 1). A uint8 is an unsigned whole number that can only hold values from 0 to 255. When the program takes a uint256 and squeezes it into a uint8, it does not complain if the value is too big. It simply keeps the low part and throws the rest away. This is called a downcast, and when it loses information it is a silent overflow: silent because nothing errors out, overflow because the number wraps around.
So if the campaign ever runs for 256 weeks, the week counter becomes 256, the downcast turns it into 0, and the program looks up the prize for week 0 instead of week 256. The user gets the wrong prize, usually a smaller one. No money is stolen, but the contract does not pay what it promised. That is why the report is rated Low.
Context
The target is the Spin.sol contract in the Plume Network attackathon repository, specifically the determineReward() function. The contract implements a spin-to-win feature with a 12-week campaign: a user calls startSpin(), an oracle callback supplies verifiable randomness, and determineReward() maps that randomness and the user's streak onto a prize. Part of that mapping depends on which week of the campaign the spin happened in, and on which day of the week.
A few of those words need unpacking. A function is a named block of code inside a program; determineReward() is the function that decides what a spin pays out. An oracle is an outside service that feeds information into a blockchain, because a blockchain program cannot reach the internet by itself; here the oracle supplies random numbers. Verifiable randomness means the random number comes with proof that it was really produced randomly and not chosen by an interested party. A streak is a count of consecutive actions by a user. A callback is code that runs later, when an outside event happens; the oracle callback is the moment the random number arrives and the contract continues processing the spin.
Setup
The only precondition the report names is that the Spin.sol contract has been deployed and has been actively running for at least 255 weeks. Deployed means the program has been published to the blockchain and is live. The proof of concept also assumes an admin holding ADMIN_ROLE has configured jackpot prizes, and that a user can call startSpin() and receive a randomness result that produces a "Jackpot" win. A role is a permission label inside the contract; only an address granted ADMIN_ROLE may run administrative functions such as setting prizes. No tooling, versions or accounts beyond these roles are described.
Finding it
The determineReward() function derives the campaign week with an unsafe narrowing conversion. The function computes the week counter as a uint256 and then forces it into a uint8:
function determineReward(
uint256 randomness,
uint256 streakForReward
) internal view returns (string memory, uint256) {
uint256 probability = randomness % 1_000_000; // Normalize VRF range to 1M
// Determine the current week in the 12-week campaign
uint256 daysSinceStart = (block.timestamp - campaignStartDate) / 1 days;
uint8 weekNumber = uint8(getCurrentWeek());
uint8 dayOfWeek = uint8(daysSinceStart % 7);
Line by line:
function determineReward(starts the declaration of a function nameddetermineReward.internalmeans only this contract (or contracts derived from it) can call it, andviewmeans it only reads data and does not change the blockchain state.uint256 randomness,is the first input: the random number supplied by the oracle, stored in a 256-bit unsigned integer.uint256 streakForRewardis the second input: the user's streak, also a 256-bit unsigned integer.) internal view returns (string memory, uint256) {says the function gives back two things: a piece of text (the name of the prize, such as "Jackpot") and a number (the prize amount).uint256 probability = randomness % 1_000_000;takes the random number modulo one million. Modulo gives the remainder of a division, so the result is always between 0 and 999,999. The comment explains this normalises the oracle's range to one million, which makes probability comparisons easy.// Determine the current week in the 12-week campaignis a comment, ignored by the computer. It records the developer's intent: a campaign of 12 weeks.uint256 daysSinceStart = (block.timestamp - campaignStartDate) / 1 days;subtracts the campaign start date from the current block timestamp (the time of the block being processed) to get elapsed seconds, then divides by the number of seconds in a day. The result is how many whole days have passed.uint8 weekNumber = uint8(getCurrentWeek());calls another function,getCurrentWeek(), and squeezes its result into an 8-bit number. This is the line the report is about.uint8 dayOfWeek = uint8(daysSinceStart % 7);works out the day of the week by taking the day count modulo 7, giving a value from 0 to 6, and stores it in auint8. Here the value can never exceed 6, so the narrowing is harmless.
The developer's intent, as the comment says, was a 12-week campaign, but the contract does not enforce that limit anywhere. If the campaign runs longer than 255 weeks, the downcast truncates the value and weekNumber wraps, potentially to 0. That is a silent overflow: Solidity does not revert on an explicit narrowing cast, so nothing in the code flags the loss of the high bits. Revert is the blockchain term for aborting and undoing a transaction with an error; a silent failure is one where no error is raised at all.
Exploitation
Exploitation here means walking through the conditions under which the bug produces a wrong result. Nobody attacks anything; the contract simply misbehaves on its own once enough time has passed.
Step 1: Prerequisite
The Spin.sol contract has been deployed and is actively running for at least 255 weeks. This step exists because the bug only appears once the week counter grows past what an 8-bit number can hold. 255 weeks is almost five years.
Step 2: State
The admin sets the jackpot prize for week 256 to 10_000. The proof of concept assumes jackpotPrizes[0] = 100. The setter is:
function setJackpotPrizes(uint8 week, uint256 prize) external onlyRole(ADMIN_ROLE) {
jackpotPrizes[week] = prize;
}
Line by line:
function setJackpotPrizes(uint8 week, uint256 prize)declares a function that takes a week number and a prize amount.externalmeans it can be called from outside the contract.onlyRole(ADMIN_ROLE)is a guard: the call only proceeds if the caller holds the admin role. Without it, anyone could set prizes.jackpotPrizes[week] = prize;stores the prize in a table (a mapping) under the given week number.
Step 3: User action
A user calls startSpin() and the oracle callback provides randomness that results in a "Jackpot" win. This step is needed to reach the reward calculation at all: the spin is what triggers determineReward().
Step 4: Flawed execution
getCurrentWeek()returns256.uint8 weekNumber = uint8(256)truncates to0due to the downcast.- The function uses
jackpotPrizes[0]instead ofjackpotPrizes[256].
This is the heart of the report. The real week is 256, but the stored week is 0, so the prize lookup goes to the wrong row of the table.
Step 5: Result
The user receives 100 tokens (the value for week index 0) instead of the intended 10_000 tokens for week 256. A token here is a digital unit of value on the blockchain. The user is not robbed; they simply get the payout configured for a different week.
Root cause
The week counter is produced by getCurrentWeek(), which returns a uint256:
function getCurrentWeek() public view returns (uint256) {
return (block.timestamp - campaignStartDate) / 7 days;
}
Line by line:
function getCurrentWeek() public view returns (uint256)declares a function anyone can call (public), which only reads (view) and gives back a 256-bit unsigned integer.return (block.timestamp - campaignStartDate) / 7 days;subtracts the campaign start from the current time to get elapsed seconds, then divides by the number of seconds in seven days. The result is the number of whole weeks since the campaign started.
Because the return type is uint256, assigning it to a uint8 with uint8 weekNumber = uint8(getCurrentWeek()); silently truncates any value above 255. When the week count reaches 256, weekNumber becomes 0, and reward lookups use the wrong week index. The 12-week campaign length exists only as a comment; the contract does not enforce it, so nothing stops the counter from growing past the width of the destination type.
Impact
If the campaign runs past 255 weeks (roughly 5 years), reward calculation can reference incorrect week indices, for example week 256 truncated to 0, causing users to receive incorrect, typically lower, rewards. No funds are directly lost, but promised returns are not delivered. The report rates this Low on that basis: the contract fails to deliver promised returns without losing value.
Fix
The report does not state that a fix was applied, nor does it prescribe one. The report does not prescribe a fix. The report does not say which approach the project took.
Variants to test
These are other places where the same kind of mistake could hide, useful for anyone reviewing similar code.
- Other explicit narrowing casts of counters or timestamps, such as
uint128(x),uint64(x),uint32(x)oruint16(x), where the source value grows with time or usage. A positive result is a value that can plausibly exceed the destination width during the contract's lifetime. - Prize or configuration tables indexed by a narrowed type, as with
setJackpotPrizes(uint8 week, ...). A positive result is an index that wraps to an entry the operator did not intend for that period. - Campaign or epoch counters that are documented as bounded (here, 12 weeks) but not enforced in code. A positive result is a comment or specification stating a limit with no corresponding check.
- Any place where a
uint256from a view function such asgetCurrentWeek()is stored or compared in a smaller type. A positive result is a mismatch between the declared return type and the storage or local type.
Hunting notes
This class of bug hides in time-derived counters and in configuration tables keyed by the same narrowed type. During review, look for explicit casts (uint8(...), uint16(...)) applied to values that come from block.timestamp arithmetic, and for comments that promise a bounded campaign or epoch without a check that enforces the bound. The signal is a view function returning a wide integer whose result is immediately narrowed.
Glossary
- Smart contract: a program stored on a blockchain that runs automatically when called and cannot be changed after deployment.
- Blockchain: a shared, append-only record of transactions kept by many computers, where programs can live and run.
- Solidity: the programming language used to write smart contracts, mostly on Ethereum-style blockchains.
uint256: an unsigned whole number of 256 bits, able to hold values from 0 up to 2^256 minus 1.uint8: an unsigned whole number of 8 bits, able to hold values from 0 to 255.- Downcast / narrowing conversion: converting a value into a smaller type, which discards the high bits if the value does not fit.
- Silent overflow: a wrap-around of a number that happens without any error being raised.
- Revert: aborting a transaction and undoing its effects with an error; a narrowing cast does not revert.
- Function: a named block of code that takes inputs and returns outputs.
internal/public/external: visibility labels saying who may call a function (inside the contract, anyone, or only from outside).view: a function label meaning it only reads data and does not change the blockchain.- Modulo (
%): the remainder after division;x % 7is always between 0 and 6. - Oracle: an outside service that feeds data, such as random numbers, into a smart contract.
- Verifiable randomness: random numbers delivered with proof that they were genuinely random.
- Callback: code that runs later, when an outside event such as an oracle response arrives.
- Streak: a count of consecutive actions by a user.
- Deployed: published to the blockchain and live.
- Role /
ADMIN_ROLE: a permission label; only addresses granted the role may call administrative functions. - Mapping: a lookup table inside a contract, here mapping a week number to a prize amount.
block.timestamp: the time of the block currently being processed, used as the contract's clock.- Token: a digital unit of value on a blockchain.
- Severity: the rating of how serious a finding is, here Low because no funds are lost.
- Proof of concept: a step-by-step demonstration that the bug produces the described result.
- Epoch / campaign: a named period of time the contract counts, here a 12-week campaign.
- Attackathon: a time-boxed security competition in which researchers look for bugs in a project's code.