Inconsistent check in `harvestPositionsTo()` function
The harvestPositionsTo function contains a contradictory check that only allows the token owner to call it, even though an earlier check permits an approved address. As a result, an approved address cannot harvest on behalf of the NFT owner.
AuditorPraise Follow with BengalCatBalu, ChinmayF and 12 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-06-magicsea-judging/issues/329
Found by
AuditorPraise, BengalCatBalu, ChinmayF, Honour, Reentrants, Ryonen, bbl4de, blackhole, coffiasd, dany.armstrong90, minhquanym, novaman33, radin200, scammed, sh0velware
Summary
Inconsistent check in harvestPositionsTo() function limits the ability of approved address to harvest on behalf of owner.
Vulnerability Detail
In the function harvestPositionsTo(), function _requireOnlyApprovedOrOwnerOf() allows owner or approved address to harvest for the position.
However, the check (msg.sender == tokenOwner && msg.sender == to) only allowing the caller to be token owner. Thus these 2 checks are contradicted.
function harvestPositionsTo(uint256[] calldata tokenIds, address to) external override nonReentrant {
_updatePool();
uint256 length = tokenIds.length;
for (uint256 i = 0; i < length; ++i) {
uint256 tokenId = tokenIds[i];
_requireOnlyApprovedOrOwnerOf(tokenId);
address tokenOwner = ERC721Upgradeable.ownerOf(tokenId);
// if sender is the current owner, must also be the harvest dst address
// if sender is approved, current owner must be a contract
// @audit not consistent with _requireOnlyApprovedOrOwnerOf()
require(
(msg.sender == tokenOwner && msg.sender == to), // legacy || tokenOwner.isContract()
"FORBIDDEN"
);
_harvestPosition(tokenId, to);
_updateBoostMultiplierInfoAndRewardDebt(_stakingPositions[tokenId]);
}
}
Impact
Contradictions in the function harvestPositionsTo(). Approved address cannot call harvestPositionsTo() on behalf of NFT owner.
Code Snippet
Tool used
Manual Review
Recommendation
The intended check in function harvestPositionsTo() might be, changing && to ||
require(
- (msg.sender == tokenOwner && msg.sender == to), // legacy || tokenOwner.isContract()
+ (msg.sender == tokenOwner || msg.sender == to), // legacy || tokenOwner.isContract()
"FORBIDDEN"
);
Discussion
0xSmartContract
Due to conflicting checks in the harvestPositionsTo() function, authorized addresses are prevented from harvesting on behalf of the NFT owner.
0xHans1
This was fixed during an other audit. add code comment to show the fix in the PR
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/metropolis-exchange/magicsea-staking/pull/23
sherlock-admin2
The Lead Senior Watson signed off on the fix.
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 require condition uses a logical AND instead of OR, so it only passes when the caller is both the token owner and the destination address, contradicting the earlier approval check.
What the attacker needed
An NFT position in the staking contract; an approved address for that NFT
The fix
The check should be changed from `&&` to `||` so that either the token owner or the destination address can call the function, and this was fixed in a later pull request.
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 this smart contract, an NFT owner can approve someone else to act on their behalf. The function that harvests rewards first checks that the caller is either the owner or an approved address, but then a second check only allows the owner. This means the approved person is blocked from harvesting, which is not what the code intended.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.