Skip to content

Commit caa8206

Browse files
Amxxluiz-lvjarr00
authored
SafeERC20: reject returndata shorter than 32 bytes (#6896)
Co-authored-by: Luiz Vasconcelos Júnior <64055364+luiz-lvj@users.noreply.github.com> Co-authored-by: Arr00 <13561405+arr00@users.noreply.github.com>
1 parent 4be6645 commit caa8206

4 files changed

Lines changed: 87 additions & 6 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openzeppelin-solidity': patch
3+
---
4+
5+
`SafeERC20`: Treat non-empty token return data too short to decode as a `bool` as a failure.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8.27;
4+
5+
import {ERC20} from "../../token/ERC20/ERC20.sol";
6+
7+
abstract contract ERC20ShortReturnMock is ERC20 {
8+
error NotShortReturn();
9+
10+
uint256 internal immutable _zeroCount;
11+
12+
constructor(uint256 zeroCount) {
13+
require(zeroCount < 32, NotShortReturn());
14+
_zeroCount = zeroCount;
15+
}
16+
17+
function transfer(address to, uint256 amount) public override returns (bool) {
18+
super.transfer(to, amount);
19+
20+
uint256 returndatalength = _zeroCount;
21+
assembly ("memory-safe") {
22+
mstore(0, 0)
23+
return(0, returndatalength)
24+
}
25+
}
26+
27+
function transferFrom(address from, address to, uint256 amount) public override returns (bool) {
28+
super.transferFrom(from, to, amount);
29+
30+
uint256 returndatalength = _zeroCount;
31+
assembly ("memory-safe") {
32+
mstore(0, 0)
33+
return(0, returndatalength)
34+
}
35+
}
36+
37+
function approve(address spender, uint256 allowance) public override returns (bool) {
38+
super.approve(spender, allowance);
39+
40+
uint256 returndatalength = _zeroCount;
41+
assembly ("memory-safe") {
42+
mstore(0, 0)
43+
return(0, returndatalength)
44+
}
45+
}
46+
}

‎contracts/token/ERC20/utils/SafeERC20.sol‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ library SafeERC20 {
178178

179179
/**
180180
* @dev Imitates a Solidity `token.transfer(to, value)` call, relaxing the requirement on the return value: the
181-
* return value is optional (but if data is returned, it must not be false).
181+
* return value is optional (but if data is returned, it must decode to true).
182182
*
183183
* @param token The token targeted by the call.
184184
* @param to The recipient of the tokens
@@ -196,7 +196,7 @@ library SafeERC20 {
196196
success := call(gas(), token, 0, 0x00, 0x44, 0x00, 0x20)
197197
// if call success and return is true, all is good.
198198
// otherwise (not success or return is not true), we need to perform further checks
199-
if iszero(and(success, eq(mload(0x00), 1))) {
199+
if iszero(and(success, and(gt(returndatasize(), 0x1f), eq(mload(0x00), 1)))) {
200200
// if the call was a failure and bubble is enabled, bubble the error
201201
if and(iszero(success), bubble) {
202202
returndatacopy(fmp, 0x00, returndatasize())
@@ -213,7 +213,7 @@ library SafeERC20 {
213213

214214
/**
215215
* @dev Imitates a Solidity `token.transferFrom(from, to, value)` call, relaxing the requirement on the return
216-
* value: the return value is optional (but if data is returned, it must not be false).
216+
* value: the return value is optional (but if data is returned, it must decode to true).
217217
*
218218
* @param token The token targeted by the call.
219219
* @param from The sender of the tokens
@@ -239,7 +239,7 @@ library SafeERC20 {
239239
success := call(gas(), token, 0, 0x00, 0x64, 0x00, 0x20)
240240
// if call success and return is true, all is good.
241241
// otherwise (not success or return is not true), we need to perform further checks
242-
if iszero(and(success, eq(mload(0x00), 1))) {
242+
if iszero(and(success, and(gt(returndatasize(), 0x1f), eq(mload(0x00), 1)))) {
243243
// if the call was a failure and bubble is enabled, bubble the error
244244
if and(iszero(success), bubble) {
245245
returndatacopy(fmp, 0x00, returndatasize())
@@ -257,7 +257,7 @@ library SafeERC20 {
257257

258258
/**
259259
* @dev Imitates a Solidity `token.approve(spender, value)` call, relaxing the requirement on the return value:
260-
* the return value is optional (but if data is returned, it must not be false).
260+
* the return value is optional (but if data is returned, it must decode to true).
261261
*
262262
* @param token The token targeted by the call.
263263
* @param spender The spender of the tokens
@@ -275,7 +275,7 @@ library SafeERC20 {
275275
success := call(gas(), token, 0, 0x00, 0x44, 0x00, 0x20)
276276
// if call success and return is true, all is good.
277277
// otherwise (not success or return is not true), we need to perform further checks
278-
if iszero(and(success, eq(mload(0x00), 1))) {
278+
if iszero(and(success, and(gt(returndatasize(), 0x1f), eq(mload(0x00), 1)))) {
279279
// if the call was a failure and bubble is enabled, bubble the error
280280
if and(iszero(success), bubble) {
281281
returndatacopy(fmp, 0x00, returndatasize())

‎test/token/ERC20/utils/SafeERC20.test.js‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const name = 'ERC20Mock';
1010
const symbol = 'ERC20Mock';
1111
const value = 100n;
1212
const data = '0x12345678';
13+
const shortReturnLengths = [1n, 4n, 31n];
1314

1415
async function fixture() {
1516
const [hasNoCode, owner, receiver, spender, other] = await ethers.getSigners();
@@ -167,6 +168,35 @@ describe('SafeERC20', function () {
167168
shouldOnlyRevertOnErrors();
168169
});
169170

171+
for (const length of shortReturnLengths) {
172+
describe(`with token that returns ${length} zero bytes on all calls`, function () {
173+
beforeEach(async function () {
174+
this.token = await ethers.deployContract('$ERC20ShortReturnMock', [name, symbol, length]);
175+
// 15 zero bytes followed by 0x01: if the short return is completed with leftover calldata (the first address
176+
// argument), the return buffer reads as `true`.
177+
this.account = ethers.getAddress('0x' + '00'.repeat(15) + '01' + 'ab'.repeat(4));
178+
});
179+
180+
it('reverts on transfer', async function () {
181+
await expect(this.mock.$safeTransfer(this.token, this.account, 0n))
182+
.to.be.revertedWithCustomError(this.mock, 'SafeERC20FailedOperation')
183+
.withArgs(this.token);
184+
});
185+
186+
it('reverts on transferFrom', async function () {
187+
await expect(this.mock.$safeTransferFrom(this.token, this.account, this.receiver, 0n))
188+
.to.be.revertedWithCustomError(this.mock, 'SafeERC20FailedOperation')
189+
.withArgs(this.token);
190+
});
191+
192+
it('reverts on forceApprove', async function () {
193+
await expect(this.mock.$forceApprove(this.token, this.account, 0n))
194+
.to.be.revertedWithCustomError(this.mock, 'SafeERC20FailedOperation')
195+
.withArgs(this.token);
196+
});
197+
});
198+
}
199+
170200
describe('with usdt approval behaviour', function () {
171201
beforeEach(async function () {
172202
this.token = this.erc20ForceApproveMock;

0 commit comments

Comments
 (0)