From 610916ce66cc2f5254743205b3aa820a7ab524ee Mon Sep 17 00:00:00 2001 From: Zeusina Date: Wed, 14 Oct 2026 14:19:33 +0300 Subject: [PATCH] fix: reject concealed qualification alias traversal Validate the raw pinned package alias before path normalization so symlink/.. traversal cannot attest one executable and launch another. Preserve package ownership, held-file, hash, inode and Lua checks. Co-authored-by: GPT-6 --- .../resource_qualify_binary.go | 12 +-- .../resource_qualify_binary_test.go | 82 +++++++++++++++++++ 2 files changed, 88 insertions(+), 6 deletions(-) diff --git a/cmd/zapret-autotune/resource_qualify_binary.go b/cmd/zapret-autotune/resource_qualify_binary.go index 4795cc9..af3394c 100644 --- a/cmd/zapret-autotune/resource_qualify_binary.go +++ b/cmd/zapret-autotune/resource_qualify_binary.go @@ -4,7 +4,6 @@ import ( "io" "os" "path/filepath" - "strings" "syscall" ) @@ -59,12 +58,13 @@ func qualificationEnginePath(root string) (string, os.FileInfo, error) { if e != nil { return "", nil, errResourcePlan } - path := link - if !filepath.IsAbs(path) { - path = filepath.Join(filepath.Dir(alias), path) + // Check the raw spelling before Join can erase symlink/.. traversal. Only + // the platform observed in the pinned qualification package is supported. + if link != "../binaries/linux-x86_64/nfqws2" { + return "", nil, errResourcePlan } - path = filepath.Clean(path) - if !strings.HasPrefix(path, filepath.Join(root, "binaries")+"/") || filepath.Base(path) != "nfqws2" || qualificationParents(path, boundary) != nil { + path := filepath.Join(root, "binaries/linux-x86_64/nfqws2") + if qualificationParents(path, boundary) != nil { return "", nil, errResourcePlan } return path, info, nil diff --git a/cmd/zapret-autotune/resource_qualify_binary_test.go b/cmd/zapret-autotune/resource_qualify_binary_test.go index 0e7f532..d461f41 100644 --- a/cmd/zapret-autotune/resource_qualify_binary_test.go +++ b/cmd/zapret-autotune/resource_qualify_binary_test.go @@ -1,6 +1,8 @@ package main import ( + "context" + "errors" "fmt" "os" "os/exec" @@ -101,6 +103,83 @@ func TestQualificationPackagedAliasRejects(t *testing.T) { } } +func TestQualificationPackagedAliasRawSpelling(t *testing.T) { + for _, link := range []string{ + "../binaries/linux-x86_64/./nfqws2", + "../binaries//linux-x86_64/nfqws2", + "../binaries/linux-x86_64/../linux-x86_64/nfqws2", + "../binaries/unsupported/nfqws2", + "../binaries/linux-x86_64/nested/nfqws2", + } { + t.Run(link, func(t *testing.T) { + root, _, alias := qualificationBinaryFixture(t) + if e := os.Remove(alias); e != nil { + t.Fatal(e) + } + if e := os.Symlink(link, alias); e != nil { + t.Fatal(e) + } + if b, e := openQualificationEngine(root); e == nil { + b.file.Close() + t.Fatal("non-package spelling accepted") + } + }) + } +} + +func TestQualificationConcealedTraversalBeforeWorker(t *testing.T) { + root, _, alias := qualificationBinaryFixture(t) + attacker := t.TempDir() + marker := filepath.Join(attacker, "executed") + for _, dir := range []string{"subdir", "linux-x86_64"} { + if e := os.Mkdir(filepath.Join(attacker, dir), 0755); e != nil { + t.Fatal(e) + } + } + outside := filepath.Join(attacker, "linux-x86_64/nfqws2") + if e := os.WriteFile(outside, []byte(fmt.Sprintf("#!/bin/sh\n: > %q\n", marker)), 0755); e != nil { + t.Fatal(e) + } + if e := os.Symlink(filepath.Join(attacker, "subdir"), filepath.Join(root, "binaries/redirect")); e != nil { + t.Fatal(e) + } + if e := os.Remove(alias); e != nil { + t.Fatal(e) + } + if e := os.Symlink("../binaries/redirect/../linux-x86_64/nfqws2", alias); e != nil { + t.Fatal(e) + } + resolved, e := os.Stat(alias) + outsideInfo, outsideErr := os.Stat(outside) + if e != nil || outsideErr != nil || !os.SameFile(resolved, outsideInfo) { + t.Fatal("fixture must resolve outside package", e, outsideErr) + } + q, s, registry, events, calls := qualifierFixture(t) + q.filesHash = func() (string, error) { + b, e := openQualificationEngine(root) + if e != nil { + return "", e + } + defer b.file.Close() + return b.filesHash() + } + starts := 0 + q.runtime.start = func([]string) (managedAssetWorker, error) { + starts++ + if e := exec.Command(alias).Run(); e != nil { + t.Fatal(e) + } + return managedAssetWorker{}, errResourcePlan + } + proof, e := q.complete(context.Background(), s, registry) + if !errors.Is(e, errResourcePlan) || starts != 0 || *calls != 0 || len(*events) != 0 || proof.seal != "" { + t.Fatalf("traversal rejection too late: err=%v starts=%d calls=%d events=%v", e, starts, *calls, *events) + } + if _, e := os.Stat(marker); !errors.Is(e, os.ErrNotExist) { + t.Fatal("outside executable ran", e) + } +} + func TestQualificationPackagedAliasProcExe(t *testing.T) { root, path, alias := qualificationBinaryFixture(t) bytes, e := os.ReadFile("/bin/sleep") @@ -123,6 +202,9 @@ func TestQualificationPackagedAliasProcExe(t *testing.T) { if b.running(fmt.Sprintf("/proc/%d/exe", cmd.Process.Pid)) != nil { t.Fatal("real packaged worker inode rejected") } + if _, e := b.filesHash(); e != nil { + t.Fatal("valid pinned alias changed after start", e) + } if b.running("/proc/self/exe") == nil { t.Fatal("foreign running executable accepted") }