Open ailisp opened 4 years ago
I am going to close this issue to not confuse people. @ailisp , @Kouprin let's go over the audits and create issues for each of the findings that were not addressed yet. @ailisp , thank you for drafting the document!
@Kouprin I found i only created issues for the concensys report, the solidity report and today's fuzz test report is missing, I think they maybe more suitable to create as one big issue than a few smaller issues.
@ailisp Thank you for converting this into a checkpoint list.
According @nearmax idea about having the only assigned person, I withdraw my assignment.
Might require quite some time to address and test all of them. Setting estimate to 3.
For the record, as discussed @ailisp will address Major and Critical flaws (except ED25519) and @abacabadabacaba will take over the rest.
2. ProofDecoder.sol (6993b73)
In this section we describe issues related to the smart contract defined in the ProofDecoder.sol .
2.1 Moderate Flaws
This section lists moderate flaws, which were found in the smart contract.
Bo: This is impossible, given:
And logs.length is not possible to exceed
2^32-1
in nearcore given the gas limit, am i correct? @nearmax @bowenwang19962.2 Suboptimal Code
This section lists suboptimal code patterns, which were found in the smart contract.
[ ] 1. Line 49-51: the fields structure is packed compactly into 32-byte words, but never crosses word boundary. Actually, 29 padding bytes will be added after these fields. Consider reordering fields to avoid padding.
[ ] 2. Line 60, 65: next assignments are redundant: ● executionStatus.unknown=trueasenumIndex==0 implies unknown == true. ● executionStatus.failed=trueasenumIndex==1implies failed == true.
[ ] 3. Line 62: the if statement is indented, but is independent from the previous one. Actually it is inside the else branch of the previous if statement. Consider changing indentation.
[ ] 4. Line 85: all but the first element ExecutionOutcome struct seem to be just hash of logs. So it does not make much sense to store and pass those together with logs. Probably one of them can be dropped.
[ ] 5. Line 140: it would be more efficient to store all directions as a bit mask in a single word. This word could be treated as a position of a leaf in a Merkle tree.
2.3 Other Issues
This section lists other minor issues which were found in the token smart contract.
[ ] 1. Line 59, 62, 6 7, 7 0: assignment values 0, 1, 2, 3 should be named constants.
[ ] 2. Line 106-108: the code would be more readable, less error-prone, and more efficient in case if the peekSha256 would accept to parameters: offset and length, instead of just length.
[ ] 3. Line 123: using the same name as the outcome.outcome for different things inside one sentence is confusing. Consider using different naming.
3. NearProver.sol (3563521)
In this section we describe issues related to the smart contract defined in the NearProver.sol.
3.1 Suboptimal Code
This section lists suboptimal code patterns, which were found in the smart contract.
3.2 Other Issues
This section lists other minor issues which were found in the token smart contract.
4. NearBridgeMock.sol ( b92a80f)
In this section we describe issues related to the smart contract defined in the NearBridgeMock.sol .
4.1 Suboptimal Code
This section lists suboptimal code patterns, which were found in te smart contract.
5. NearDecoder.sol (4229cd7)
In this section we describe issues related to the smart contract defined in the NearDecoder.sol .
5.1 Suboptimal Code
This section lists suboptimal code patterns, which were found in the smart contract.
5.2 Other Issues
This section lists other minor issues which were found in the token smart contract.
6. Borsh.sol (6993b73)
In this section we describe issues related to the smart contract defined in the Borsh.sol .
6.1 Critical Flaws
This section lists critical flaws, which were found in the smart contract.
[x] Line 70: the int16 c onversion extends sign bit, so in case the first call to decodeI8(data) returned negative value, the high 8 bits of the result will be filled with ones regardless of what the second call to decodeI8(data) will return.
6.2 Arithmetic Overflow Issues
This section lists issues of the smart contract related to the arithmetic overflows.
[x] Line 23, 39: the overflow possible in these operations: ● data.offset+size ● add,add
6.3 Suboptimal Code
This section lists suboptimal code patterns, which were found in the smart contract.
[ ] 1. Line 32, 36, 4 3, 4 7: there is no range check for: ● the length parameter. In case length > data.raw.length - data.offset it will read memory after the data. ● the offset parameter. In case offset + length > ptr.length it will read memory after the data.
[ ] 2. Line 39, 51: the memory range may wrap in case l ength > 2^256 - add(add(ptr, 32), offset).
[ ] 3. Line 51: the pop operation ignores execution status of SHA256 precompiled smart contract. While this contract shouldn't fail in current implementation, it is still a bad practice to ignore such things.
[ ] 4. Line 56: the shift modifier changes the data. The function should not be declared as pure.
[ ] 5. Line 66: the calling decodeU8 function twice inside one function is suboptimal, as range check is performed twice, and offset is also updated twice. Consider implementing decodeU16 function without references to decodeU8.
[ ] 6. Line 104: the decodeU256 function is suboptimal as well as other similar functions, as it reverses byte ordering one by one and performs many internal function calls. Consider reading 32 bytes with a single mload, and then reverting byte ordering in 8 rounds similar to how this is done in Ed25519.sol.
[ ] 7. Line 115: the decodeU8(data) != 0 allows many different encodings for true value. Enforcing only one valid encoding for true would make code less error prone.
[ ] 8. Line 120: the copying byte one by one is suboptimal. Consider copying in 32-byte words when possible.
[ ] 9. Line 135: the 20 byte could be read using single mload opcode. Reading them one by one is suboptimal.
[ ] 10. Line 136: the bytes20 will internally shift value left, just to then shift it right.
6.4 Unclear Behaviour
This section lists issues of the smart contract, where the contract behavior is unclear: the business logic might be violated here, but the documentation and functional requirements are not sufficiently documented to make a clear decision.
[ ] Line 147: the decodeSECP256K1PublicKey function doesn't check that decoded coordinates fit into field order, and doesn't check that decoded point actually lays on the curve. Is it OK or not?.
6.5 Other Issues
This section lists other minor issues which were found in the token smart contract.
[ ] 1. Line 3: the SafeMath.sol file imported but not used.
[ ] 2. Line 6: the library name Borsh is confusing. Consider using more descriptive name.
[ ] 3. Line 22: the modifier shift does not shift the data, but rather consumes certain number of first bytes. Consider renaming to consumes.
[ ] 4. Line 64: the documentation comment for the decodeU16 function needed. It should be said that the function does the integer reading in little endian.
[ ] 5. Line 136: the operation & 0xFF seems redundant.
7. NearBridge.sol (4031aa8)
In this section we describe issues related to the smart contract defined in the NearBridge.sol.
7.1 Moderate Flaws
This section lists moderate flaws, which were found in the smart contract.
7.4 Suboptimal Code
This section lists suboptimal code patterns, which were found in the smart contract.
[ ] 8. Line 76: withdraw is possible when block.timestamp > last.validAfter, challenge is possible when block.timestamp < last.validAfter, and adding light client block is possible when block.timestamp >= last.validAfter, so situation when block.timestamp == last.validAfter differs from both block.timestamp > last.validAfter and block.timestamp < last.validAfter. Consider simplifying.
7.5 Unclear Behaviour
This section lists issues of the smart contract, where the contract behavior is unclear: the business logic might be violated here, but the documentation and functional requirements are not sufficiently documented to make a clear decision.
7.6 Other Issues
This section lists other minor issues which were found in the token smart contract.
8. INearBridge.sol (1b1f5fd)
In this section we describe issues related to the smart contract defined in the INearBridge.sol (1b1f5fd).
8.1 Documentation Issues
This section lists documentation issues, which were found in the smart contract
8.2 Other Issues
This section lists other minor issues which were found in the token smart contract.
Summary
Based on our findings, we also recommend the following:
[x] 1. Fix critical issues which could lead to incorrect checks.
[x] 2. Pay attention to moderate issues.
[x] 3. Fix arithmetic overflow issues.
[ ] 4. Check issues marked “unclear behavior” against functional requirements.
[ ] 5. Refactor the code to remove suboptimal parts.
[ ] 6. Fix the documentation, readability and other (minor) issues.