Skip to content

crypto: fix size_t/int regression node_crypto - #35132

Closed
jasnell wants to merge 1 commit into
nodejs:masterfrom
jasnell:regression-31406
Closed

crypto: fix size_t/int regression node_crypto#35132
jasnell wants to merge 1 commit into
nodejs:masterfrom
jasnell:regression-31406

Conversation

@jasnell

@jasnelljasnell commented Sep 9, 2020

Copy link
Copy Markdown
Member

#31406 introduced a regression in Hash and Hmac update operations.

Haven't looked yet, but it's possible that this also affects other Stream-based crypto operations (e.g. sig, verify, etc) Definitely impacted... adding those to the changeset here.

/cc @addaleax@bnoordhuis

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/crypto

@nodejs-github-botnodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. crypto Issues and PRs related to the crypto subsystem. labels Sep 9, 2020
@jasnelljasnell added the request-ci Add this label to start a Jenkins CI on a PR. label Sep 9, 2020
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 9, 2020
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Comment threadtest/common/index.js Outdated
Comment threadtest/parallel/test-crypto-hash-hmac-regression.js Outdated
@jasnelljasnell added the request-ci Add this label to start a Jenkins CI on a PR. label Sep 9, 2020
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 9, 2020
@jasnell

Copy link
Copy Markdown
MemberAuthor

@addaleax ... Please take a look, I added changes for SignBase and PublicKeyCipher.

CipherBase also technically has a challenge here also but it doesn't segfault, the cipher update operation just fails with an error, so definitely less critical.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@addaleaxaddaleax left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is there any reason not to update this for CipherBase as well, though? Semantically size_t should be the right type here

Comment threadsrc/node_crypto.h Outdated
@jasnell

Copy link
Copy Markdown
MemberAuthor

Is there any reason not to update this for CipherBase as well, though? Semantically size_t should be the right type here

No, not really a good reason not to. We should add a similar size check tho.

nodejs#31406 introduced a regression in
`Hash`, `Hmac`, `SignBase`, and `PublicKeyCipher`
Signed-off-by: James M Snell <jasnell@gmail.com>
@jasnell

Copy link
Copy Markdown
MemberAuthor

Ok @addaleax, added a fixup for CipherBaseUpdate also.

@jasnelljasnell added the request-ci Add this label to start a Jenkins CI on a PR. label Sep 9, 2020
@jasnelljasnell changed the title crypto: fix regression in Hash and Hmaccrypto: fix size_t/int regression node_cryptoSep 9, 2020
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 9, 2020
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@jasnelljasnell added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Sep 9, 2020
@bnoordhuis

Copy link
Copy Markdown
Member

I think it would be better to handle this right after the JS -> C++ transition. OpenSSL by and large uses ints for sizes and that's not something we can change. Concretely, I'd check like this:

voidCryptoOp(const FunctionCallbackInfo<Value>& args) {
// ...if (!args[0]->IsInt32()) {
// throw TypeError
}
int32_t size = args[0]->Int32Value(context).FromJust();
if (size < 0) {
// throw TypeErorr or RangeError
}
// now either cast to size_t (because known-good range) or stick with int32_t
}

That way, code deeper down can safely assume sizes are in a known-good range.

With this PR, the range checking is too much all over the place, making it harder to get it right.

@bnoordhuis

Copy link
Copy Markdown
Member

Forgot to mention: the alternative is to have Node.js transparently break up operations over ranges > INT_MAX into smaller ones but that's error prone, might not always be possible, and probably so exceedingly rare as to not be worth the effort.

@jasnell

Copy link
Copy Markdown
MemberAuthor

With this PR, the range checking is too much all over the place, making it harder to get it right.

In a separate PR I'm making much more far reaching changes that should make it significantly easier to deal with moving forward. It does not yet centralize the type checking / length checking but that's something that can be done easily.

I am perfectly fine with an across-the-board restriction for all crypto operation lengths to be <= INT_MAX

Forgot to mention: the alternative is to have Node.js transparently break up operations over ranges > INT_MAX into smaller ones but that's error prone, might not always be possible, and probably so exceedingly rare as to not be worth the effort.

Yeah, I had considered this also and ruled it out for the same reason.

@bnoordhuis

Copy link
Copy Markdown
Member

I am perfectly fine with an across-the-board restriction for all crypto operation lengths to be <= INT_MAX

I think that'd be best. @nodejs/crypto?

@jasnell

Copy link
Copy Markdown
MemberAuthor

I'll work on that in the crypto refactor I'm doing and will get it backported for 14 and 12 also. Thanks Ben.

@jasnell

Copy link
Copy Markdown
MemberAuthor

#35093 landed that includes these fixes

@jasnelljasnell closed this Oct 8, 2020
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs that have at least one approval, no pending requests for changes, and a CI started.c++Issues and PRs that require attention from people who are familiar with C++.cryptoIssues and PRs related to the crypto subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@jasnell@nodejs-github-bot@bnoordhuis@addaleax@tniessen@himself65