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

Clarify the safety of NetClock::time_point arithmetic: #3787

Closed
wants to merge 2 commits into from

Conversation

HowardHinnant
Copy link
Contributor

High Level Overview of Change

To address #3656 a comprehensive survey of all uses of NetClock::time_point in rippled was made. Only two minor were found.

  1. src/ripple/consensus/Validations.h has this expression:
signTime > (now - p.validationCURRENT_EARLY)

This is the expression #3656 refers to as being vulnerable to unsigned overflow. However it turns out this expression is fine because p.validationCURRENT_EARLY is a 64 bit signed integral type. Thus the 32 but unsigned now is losslessly converted to signed 64 bit prior to the conversion. Additionally the 32 but unsigned signTime is converted to signed 64 bit prior to the comparison.

The expression is correct and bullet-proof as is. A comment to this effect has been added.

  1. src/ripple/app/tx/impl/CreateOffer.cpp has this expression:
NetClock::time_point const when{ctx_.view().parentCloseTime()};

While correct, using explicit conversion syntax for implicit construction is error-prone because one might accidentally choose an unwanted explicit constructor. This code is safer using implicit syntax to ensure that only an implicit constructor is chosen:

NetClock::time_point const when = ctx_.view().parentCloseTime();

* NetClock::rep is uint32_t and can be error-prone when
  used with subtraction.
* Fixes XRPLF#3656
@nbougalis
Copy link
Contributor

nbougalis commented Mar 9, 2021

While you're here can you please also take a look at https://github.com/ripple/rippled/blob/develop/src/ripple/overlay/impl/Handshake.cpp#L268-L282 as well? Thanks! 🖖

@HowardHinnant
Copy link
Contributor Author

I actually did look at this, and found the logic sound. However I should have removed the comment. I'll do that.

The reason it is ok is that the a > b check is done and if needed, b-a is computed, converted to signed, and then negated.

@nbougalis nbougalis added Passed Passed code review & PR owner thinks it's ready to merge. Perf sign-off may still be required. Reviewed labels Mar 14, 2021
Copy link
Collaborator

@gregtatcam gregtatcam left a comment

Choose a reason for hiding this comment

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

👍 LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Passed Passed code review & PR owner thinks it's ready to merge. Perf sign-off may still be required. Reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix time arithmetic in Validations and Consensus
3 participants