Report fixed procfs scan reasons, typed errno and numeric worker identities without exposing paths or errors. Preserve scan metadata through attestation and restore normalization while retaining fail-closed decisions and cleanup classification. Co-authored-by: GPT-6 <gpt-6@openai.com>
169 lines
6.5 KiB
Go
169 lines
6.5 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
)
|
|
|
|
func TestQualificationWorkerScanBranches(t *testing.T) {
|
|
secret := errors.New("PRIVATE path argv propertyID sentinel")
|
|
dir := t.TempDir()
|
|
for _, name := range []string{"0-nonnumeric", "41"} {
|
|
if err := os.Mkdir(filepath.Join(dir, name), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
entries, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, tc := range []struct {
|
|
name string
|
|
err error
|
|
code qualificationErrno
|
|
}{
|
|
{"eacces", syscall.EACCES, errnoEACCES},
|
|
{"eperm", syscall.EPERM, errnoEPERM},
|
|
{"esrch", syscall.ESRCH, errnoESRCH},
|
|
{"enoent", syscall.ENOENT, errnoENOENT},
|
|
{"not_exist", os.ErrNotExist, errnoOther},
|
|
{"other", secret, errnoOther},
|
|
} {
|
|
for _, readDirFailure := range []bool{false, true} {
|
|
t.Run(fmt.Sprintf("%s/readdir=%t", tc.name, readDirFailure), func(t *testing.T) {
|
|
wrapped := &os.PathError{Op: "PRIVATE", Path: secret.Error(), Err: errors.Join(tc.err, secret)}
|
|
calls := 0
|
|
err := qualificationScanWorkers(42, func(path string) ([]os.DirEntry, error) {
|
|
if path != "/proc" {
|
|
t.Fatal(path)
|
|
}
|
|
if readDirFailure {
|
|
return nil, wrapped
|
|
}
|
|
return entries, nil
|
|
}, func(path string) (string, error) {
|
|
calls++
|
|
if path != "/proc/41/exe" {
|
|
t.Fatal("nonnumeric entry scanned", path)
|
|
}
|
|
return "", wrapped
|
|
})
|
|
if !readDirFailure && errors.Is(wrapped, os.ErrNotExist) {
|
|
if err != nil || calls != 1 {
|
|
t.Fatal("wrapped ENOENT must skip", err, calls)
|
|
}
|
|
return
|
|
}
|
|
var d *qualificationDiagnostic
|
|
if !errors.As(err, &d) || d.Errno != tc.code || d.OwnedPID != 42 {
|
|
t.Fatal("missing typed metadata", err)
|
|
}
|
|
if readDirFailure {
|
|
if d.Reason != reasonWorkerScanRead || d.ScanPID != 0 || calls != 0 || !errors.Is(err, wrapped) {
|
|
t.Fatal("readdir classification changed", d, calls)
|
|
}
|
|
} else if d.Reason != reasonWorkerScanReadlink || d.ScanPID != 41 || !errors.Is(err, errResourcePlan) || errors.Is(err, wrapped) {
|
|
t.Fatal("readlink classification changed", d)
|
|
}
|
|
assertWorkerScanOutput(t, err, stageUnknown, d.Reason, tc.code, d.ScanPID, 42)
|
|
})
|
|
}
|
|
}
|
|
for _, tc := range []struct {
|
|
name, exe string
|
|
owned int
|
|
deny bool
|
|
}{
|
|
{"owned", "/PRIVATE/nfqws2", 41, false},
|
|
{"foreign", "/PRIVATE/nfqws2", 42, true},
|
|
// A foreign --version invocation has the same exe: never inspect argv
|
|
// or exempt it from the exclusive-worker predicate.
|
|
{"foreign_version", "/PRIVATE/nfqws2", 0, true},
|
|
{"unrelated", "/PRIVATE/other", 42, false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
err := qualificationScanWorkers(tc.owned, func(string) ([]os.DirEntry, error) { return entries, nil }, func(string) (string, error) { return tc.exe, nil })
|
|
if !tc.deny {
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return
|
|
}
|
|
if !errors.Is(err, errResourcePlan) {
|
|
t.Fatal("foreign worker accepted", err)
|
|
}
|
|
assertWorkerScanOutput(t, err, stageUnknown, reasonWorkerScanForeign, errnoNone, 41, tc.owned)
|
|
})
|
|
}
|
|
}
|
|
|
|
func assertWorkerScanOutput(t *testing.T, err error, stage qualificationStage, reason qualificationReason, errno qualificationErrno, scan, owned int) {
|
|
t.Helper()
|
|
var out bytes.Buffer
|
|
writeQualificationDiagnostics(&out, err)
|
|
want := fmt.Sprintf(" errno=%s scan_pid=%d owned_pid=%d", errno, scan, owned)
|
|
if !strings.Contains(out.String(), "stage="+stage.String()+" reason="+reason.String()) || !strings.Contains(out.String(), want) || strings.Contains(out.String(), "PRIVATE") {
|
|
t.Fatal(out.String())
|
|
}
|
|
}
|
|
|
|
func TestQualificationWorkerScanNormalization(t *testing.T) {
|
|
secret := errors.New("PRIVATE path argv propertyID sentinel")
|
|
for _, reason := range []qualificationReason{reasonWorkerScanRead, reasonWorkerScanReadlink, reasonWorkerScanForeign} {
|
|
for _, errno := range []qualificationErrno{errnoNone, errnoEACCES, errnoEPERM, errnoESRCH, errnoENOENT, errnoOther} {
|
|
scan := &qualificationDiagnostic{Err: secret, Reason: reason, Errno: errno, ScanPID: 41, OwnedPID: 42}
|
|
attestation := qualificationPlanPredicate(qualificationOperation(scan, stageAttestation), reasonWorkerScan)
|
|
if !errors.Is(attestation, errResourcePlan) || errors.Is(attestation, secret) {
|
|
t.Fatal("attestation classification changed", attestation)
|
|
}
|
|
assertWorkerScanOutput(t, qualificationOperation(attestation, stagePostDNSGuard), stagePostDNSGuard, reason, errno, 41, 42)
|
|
q := resourceQualifier{guard: func() error { return nil }}
|
|
q.runtime.nft = func(context.Context, string) (string, error) {
|
|
t.Fatal("restore passed scan veto")
|
|
return "", nil
|
|
}
|
|
restore := q.restoreBaseline(context.Background(), func() error {
|
|
return qualificationPredicate(scan, qualificationErrorReason(scan, reasonWorkerScan))
|
|
}, func() error { t.Fatal("unsafe baseline restore"); return nil })
|
|
if !errors.Is(restore, errResourcePlan) || errors.Is(restore, secret) {
|
|
t.Fatal("restore classification changed", restore)
|
|
}
|
|
assertWorkerScanOutput(t, qualificationOperation(restore, stageRestore), stageRestore, reason, errno, 41, 42)
|
|
}
|
|
}
|
|
var out bytes.Buffer
|
|
writeQualificationDiagnostics(&out, qualificationPredicate(secret, reasonEngine))
|
|
if strings.Contains(out.String(), "errno=") || strings.Contains(out.String(), "_pid=") || strings.Contains(out.String(), "PRIVATE") {
|
|
t.Fatal("scan metadata on non-scan diagnostic", out.String())
|
|
}
|
|
}
|
|
|
|
func TestQualificationWorkerScanPostDNSVeto(t *testing.T) {
|
|
q, s, registry, _, transfers := qualifierFixture(t)
|
|
attest, calls := q.attest, 0
|
|
q.attest = func(ctx context.Context, pid int, args []string, d strategyDefinition) (qualificationAttestation, error) {
|
|
calls++
|
|
if calls == 3 {
|
|
scan := &qualificationDiagnostic{Err: errors.New("PRIVATE path argv propertyID sentinel"), Reason: reasonWorkerScanReadlink, Errno: errnoEPERM, ScanPID: 41, OwnedPID: pid}
|
|
return qualificationAttestation{}, qualificationPlanPredicate(scan, reasonWorkerScan)
|
|
}
|
|
return attest(ctx, pid, args, d)
|
|
}
|
|
proof, err := q.complete(context.Background(), s, registry)
|
|
if !errors.Is(err, errResourcePlan) || proof.seal != "" || *transfers != 0 {
|
|
t.Fatal("ownership veto or execution order changed", err, *transfers)
|
|
}
|
|
var out bytes.Buffer
|
|
writeQualificationDiagnostics(&out, err)
|
|
if strings.Count(out.String(), "stage=post_dns_guard reason=worker_scan_readlink candidate=1 resource=1 attempt=1 tls=tls12 errno=eperm scan_pid=41 owned_pid=") != 2 || strings.Contains(out.String(), "PRIVATE") {
|
|
t.Fatal(out.String())
|
|
}
|
|
}
|