Skip to content

Fix SIGSEGV in pack-weight unpack/GEMM on malformed blob#1928

Merged
luoyu-intel merged 5 commits into
mainfrom
fix/ark-pack-weight-validation
Jun 22, 2026
Merged

Fix SIGSEGV in pack-weight unpack/GEMM on malformed blob#1928
luoyu-intel merged 5 commits into
mainfrom
fix/ark-pack-weight-validation

Conversation

@luoyu-intel

Copy link
Copy Markdown
Contributor

Summary

Add C++-level validation after BestLA blob deserialization to reject corrupted/metadata-tampered packed-weight blobs before they can cause out-of-bounds memory access (SIGSEGV).

Background

A vulnerability was reported where a malformed packed weight blob (e.g., one byte flipped at offset 64) causes a segmentation fault in auto_round_kernel.unpack_weight(). The BestLA packed format embeds metadata (dimensions, block size, buffer offsets, etc.) in the blob itself. If those fields are corrupted, the deserialized pointers become invalid, leading to out-of-bounds reads.

Changes

cpu_wrapper.hpp — CPU (BestLA) path

  • Added _validate_storage_weight() that validates all deserialized metadata before set_buffers():
    • n, k > 0 and ≤ 16M
    • npad ≥ n, kpad ≥ k
    • block > 0 and block ≤ k
    • n_block consistent with kpad / block
    • Layout must be NPack
    • misc_size + buf_size ≤ blob_count (if available)
  • Called in BTLAGemmUnpack, BTLAWOQGemmForwardWorkspace, BTLAWOQGemmFp32Forward.

xpu_wrapper.hpp — XPU path

  • unpackq() and woq_gemm() validate blob_count ≥ get_packw_size().

ark.cpp — Binding layer

  • unpack_weight() and woqgemm() accept and forward blob_numel.

__init__.py — Python API

  • Added _validate_packed_blob() for parameter type validation.
  • _unpack_weight_core() and woqgemm() pass blob.numel() / B.numel() to native calls.
  • _repack_quantized_weight_core() validates input tensor shapes.

luoyu-intel and others added 5 commits June 17, 2026 10:29
Add C++-level validation after BestLA blob deserialization to reject
corrupted/metadata-tampered packed-weight blobs before they can cause
out-of-bounds memory access.

CPU (BestLA) path - cpu_wrapper.hpp:
  _validate_storage_weight() checks n, k, npad, kpad, block size,
  n_block consistency, NPack layout, and total buffer size against
  blob_count. Called in BTLAGemmUnpack, BTLAWOQGemmForwardWorkspace,
  and BTLAWOQGemmFp32Forward before set_buffers().

XPU path - xpu_wrapper.hpp:
  unpackq() and woq_gemm() validate blob_count against get_packw_size().

Binding layer - ark.cpp:
  Forward blob_numel from Python to native code.

Python - __init__.py:
  _validate_packed_blob() validates parameter types at the Python layer.
  blob.numel() is passed to native calls for size validation.
Add forward declaration before the 3-arg compatibility wrapper to prevent
recursive call when the 4-arg overload is defined later in the same
translation unit.
@luoyu-intel luoyu-intel requested a review from a32543254 June 18, 2026 08:48

@a32543254 a32543254 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

@chensuyue chensuyue added this to the 0.14.0 milestone Jun 22, 2026
@chensuyue

Copy link
Copy Markdown
Contributor

/azp run Unit-Test-CUDA-AutoRound

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines could not run because the pipeline triggers exclude this branch/path.

@luoyu-intel luoyu-intel merged commit 495e253 into main Jun 22, 2026
39 checks passed
@luoyu-intel luoyu-intel deleted the fix/ark-pack-weight-validation branch June 22, 2026 07:08
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.

3 participants