Skip to content

Fix Use-After-Release in CommandHandler.channelUnregistered() and close channel on channelRead() writeBytes failure #4030 - #4031

Open
juliojgd wants to merge 1 commit into
redis:mainfrom
InditexTech:fix/4030-command-handler-read-buffer-uaf
Open

juliojgd wants to merge 1 commit into
redis:mainfrom
InditexTech:fix/4030-command-handler-read-buffer-uaf

Conversation

@juliojgd

@juliojgd juliojgd commented Nov 24, 2026 •

Copy link
Copy Markdown

Closes #4030
Closes #4027

Summary

This PR fixes two chained bugs in io.lettuce.core.protocol.CommandHandler that together cause spontaneous IndexOutOfBoundsException followed by silent cross-key response corruption under Netty 4.2's default AdaptiveByteBufAllocator:

  1. Fix Use-After-Release in CommandHandler.channelUnregistered() (Use-After-Release in CommandHandler.channelUnregistered() under Netty 4.2 AdaptiveByteBufAllocator causes silent cross-key response corruption #4030):
    • Null out readBuffer before releasing it and calling reset().
    • Previously, readBuffer.release() returned the 64 KiB AdaptiveByteBuf to Netty 4.2's shared cross-thread MPMC recycler while keeping this.readBuffer non-null, allowing resetInternals() to call readBuffer.clear() on a buffer already reclaimed by another EventLoop.
  2. Prevent silent cross-key response shifting in CommandHandler.channelRead() (Read-side exception after buffer copy leaves channel active and shifts subsequent command responses #4027):
    • Guard readBuffer.writeBytes(input) with try/catch (Throwable e) { ctx.close(); throw e; } (consistent with decode(...)) so undecoded bytes cannot remain stranded in readBuffer on an active channel.

Verification

  • Added dedicated unit tests in CommandHandlerUnitTests:
    • shouldNotClearReadBufferAfterReleaseInChannelUnregistered
    • shouldCloseChannelWhenWriteBytesThrowsInChannelRead
  • Verified mvn formatter:format / formatter:validate (0 formatting diffs) and CommandHandlerUnitTests + PubSubCommandHandlerUnitTests (50/50 passing).

Checklist


Note

Medium Risk
Touches core Netty read/teardown paths in CommandHandler; changes are small and targeted but affect every Redis connection’s protocol handling.

Overview
Fixes two related bugs in CommandHandler around Netty 4.2’s aggressive ByteBuf recycling (#4030, #4027).

In channelUnregistered(), readBuffer is nulled before release(), so reset() → resetInternals() no longer calls clear() on a buffer that may already be reused on another event loop.

In channelRead(), readBuffer.writeBytes(input) is wrapped so any failure closes the channel and rethrows, matching decode() and avoiding undecoded bytes left on an open connection.

Unit tests cover nulling after release and channel close when writeBytes throws.

Reviewed by Cursor Bugbot for commit 952c57e. Bugbot is set up for automated code reviews on this repo. Configure here.

…ered() and close channel on writeBytes failure redis#4030

We now null out readBuffer before releasing it in CommandHandler.channelUnregistered() so resetInternals() cannot call readBuffer.clear() on a ByteBuf already reclaimed by another EventLoop (e.g. under Netty 4.2 AdaptiveByteBufAllocator's shared MPMC recycler).

We also close the channel if readBuffer.writeBytes(input) throws Throwable in CommandHandler.channelRead() so undecoded bytes cannot remain in readBuffer on an active channel and silently shift subsequent command responses.

Closes redis#4030
Closes redis#4027
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T09:13:23.905363Z 952c57e PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@a-TODO-rov

Copy link
Copy Markdown
Collaborator

Thanks for the reports.
I will try to reproduce it first, than we can talk solutions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants