Skip to content

[Security] trust_remote_code=False is bypassed: load, save and dequantize import checkpoint code (CWE-94) #3968

Closed

Description

@Qubitium

Summary

With trust_remote_code=False (the default), several load, save and dequantize paths still import Python files from the model snapshot. Loading or re-saving an untrusted Hub repo can therefore run arbitrary code. Audited at a16977e.

Findings

1. GPTQModel.load() / from_pretrained() import auto_map classes when trust is off (Critical, CWE-94/829)

  • Sink: gptqmodel/utils/hf.py:1900-1923. prepare_remote_model_init_compat() reads config.auto_map["AutoModelForCausalLM"] (falling back to AutoModelForVision2Seq, or AutoModel for locateanything). It passes the value to transformers.dynamic_module_utils.get_class_from_dynamic_module(class_ref, model_path), which has no trust check of its own.
  • Reached from:
    • Load: loader.py:1789 calls AutoConfig.from_pretrained(..., trust_remote_code=trust_remote_code), then loader.py:1801 calls prepare_remote_model_init_compat(model_local_path, config) without passing the flag.
    • Quantize: the same pattern at loader.py:1307 → 1313.
  • Why the existing gate misses it: a config with a native model_type (e.g. llama) plus an auto_map entry loads as a plain built-in config and keeps auto_map as an attribute. Model selection then picks a native QModel, so the require_trust_remote_code gate (loader.py:1722) never fires, and the snapshot module is imported anyway.
  • Cross-repo variant: transformers' repo--module.Class syntax also lets class_ref pull code from a different Hub repo.
  • Related: normalize_hf_config_compat (hf.py:1729-1735) also turns on remote-code compat patches when only auto_map is present (has_remote_automap).
  • Verified: a standalone reproduction with transformers 5.22.2 imported the snapshot module.

2. Re-saving a loaded quantized model forces trust_remote_code=True (High, CWE-94)

  • Sink: gptqmodel/models/writer.py:2140-2147. get_model_with_quantize() calls AutoConfig.from_pretrained(model_id_or_path, trust_remote_code=True).
  • Reached from: GPTQModel.load(untrusted) followed by .save() / save_quantized(), through writer.py:1861. This path runs for any non-EXL3 re-save, conversion, or push of a downloaded quant.
  • Trigger: an auto_map.AutoConfig entry is ignored at load (trust=False) but executed at save. This is independent of finding 1.
  • Fix: use self.trust_remote_code.

3. dequantize_model() hard-codes trust_remote_code=True (High, CWE-94)

  • Sinks: gptqmodel/utils/model_dequant.py:335 and :354, on the compressed-tensors path. The format is chosen from checkpoint data: quant_method=compressed-tensors with format=pack-quantized, or any .weight_packed key.
  • Reached from: the public dequantize_model() and scripts/dequantize_model.py. The API has no trust parameter.
  • Fix: add trust_remote_code: bool = False and pass it through.

4. Hardening

  • Processor loading ignores the caller's trust flag:
    • base.py:746 uses self.require_trust_remote_code instead of the user's value.
    • Several load_processor() methods hard-code trust_remote_code=True, for example deepseek_vl_v2.py:162, minicpmv.py:94, openvla.py:177 and nemotron_omni.py:143.
    • Today these are protected only indirectly, by the loader's trust gate.
  • KimiK3Source runs snapshot code with no opt-in: utils/kimi_k3.py:24-57 load_calibration_reference execs configuration_kimi_k3.py / modeling_kimi_linear.py from the snapshot with no opt-in. It is reached only through the explicit KimiK3*Source research APIs, but those should require trust_remote_code=True.

Suggested fix

  • Pass the user's trust_remote_code into prepare_remote_model_init_compat and normalize_hf_config_compat, and return early when it is False.
  • Never hard-code True.
  • Add a regression test that loads, saves and dequantizes a checkpoint whose auto_map points at a module that writes a marker file, with trust=False, and asserts the marker is absent.
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