registerTemplate() can't handle properly when ITemplate version is 0
The registerTemplate function fails to set the latest implementation when a template's version is 0, leaving latestImplementation as the zero address and causing duplicate entries in _templateNames. This can lead external contracts to believe no setup exists, resulting in repeated failed setup attempts.
Source: https://github.com/sherlock-audit/2022-10-nftport-judging/issues/80
Found by
bin2chen
Summary
Factory.sol when register one template , and template ' s version is 0, the latestImplementation[templateName] will be address(0) and add other version, "_templateNames" will duplicate
Vulnerability Detail
When version is equal 0 latestImplementation[templateName] don't set
function _setTemplate(
string memory templateName,
uint256 templateVersion,
address implementationAddress
) internal {
...
if (latestImplementation[templateName] == address(0)) { /****add other version, _templateNames will duplicate ****/
_templateNames.push(templateName);
}
if (templateVersion > latestVersion[templateName]) {
latestVersion[templateName] = templateVersion;
latestImplementation[templateName] = implementationAddress; /****templateVersion==0 , don't set ****/
}
}
Impact
latestImplementation[templateName] and _templateNames will error. external contracts may think there is no setup, resulting in duplicate setups that keep failing
Code Snippet
Tool used
Manual Review
Recommendation
function _setTemplate(
string memory templateName,
uint256 templateVersion,
address implementationAddress
) internal {
- if (templateVersion > latestVersion[templateName]) {
+ if (templateVersion > = latestVersion[templateName]) {
latestVersion[templateName] = templateVersion;
latestImplementation[templateName] = implementationAddress;
}
Discussion
hyperspacebunny
Fixed in https://github.com/nftport/evm-minting-sherlock-fixes/pull/4
rayn731
Fixed, it prevents using version 0, only > 0 is allowed.
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
The condition for updating the latest implementation uses a strict greater-than comparison, so a version of 0 is never stored, leaving the implementation address as zero and causing the name to be pushed again on subsequent registrations.
Impact
External contracts may incorrectly think no template is set up, leading to duplicate setup attempts that keep failing.
What the attacker needed
Version 0 is used for a template
The fix
The issue was fixed by preventing the use of version 0, only allowing versions greater than 0.
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.
A smart contract function that registers templates has a bug: if you register a template with version number 0, it doesn't properly record the template's address. Later, when you add another version, the contract thinks the template was never registered and adds its name again, causing duplicates. This can confuse other contracts that rely on this list.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.