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

In implement of signature, no return check and no use nonce. #1516

Closed
wants to merge 1 commit 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
6 changes: 3 additions & 3 deletions slither/core/solidity_types/elementary_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
"int256",
]

Max_Int = {k: 2 ** (8 * i - 1) - 1 if i > 0 else 2**255 - 1 for i, k in enumerate(Int)}
Min_Int = {k: -(2 ** (8 * i - 1)) if i > 0 else -(2**255) for i, k in enumerate(Int)}
Max_Int = {k: 2 ** (8 * i - 1) - 1 if i > 0 else 2 ** 255 - 1 for i, k in enumerate(Int)}
Min_Int = {k: -(2 ** (8 * i - 1)) if i > 0 else -(2 ** 255) for i, k in enumerate(Int)}

Uint = [
"uint",
Expand Down Expand Up @@ -82,7 +82,7 @@
"uint256",
]

Max_Uint = {k: 2 ** (8 * i) - 1 if i > 0 else 2**256 - 1 for i, k in enumerate(Uint)}
Max_Uint = {k: 2 ** (8 * i) - 1 if i > 0 else 2 ** 256 - 1 for i, k in enumerate(Uint)}
Min_Uint = {k: 0 for k in Uint}


Expand Down
1 change: 1 addition & 0 deletions slither/detectors/all_detectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,4 @@
from .statements.delegatecall_in_loop import DelegatecallInLoop
from .functions.protected_variable import ProtectedVariables
from .functions.permit_domain_signature_collision import DomainSeparatorCollision
from .statements.ecrecover import Ecrecover
12 changes: 8 additions & 4 deletions slither/detectors/naming_convention/naming_convention.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,14 @@ def _detect(self): # pylint: disable=too-many-branches,too-many-statements
if func.is_constructor:
continue
if not self.is_mixed_case(func.name):
if func.visibility in [
"internal",
"private",
] and self.is_mixed_case_with_underscore(func.name):
if (
func.visibility
in [
"internal",
"private",
]
and self.is_mixed_case_with_underscore(func.name)
):
continue
if func.name.startswith(("echidna_", "crytic_")):
continue
Expand Down
104 changes: 104 additions & 0 deletions slither/detectors/statements/ecrecover.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
"""
Module detecting improper use of ecrecover.

"""

from slither.detectors.abstract_detector import AbstractDetector, DetectorClassification
from slither.slithir.operations import Binary, BinaryType
from slither.slithir.operations.solidity_call import SolidityCall


class Ecrecover(AbstractDetector):
"""
Detect improper use of ecrecover
"""

ARGUMENT = "ecrecover"
HELP = "Return value of ecrecover is not checked. And Signature does not contain a nonce."
IMPACT = DetectorClassification.MEDIUM
CONFIDENCE = DetectorClassification.LOW

WIKI = "https://github.com/crytic/slither/wiki/Detector-Documentation#ecrecover"
WIKI_TITLE = "ECRECOVER"
WIKI_DESCRIPTION = "ECRECOVER"
WIKI_EXPLOIT_SCENARIO = """
```solidity
contract A {
struct Info {
uint tokenId;
address owner;
// uint nonce
}
uint maxBreed = 5;
mapping (uint => mapping(uint => address)) list;
mapping (uint => uint) count;
function mint(uint tokenId, address addr) internal {
require(count[tokenId] < maxBreed, "");
list[tokenId][count[tokenId]] = addr;
count[tokenId]++;
}
function verify(Info calldata info, uint8 v, bytes32 r, bytes32 s) external {
bytes32 hash = keccak256(abi.encode(info));
bytes32 data =
keccak256(
abi.encodePacked("\x19Ethereum Signed Message:\n32", hash)
);
address receiver = ecrecover(data, v, r, s);
// require(signer != address(0), "ECDSA: invalid signature");
mint(info.tokenId, receiver);
}
}
```
First, signature does not contain nonce.
Second, there is no verification of ecrecover's return value.
"""
WIKI_RECOMMENDATION = "Check return value of ecrecover and signature contains a nonce"

def _detect(self):

results = []

for contract in self.compilation_unit.contracts:
for function in contract.functions:
for call in function.solidity_calls:
if call.name == "ecrecover(bytes32,uint8,bytes32,bytes32)":
flag = 0
for vari in function._variables:
if "nonce" in vari.lower():
flag = 1
if flag == 0:
info = [
"No nonce check found in ",
function,
"\n",
]
res = self.generate_result(info)
results.append(res)
flag = 0

find_erecovery = 0
for node in function.nodes:

for ir in node.irs:
if find_erecovery == 1:
if isinstance(ir, Binary):
if (ir.type == BinaryType.NOT_EQUAL) or (
ir.type == BinaryType.EQUAL
):
flag = 1
else:
if (
isinstance(ir, SolidityCall)
and ir.function.name
== "ecrecover(bytes32,uint8,bytes32,bytes32)"
):
find_erecovery = 1
if flag == 0:
info = [
"No return check found in ",
function,
"\n",
]
res = self.generate_result(info)
results.append(res)
return results
2 changes: 1 addition & 1 deletion slither/detectors/statements/type_based_tautology.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
def typeRange(t):
bits = int(t.split("int")[1])
if t in Uint:
return 0, (2**bits) - 1
return 0, (2 ** bits) - 1
if t in Int:
v = (2 ** (bits - 1)) - 1
return -v, v
Expand Down
4 changes: 2 additions & 2 deletions slither/slithir/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ def _fits_under_integer(val: int, can_be_int: bool, can_be_uint) -> List[str]:
assert can_be_int | can_be_uint
while n <= 256:
if can_be_uint:
if val <= 2**n - 1:
if val <= 2 ** n - 1:
ret.append(f"uint{n}")
if can_be_int:
if val <= (2**n) / 2 - 1:
if val <= (2 ** n) / 2 - 1:
ret.append(f"int{n}")
n = n + 8
return ret
Expand Down
2 changes: 1 addition & 1 deletion slither/utils/integer_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def convert_string_to_fraction(val: Union[str, int]) -> Fraction:
f"{base}e{expo} is too large to fit in any Solidity integer size"
)
return 0
return Fraction(base) * Fraction(10**expo)
return Fraction(base) * Fraction(10 ** expo)

return Fraction(val)

Expand Down
2 changes: 1 addition & 1 deletion slither/visitors/expression/constants_folding.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _post_binary_operation(self, expression):
left = get_val(expression.expression_left)
right = get_val(expression.expression_right)
if expression.type == BinaryOperationType.POWER:
set_val(expression, left**right)
set_val(expression, left ** right)
elif expression.type == BinaryOperationType.MULTIPLICATION:
set_val(expression, left * right)
elif expression.type == BinaryOperationType.DIVISION:
Expand Down
25 changes: 25 additions & 0 deletions tests/detectors/ecrecover/0.8.0/ecrecover.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
contract A {
struct Info {
uint tokenId;
address owner;
// uint nonce
}
uint maxBreed = 5;
mapping (uint => mapping(uint => address)) list;
mapping (uint => uint) count;
function mint(uint tokenId, address addr) internal {
require(count[tokenId] < maxBreed, "");
list[tokenId][count[tokenId]] = addr;
count[tokenId]++;
}
function verify(Info calldata info, uint8 v, bytes32 r, bytes32 s) external {
bytes32 hash = keccak256(abi.encode(info));
bytes32 data =
keccak256(
abi.encodePacked("\x19Ethereum Signed Message:\n32", hash)
);
address receiver = ecrecover(data, v, r, s);
// require(signer != address(0), "ECDSA: invalid signature");
mint(info.tokenId, receiver);
}
}
168 changes: 168 additions & 0 deletions tests/detectors/ecrecover/0.8.0/ecrecover.sol.0.8.0.Ecrecover.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
[
[
{
"elements": [
{
"type": "function",
"name": "verify",
"source_mapping": {
"start": 401,
"length": 432,
"filename_relative": "tests/detectors/ecrecover/0.8.0/ecrecover.sol",
"filename_absolute": "/GENERIC_PATH",
"filename_short": "tests/detectors/ecrecover/0.8.0/ecrecover.sol",
"is_dependency": false,
"lines": [
15,
16,
17,
18,
19,
20,
21,
22,
23,
24
],
"starting_column": 5,
"ending_column": 6
},
"type_specific_fields": {
"parent": {
"type": "contract",
"name": "A",
"source_mapping": {
"start": 0,
"length": 835,
"filename_relative": "tests/detectors/ecrecover/0.8.0/ecrecover.sol",
"filename_absolute": "/GENERIC_PATH",
"filename_short": "tests/detectors/ecrecover/0.8.0/ecrecover.sol",
"is_dependency": false,
"lines": [
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26
],
"starting_column": 1,
"ending_column": 0
}
},
"signature": "verify(A.Info,uint8,bytes32,bytes32)"
}
}
],
"description": "No nonce check found in A.verify(A.Info,uint8,bytes32,bytes32) (tests/detectors/ecrecover/0.8.0/ecrecover.sol#15-24)\n",
"markdown": "No nonce check found in [A.verify(A.Info,uint8,bytes32,bytes32)](tests/detectors/ecrecover/0.8.0/ecrecover.sol#L15-L24)\n",
"first_markdown_element": "tests/detectors/ecrecover/0.8.0/ecrecover.sol#L15-L24",
"id": "27569155b0bbd34ebee696a624ae9cc839594b156c87544fdb59975326105b18",
"check": "ecrecover",
"impact": "Medium",
"confidence": "Low"
},
{
"elements": [
{
"type": "function",
"name": "verify",
"source_mapping": {
"start": 401,
"length": 432,
"filename_relative": "tests/detectors/ecrecover/0.8.0/ecrecover.sol",
"filename_absolute": "/GENERIC_PATH",
"filename_short": "tests/detectors/ecrecover/0.8.0/ecrecover.sol",
"is_dependency": false,
"lines": [
15,
16,
17,
18,
19,
20,
21,
22,
23,
24
],
"starting_column": 5,
"ending_column": 6
},
"type_specific_fields": {
"parent": {
"type": "contract",
"name": "A",
"source_mapping": {
"start": 0,
"length": 835,
"filename_relative": "tests/detectors/ecrecover/0.8.0/ecrecover.sol",
"filename_absolute": "/GENERIC_PATH",
"filename_short": "tests/detectors/ecrecover/0.8.0/ecrecover.sol",
"is_dependency": false,
"lines": [
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26
],
"starting_column": 1,
"ending_column": 0
}
},
"signature": "verify(A.Info,uint8,bytes32,bytes32)"
}
}
],
"description": "No return check found in A.verify(A.Info,uint8,bytes32,bytes32) (tests/detectors/ecrecover/0.8.0/ecrecover.sol#15-24)\n",
"markdown": "No return check found in [A.verify(A.Info,uint8,bytes32,bytes32)](tests/detectors/ecrecover/0.8.0/ecrecover.sol#L15-L24)\n",
"first_markdown_element": "tests/detectors/ecrecover/0.8.0/ecrecover.sol#L15-L24",
"id": "de38bde3cbf7427267ce079a656d9e99c3b2126bdc6b576b05e94df2357dd5d4",
"check": "ecrecover",
"impact": "Medium",
"confidence": "Low"
}
]
]
5 changes: 5 additions & 0 deletions tests/test_detectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1557,6 +1557,11 @@ def id_test(test_item: Test):
"permit_domain_state_var_collision.sol",
"0.8.0",
),
Test(
all_detectors.Ecrecover,
"ecrecover.sol",
"0.8.0",
),
]


Expand Down