Skip to content

Commit

Permalink
Fix range issues in ZRANDMEMBER and HRANDFIELD (CVE-2023-22458)
Browse files Browse the repository at this point in the history
missing range check in ZRANDMEMBER and HRANDIFLD leading to panic due
to protocol limitations
  • Loading branch information
oranagra committed Jan 16, 2023
1 parent 6c25c6b commit 3f1f020
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/t_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -1124,8 +1124,13 @@ void hrandfieldCommand(client *c) {
if (c->argc > 4 || (c->argc == 4 && strcasecmp(c->argv[3]->ptr,"withvalues"))) {
addReplyErrorObject(c,shared.syntaxerr);
return;
} else if (c->argc == 4)
} else if (c->argc == 4) {
withvalues = 1;
if (l < LONG_MIN/2 || l > LONG_MAX/2) {
addReplyError(c,"value is out of range");
return;
}
}
hrandfieldWithCountCommand(c, l, withvalues);
return;
}
Expand Down
7 changes: 6 additions & 1 deletion src/t_zset.c
Original file line number Diff line number Diff line change
Expand Up @@ -4293,8 +4293,13 @@ void zrandmemberCommand(client *c) {
if (c->argc > 4 || (c->argc == 4 && strcasecmp(c->argv[3]->ptr,"withscores"))) {
addReplyErrorObject(c,shared.syntaxerr);
return;
} else if (c->argc == 4)
} else if (c->argc == 4) {
withscores = 1;
if (l < LONG_MIN/2 || l > LONG_MAX/2) {
addReplyError(c,"value is out of range");
return;
}
}
zrandmemberWithCountCommand(c, l, withscores);
return;
}
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/type/hash.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ start_server {tags {"hash"}} {
r hrandfield myhash 0
} {}

test "HRANDFIELD count overflow" {
r hmset myhash a 1
assert_error {*value is out of range*} {r hrandfield myhash -9223372036854770000 withvalues}
} {}

test "HRANDFIELD with <count> against non existing key" {
r hrandfield nonexisting_key 100
} {}
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/type/zset.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -2300,6 +2300,11 @@ start_server {tags {"zset"}} {
r zrandmember nonexisting_key 100
} {}

test "ZRANDMEMBER count overflow" {
r zadd myzset 0 a
assert_error {*value is out of range*} {r zrandmember myzset -9223372036854770000 withscores}
} {}

# Make sure we can distinguish between an empty array and a null response
r readraw 1

Expand Down

0 comments on commit 3f1f020

Please sign in to comment.