Solidity&Foundry 安全审计测试 memory滥用

名称:

memory滥用

https://github.com/XuHugo/solidityproject/tree/master/vulnerable-defi

描述:

在合约函数中滥用storage和memory。

memory是一个关键字,用于临时存储执行合约所需的数据。它保存函数的参数数据,并在执行后清除。
storage可以看作是默认的数据存储。它持久地保存数据,消耗更多的gas

函数updaterewardDebt的功能是,更新UserInfo结构体的rewardDebt值。为了节约gas,我们将变量用关键字memory声明了,这样会导致的问题是,在函数执行结束之后,rewardDebt的值并不会保存下来。因为一旦函数完成执行,内存就会被清除,所做的更改也会丢失。

参考:

Cover protocol hack analysis: Infinite Cover tokens minted via an exploit - Mudit Gupta's Blog

解决方法:

https://mudit.blog/cover-protocol-hack-analysis-tokens-minted-exploit/

proxy合约:

javascript 复制代码
contract Array is Test {
    mapping(address => UserInfo) public userInfo; // storage

    struct UserInfo {
        uint256 amount; // How many tokens got staked by user.
        uint256 rewardDebt; // Reward debt. See Explanation below.
    }

    function updaterewardDebt(uint amount) public {
        UserInfo memory user = userInfo[msg.sender]; // memory, vulnerable point
        user.rewardDebt = amount;
    }

    function fixedupdaterewardDebt(uint amount) public {
        UserInfo storage user = userInfo[msg.sender]; // storage
        user.rewardDebt = amount;
    }
}

foundry测试合约;

javascript 复制代码
// A function to demonstrate the difference between memory and storage data locations in Solidity.
function testDataLocation() public {
    // Simulate dealing 1 ether to Alice and Bob.
    address alice = vm.addr(1);
    address bob = vm.addr(2);
    vm.deal(address(alice), 1 ether);
    vm.deal(address(bob), 1 ether);

    // Create a new instance of the Array contract.
    ArrayContract = new Array();

    // Update the rewardDebt storage variable in the Array contract to 100.
    ArrayContract.updaterewardDebt(100); 

    // Retrieve the userInfo struct for the contract's address and print the rewardDebt variable.
    // Note that the rewardDebt should still be the initial value, as updaterewardDebt operates on a memory variable, not the storage one.
    (uint amount, uint rewardDebt) = ArrayContract.userInfo(address(this));
    console.log("Non-updated rewardDebt", rewardDebt);

    // Print a message.
    console.log("Update rewardDebt with storage");

    // Now use the fixedupdaterewardDebt function, which correctly updates the storage variable.
    ArrayContract.fixedupdaterewardDebt(100);

    // Retrieve the userInfo struct again, and print the rewardDebt variable.
    // This time the rewardDebt should be updated to 100.
    (uint newamount, uint newrewardDebt) = ArrayContract.userInfo(
        address(this)
    );
    console.log("Updated rewardDebt", newrewardDebt);
}
相关推荐
2501_9467862021 分钟前
如何高效查找同时持有CCRC和CMA双认证的信息安全服务商?
服务器·网络·安全
pengyi87101542 分钟前
共享IP使用基础注意事项,从源头降低关联风险
网络·网络协议·tcp/ip·安全·http
星幻元宇VR1 小时前
VR动感电动车|以沉浸体验推动交通安全科普新方式
人工智能·科技·学习·安全·生活·vr
云空1 小时前
《OpenClaw(macOS版)部署与使用中的安全问题及解决方案》
安全·macos·策略模式
Dontla1 小时前
VPC(Virtual Private Cloud虚拟私有云)介绍(内部网络隔离、逻辑私有网络、子网隔离Subnet、公有子网、私有子网、路由表控制、安全组)
网络·安全
wsdswzj2 小时前
数据库基础安全
数据库·安全
CV-杨帆2 小时前
OpenClaw模型攻击与防御研究论文综述
安全
free_732 小时前
OpenClaw×AI隐私安全舱——ClawVault:重新定义企业级智能数据防线
人工智能·python·安全
安科瑞小许2 小时前
直流系统的“绝缘卫士”——储能与充电桩的安全防线
安全·充电桩·直流绝缘监测
Chengbei112 小时前
Fortify_SCA_26.1版下载(OpenText SAST(Fortify SCA)26.1 windows/Linux/Mac)全版本下载
运维·安全·web安全·macos·网络安全·系统安全·代码审计