Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Insecure Data Processing - Timing Attack Against Secret - High (7.4) #649

Open
gbiagomba opened this issue Feb 12, 2025 · 1 comment

Comments

@gbiagomba
Copy link

gbiagomba commented Feb 12, 2025

Risk: High (7.5)

CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:N/A:L

Score Explanation:
The vulnerability has a high confidentiality impact (C:H) because a successful timing attack can leak sensitive information. The issue is exploitable remotely (AV:N), though it requires significant effort and precision (AC:H). No privileges are required (PR:N), and the attack does not require user interaction (UI:N). Integrity is unaffected (I:N), but availability can be minimally impacted due to computational overhead (A:L).


Executive Summary

DeepSeek-V3, a Mixture-of-Experts (MoE) LLM, is vulnerable to a timing attack against secret verification due to its use of a non-constant-time comparison routine in token processing. This flaw could allow an attacker to infer secret values, such as authentication tokens or cryptographic keys, by measuring response times. While DeepSeek-V3 delivers state-of-the-art performance, this issue poses a serious risk to applications that rely on it for secure processing of sensitive inputs.


Detail Finding:

While performing a security code review of DeepSeek, we observed the noted code repo is susceptible to a Timing Attack Against Secret. Specifically, the vulnerability arises from how prompt_mask is computed and used in DeepSeek-V3's inference pipeline. Particularly, the comparison logic at lines 59 and 68 in generate.py introduces timing discrepancies based on token values.

  1. Line 59 (prompt_mask = tokens != -1) checks whether a token exists but does not use a constant-time approach, leading to processing time variations based on token content.
  2. Line 68 (finished |= torch.logical_and(~prompt_mask[:, cur_pos], next_token == eos_id)) introduces further timing variations when checking the end-of-sequence (EOS) token.

The timing attack vulnerability we’ve identified in DeepSeek-V3 aligns with the following OWASP Top 10 for Large Language Model (LLM) Applications 2025 categories:

  1. LLM02:2025 Sensitive Information Disclosure: This category addresses scenarios where LLMs inadvertently expose confidential data, including personal identifiable information (PII), financial details, or proprietary business information. In the context of DeepSeek-V3, the timing attack could allow attackers to infer sensitive information by analyzing response times, leading to unauthorized data access and privacy violations. 
  2. LLM08:2025 Vector and Embedding Weaknesses: This risk pertains to vulnerabilities in systems utilizing vectors and embeddings, especially in Retrieval Augmented Generation (RAG) setups. Weaknesses in how vectors and embeddings are generated, stored, or retrieved can be exploited to inject harmful content, manipulate model outputs, or access sensitive information. In DeepSeek-V3, the non-constant-time verification routine could be exploited to manipulate embeddings, leading to potential data leakage or unauthorized access. 

Impact:

An attacker could craft special inputs, measure response delays, and infer private data by exploiting these timing inconsistencies. Additionally, an attacker could perform the following attacks:

  • Sensitive token leakage: Attackers could extract secret keys, authentication tokens, or model-internal data through statistical analysis of response times.
  • Potential model poisoning: If DeepSeek is used in a multi-tenant environment, adversaries could deduce how different input sequences affect the model’s state.
  • Increased risk in security-critical deployments: AI-driven access control mechanisms, chatbots handling confidential queries, or secure computations are at risk.

While DeepSeek-V3 offers efficient inference and high performance, this timing flaw could undermine its security, especially in sensitive use cases.


Affected Assets:

Affected File(s):

  • DeepSeek-V3/inference/generate.py:59
  • DeepSeek-V3/inference/generate.py:68

Evidence:

DeepSeek-V3/inference/generate.py:59

    prompt_mask = tokens != -1

DeepSeek-V3/inference/generate.py:68

        finished |= torch.logical_and(~prompt_mask[:, cur_pos], next_token == eos_id)

Replicate Finding:

  1. Clone the impacted repo: git clone https://github.com/deepseek-ai/DeepSeek-V3
  2. Navigate into the noted repo
  3. Open the impacted file(s)
  4. Got to impacted line

Mitigation/Remediation:

We recommend implementing constant-time operations for secret-dependent comparisons. Instead of directly comparing tokens, leverage PyTorch’s optimized cryptographic-safe operations. Additionally, we recommend the following hardening steps:

  1. Use padding techniques to mask timing variances.
  2. Normalize execution times to reduce distinguishability.
  3. Conduct differential analysis to detect timing discrepancies in responses.

Fix (Using Constant-Time Masking)

Modify generate.py to ensure constant-time computation:

import torch

# Secure comparison to avoid timing leaks
def constant_time_mask_comparison(tokens, value):
    return torch.eq(tokens, value).to(dtype=torch.uint8)

# Fix for line 59
prompt_mask = constant_time_mask_comparison(tokens, -1)

# Fix for line 68
finished |= torch.logical_and(~prompt_mask[:, cur_pos], next_token == eos_id)

For more information & context, please see the reference section below.

Please ensure that the above patch is applied to all affected software, services, applications, instances or systems managed by the team.


References:

  1. Constant-Time Comparison in PyTorch: https://pytorch.org/docs/stable/generated/torch.eq.html
  2. Preventing Timing Attacks in Deep Learning: https://arxiv.org/abs/2006.06766
  3. https://owasp.org/www-project-top-10-for-large-language-model-applications/
  4. https://owaspai.org/docs/ai_security_overview/
@WalkerSong
Copy link

牛逼

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

No branches or pull requests

2 participants