arbitrary code execution when loading a model via from_pickle (CWE-502)

Aperta
#820 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@ifaizk5 ci sta già lavorando.

Dal 2/12/2026.

  • #849 di @ifaizk5 — aperta

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
numpy, python, scikit-learn

Direzione di ricerca

Start with tslearn/bases/bases.py, especially from_pickle() around line 302 and the to_pickle() method around line 288. Review how estimator models are serialized and which classes they need during loading; the report proposes a restricted unpickler but does not identify a complete safe allow-list. Done means untrusted pickle data cannot execute arbitrary code while legitimate saved models still load.

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

Descrizione

documentation good first issue
Summary

Loading an attacker-supplied model file with a tslearn estimator's from_pickle() runs arbitrary Python code as the user. The classmethod passes the file to pickle.load, which executes any __reduce__ gadget in the stream during deserialization, before the model is returned. from_pickle() is the documented counterpart to to_pickle() and is inherited by every tslearn estimator, reached with no option set, so opening a shared or downloaded model file is enough.

Details

The entry point is the base-class loader that every tslearn estimator inherits, in tslearn/bases/bases.py (main at d0708f25ad68):

302     def from_pickle(cls, path):
...
315         model = pickle.load(open(path, 'rb'))

from_pickle() opens path (a path the caller supplies) and calls pickle.load on it with no restricted unpickler and no allow-list. pickle.load runs a crafted file's __reduce__ at load time, so the whole file is attacker-controlled code. Models are meant to be saved and shared (to_pickle() writes this format at line 288), so a received model file is a normal input, not an unusual one.

Proof of concept

Tested with tslearn 0.10.0, Python 3.13 on Linux.

Build a file whose object runs a benign marker, then load it the documented way:

import pickle, os

class Evil:
    def __reduce__(self):
        return (os.system, ("id > /tmp/tslearn_poc",))

with open("model.pkl", "wb") as f:
    pickle.dump(Evil(), f)

from tslearn.clustering import TimeSeriesKMeans
TimeSeriesKMeans.from_pickle("model.pkl")

/tmp/tslearn_poc then holds the output of id; the command runs before from_pickle() returns. The control is the same call on a legitimate model saved with to_pickle(): it loads to its estimator and runs nothing. The gadget is the only difference between the two runs.

Impact

Any user that loads a tslearn model received from someone else (a shared trained estimator, a downloaded artifact, a file attached to a report) runs that sender's code with their own privileges: file access, credential theft, lateral movement.

Suggested fix

In tslearn/bases/bases.py, load through a restricted Unpickler whose find_class allows only the tslearn, sklearn and numpy classes a model needs, and reject everything else; prefer the existing to_hdf5/to_json paths for untrusted exchange. Until then, document on from_pickle() that model files from untrusted sources must not be loaded.

Affected
  • Package: tslearn (PyPI)
  • Affected versions: <= 0.10.0 (through default-branch HEAD d0708f25)
  • 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-502
Credit

Dilanka Kaushal Hewage (r3vpwnx) [email protected]

Lingua principale
Python
Stelle
3.2k
Fork
418
Merge medio
2g 14h
PR unite (30g)
24

Preparare l'ambiente

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 tslearn-team/tslearn

Tutte le issue di tslearn-team/tslearn

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.