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);
}
相关推荐
小程故事多_8014 小时前
算力工厂与安全沙箱,企业AI规模化落地的两条核心生命线
人工智能·安全
牛马工作号15 小时前
Zigbee 专业详解:从协议到排障
网络·物联网·安全
延凡科技15 小时前
智慧燃气解决方案:管道煤气监管应用管理系统(三维GIS+IoT+大数据落地实战)
大数据·人工智能·科技·物联网·安全
spear80015 小时前
高压绝缘测试安全指南:击穿保护、燃弧定位、快速放电、抗干扰滤波详解
安全·绝缘电阻·三级承装承修承试资质公司设备·电缆绝缘电阻
liukuang11017 小时前
Zara“夺命裤”风波,白晨铭的安全大考
安全
Android小码家17 小时前
OWASP 移动应用安全之授权(MASTG-DEMO-0090)
android·安全·frida
Multipath71217 小时前
智能巡检机器人多链路聚合路由解决方案
网络·5g·安全·实时音视频·信息与通信
学习溢出17 小时前
【网络安全】Living off the Land 攻击 | 蓝队防守
安全·web安全·网络安全
上海云盾-小余18 小时前
全链路多层防护体系,一站式拦截 DDoS、CC、爬虫与注入攻击
网络·爬虫·安全·ddos
潘潘的嵌入式日记18 小时前
一个宏在调试阶段挡住所有误操作
安全·嵌入式··调试·编译期·bring-up