is_likely_safe() only checks the first pickle stream in a file

Open
#346 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
30/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
security

Research direction

Start at is_likely_safe() in fickling/analysis.py (around lines 686-692), which calls Pickled.load(..., fail_on_decode_error=False) and so reads only the first stream. Compare with how fickling/cli.py:312 and fickling/loader.py:246 use StackedPickle.load(). Run the reproducer in the issue, which writes a stacked file and calls is_likely_safe(), and confirm it returns False once every stream is checked. Done means is_likely_safe() agrees with the CLI on stacked and legacy PyTorch files. A fix PR is reportedly under way but is not linked, and the report is under embargo, so check with maintainers before starting.

Written by the indexing model from the issue text.

Description

fickling.is_likely_safe(filepath) (fickling/analysis.py:686-692) reads the file with Pickled.load(f, fail_on_decode_error=False), which stops at the first STOP opcode. Later pickle streams in the same file are not decoded or passed to check_safety(), so the result depends on the first stream only. The CLI safety check (fickling/cli.py:312) and fickling.loader (fickling/loader.py:246) read the whole file with StackedPickle.load(), so they can disagree with is_likely_safe() on the same file.

Reproducer with the stdlib pickle module and the opcode API (nothing is unpickled):

import pickle
import tempfile
from pathlib import Path

import fickling
import fickling.fickle as op
from fickling.analysis import check_safety
from fickling.fickle import Pickled

second = Pickled(
    [
        op.Proto.create(4),
        op.ShortBinUnicode("os"),
        op.ShortBinUnicode("system"),
        op.StackGlobal(),
        op.Stop(),
    ]
)
print(check_safety(second).severity)  # LIKELY_OVERTLY_MALICIOUS on its own

with tempfile.TemporaryDirectory() as d:
    path = Path(d) / "stacked.pkl"
    path.write_bytes(pickle.dumps({"label": 1}) + second.dumps())
    print(fickling.is_likely_safe(str(path)))  # True on master

The same applies to PyTorch files in the legacy non-zip format, which hold five pickle streams where the first is only the magic number: is_likely_safe() returns True for them whatever the object stream contains, while fickling --check-safety on the same file exits 1.

Reported privately as GHSA-vv58-2pw5-4fcf and opened here at the maintainers' request. A fix is in a pull request that switches is_likely_safe() to StackedPickle.load() and requires every stream to be rated LIKELY_SAFE.

Dominant language
Python
Stars
678
Forks
89
Avg merge
41m
Merged PRs (30d)
14

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from trailofbits/fickling

All issues in trailofbits/fickling

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.