Contract fails to deliver promised returns, but doesn't lose value
Description
Brief/Intro
In Solidity, a struct is a complex data type that allows you to group together variables of different data types. And a mapping is a data structure that allows you to store key-value pairs.
The security implications of deleting a struct that contains a mapping are subtle, but important to understand in the context of Ethereum smart contracts.
When you delete a struct in Solidity, it will not delete the mapping within it. The delete keyword in Solidity sets every field in the struct to its default value. For integers, strings, arrays, and other simple data types, this means they will be set to zero, an empty string, or an empty array, respectively.
However, for mappings, the delete keyword has no effect. This is because mappings are implemented as hash tables and the Ethereum Virtual Machine (EVM) does not keep track of which keys have been used in the mapping. As a result, it doesn't know how to "reset" a mapping. Therefore, when you delete a struct, the mapping within it will still retain its old data.
This can lead to potential security issues, particularly if you’re not aware of this behavior. For example, let’s say you have a struct that contains sensitive data within a mapping. If you delete the struct assuming that all data within it will be erased, the data in the mapping will still persist, potentially leading to unintended access or misuse.
Vulnerability Details
unintended access or misuse.
Vulnerable contract is LoanManager.sol : https://github.com/Folks-Finance/folks-finance-xchain-contracts/blob/main/contracts/hub/LoanManager.sol
Vulnerable function is deleteUserLoan() : https://github.com/Folks-Finance/folks-finance-xchain-contracts/blob/fb92deccd27359ea4f0cf0bc41394c86448c7abb/contracts/hub/LoanManager.sol#L60C1-L73C1
This function will delete _userLoans after several checks. _userLoans is struct which contains a mapping.
UserLoan struct have two struct which contains mapping - UserLoanCollateral and UserLoanBorrow:
UserLoanCollateral and UserLoanBorrow , each of these two stores important values:
Result: The deleteUserLoan function used delete keyword to delete _userLoans struct which contains mapping for a loanId. However, if you call the deleteUserLoan function, it will not delete the collaterals and borrows mapping within the UserInfo struct. This means that even after a user has been deleted, their active data will still persist in the contract.
Impact Details
All data in _userLoans for loanId is not deleted we expect it to be completely erased
function deleteUserLoan(bytes32 loanId, bytes32 accountId) external override onlyRole(HUB_ROLE) nonReentrant {
// check user loan active and account owner
if (!isUserLoanActive(loanId)) revert UnknownUserLoan(loanId);
if (!isUserLoanOwner(loanId, accountId)) revert NotAccountOwner(loanId, accountId);
// ensure loan is empty
if (!_isUserLoanEmpty(loanId)) revert LoanNotEmpty(loanId);
// delete by setting isActive to false
delete _userLoans[loanId]; // @Audit is here
emit DeleteUserLoan(loanId, accountId);
}