Skip to content

server: fix heap-buffer-overflow from negative n_discard (CVE-2026-21869) - #22267

Merged
ServeurpersoCom merged 4 commits into
ggml-org:masterfrom
SongTonyLi:fix/cve-2026-21869-n_discard-clamp
Apr 23, 2026
Merged

ServeurpersoCom merged 4 commits into
ggml-org:masterfrom
SongTonyLi:fix/cve-2026-21869-n_discard-clamp

Conversation

@SongTonyLi

@SongTonyLi SongTonyLi commented Apr 22, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Proposing a fix for GHSA-8947-pfff-2f3c (CVE-2026-21869, CWE-787, CVSS 8.8 High).

This vulnerability is still present on current master (0d0764dfd257, 2026-04-22) — every llama-server deployment with context shift enabled is exploitable by any unauthenticated remote client. The fix is a single-line std::max(0, ...) clamp on n_discard at the JSON parse boundary.

Proposed Fix

Check the commit.

Since n_discard = 0 already triggers the auto-discard path (n_left / 2 at the context shift site), clamping negative values to zero preserves all existing semantics with no behavior change for valid inputs.

Additional Information

Root Cause Analysis

The vulnerability is a missing non-negative check on a client-controlled integer parameter that is later used in array index arithmetic.

Reproduction

ASan output on current master (0d0764df):

slot update_slots: id  0 | task 0 | slot context shift, n_keep = 65, n_left = 190, n_discard = -32
=================================================================
==34914==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x519000055983
WRITE of size 16 at 0x519000055983 thread T0
    #0 in server_context_impl::update_slots() llama-server+0x435ee0
    #1 in server_queue::start_loop(long)      llama-server+0x57696d
    #2 in main                                llama-server+0x2e8ca7

0x519000055983 is located 7 bytes after 1020-byte region [0x519000055580,0x51900005597c)

PoC payload:

{
    "prompt": "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.",
    "stream": false,
    "cache_prompt": true,
    "n_predict": 512,
    "n_keep": 64,
    "n_discard": -32,
    "temperature": 0.0
}

We provide the following Docker image with ASan-instrumented build and minimal GGUF for independent verification:

docker pull songtli/secb.eval.x86_64.llama-cpp.cve-2026-21869:patch

Post-fix: same PoC runs clean under both ASan and Valgrind, n_discard normalizes to auto-discard. No new cppcheck warnings.

Requirements

  • I have read the contributing guidelines
  • AI: YES — Claude helped me to perform some debuggings. The vulnerability analysis, root cause investigation, fix design, and validation were performed by humans. The one-line fix (std::max(0, ...)) was human-conceived. Every line can be independently explained and defended.

Note

If this PR has been created by an AI agent on behalf of a contributor, the contributor is
still responsible for the PR and must review and understand the changes being proposed.
The contributor must also be able to explain and defend the changes when questioned.

llama.cpp restricts AI-generated contributions. See AGENTS.md and
CONTRIBUTING.md for more information.


Columbia University Security Research Team

…026-21869)

A negative n_discard from client JSON causes heap-buffer-overflow in
update_slots() context-shift loop (CWE-787, CVSS 8.8). Clamp to 0 at
ingress; n_discard=0 already triggers auto-discard (n_left/2).

Ref: GHSA-8947-pfff-2f3c
@SongTonyLi
SongTonyLi requested a review from a team as a code owner April 22, 2026 23:39
@ggml-gh-bot

ggml-gh-bot Bot commented Apr 22, 2026

Copy link
Copy Markdown

Hi @SongTonyLi, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • AI-generated content: This project does not accept PRs, descriptions or commit messages that are fully or predominantly AI-generated. If you have used AI to assist you in writing code, please make sure to disclose that explicitly.

Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

@SongTonyLi

Copy link
Copy Markdown
Contributor Author

@ggml-bot check one more time, I wrote the PR fully myself.

Comment thread tools/server/server-task.cpp Resolved
Comment thread tools/server/server-task.cpp Resolved
Comment thread tools/server/server-task.cpp Resolved
@ggerganov ggerganov added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Apr 23, 2026

@ngxson ngxson left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

A general note is that it should be a requirement to specify this very clearly on the title of the bug report when it is public:

(requires context shifting - not enabled by default)

We don't want to spread panic among users after all. Features that are prone to bugs / errors are already disabled by default.

@ServeurpersoCom ServeurpersoCom left a comment

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.

LGTM. Verified n_discard has a single external entry point at the JSON parse boundary (server-task.cpp) and a single usage site at the context shift (server-context.cpp), with no other paths in include/, src/, or common/. The clamp is at the right spot, and the semantics are preserved since n_discard == 0 falls through to the existing n_left / 2 auto-discard path. +1 on @ngxson's point about flagging the context-shift requirement in public advisories.

@SongTonyLi

Copy link
Copy Markdown
Contributor Author

Thank you all for the quick responses. When is it gonna be merged? I feel like this vulnerability is pretty severe.

@ServeurpersoCom
ServeurpersoCom merged commit c78fb90 into ggml-org:master Apr 23, 2026
45 of 46 checks passed
@ServeurpersoCom

Copy link
Copy Markdown
Contributor

Thank you all for the quick responses. When is it gonna be merged? I feel like this vulnerability is pretty severe.

Now:)

@SongTonyLi

Copy link
Copy Markdown
Contributor Author

additional credit: ARiSE Lab @ Columbia University

@SongTonyLi

Copy link
Copy Markdown
Contributor Author

@ggerganov looks like this is merged, can you also update the patched versions at GHSA-8947-pfff-2f3c?

@SongTonyLi
SongTonyLi deleted the fix/cve-2026-21869-n_discard-clamp branch April 23, 2026 19:35
@SongTonyLi

Copy link
Copy Markdown
Contributor Author

@ServeurpersoCom Can you update the patched versions at GHSA-8947-pfff-2f3c?

@ServeurpersoCom

Copy link
Copy Markdown
Contributor

@ServeurpersoCom Can you update the patched versions at GHSA-8947-pfff-2f3c?

I don't have edit rights on the GHSA itself (repository write access doesn't grant advisory edit permissions).
@ggerganov the "Patched versions" field can be updated to >= c78fb90

IntelNav pushed a commit to IntelNav/llama.cpp that referenced this pull request Apr 29, 2026
…869) (ggml-org#22267)

* server: clamp n_discard to non-negative at JSON parse boundary (CVE-2026-21869)

A negative n_discard from client JSON causes heap-buffer-overflow in
update_slots() context-shift loop (CWE-787, CVSS 8.8). Clamp to 0 at
ingress; n_discard=0 already triggers auto-discard (n_left/2).

Ref: GHSA-8947-pfff-2f3c

* cont : cleaner

* cont : cleanerer

* cont : cleanest

---------

Co-authored-by: Georgi Gerganov <[email protected]>
LifesLight pushed a commit to LifesLight/custom.llama.cpp that referenced this pull request May 4, 2026
…869) (ggml-org#22267)

* server: clamp n_discard to non-negative at JSON parse boundary (CVE-2026-21869)

A negative n_discard from client JSON causes heap-buffer-overflow in
update_slots() context-shift loop (CWE-787, CVSS 8.8). Clamp to 0 at
ingress; n_discard=0 already triggers auto-discard (n_left/2).

Ref: GHSA-8947-pfff-2f3c

* cont : cleaner

* cont : cleanerer

* cont : cleanest

---------

Co-authored-by: Georgi Gerganov <[email protected]>
my-other-github-account pushed a commit to my-other-github-account/llama.cpp that referenced this pull request May 15, 2026
…869) (ggml-org#22267)

* server: clamp n_discard to non-negative at JSON parse boundary (CVE-2026-21869)

A negative n_discard from client JSON causes heap-buffer-overflow in
update_slots() context-shift loop (CWE-787, CVSS 8.8). Clamp to 0 at
ingress; n_discard=0 already triggers auto-discard (n_left/2).

Ref: GHSA-8947-pfff-2f3c

* cont : cleaner

* cont : cleanerer

* cont : cleanest

---------

Co-authored-by: Georgi Gerganov <[email protected]>
baramofme pushed a commit to baramofme/llama-cpp-turboquant that referenced this pull request May 23, 2026
…869) (ggml-org#22267)

* server: clamp n_discard to non-negative at JSON parse boundary (CVE-2026-21869)

A negative n_discard from client JSON causes heap-buffer-overflow in
update_slots() context-shift loop (CWE-787, CVSS 8.8). Clamp to 0 at
ingress; n_discard=0 already triggers auto-discard (n_left/2).

Ref: GHSA-8947-pfff-2f3c

* cont : cleaner

* cont : cleanerer

* cont : cleanest

---------

Co-authored-by: Georgi Gerganov <[email protected]>
wel97459 pushed a commit to wel97459/llama-cpp-turboquant that referenced this pull request Jun 4, 2026
…869) (ggml-org#22267)

* server: clamp n_discard to non-negative at JSON parse boundary (CVE-2026-21869)

A negative n_discard from client JSON causes heap-buffer-overflow in
update_slots() context-shift loop (CWE-787, CVSS 8.8). Clamp to 0 at
ingress; n_discard=0 already triggers auto-discard (n_left/2).

Ref: GHSA-8947-pfff-2f3c

* cont : cleaner

* cont : cleanerer

* cont : cleanest

---------

Co-authored-by: Georgi Gerganov <[email protected]>
MrLordCat referenced this pull request in MrLordCat/llama.cpp-rdna-lab Jul 16, 2026
…869) (#22267)

* server: clamp n_discard to non-negative at JSON parse boundary (CVE-2026-21869)

A negative n_discard from client JSON causes heap-buffer-overflow in
update_slots() context-shift loop (CWE-787, CVSS 8.8). Clamp to 0 at
ingress; n_discard=0 already triggers auto-discard (n_left/2).

Ref: GHSA-8947-pfff-2f3c

* cont : cleaner

* cont : cleanerer

* cont : cleanest

---------

Co-authored-by: Georgi Gerganov <[email protected]>
zommiommy pushed a commit to zommiommy/llama.cpp that referenced this pull request Aug 18, 2026
…869) (ggml-org#22267)

* server: clamp n_discard to non-negative at JSON parse boundary (CVE-2026-21869)

A negative n_discard from client JSON causes heap-buffer-overflow in
update_slots() context-shift loop (CWE-787, CVSS 8.8). Clamp to 0 at
ingress; n_discard=0 already triggers auto-discard (n_left/2).

Ref: GHSA-8947-pfff-2f3c

* cont : cleaner

* cont : cleanerer

* cont : cleanest

---------

Co-authored-by: Georgi Gerganov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

examples merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. server

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants