From f219788f91952827132fa4fdf916427cd20d225e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20P=C3=A9rez=20de=20Algaba?= <124347725+jperezdealgaba@users.noreply.github.com> Date: Thu, 11 Jun 2026 10:05:14 +0200 Subject: [PATCH] [Security] Fix info disclosure via int32 truncation in GGUF dequantize kernels (#44971) Signed-off-by: jperezde --- .../quantization/gguf/dequantize.cuh | 36 +++++++++---------- .../quantization/gguf/ggml-common.h | 2 +- .../quantization/gguf/gguf_kernel.cu | 24 +++++++------ 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/csrc/libtorch_stable/quantization/gguf/dequantize.cuh b/csrc/libtorch_stable/quantization/gguf/dequantize.cuh index 9d355003ef..e18577da56 100644 --- a/csrc/libtorch_stable/quantization/gguf/dequantize.cuh +++ b/csrc/libtorch_stable/quantization/gguf/dequantize.cuh @@ -78,8 +78,8 @@ static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const in } template -static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) { - const int i = 2*(blockDim.x*blockIdx.x + threadIdx.x); +static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int64_t k) { + const int64_t i = 2*((int64_t)blockDim.x*blockIdx.x + threadIdx.x); if (i >= k) { return; @@ -435,91 +435,91 @@ static __global__ void dequantize_block_iq4_xs(const void * __restrict__ vx, dst } template -static void dequantize_block_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) { - const int num_blocks = (k + 2*CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / (2*CUDA_DEQUANTIZE_BLOCK_SIZE); +static void dequantize_block_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int64_t k, cudaStream_t stream) { + const int64_t num_blocks = (k + 2*CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / (2*CUDA_DEQUANTIZE_BLOCK_SIZE); dequantize_block<<>>(vx, y, k); } template -static void dequantize_row_q2_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) { +static void dequantize_row_q2_K_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { const int nb = k / QK_K; dequantize_block_q2_K<<>>(vx, y); } template -static void dequantize_row_q3_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) { +static void dequantize_row_q3_K_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { const int nb = k / QK_K; dequantize_block_q3_K<<>>(vx, y); } template -static void dequantize_row_q4_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) { +static void dequantize_row_q4_K_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { const int nb = k / QK_K; dequantize_block_q4_K<<>>(vx, y); } template -static void dequantize_row_q5_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) { +static void dequantize_row_q5_K_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { const int nb = k / QK_K; dequantize_block_q5_K<<>>(vx, y); } template -static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) { +static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { const int nb = k / QK_K; dequantize_block_q6_K<<>>(vx, y); } template -static void dequantize_row_iq2_xxs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) { +static void dequantize_row_iq2_xxs_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { const int nb = k / QK_K; dequantize_block_iq2_xxs<<>>(vx, y); } template -static void dequantize_row_iq2_xs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) { +static void dequantize_row_iq2_xs_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { const int nb = k / QK_K; dequantize_block_iq2_xs<<>>(vx, y); } template -static void dequantize_row_iq2_s_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) { +static void dequantize_row_iq2_s_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { const int nb = k / QK_K; dequantize_block_iq2_s<<>>(vx, y); } template -static void dequantize_row_iq3_xxs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) { +static void dequantize_row_iq3_xxs_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { const int nb = k / QK_K; dequantize_block_iq3_xxs<<>>(vx, y); } template -static void dequantize_row_iq3_s_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) { +static void dequantize_row_iq3_s_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { const int nb = k / QK_K; dequantize_block_iq3_s<<>>(vx, y); } template -static void dequantize_row_iq1_s_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) { +static void dequantize_row_iq1_s_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { const int nb = k / QK_K; dequantize_block_iq1_s<<>>(vx, y); } template -static void dequantize_row_iq1_m_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) { +static void dequantize_row_iq1_m_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { const int nb = k / QK_K; dequantize_block_iq1_m<<>>(vx, y); } template -static void dequantize_row_iq4_nl_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) { +static void dequantize_row_iq4_nl_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { const int nb = (k + QK_K - 1) / QK_K; dequantize_block_iq4_nl<<>>(vx, y); } template -static void dequantize_row_iq4_xs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) { +static void dequantize_row_iq4_xs_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { const int nb = (k + QK_K - 1) / QK_K; dequantize_block_iq4_xs<<>>(vx, y); } diff --git a/csrc/libtorch_stable/quantization/gguf/ggml-common.h b/csrc/libtorch_stable/quantization/gguf/ggml-common.h index 6bef5db3cc..282875b8c7 100644 --- a/csrc/libtorch_stable/quantization/gguf/ggml-common.h +++ b/csrc/libtorch_stable/quantization/gguf/ggml-common.h @@ -1064,7 +1064,7 @@ typedef half dfloat; // dequantize float typedef half2 dfloat2; typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v); template -using to_cuda_ggml_t = void (*)(const void * __restrict__ x, dst_t * __restrict__ y, int k, cudaStream_t stream); +using to_cuda_ggml_t = void (*)(const void * __restrict__ x, dst_t * __restrict__ y, int64_t k, cudaStream_t stream); typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs); typedef void (*allocate_tiles_cuda_t)(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc); typedef void (*load_tiles_cuda_t)( diff --git a/csrc/libtorch_stable/quantization/gguf/gguf_kernel.cu b/csrc/libtorch_stable/quantization/gguf/gguf_kernel.cu index 2a56d7a18f..e90aa1565c 100644 --- a/csrc/libtorch_stable/quantization/gguf/gguf_kernel.cu +++ b/csrc/libtorch_stable/quantization/gguf/gguf_kernel.cu @@ -79,6 +79,7 @@ torch::stable::Tensor ggml_dequantize( W.get_device_index()); auto dtype_ = dtype.value_or(torch::headeronly::ScalarType::Half); auto DW = torch::stable::empty({m, n}, dtype_, std::nullopt, W.device()); + torch::stable::fill_(DW, 0.0); cudaStream_t stream = get_current_cuda_stream(); VLLM_STABLE_DISPATCH_FLOATING_TYPES(DW.scalar_type(), "ggml_dequantize", [&] { @@ -93,13 +94,14 @@ torch::stable::Tensor ggml_mul_mat_vec_a8( torch::stable::Tensor W, // quant weight torch::stable::Tensor X, // input int64_t type, int64_t row) { - int col = X.sizes()[1]; - int vecs = X.sizes()[0]; - const int padded = (col + 512 - 1) / 512 * 512; + int64_t col = X.sizes()[1]; + int64_t vecs = X.sizes()[0]; + const int64_t padded = (col + 512 - 1) / 512 * 512; const torch::stable::accelerator::DeviceGuard device_guard( X.get_device_index()); auto Y = torch::stable::empty({vecs, row}, X.scalar_type(), std::nullopt, W.device()); + torch::stable::fill_(Y, 0.0); cudaStream_t stream = get_current_cuda_stream(); auto quant_X = torch::stable::empty({vecs, padded / 32 * 9}, torch::headeronly::ScalarType::Int, @@ -213,13 +215,14 @@ torch::stable::Tensor ggml_mul_mat_vec_a8( torch::stable::Tensor ggml_mul_mat_a8(torch::stable::Tensor W, // quant weight torch::stable::Tensor X, // input int64_t type, int64_t row) { - int col = X.sizes()[1]; - int padded = (col + 512 - 1) / 512 * 512; - int batch = X.sizes()[0]; + int64_t col = X.sizes()[1]; + int64_t padded = (col + 512 - 1) / 512 * 512; + int64_t batch = X.sizes()[0]; const torch::stable::accelerator::DeviceGuard device_guard( X.get_device_index()); auto Y = torch::stable::empty({batch, row}, X.scalar_type(), std::nullopt, W.device()); + torch::stable::fill_(Y, 0.0); cudaStream_t stream = get_current_cuda_stream(); auto quant_X = torch::stable::empty({batch, padded / 32 * 9}, torch::headeronly::ScalarType::Int, @@ -291,12 +294,13 @@ torch::stable::Tensor ggml_moe_a8(torch::stable::Tensor X, // input torch::stable::Tensor num_tokens_post_padded, int64_t type, int64_t row, int64_t top_k, int64_t tokens) { - int col = X.sizes()[1]; - int padded = (col + 512 - 1) / 512 * 512; + int64_t col = X.sizes()[1]; + int64_t padded = (col + 512 - 1) / 512 * 512; const torch::stable::accelerator::DeviceGuard device_guard( X.get_device_index()); auto Y = torch::stable::empty({tokens * top_k, row}, X.scalar_type(), std::nullopt, W.device()); + torch::stable::fill_(Y, 0.0); cudaStream_t stream = get_current_cuda_stream(); auto quant_X = torch::stable::empty({tokens, padded / 32 * 9}, torch::headeronly::ScalarType::Int, @@ -395,8 +399,8 @@ torch::stable::Tensor ggml_moe_a8_vec( torch::stable::Tensor W, // expert weights torch::stable::Tensor topk_ids, int64_t top_k, int64_t type, int64_t row, int64_t tokens) { - int col = X.sizes()[1]; - const int padded = (col + 512 - 1) / 512 * 512; + int64_t col = X.sizes()[1]; + const int64_t padded = (col + 512 - 1) / 512 * 512; const torch::stable::accelerator::DeviceGuard device_guard( X.get_device_index()); auto Y = torch::stable::empty({tokens * top_k, row}, X.scalar_type(),