Skip to content

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

Closed

Description

@professor-moody

Summary. Loading and running inference on a crafted GPTQ checkpoint (desc_act=True) can trigger an out-of-bounds GPU memory read in GPTQModel's Triton dequant kernel. The per-row group index g_idx is loaded from the checkpoint and used to index the scales/qzeros buffers with no check that its values are within [0, num_groups), where num_groups is itself taken from the (attacker-supplied) scales tensor. A g_idx entry >= num_groups reads out of bounds, crashing the CUDA context (denial of service) and potentially exposing adjacent GPU memory. This requires loading an untrusted checkpoint; it is not reachable with a legitimate model.

Details. GPTQ checkpoints with activation reordering (desc_act=True) ship a g_idx tensor of shape (in_features,). On load, from_quantized → accelerate.load_checkpoint_in_model → set_module_tensor_to_device replaces the layer's config-derived buffers with the checkpoint tensors and does not re-validate them; the Triton layer's post_init has its g_idx regeneration commented out, so the file's g_idx values reach the kernel verbatim. The Triton dequant_kernel (gptqmodel/nn_modules/triton_utils/dequant.py) then computes:

g_idx  = tl.load(g_idx_ptr + row_idx, ...)
groups = tl.where(g_idx < 0, g_idx + num_groups, g_idx)   # only the negative-wrap; no upper bound
scales = tl.load(scales_ptr + (col_idx + out_features * groups), ...)   # no groups < num_groups check
qzeros = tl.load(qzeros_ptr + (qzero_ncols * groups + col_idx // pack_scale), ...)

with num_groups = scales.shape[0], out_features = scales.shape[1], in_features = g_idx.shape[0] — all read from the checkpoint. There is no groups < num_groups bound, so an out-of-range g_idx value indexes scales/qzeros past their extent. The qzeros load has the same unchecked groups factor. The safe Torch backend (nn_modules/qlinear/torch.py) performs the same gather with bounds-checked PyTorch indexing (scales_tile[g_idx]), which faults safely — only the Triton path reads raw.

Proof of concept. Confirmed on GPTQModel 7.1.0, torch 2.13.0+cu130, triton 3.7.1, NVIDIA A100, under compute-sanitizer --tool memcheck with PYTORCH_NO_CUDA_MEMORY_CACHING=1. Build a 4-bit desc_act=True GPTQ layer (in=256, out=128, group_size=128, so num_groups=2), load a g_idx tensor of the correct shape (256,) but with one out-of-range value (e.g. g_idx[0] = 6) via accelerate.set_module_tensor_to_device (the real load primitive accepts it with no bounds check), and run forward() on CUDA through the Triton dequant path. The valid case runs clean (0 errors); the crafted case reports an out-of-bounds read inside dequant_kernel:

Invalid __global__ read of size 4 bytes
    at dequant_kernel ... in dequant.py
    Access to 0x... is out of bounds
ERROR SUMMARY: 20 errors   (+ cudaErrorLaunchFailure)

On CPU, the Torch backend raises IndexError: index 6 is out of bounds for dimension 0 with size 2 on the same input, confirming the value is genuinely out of range and that only the Triton path reads unchecked.

Impact. An attacker who can get a victim to load and run a crafted GPTQ checkpoint (a model file, e.g. from a model hub) can crash the inference process (CUDA-context launch failure = denial of service) and potentially read adjacent GPU memory into the dequantized weights.

Suggested fix. Validate g_idx values against num_groups (clamp or reject g_idx >= num_groups) at load (post_init), and/or add a groups < num_groups guard in the Triton kernel, matching the safe behavior of the Torch backend.

Severity. Medium — CVSS:3.1/AV:L/AC:L/PR:N/UI:R/S:U/C:L/I:N/A:H = 6.1. CWE-125 (out-of-bounds read). Affected: gptqmodel <= 7.1.0 and current main.


Reported by Halo Forge Labs.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions