Skip to content

Reset LengthFieldBasedFrameDecoder state when the frame is shorter than initialBytesToStrip - #17606

Merged
normanmaurer merged 1 commit into
netty:4.2from
allthingssecurity:fix-lengthfield-strip-resync
Sep 25, 2026
Merged

normanmaurer merged 1 commit into
netty:4.2from
allthingssecurity:fix-lengthfield-strip-resync

Conversation

@allthingssecurity

Copy link
Copy Markdown
Contributor

Motivation:

When the adjusted frame length is less than initialBytesToStrip, LengthFieldBasedFrameDecoder throws a CorruptedFrameException without resetting frameLengthInt. Every following decode(...) call then skips reading the length field of the next frame, sees the same short frame length again, and throws again. frameLength is 0 in those calls, so no bytes are skipped either. One malformed frame therefore makes every later frame on the connection fail (Adjusted frame length (0) is less than initialBytesToStrip: ...). The same happens on the first malformed frame if its length field and its content arrive in separate reads. All the other error paths of the decoder reset their state and go on with the next frame. This regressed in #12173, which cached the frame length in frameLengthInt but only reset it on the success path.

Modification:

  • Reset frameLengthInt before failing, and skip the frame using frameLengthInt instead of the local frameLength, which is 0 when the length field was read by an earlier call.
  • Add two tests: the malformed frame in one read, and its length field and content in separate reads. Both fail without the change (with the exception above on the following valid frame) and pass with it.

Result:

The decoder fails the malformed frame and then decodes the following frames again. codec-base tests: 352, 0 failures.

This was found by modelling the decoder's frame-length arithmetic and state in Lean 4 (a proof that every error path returns to the "new frame" state failed on this path), then reproducing it with the real class. The change was prepared with Claude Code (Claude Opus 5.5), and the commit has a Co-Authored-By trailer.

🤖 Generated with Claude Code

…an initialBytesToStrip

Motivation:

When the adjusted frame length is less than initialBytesToStrip,
LengthFieldBasedFrameDecoder throws a CorruptedFrameException without
resetting frameLengthInt. Every following decode(...) call then skips
reading the length field of the next frame, sees the same short frame
length again, and throws again. frameLength is 0 in those calls, so no
bytes are skipped either. One malformed frame therefore makes every
later frame on the connection fail. The same happens on the first
malformed frame if its length field and its content arrive in separate
reads. All the other error paths of the decoder reset their state.

This regressed in netty#12173, which cached the frame length in
frameLengthInt but only reset it on the success path.

Modification:

Reset frameLengthInt before failing, and skip the frame using
frameLengthInt instead of the local frameLength.

Result:

The decoder fails the malformed frame and then decodes the following
frames again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@normanmaurer

Copy link
Copy Markdown
Member

@allthingssecurity

Copy link
Copy Markdown
Contributor Author

@normanmaurer yes, I signed the ICLA (https://netty.io/s/icla) on 2026-09-24, under the name used in my commits (smjain).

This comment was prepared with Claude Code on behalf of allthingssecurity.

@normanmaurer normanmaurer added this to the 4.2.19.Final milestone Sep 25, 2026
@normanmaurer normanmaurer added needs-cherry-pick-4.1 This PR should be cherry-picked to 4.1 once merged. needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. labels Sep 25, 2026
@testlens-app

testlens-app Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

🚨 All tests passed but jobs failed 🚨

Failed Jobs without Test Failures

❌ Cancel PR Jobs on Merge / cancel

🏷️ Commit: 363c98d
▶️ Tests: 440002 executed
⚪️ Checks: 32/32 completed

Rerun Controls

Click the checkbox to trigger a rerun:

  • Rerun jobs

Learn more about TestLens at testlens.app/docs.

@normanmaurer
normanmaurer merged commit c784721 into netty:4.2 Sep 25, 2026
29 of 30 checks passed
@normanmaurer

Copy link
Copy Markdown
Member

@allthingssecurity thanks!

@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port PR for 4.1: #17621

@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port PR for 5.0: #17622

@github-actions github-actions Bot removed the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Sep 25, 2026
@allthingssecurity

Copy link
Copy Markdown
Contributor Author

@allthingssecurity thanks!

Pleasure is entirely mine to contribute to a project I absolutely love

normanmaurer pushed a commit that referenced this pull request Sep 25, 2026
…e is shorter than initialBytesToStrip (#17621)

Auto-port of #17606 to 4.1
Cherry-picked commit: c784721

---
Motivation:

When the adjusted frame length is less than `initialBytesToStrip`,
`LengthFieldBasedFrameDecoder` throws a `CorruptedFrameException`
without resetting `frameLengthInt`. Every following `decode(...)` call
then skips reading the length field of the next frame, sees the same
short frame length again, and throws again. `frameLength` is `0` in
those calls, so no bytes are skipped either. One malformed frame
therefore makes every later frame on the connection fail (`Adjusted
frame length (0) is less than initialBytesToStrip: ...`). The same
happens on the first malformed frame if its length field and its content
arrive in separate reads. All the other error paths of the decoder reset
their state and go on with the next frame. This regressed in #12173,
which cached the frame length in `frameLengthInt` but only reset it on
the success path.

Modification:

- Reset `frameLengthInt` before failing, and skip the frame using
`frameLengthInt` instead of the local `frameLength`, which is `0` when
the length field was read by an earlier call.
- Add two tests: the malformed frame in one read, and its length field
and content in separate reads. Both fail without the change (with the
exception above on the following valid frame) and pass with it.

Result:

The decoder fails the malformed frame and then decodes the following
frames again. `codec-base` tests: 352, 0 failures.

Co-authored-by: allthingssecurity <[email protected]>
normanmaurer added a commit that referenced this pull request Sep 25, 2026
…e is shorter than initialBytesToStrip (#17622)

Auto-port of #17606 to 5.0
Cherry-picked commit: c784721

---
Motivation:

When the adjusted frame length is less than `initialBytesToStrip`,
`LengthFieldBasedFrameDecoder` throws a `CorruptedFrameException`
without resetting `frameLengthInt`. Every following `decode(...)` call
then skips reading the length field of the next frame, sees the same
short frame length again, and throws again. `frameLength` is `0` in
those calls, so no bytes are skipped either. One malformed frame
therefore makes every later frame on the connection fail (`Adjusted
frame length (0) is less than initialBytesToStrip: ...`). The same
happens on the first malformed frame if its length field and its content
arrive in separate reads. All the other error paths of the decoder reset
their state and go on with the next frame. This regressed in #12173,
which cached the frame length in `frameLengthInt` but only reset it on
the success path.

Modification:

- Reset `frameLengthInt` before failing, and skip the frame using
`frameLengthInt` instead of the local `frameLength`, which is `0` when
the length field was read by an earlier call.
- Add two tests: the malformed frame in one read, and its length field
and content in separate reads. Both fail without the change (with the
exception above on the following valid frame) and pass with it.

Result:

The decoder fails the malformed frame and then decodes the following
frames again. `codec-base` tests: 352, 0 failures.

---------

Co-authored-by: allthingssecurity <[email protected]>
Co-authored-by: Norman Maurer <[email protected]>
@allthingssecurity
allthingssecurity deleted the fix-lengthfield-strip-resync branch September 27, 2026 02:04
lltopk pushed a commit to lltopk/netty that referenced this pull request Oct 24, 2026
…e is shorter than initialBytesToStrip (netty#17621)

Auto-port of netty#17606 to 4.1
Cherry-picked commit: c784721

---
Motivation:

When the adjusted frame length is less than `initialBytesToStrip`,
`LengthFieldBasedFrameDecoder` throws a `CorruptedFrameException`
without resetting `frameLengthInt`. Every following `decode(...)` call
then skips reading the length field of the next frame, sees the same
short frame length again, and throws again. `frameLength` is `0` in
those calls, so no bytes are skipped either. One malformed frame
therefore makes every later frame on the connection fail (`Adjusted
frame length (0) is less than initialBytesToStrip: ...`). The same
happens on the first malformed frame if its length field and its content
arrive in separate reads. All the other error paths of the decoder reset
their state and go on with the next frame. This regressed in netty#12173,
which cached the frame length in `frameLengthInt` but only reset it on
the success path.

Modification:

- Reset `frameLengthInt` before failing, and skip the frame using
`frameLengthInt` instead of the local `frameLength`, which is `0` when
the length field was read by an earlier call.
- Add two tests: the malformed frame in one read, and its length field
and content in separate reads. Both fail without the change (with the
exception above on the following valid frame) and pass with it.

Result:

The decoder fails the malformed frame and then decodes the following
frames again. `codec-base` tests: 352, 0 failures.

Co-authored-by: allthingssecurity <[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.

3 participants