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);
}
相关推荐
Guheyunyi7 小时前
智能守护:视频安全监测系统的演进与未来
大数据·人工智能·科技·安全·信息可视化
Traced back7 小时前
WinForms 线程安全三剑客详解
安全·c#·winform
汉堡包0017 小时前
【网安基础】--内网代理转发基本流程(正向与反向代理)
安全·web安全·php
桌面运维家9 小时前
vDisk VOI/IDV权限管理怎么做?安全方案详解
安全
世界尽头与你10 小时前
(修复方案)kibana 未授权访问漏洞
安全·网络安全·渗透测试
xixixi7777711 小时前
今日 AI 、通信、安全行业前沿日报(2026 年 2 月 4 日,星期三)
大数据·人工智能·安全·ai·大模型·通信·卫星通信
蓝队云计算12 小时前
蓝队云部署OpenClaw深度指南:避坑、优化与安全配置,从能用做到好用
运维·安全·云计算
lingggggaaaa12 小时前
安全工具篇&Go魔改二开&Fscan扫描&FRP代理&特征消除&新增扩展&打乱HASH
学习·安全·web安全·网络安全·golang·哈希算法
Black蜡笔小新14 小时前
国密GB35114平台EasyGBS筑牢安防安全防线,GB28181/GB35114无缝接入
网络·安全·音视频·gb35114
2401_8658548814 小时前
安全为先|咕噜分发平台:一站式App漏洞分析与安全分发解决方案
安全