fix: attest trusted packaged qualification engine alias

Resolve only the fixed nfq2 package alias into trusted binaries parents, hold the no-follow engine descriptor and verify inode/hash against the running worker before nft activation. Keep Lua/import guards and completed-proof publication unchanged.

Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
ZeusinaandGPT-6 Bot committed 2026-10-14 13:57:58 +03:00
1 parent 573da3f797
commit a1a76bff76
3 files changed
+371 -7

No files matched your search

+55 -7
View File
@@ -97,10 +97,22 @@ func qualificationMode(config, strategies string, d managedDeps) error {
}
func qualificationFilesHash() (string, error) {
engine, e := openQualificationEngine("/opt/zapret2")
if e != nil {
return "", e
}
defer engine.file.Close()
return engine.filesHash()
}
func (engine *qualificationBinary) filesHash() (string, error) {
// Fixed image files only. Bind both executable and Lua inputs before and after
// every candidate; never accept a caller-supplied native hash/version label.
hashes := []string{}
for _, path := range []string{qualificationEngine, "/opt/zapret2/lua/zapret-lib.lua", "/opt/zapret2/lua/zapret-antidpi.lua"} {
if engine.validate() != nil {
return "", errResourcePlan
}
hashes := []string{engine.hash}
for _, path := range []string{filepath.Join(engine.root, "lua/zapret-lib.lua"), filepath.Join(engine.root, "lua/zapret-antidpi.lua")} {
fd, e := syscall.Open(path, syscall.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK|syscall.O_CLOEXEC, 0)
if e != nil {
return "", errResourcePlan
@@ -187,11 +199,47 @@ type resourceQualifier struct {
guard func() error
now func() time.Time
filesHash func() (string, error)
close func()
}
func newResourceQualifier(config, strategies string, d managedDeps, s *resourcePlanStore) resourceQualifier {
r := newManagedAssetRuntime(d)
q := resourceQualifier{runtime: r, checks: productionResourceCheckDeps(), now: time.Now, filesHash: qualificationFilesHash}
q := resourceQualifier{runtime: r, checks: productionResourceCheckDeps(), now: time.Now}
var engine *qualificationBinary
q.close = func() {
if engine != nil {
engine.file.Close()
}
}
q.filesHash = func() (string, error) {
if engine == nil {
var e error
engine, e = openQualificationEngine("/opt/zapret2")
if e != nil {
return "", e
}
}
return engine.filesHash()
}
start, queue := r.start, r.queue
r.start = func(args []string) (managedAssetWorker, error) {
if engine == nil || engine.validate() != nil {
return managedAssetWorker{}, errResourcePlan
}
return start(args)
}
r.queue = func(pid int) (bool, error) {
if engine == nil || engine.validate() != nil {
return false, errResourcePlan
}
ready, e := queue(pid)
// withOwnedWorker checks READY before creating the nft table.
if e == nil && ready && pid != 0 && engine.running(fmt.Sprintf("/proc/%d/exe", pid)) != nil {
return false, errResourcePlan
}
return ready, e
}
q.runtime = r
q.source = func() (resourceMonitorSource, error) { return readResourceMonitorSource(config, d.lookup, s.files.key) }
q.guard = func() error {
if qualificationMode(config, strategies, d) != nil || s.guard() != nil {
@@ -213,8 +261,7 @@ func newResourceQualifier(config, strategies string, d managedDeps, s *resourceP
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 {
if engine == nil || engine.validate() != nil || engine.running(fmt.Sprintf("/proc/%d/exe", pid)) != nil {
return qualificationAttestation{}, errResourcePlan
}
cmd, e := os.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid))
@@ -241,14 +288,14 @@ func newResourceQualifier(config, strategies string, d managedDeps, s *resourceP
if e != nil || !ready {
return qualificationAttestation{}, errResourcePlan
}
hash, e := qualificationFilesHash()
hash, e := q.filesHash()
if e != nil {
return qualificationAttestation{}, e
}
versionCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
var output bytes.Buffer
version := exec.CommandContext(versionCtx, qualificationEngine, "--version")
version := exec.CommandContext(versionCtx, engine.path, "--version")
version.Stdout = &playerBoundedWriter{dst: &output, limit: 4096}
version.Stderr = io.Discard
if version.Run() != nil {
@@ -535,6 +582,7 @@ func qualifyResourceCLI(config, strategies string, stdout, stderr io.Writer, d m
ctx, cancel := context.WithTimeout(signals, 15*time.Minute)
defer cancel()
q := newResourceQualifier(config, strategies, d, s)
defer q.close()
var sessionErr error
defer func() {
if code != 0 && code != 78 {
@@ -0,0 +1,133 @@
package main
import (
"io"
"os"
"path/filepath"
"strings"
"syscall"
)
// Only the packaged nfq2 alias is followed. All target parents and the final
// binary remain no-symlink, root-owned and not group/other writable. As with
// the runtime contract, a malicious root racing checks is outside the boundary.
type qualificationBinary struct {
root, path string
file *os.File
info, alias os.FileInfo
hash string
}
func qualificationTrusted(info os.FileInfo) bool {
st, ok := info.Sys().(*syscall.Stat_t)
return ok && st.Uid == 0 && info.Mode().Perm()&0022 == 0
}
func qualificationParents(path, boundary string) error {
for dir := filepath.Dir(path); ; dir = filepath.Dir(dir) {
info, e := os.Lstat(dir)
if e != nil || !info.IsDir() || !qualificationTrusted(info) {
return errResourcePlan
}
if dir == boundary {
return nil
}
if dir == "/" {
return errResourcePlan
}
}
}
func qualificationEnginePath(root string) (string, os.FileInfo, error) {
alias := filepath.Join(root, "nfq2/nfqws2")
boundary := root // Private fixture seam; production also checks /opt and /.
if root == "/opt/zapret2" {
boundary = "/"
}
if qualificationParents(alias, boundary) != nil {
return "", nil, errResourcePlan
}
info, e := os.Lstat(alias)
if e != nil || info.Mode()&os.ModeSymlink == 0 {
return "", nil, errResourcePlan
}
st, ok := info.Sys().(*syscall.Stat_t)
if !ok || st.Uid != 0 {
return "", nil, errResourcePlan
}
link, e := os.Readlink(alias)
if e != nil {
return "", nil, errResourcePlan
}
path := link
if !filepath.IsAbs(path) {
path = filepath.Join(filepath.Dir(alias), path)
}
path = filepath.Clean(path)
if !strings.HasPrefix(path, filepath.Join(root, "binaries")+"/") || filepath.Base(path) != "nfqws2" || qualificationParents(path, boundary) != nil {
return "", nil, errResourcePlan
}
return path, info, nil
}
func qualificationBinaryHash(f *os.File) (os.FileInfo, string, error) {
info, e := f.Stat()
if e != nil || !info.Mode().IsRegular() || !qualificationTrusted(info) || info.Mode().Perm()&0111 == 0 || info.Size() > 32<<20 {
return nil, "", errResourcePlan
}
b, e := io.ReadAll(io.NewSectionReader(f, 0, 32<<20+1))
if e != nil || int64(len(b)) != info.Size() {
return nil, "", errResourcePlan
}
return info, planDigest(b), nil
}
func openQualificationEngine(root string) (*qualificationBinary, error) {
path, alias, e := qualificationEnginePath(root)
if e != nil {
return nil, e
}
fd, e := syscall.Open(path, syscall.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK|syscall.O_CLOEXEC, 0)
if e != nil {
return nil, errResourcePlan
}
f := os.NewFile(uintptr(fd), path)
info, hash, e := qualificationBinaryHash(f)
b := &qualificationBinary{root: root, path: path, alias: alias, file: f, info: info, hash: hash}
if e != nil || b.validate() != nil {
f.Close()
return nil, errResourcePlan
}
return b, nil
}
func (b *qualificationBinary) validate() error {
path, alias, e := qualificationEnginePath(b.root)
if e != nil || path != b.path || !os.SameFile(alias, b.alias) {
return errResourcePlan
}
current, e := os.Lstat(path)
if e != nil || !current.Mode().IsRegular() || !os.SameFile(current, b.info) || !qualificationTrusted(current) {
return errResourcePlan
}
info, hash, e := qualificationBinaryHash(b.file)
if e != nil || !os.SameFile(info, b.info) || hash != b.hash {
return errResourcePlan
}
return nil
}
func (b *qualificationBinary) running(procExe string) error {
// /proc/PID/exe is a kernel-owned link, not a catalogue path. Compare the
// opened running inode and bytes, never the alias spelling from readlink.
f, e := os.Open(procExe)
if e != nil {
return errResourcePlan
}
defer f.Close()
info, hash, e := qualificationBinaryHash(f)
if e != nil || !os.SameFile(info, b.info) || hash != b.hash {
return errResourcePlan
}
return nil
}
@@ -0,0 +1,183 @@
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
)
func qualificationBinaryFixture(t *testing.T) (string, string, string) {
t.Helper()
if os.Geteuid() != 0 {
t.Skip("root-owned package fixture requires root; run with sudo")
}
root := t.TempDir()
for _, dir := range []string{"nfq2", "binaries/linux-x86_64", "lua"} {
if e := os.MkdirAll(filepath.Join(root, dir), 0755); e != nil {
t.Fatal(e)
}
}
path := filepath.Join(root, "binaries/linux-x86_64/nfqws2")
for _, name := range []string{path, filepath.Join(root, "lua/zapret-lib.lua"), filepath.Join(root, "lua/zapret-antidpi.lua")} {
if e := os.WriteFile(name, []byte("fixed package bytes"), 0755); e != nil {
t.Fatal(e)
}
}
alias := filepath.Join(root, "nfq2/nfqws2")
if e := os.Symlink("../binaries/linux-x86_64/nfqws2", alias); e != nil {
t.Fatal(e)
}
return root, path, alias
}
func TestQualificationPackagedAlias(t *testing.T) {
root, path, _ := qualificationBinaryFixture(t)
b, e := openQualificationEngine(root)
if e != nil {
t.Fatal(e)
}
defer b.file.Close()
want := planDigest([]string{planDigest([]byte("fixed package bytes")), planDigest([]byte("fixed package bytes")), planDigest([]byte("fixed package bytes"))})
if hash, e := b.filesHash(); e != nil || hash != want || b.path != path {
t.Fatal("packaged alias hash", hash, e)
}
proc := filepath.Join(root, "proc-exe")
if e := os.Symlink(path, proc); e != nil {
t.Fatal(e)
}
if b.running(proc) != nil {
t.Fatal("canonical running inode rejected")
}
other := filepath.Join(root, "other")
if e := os.WriteFile(other, []byte("fixed package bytes"), 0755); e != nil {
t.Fatal(e)
}
if b.running(other) == nil {
t.Fatal("same hash, different running inode accepted")
}
}
func TestQualificationPackagedAliasRejects(t *testing.T) {
for _, mode := range []string{"escape", "final-symlink", "writable", "untrusted", "parent-writable", "parent-symlink", "missing"} {
t.Run(mode, func(t *testing.T) {
root, path, alias := qualificationBinaryFixture(t)
var e error
switch mode {
case "escape":
e = os.Remove(alias)
if e == nil {
e = os.Symlink("/bin/nfqws2", alias)
}
case "final-symlink":
e = os.Remove(path)
if e == nil {
e = os.Symlink("/bin/true", path)
}
case "writable":
e = os.Chmod(path, 0775)
case "untrusted":
e = os.Chown(path, 1000, 1000)
case "parent-writable":
e = os.Chmod(filepath.Dir(path), 0777)
case "parent-symlink":
dir := filepath.Dir(path)
e = os.Rename(dir, dir+"-real")
if e == nil {
e = os.Symlink(dir+"-real", dir)
}
case "missing":
e = os.Remove(path)
}
if e != nil {
t.Fatal(e)
}
if b, e := openQualificationEngine(root); e == nil {
b.file.Close()
t.Fatal("unsafe package accepted")
}
})
}
}
func TestQualificationPackagedAliasProcExe(t *testing.T) {
root, path, alias := qualificationBinaryFixture(t)
bytes, e := os.ReadFile("/bin/sleep")
if e != nil {
t.Fatal(e)
}
if e := os.WriteFile(path, bytes, 0755); e != nil {
t.Fatal(e)
}
b, e := openQualificationEngine(root)
if e != nil {
t.Fatal(e)
}
defer b.file.Close()
cmd := exec.Command(alias, "60")
if e := cmd.Start(); e != nil {
t.Fatal(e)
}
defer func() { cmd.Process.Kill(); cmd.Wait() }()
if b.running(fmt.Sprintf("/proc/%d/exe", cmd.Process.Pid)) != nil {
t.Fatal("real packaged worker inode rejected")
}
if b.running("/proc/self/exe") == nil {
t.Fatal("foreign running executable accepted")
}
}
func TestQualificationPackagedAliasMutationBeforeStart(t *testing.T) {
for _, mode := range []string{"resolution", "alias-replaced", "binary-replaced", "bytes", "lua-symlink", "lua-writable"} {
t.Run(mode, func(t *testing.T) {
root, path, alias := qualificationBinaryFixture(t)
b, e := openQualificationEngine(root)
if e != nil {
t.Fatal(e)
}
defer b.file.Close()
if _, e := b.filesHash(); e != nil {
t.Fatal(e)
}
switch mode {
case "resolution", "alias-replaced":
link := "../binaries/linux-x86_64/nfqws2"
if mode == "resolution" {
link = "../binaries/other/nfqws2"
if e := os.Mkdir(filepath.Join(root, "binaries/other"), 0755); e != nil {
t.Fatal(e)
}
if e := os.WriteFile(filepath.Join(root, "binaries/other/nfqws2"), []byte("fixed package bytes"), 0755); e != nil {
t.Fatal(e)
}
}
e = os.Rename(alias, alias+"-old")
if e == nil {
e = os.Symlink(link, alias)
}
case "binary-replaced":
e = os.Rename(path, path+"-old")
if e == nil {
e = os.WriteFile(path, []byte("fixed package bytes"), 0755)
}
case "bytes":
e = os.WriteFile(path, []byte("mutated package"), 0755)
case "lua-symlink":
lua := filepath.Join(root, "lua/zapret-lib.lua")
e = os.Remove(lua)
if e == nil {
e = os.Symlink(path, lua)
}
case "lua-writable":
e = os.Chmod(filepath.Join(root, "lua/zapret-lib.lua"), 0666)
}
if e != nil {
t.Fatal(e)
}
if _, e := b.filesHash(); e == nil {
t.Fatal("changed package accepted before start")
}
})
}
}