HolyPaladinToken.sol uses ERC20 token with a highly unsafe pattern
The custom ERC20 implementation used by HolyPaladinToken.sol updates the sender's allowance after calling _transfer, contrary to the checks-effects-interactions pattern. An attacker could potentially exploit this ordering to double-spend or reenter if the token were extended with external calls.
Submitted by jayjonah8
In HolyPaladinToken.sol it imports ERC20.sol with some changes from the original Open Zeppelin standard. One change is that the transferFrom() function does not follow the Checks Effect and Interactions safety pattern to safely make external calls to other contracts. All checks should be handled first, then any effects/state updates, followed by the external call to prevent reentrancy attacks. Currently the transferFrom() function in ERC20.sol used by HolyPaladinToken.sol calls _transfer() first and then updates the sender allowance which is highly unsafe. The openZeppelin ER20.sol contract which is the industry standard first updates the sender allowance before calling _transfer. The external call should always be done last to avoid any double spending bugs or reentrancy attacks.
Proof of Concept
https://fravoll.github.io/solidity-patterns/checks_effects_interactions.html
https://github.com/code-423n4/2022-03-paladin/blob/main/contracts/open-zeppelin/ERC20.sol#L149
Open Zeppelins Implementation: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol
Recommended Mitigation Steps
Be sure to follow the Checks Effects and Interactions safety pattern as the transferFrom function is one of the most important functions in any protocol. Consider importing the Open Zeppelin ERC20.sol contract code directly as it is battle tested and safe code.
Kogaroshi (Paladin) confirmed, resolved, and commented:
ERC20.sol used was an older version from OpenZeppelin. Updated in the codebase via the PR: PaladinFinance/Paladin-Tokenomics#1. We now use the latest version of ERC20.sol that has the correct Checks Effects and Interactions safety pattern.
While I agree this is an issue, I think the severity has been overstated slightly. The transferFrom() function itself does not make any external calls so there is no exposure to reentrancy issues. That being said in the future if the contract had been extended in a way that enable an external call this would prove problematic.
Since the sponsor has confirmed the issue and this does violate well known best practices and open up the codebase to future issues, I will award the medium severity.
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 custom ERC20 implementation updates the sender's allowance after performing the token transfer, violating the checks-effects-interactions pattern.
Impact
The judge stated that the transferFrom function makes no external calls, so there is no current reentrancy exposure, but the pattern could become problematic if the contract is extended.
What the attacker needed
Token contract extended with external calls
The fix
The issue was fixed by updating to the latest OpenZeppelin ERC20.sol that follows the checks-effects-interactions pattern.
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.
This report is about a token contract that handles allowances in the wrong order. When someone transfers tokens on behalf of another user, the contract first moves the tokens and only then reduces the allowance. If the token contract ever made an external call during the transfer, an attacker could re-enter and spend the allowance again. The judge noted that the current code makes no external calls, so the risk is theoretical for now.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.