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);
}
相关推荐
00后程序员张1 小时前
iOS App 混淆与资源保护:iOS配置文件加密、ipa文件安全、代码与多媒体资源防护全流程指南
android·安全·ios·小程序·uni-app·cocoa·iphone
Ginkgo_Lo3 小时前
【LLM越狱】AI大模型DRA攻击解读与复现
人工智能·安全·ai·语言模型
爱思德学术4 小时前
中国计算机学会(CCF)推荐学术会议-B(数据库/数据挖掘/内容检索):DASFAA 2026
数据库·区块链·数据管理·数据库系统
七牛云行业应用4 小时前
企业级AI大模型选型指南:从评估部署到安全实践
大数据·人工智能·安全
CV-杨帆7 小时前
大模型在题目生成中的安全风险研究综述
人工智能·深度学习·安全
猫耳君7 小时前
汽车功能安全 Functional Safety ISO 26262 测试之一
测试开发·安全·汽车·功能安全·汽车测试·汽车电子测试
ZYMFZ8 小时前
Linux系统 SELinux 安全管理与故障排查
linux·运维·安全
BillKu8 小时前
Vue3 中使用 DOMPurify 对渲染动态 HTML 进行安全净化处理
前端·安全·html
taxunjishu10 小时前
DeviceNet 转 EtherCAT:发那科焊接机器人与倍福 CX5140 在汽车焊装线的高速数据同步通讯配置案例
人工智能·区块链·工业物联网·工业自动化·总线协议
云边云科技20 小时前
门店网络重构:告别“打补丁”,用“云网融合”重塑数字竞争力!
大数据·人工智能·安全·智能路由器·零售