Files
zapret-autotune/cmd/zapret-autotune/resource_qualify.go
T
ZeusinaandGPT-6 Bot a1a76bff76 fix: attest trusted packaged qualification engine alias
Resolve only the fixed nfq2 package alias into trusted binaries parents, hold the no-follow engine descriptor and verify inode/hash against the running worker before nft activation. Keep Lua/import guards and completed-proof publication unchanged.

Co-authored-by: GPT-6 <gpt-6@openai.com>
2026-10-14 13:57:58 +03:00

639 lines
21 KiB
Go

package main
import (
"bytes"
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"os"
"os/exec"
"os/signal"
"path/filepath"
"reflect"
"sort"
"strconv"
"strings"
"syscall"
"time"
)
const qualificationEngine = "/opt/zapret2/nfq2/nfqws2"
var errQualificationCapacity = errors.New("qualification matrix exceeds private capacity")
// A fresh disabled baseline needs no daemon stop/config transaction. Deliberately
// refuse existing active profiles: maintenance pause/restore is not implemented.
// ponytail: isolated startup baseline only; add job-bound profile rollback before
// supporting an already active desired set, never just invoke a daemon stop.
func qualificationBaseline(dir string) error {
if resourceManagedIdentity(dir) != nil {
return errResourcePlan
}
m := &managedTransaction{Dir: dir, Name: "config"}
j, e := m.load()
if e != nil || j.State != "committed" || len(j.Candidates) != 0 {
return errResourcePlan
}
b, e := m.read("config")
if e != nil || string(b) != managedBaseline {
return errResourcePlan
}
return nil
}
func qualificationMode(config, strategies string, d managedDeps) error {
if os.Geteuid() != 0 || os.Getppid() != 1 {
return errResourcePlan
}
if _, e := managedRuntimeGuard(d); e != nil {
return e
}
cmd, e := os.ReadFile("/proc/1/cmdline")
if e != nil || string(cmd) != "/bin/sh\x00/opt/managed-target-entrypoint.sh\x00" {
return errResourcePlan
}
env, e := os.ReadFile("/proc/1/environ")
if e != nil {
return errResourcePlan
}
values := map[string]string{}
for _, s := range strings.Split(string(env), "\x00") {
k, v, _ := strings.Cut(s, "=")
values[k] = v
}
if config == "" || strategies == "" || values["ZAPRET_AUTOTUNE_QUALIFY_CONFIG"] != config || values["ZAPRET_AUTOTUNE_STRATEGY_CONFIG"] != strategies {
return errResourcePlan
}
for _, ns := range []string{"pid", "net"} {
a, e := os.Readlink("/proc/1/ns/" + ns)
b, f := os.Readlink("/proc/self/ns/" + ns)
if e != nil || f != nil || a != b {
return errResourcePlan
}
}
if !resourceProcessOwned("/proc", os.Getpid(), "/usr/local/bin/zapret-autotune", []string{"/usr/local/bin/zapret-autotune", "qualify", "--config", config, "--strategies", strategies, "--isolated"}) {
return errResourcePlan
}
dir, e := os.Open("/run/zapret-autotune-ss")
if e != nil {
return errResourcePlan
}
defer dir.Close()
ready, e := resourcePrivateFile(dir, "monitor-ready")
if e != nil {
return e
}
defer ready.Close()
b, e := io.ReadAll(io.LimitReader(ready, 32))
if e != nil || string(b) != strconv.Itoa(os.Getpid())+"\n" {
return errResourcePlan
}
return qualificationBaseline(d.directory)
}
func qualificationFilesHash() (string, error) {
engine, e := openQualificationEngine("/opt/zapret2")
if e != nil {
return "", e
}
defer engine.file.Close()
return engine.filesHash()
}
func (engine *qualificationBinary) filesHash() (string, error) {
// Fixed image files only. Bind both executable and Lua inputs before and after
// every candidate; never accept a caller-supplied native hash/version label.
if engine.validate() != nil {
return "", errResourcePlan
}
hashes := []string{engine.hash}
for _, path := range []string{filepath.Join(engine.root, "lua/zapret-lib.lua"), filepath.Join(engine.root, "lua/zapret-antidpi.lua")} {
fd, e := syscall.Open(path, syscall.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK|syscall.O_CLOEXEC, 0)
if e != nil {
return "", errResourcePlan
}
f := os.NewFile(uintptr(fd), path)
info, e := f.Stat()
if e != nil || !info.Mode().IsRegular() || info.Size() > 32<<20 || info.Mode().Perm()&0022 != 0 {
f.Close()
return "", errResourcePlan
}
st, ok := info.Sys().(*syscall.Stat_t)
if !ok || st.Uid != 0 {
f.Close()
return "", errResourcePlan
}
b, e := io.ReadAll(io.LimitReader(f, 32<<20+1))
current, se := os.Lstat(path)
f.Close()
if e != nil || se != nil || !os.SameFile(info, current) || int64(len(b)) != info.Size() {
return "", errResourcePlan
}
hashes = append(hashes, planDigest(b))
}
return planDigest(hashes), nil
}
func (s *resourcePlanStore) protectBaseline() error {
for _, name := range []string{"config", managedMarkerName, managedJournalName, managedBackupName} {
f, b, e := s.files.openEntry(name)
if e != nil {
return errResourcePlan
}
s.held[name], s.snapshots[name] = f, b
}
return s.guard()
}
func qualificationNoWorker() error {
if e := qualificationOnlyWorker(0); e != nil {
return e
}
occupied, e := managedAssetQueueReady(0)
if e != nil || occupied {
return errResourcePlan
}
return nil
}
func qualificationOnlyWorker(owned int) error {
entries, e := os.ReadDir("/proc")
if e != nil {
return e
}
for _, entry := range entries {
if _, e := strconv.Atoi(entry.Name()); e != nil {
continue
}
exe, e := os.Readlink(filepath.Join("/proc", entry.Name(), "exe"))
if e != nil {
if errors.Is(e, os.ErrNotExist) {
continue
}
return errResourcePlan
}
if filepath.Base(exe) == "nfqws2" && entry.Name() != strconv.Itoa(owned) {
return errResourcePlan
}
}
return nil
}
func qualificationWorkerArgs(endpoints []string, args string) []string {
base := managedProbeWorkerArgs(endpoints, "")
base = append(base, "--filter-l7=tls") // Supported by pinned 1.0.2; generic caller only.
return append(base, strings.Fields(args)...)
}
// All seams are package-private and supplied by the runtime factory, not JSON.
type resourceQualifier struct {
runtime managedAssetRuntime
checks resourceCheckDeps
source func() (resourceMonitorSource, error)
restore func(context.Context) error
attest func(context.Context, int, []string, strategyDefinition) (qualificationAttestation, error)
guard func() error
now func() time.Time
filesHash func() (string, error)
close func()
}
func newResourceQualifier(config, strategies string, d managedDeps, s *resourcePlanStore) resourceQualifier {
r := newManagedAssetRuntime(d)
q := resourceQualifier{runtime: r, checks: productionResourceCheckDeps(), now: time.Now}
var engine *qualificationBinary
q.close = func() {
if engine != nil {
engine.file.Close()
}
}
q.filesHash = func() (string, error) {
if engine == nil {
var e error
engine, e = openQualificationEngine("/opt/zapret2")
if e != nil {
return "", e
}
}
return engine.filesHash()
}
start, queue := r.start, r.queue
r.start = func(args []string) (managedAssetWorker, error) {
if engine == nil || engine.validate() != nil {
return managedAssetWorker{}, errResourcePlan
}
return start(args)
}
r.queue = func(pid int) (bool, error) {
if engine == nil || engine.validate() != nil {
return false, errResourcePlan
}
ready, e := queue(pid)
// withOwnedWorker checks READY before creating the nft table.
if e == nil && ready && pid != 0 && engine.running(fmt.Sprintf("/proc/%d/exe", pid)) != nil {
return false, errResourcePlan
}
return ready, e
}
q.runtime = r
q.source = func() (resourceMonitorSource, error) { return readResourceMonitorSource(config, d.lookup, s.files.key) }
q.guard = func() error {
if qualificationMode(config, strategies, d) != nil || s.guard() != nil {
return errResourcePlan
}
return r.guard()
}
q.restore = func(ctx context.Context) error {
if ctx.Err() != nil || q.guard() != nil || qualificationNoWorker() != nil {
return errResourcePlan
}
tables, e := r.nft(ctx, "list tables\n")
if e != nil || strings.Contains(tables, "table inet "+managedAssetTable) {
return errResourcePlan
}
return qualificationBaseline(d.directory)
}
q.attest = func(ctx context.Context, pid int, args []string, definition strategyDefinition) (qualificationAttestation, error) {
if q.guard() != nil || qualificationOnlyWorker(pid) != nil {
return qualificationAttestation{}, errResourcePlan
}
if engine == nil || engine.validate() != nil || engine.running(fmt.Sprintf("/proc/%d/exe", pid)) != nil {
return qualificationAttestation{}, errResourcePlan
}
cmd, e := os.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid))
if e != nil || string(cmd) != strings.Join(append([]string{qualificationEngine}, args...), "\x00")+"\x00" {
return qualificationAttestation{}, errResourcePlan
}
stat, e := os.ReadFile(fmt.Sprintf("/proc/%d/stat", pid))
if e != nil {
return qualificationAttestation{}, e
}
end := strings.LastIndex(string(stat), ") ")
if end < 0 {
return qualificationAttestation{}, errResourcePlan
}
tail := strings.Fields(string(stat)[end+2:])
if len(tail) < 2 || tail[1] != strconv.Itoa(os.Getpid()) {
return qualificationAttestation{}, errResourcePlan
}
start, e := readMonitorProcessStarttime(pid)
if e != nil {
return qualificationAttestation{}, e
}
ready, e := r.queue(pid)
if e != nil || !ready {
return qualificationAttestation{}, errResourcePlan
}
hash, e := q.filesHash()
if e != nil {
return qualificationAttestation{}, e
}
versionCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
var output bytes.Buffer
version := exec.CommandContext(versionCtx, engine.path, "--version")
version.Stdout = &playerBoundedWriter{dst: &output, limit: 4096}
version.Stderr = io.Discard
if version.Run() != nil {
return qualificationAttestation{}, errResourcePlan
}
fields := strings.Fields(output.String())
valid := false
for i, f := range fields {
if f == "version" && i+1 < len(fields) && (fields[i+1] == "1.0.2" || fields[i+1] == "v1.0.2") {
valid = true
}
}
if !valid {
return qualificationAttestation{}, errResourcePlan
}
rules, e := r.nft(ctx, "list table inet "+managedAssetTable+"\n")
if e != nil || strings.TrimSpace(rules) == "" {
return qualificationAttestation{}, errResourcePlan
}
return qualificationAttestation{Collector: "owned-resource-nfq-v1", Runtime: "nfqws2-1.0.2", Definition: planDigest(definition), LuaHash: hash, RulesHash: planDigest(rules), WorkerArgsHash: planDigest(args), Queue: managedAssetQueue, PID: pid, StartTime: start}, nil
}
return q
}
func (q resourceQualifier) complete(ctx context.Context, s *resourcePlanStore, registry strategyRegistry) (completedQualification, error) {
fail := func(e error) (completedQualification, error) { return completedQualification{}, e }
if q.guard == nil || q.restore == nil || q.attest == nil || q.source == nil || q.now == nil || q.filesHash == nil || q.guard() != nil {
return fail(errResourcePlan)
}
if s.held[planMatrixName] != nil {
return fail(errResourcePlan)
}
trustedRegistry, registryErr := s.registry()
if registryErr != nil || planDigest(trustedRegistry) != planDigest(registry) {
return fail(errResourcePlan)
}
source, e := q.source()
if e != nil {
return fail(e)
}
hosts := map[string]bool{}
for _, service := range source.config.Services {
for _, spec := range service.Resources {
if !validDomain(spec.host) || net.ParseIP(spec.host) != nil || spec.host != strings.ToLower(spec.host) {
return fail(errResourcePlan)
}
hosts[spec.host] = true
}
}
endpoints := []string{}
for host := range hosts {
endpoints = append(endpoints, host)
}
sort.Strings(endpoints)
matrix := qualificationMatrix{Version: 1, Registry: planDigest(registry), Runs: []qualificationRun{}}
// Check the worst-case immutable envelope size before launching any worker.
estimate := qualificationMatrix{Version: 1, Registry: matrix.Registry}
for _, definition := range registry.Strategies {
run := qualificationRun{ID: "run-" + strings.Repeat("a", 32), Strategy: definition.ID, Definition: planDigest(definition), Config: source.fingerprint, Started: 9223372036854775807, Completed: 9223372036854775807, Attestation: qualificationAttestation{Collector: "owned-resource-nfq-v1", Runtime: "nfqws2-1.0.2", Definition: planDigest(definition), LuaHash: strings.Repeat("a", 64), RulesHash: strings.Repeat("a", 64), WorkerArgsHash: strings.Repeat("a", 64), Queue: 200, PID: 2147483647, StartTime: 9223372036854775807, Restored: true, Joined: true}}
for _, service := range source.config.Services {
for _, spec := range service.Resources {
protocols := []string{spec.TLS}
if spec.TLS == "both" {
protocols = []string{"tls12", "tls13"}
}
for _, protocol := range protocols {
run.Checks = append(run.Checks, qualificationCheck{Service: service.ID, Resource: spec.ID, TLS: protocol, Fingerprint: resourceRowFingerprint(s.files.key, source, spec), Outcome: "expectation_failed", Attempts: 3})
}
}
}
estimate.Runs = append(estimate.Runs, run)
}
b, _ := json.Marshal(estimate)
// Include the exact authenticated envelope overhead, using maximal timestamp
// widths and the longest permitted outcome. Args/selectors are fixed digests.
envelope, _ := json.Marshal(planEnvelope{Payload: b, MAC: strings.Repeat("a", 64)})
if len(envelope) > 65536 {
return fail(errQualificationCapacity)
}
stable := func() error {
if ctx.Err() != nil {
return ctx.Err()
}
if q.guard() != nil {
return errResourcePlan
}
fresh, e := q.source()
if e != nil || fresh.fingerprint != source.fingerprint {
return errResourcePlan
}
return nil
}
for _, definition := range registry.Strategies {
if _, e := canonicalStrategy(definition); e != nil {
return fail(e)
}
if e := stable(); e != nil {
return fail(e)
}
nativeBefore, nativeErr := q.filesHash()
if nativeErr != nil || !hashOK(nativeBefore) {
return fail(errResourcePlan)
}
id := make([]byte, 16)
if _, e := rand.Read(id); e != nil {
return fail(e)
}
run := qualificationRun{ID: "run-" + hex.EncodeToString(id), Strategy: definition.ID, Definition: planDigest(definition), Config: source.fingerprint, Started: q.now().UnixMilli()}
runtime := q.runtime
originalStart := runtime.start
pid := 0
var args []string
runtime.start = func(actual []string) (managedAssetWorker, error) {
if !reflect.DeepEqual(actual, managedProbeWorkerArgs(endpoints, definition.Args)) {
return managedAssetWorker{}, errResourcePlan
}
args = qualificationWorkerArgs(endpoints, definition.Args)
worker, e := originalStart(args)
pid = worker.pid
return worker, e
}
err := runtime.withOwnedWorker(ctx, endpoints, definition.Args, func(captureCtx context.Context) error {
if !reflect.DeepEqual(args, qualificationWorkerArgs(endpoints, definition.Args)) {
return errResourcePlan
}
a, e := q.attest(captureCtx, pid, args, definition)
if e != nil {
return e
}
if a.LuaHash != nativeBefore {
return errResourcePlan
}
run.Attestation = a
checks := q.checks
checks.qualification = true
originalGuard := checks.guard
checks.guard = func() error {
if e := stable(); e != nil {
return e
}
if e := originalGuard(); e != nil {
return e
}
current, e := q.attest(captureCtx, pid, args, definition)
if e != nil || current != a {
return errResourcePlan
}
return nil
}
for _, service := range source.config.Services {
for _, spec := range service.Resources {
protocols := []string{spec.TLS}
if spec.TLS == "both" {
protocols = []string{"tls12", "tls13"}
}
for _, protocol := range protocols {
// Unsupported TLS policy is unknown for this candidate, never PASS. Do
// still measure the full required set; planner applies selector eligibility.
cell := qualificationCheck{Service: service.ID, Resource: spec.ID, Fingerprint: resourceRowFingerprint(s.files.key, source, spec), TLS: protocol, Outcome: "pass"}
for repeat := 0; repeat < 3; repeat++ {
result, e := runResourceProtocol(captureCtx, service.ID, spec, protocol, source.server, source.proxy, checks)
if e != nil {
return e
}
if captureCtx.Err() != nil {
return captureCtx.Err()
}
if result.Outcome == "cancelled" {
return context.Canceled
}
if result.Outcome != "pass" {
cell.Outcome = result.Outcome
}
cell.Attempts++
}
run.Checks = append(run.Checks, cell)
}
}
}
return stable()
})
cleanup, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second)
restoreErr := q.restore(cleanup)
cancel()
if err != nil || restoreErr != nil {
if restoreErr != nil {
return fail(&qualificationCleanupFailure{Err: errors.Join(err, restoreErr)})
}
return fail(errors.Join(err, restoreErr))
}
hash, hashErr := q.filesHash()
if hashErr != nil || hash != run.Attestation.LuaHash {
return fail(errResourcePlan)
}
if e := stable(); e != nil {
return fail(e)
}
run.Attestation.Joined, run.Attestation.Restored = true, true
run.Completed = q.now().UnixMilli()
matrix.Runs = append(matrix.Runs, run)
}
if e := stable(); e != nil {
return fail(e)
}
if validateMatrix(matrix, registry) != nil {
return fail(errResourcePlan)
}
return completedQualification{matrix: matrix, seal: planDigest(matrix)}, nil
}
func qualifyResourceCLI(config, strategies string, stdout, stderr io.Writer, d managedDeps) (code int) {
if qualificationMode(config, strategies, d) != nil {
fmt.Fprintln(stderr, "blocked: qualification requires PID 1 isolated startup with disabled baseline")
return 78
}
// Refuse existing/foreign proof state before even creating a singleton inode.
prior, priorErr := openResourcePlanStore(d.directory, false)
if priorErr == nil {
exists := prior.held[planMatrixName] != nil
prior.close()
if exists {
fmt.Fprintln(stderr, "blocked: immutable matrix already exists; no runtime changes made")
return 78
}
} else if !errors.Is(priorErr, os.ErrNotExist) {
fmt.Fprintln(stderr, "blocked: qualification private state")
return 78
}
lock, e := acquireManagedMonitorSingleton(d)
if e != nil {
fmt.Fprintln(stderr, "blocked: another controller owns target")
return 78
}
defer lock.close()
if qualificationNoWorker() != nil {
fmt.Fprintln(stderr, "blocked: conflicting NFQ worker/queue")
return 78
}
s, e := openResourcePlanStore(d.directory, true)
if e != nil {
fmt.Fprintln(stderr, "blocked: qualification private state")
return 78
}
defer s.close()
if s.held[planMatrixName] != nil {
fmt.Fprintln(stderr, "blocked: immutable matrix already exists; no runtime changes made")
return 78
}
if s.protectBaseline() != nil {
fmt.Fprintln(stderr, "blocked: known baseline snapshot unavailable")
return 78
}
fd, e := syscall.Open(strategies, syscall.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK|syscall.O_CLOEXEC, 0)
if e != nil {
return 64
}
f := os.NewFile(uintptr(fd), "definitions")
defer f.Close()
if resourcePrivateEntry(f) != nil {
return 64
}
b, e := io.ReadAll(io.LimitReader(f, 65537))
info, se := os.Lstat(strategies)
held, he := f.Stat()
if e != nil || se != nil || he != nil || !os.SameFile(info, held) {
return 64
}
registry, e := parseStrategyRegistry(b)
if e != nil {
return 64
}
if s.held[planRegistryName] == nil {
if s.importDefinitions(b) != nil {
return 78
}
} else {
existing, e := s.registry()
if e != nil || planDigest(existing) != planDigest(registry) {
return 78
}
}
signals, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
ctx, cancel := context.WithTimeout(signals, 15*time.Minute)
defer cancel()
q := newResourceQualifier(config, strategies, d, s)
defer q.close()
var sessionErr error
defer func() {
if code != 0 && code != 78 {
return
}
if !qualificationCleanupVerified(sessionErr, q.restore) || s.guard() != nil {
code = 78
return
}
if writeQualificationCompletion(qualificationCompletionDir, s.files.key, os.Getpid(), code) != nil {
code = 78
}
}()
originalGuard := q.guard
q.guard = func() error {
current, e := os.Lstat(strategies)
heldInfo, he := f.Stat()
read, re := io.ReadAll(io.NewSectionReader(f, 0, 65537))
if e != nil || he != nil || re != nil || !os.SameFile(current, heldInfo) || resourcePrivateEntry(f) != nil || !bytes.Equal(read, b) {
return errResourcePlan
}
return originalGuard()
}
proof, e := q.complete(ctx, s, registry)
sessionErr = e
if errors.Is(e, errQualificationCapacity) {
fmt.Fprintln(stderr, "unknown: qualification matrix exceeds 64 KiB capacity; no worker launched")
return 64
}
if e != nil || ctx.Err() != nil {
fmt.Fprintln(stderr, "unknown: qualification incomplete or ownership/cleanup blocked; no completed matrix published")
if signals.Err() != nil && managedCancellationOnly(e, signals.Err()) {
return 0
}
return 78
}
fresh, sourceErr := q.source()
if sourceErr != nil || q.guard() != nil || ctx.Err() != nil {
fmt.Fprintln(stderr, "unknown: source changed before publication")
return 78
}
for _, run := range proof.matrix.Runs {
if run.Config != fresh.fingerprint {
fmt.Fprintln(stderr, "unknown: source changed before publication")
return 78
}
}
if s.saveCompleted(proof) != nil {
fmt.Fprintln(stderr, "blocked: completed matrix publication refused")
return 78
}
return resourcePlanProposal(config, stdout, stderr, d)
}