Skip to content

Commit 520e81c

Browse files
bechedh1kk4
andauthored
Adding the exact balance check rule (#20)
* Adding the exact balance check rule * Fix the exact balance check rule * Fix the taint mode of the exact balance check rule * Another fix for the exact balance check rule * extend if pattern * Fixed the taint mode and separated the if branch detection without using the taint mode * Remove the redundant pattern in the exact balance check rule * Redo everything again based on the h1kk4's commit, extend it to cover more cases --------- Co-authored-by: h1kk4 <[email protected]>
1 parent f1f053f commit 520e81c

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public-transfer-fees-supporting-tax-tokens | LeetSwap | public _transferFeesSupp
133133
olympus-dao-staking-incorrect-call-order | OlympusDAO, FloorDAO, Heavens Gate, Jump Farm, QuantumWN | The order of calling the transferFrom() and rebase() functions is incorrect in Olympus DAO forks
134134
compound-precision-loss | Hundred Finance, Midas Finance, Onyx Protocol | In Compound forks if there is a market with totalSupply = 0 and collateralFactor != 0 a precision loss attack is possible if redeemAmount is taken from the arguments of redeemFresh()
135135
thirdweb-vulnerability | Swopple Token, TIME Token, NAME Token, HXA Token | In contracts that support Multicall and ERC2771Context an Arbitrary Address Spoofing attack is possible
136+
exact-balance-check | Generic | Testing the balance of an account as a basis for some action has risks associated with unexpected receipt of ether or another token, including tokens deliberately transfered to cause such tests to fail, as an MEV attack.
136137

137138
## Gas Optimization Rules
138139

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
contract Foobar {
2+
function doit1(address ext) {
3+
uint256 bal = IERC20(ext).balanceOf(address(this));
4+
// ok: exact-balance-check
5+
bal == 1338;
6+
require(
7+
1==1 &&
8+
// ruleid: exact-balance-check
9+
(bal == 1337) ||
10+
1==2,
11+
"Wrong balance!"
12+
);
13+
// do smth
14+
}
15+
16+
function doit2(address ext) {
17+
require(
18+
1==1 &&
19+
// ruleid: exact-balance-check
20+
(address(ext).balance == 1337) ||
21+
1==2,
22+
"Wrong balance!"
23+
);
24+
// do smth
25+
}
26+
27+
function doit3(address ext) {
28+
require(
29+
1==1 &&
30+
// ruleid: exact-balance-check
31+
(1337 == address(ext).balance) ||
32+
1==2,
33+
"Wrong balance!"
34+
);
35+
// do smth
36+
}
37+
38+
function doit4(address ext) {
39+
// ruleid: exact-balance-check
40+
if (address(ext).balance == 1337 && 1 == 1) {
41+
// do smth
42+
uint a = 123;
43+
};
44+
// do smth
45+
}
46+
47+
function doit5(address ext) {
48+
// ok: exact-balance-check
49+
if (1==1) {
50+
// ruleid: exact-balance-check
51+
bool b = address(ext).balance == 1337;
52+
if(b) {}
53+
};
54+
// do smth
55+
}
56+
57+
function doit_safe(address ext) {
58+
require(
59+
1==1 &&
60+
// ok: exact-balance-check
61+
(IERC20(ext).balanceOf(address(this)) >= 1337) ||
62+
1==2,
63+
"Wrong balance!"
64+
);
65+
// do smth
66+
}
67+
68+
function doit2_safe(address ext) {
69+
require(
70+
1==1 &&
71+
// ok: exact-balance-check
72+
(address(ext).balance <= 1337) ||
73+
1==2,
74+
"Wrong balance!"
75+
);
76+
// do smth
77+
}
78+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
rules:
2+
- id: exact-balance-check
3+
message: Testing the balance of an account as a basis for some action has risks
4+
associated with unexpected receipt of ether or another token, including
5+
tokens deliberately transfered to cause such tests to fail, as an MEV
6+
attack.
7+
metadata:
8+
category: security
9+
technology:
10+
- solidity
11+
cwe: "CWE-667: Improper Locking"
12+
confidence: LOW
13+
likelihood: MEDIUM
14+
impact: MEDIUM
15+
subcategory:
16+
- vuln
17+
references:
18+
- https://entethalliance.org/specs/ethtrust-sl/v1/#req-1-exact-balance-check
19+
mode: taint
20+
pattern-sinks:
21+
- pattern-either:
22+
- patterns:
23+
- pattern-either:
24+
- pattern: $X == ...
25+
- pattern: ... == $X
26+
- pattern-either:
27+
- pattern-inside: require(...)
28+
- pattern-inside: |
29+
if (<... $X == ... ...>) {
30+
...
31+
}
32+
- pattern-inside: |
33+
if (<... ... == $X ...>) {
34+
...
35+
}
36+
- patterns:
37+
- pattern-either:
38+
- pattern: $BOOL = <... ... == ... ...>
39+
- pattern-either:
40+
- pattern-inside: |
41+
...
42+
require(<... $BOOL ...>)
43+
- pattern-inside: |
44+
...
45+
if (<... $BOOL ...>) {
46+
...
47+
}
48+
pattern-sources:
49+
- pattern-either:
50+
- pattern: $T.balanceOf($A)
51+
- pattern: $T.balance
52+
languages:
53+
- solidity
54+
severity: WARNING

0 commit comments

Comments
 (0)