Fix use-after-free in SearchApplicationIndexService buffer lifecycle#143134
Merged
ebarlas merged 6 commits intoelastic:mainfrom Feb 26, 2026
Merged
Conversation
Collaborator
|
Pinging @elastic/es-security (Team:Security) |
Collaborator
|
Hi @ebarlas, I've created a changelog YAML for you. |
DaveCTurner
approved these changes
Feb 26, 2026
Member
DaveCTurner
left a comment
There was a problem hiding this comment.
LGTM nice catch, this would have been bad had the doc ever grown past 8kiB in the past.
| clientWithOrigin.index(indexRequest, wrappedListener); | ||
| } catch (Exception e) { | ||
| listener.onFailure(e); | ||
| wrappedListener.onFailure(e); |
Member
There was a problem hiding this comment.
I have a slight preference for ActionListener#run (makes it harder to forget to complete the wrapped listener here, which is important). But this is ok too.
Collaborator
💔 Backport failed
You can use sqren/backport to manually backport by running |
PeteGillinElastic
pushed a commit
to PeteGillinElastic/elasticsearch
that referenced
this pull request
Feb 27, 2026
updateSearchApplication released a ReleasableBytesStreamOutput in a try-with-resources block while an async client.index() call still held a BytesReference view of its pages. The bug was latent until elastic#142451 switched the default buffer size to PAGE_SIZE_IN_BYTES, causing BigArrays to recycle pages instead of plain byte arrays. Freed pages were immediately reused, corrupting the IndexRequest source on replica nodes. Bind buffer release to the async listener via ActionListener.run and flush instead of close, matching AsyncTaskIndexService.
Contributor
Author
|
I'm going to manually apply the fix to the backport targets, since cherry pick failed. |
ebarlas
added a commit
to ebarlas/elasticsearch
that referenced
this pull request
Feb 27, 2026
updateSearchApplication released a ReleasableBytesStreamOutput in a try-with-resources block while an async client.index() call still held a BytesReference view of its pages. The bug was latent until elastic#142451 switched the default buffer size to PAGE_SIZE_IN_BYTES, causing BigArrays to recycle pages instead of plain byte arrays. Freed pages were immediately reused, corrupting the IndexRequest source on replica nodes. Bind buffer release to the async listener via ActionListener.run and flush instead of close, matching AsyncTaskIndexService.
ebarlas
added a commit
to ebarlas/elasticsearch
that referenced
this pull request
Feb 27, 2026
updateSearchApplication released a ReleasableBytesStreamOutput in a try-with-resources block while an async client.index() call still held a BytesReference view of its pages. The bug was latent until elastic#142451 switched the default buffer size to PAGE_SIZE_IN_BYTES, causing BigArrays to recycle pages instead of plain byte arrays. Freed pages were immediately reused, corrupting the IndexRequest source on replica nodes. Bind buffer release to the async listener via ActionListener.run and flush instead of close, matching AsyncTaskIndexService.
ebarlas
added a commit
to ebarlas/elasticsearch
that referenced
this pull request
Feb 27, 2026
updateSearchApplication released a ReleasableBytesStreamOutput in a try-with-resources block while an async client.index() call still held a BytesReference view of its pages. The bug was latent until elastic#142451 switched the default buffer size to PAGE_SIZE_IN_BYTES, causing BigArrays to recycle pages instead of plain byte arrays. Freed pages were immediately reused, corrupting the IndexRequest source on replica nodes. Bind buffer release to the async listener via ActionListener.run and flush instead of close, matching AsyncTaskIndexService.
ebarlas
added a commit
that referenced
this pull request
Mar 2, 2026
updateSearchApplication released a ReleasableBytesStreamOutput in a try-with-resources block while an async client.index() call still held a BytesReference view of its pages. The bug was latent until #142451 switched the default buffer size to PAGE_SIZE_IN_BYTES, causing BigArrays to recycle pages instead of plain byte arrays. Freed pages were immediately reused, corrupting the IndexRequest source on replica nodes. Bind buffer release to the async listener via ActionListener.run and flush instead of close, matching AsyncTaskIndexService.
ebarlas
added a commit
that referenced
this pull request
Mar 2, 2026
updateSearchApplication released a ReleasableBytesStreamOutput in a try-with-resources block while an async client.index() call still held a BytesReference view of its pages. The bug was latent until #142451 switched the default buffer size to PAGE_SIZE_IN_BYTES, causing BigArrays to recycle pages instead of plain byte arrays. Freed pages were immediately reused, corrupting the IndexRequest source on replica nodes. Bind buffer release to the async listener via ActionListener.run and flush instead of close, matching AsyncTaskIndexService.
ebarlas
added a commit
that referenced
this pull request
Mar 2, 2026
updateSearchApplication released a ReleasableBytesStreamOutput in a try-with-resources block while an async client.index() call still held a BytesReference view of its pages. The bug was latent until #142451 switched the default buffer size to PAGE_SIZE_IN_BYTES, causing BigArrays to recycle pages instead of plain byte arrays. Freed pages were immediately reused, corrupting the IndexRequest source on replica nodes. Bind buffer release to the async listener via ActionListener.run and flush instead of close, matching AsyncTaskIndexService.
tballison
pushed a commit
to tballison/elasticsearch
that referenced
this pull request
Mar 3, 2026
updateSearchApplication released a ReleasableBytesStreamOutput in a try-with-resources block while an async client.index() call still held a BytesReference view of its pages. The bug was latent until elastic#142451 switched the default buffer size to PAGE_SIZE_IN_BYTES, causing BigArrays to recycle pages instead of plain byte arrays. Freed pages were immediately reused, corrupting the IndexRequest source on replica nodes. Bind buffer release to the async listener via ActionListener.run and flush instead of close, matching AsyncTaskIndexService.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SearchApplicationIndexService.updateSearchApplicationallocated aReleasableBytesStreamOutputin a try-with-resources block, serialized JSON into it, then passed aBytesReference(a view over the buffer's pages) to an asyncclient.index()call. The try-with-resources released the buffer'sBigArrayspages before the async operation completed, leaving theIndexRequestsource pointing at freed memory.A second, subtler issue compounded the first: the inner
try (XContentBuilder source = ...)closed theXContentBuilder, which cascaded through Jackson'sJsonGenerator.close()toReleasableBytesStreamOutput.close()viaAUTO_CLOSE_TARGET(enabled by default). This released the buffer's pages even beforebuffer.bytes()was called.The bug was latent until #142451 changed
ReleasableBytesStreamOutput's defaultexpectedSizefrom0toPAGE_SIZE_IN_BYTES. WithexpectedSize=0,BigArraysallocated a plainbyte[]that was simply garbage-collected on release. WithPAGE_SIZE_IN_BYTES, it allocates a recycled page that is immediately reusable after release, making the corruption observable: the replica node would read garbage bytes and throw aDocumentParsingExceptionwith "Illegal character (CTRL-CHAR, code 0)". This hit aTransportWriteActionassertion (failure instanceof MapperParsingException) that crashed the node, causing theConnection refusedfailures seen in CI.The fix follows the pattern established by
AsyncTaskIndexService:ActionListener.runAfter(listener, buffer::close)source.flush()instead of closing the XContentBuilder, to avoid transitively closing the buffer