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

fix(contracts): fix contract audit findings #1254

Merged
merged 5 commits into from
Jan 23, 2025
Merged

fix(contracts): fix contract audit findings #1254

merged 5 commits into from
Jan 23, 2025

Conversation

cryptoAtwill
Copy link
Contributor

@cryptoAtwill cryptoAtwill commented Jan 20, 2025

This PR addresses the review audits.

Fixes

Mediums:

  • M1: Addressed

Lows:

  • L1: I dont think we should address this. The subnet does not really know if the destination subnet exists. Caller should be aware.
  • L2: Updated, this one is known to have created tons of confusion.

Informationals:

  • I1: Addressed
  • I2: Addressed
  • I3: Addressed
  • I5: Addressed
  • I6: Addressed
  • I8: Addressed

To discuss

  • L3: We should taggle gas_limit in cross message transactions, but the issue is more complex than described. Maybe delay to another issue?
  • I4: We should discuss if we need this, but I think it’s a good practice
  • I7: I dont see the line mentioned, maybe the codebase is different

Sorry, something went wrong.

@cryptoAtwill cryptoAtwill requested a review from a team as a code owner January 20, 2025 10:24
@raulk raulk changed the title fix(node): update contract audit fix(contracts): update contract audit Jan 20, 2025
@raulk raulk changed the title fix(contracts): update contract audit fix(contracts): fix contract audit findings Jan 20, 2025
@karlem
Copy link
Contributor

karlem commented Jan 20, 2025

@cryptoAtwill I think it might be useful to add a sentence to each point in the list if possible. It is not clear from here what is being fixed.

@@ -27,6 +27,8 @@ contract GatewayManagerFacet is GatewayActorModifiers, ReentrancyGuard {
using AssetHelper for Asset;
using EnumerableSet for EnumerableSet.Bytes32Set;

event SubnetKilled(SubnetID id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's name this event SubnetDestroyed. That term is more general as it can apply in ephemeral subnets, self-destruction, expirations (e.g. subnet is not bootstrapped in a predefined amount of time).

}

/// @notice addStake - add collateral for an existing subnet
function addStake(uint256 amount) external payable {
function addStake(uint256 amount) external payable nonReentrant {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine, but is there really a re-entrancy risk here? The contract call is performed before we update credit the stake, so it shouldn't bear any risks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lock does the check already, this then can go.

@@ -45,7 +45,7 @@ contract GatewayMessengerFacet is GatewayActorModifiers {
}

// We prevent the sender from being an EoA.
if (!(msg.sender.code.length > 0)) {
if (msg.sender.code.length == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😆

@@ -423,7 +423,7 @@ library LibGateway {
sendReceipt(crossMsg, OutcomeType.SystemErr, abi.encodeWithSelector(InvalidXnetMessage.selector, InvalidXnetMessageReason.Nonce));
return;
}
subnet.appliedBottomUpNonce += 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that this is purely stylistic, I would use a post-increment. It's visually more common.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cryptoAtwill Can't we do appliedBottomUpNonce++?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ++appliedBottomUpNonce is appliedBottomUpNonce += 1 but appliedBottomUpNonce++ is actually:

i = appliedBottomUpNonce;
appliedBottomUpNonce = appliedBottomUpNonce + 1;
i

it's slightly more gas consuming.

@@ -72,7 +74,6 @@ contract GatewayManagerFacet is GatewayActorModifiers, ReentrancyGuard {
SubnetActorGetterFacet(msg.sender).collateralSource().lock(amount);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not move the lock after the check?

@@ -73,8 +76,13 @@ contract SubnetActorManagerFacet is SubnetActorModifiers, ReentrancyGuard, Pausa
}
}

/// @notice Sets the validator gater contract implementation
/// @param gater The addresseof validator gater implementation.
Copy link
Contributor

@karlem karlem Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: addresseof -> address of

Copy link
Contributor

@karlem karlem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just small nits.

Copy link

@aiagentdev-1990 aiagentdev-1990 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a small comment but this looks good overall. Also consider adding additional unit tests to cover some of the fixes.

@@ -1584,7 +1584,7 @@ contract SubnetActorDiamondTest is Test, IntegrationTestBase {
gatewayAddress,
ConsensusType.Fendermint,
DEFAULT_MIN_VALIDATOR_STAKE,
DEFAULT_MIN_VALIDATORS,
2,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend assigning this to a constant variable for readability

@cryptoAtwill cryptoAtwill merged commit c455add into main Jan 23, 2025
15 checks passed
@cryptoAtwill cryptoAtwill deleted the fix-audit branch January 23, 2025 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants