Submitted by serial-coder, also found by EV_om, rbserver, zzzitron (1, 2), AkshaySrivastav, mussucal, kaden, 0xAlix2, Beepidibop, and haxatron
The gnosis_safe_disable_module_offset constant was incorrectly specified to point at an incorrect function parameter of the disableModule(address prevModule, address module).
Specifically, the offset constant will point at the prevModule (1st param) instead of the module (2nd param).
When a safe transaction initiated from a rental safe containing a call to the safes disableModule() is invoked, the Guard::checkTransaction() cannot verify the module expected to be removed.
If the prevModule was a non-whitelisted extension, the safe transaction will be reverted.
However, if the prevModule was a whitelisted extension, the module will be removed without verification. Removing the rental safes module without verification can lead to other issues or attacks since the removed module can be a critical component (e.g., removing the protocols Stop policy contract).
The snippet below presents some of the Gnosis Safe configurations of the reNFT protocol. The gnosis_safe_disable_module_selector constant contains the function selector (0xe009cfde) of the rental safes ModuleManager::disableModule(address prevModule, address module).
Meanwhile, the gnosis_safe_disable_module_offset constant contains a memory offset (0x24) intended to point at the module param of the disableModule().
The below snippet shows the function signature of the rental safes ModuleManager::disableModule():
function disableModule(address prevModule, address module) external
Lets break down the value of the gnosis_safe_disable_module_offset constant (0x24):
0x24 == 36 == 32 (the calldatas array length) + 4 (the function selector)
As you can see, the gnosis_safe_disable_module_offset was incorrectly specified to point at the prevModule param (1st param) instead of the module param (2nd param) that refers to the module expected to be removed.
With the incorrect gnosis_safe_disable_module_offset constant, once the Guard::_checkTransaction() is triggered to verify the safe transaction containing a call to the safes disableModule(), the address of the prevModule contract will be extracted and assigned to the extension variable instead of the module contracts.
Consequently, the address of the prevModule contract will be verified for the whitelist by the Guard::_revertNonWhitelistedExtension() instead of the expected module contract address.
Besides, I also noticed that the developer also assumed that the disableModule() would have one function parameter while writing the test functions: test_Success_CheckTransaction_Gnosis_DisableModule() and test_Reverts_CheckTransaction_Gnosis_DisableModule().
That can confirm why the test functions cannot catch up with the mistake.
To point the memory offset at the module param (2nd param), the gnosis_safe_disable_module_offset constant must be set to 0x44 (0x44 == 68 == 32 (the calldatas array length) + 4 (the function selector) + 32 (1st param)).
Alec1017 (reNFT) confirmed and commented:
Note: To see full discussion, see here.
reNFT mitigated:
Status: Mitigation confirmed. Full details in reports from juancito, EV_om and sin1st3r__.
