Skip to content

add support for resp3 - #182

Merged
YaacovHazan merged 3 commits into
redis:masterfrom
YaacovHazan:support-resp-3
Jul 28, 2022
Merged

YaacovHazan merged 3 commits into
redis:masterfrom
YaacovHazan:support-resp-3

Conversation

@YaacovHazan

Copy link
Copy Markdown
Collaborator
  • add new command line argument, '--resp', to send HELLO command
    at the begining of a connection to set the RESP mode: 2 or 3.
  • the redis protocol component updated to support the new RESP 3 types.
    exceptional are:
    • Push type: Maybe will be added on next phase, but need to understand better
      how and what does it mean to benchmark Redis with push commands.
    • Streamed strings & Streamed aggregated: Currently not used by Redis.

yaacov Hazan added 2 commits June 21, 2022 20:12
- add new command line argument, '--resp', to send HELLO command
  at the begining of a connection to set the RESP mode: 2 or 3.
- the redis protocol component updated to support the new RESP 3 types.
  exceptional are:
  - Push type: Maybe will be added on next phase, but need to understand better
    how and what does it mean to benchmark Redis with push commands.
  - Streamed strings & Streamed aggregated: Currently not used by Redis.
- remove unused set_authentication() & set_select_db()
- unite all the setup state enums of aute, db selection, cluster slots
  and the new hello to one setup_state enum, as all of them have the same
  states of none, sent and done.
@YaacovHazan
YaacovHazan requested a review from yossigo June 21, 2022 18:00
Comment thread memtier_benchmark.cpp Outdated
{ "port", 1, 0, 'p' },
{ "unix-socket", 1, 0, 'S' },
{ "protocol", 1, 0, 'P' },
{ "resp", 1, 0, o_resp },

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Did you consider extending --protocol instead of adding another flag?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, but for me, it seems more internal configuration under the Redis protocol. and for backport compatibility, we probably need to add another 2 options (redis-resp2, redis-resp3) as the existing one refers to not set resp version at all.

Comment thread protocol.cpp Outdated
Comment on lines +266 to +268
if (conf == PROTOCOL_CONF_RESP2 || conf == PROTOCOL_CONF_RESP3) {
m_resp3 = conf == PROTOCOL_CONF_RESP3;
size = evbuffer_add_printf(m_write_buf,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we want to maintain a backwards compatible mode where we don't send HELLO and we assume we're using RESP2? Without it, we'd lose compatibility with Redis <6.0.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Not sure I got it. In the current implementation if the user doesn't add --resp he will get the old behavior (will not get into this piece of code).

Comment thread protocol.cpp
return size;
}

bool redis_protocol::aggregate_type(char c) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder if these additional branches are going to have some measurable impact on performance, being executed in tight loops. If that's the case, maybe we'd want to subclass redis_protocol into V2 and V3 instead.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Well not sore it is noticeable and that simple benchmark (of memtier-benchmark) will show us something.
regarding your suggestion to subclass it, do you mean that V2 will not have the impact on the performance as V3?
@filipecosta90 WDYT about this performance concern?

@YaacovHazan YaacovHazan linked an issue Jul 10, 2022 that may be closed by this pull request
- change config->protocol to be one of the PROTOCOL_TYPE enum (instead of char *)
- use --protocol to set resp2 or resp3 instead of --resp
@YaacovHazan
YaacovHazan merged commit 1d5ac99 into redis:master Jul 28, 2022
@YaacovHazan
YaacovHazan deleted the support-resp-3 branch July 28, 2022 16:31
fcostaoliveira added a commit that referenced this pull request Oct 26, 2026
## Problem

`redis_protocol::parse_response()` does signed arithmetic on lengths and counts that come from the server. An oversized value makes that arithmetic overflow, which is undefined behaviour (#831):

- `$9223372036854775807` (and `$99999999999999999999`, which `strtol` clamps to `LONG_MAX`): `m_bulk_len + 2` overflows `long` in `rs_read_bulk` (`protocol.cpp:864`). The same happens for RESP3 `!` and `=`.
- RESP3 `%1073741824` / `|1073741824`: `count *= 2` overflows `int` (`protocol.cpp:723`).
- With per-element miss tracking, nested counts such as `*2\r\n*2147483646\r\n*3\r\n` overflow `m_nested_remaining` (`protocol.cpp:747`). So does a nested RESP3 attribute followed by a large count, e.g. `*1\r\n*2\r\n|0\r\n*2147483647\r\n`: the `|` takes a parent slot without being tracked.
- Not UB, but wrong: a count above `INT_MAX` is truncated when stored in `int`, so `*2147483648` is read as a null array.

The existing fixture `bulk_int_overflow.bin` reaches the first case. `test_bulk_int_overflow` still passed under `ubsan.yml` because `halt_on_error=1` without `abort_on_error` makes UBSan exit 1 after printing only `runtime error:`. The harness checks only for negative exit codes and a set of stderr needles that did not include that string. #545's libevent CI aborts on UBSan and failed on it with `exit_code=-6`.

## Root cause

- 84c922a (#134) changed `m_bulk_len` from `unsigned int` to a `long` read with `strtol`. Nothing bounds it, so `m_bulk_len + 2` can overflow.
- 1d5ac99 (#182) added the map/attribute doubling on an unbounded `int`.
- 61a261b (#472) added the `int` nested-element bookkeeping.

## Fix

- Bulk/blob: the length is parsed into a local first. If it is above `LONG_MAX - 2`, the parser returns the existing protocol error (`-1`, "error: response parsing failed.") without changing any parser state, and `line` is freed. Where `long` is 32 bits (LLP64, e.g. MSYS2/Windows), lengths above 2^31 - 3 are now rejected; before, they overflowed (UB) or waited forever.
- Aggregates: the count is parsed with `strtoll`, because `long` is 32 bits on Windows. Negative values are clamped as before. The parser then rejects the aggregate if its elements (doubled for `%`/`|`), plus the elements still outstanding, would exceed `INT_MAX`. A nested aggregate counts as one outstanding slot of its parent. When miss tracking is recording a nested element, the bound also uses `m_nested_remaining - 1`, because a nested attribute can leave `m_nested_remaining` larger than the outstanding count. This keeps `m_total_bulks_count`, `m_nested_remaining` and the doubling within `int`.
- Valid lengths and counts behave exactly as before. That includes `$-1`, `$0`, other negative lengths, `*-1`, and counts up to `INT_MAX`.
- `tests/test_resp_response_fuzzer.py`: add `runtime error:` and `LeakSanitizer` to the crash needles. UBSan is now caught whatever `abort_on_error` is set to. The fixture README is updated to match.

## Tests

- New `check_length_limits()` in `tests/unit/redis_protocol_test.cpp`. It covers the boundaries (LONG_MAX, LONG_MAX-1 and out-of-range bulk lengths are rejected, LONG_MAX-2 waits for data; INT_MAX vs INT_MAX+1 arrays; INT_MAX/2 vs INT_MAX/2+1 maps and attributes; maps nested under outstanding elements; nested outstanding at INT_MAX vs above it; the nested-attribute tracking case at and above the bound). It also checks that ordinary bulk, empty, null, map and nested replies still parse.
- Before (master `protocol.cpp` with the new tests), Ubuntu 24.04 / GCC 13 / system libevent:
  - normal build: `redis_protocol_test` reports 14 `FAIL` lines.
  - UBSan build: `runtime error` at `protocol.cpp:864:80` (`9223372036854775807 + 2`), `:723:27` (`1073741824 * 2`) and `:747:48` (`2147483646 + 2`).
  - `test_resp_response_fuzzer.py` with the CI `UBSAN_OPTIONS`: `test_bulk_int_overflow` FAIL, `needle 'runtime error:' exit_code=1`. With `abort_on_error=1`: FAIL, `exit_code=-6`.
- After:
  - `make check`: 18/18 in both the normal and the ASan+UBSan (`detect_leaks=1`, `abort_on_error=1`) builds.
  - `test_resp_response_fuzzer.py`: 12/12 in the normal build, in ASan+UBSan with the CI UBSan options, and in ASan+UBSan with `abort_on_error=1` (`ASAN_OPTIONS=detect_leaks=1:abort_on_error=1` throughout).
- The bulk boundaries in the unit test are built from `LONG_MAX` at run time, so the test also holds where `long` is 32 bits (MSYS2 UCRT64).
- `clang-format` 18 `--dry-run --Werror` is clean.

**Unrelated CI failure:** `TSAN OSS-CLUSTER API: TCP TLS (misc)` fails on 3b65721 and on the current head a92655a in `test_crash_handler_integration:test_crash_handler_time_zone_lock_held` (watchdog exit -14, missing BUG REPORT END). This is the known TSan crash-tail failure, tracked by #412 and targeted by #828. This PR does not touch the crash handler.

## Release notes

Behaviour change: an aggregate count above what the parser can represent (e.g. `*2147483648`, which used to be truncated and read as a null array) and a bulk length above `LONG_MAX - 2` are now protocol errors (`response parsing failed`). No real server sends these.

## Affected versions

- Bulk length overflow: 84c922a, every tag from 1.4.0 through 2.5.2 (not 1.3.0).
- Map/attribute doubling: 1d5ac99, 1.4.0 through 2.5.2.
- Nested miss-tracking overflow: 61a261b, 2.5.0 through 2.5.2.

Fixes #831



<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Medium Risk**
> Changes core RESP parsing on every connection; malformed oversized frames now error out instead of UB, but legitimate traffic within prior limits is unchanged.
> 
> **Overview**
> Hardens the Redis RESP parser in `parse_response` so malicious or oversized wire values cannot trigger integer overflow (UB) or silent truncation.
> 
> **Bulk/blob lengths** (`$`, `!`, `=`) are validated before updating parser state: if `strtol` yields a length above `LONG_MAX - 2`, parsing fails with `-1` instead of overflowing `m_bulk_len + 2` in the bulk-read path.
> 
> **Aggregate counts** (`*`, `%`, `|`, etc.) are read with `strtoll` (for 32-bit `long` on Windows), negative counts are still treated as zero, and counts are rejected when declared elements (×2 for maps/attributes) plus outstanding/nested miss-tracking slots would exceed `INT_MAX`. Valid replies behave as before.
> 
> **Tests/docs:** adds `check_length_limits()` unit coverage for boundary cases; extends the RESP fuzz harness crash needles (`runtime error:`, `LeakSanitizer`) and updates fixture README expectations.
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit a7fe64f. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->






Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RESP3 support

2 participants