[Bug] basicsr/utils/options.py uses the unsafe yaml.Loader (CLoader), so a crafted config executes code

Author: Carlson-JLQCreated Sep 20, 2026Updated Sep 20, 2026

Summary

CodeFormer vendors a copy of basicsr/, and its config loading uses a non-safe YAML loader. ordered_yaml() uses the full PyYAML loader to get OrderedDict support, and both branches it can take are unsafe. Loading a crafted config therefore executes arbitrary code in the process.

Location

python
# basicsr/utils/options.py:7-29
def ordered_yaml():
    """Support OrderedDict for yaml."""
    try:
        from yaml import CDumper as Dumper
        from yaml import CLoader as Loader      # <- full loader, unsafe
    except ImportError:
        from yaml import Dumper, Loader        # <- full loader, unsafe
    ...
    Loader.add_constructor(_mapping_tag, dict_constructor)
    return Loader, Dumper
python
# basicsr/utils/options.py:32-44
def parse(opt_path, root_path, is_train=True):
    """Parse option file."""
    with open(opt_path, mode='r') as f:
        Loader, _ = ordered_yaml()
        opt = yaml.load(f, Loader=Loader)       # <- unsafe load

Entry point: basicsr/train.py:30 → opt = parse(args.opt, root_path, is_train=is_train).

Root cause

Neither branch is a safe loader.

  • yaml.CLoader (preferred when libyaml is available) is the full loader;
  • yaml.Loader (the ImportError fallback) is the full loader.

Both support tags such as !!python/object/apply, which let yaml.load invoke arbitrary Python callables during parsing. PyYAML's own docs say to use safe_load / SafeLoader for untrusted input.

The OrderedDict requirement does not need the full loader — a SafeLoader with a custom mapping constructor does the same job (see the fix below).

Steps to reproduce

python
import os, tempfile, yaml

p = tempfile.mktemp(suffix=".yml")
with open(p, "w") as f:
    f.write('!!python/object/apply:os.system ["touch /tmp/CF_MARK"]\n')

# fallback branch (what ordered_yaml() returns without libyaml)
with open(p) as f:
    yaml.load(f, Loader=yaml.Loader)          # -> executes os.system

# preferred branch (what it returns *with* libyaml) is also unsafe:
yaml.load('!!python/object/apply:os.system ["true"]', Loader=yaml.CLoader)

Observed:

yaml.Loader fallback -> marker exists: True
CLoader: loaded (also unsafe)

Expected results

Parsing a config should build plain data structures only; !!python/... tags should raise an error instead of executing anything (i.e. SafeLoader behavior).

Actual results

A crafted config executes arbitrary commands in the loading process.

Impact

CodeFormer configs live in options/ and are routinely shared and copied between users. Anyone who can get a user to load a crafted config — or tamper with one they fetch — gets arbitrary code execution at that process's privilege level.

A local, self-authored config is not a trust boundary on its own, so exploitability depends on the config's provenance, which in practice is often third-party.