Weights that run code are refused
Python pickles (.bin, .pt, .pth, .ckpt, .pkl) run whatever they hold when loaded, as the engine's user, on the node; that is how the malicious models found on Hugging Face work. The node refuses such a source, or a mirror set holding one, before fetching anything. A model added with one, or given one, is refused, while one already in the catalog still loads and is refused by its node with the same words. vLLM and SGLang downloading a model by its id get --load-format safetensors. The mirror page marks the file. allow_pickle: true on a model lets it through on purpose, carried to the node in EngineProfile.allow_pickle. BACKLOG M lists the rest: checksums against Hugging Face on every path, its own scan, trusted publishers, engines without the internet, an eval before an untrusted model serves. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6752938e6d
commit
4466d2d15a
31 files changed
+447
-44
No files matched your search
@@ -201,8 +201,9 @@ func StartShardFromCommand(cmd reconciler.ShardCommand) *llmgwv1.StartShard {
|
||||
GpusPerNode: int32(p.GPUsPerNode), Nodes: int32(p.Nodes),
|
||||
Args: p.Args, Env: p.Env,
|
||||
ModelPath: p.ModelPath, ServedModelName: p.ServedModelName,
|
||||
Adapters: adaptersToWire(p.Adapters),
|
||||
KvEvents: p.KVEvents,
|
||||
Adapters: adaptersToWire(p.Adapters),
|
||||
KvEvents: p.KVEvents,
|
||||
AllowPickle: p.AllowPickle,
|
||||
},
|
||||
StartupTimeoutMs: cmd.StartupTimeout.Milliseconds(),
|
||||
Pool: cmd.Pool,
|
||||
@@ -238,8 +239,9 @@ func ShardSpecFromStart(m *llmgwv1.StartShard) (engines.ShardSpec, time.Duration
|
||||
GPUsPerNode: int(p.GetGpusPerNode()), Nodes: int(p.GetNodes()),
|
||||
Args: p.GetArgs(), Env: p.GetEnv(),
|
||||
ModelPath: p.GetModelPath(), ServedModelName: p.GetServedModelName(),
|
||||
Adapters: adaptersFromWire(p.GetAdapters()),
|
||||
KVEvents: p.GetKvEvents(),
|
||||
Adapters: adaptersFromWire(p.GetAdapters()),
|
||||
KVEvents: p.GetKvEvents(),
|
||||
AllowPickle: p.GetAllowPickle(),
|
||||
},
|
||||
}
|
||||
return spec, time.Duration(m.GetStartupTimeoutMs()) * time.Millisecond
|
||||
|
||||
@@ -323,9 +323,15 @@ type Model struct {
|
||||
// choice by guesswork.
|
||||
Description string `json:"description,omitempty" koanf:"description"`
|
||||
// Source is the weights path or a HuggingFace id.
|
||||
Source string `json:"source,omitempty" koanf:"source"`
|
||||
Profiles []Profile `json:"profiles,omitempty" koanf:"profiles"`
|
||||
Scaling Scaling `json:"scaling,omitempty" koanf:"scaling"`
|
||||
Source string `json:"source,omitempty" koanf:"source"`
|
||||
// AllowPickle lets the nodes fetch this model's weights, or its
|
||||
// adapters', in a pickle format (.bin, .pt, .pth, .ckpt), which run code
|
||||
// as they are loaded. Off, such a source is refused when the model is
|
||||
// added and by the node before it fetches anything. For a model that has
|
||||
// no safetensors or GGUF form and whose files were checked.
|
||||
AllowPickle bool `json:"allow_pickle,omitempty" koanf:"allow_pickle"`
|
||||
Profiles []Profile `json:"profiles,omitempty" koanf:"profiles"`
|
||||
Scaling Scaling `json:"scaling,omitempty" koanf:"scaling"`
|
||||
// Class is how much this model's capacity is worth against another's,
|
||||
// when there is not enough for both: critical, standard or opportunistic.
|
||||
// Empty is standard, or opportunistic for a model loaded on demand. See
|
||||
@@ -1336,10 +1342,16 @@ func (m Model) EngineProfile(name string) (engines.Profile, bool) {
|
||||
Name string `json:"name"`
|
||||
Source string `json:"source"`
|
||||
HFRepo string `json:"hf_repo,omitempty"`
|
||||
// A string: the node reads the members as strings.
|
||||
AllowPickle string `json:"allow_pickle,omitempty"`
|
||||
}
|
||||
var ms []member
|
||||
for _, sm := range m.Shared.Members {
|
||||
ms = append(ms, member{Name: sm.Name, Source: sm.Source, HFRepo: sm.HFRepo})
|
||||
mb := member{Name: sm.Name, Source: sm.Source, HFRepo: sm.HFRepo}
|
||||
if sm.AllowPickle {
|
||||
mb.AllowPickle = "true"
|
||||
}
|
||||
ms = append(ms, mb)
|
||||
}
|
||||
b, _ := json.Marshal(ms)
|
||||
env = maps.Clone(env)
|
||||
@@ -1369,6 +1381,7 @@ func (m Model) EngineProfile(name string) (engines.Profile, bool) {
|
||||
ServedModelName: m.Name,
|
||||
Adapters: m.engineAdapters(),
|
||||
KVEvents: p.KVEvents,
|
||||
AllowPickle: m.AllowPickle,
|
||||
}.WithRuntimeLoRA(m.MaxLoadedAdapters), true
|
||||
}
|
||||
return engines.Profile{}, false
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package catalog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/agent/weights"
|
||||
)
|
||||
|
||||
// PickleSources are the sources of a model's weights and adapters that are
|
||||
// in a pickle format, which run code as they are loaded (weights.Pickled).
|
||||
func (m Model) PickleSources() []string {
|
||||
var out []string
|
||||
add := func(s string) {
|
||||
if s != "" && weights.Pickled(s) && !slices.Contains(out, s) {
|
||||
out = append(out, s)
|
||||
}
|
||||
}
|
||||
add(m.Source)
|
||||
for _, p := range m.Profiles {
|
||||
add(p.Source)
|
||||
}
|
||||
for _, a := range m.Adapters {
|
||||
add(a.Source)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// CheckPickle refuses a model whose weights are in a pickle format, unless
|
||||
// it says allow_pickle. Asked when a model is added or its sources change,
|
||||
// not when the catalog is built: a model already running is refused by its
|
||||
// node, with the same words, rather than dropped from the catalog.
|
||||
// Sources in except were there before and are not asked about again.
|
||||
func (m Model) CheckPickle(except ...string) error {
|
||||
if m.AllowPickle {
|
||||
return nil
|
||||
}
|
||||
for _, s := range m.PickleSources() {
|
||||
if slices.Contains(except, s) {
|
||||
continue
|
||||
}
|
||||
return fmt.Errorf("model %q: %s is in a pickle format, which runs code when it is loaded; "+
|
||||
"use the model's safetensors or GGUF, or set allow_pickle: true if it was checked and has no other form",
|
||||
m.Name, s)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package catalog
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAModelInAPickleFormatIsRefusedUnlessItSaysSo(t *testing.T) {
|
||||
m := Model{Name: "triposr", Source: "hf:stabilityai/TripoSR/model.ckpt",
|
||||
Adapters: []Adapter{{Name: "a", Source: "http://m/a/adapter_model.bin"}}}
|
||||
err := m.CheckPickle()
|
||||
if err == nil || !strings.Contains(err.Error(), "model.ckpt") || !strings.Contains(err.Error(), "allow_pickle") {
|
||||
t.Fatalf("not refused, or not said why: %v", err)
|
||||
}
|
||||
if got := m.PickleSources(); len(got) != 2 {
|
||||
t.Errorf("pickle sources %v, want the model's and the adapter's", got)
|
||||
}
|
||||
// A source that was there before is not asked about again: changing
|
||||
// the scaling of a model that runs does not need allow_pickle.
|
||||
if err := m.CheckPickle(m.PickleSources()...); err != nil {
|
||||
t.Errorf("sources already there were refused: %v", err)
|
||||
}
|
||||
m.AllowPickle = true
|
||||
if err := m.CheckPickle(); err != nil {
|
||||
t.Errorf("allow_pickle was not honoured: %v", err)
|
||||
}
|
||||
if err := (Model{Name: "q", Source: "http://m/q/v/q-Q4_K_M.gguf"}).CheckPickle(); err != nil {
|
||||
t.Errorf("a GGUF was refused: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,8 @@ type SharedMember struct {
|
||||
Source string `json:"source"`
|
||||
// HFRepo is the repository its node loads the weights by name from.
|
||||
HFRepo string `json:"hf_repo,omitempty"`
|
||||
// AllowPickle is the member model's allow_pickle.
|
||||
AllowPickle bool `json:"allow_pickle,omitempty"`
|
||||
// Workflow and Checkpoint are what a request for it is sent.
|
||||
Workflow string `json:"workflow"`
|
||||
Checkpoint string `json:"checkpoint"`
|
||||
@@ -80,7 +82,7 @@ func envModel(env Environment, members []Model) Model {
|
||||
hf = m.ComfyUI.HuggingFaceRepo
|
||||
}
|
||||
shared.Members = append(shared.Members, SharedMember{Name: m.Name, Aliases: m.Aliases,
|
||||
Source: m.Source, HFRepo: hf, Workflow: workflow, Checkpoint: ckpt})
|
||||
Source: m.Source, HFRepo: hf, AllowPickle: m.AllowPickle, Workflow: workflow, Checkpoint: ckpt})
|
||||
idle = max(idle, m.Scaling.IdleTimeout)
|
||||
minN, maxN = max(minN, m.Scaling.Min), max(maxN, m.Scaling.Max)
|
||||
if ClassRank(m.EffectiveClass()) > ClassRank(class) || class == "" {
|
||||
|
||||
@@ -154,6 +154,9 @@ func (s *Service) add(ctx context.Context, m catalog.Model, by string, onlyNew b
|
||||
next = append(next, e)
|
||||
}
|
||||
next = append(next, m)
|
||||
if err := m.CheckPickle(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
built, err := catalog.New(next)
|
||||
if err != nil {
|
||||
@@ -406,9 +409,13 @@ func (s *Service) update(ctx context.Context, name, by, what string, change func
|
||||
found := false
|
||||
for _, e := range stored {
|
||||
if e.Name == name {
|
||||
before := e.PickleSources()
|
||||
if err := change(&e); err != nil {
|
||||
return catalog.Model{}, err
|
||||
}
|
||||
if err := e.CheckPickle(before...); err != nil {
|
||||
return catalog.Model{}, err
|
||||
}
|
||||
found = true
|
||||
}
|
||||
next = append(next, e)
|
||||
|
||||
Reference in new issue
Block a user