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: fix uint64->uint32 underflow issues resulting from using cosmossdk.io/math #293

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

odeke-em
Copy link

@odeke-em odeke-em commented Mar 13, 2024

This fixes potential security issues resulting from extraneous
parsing that used cosmossdk.math.ParseUint which uses math/big.Int
which is a big integer package and can allow uint64 in places
where uint32 is used and there were underflow checks.
The fix for this change was to simply use

strconv.ParseUint(s, 10, BIT_SIZE)

where BIT_SIZE is any of 32 or 64 bits for uint32 and uint64 respectively,
and by extracting the shared code to an internal package.

Fixes #292

Summary by CodeRabbit

  • New Features

    • Enhanced type safety and simplified data handling for auction and bid IDs.
    • Improved parsing logic for chain IDs and block heights to increase clarity and efficiency.
  • Bug Fixes

    • Resolved potential issues with integer parsing, enhancing robustness and reducing errors.
  • Refactor

    • Streamlined transaction handling logic to centralize parsing in the internal package, improving maintainability.

Copy link

coderabbitai bot commented Mar 13, 2024

Walkthrough

The modification in the codebase focuses on enhancing security and reliability by replacing the use of cosmossdk.io/math for parsing unsigned integers with internal utility functions. This change introduces ParseUint32 and ParseUint64, ensuring safer parsing and directly addressing potential underflows and security concerns associated with casting uint64 to uint32. The updates streamline the code and improve type safety across various query and transaction functions.

Changes

File(s) Change Summary
x/auction/client/cli/query.go Replaced cosmossdk.io/math.ParseUint with internal.ParseUint32 for auction and bid IDs. Updated relevant query functions accordingly.
x/auction/client/cli/tx.go Removed math import and replaced math.ParseUint with internal.ParseUint32. Updated NewMsgSubmitBidRequest to directly accept uint32 auction ID.
x/axelarcork/client/cli/query.go Updated parsing of chain IDs and heights to use internal.ParseUint64, improving code clarity and reducing type conversions.
x/axelarcork/client/cli/tx.go Replaced math.ParseUint with internal.ParseUint64 in command functions for consistency and maintainability.
x/cork/client/cli/query.go Simplified queryScheduledCorksByBlockHeight by using internal.ParseUint64, directly assigning to BlockHeight.
internal/utils.go Introduced ParseUint32 and ParseUint64 functions for safe unsigned integer parsing, enhancing robustness.

Assessment against linked issues

Objective Addressed Explanation
Identify and address the use of cosmossdk.io/math.ParseUint followed by casting to uint32, preventing underflows (#292)
Replace problematic code with strconv.ParseUint(args[0], 10, 32) to prevent underflows and catch range errors (#292)
Create a parseUint32 helper function for consistent and secure handling across 11 affected sites (#292)
Apply the fix to all instances of the identified code pattern to mitigate underflow risks and improve reliability (#292)

Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between af08855 and 068f594.

Files ignored due to path filters (2)
  • go.mod is excluded by !**/*.mod
  • internal/go.mod is excluded by !**/*.mod
Files selected for processing (6)
  • internal/utils.go (1 hunks)
  • x/auction/client/cli/query.go (5 hunks)
  • x/auction/client/cli/tx.go (3 hunks)
  • x/axelarcork/client/cli/query.go (10 hunks)
  • x/axelarcork/client/cli/tx.go (8 hunks)
  • x/cork/client/cli/query.go (2 hunks)
Additional comments not posted (25)
internal/utils.go (3)

1-6: Approved: New file setup and imports look good.

The package declaration and imports are appropriate for the functionality provided in this file.


8-11: Approved: Correct implementation of ParseUint64.

The ParseUint64 function correctly uses strconv.ParseUint to parse a string as a uint64 value.


13-27: Approved: Correct implementation of ParseUint32 with additional underflow check.

The ParseUint32 function correctly parses a string as a uint32 value and includes an additional check to ensure no underflows occur. This is a robust approach to handle potential security issues.

x/auction/client/cli/tx.go (3)

14-14: Approved: Correct import of the new internal package.

The import statement correctly includes the new internal package for utility functions.


123-126: Approved: Correct usage of internal.ParseUint32 for auction ID parsing.

The ParseUint32 function is correctly used to parse the auction ID, ensuring type safety and preventing potential underflow issues.


143-146: Approved: Correct integration of parsed auction ID in NewMsgSubmitBidRequest.

The parsed auction ID is correctly passed to the NewMsgSubmitBidRequest function, ensuring consistent data handling.

x/cork/client/cli/query.go (3)

8-8: Approved: Correct import of the new internal package.

The import statement correctly includes the new internal package for utility functions.


137-139: Approved: Correct usage of internal.ParseUint64 for block height parsing.

The ParseUint64 function is correctly used to parse the block height, ensuring type safety and preventing potential underflow issues.


144-146: Approved: Correct integration of parsed block height in QueryScheduledCorksByBlockHeightRequest.

The parsed block height is correctly assigned to the BlockHeight field in the request struct, ensuring consistent data handling.

x/auction/client/cli/query.go (4)

82-86: LGTM! The change enhances type safety.

The use of internal.ParseUint32 ensures that auction IDs are parsed correctly as 32-bit unsigned integers, improving type safety and preventing potential underflow issues.


118-122: LGTM! The change enhances type safety.

The use of internal.ParseUint32 ensures that auction IDs are parsed correctly as 32-bit unsigned integers, improving type safety and preventing potential underflow issues.


290-297: LGTM! The change enhances type safety.

The use of internal.ParseUint32 and internal.ParseUint64 ensures that auction and bid IDs are parsed correctly as 32-bit and 64-bit unsigned integers, respectively, improving type safety and preventing potential underflow issues.


332-336: LGTM! The change enhances type safety.

The use of internal.ParseUint32 ensures that auction IDs are parsed correctly as 32-bit unsigned integers, improving type safety and preventing potential underflow issues.

x/axelarcork/client/cli/query.go (9)

114-118: LGTM! The change enhances type safety.

The use of internal.ParseUint64 ensures that chain IDs are parsed correctly as 64-bit unsigned integers, improving type safety and preventing potential underflow issues.


153-157: LGTM! The change enhances type safety.

The use of internal.ParseUint64 ensures that chain IDs are parsed correctly as 64-bit unsigned integers, improving type safety and preventing potential underflow issues.


188-201: LGTM! The change enhances type safety.

The use of internal.ParseUint64 ensures that block heights and chain IDs are parsed correctly as 64-bit unsigned integers, improving type safety and preventing potential underflow issues.


231-235: LGTM! The change enhances type safety.

The use of internal.ParseUint64 ensures that chain IDs are parsed correctly as 64-bit unsigned integers, improving type safety and preventing potential underflow issues.


274-278: LGTM! The change enhances type safety.

The use of internal.ParseUint64 ensures that chain IDs are parsed correctly as 64-bit unsigned integers, improving type safety and preventing potential underflow issues.


316-320: LGTM! The change enhances type safety.

The use of internal.ParseUint64 ensures that chain IDs are parsed correctly as 64-bit unsigned integers, improving type safety and preventing potential underflow issues.


353-357: LGTM! The change enhances type safety.

The use of internal.ParseUint64 ensures that chain IDs are parsed correctly as 64-bit unsigned integers, improving type safety and preventing potential underflow issues.


479-483: LGTM! The change enhances type safety.

The use of internal.ParseUint64 ensures that chain IDs are parsed correctly as 64-bit unsigned integers, improving type safety and preventing potential underflow issues.


519-523: LGTM! The change enhances type safety.

The use of internal.ParseUint64 ensures that chain IDs are parsed correctly as 64-bit unsigned integers, improving type safety and preventing potential underflow issues.

x/axelarcork/client/cli/tx.go (3)

Line range hint 58-62: LGTM! The change enhances type safety.

The use of internal.ParseUint64 ensures that chain IDs and block heights are parsed correctly as 64-bit unsigned integers, improving type safety and preventing potential underflow issues.

Also applies to: 68-72, 78-82


Line range hint 111-115: LGTM! The change enhances type safety.

The use of internal.ParseUint64 ensures that chain IDs and fees are parsed correctly as 64-bit unsigned integers, improving type safety and preventing potential underflow issues.

Also applies to: 126-130, 134-138


Line range hint 164-168: LGTM! The change enhances type safety.

The use of internal.ParseUint64 ensures that chain IDs and fees are parsed correctly as 64-bit unsigned integers, improving type safety and preventing potential underflow issues.

Also applies to: 174-178, 182-186


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@odeke-em odeke-em force-pushed the fix-x-auction-client-underflow-issues branch from a882442 to af08855 Compare March 13, 2024 22:54
@odeke-em odeke-em changed the title fix(x/auction/client/cli): fix uint64->uint32 underflow issues fix: fix uint64->uint32 underflow issues resulting from using cosmossdk.io/math Mar 13, 2024
@odeke-em odeke-em force-pushed the fix-x-auction-client-underflow-issues branch 3 times, most recently from 3425674 to 9f66b41 Compare March 13, 2024 23:14
@cbrit
Copy link
Member

cbrit commented Jul 19, 2024

Thank you for the PR. Is this something that has been fixed in strconv in the intervening time since you opened it? If you find time would you mind merging main?

…dk.io/math

This fixes potential security issues resulting from extraneous
parsing that used cosmossdk.math.ParseUint which uses math/big.Int
which is a big integer package and can allow uint64 in places
where uint32 is used and there were underflow checks.
The fix for this change was to simply use

    strconv.ParseUint(s, 10, BIT_SIZE)

where BIT_SIZE is any of 32 or 64 bits for uint32 and uint64 respectively,
and by extracting the shared code to an internal package.

Fixes PeggyJV#292
@odeke-em odeke-em force-pushed the fix-x-auction-client-underflow-issues branch from 9f66b41 to 068f594 Compare July 21, 2024 11:01
@odeke-em
Copy link
Author

Hey @cbrit, I've rebased from the latest on main. Please take a look again.

Thank you for the PR. Is this something that has been fixed in strconv in the intervening time since you opened it?

So the problems weren't in strconv but rather in the usage of it in these libraries, without careful consideration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants