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

Reduce fps pess strange setter #123

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions docs/strange_setter.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
* Confidence: `Medium`

## Description
The detector sees if a contract contains a setter (also constructor) that does not change contract storage variables.
Setter functions MUST change the values of storage variables.
Setter functions that do not modify storage variables may lead to contract misfunctions.
The detector sees if a contract contains a setter (also constructor) that does not change contract storage variables or does not perform external calls using provided arguments.
Setter functions MUST change the values of storage variables or perform external calls using provided parameters.
Setter functions that do not use provided variables may lead to contract misfunctions.

### Potential Improvement
Remove highlights of mappings.
Detect shadowing before storage update/external call

## Vulnerable Scenario
[test scenario](../tests/strange_setter_test.sol)

## Recommendation
Make sure that setter functions modify the states of storage variables.
Make sure that setter functions modify the states of storage variables or performs external call using provided arguments.
44 changes: 39 additions & 5 deletions slitherin/detectors/strange_setter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
from slither.utils.output import Output
from slither.detectors.abstract_detector import AbstractDetector, DetectorClassification
from slither.core.declarations import Function
from slither.analyses.data_dependency.data_dependency import is_dependent, is_tainted


class StrangeSetter(AbstractDetector):
"""
Sees if contract contains a setter, that does not change contract storage variables.
Sees if contract contains a setter, that does not change contract storage variables or that does not use arguments for an external call.
"""

ARGUMENT = "pess-strange-setter" # slither will launch the detector with slither.py --detect mydetector
Expand All @@ -18,7 +19,7 @@ class StrangeSetter(AbstractDetector):
"https://github.com/pessimistic-io/slitherin/blob/master/docs/strange_setter.md"
)
WIKI_TITLE = "Strange Setter"
WIKI_DESCRIPTION = "Setter must write to storage variables"
WIKI_DESCRIPTION = "Setter must write to storage variables or pass arguments to external calls"
WIKI_EXPLOIT_SCENARIO = "-"
WIKI_RECOMMENDATION = "Make sure that your setter actually sets something"

Expand All @@ -30,6 +31,7 @@ def _is_strange_setter(self, fun: Function) -> bool:
if not fun.parameters:
# nothing is in the params, so we don't care
return False
used_params = set()
for (
fin
) in fun.internal_calls: # branch with for-loop for setters in internal calls
Expand All @@ -39,13 +41,45 @@ def _is_strange_setter(self, fun: Function) -> bool:
if n.state_variables_written and str(param) in str(
n
): # check if there's a state variable setter using function parameters
return False
used_params.add(param)
for param in fun.parameters:
if fun.state_variables_written:
for n in fun.nodes:
if str(param) in str(n):
return False
return True
used_params.add(param)
for param in fun.parameters:
for external in fun.external_calls_as_expressions:
for arg in [*external.arguments, external._called._expression]:
if str(arg) == str(param):
used_params.add(param)
intersection_len = len(set(fun.parameters) & used_params)
print(intersection_len, len(fun.parameters), fun.name)
return intersection_len != len(fun.parameters)

# def _is_strange_setter(self, fun: Function) -> bool:
# """Checks if setter sets smth to a storage variable and if function parameters are used when setting"""
# if not isinstance(fun, Function):
# return True

# if not fun.parameters:
# # nothing is in the params, so we don't care
# return False
# for (
# fin
# ) in fun.internal_calls: # branch with for-loop for setters in internal calls
# if isinstance(fin, Function):
# for param in fin.parameters:
# for n in fin.nodes:
# if n.state_variables_written and str(param) in str(
# n
# ): # check if there's a state variable setter using function parameters
# return False
# for param in fun.parameters:
# if fun.state_variables_written:
# for n in fun.nodes:
# if str(param) in str(n):
# return False
# return True

def _is_strange_constructor(self, fun: Function) -> bool:
"""Checks if constructor sets nothing"""
Expand Down
13 changes: 12 additions & 1 deletion tests/strange_setter_test.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
pragma solidity ^0.8.0;

interface ExternalContract {
function set(bool arg) external;
}
// What it should detect:
// If smth is set in the function, and the function contains parameters,
// and this parameters were not uset to set.
Expand Down Expand Up @@ -37,10 +40,18 @@ contract StrangeSetter {
}

function setWithInt(bytes32 nameHash, address builder) public onlyOwner {
uint256 x = 0; //TODO this is not detected. There are params which are not used
uint256 x = 0;
vulnurable_internal(x);
}

function setSwapEnabledExternal_ok(ExternalContract target, bool swapEnabled) external onlyOwner {
target.set(swapEnabled);
}

function setUseOnlyOneArg_vulnerable(uint256 arg1, bool isProtectedArg) external onlyOwner {
isProtected = isProtectedArg;
}

function set_ok(uint256 setter) public onlyOwner {
toSet = setter;
}
Expand Down
Loading