Skip to content

fix(triton): bound-check checkpoint g_idx to prevent OOB dequant read (#2949) - #2950

Merged
Qubitium merged 1 commit into
ModelCloud:mainfrom
Anai-Guo:fix/triton-g-idx-bounds
Jul 14, 2026
Merged

Qubitium merged 1 commit into
ModelCloud:mainfrom
Anai-Guo:fix/triton-g-idx-bounds

Conversation

@Anai-Guo

Copy link
Copy Markdown
Contributor

Summary

Fixes #2949 — an out-of-bounds device read (CWE-125) in the Triton dequant path when loading an untrusted GPTQ checkpoint.

The Triton dequant kernel (gptqmodel/nn_modules/triton_utils/dequant.py) resolves each row's group as groups = where(g_idx < 0, g_idx + num_groups, g_idx) and then indexes scales/qzeros with groups with no upper-bound check, where num_groups = scales.shape[0]. Because TritonV2Linear.post_init leaves g_idx regeneration disabled, a crafted desc_act=True checkpoint whose g_idx exceeds num_groups reaches the kernel verbatim and reads past the buffer — crashing the CUDA context (DoS) and potentially pulling adjacent GPU memory into the dequantized weights. The Torch backend's scales[g_idx] gather faults safely on the same input; only the Triton path reads raw.

Fix

Validate g_idx once at load in TritonV2Linear.post_init, rejecting values outside [-num_groups, num_groups). That range is exactly what the kernel's negative-wrap can resolve safely and what the Torch backend's scales[g_idx] gather accepts, so the guard only rejects inputs that already fault safely on the Torch path — no legitimate checkpoint is affected. It runs once at load, not on the per-forward hot path.

I kept the change to the load-side guard alone (rather than also masking the load inside the kernel) because rejecting at load fully prevents the bad g_idx from ever reaching the device; a kernel-side groups < num_groups mask would be reasonable defense-in-depth if you prefer it.

Tests

tests/test_triton_g_idx_bounds.py — CPU-only unit tests for the pure validator (no GPU / Triton needed): in-range and negative-wrap-in-range pass; g_idx >= num_groups and g_idx < -num_groups are rejected; None/empty buffers are no-ops. The accept/reject boundaries were cross-checked against torch.zeros((num_groups, n))[g_idx] — they match the Torch backend's safe-fault semantics exactly.

🤖 Generated with Claude Code

…ModelCloud#2949)

The Triton dequant kernel resolves each row's group as
`where(g_idx < 0, g_idx + num_groups, g_idx)` and then indexes the scales/qzeros
buffers with no upper-bound check, where `num_groups = scales.shape[0]`. Because
the Triton QuantLinear leaves g_idx regeneration disabled in post_init, a crafted
`desc_act=True` checkpoint whose g_idx exceeds num_groups reaches the kernel
verbatim and reads out of bounds on the device (CWE-125: crash / possible
adjacent-memory disclosure when loading an untrusted model).

Validate g_idx once at load in TritonV2Linear.post_init, rejecting values outside
`[-num_groups, num_groups)` — exactly the range the Torch backend's `scales[g_idx]`
gather accepts, so this only rejects inputs that already fault safely there. The
check is a one-time load-side guard, not per-forward overhead.

Adds CPU-only unit tests for the pure validator (no GPU / Triton required).
@Qubitium
Qubitium merged commit 877c732 into ModelCloud:main Jul 14, 2026
@Qubitium

Copy link
Copy Markdown
Collaborator

@Anai-Guo Thank you!

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.

Out-of-bounds read in the Triton dequant kernel via an unchecked checkpoint g_idx (CWE-125)

2 participants