Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommend reading variable without additional STATICCALL #1484

Merged
merged 6 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions slither/detectors/all_detectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .variables.uninitialized_state_variables import UninitializedStateVarsDetection
from .variables.uninitialized_storage_variables import UninitializedStorageVars
from .variables.uninitialized_local_variables import UninitializedLocalVars
from .variables.var_read_using_this import VarReadUsingThis
from .attributes.constant_pragma import ConstantPragma
from .attributes.incorrect_solc import IncorrectSolc
from .attributes.locked_ether import LockedEther
Expand Down
56 changes: 56 additions & 0 deletions slither/detectors/variables/var_read_using_this.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from typing import List
from slither.core.cfg.node import Node
from slither.core.declarations import Function, SolidityVariable
from slither.detectors.abstract_detector import AbstractDetector, DetectorClassification
from slither.slithir.operations.high_level_call import HighLevelCall


class VarReadUsingThis(AbstractDetector):
ARGUMENT = "var-read-using-this"
HELP = "Contract reads its own variable using `this`"
IMPACT = DetectorClassification.OPTIMIZATION
CONFIDENCE = DetectorClassification.MEDIUM
0xalpharush marked this conversation as resolved.
Show resolved Hide resolved

WIKI = "https://github.com/trailofbits/slither-private/wiki/Vulnerabilities-Description#var-read-using-this"

WIKI_TITLE = "Variable read using this"
WIKI_DESCRIPTION = "Contract reads its own variable using `this`, adding overhead of an unnecessary STATICCALL."
WIKI_EXPLOIT_SCENARIO = """
```solidity
contract C {
mapping(uint => address) public myMap;
function test(uint x) external returns(address) {
return this.myMap(x);
}
}
```
"""

WIKI_RECOMMENDATION = "Read the variable directly from storage instead of calling the contract."

def _detect(self):
results = []
for c in self.contracts:
for func in c.functions:
for node in self._detect_var_read_using_this(func):
info = [
"The function ",
func,
" reads ",
node,
" with `this` which adds an extra STATICALL.\n",
]
json = self.generate_result(info)
results.append(json)

return results

@staticmethod
def _detect_var_read_using_this(func: Function) -> List[Node]:
results: List[Node] = []
for node in func.nodes:
for ir in node.irs:
if isinstance(ir, HighLevelCall):
if ir.destination == SolidityVariable("this") and ir.is_static_call():
results.append(node)
return results
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

contract VarReadUsingThis {
address public erc20;
mapping(uint => address) public myMap;
function bad1(uint x) external returns(address) {
return this.myMap(x);
}
function bad2() external returns(address) {
return this.erc20();
}
function bad3() external returns(address) {
if (this.erc20() == address(0)) revert();
}
function bad4() internal returns(address) {
for (uint x; x < 10; x++) {
address local = this.erc20();
}
}
function good1(uint x) external returns(address) {
return myMap[x];
}
function good2() external returns(address) {
return erc20;
}
function good3() external returns(address) {
if (erc20 == address(0)) revert();
}
function good4() internal returns(address) {
for (uint x; x < 10; x++) {
address local = erc20;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
[]
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

contract VarReadUsingThis {
address public erc20;
mapping(uint => address) public myMap;
function bad1(uint x) external returns(address) {
return this.myMap(x);
}
function bad2() external returns(address) {
return this.erc20();
}
function bad3() external returns(address) {
if (this.erc20() == address(0)) revert();
}
function bad4() internal returns(address) {
for (uint x; x < 10; x++) {
address local = this.erc20();
}
}
function good1(uint x) external returns(address) {
return myMap[x];
}
function good2() external returns(address) {
return erc20;
}
function good3() external returns(address) {
if (erc20 == address(0)) revert();
}
function good4() internal returns(address) {
for (uint x; x < 10; x++) {
address local = erc20;
}
}
}
Loading