Skip to content

rpc : reject malformed tensor dims and op in deserialize_tensor - #30474

Open
void-mckenzie wants to merge 1 commit into
ggml-org:masterfrom
void-mckenzie:rpc-reject-malformed-tensor-dims
Open

void-mckenzie wants to merge 1 commit into
ggml-org:masterfrom
void-mckenzie:rpc-reject-malformed-tensor-dims

Conversation

@void-mckenzie

Copy link
Copy Markdown

Closes #30458

Overview

deserialize_tensor passed the client-supplied ne[] straight into ggml_new_tensor_4d, so a single RPC request whose element count overflows int64_t hit GGML_ASSERT in ggml_new_tensor_impl and aborted the whole rpc-server process, dropping every connected client. The path is reachable from every tensor-deserializing command (SET_TENSOR, GET_ALLOC_SIZE, GRAPH_COMPUTE, ...).

This PR performs the same element-count product check as ggml.c up front and returns nullptr, matching how the other malformed-tensor cases in deserialize_tensor are already handled, so a malformed request only fails the command that carries it. It also rejects:

  • ne[0] values that are not a multiple of the tensor type's block size (previously asserted inside ggml_row_size)
  • op >= GGML_OP_COUNT (previously read out of bounds via ggml_op_name in the buffer-bounds error log)

A raw-socket regression case is added to test-rpc-multi-server: it sends the three malformed requests as GET_ALLOC_SIZE, asserts that each is rejected without a response (the server closes the connection instead of replying), and that the server keeps accepting new connections and answering HELLO. The check runs before ggml_backend_rpc_init() because the server serves clients one at a time, and the probe sockets use a receive timeout so a regression fails fast instead of hanging.

Additional information

  • The regression test fails on the unpatched code and passes with the fix, verified in both directions; the overflow case aborts the server at ggml.c's GGML_ASSERT.
  • Valid traffic is unaffected: a real RPC client computed an F32 ADD graph end-to-end over RPC with correct results.
  • Pre-existing, deliberately out of scope to keep this PR single-purpose: get_alloc_size stores a failed src deserialization as null without checking, which can still abort CUDA servers on malformed inputs. Happy to follow up separately.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: Yes, the fix and the regression test were developed with AI assistance, then manually reviewed and tested by me.

@void-mckenzie
void-mckenzie requested review from a team and ggerganov as code owners October 13, 2026 18:19
@github-actions github-actions Bot added testing Everything test related ggml changes relating to the ggml tensor library for machine learning labels Oct 13, 2026
@void-mckenzie
void-mckenzie force-pushed the rpc-reject-malformed-tensor-dims branch from 996965a to 78ea7c2 Compare October 13, 2026 18:20
@void-mckenzie

Copy link
Copy Markdown
Author

@am17an friendly ping in case this slipped by, it's a small one (and my first PR here). One question: should the overflow and block size checks stay in deserialize_tensor or move into ggml_new_tensor? Please let me know :)

@akushonkamen

Copy link
Copy Markdown

I ran into this independently and came to propose a fix — found this PR already covers it, so I verified it locally instead (Apple M1 Max, macOS 15, CPU backend):

Issue reproduced on the unpatched parent commit (78ea7c2^) — a raw GET_ALLOC_SIZE with all four ne = 0xFFFFFFFF kills the whole server:

ggml.c:1798: GGML_ASSERT(INT64_MAX / ne[i] > current_nelements) failed
  ggml_new_tensor_4d <- rpc_server::deserialize_tensor <- rpc_server::get_alloc_size

Fix verified at 78ea7c2:

  • same probe: connection closed with [deserialize_tensor] ... element count overflows int64_t logged, server stays alive and keeps answering HELLO
  • all three malformed cases hit their rejection paths (ne overflow / Q4_0 ne0 = 33 misalignment / op 0xFFFFFF), confirmed in the server log
  • ctest -R test-rpc-multi-server passes; llama-cli, ggml-rpc-server and the test all build clean

FWIW on your question about keeping the checks in deserialize_tensor vs moving them into ggml_new_tensor: the ne[0] % blck_size assert in ggml_row_size is a plain assert() (ggml.c:1353), so it compiles out in release builds — a misaligned ne[0] arriving over RPC would silently compute a wrong nbytes there rather than fail. Keeping the validation at the deserialization boundary, where the offending client values can be logged, seems right to me.

One optional nit: test-rpc-multi-server.cpp hardcodes RPC_CMD_GET_ALLOC_SIZE = 13, and the server-side enum only static_asserts RPC_CMD_HELLO == 14 (ggml-rpc.cpp:85). A static_assert(RPC_CMD_GET_ALLOC_SIZE == 13, ...) next to the existing one would keep the probe from silently targeting the wrong command if a command is ever inserted before it.

This branch has not been deployed

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

Labels

ggml changes relating to the ggml tensor library for machine learning testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rpc : a tensor with oversized ne aborts ggml-rpc-server in deserialize_tensor

2 participants