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

Aperta
#346 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
30/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
security

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
678
Fork
89
Merge medio
41m
PR unite (30g)
14

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di trailofbits/fickling

Tutte le issue di trailofbits/fickling

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.