feat: add candidate-bound domain qualification and planning

Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
ZeusinaandGPT-6 Bot committed 2026-10-14 12:21:52 +03:00
1 parent 2242643000
commit 573da3f797
15 files changed
+2486 -10

No files matched your search

+156
View File
@@ -0,0 +1,156 @@
# Resource compatibility planner and isolated qualifier
This is a **proposal-only** P0 slice. `plan` never changes runtime or config.
Explicit isolated qualification temporarily installs an owned NFQ worker and
queue rules, but never changes legacy SQL groups, renderer or apply journals.
Monitor HEALTHY, old SQL measurements and public check JSON are not proof.
Without a completed owned qualification batch, compatibility is UNKNOWN and
hosts are unassigned. No PASS/evidence import is exposed.
Commands require root and the existing `managed-test` target settings:
```
zapret-autotune strategy-import --strategies /private/strategies.json
zapret-autotune plan --config /private/resources.json
zapret-autotune qualify --config /private/resources.json --strategies /private/strategies.json --isolated
```
`strategy-import` requires an owned, regular, single-link 0600 file. Only
definitions are accepted; duplicate keys, unknown fields and evidence fields
are rejected. Example:
```json
{
"version": 1,
"strategies": [
{
"id": "split-one",
"args": "--payload=tls_client_hello --lua-desync=multisplit:pos=1",
"selectors": {"transport":"tcp","ports":"443","l7":"tls","tls":"both"}
}
]
}
```
The registry is one immutable batch (1–32 unique IDs). This conservative slice
accepts only TCP/443/TLS selectors and `tls12`, `tls13`, or `both` policy.
Argument whitespace is canonicalized; option/Lua stage order is preserved.
Existing payload/range validation is reused. Lua stages are restricted to
fake, multisplit, multidisorder, fakeddisorder, split and disorder; parameters
to pos, repeats, tcp_ts, tcp_flags_set, seqovl and blob=fake_default_tls.
File/inline Lua, queue/user/fwmark/filter options and arbitrary stages are
rejected. Acceptance of a definition is **not** runtime verification.
The private target directory holds `.resource-plan-key` (32 random bytes),
`.resource-plan-strategies.json` and, when an owned producer exists,
`.resource-plan-matrix.json`. JSON envelopes have `payload` and an HMAC-SHA256
`mac`. Each file is write-once, owned, 0600, no-follow, single-link and bounded
to 64 KiB. Held directory/file identities and exact snapshots are checked;
foreign entries, replacement inodes and modified bytes are never clobbered or
silently adopted. Interrupted writes remain blocked for explicit operator
repair. There is no automatic rotation/update/repair in this slice. The private
key and same-UID/root boundary must be protected: HMAC is not protection from an
actor who can read the key and replace the whole target state between processes.
Matrix v1 binds the registry digest and at most one completed run per candidate.
Each run binds ID, definition digest (canonical args **and selectors**), keyed
config fingerprint, start/completion milliseconds, resource/TLS outcomes and
keyed resource fingerprints. Its attestation records collector/runtime,
definition digest, composite executable/Lua input hash, installed rules hash,
actual worker-argv hash, queue and worker PID/start identity, plus
restored/joined completion. No URLs, headers, resolved IPs, bodies or raw errors
are persisted. A nonserialized producer seal protects the completed in-memory
snapshot against mutation before publication. Only the owned producer and
test-internal fixtures create seals; JSON DTOs cannot authorize publication.
Every resource/protocol cell records exactly three actual attempts; any nonpass
in those attempts makes the cell negative (never an average or partial PASS).
`plan` loads only authenticated private state and the existing resource-config
snapshot (including resolved environment secrets, DNS/proxy and private headers
in keyed fingerprints). Every resource on an exact host, across all services
and input group labels, must have matching complete results for every required
TLS protocol. Complete failed checks are negative; missing, expired (24 hours),
future, mismatched or partial checks are unknown. Changed config invalidates old
run evidence. Malformed/unauthenticated state yields blocked, unassigned output
and exit 78; valid missing evidence yields unknown output and exit 0.
Output contains `proposal_only`, `evidence` (authentication status, **not**
health), per-host `validated`/`negative`/`unknown` candidate alternatives,
`assignments` and `unassigned`. Compatible equal args/selectors merge coverage
across input labels. Greedy greatest-uncovered coverage, then lexical candidate
ID, produces deterministic disjoint exact-host assignments. It does not claim
global optimality or suffix coverage. Explicit legacy YouTube suffix policy is
unchanged.
## INTRUSIVE / ISOLATED startup only
Set **both** `ZAPRET_AUTOTUNE_QUALIFY_CONFIG` and
`ZAPRET_AUTOTUNE_STRATEGY_CONFIG` to container paths on a dedicated target.
Do not send working user traffic to this target's published SOCKS port. This is
not noninterrupting background search. Provision the private resource and 0600
strategy files separately; compose forwards paths but does not mount files for
you. Start with the managed disabled baseline, not an existing active profile.
Qualification v1 accepts lowercase DNS hostnames, not literal IP targets; the
ordinary resource-check command's broader literal-IP policy is unchanged.
PID 1 starts its owned SS+DoH services, initializes/reconciles known state, then
launches one finite `qualify ... --isolated` session **instead of** starting the
live zapret daemon or monitor. Qualifier checks root/target namespace, PID 1's
actual entrypoint/environment, direct-child executable/arguments/session ACK,
owned SS and DNS readiness anchors. A flag or docker-exec environment alone
does not authorize qualification. Missing either startup path fails closed.
Without `--isolated`, `qualify` remains UNKNOWN/exit 78 without runtime changes.
The shared monitor singleton excludes legacy and generic controllers. Any
existing NFQ worker/queue, active profile, unknown config/journal or existing
immutable matrix is refused. Known baseline config/journal/backup/identity
inodes and bytes remain held unchanged throughout the job. This first producer
does **not** pause/stop/restart existing active profiles; job-bound maintenance
rollback is deferred rather than pretending a path-only daemon stop is safe.
Each candidate uses the existing worker-before-nft/queue-ready lifecycle on
queue 200, fixed Lua imports and fwmark 0x10000000, plus native
`--filter-l7=tls`. The actual child executable, argv, parent, start identity,
queue socket ownership, executable/Lua hashes, native `--version` output 1.0.2
and installed nft rule bytes are attested before capture and checked again at
each collector guard. DoH's existing 0x50000000 socket mark bypass remains.
TLS policy is collector/eligibility policy, not a nonexistent native TLS-version
filter. All configured protocols are measured; incompatible selector policies
remain UNKNOWN in the proposal.
The existing GET/HEAD generic collector preserves TLS origin verification,
configured body/status/content-type expectations, private stdin and IP pinning.
Completed expectation failures and typed peer response/TLS/transport negatives
do not stop the remaining repeats/cells. Guard/source/unknown infrastructure or
batch cancellation veto the entire batch; no partial positive matrix is saved.
Config (including resolved environment/header secrets) and strategy-source
snapshots are checked after DNS/before transfer and before sealing/publication.
Workers are joined and temporary nft queues removed before verifying unchanged
disabled baseline/runtime and sealing. The batch has a fixed 15-minute deadline
with separate bounded cleanup; PID 1 keeps SS+DoH alive through cleanup.
The bounded 64 KiB matrix size (including its authenticated envelope and
worst-case field widths) is checked before any worker/nft operation; excessive
catalogue/resource combinations return UNKNOWN/exit 64 even within individual
count caps (32 strategies, 64 resources).
Successful batches, including complete negatives, return the assignment proposal.
Incomplete/unsafe jobs return UNKNOWN/blocked exit 78 without proof. PID 1 then
stays safe-idle with SS+DoH for inspection, for either success or blocked
completion: no qualification retry loop, duplicate controller or automatic
generic monitor start. Retests require future explicit immutable snapshot
rotation, not overwriting prior evidence.
Completed controller cleanup is acknowledged separately from qualification in
an exclusive, 0600 HMAC-authenticated `qualification-completed.json` on the
current boot's SS tmpfs. It binds the launched PID, exit outcome (0 or 78) and
verified cleanup; it is **not** strategy proof. PID 1 checks that ACK against its
private session launch and revalidates the disabled baseline, singleton,
SS/DoH ownership and absence of NFQ workers/temporary nft queues before shutting
down dependencies. Thus TERM after a clean blocked qualifier has exited but
before its parent reaps it can exit cleanly. Worker/nft/join/restoration failure,
missing/foreign/mutated ACK or unverified early failure still veto clean shutdown
with exit 78. Ordinary legacy/generic monitor exit 78 remains a safety veto.
No unsupported `--filter-mark` or blockcheck embedded/stream/progress/deadline
flags are used. Background noninterrupting search and generic apply migration
remain deferred, as does immutable snapshot rotation for repeated runs.
+15
View File
@@ -9,6 +9,12 @@ import (
const usage = `Usage: zapret-autotune <command>
Commands:
strategy-import --strategies FILE
Import immutable definitions only into the private target registry
plan --config FILE
Authenticated domain compatibility proposal (never apply)
qualify --config FILE --strategies FILE --isolated
Intrusive finite batch; only PID 1 isolated startup on disabled baseline
dns-forwarder mode|address|serve
Managed DNS configuration or loopback DoH forwarder
dns-forwarder readiness-check|readiness-cleanup <expected-dns-pid>
@@ -45,6 +51,12 @@ func run(args []string, stdout, stderr io.Writer) int {
}
func runWithPlayer(args []string, stdout, stderr io.Writer, player playerRunner) int {
if len(args) > 0 && args[0] == "qualification-completion-check" {
return qualificationCompletionCLI(args[1:], managedDependencies(player))
}
if len(args) > 0 && (args[0] == "plan" || args[0] == "strategy-import" || args[0] == "qualify") {
return resourcePlanCLI(args, stdout, stderr, managedDependencies(player))
}
if len(args) == 3 && args[0] == "check" && args[1] == "--config" {
return configuredResourceCheck(args[2], stdout, stderr)
}
@@ -53,6 +65,9 @@ func runWithPlayer(args []string, stdout, stderr io.Writer, player playerRunner)
}
if len(args) == 1 && args[0] == "target-config-check" {
path, _ := os.LookupEnv("ZAPRET_AUTOTUNE_RESOURCE_CONFIG")
if path == "" {
path, _ = os.LookupEnv("ZAPRET_AUTOTUNE_QUALIFY_CONFIG")
}
if path != "" {
port, err := managedTargetSettings(os.LookupEnv)
if err != nil || port != 1080 {
+23 -4
View File
@@ -326,10 +326,13 @@ func approvedResourceIP(r resourceCheckSpec, ip net.IP) bool {
}
type resourceCheckDeps struct {
lookup func(string) (string, bool)
guard func() error
resolve func(context.Context, string, string) ([]net.IP, error)
runner subprocessRunner
// Qualification must distinguish infrastructure from verified peer negatives.
// Monitoring retains its existing coarse outcome behavior.
qualification bool
lookup func(string) (string, bool)
guard func() error
resolve func(context.Context, string, string) ([]net.IP, error)
runner subprocessRunner
}
func resolveResourceIPs(ctx context.Context, server, host string) ([]net.IP, error) {
@@ -376,6 +379,16 @@ func runResourceProtocol(ctx context.Context, service string, r resourceCheckSpe
probeCtx, cancel := context.WithTimeout(ctx, time.Duration(r.TimeoutMS)*time.Millisecond)
defer cancel()
ips, err := d.resolve(probeCtx, server, r.host)
if d.qualification && err != nil {
var dns *net.DNSError
knownTimeout := probeCtx.Err() != nil && managedRecoveryTimeoutSafe(err)
if !knownTimeout && (!errors.As(err, &dns) || !dns.IsNotFound) {
return result, err
}
}
if d.qualification && err == nil && (len(ips) == 0 || len(ips) > 64) {
return result, errResourceConfig
}
if err == nil && len(ips) > 0 && len(ips) <= 64 {
approved := true
for _, ip := range ips {
@@ -391,6 +404,12 @@ func runResourceProtocol(ctx context.Context, service string, r resourceCheckSpe
pin := r.host + ":443:" + net.JoinHostPort(ips[0].String(), "443")
options := resourceTransferOptions{generic: true, method: r.Method, limit: r.Expect.MaxBytes, timeout: time.Duration(r.TimeoutMS) * time.Millisecond, config: r.privateConfig + "connect-to = " + privateCurlValue(pin) + "\n"}
transfer, _, err := collectResourceTransferOptions(probeCtx, proxy, r.URL, r.host, protocol, false, d.runner, options)
if d.qualification && err != nil {
knownTimeout := probeCtx.Err() != nil && managedRecoveryTimeoutSafe(err)
if !knownTimeout && !resourceRejectionOnly(err) && !managedAssetTransferNonpass(err) {
return result, err
}
}
result.Outcome = "transfer_failed"
if err == nil {
result.Bytes, result.TimeMS, result.Status = len(transfer.Body), transfer.LatencyMS, transfer.Status
@@ -49,6 +49,9 @@ func configuredWire(raw, method, protocol, mode string, out, diag io.Writer) {
if mode == "redirect" {
status = 302
}
if mode == "status500" {
status = 500
}
if mode == "redirect-count" {
redirects = 1
}
+9 -2
View File
@@ -37,12 +37,19 @@ type resourceError struct{ cause error }
// Rejection is never qualification, nor evidence that local ownership failed.
type resourcePeerTLSRejection struct{ error }
// Created only after origin, verified TLS and HTTP framing have been decoded.
// A configured HTTP peer response rejection is not local ownership failure.
type resourcePeerResponseRejection struct{ error }
func (e *resourcePeerResponseRejection) Error() string { return "resource peer response rejected" }
func (e *resourcePeerResponseRejection) Unwrap() error { return e.error }
func (e *resourcePeerTLSRejection) Error() string { return "resource peer TLS rejected" }
func (e *resourcePeerTLSRejection) Unwrap() error { return e.error }
func resourceRejectionOnly(err error) bool {
switch err.(type) {
case *resourcePeerTLSRejection, imageBodyRejection:
case *resourcePeerTLSRejection, *resourcePeerResponseRejection, imageBodyRejection:
return true
}
if e, ok := err.(*resourceError); ok {
@@ -362,7 +369,7 @@ func collectResourceTransferOptions(ctx context.Context, proxy, raw, host, proto
return fail(errors.New("unsupported response contract"))
}
if options.generic && response.StatusCode >= 300 && response.StatusCode < 400 {
return fail(errors.New("redirect rejected"))
return fail(&resourcePeerResponseRejection{error: errors.New("redirect rejected")})
}
body, err := io.ReadAll(io.LimitReader(response.Body, int64(limit+1)))
// With --raw curl counts the wire body, including chunk framing; the image
+332
View File
@@ -0,0 +1,332 @@
package main
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"io"
"sort"
"strings"
"time"
)
var errResourcePlan = errors.New("invalid private qualification data")
// Input groups are metadata. Only the HTTPS collector's selectors are supported.
type strategySelectors struct {
Transport string `json:"transport"`
Ports string `json:"ports"`
L7 string `json:"l7"`
TLS string `json:"tls"`
}
type strategyDefinition struct {
ID string `json:"id"`
Args string `json:"args"`
Selectors strategySelectors `json:"selectors"`
}
type strategyRegistry struct {
Version int `json:"version"`
Strategies []strategyDefinition `json:"strategies"`
}
func strictPlanJSON(b []byte, dst interface{}) error {
if len(b) > 65536 {
return errResourcePlan
}
d := json.NewDecoder(bytes.NewReader(b))
if uniqueResourceJSON(d, 0) != nil {
return errResourcePlan
}
if _, err := d.Token(); err != io.EOF {
return errResourcePlan
}
d = json.NewDecoder(bytes.NewReader(b))
d.DisallowUnknownFields()
if d.Decode(dst) != nil {
return errResourcePlan
}
return nil
}
func planDigest(v interface{}) string {
b, _ := json.Marshal(v)
h := sha256.Sum256(b)
return hex.EncodeToString(h[:])
}
func canonicalStrategy(d strategyDefinition) (strategyDefinition, error) {
d.Args = strings.Join(strings.Fields(d.Args), " ")
if !resourceID.MatchString(d.ID) || d.Args == "" || len(d.Args) > 4096 || (&candidate{Args: d.Args}).validateArgs() != nil {
return d, errResourcePlan
}
if d.Selectors.Transport != "tcp" || d.Selectors.Ports != "443" || d.Selectors.L7 != "tls" || (d.Selectors.TLS != "both" && d.Selectors.TLS != "tls12" && d.Selectors.TLS != "tls13") {
return d, errResourcePlan
}
for _, arg := range strings.Fields(d.Args) {
option, value, _ := strings.Cut(arg, "=")
if option == "--lua-init" {
return d, errResourcePlan
}
if option != "--lua-desync" {
continue
}
parts := strings.Split(value, ":")
switch parts[0] {
case "fake", "multisplit", "multidisorder", "fakeddisorder", "split", "disorder":
default:
return d, errResourcePlan
}
for _, part := range parts[1:] {
key, v, ok := strings.Cut(part, "=")
if !ok || v == "" || strings.ContainsAny(v, "/@%") || strings.Contains(v, "..") {
return d, errResourcePlan
}
switch key {
case "pos", "repeats", "tcp_ts", "tcp_flags_set", "seqovl":
case "blob":
if v != "fake_default_tls" {
return d, errResourcePlan
}
default:
return d, errResourcePlan
}
}
}
return d, nil
}
func parseStrategyRegistry(b []byte) (strategyRegistry, error) {
var r strategyRegistry
if strictPlanJSON(b, &r) != nil || r.Version != 1 || len(r.Strategies) == 0 || len(r.Strategies) > 32 {
return r, errResourcePlan
}
seen := map[string]bool{}
for i, d := range r.Strategies {
v, err := canonicalStrategy(d)
if err != nil || seen[v.ID] {
return r, errResourcePlan
}
seen[v.ID] = true
r.Strategies[i] = v
}
sort.Slice(r.Strategies, func(i, j int) bool { return r.Strategies[i].ID < r.Strategies[j].ID })
return r, nil
}
type qualificationCheck struct {
Service string `json:"service"`
Resource string `json:"resource"`
Fingerprint string `json:"fingerprint"`
TLS string `json:"tls"`
Outcome string `json:"outcome"`
Attempts int `json:"attempts"`
}
type qualificationRun struct {
ID string `json:"id"`
Strategy string `json:"strategy"`
Definition string `json:"definition"`
Config string `json:"config"`
Started int64 `json:"started_ms"`
Completed int64 `json:"completed_ms"`
Attestation qualificationAttestation `json:"attestation"`
Checks []qualificationCheck `json:"checks"`
}
type qualificationAttestation struct {
Collector string `json:"collector"`
Runtime string `json:"runtime"`
Definition string `json:"definition"`
LuaHash string `json:"lua_hash"`
RulesHash string `json:"rules_hash"`
WorkerArgsHash string `json:"worker_args_hash"`
Queue int `json:"queue"`
PID int `json:"pid"`
StartTime int64 `json:"starttime"`
Restored bool `json:"restored"`
Joined bool `json:"joined"`
}
type qualificationMatrix struct {
Version int `json:"version"`
Registry string `json:"registry"`
Runs []qualificationRun `json:"runs"`
}
// Only the owned producer (or internal test fixtures) constructs this capability.
// JSON imports cannot authorize writes, and post-seal DTO mutation is rejected.
type completedQualification struct {
matrix qualificationMatrix
seal string
}
func hashOK(s string) bool {
b, e := hex.DecodeString(s)
return e == nil && len(b) == 32 && strings.ToLower(s) == s
}
func validateMatrix(m qualificationMatrix, registry strategyRegistry) error {
if m.Version != 1 || m.Registry != planDigest(registry) || len(m.Runs) > 32 {
return errResourcePlan
}
defs := map[string]strategyDefinition{}
for _, d := range registry.Strategies {
defs[d.ID] = d
}
ids, strategies := map[string]bool{}, map[string]bool{}
for _, r := range m.Runs {
d, ok := defs[r.Strategy]
a := r.Attestation
if !ok || !resourceID.MatchString(r.ID) || ids[r.ID] || strategies[r.Strategy] || r.Definition != planDigest(d) || !hashOK(r.Config) || r.Started <= 0 || r.Completed < r.Started || r.Completed-r.Started > 86400000 || len(r.Checks) == 0 || len(r.Checks) > 128 {
return errResourcePlan
}
ids[r.ID], strategies[r.Strategy] = true, true
if a.Collector != "owned-resource-nfq-v1" || a.Runtime != "nfqws2-1.0.2" || a.Definition != r.Definition || !hashOK(a.LuaHash) || !hashOK(a.RulesHash) || !hashOK(a.WorkerArgsHash) || a.Queue != managedAssetQueue || a.PID <= 0 || a.StartTime <= 0 || !a.Restored || !a.Joined {
return errResourcePlan
}
seen := map[string]bool{}
for _, c := range r.Checks {
id := c.Service + "/" + c.Resource + "/" + c.TLS
if !resourceID.MatchString(c.Service) || !resourceID.MatchString(c.Resource) || !hashOK(c.Fingerprint) || seen[id] || c.Attempts != 3 || (c.TLS != "tls12" && c.TLS != "tls13") {
return errResourcePlan
}
seen[id] = true
switch c.Outcome {
case "pass", "dns_failed", "destination_denied", "transfer_failed", "expectation_failed", "timeout":
default:
return errResourcePlan
}
}
}
return nil
}
type domainCompatibility struct {
Host string `json:"host"`
Validated []string `json:"validated"`
Negative []string `json:"negative"`
Unknown []string `json:"unknown"`
}
type runtimeAssignment struct {
Strategy string `json:"strategy"`
Args string `json:"args"`
Selectors strategySelectors `json:"selectors"`
Hosts []string `json:"hosts"`
}
type resourcePlan struct {
ProposalOnly bool `json:"proposal_only"`
Evidence string `json:"evidence"`
Domains []domainCompatibility `json:"domains"`
Assignments []runtimeAssignment `json:"assignments"`
Unassigned []string `json:"unassigned"`
}
func deriveResourcePlan(source resourceMonitorSource, key []byte, registry strategyRegistry, matrix qualificationMatrix, now time.Time) resourcePlan {
p := resourcePlan{ProposalOnly: true, Evidence: "unknown", Domains: []domainCompatibility{}, Assignments: []runtimeAssignment{}, Unassigned: []string{}}
hosts := map[string][]qualificationCheck{}
for _, s := range source.config.Services {
for _, r := range s.Resources {
protocols := []string{r.TLS}
if r.TLS == "both" {
protocols = []string{"tls12", "tls13"}
}
for _, tls := range protocols {
host := strings.ToLower(r.host)
hosts[host] = append(hosts[host], qualificationCheck{Service: s.ID, Resource: r.ID, Fingerprint: resourceRowFingerprint(key, source, r), TLS: tls})
}
}
}
runs := map[string]qualificationRun{}
if validateMatrix(matrix, registry) == nil {
p.Evidence = "authenticated"
for _, r := range matrix.Runs {
if r.Config == source.fingerprint && r.Completed <= now.UnixMilli() && now.UnixMilli()-r.Completed <= 86400000 {
runs[r.Strategy] = r
}
}
}
names := []string{}
for host := range hosts {
names = append(names, host)
}
sort.Strings(names)
for _, host := range names {
row := domainCompatibility{Host: host, Validated: []string{}, Negative: []string{}, Unknown: []string{}}
for _, d := range registry.Strategies {
r, exists := runs[d.ID]
checks := map[string]qualificationCheck{}
for _, c := range r.Checks {
checks[c.Service+"/"+c.Resource+"/"+c.TLS] = c
}
complete, failed := exists, false
for _, expected := range hosts[host] {
c, ok := checks[expected.Service+"/"+expected.Resource+"/"+expected.TLS]
if !ok || c.Fingerprint != expected.Fingerprint || (d.Selectors.TLS != "both" && d.Selectors.TLS != expected.TLS) {
complete = false
continue
}
if c.Outcome != "pass" {
failed = true
}
}
if !complete {
row.Unknown = append(row.Unknown, d.ID)
} else if failed {
row.Negative = append(row.Negative, d.ID)
} else {
row.Validated = append(row.Validated, d.ID)
}
}
p.Domains = append(p.Domains, row)
}
// Merge only equal canonical args/selectors, retaining per-candidate alternatives.
// Greatest uncovered coverage is deterministic greedy, not globally optimal.
profiles := map[string]runtimeAssignment{}
coverage := map[string]map[string]bool{}
for _, d := range registry.Strategies {
profile := planDigest(struct {
Args string
Selectors strategySelectors
}{d.Args, d.Selectors})
a, exists := profiles[profile]
if !exists || d.ID < a.Strategy {
profiles[profile] = runtimeAssignment{Strategy: d.ID, Args: d.Args, Selectors: d.Selectors}
}
if coverage[profile] == nil {
coverage[profile] = map[string]bool{}
}
for _, row := range p.Domains {
for _, id := range row.Validated {
if id == d.ID {
coverage[profile][row.Host] = true
}
}
}
}
assigned := map[string]bool{}
for {
best := ""
var chosen []string
for profile, a := range profiles {
uncovered := []string{}
for _, host := range names {
if coverage[profile][host] && !assigned[host] {
uncovered = append(uncovered, host)
}
}
if len(uncovered) > len(chosen) || len(uncovered) > 0 && len(uncovered) == len(chosen) && (best == "" || a.Strategy < profiles[best].Strategy) {
best, chosen = profile, uncovered
}
}
if len(chosen) == 0 {
break
}
a := profiles[best]
a.Hosts = chosen
p.Assignments = append(p.Assignments, a)
for _, host := range chosen {
assigned[host] = true
}
}
for _, host := range names {
if !assigned[host] {
p.Unassigned = append(p.Unassigned, host)
}
}
return p
}
+300
View File
@@ -0,0 +1,300 @@
package main
import (
"bytes"
"crypto/hmac"
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"syscall"
"time"
)
const planKeyName = ".resource-plan-key"
const planRegistryName = ".resource-plan-strategies.json"
const planMatrixName = ".resource-plan-matrix.json"
// Reuse held-directory/private-entry checks, not monitor publication/lifecycle.
// Entries are write-once O_EXCL: no replacement, repair or clobber of foreign files.
type resourcePlanStore struct {
files *resourceMonitorStore
held map[string]*os.File
snapshots map[string][]byte
}
type planEnvelope struct {
Payload json.RawMessage `json:"payload"`
MAC string `json:"mac"`
}
func openResourcePlanStore(path string, create bool) (*resourcePlanStore, error) {
if managedDirectory(path) != nil {
return nil, errResourcePlan
}
fd, err := syscall.Open(path, syscall.O_RDONLY|syscall.O_DIRECTORY|syscall.O_NOFOLLOW|syscall.O_CLOEXEC, 0)
if err != nil {
return nil, err
}
s := &resourcePlanStore{files: &resourceMonitorStore{path: path, dir: os.NewFile(uintptr(fd), "plan directory")}, held: map[string]*os.File{}, snapshots: map[string][]byte{}}
fail := func(err error) (*resourcePlanStore, error) { s.close(); return nil, err }
for _, name := range []string{planKeyName, planRegistryName, planMatrixName} {
f, b, e := s.files.openEntry(name)
if e != nil && !errors.Is(e, os.ErrNotExist) {
return fail(errResourcePlan)
}
if e == nil {
s.held[name] = f
s.snapshots[name] = b
}
}
key := s.snapshots[planKeyName]
if key == nil && create && s.held[planRegistryName] == nil && s.held[planMatrixName] == nil {
key = make([]byte, 32)
if _, err = rand.Read(key); err != nil {
return fail(errResourcePlan)
}
if s.publish(planKeyName, key) != nil {
return fail(errResourcePlan)
}
}
if len(key) != 32 {
if !create && key == nil && s.held[planRegistryName] == nil && s.held[planMatrixName] == nil {
return fail(os.ErrNotExist)
}
return fail(errResourcePlan)
}
s.files.key = append([]byte(nil), key...)
if s.guard() != nil {
return fail(errResourcePlan)
}
return s, nil
}
func (s *resourcePlanStore) close() {
for _, f := range s.held {
f.Close()
}
s.files.dir.Close()
}
func (s *resourcePlanStore) guard() error {
if s.files.guard() != nil {
return errResourcePlan
}
names := map[string]bool{planKeyName: true, planRegistryName: true, planMatrixName: true}
for name := range s.held {
names[name] = true
}
for name := range names {
f := s.held[name]
if f == nil {
g, _, e := s.files.openEntry(name)
if g != nil {
g.Close()
}
if !errors.Is(e, os.ErrNotExist) {
return errResourcePlan
}
continue
}
b, e := s.files.trustedRead(name, f)
if e != nil || !bytes.Equal(b, s.snapshots[name]) {
return errResourcePlan
}
}
return nil
}
func (s *resourcePlanStore) publish(name string, b []byte) error {
if len(b) > 65536 || s.guard() != nil {
return errResourcePlan
}
fd, e := syscall.Openat(int(s.files.dir.Fd()), name, syscall.O_RDWR|syscall.O_CREAT|syscall.O_EXCL|syscall.O_NOFOLLOW|syscall.O_CLOEXEC, 0600)
if e != nil {
return errResourcePlan
}
f := os.NewFile(uintptr(fd), name)
// Keep incomplete entries on failure: never delete a possible foreign inode.
if _, e = f.Write(b); e != nil {
f.Close()
return errResourcePlan
}
if f.Sync() != nil || s.files.entryMatches(name, f) != nil {
f.Close()
return errResourcePlan
}
read, e := s.files.trustedRead(name, f)
if e != nil || !bytes.Equal(read, b) {
f.Close()
return errResourcePlan
}
s.held[name], s.snapshots[name] = f, append([]byte(nil), b...)
if s.files.dir.Sync() != nil || s.guard() != nil {
return errResourcePlan
}
return nil
}
func (s *resourcePlanStore) writeAuthenticated(name string, v interface{}) error {
b, e := json.Marshal(v)
if e != nil {
return errResourcePlan
}
envelope, e := json.Marshal(planEnvelope{Payload: b, MAC: resourceMAC(s.files.key, b)})
if e != nil {
return errResourcePlan
}
return s.publish(name, envelope)
}
func (s *resourcePlanStore) readAuthenticated(name string, dst interface{}) error {
if s.guard() != nil {
return errResourcePlan
}
if s.held[name] == nil {
return os.ErrNotExist
}
var envelope planEnvelope
if strictPlanJSON(s.snapshots[name], &envelope) != nil || !hmac.Equal([]byte(envelope.MAC), []byte(resourceMAC(s.files.key, envelope.Payload))) || strictPlanJSON(envelope.Payload, dst) != nil {
return errResourcePlan
}
return s.guard()
}
func (s *resourcePlanStore) registry() (strategyRegistry, error) {
var r strategyRegistry
if e := s.readAuthenticated(planRegistryName, &r); e != nil {
return r, e
}
b, _ := json.Marshal(r)
canonical, e := parseStrategyRegistry(b)
if e != nil || planDigest(canonical) != planDigest(r) {
return r, errResourcePlan
}
return r, nil
}
func (s *resourcePlanStore) importDefinitions(b []byte) error {
r, e := parseStrategyRegistry(b)
if e != nil {
return e
}
return s.writeAuthenticated(planRegistryName, r)
}
func (s *resourcePlanStore) saveCompleted(p completedQualification) error {
r, e := s.registry()
if e != nil || p.seal == "" || p.seal != planDigest(p.matrix) || validateMatrix(p.matrix, r) != nil {
return errResourcePlan
}
return s.writeAuthenticated(planMatrixName, p.matrix)
}
func (s *resourcePlanStore) matrix(r strategyRegistry) (qualificationMatrix, error) {
var m qualificationMatrix
e := s.readAuthenticated(planMatrixName, &m)
if e != nil {
return m, e
}
if validateMatrix(m, r) != nil {
return m, errResourcePlan
}
return m, nil
}
func resourcePlanCLI(args []string, stdout, stderr io.Writer, d managedDeps) int {
if os.Geteuid() != 0 {
fmt.Fprintln(stderr, "blocked: root target role required")
return 78
}
if _, err := managedTargetSettings(d.lookup); err != nil || d.directory != managedTargetDirectory {
fmt.Fprintln(stderr, "blocked: managed target required")
return 78
}
if len(args) == 6 && args[0] == "qualify" && args[1] == "--config" && args[3] == "--strategies" && args[5] == "--isolated" {
return qualifyResourceCLI(args[2], args[4], stdout, stderr, d)
}
if len(args) == 3 && args[0] == "qualify" && args[1] == "--config" {
fmt.Fprintln(stderr, "unknown: qualification requires --isolated and PID 1 isolated startup; no runtime changes made")
return 78
}
if len(args) == 5 && args[0] == "qualify" && args[1] == "--config" && args[3] == "--strategies" {
fmt.Fprintln(stderr, "unknown: qualification requires --isolated and PID 1 isolated startup; no runtime changes made")
return 78
}
if len(args) == 3 && args[0] == "strategy-import" && args[1] == "--strategies" {
fd, e := syscall.Open(args[2], syscall.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK|syscall.O_CLOEXEC, 0)
if e != nil {
fmt.Fprintln(stderr, "configerror: definitions unavailable")
return 64
}
f := os.NewFile(uintptr(fd), "definitions")
defer f.Close()
if resourcePrivateEntry(f) != nil {
fmt.Fprintln(stderr, "configerror: definitions require owned regular 0600 file")
return 64
}
b, e := io.ReadAll(io.LimitReader(f, 65537))
current, statErr := os.Lstat(args[2])
held, heldErr := f.Stat()
if e != nil || statErr != nil || heldErr != nil || !os.SameFile(current, held) || resourcePrivateEntry(f) != nil {
return 64
}
s, e := openResourcePlanStore(d.directory, true)
if e != nil {
fmt.Fprintln(stderr, "blocked: private registry unavailable")
return 78
}
defer s.close()
if s.importDefinitions(b) != nil {
fmt.Fprintln(stderr, "blocked: invalid definitions or immutable registry already exists")
return 78
}
fmt.Fprintln(stdout, "strategy definitions imported; qualification UNKNOWN")
return 0
}
if len(args) != 3 || args[0] != "plan" || args[1] != "--config" {
fmt.Fprintln(stderr, "usage: plan --config FILE | strategy-import --strategies FILE | qualify --config FILE --strategies FILE --isolated")
return 64
}
return resourcePlanProposal(args[2], stdout, stderr, d)
}
// Internal reader separated only to test the real CLI trust boundary in a
// temporary owned directory, without touching the provisioned target.
func resourcePlanProposal(configPath string, stdout, stderr io.Writer, d managedDeps) int {
key := make([]byte, 32)
registry := strategyRegistry{Version: 1, Strategies: []strategyDefinition{}}
var matrix qualificationMatrix
s, e := openResourcePlanStore(d.directory, false)
blocked := e != nil && !errors.Is(e, os.ErrNotExist)
if e == nil {
defer s.close()
key = s.files.key
registry, e = s.registry()
if e != nil {
blocked = !errors.Is(e, os.ErrNotExist)
registry = strategyRegistry{Version: 1, Strategies: []strategyDefinition{}}
}
if e == nil {
matrix, e = s.matrix(registry)
if e != nil {
blocked = !errors.Is(e, os.ErrNotExist)
matrix = qualificationMatrix{}
}
}
}
source, e := readResourceMonitorSource(configPath, d.lookup, key)
if e != nil {
fmt.Fprintln(stderr, "configerror: resource configuration unavailable")
return 64
}
p := deriveResourcePlan(source, key, registry, matrix, time.Now())
if blocked {
p.Evidence = "blocked"
}
if s != nil && s.guard() != nil {
p = deriveResourcePlan(source, key, registry, qualificationMatrix{}, time.Now())
p.Evidence = "blocked"
}
if json.NewEncoder(stdout).Encode(p) != nil {
return 1
}
if blocked || p.Evidence == "blocked" {
return 78
}
return 0
}
+331
View File
@@ -0,0 +1,331 @@
package main
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
)
func planFixture(t *testing.T) (resourceMonitorSource, []byte, strategyRegistry, qualificationMatrix, time.Time) {
t.Helper()
key := bytes.Repeat([]byte{7}, 32)
now := time.Unix(1700000000, 0)
source := resourceMonitorSource{server: "127.0.0.1:5353", proxy: "socks5h://127.0.0.1:1080", fingerprint: resourceMAC(key, []byte("config-secret")), config: resourceCheckConfig{Version: 1}}
for i, host := range []string{"a.example", "b.example", "c.example", "d.example", "e.example", "a.example"} {
service := "alpha"
group := "group-a"
if i == 2 {
service = "beta"
group = "group-b"
}
if i == 5 {
service = "gamma"
group = "group-c"
}
source.config.Services = append(source.config.Services, resourceCheckService{ID: service + string(rune('a'+i)), Resources: []resourceCheckSpec{{ID: "resource", Group: group, host: host, TLS: "both", URL: "https://" + host + "/SECRET?token=SECRET", privateConfig: "header = SECRET"}}})
}
registry := strategyRegistry{Version: 1}
for i, args := range []string{"--lua-desync=multisplit:pos=1", "--lua-desync=multidisorder:pos=1", "--lua-desync=fake:repeats=2"} {
registry.Strategies = append(registry.Strategies, strategyDefinition{ID: "s" + string(rune('1'+i)), Args: args, Selectors: strategySelectors{Transport: "tcp", Ports: "443", L7: "tls", TLS: "both"}})
}
matrix := qualificationMatrix{Version: 1, Registry: planDigest(registry), Runs: []qualificationRun{}}
for _, d := range registry.Strategies {
digest := planDigest(d)
r := qualificationRun{ID: "run-" + d.ID, Strategy: d.ID, Definition: digest, Config: source.fingerprint, Started: now.Add(-time.Minute).UnixMilli(), Completed: now.UnixMilli(), Attestation: qualificationAttestation{Collector: "owned-resource-nfq-v1", Runtime: "nfqws2-1.0.2", Definition: digest, LuaHash: strings.Repeat("a", 64), RulesHash: strings.Repeat("b", 64), WorkerArgsHash: strings.Repeat("c", 64), Queue: 200, PID: 123, StartTime: 456, Restored: true, Joined: true}}
for _, s := range source.config.Services {
for _, spec := range s.Resources {
if spec.host == "e.example" {
continue
}
pass := d.ID == "s1" && (spec.host == "a.example" || spec.host == "c.example") || d.ID == "s2" && spec.host == "b.example" || d.ID == "s3" && spec.host == "d.example"
for _, tls := range []string{"tls12", "tls13"} {
outcome := "expectation_failed"
if pass {
outcome = "pass"
}
r.Checks = append(r.Checks, qualificationCheck{Service: s.ID, Resource: spec.ID, Fingerprint: resourceRowFingerprint(key, source, spec), TLS: tls, Outcome: outcome, Attempts: 3})
}
}
}
matrix.Runs = append(matrix.Runs, r)
}
return source, key, registry, matrix, now
}
func cloneMatrix(m qualificationMatrix) qualificationMatrix {
b, _ := json.Marshal(m)
var v qualificationMatrix
json.Unmarshal(b, &v)
return v
}
func TestResourcePlanCoverage(t *testing.T) {
source, key, registry, matrix, now := planFixture(t)
p := deriveResourcePlan(source, key, registry, matrix, now)
want := [][]string{{"a.example", "c.example"}, {"b.example"}, {"d.example"}}
if len(p.Assignments) != 3 || !reflect.DeepEqual(p.Unassigned, []string{"e.example"}) {
t.Fatalf("%+v", p)
}
for i, a := range p.Assignments {
if !reflect.DeepEqual(a.Hosts, want[i]) {
t.Fatalf("%+v", p)
}
}
for i := 0; i < 100; i++ {
if !reflect.DeepEqual(p, deriveResourcePlan(source, key, registry, matrix, now)) {
t.Fatal("nondeterministic")
}
}
b, _ := json.Marshal(matrix)
out, _ := json.Marshal(p)
if strings.Contains(string(b)+string(out), "SECRET") || strings.Contains(string(b), "https://") {
t.Fatal("secret serialization")
}
// One bad URL on the same host in another service/group invalidates only a.
bad := cloneMatrix(matrix)
bad.Runs[0].Checks[len(bad.Runs[0].Checks)-1].Outcome = "expectation_failed"
changed := deriveResourcePlan(source, key, registry, bad, now)
if len(changed.Domains[0].Validated) != 0 || !reflect.DeepEqual(p.Domains[1:], changed.Domains[1:]) {
t.Fatalf("subset failure leaked: %+v", changed)
}
partial := cloneMatrix(matrix)
partial.Runs[0].Checks = partial.Runs[0].Checks[:len(partial.Runs[0].Checks)-1]
changed = deriveResourcePlan(source, key, registry, partial, now)
if len(changed.Domains[0].Validated) != 0 || len(changed.Domains[0].Unknown) != 1 {
t.Fatal("partial TLS qualified")
}
for _, mutate := range []func(*qualificationMatrix){
func(m *qualificationMatrix) { m.Runs[0].Config = strings.Repeat("c", 64) },
func(m *qualificationMatrix) {
m.Runs[0].Completed = now.Add(-25 * time.Hour).UnixMilli()
m.Runs[0].Started = m.Runs[0].Completed - 1
},
func(m *qualificationMatrix) { m.Runs[0].Attestation.Joined = false },
func(m *qualificationMatrix) { m.Runs[0].Checks = append(m.Runs[0].Checks, m.Runs[0].Checks[0]) },
func(m *qualificationMatrix) { m.Runs[1].ID = m.Runs[0].ID },
func(m *qualificationMatrix) { m.Runs[0].Definition = strings.Repeat("c", 64) },
} {
m := cloneMatrix(matrix)
mutate(&m)
if len(deriveResourcePlan(source, key, registry, m, now).Domains[0].Validated) != 0 {
t.Fatal("invalid evidence qualified")
}
}
source.fingerprint = strings.Repeat("d", 64)
if len(deriveResourcePlan(source, key, registry, matrix, now).Assignments) != 0 {
t.Fatal("changed config qualified")
}
}
func TestResourcePlanSelectorsAndAlternatives(t *testing.T) {
source, key, registry, matrix, now := planFixture(t)
registry.Strategies[1].Args = registry.Strategies[0].Args
matrix.Registry = planDigest(registry)
matrix.Runs[1].Definition = planDigest(registry.Strategies[1])
matrix.Runs[1].Attestation.Definition = matrix.Runs[1].Definition
// Add a shared alternative on c without removing original evidence.
for i := range matrix.Runs[1].Checks {
if matrix.Runs[1].Checks[i].Service == "betac" {
matrix.Runs[1].Checks[i].Outcome = "pass"
}
}
p := deriveResourcePlan(source, key, registry, matrix, now)
if len(p.Assignments) != 2 || !reflect.DeepEqual(p.Assignments[0].Hosts, []string{"a.example", "b.example", "c.example"}) || len(p.Domains[2].Validated) != 2 {
t.Fatalf("merge/alternatives: %+v", p)
}
registry.Strategies[1].Selectors.TLS = "tls12"
matrix.Registry = planDigest(registry)
matrix.Runs[1].Definition = planDigest(registry.Strategies[1])
matrix.Runs[1].Attestation.Definition = matrix.Runs[1].Definition
// Hosts a,c require both; host b only tls12.
source.config.Services[1].Resources[0].TLS = "tls12"
for i := range matrix.Runs[1].Checks {
c := &matrix.Runs[1].Checks[i]
if c.Service == "alphab" {
c.Fingerprint = resourceRowFingerprint(key, source, source.config.Services[1].Resources[0])
}
}
p = deriveResourcePlan(source, key, registry, matrix, now)
if len(p.Assignments) != 3 || p.Assignments[0].Selectors == p.Assignments[1].Selectors {
t.Fatalf("selectors merged: %+v", p)
}
}
func TestResourcePlanRegistryValidation(t *testing.T) {
_, _, r, _, _ := planFixture(t)
d := r.Strategies[0]
d.Args = " \t--payload=tls_client_hello\n--lua-desync=multisplit:pos=1 "
v, e := canonicalStrategy(d)
if e != nil || v.Args != "--payload=tls_client_hello --lua-desync=multisplit:pos=1" {
t.Fatalf("canonical: %+v %v", v, e)
}
for _, args := range []string{"--lua-init=@/opt/zapret2/lua/evil.lua", "--qnum=200", "--user=root", "--fwmark=1", "--filter-mark=1", "--lua-desync=execute:cmd=id", "--lua-desync=fake:blob=/etc/passwd", "--lua-desync=fake:blob=secret.lua", "--lua-desync=fake:repeats=1;id"} {
d.Args = args
if _, e := canonicalStrategy(d); e == nil {
t.Fatalf("accepted %s", args)
}
}
for _, b := range []string{`{"version":1,"version":1,"strategies":[]}`, `{"version":1,"strategies":[],"evidence":true}`} {
if _, e := parseStrategyRegistry([]byte(b)); e == nil {
t.Fatal("import accepted proof/duplicate")
}
}
}
func TestResourcePlanPrivateStore(t *testing.T) {
_, _, registry, matrix, _ := planFixture(t)
dir := t.TempDir()
os.Chmod(dir, 0700)
s, e := openResourcePlanStore(dir, true)
if e != nil {
t.Fatal(e)
}
defer s.close()
b, _ := json.Marshal(registry)
if s.importDefinitions(b) != nil {
t.Fatal("import")
}
if s.importDefinitions(b) == nil {
t.Fatal("clobbered immutable registry")
}
proof := completedQualification{matrix: cloneMatrix(matrix), seal: planDigest(matrix)}
proof.matrix.Runs[0].Checks[0].Outcome = "expectation_failed"
if s.saveCompleted(proof) == nil {
t.Fatal("post-seal mutation")
}
if s.saveCompleted(completedQualification{matrix: matrix}) == nil {
t.Fatal("unsealed DTO")
}
if s.saveCompleted(completedQualification{matrix: matrix, seal: planDigest(matrix)}) != nil {
t.Fatal("fixture save")
}
loaded, e := s.matrix(registry)
if e != nil || !reflect.DeepEqual(loaded, matrix) {
t.Fatalf("load: %v", e)
}
second, e := openResourcePlanStore(dir, false)
if e != nil {
t.Fatal(e)
}
second.close()
path := filepath.Join(dir, planMatrixName)
raw, e := os.ReadFile(path)
if e != nil {
t.Fatal(e)
}
raw = bytes.Replace(raw, []byte(`"outcome":"pass"`), []byte(`"outcome":"timeout"`), 1)
os.WriteFile(path, raw, 0600)
if _, e = s.matrix(registry); e == nil {
t.Fatal("held mutation accepted")
}
reopened, e := openResourcePlanStore(dir, false)
if e != nil {
t.Fatal(e)
}
defer reopened.close()
if _, e = reopened.matrix(registry); e == nil {
t.Fatal("forged HMAC accepted")
}
}
func TestResourcePlanForeignEntries(t *testing.T) {
for _, kind := range []string{"file", "symlink", "replacement"} {
t.Run(kind, func(t *testing.T) {
dir := t.TempDir()
os.Chmod(dir, 0700)
path := filepath.Join(dir, planRegistryName)
if kind == "file" {
os.WriteFile(path, []byte("FOREIGN"), 0600)
} else if kind == "symlink" {
os.Symlink("foreign", path)
}
s, e := openResourcePlanStore(dir, true)
if kind != "replacement" {
if e == nil {
s.close()
t.Fatal("adopted foreign registry without key")
}
return
}
if e != nil {
t.Fatal(e)
}
defer s.close()
_, _, r, _, _ := planFixture(t)
b, _ := json.Marshal(r)
if s.importDefinitions(b) != nil {
t.Fatal("import")
}
os.Rename(path, path+".old")
os.WriteFile(path, s.snapshots[planRegistryName], 0600)
if _, e = s.registry(); e == nil {
t.Fatal("adopted identical foreign replacement")
}
if s.importDefinitions(b) == nil {
t.Fatal("clobbered replacement")
}
})
}
}
func TestResourcePlanCLIUnknownAndForged(t *testing.T) {
dir := t.TempDir()
os.Chmod(dir, 0700)
path := filepath.Join(dir, "resources.json")
config := `{"version":1,"services":[{"id":"service","resources":[{"id":"resource","group":"metadata","kind":"http","url":"https://a.example/SECRET","method":"GET","tls":"both","timeout_ms":1000,"expect":{"statuses":[200],"min_bytes":0,"max_bytes":100}}]}]}`
if err := os.WriteFile(path, []byte(config), 0600); err != nil {
t.Fatal(err)
}
env := map[string]string{"ZAPRET_AUTOTUNE_SOCKS_PROXY": "socks5h://127.0.0.1:1080", "ZAPRET_AUTOTUNE_DNS_MODE": "native", "SS_DNS_SERVER": "1.1.1.1"}
d := managedDeps{directory: dir, lookup: func(k string) (string, bool) { v, ok := env[k]; return v, ok }}
var out, diagnostics bytes.Buffer
code := resourcePlanProposal(path, &out, &diagnostics, d)
if code != 0 {
t.Fatalf("unknown reader: %d %s", code, diagnostics.String())
}
var p resourcePlan
if json.Unmarshal(out.Bytes(), &p) != nil || p.Evidence != "unknown" || len(p.Assignments) != 0 || len(p.Unassigned) != 1 || strings.Contains(out.String(), "SECRET") {
t.Fatalf("%s", out.String())
}
s, err := openResourcePlanStore(dir, true)
if err != nil {
t.Fatal(err)
}
_, _, registry, matrix, _ := planFixture(t)
definitions, _ := json.Marshal(registry)
if s.importDefinitions(definitions) != nil {
t.Fatal("import")
}
source, err := readResourceMonitorSource(path, d.lookup, s.files.key)
if err != nil {
t.Fatal(err)
}
for i := range matrix.Runs {
r := &matrix.Runs[i]
r.Config = source.fingerprint
r.Started = time.Now().Add(-time.Minute).UnixMilli()
r.Completed = time.Now().UnixMilli()
r.Checks = nil
for _, tls := range []string{"tls12", "tls13"} {
r.Checks = append(r.Checks, qualificationCheck{Service: "service", Resource: "resource", TLS: tls, Outcome: "pass", Fingerprint: resourceRowFingerprint(s.files.key, source, source.config.Services[0].Resources[0]), Attempts: 3})
}
}
if s.saveCompleted(completedQualification{matrix: matrix, seal: planDigest(matrix)}) != nil {
t.Fatal("sealed fixture")
}
s.close()
out.Reset()
code = resourcePlanProposal(path, &out, &diagnostics, d)
if code != 0 || json.Unmarshal(out.Bytes(), &p) != nil || p.Evidence != "authenticated" || len(p.Assignments) != 1 || len(p.Domains[0].Validated) != 3 {
t.Fatalf("authenticated CLI %d %s", code, out.String())
}
// Public JSON PASS and monitor observations cannot become private proof.
if err := os.WriteFile(filepath.Join(dir, planMatrixName), []byte(`{"pass":true,"health":"HEALTHY"}`), 0600); err != nil {
t.Fatal(err)
}
out.Reset()
diagnostics.Reset()
code = resourcePlanProposal(path, &out, &diagnostics, d)
if code != 78 || !strings.Contains(out.String(), `"evidence":"blocked"`) || strings.Contains(out.String(), `"hosts":[`) {
t.Fatalf("forged CLI %d %s %s", code, out.String(), diagnostics.String())
}
}
+590
View File
@@ -0,0 +1,590 @@
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) {
// Fixed image files only. Bind both executable and Lua inputs before and after
// every candidate; never accept a caller-supplied native hash/version label.
hashes := []string{}
for _, path := range []string{qualificationEngine, "/opt/zapret2/lua/zapret-lib.lua", "/opt/zapret2/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)
}
func newResourceQualifier(config, strategies string, d managedDeps, s *resourcePlanStore) resourceQualifier {
r := newManagedAssetRuntime(d)
q := resourceQualifier{runtime: r, checks: productionResourceCheckDeps(), now: time.Now, filesHash: qualificationFilesHash}
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
}
exe, e := os.Readlink(fmt.Sprintf("/proc/%d/exe", pid))
if e != nil || exe != qualificationEngine {
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 := qualificationFilesHash()
if e != nil {
return qualificationAttestation{}, e
}
versionCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
var output bytes.Buffer
version := exec.CommandContext(versionCtx, qualificationEngine, "--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)
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)
}
@@ -0,0 +1,159 @@
package main
import (
"bytes"
"context"
"errors"
"io"
"os"
"strconv"
"strings"
"syscall"
"time"
)
const qualificationCompletionName = "qualification-completed.json"
const qualificationCompletionDir = "/run/zapret-autotune-ss"
// This acknowledges controller cleanup, NOT candidate qualification. It cannot
// authorize a PASS, matrix write or acceptance of ordinary monitor exit 78.
type qualificationCompletion struct {
Version int `json:"version"`
PID int `json:"pid"`
Code int `json:"code"`
CleanupComplete bool `json:"cleanup_complete"`
}
type qualificationCleanupFailure struct{ Err error }
func (e *qualificationCleanupFailure) Error() string { return "qualification cleanup unverified" }
func (e *qualificationCleanupFailure) Unwrap() error { return e.Err }
func qualificationCompletionStore(path string, key []byte) (*resourcePlanStore, error) {
if managedDirectory(path) != nil || len(key) != 32 {
return nil, errResourcePlan
}
fd, e := syscall.Open(path, syscall.O_RDONLY|syscall.O_DIRECTORY|syscall.O_NOFOLLOW|syscall.O_CLOEXEC, 0)
if e != nil {
return nil, e
}
s := &resourcePlanStore{files: &resourceMonitorStore{path: path, dir: os.NewFile(uintptr(fd), "qualification completion directory"), key: append([]byte(nil), key...)}, held: map[string]*os.File{}, snapshots: map[string][]byte{}}
f, b, e := s.files.openEntry(qualificationCompletionName)
if e == nil {
s.held[qualificationCompletionName], s.snapshots[qualificationCompletionName] = f, b
} else if !errors.Is(e, os.ErrNotExist) {
s.close()
return nil, errResourcePlan
}
return s, nil
}
func writeQualificationCompletion(path string, key []byte, pid, code int) error {
if pid <= 1 || (code != 0 && code != 78) {
return errResourcePlan
}
s, e := qualificationCompletionStore(path, key)
if e != nil {
return e
}
defer s.close()
return s.writeAuthenticated(qualificationCompletionName, qualificationCompletion{Version: 1, PID: pid, Code: code, CleanupComplete: true})
}
func readQualificationCompletion(path string, key []byte, pid, code int) error {
s, e := qualificationCompletionStore(path, key)
if e != nil {
return e
}
defer s.close()
var ack qualificationCompletion
if s.readAuthenticated(qualificationCompletionName, &ack) != nil || ack.Version != 1 || ack.PID != pid || pid <= 1 || ack.Code != code || (code != 0 && code != 78) || !ack.CleanupComplete {
return errResourcePlan
}
return nil
}
func qualificationCleanupVerified(err error, restore func(context.Context) error) bool {
var ownedCleanup *managedAssetCleanupFailure
var restoration *qualificationCleanupFailure
if errors.As(err, &ownedCleanup) || errors.As(err, &restoration) {
return false
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
return restore(ctx) == nil
}
func qualificationCompletionCLI(args []string, d managedDeps) int {
if len(args) != 2 || os.Geteuid() != 0 || os.Getppid() != 1 {
return 78
}
pid, e := strconv.Atoi(args[0])
code, ce := strconv.Atoi(args[1])
if e != nil || ce != nil || strconv.Itoa(pid) != args[0] || strconv.Itoa(code) != args[1] {
return 78
}
cmd, e := os.ReadFile("/proc/1/cmdline")
if e != nil || !bytes.Equal(cmd, []byte("/bin/sh\x00/opt/managed-target-entrypoint.sh\x00")) {
return 78
}
env, e := os.ReadFile("/proc/1/environ")
if e != nil {
return 78
}
values := map[string]string{}
for _, field := range strings.Split(string(env), "\x00") {
k, v, _ := strings.Cut(field, "=")
values[k] = v
}
if values["ZAPRET_AUTOTUNE_QUALIFY_CONFIG"] == "" || values["ZAPRET_AUTOTUNE_STRATEGY_CONFIG"] == "" {
return 78
}
if _, e := managedRuntimeGuard(d); e != nil {
return 78
}
lock, e := acquireManagedMonitorSingleton(d)
if e != nil {
return 78
}
defer lock.close()
s, e := openResourcePlanStore(d.directory, false)
if e != nil {
return 78
}
defer s.close()
if s.protectBaseline() != nil || qualificationBaseline(d.directory) != nil {
return 78
}
dns, e := parseManagedDNSConfig(d.lookup)
if e != nil || verifyResourceDNSRuntime(dns) != nil || qualificationNoWorker() != nil {
return 78
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
r := newManagedAssetRuntime(d)
tables, e := r.nft(ctx, "list tables\n")
if e != nil || strings.Contains(tables, "table inet "+managedAssetTable) {
return 78
}
// The private session ACK ties completion to this PID 1 launch, not a stale
// target-volume status. The completion itself lives only on this boot's tmpfs.
dir, e := os.Open(qualificationCompletionDir)
if e != nil {
return 78
}
defer dir.Close()
f, e := resourcePrivateFile(dir, "monitor-ready")
if e != nil {
return 78
}
defer f.Close()
b, e := io.ReadAll(io.LimitReader(f, 32))
if e != nil || string(b) != strconv.Itoa(pid)+"\n" {
return 78
}
if readQualificationCompletion(qualificationCompletionDir, s.files.key, pid, code) != nil || s.guard() != nil {
return 78
}
return 0
}
@@ -0,0 +1,102 @@
package main
import (
"bytes"
"context"
"errors"
"os"
"path/filepath"
"strings"
"testing"
)
func TestResourceQualifierCompletionOwnership(t *testing.T) {
dir := t.TempDir()
if e := os.Chmod(dir, 0700); e != nil {
t.Fatal(e)
}
key := bytes.Repeat([]byte{9}, 32)
if e := writeQualificationCompletion(dir, key, 123, 78); e != nil {
t.Fatal(e)
}
if e := readQualificationCompletion(dir, key, 123, 78); e != nil {
t.Fatal(e)
}
for _, mismatch := range []struct {
pid, code int
key []byte
}{{124, 78, key}, {123, 0, key}, {123, 78, bytes.Repeat([]byte{8}, 32)}} {
if readQualificationCompletion(dir, mismatch.key, mismatch.pid, mismatch.code) == nil {
t.Fatal("accepted foreign launch/status/key")
}
}
if writeQualificationCompletion(dir, key, 123, 78) == nil {
t.Fatal("clobbered write-once completion")
}
path := filepath.Join(dir, qualificationCompletionName)
raw, e := os.ReadFile(path)
if e != nil {
t.Fatal(e)
}
if e := os.WriteFile(path, bytes.Replace(raw, []byte(`"cleanup_complete":true`), []byte(`"cleanup_complete":false`), 1), 0600); e != nil {
t.Fatal(e)
}
if readQualificationCompletion(dir, key, 123, 78) == nil {
t.Fatal("accepted forged completion")
}
}
func TestResourceQualifierCompletionCleanupVeto(t *testing.T) {
for _, tc := range []struct {
err error
safe bool
}{
{nil, true}, {errors.New("source changed, proof UNKNOWN"), true}, {context.Canceled, true},
{&managedRecoveryFatal{Err: &managedAssetCleanupFailure{Err: errors.New("worker join failed")}}, false},
{errors.Join(context.Canceled, &qualificationCleanupFailure{Err: errors.New("baseline restoration failed")}), false},
} {
called := false
got := qualificationCleanupVerified(tc.err, func(context.Context) error { called = true; return nil })
if got != tc.safe || called != tc.safe {
t.Fatalf("cleanup ACK=%v called=%v for %T", got, called, tc.err)
}
}
if qualificationCleanupVerified(nil, func(context.Context) error { return errors.New("live baseline guard failed") }) {
t.Fatal("acknowledged unverified cleanup")
}
}
func TestResourceQualifierCompletionForeignFile(t *testing.T) {
dir := t.TempDir()
os.Chmod(dir, 0700)
path := filepath.Join(dir, qualificationCompletionName)
if e := os.WriteFile(path, []byte("FOREIGN"), 0600); e != nil {
t.Fatal(e)
}
key := bytes.Repeat([]byte{9}, 32)
if writeQualificationCompletion(dir, key, 123, 78) == nil || readQualificationCompletion(dir, key, 123, 78) == nil {
t.Fatal("adopted foreign completion")
}
b, e := os.ReadFile(path)
if e != nil || string(b) != "FOREIGN" {
t.Fatal("changed foreign completion")
}
}
func TestResourceQualifierOwnedCleanupFailureCannotAcknowledge(t *testing.T) {
q, s, r, _, _ := qualifierFixture(t)
nft := q.runtime.nft
q.runtime.nft = func(ctx context.Context, input string) (string, error) {
if strings.HasPrefix(input, "delete table") {
return "", errors.New("owned nft cleanup failed")
}
return nft(ctx, input)
}
proof, err := q.complete(context.Background(), s, r)
if err == nil || proof.seal != "" {
t.Fatal("unsafe lifecycle sealed proof")
}
if qualificationCleanupVerified(err, q.restore) {
t.Fatal("unsafe owned cleanup authorized completed ACK")
}
}
@@ -0,0 +1,332 @@
package main
import (
"context"
"encoding/json"
"errors"
"io"
"net"
"os"
"os/exec"
"strconv"
"strings"
"testing"
"time"
)
func qualifierFixture(t *testing.T, candidates ...int) (resourceQualifier, *resourcePlanStore, strategyRegistry, *[]string, *int) {
t.Helper()
dir := t.TempDir()
os.Chmod(dir, 0700)
store, e := openResourcePlanStore(dir, true)
if e != nil {
t.Fatal(e)
}
t.Cleanup(store.close)
raw := `{"version":1,"services":[{"id":"one","resources":[{"id":"good","group":"a","kind":"http","url":"https://a.example/good?SECRET","method":"GET","tls":"both","timeout_ms":1000,"expect":{"statuses":[200],"min_bytes":1,"max_bytes":1000}}]},{"id":"two","resources":[{"id":"other","group":"b","kind":"http","url":"https://a.example/other?SECRET","method":"GET","tls":"both","timeout_ms":1000,"expect":{"statuses":[200],"min_bytes":1,"max_bytes":1000}}]}]}`
cfg, e := parseResourceChecks([]byte(raw), func(string) (string, bool) { return "", false })
if e != nil {
t.Fatal(e)
}
source := resourceMonitorSource{config: cfg, server: "127.0.0.1:5353", proxy: "socks5h://127.0.0.1:1080", fingerprint: resourceMAC(store.files.key, []byte(raw))}
_, _, registry, _, _ := planFixture(t)
count := 1
if len(candidates) > 0 {
count = candidates[0]
}
registry.Strategies = registry.Strategies[:count]
b, _ := json.Marshal(registry)
if store.importDefinitions(b) != nil {
t.Fatal("registry import")
}
runtime, events, _ := assetRuntimeFixture(t)
calls := 0
q := resourceQualifier{runtime: runtime, source: func() (resourceMonitorSource, error) { return source, nil }, guard: func() error { return nil }, now: time.Now, filesHash: func() (string, error) { return strings.Repeat("a", 64), nil }}
q.restore = func(ctx context.Context) error { *events = append(*events, "restore"); return ctx.Err() }
q.attest = func(_ context.Context, pid int, args []string, d strategyDefinition) (qualificationAttestation, error) {
if !strings.Contains(strings.Join(args, " "), "--filter-l7=tls") {
t.Fatal("generic TLS selector not enforced")
}
*events = append(*events, "attest")
return qualificationAttestation{Collector: "owned-resource-nfq-v1", Runtime: "nfqws2-1.0.2", Definition: planDigest(d), LuaHash: strings.Repeat("a", 64), RulesHash: planDigest(managedAssetRules), WorkerArgsHash: planDigest(args), Queue: 200, PID: pid, StartTime: 456}, nil
}
q.checks = resourceCheckDeps{guard: func() error { return nil }, resolve: func(context.Context, string, string) ([]net.IP, error) { return []net.IP{net.ParseIP("8.8.8.8")}, nil }, runner: func(_ context.Context, args []string, stdin io.Reader, out, diagnostics io.Writer) error {
calls++
*events = append(*events, "capture")
b, e := io.ReadAll(stdin)
if e != nil {
return e
}
if !strings.Contains(string(b), "connect-to =") || strings.Contains(strings.Join(args, " "), "SECRET") {
t.Fatal("private pin/argv violated")
}
raw, e := strconv.Unquote(strings.TrimPrefix(strings.Split(string(b), "\n")[0], "url = "))
if e != nil {
return e
}
protocol := "tls12"
if strings.Contains(strings.Join(args, " "), "--tlsv1.3") {
protocol = "tls13"
}
configuredWire(raw, "GET", protocol, "pass", out, diagnostics)
return nil
}}
return q, store, registry, events, &calls
}
func TestResourceQualifierCompletedProducer(t *testing.T) {
q, s, r, events, calls := qualifierFixture(t)
proof, e := q.complete(context.Background(), s, r)
if e != nil {
t.Fatal(e)
}
if *calls != 12 || len(proof.matrix.Runs) != 1 || len(proof.matrix.Runs[0].Checks) != 4 {
t.Fatalf("real repeats %d %+v", *calls, proof)
}
for _, c := range proof.matrix.Runs[0].Checks {
if c.Outcome != "pass" || c.Attempts != 3 {
t.Fatalf("%+v", c)
}
}
last := -1
for _, event := range []string{"start", "ready", "activate", "attest", "capture", "delete", "join", "restore"} {
found := -1
for i := last + 1; i < len(*events); i++ {
if (*events)[i] == event {
found = i
break
}
}
if found < 0 {
t.Fatalf("ordering: %v", *events)
}
last = found
}
if !proof.matrix.Runs[0].Attestation.Joined || !proof.matrix.Runs[0].Attestation.Restored {
t.Fatal("premature seal")
}
if s.saveCompleted(proof) != nil {
t.Fatal("producer publication")
}
before := *calls
if _, e := q.complete(context.Background(), s, r); e == nil || *calls != before {
t.Fatal("retested immutable matrix")
}
}
func TestResourceQualifierCapacityBeforeWorkers(t *testing.T) {
q, _, registry, events, calls := qualifierFixture(t)
dir := t.TempDir()
if e := os.Chmod(dir, 0700); e != nil {
t.Fatal(e)
}
s, e := openResourcePlanStore(dir, true)
if e != nil {
t.Fatal(e)
}
defer s.close()
template := registry.Strategies[0]
registry.Strategies = nil
for i := 0; i < 32; i++ {
definition := template
definition.ID = "strategy-" + strconv.Itoa(i)
registry.Strategies = append(registry.Strategies, definition)
}
b, _ := json.Marshal(registry)
registry, e = parseStrategyRegistry(b)
if e != nil {
t.Fatal(e)
}
if e = s.importDefinitions(b); e != nil {
t.Fatal(e)
}
source, e := q.source()
if e != nil {
t.Fatal(e)
}
spec := source.config.Services[0].Resources[0]
cfg := resourceCheckConfig{Version: 1, Services: []resourceCheckService{{ID: "service"}}}
for i := 0; i < 64; i++ {
resource := spec
resource.ID = "resource-" + strconv.Itoa(i)
cfg.Services[0].Resources = append(cfg.Services[0].Resources, resource)
}
b, _ = json.Marshal(cfg)
cfg, e = parseResourceChecks(b, func(string) (string, bool) { return "", false })
if e != nil {
t.Fatal(e)
}
source.config = cfg
source.fingerprint = resourceMAC(s.files.key, b)
q.source = func() (resourceMonitorSource, error) { return source, nil }
starts := 0
start := q.runtime.start
q.runtime.start = func(args []string) (managedAssetWorker, error) { starts++; return start(args) }
proof, e := q.complete(context.Background(), s, registry)
if !errors.Is(e, errQualificationCapacity) || starts != 0 || *calls != 0 || len(*events) != 0 || proof.seal != "" || s.held[planMatrixName] != nil {
t.Fatalf("capacity rejection too late: err=%v starts=%d calls=%d events=%v", e, starts, *calls, *events)
}
}
func TestResourceQualifierSequentialCandidates(t *testing.T) {
q, s, r, events, calls := qualifierFixture(t, 2)
proof, err := q.complete(context.Background(), s, r)
if err != nil {
t.Fatal(err)
}
if *calls != 24 || len(proof.matrix.Runs) != 2 {
t.Fatal("incomplete sequential batch", *calls)
}
stages := strings.Join(*events, ",")
if strings.Count(stages, "start") != 2 || strings.Count(stages, "restore") != 2 || !strings.Contains(stages, "delete,join,restore,inventory,start") {
t.Fatal("next candidate began before previous cleanup", stages)
}
if s.saveCompleted(proof) != nil {
t.Fatal("sequential producer publication")
}
}
func TestResourceQualifierNegativeAndUnknown(t *testing.T) {
for _, mode := range []string{"negative", "status500", "tls-peer", "timeout", "timeout-infrastructure", "infrastructure", "guard", "cancel", "source", "restore", "hash", "mutation"} {
t.Run(mode, func(t *testing.T) {
q, s, r, _, calls := qualifierFixture(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
original := q.checks.runner
if strings.HasPrefix(mode, "timeout") {
oldSource := q.source
source, e := oldSource()
if e != nil {
t.Fatal(e)
}
for i := range source.config.Services {
for j := range source.config.Services[i].Resources {
source.config.Services[i].Resources[j].TimeoutMS = 100
}
}
q.source = func() (resourceMonitorSource, error) { return source, nil }
}
q.checks.runner = func(ctx context.Context, args []string, stdin io.Reader, out, diag io.Writer) error {
if mode == "tls-peer" {
(*calls)++
return exec.CommandContext(ctx, "/bin/sh", "-c", "exit 35").Run()
}
if strings.HasPrefix(mode, "timeout") {
(*calls)++
<-ctx.Done()
if mode == "timeout-infrastructure" {
return errors.New("local safety failure coincident with timeout")
}
return ctx.Err()
}
if mode == "infrastructure" {
return errors.New("unknown local collector infrastructure")
}
if mode == "cancel" {
cancel()
return context.Canceled
}
if (mode == "negative" || mode == "status500") && *calls == 6 { // First repeat of other URL; still do all repeats.
*calls++
b, e := io.ReadAll(stdin)
if e != nil {
return e
}
raw, e := strconv.Unquote(strings.TrimPrefix(strings.Split(string(b), "\n")[0], "url = "))
if e != nil {
return e
}
wireMode := "redirect"
if mode == "status500" {
wireMode = "status500"
}
configuredWire(raw, "GET", "tls12", wireMode, out, diag)
return nil
}
return original(ctx, args, stdin, out, diag)
}
if mode == "guard" {
q.checks.guard = func() error { return errors.New("ownership lost") }
}
if mode == "source" {
source := q.source
q.source = func() (resourceMonitorSource, error) {
v, e := source()
if *calls > 0 {
v.fingerprint = strings.Repeat("d", 64)
}
return v, e
}
}
if mode == "restore" {
q.restore = func(context.Context) error { return errors.New("unsafe restore") }
}
if mode == "hash" {
q.filesHash = func() (string, error) { return strings.Repeat("b", 64), nil }
}
proof, e := q.complete(ctx, s, r)
if mode == "negative" || mode == "status500" || mode == "timeout" || mode == "tls-peer" {
if e != nil || *calls != 12 {
t.Fatalf("negative incomplete: %v %d", e, *calls)
}
source, _ := q.source()
if mode == "status500" && proof.matrix.Runs[0].Checks[2].Outcome != "expectation_failed" {
t.Fatal("500 must be a complete configured expectation failure")
}
if mode == "timeout" {
for _, c := range proof.matrix.Runs[0].Checks {
if c.Attempts != 3 || c.Outcome != "timeout" {
t.Fatal("timeout contract", c)
}
}
}
p := deriveResourcePlan(source, s.files.key, r, proof.matrix, time.Now())
if len(p.Assignments) != 0 || len(p.Domains[0].Negative) != 1 {
t.Fatalf("bad required URL qualified: %+v", p)
}
return
}
if mode == "mutation" {
if e != nil {
t.Fatal(e)
}
proof.matrix.Runs[0].Checks[0].Outcome = "timeout"
if s.saveCompleted(proof) == nil {
t.Fatal("post-seal mutation")
}
return
}
if e == nil || proof.seal != "" || s.held[planMatrixName] != nil {
t.Fatalf("published incomplete %s: %v", mode, e)
}
})
}
}
func TestResourceQualifierTypedPeerResponseBoundary(t *testing.T) {
for _, mode := range []string{"redirect", "extra"} {
t.Run(mode, func(t *testing.T) {
runner := func(_ context.Context, _ []string, _ io.Reader, out, diag io.Writer) error {
configuredWire("https://a.example/", "GET", "tls12", mode, out, diag)
return nil
}
_, _, err := collectResourceTransferOptions(context.Background(), "socks5h://127.0.0.1:1080", "https://a.example/", "a.example", "tls12", false, runner, resourceTransferOptions{generic: true, method: "GET", limit: 100, timeout: time.Second})
var peer *resourcePeerResponseRejection
var fatal *managedRecoveryFatal
if mode == "redirect" {
if !errors.As(err, &peer) || errors.As(err, &fatal) || !resourceRejectionOnly(err) {
t.Fatalf("redirect cause should be typed peer rejection, got %T", err)
}
} else if err == nil || resourceRejectionOnly(err) || !errors.As(err, &fatal) {
t.Fatal("malformed body/evidence became peer negative")
}
})
}
}
func TestResourceQualifierRejectsRegularTarget(t *testing.T) {
d := managedDependencies(runPlayerCurl)
if qualificationMode("resources", "strategies", d) == nil {
t.Fatal("regular process authorized isolated qualification")
}
}
+91 -1
View File
@@ -24,7 +24,7 @@ DNS_CLI = Path(CLI_TMP.name) / 'autotune'
subprocess.run(['go', 'build', '-o', str(DNS_CLI), './cmd/zapret-autotune'],
cwd=ROOT, env=dict(os.environ, GOPROXY='off', GOTOOLCHAIN='local'), check=True)
FAKE = r'''#!/usr/bin/env python3
import ctypes, os, pathlib, signal, subprocess, sys, time
import ctypes, json, os, pathlib, signal, subprocess, sys, time
root = pathlib.Path(os.environ['FIXTURE'])
name = pathlib.Path(sys.argv[0]).name
scenario = os.environ['SCENARIO']
@@ -71,6 +71,11 @@ if name == 'date':
sys.exit(0)
if name == 'sleep':
seconds = int(sys.argv[1])
if scenario.startswith('qualify-') and (root / 'qualified').exists():
log('safe-idle')
os.kill(os.getppid(), signal.SIGTERM)
time.sleep(.02)
sys.exit(0)
if scenario.startswith('generic-budget-') and seconds != 60:
os.execv(os.environ['REAL_SLEEP'], [os.environ['REAL_SLEEP'], *sys.argv[1:]])
if seconds == 60 and scenario in ('generic-ss', 'generic-unsafe', 'generic-budget-monitor', 'generic-budget-reconcile', 'generic-budget-ack'):
@@ -125,6 +130,27 @@ if name == 'zapret2':
sys.exit(1)
sys.exit(0)
assert name == 'autotune'
if sys.argv[1] == 'qualification-race-barrier':
# Deterministic interleaving: child has exited, shell has not reached wait.
pid = int(sys.argv[2])
deadline = time.monotonic() + 5
while True:
stat = pathlib.Path('/proc/' + str(pid) + '/stat')
if not stat.exists() or stat.read_text().rsplit(') ', 1)[1].split()[0] == 'Z':
break
assert time.monotonic() < deadline, 'qualifier did not exit at barrier'
time.sleep(.001)
log('term-before-parent-reap')
os.kill(os.getppid(), signal.SIGTERM)
sys.exit(0)
if sys.argv[1] == 'qualification-completion-check':
ack = root / 'completion-ack'
if not ack.exists() or ack.is_symlink() or ack.stat().st_mode & 0o777 != 0o600:
sys.exit(78)
payload = json.loads(ack.read_text())
assert payload == {'pid': int(sys.argv[2]), 'code': int(sys.argv[3]), 'cleanup_complete': True}
log('completion-checked')
sys.exit(0)
if sys.argv[1:] == ['target-config-check']:
sys.exit(0)
if sys.argv[1] == 'dns-forwarder':
@@ -194,6 +220,40 @@ if sys.argv[1] == 'dns-forwarder':
os.kill(os.getpid(), signal.SIGKILL)
while True:
time.sleep(.01)
if sys.argv[1] == 'qualify':
assert sys.argv[2:] == ['--config', '/operator/resources.json', '--strategies', '/operator/strategies.json', '--isolated']
assert os.getsid(0) == os.getpid(), 'qualifier lacks owned session'
assert 'SS_PASSWORD' not in os.environ
events = (root / 'events').read_text().splitlines()
assert 'start' not in events and 'stop' not in events
assert events[-1] == 'reconcile', events
log('qualify')
assert increment('qualification_runs') == 1, 'qualification restarted'
def acknowledge(code):
ack = root / 'completion-ack'
ack.write_text(json.dumps({'pid': os.getpid(), 'code': code, 'cleanup_complete': True}))
ack.chmod(0o600)
if not scenario.endswith('-race'):
(root / 'qualified').touch()
if scenario in ('qualify-unsafe', 'qualify-unsafe-race'):
if not scenario.endswith('-race'):
(root / 'qualified').touch()
sys.exit(78)
if scenario in ('qualify-blocked', 'qualify-blocked-race'):
acknowledge(78)
sys.exit(78)
if scenario == 'qualify-cancel':
def restored(*_):
log('qualification-restored')
acknowledge(0)
sys.exit(0)
signal.signal(signal.SIGTERM, restored)
os.kill(os.getppid(), signal.SIGTERM)
while True:
time.sleep(.01)
log('qualification-restored')
acknowledge(0)
sys.exit(0)
command = sys.argv[2]
if command == 'init':
log('init')
@@ -304,6 +364,11 @@ def run(scenario, expected, dns='', mode='native'):
text = text.replace('config_dir=/run/zapret-autotune-ss', f'config_dir={configs}')
text = text.replace('/usr/local/bin/zapret-autotune', str(binary / 'autotune'))
text = text.replace('/opt/zapret2/init.d/sysv/zapret2', str(binary / 'zapret2'))
if scenario.endswith('-race'):
barrier = f'{binary / "autotune"} qualification-race-barrier "$monitor_pid"\n'
loop = 'while dns_alive && kill -0 "$server_pid" && kill -0 "$local_pid" && kill -0 "$monitor_pid"; do'
assert loop in text
text = text.replace(loop, barrier + loop, 1)
if realtime:
# Private fixture scaling only: production has no configurable grace.
text = text.replace('$(date +%s) + 80', '$(date +%s) + 12')
@@ -317,6 +382,12 @@ def run(scenario, expected, dns='', mode='native'):
env['ZAPRET_AUTOTUNE_RESOURCE_CONFIG'] = '/operator/resources.json'
else:
env.pop('ZAPRET_AUTOTUNE_RESOURCE_CONFIG', None)
env.pop('ZAPRET_AUTOTUNE_QUALIFY_CONFIG', None)
env.pop('ZAPRET_AUTOTUNE_STRATEGY_CONFIG', None)
if scenario.startswith('qualify-'):
env['ZAPRET_AUTOTUNE_QUALIFY_CONFIG'] = '/operator/resources.json'
if scenario != 'qualify-missing':
env['ZAPRET_AUTOTUNE_STRATEGY_CONFIG'] = '/operator/strategies.json'
if mode is None:
env.pop('ZAPRET_AUTOTUNE_DNS_MODE', None)
else:
@@ -432,6 +503,25 @@ for scenario in ('dns-term', 'dns-int'):
events = run(scenario, 0, mode='doh')
assert events == ['dns-start', 'dns-stop'], events
for scenario in ('qualify-pass', 'qualify-negative', 'qualify-blocked', 'qualify-cancel'):
events = run(scenario, 0, mode='doh')
assert events.count('qualify') == 1 and not any(e.startswith('monitor ') for e in events), events
assert 'start' not in events and 'stop' not in events, events
assert events.index('dns-ready') < events.index('qualify') < events.index('dns-stop'), events
if scenario != 'qualify-blocked':
assert events.index('qualification-restored') < events.index('dns-stop'), events
events = run('qualify-missing', 64, mode='doh')
assert not events, events
for scenario, code in (('qualify-blocked-race', 0), ('qualify-unsafe-race', 78), ('qualify-unsafe', 78)):
events = run(scenario, code, mode='doh')
assert events.count('qualify') == 1 and 'start' not in events and 'stop' not in events, events
if scenario.endswith('-race'):
assert events.count('term-before-parent-reap') == 1, events
if code == 0:
assert events.index('term-before-parent-reap') < events.index('completion-checked') < events.index('dns-stop'), events
else:
assert 'completion-checked' not in events, events
def check_watchdog_traps(shell):
"""Same bounded_wait from normal flow, plain EXIT, and TERM -> EXIT traps."""
+3
View File
@@ -19,6 +19,9 @@ services:
ZAPRET_AUTOTUNE_DOH_TIMEOUT_SEC: ${ZAPRET_AUTOTUNE_DOH_TIMEOUT_SEC-5}
ZAPRET_AUTOTUNE_YOUTUBE_SERVICE: ${ZAPRET_AUTOTUNE_YOUTUBE_SERVICE-0}
ZAPRET_AUTOTUNE_RESOURCE_CONFIG: ${ZAPRET_AUTOTUNE_RESOURCE_CONFIG-}
# INTRUSIVE isolated startup batch. Never route working traffic here.
ZAPRET_AUTOTUNE_QUALIFY_CONFIG: ${ZAPRET_AUTOTUNE_QUALIFY_CONFIG-}
ZAPRET_AUTOTUNE_STRATEGY_CONFIG: ${ZAPRET_AUTOTUNE_STRATEGY_CONFIG-}
ZAPRET_AUTOTUNE_GOOGLEVIDEO_MODE: ${ZAPRET_AUTOTUNE_GOOGLEVIDEO_MODE-cold}
ZAPRET_AUTOTUNE_VIDEO_ID: ${ZAPRET_AUTOTUNE_VIDEO_ID-dQw4w9WgXcQ}
ZAPRET_AUTOTUNE_GOOGLEVIDEO_VIDEO_IDS: '${ZAPRET_AUTOTUNE_GOOGLEVIDEO_VIDEO_IDS:-}'
+40 -3
View File
@@ -18,6 +18,16 @@ dns_mode=$(/usr/local/bin/zapret-autotune dns-forwarder mode) || exit 64
dns_address=$(/usr/local/bin/zapret-autotune dns-forwarder address) || exit 64
generic_mode=0
[ -z "${ZAPRET_AUTOTUNE_RESOURCE_CONFIG-}" ] || generic_mode=1
qualification_mode=0
qualification_attempted=0
if [ -n "${ZAPRET_AUTOTUNE_QUALIFY_CONFIG-}${ZAPRET_AUTOTUNE_STRATEGY_CONFIG-}" ]; then
[ -n "${ZAPRET_AUTOTUNE_QUALIFY_CONFIG-}" ] && [ -n "${ZAPRET_AUTOTUNE_STRATEGY_CONFIG-}" ] || {
echo 'error: isolated qualification requires both config and strategy paths' >&2; exit 64;
}
qualification_mode=1
generic_mode=1
echo 'INTRUSIVE ISOLATED qualification target: do not route working traffic here' >&2
fi
case "$dns_mode" in doh|native) ;; *) exit 64 ;; esac
config_dir=/run/zapret-autotune-ss
awk '$2 == "/run/zapret-autotune-ss" && $3 == "tmpfs" { found=1 } END { exit !found }' /proc/mounts || {
@@ -196,9 +206,15 @@ cleanup_cycle() {
if [ -n "$monitor_pid" ]; then
capture_monitor_session || cleanup_code=78
kill -TERM "$monitor_pid" 2>/dev/null || true
bounded_wait "$monitor_pid" 45
if [ "$qualification_mode" -eq 1 ]; then bounded_wait "$monitor_pid" 75; else bounded_wait "$monitor_pid" 45; fi
# Cancellation must restore safely even when SS failure requested retry.
case "$waited_code" in 0|143) ;; *) echo "error: monitor cleanup status $waited_code" >&2; cleanup_code=78 ;; esac
if [ "$qualification_mode" -eq 1 ]; then
# Exit 78 is an outcome only with a producer-owned completed-cleanup
# ACK and a fresh baseline/worker/nft check, while SS+DNS still live.
cleanup_command 10 /usr/local/bin/zapret-autotune qualification-completion-check "$monitor_pid" "$waited_code" || cleanup_code=78
else
case "$waited_code" in 0|143) ;; *) echo "error: monitor cleanup status $waited_code" >&2; cleanup_code=78 ;; esac
fi
[ "$join_timed_out" -eq 0 ] || { echo 'error: monitor join timeout' >&2; cleanup_code=78; }
# SIGTERM may have arrived before the launch acknowledgement was read.
capture_monitor_session || cleanup_code=78
@@ -310,6 +326,7 @@ start_dns() {
done
}
run_cycle() {
[ "$qualification_attempted" -eq 0 ] || return 78
start_dns || return $?
set -- ss-server -c "$config_dir/server.json"
# DNS is not a secret. Native -d avoids assuming support for a JSON DNS key.
@@ -338,8 +355,10 @@ fi
# Never activate pending or unknown bytes. Any reconciliation failure is fatal,
# not a transient (75) restart request. Reload restores known bytes first.
timeout -k 5 45 /usr/local/bin/zapret-autotune managed reconcile || return 78
if [ "$qualification_mode" -eq 0 ]; then
zapret_started=1
timeout -k 5 30 /opt/zapret2/init.d/sysv/zapret2 start || return 78
fi
# Private operator request, consumed only after a successful guarded transaction.
# Never suppress qualification gates globally or race the owned monitor.
@@ -352,7 +371,13 @@ fi
# startup before its cancellation handler exists. Clean this cycle directly.
{ dns_alive && kill -0 "$server_pid" 2>/dev/null && kill -0 "$local_pid" 2>/dev/null; } || return 75
rm -f "$config_dir/monitor-ready"
setsid /bin/sh /opt/managed-target-supervision.sh --monitor "$config_dir" /usr/local/bin/zapret-autotune managed monitor 8<&- 9<&- &
if [ "$qualification_mode" -eq 1 ]; then
qualification_attempted=1
set -- qualify --config "$ZAPRET_AUTOTUNE_QUALIFY_CONFIG" --strategies "$ZAPRET_AUTOTUNE_STRATEGY_CONFIG" --isolated
else
set -- managed monitor
fi
setsid /bin/sh /opt/managed-target-supervision.sh --monitor "$config_dir" /usr/local/bin/zapret-autotune "$@" 8<&- 9<&- &
monitor_pid=$!
monitor_ack_deadline=$(($(date +%s) + 5))
while [ ! -e "$config_dir/monitor-ready" ]; do
@@ -372,6 +397,18 @@ done
if ! kill -0 "$monitor_pid" 2>/dev/null; then
code=0
wait "$monitor_pid" || code=$?
if [ "$qualification_mode" -eq 1 ]; then
if ! timeout -k 1 10 /usr/local/bin/zapret-autotune qualification-completion-check "$monitor_pid" "$code"; then
generic_cleanup_failed=1
fi
monitor_pid=
# One finite batch only, including negative/blocked completion. No live
# daemon and no automatic retest. Keep owned SS/DoH alive for inspection.
echo "isolated qualification finished (status $code); target safe-idle" >&2
cleanup_session || return 78
while dns_alive && kill -0 "$server_pid" && kill -0 "$local_pid"; do pause 1; done
return 78
fi
monitor_pid=
echo 'error: target managed monitor exited' >&2
case "$code" in