Skip to content

[Memory safety] Native kernels index with unchecked checkpoint tensors (ExLlamaV2 g_idx, NF12 escapes, ParoQuant pairs, VPTQ outliers, EXL3 K) #3971

Closed

Description

@Qubitium

Summary

Several native ops index memory using integer tensors loaded from the checkpoint, with no bounds checks. A crafted quantized checkpoint can therefore cause host heap corruption or out-of-bounds GPU reads and writes. These are separate sinks from #2949 (Triton g_idx), #3922 (NF12 embedding token IDs) and #3919 (ParoQuant shapes). Audited at a16977e by reading the source; nothing was built or reproduced on hardware.

Findings

1. ExLlamaV2 make_q_matrix: host heap OOB write from g_idx (High)

  • Entry: gptqmodel_ext/exllamav2/ext_gptq.cpp:10 checks only that g_idx is int32. It does not check values, length, or that the tensor is on CPU.
  • Path: cuda/q_matrix.cu:131 → make_sequential.
  • Sinks:
    • :553 cpu_g_idx_map = calloc(groups, 4), then :559 cpu_g_idx_map[cpu_g_idx[i]]++. The uint32 index comes from the checkpoint with no bound, giving a heap write at a chosen offset.
    • :575-576 and :582 cpu_x_map[cpu_x_map_inv[row]] = row write further out of bounds.
  • Reached at load time: ExllamaV2Linear.post_init → _ensure_q_handle → exllamav2_make_q_matrix(..., w["g_idx"].cpu()) (nn_modules/qlinear/exllamav2.py:~341). No forward pass is needed. ExLlamaV2 is selected when Marlin is unavailable (e.g. sm75) or chosen explicitly.
  • Gap: validate_g_idx_bounds (nn_modules/qlinear/_g_idx.py, the Out-of-bounds read in the Triton dequant kernel via an unchecked checkpoint g_idx (CWE-125) #2949 fix) is not called from exllamav2.py.
  • 1b: short truncation survives even valid g_idx:
    • :565 short tmp = cpu_g_idx_map[i] truncates the per-group count.
    • With in_features ≥ 32768 and one group holding ≥ 32768 rows, the prefix sum goes negative and :582 writes at about cpu_x_map[0xFFFF8000].
    • A range check alone does not fix this.
  • Fix:
    • TORCH_CHECK(g_idx.is_cpu() && g_idx.numel() == height).
    • Check 0 <= g_idx < groups.
    • Use uint32_t for tmp.
    • Assert target_row < height before writing.

2. NF12 linear/decode: escape index g.lo0 has no bound (High/Med; CUDA and HIP)

  • Entry: gptqmodel_ext/nf12/csrc/torch/ops.cpp:110-135 weight_view, used by decode, decode_fp32, sub_fp32, linear and linear_staged_b1.
    • It checks shapes, dtype and alignment, but never checks escape indices against escapes.size(0).
    • When escapes is empty, w.escapes is nullptr.
  • Sinks:
    • include/nf12/format.cuh:78 and :107 (__ldg(escapes + g.lo0)).
    • linear/gemv.cu:159.
    • linear/mma.cuh:523/720/868/1034.
    • linear/sm100/large_m_tc_kernel.cuh:670.
    • HIP: nf12_hip/nf12_format_hip.cuh:134-145.
  • The bound was intended: codec/embedding.cu:57 and the sm90/sm120 kernels already check g.lo0 < num_escapes.
  • Reached from: the checkpoint's nf12_packed / nf12_escapes (resized to the checkpoint's shape at utils/model.py:2815-2839) → NF12Linear._native_forward / dequantize_weight, and gptqmodel_nf12.NF12Weight.from_state_dict. Post-init checks only the packed shape.
  • Impact: GPU OOB reads of up to about 64 GiB past the buffer. These crash, or decode other tensors' bytes into the weights.
  • Fix:
    • Pass num_escapes to the kernels and use the same guard as the embedding kernel.
    • Add a one-time load check that max(lo0 | meta==0) < escapes.size(0).

3. NF12 MoE: escape_offsets unchecked (Med, library API)

  • Entry: nf12/csrc/torch/ops.cpp:484-517 moe_weight_view checks only that escape_offsets is contiguous int64 with E+1 entries. It does not check that offsets are ≥ 0, monotone, or end at ≤ escapes.size(0).
  • Sinks:
    • moe/common.cuh:59-61
    • codec/decode.cu:155
    • sm80, sm90 and sm120 large kernels
    • nf12_moe.hip:287
    • nf12_wmma.hip:185
  • Reached from: NF12MoEWeight.from_state_dict.

4. ParoQuant CUDA rotation: pairs index shared memory (Med)

  • Entry: gptqmodel_ext/paroquant/rotation.cu:1003-1030 checks device, dtype and rank only. It does not check pair values, idx.size(1) == h, theta shape, or scales numel.
  • Sink: rotation.cuh:73-81 (float) and :148-160 (half) read and write x_grp[i*ROW_STRIDE+m] / x_grp[j*...]. Here i/j are signed int16 values from the checkpoint, while valid values are in [0, 128).
  • Reached from: a ParoQuant checkpoint with non-zero theta → ParoLinear._rotate_inputs → apply_paroquant_rotation → fused kernel. _rotation_kernel_ready does not check values.
  • Impact: OOB shared-memory writes, typically an illegal-address fault that kills the CUDA context.
  • Fix: validate 0 <= pairs < group_size once at load.

5. VPTQ CUDA: outlier_indices used as a codebook index (Med)

  • Entries: gptqmodel_ext/vptq/vptq_kernel.cu:428 and :491.
  • Sink: :73-74 load_float(outlier_centroids, code * outlier_vector_len + component), where code is any int32.
  • Gap: the bound is enforced only at quantize time (vptq_torch.py:395). The load-time _validate_loaded_state (vptq_torch.py:327) checks only the shape of indices.
  • Fix: check 0 <= outlier_indices < outlier_num_centroids in _validate_loaded_state.

6. ExLlamaV3 reconstruct: trellis K indexes a 24-entry kernel table (Med-High)

  • Entry: gptqmodel_ext/exllamav3/bindings.cpp:59 → quant/reconstruct.cu:100. Only packed.size(2) == 256*K/16 is checked.
  • Sink: :124-130 cbi = K-1 (+8 mcg / +16 mul1), then reconstruct_kernel_instances[cbi] (an unchecked std::array), then the launch.
    • K = 0, or K large enough that cbi ≥ 24, calls through a garbage function pointer.
    • K = 9..16 runs the wrong kernel.
  • Source of K: trellis.shape[-1] // 16, from quantization_config.tensor_storage. Any forward with more than 32 rows takes this path.
  • The GEMM path already checks: exl3_kernel_map.cu:207 enforces 1 <= K <= 8.
  • Same pattern elsewhere: pack.cu:92,175 (quantize-time only).
  • Fix: TORCH_CHECK(1 <= K && K <= 8 && !(mcg && mul1)).

7. Low

  • FloatX CPU dequant divides by zero: gptqmodel_ext/floatx_cpu.cpp:205 and :237/244/261 take % count with count == 0 for an empty scale tensor. This raises SIGFPE and kills the process. _fast_scale_arg explicitly allows numel() == 0; the path is reachable from NVFP4 dequant.
  • EXL3 per-device arrays have no bound: exllamav3/quant/exl3_devctx.cuh:13,24-27, exl3_devctx.cu:25-68 and exl3_gemm.cu:37,129-143 index MAX_DEVICES = 16 arrays without checking. Device ordinal ≥ 16 gives a host OOB write.

General suggestion

  • Add one load-time "index tensor validation" pass for every format: g_idx, NF12 escapes and offsets, ParoQuant pairs, VPTQ outlier indices, and EXL3 K.
  • Add matching TORCH_CHECKs at each C++ op entry, so that direct API callers are protected too.
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions