Skip to content

Commit

Permalink
Fix Protocol desync regression test (valkey-io#1590)
Browse files Browse the repository at this point in the history
The desync regression test was created as a regression test for the
following bug:
in case we embed NULL termination inside inline/multi-bulk message we
will not be able to perform strchr in order to
identify the newline(\n)/carriage-return(\r) in the client query buffer.
this can influence (for example) replica reading primary stream and keep
filling it's query buffer endlessly consuming more and more memory.

In order to handle the above risk, a check was added to verify the
inline bulk and multi-bulk size are not exceeding the 64K bytes in the
query-buffer. A test was placed in order to verify this.

This PR introduce the following fixes to the desync regression test:
1. fix the sent payload to flush 1024 bytes block of 'A's instead of
'payload' which was sent by mistake.
2. Make sure that the connection is correctly terminated on protocol
error by the server after exceeding the 64K and not over 64K.
3. add another test intrinsic which will also verify the nested bulk
with embedded null termination (was not verified before)

fixes valkey-io#1583


NOTE: Although it is possible to change the use of strchr to a more
"safe" utility (eg memchr) which will not pause scan at first occurrence
of '\0', we still like to protect against over excessive usage of the
query buffer and also preserve the current behavior(?). We will look
into improving this though in a followup issue.

---------

Signed-off-by: Ran Shidlansik <[email protected]>
Signed-off-by: ranshid <[email protected]>
  • Loading branch information
ranshid authored and kronwerk committed Jan 27, 2025
1 parent fc20f29 commit 4b1c861
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions tests/unit/protocol.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -69,37 +69,39 @@ start_server {tags {"protocol network"}} {
}

set c 0
foreach seq [list "\x00" "*\x00" "$\x00"] {
foreach seq [list "\x00" "*\x00" "$\x00" "*1\r\n$\x00"] {
incr c
test "Protocol desync regression test #$c" {
if {$::tls} {
set s [::tls::socket [srv 0 host] [srv 0 port]]
} else {
set s [socket [srv 0 host] [srv 0 port]]
}
fconfigure $s -blocking 0
puts -nonewline $s $seq
# PROTO_INLINE_MAX_SIZE is hardcoded in Valkey code to 64K. doing the same here
# since we would like to validate it is enforced.
set PROTO_INLINE_MAX_SIZE [expr 1024 * 64]
set payload [string repeat A 1024]"\n"
set test_start [clock seconds]
set test_time_limit 30
while 1 {
set payload_size 0
while {$payload_size <= $PROTO_INLINE_MAX_SIZE} {
if {[catch {
puts -nonewline $s payload
flush $s
incr payload_size [string length $payload]
puts -nonewline $s $payload
flush $s
}]} {
set retval [gets $s]
close $s
assert_morethan $payload_size $PROTO_INLINE_MAX_SIZE
break
} else {
set elapsed [expr {[clock seconds]-$test_start}]
if {$elapsed > $test_time_limit} {
close $s
error "assertion:Valkey did not closed connection after protocol desync"
}
}
}
set retval
} {*Protocol error*}

wait_for_condition 50 100 {
[string match {*Protocol error*} [gets $s]]
} else {
fail "expected connection to be closed on protocol error after sending $payload_size bytes"
}
close $s
}
}
unset c

Expand Down

0 comments on commit 4b1c861

Please sign in to comment.