Skip to content

Commit 4c8d96b

Browse files
Johannrvagg
authored andcommitted
crypto: add more keylen sanity checks in pbkdf2
issue #2987 makes the point that crypto.pbkdf2 should not fail silently and accept invalid but numeric values like NaN and Infinity. We already check if the keylen is lower than 0, so extending that to NaN and Infinity should make sense. Fixes: #2987 PR-URL: #3029 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Brian White <[email protected]> Reviewed-By: Fedor Indutny <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
1 parent 798dad2 commit 4c8d96b

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/node_crypto.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "CNNICHashWhitelist.inc"
2020

2121
#include <errno.h>
22+
#include <math.h>
2223
#include <stdlib.h>
2324
#include <string.h>
2425

@@ -4760,7 +4761,7 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
47604761
char* salt = nullptr;
47614762
ssize_t passlen = -1;
47624763
ssize_t saltlen = -1;
4763-
ssize_t keylen = -1;
4764+
double keylen = -1;
47644765
ssize_t iter = -1;
47654766
PBKDF2Request* req = nullptr;
47664767
Local<Object> obj;
@@ -4813,8 +4814,8 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
48134814
goto err;
48144815
}
48154816

4816-
keylen = args[3]->Int32Value();
4817-
if (keylen < 0) {
4817+
keylen = args[3]->NumberValue();
4818+
if (keylen < 0 || isnan(keylen) || isinf(keylen)) {
48184819
type_error = "Bad key length";
48194820
goto err;
48204821
}
@@ -4841,7 +4842,7 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
48414842
saltlen,
48424843
salt,
48434844
iter,
4844-
keylen);
4845+
static_cast<ssize_t>(keylen));
48454846

48464847
if (args[5]->IsFunction()) {
48474848
obj->Set(env->ondone_string(), args[5]);

test/parallel/test-crypto-pbkdf2.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,31 @@ function ondone(err, key) {
5959
assert.throws(function() {
6060
crypto.pbkdf2('password', 'salt', 1, 20, null);
6161
});
62+
63+
// Should not work with Infinity key length
64+
assert.throws(function() {
65+
crypto.pbkdf2('password', 'salt', 1, Infinity, assert.fail);
66+
}, function(err) {
67+
return err instanceof Error && err.message === 'Bad key length';
68+
});
69+
70+
// Should not work with negative Infinity key length
71+
assert.throws(function() {
72+
crypto.pbkdf2('password', 'salt', 1, -Infinity, assert.fail);
73+
}, function(err) {
74+
return err instanceof Error && err.message === 'Bad key length';
75+
});
76+
77+
// Should not work with NaN key length
78+
assert.throws(function() {
79+
crypto.pbkdf2('password', 'salt', 1, NaN, assert.fail);
80+
}, function(err) {
81+
return err instanceof Error && err.message === 'Bad key length';
82+
});
83+
84+
// Should not work with negative key length
85+
assert.throws(function() {
86+
crypto.pbkdf2('password', 'salt', 1, -1, assert.fail);
87+
}, function(err) {
88+
return err instanceof Error && err.message === 'Bad key length';
89+
});

0 commit comments

Comments
 (0)