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 <gpt-6@openai.com>
This commit is contained in:
1 parent
a1a76bff76
commit
610916ce66
2 files changed
+88
-6
No files matched your search
@@ -4,7 +4,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -59,12 +58,13 @@ func qualificationEnginePath(root string) (string, os.FileInfo, error) {
|
|||||||
if e != nil {
|
if e != nil {
|
||||||
return "", nil, errResourcePlan
|
return "", nil, errResourcePlan
|
||||||
}
|
}
|
||||||
path := link
|
// Check the raw spelling before Join can erase symlink/.. traversal. Only
|
||||||
if !filepath.IsAbs(path) {
|
// the platform observed in the pinned qualification package is supported.
|
||||||
path = filepath.Join(filepath.Dir(alias), path)
|
if link != "../binaries/linux-x86_64/nfqws2" {
|
||||||
|
return "", nil, errResourcePlan
|
||||||
}
|
}
|
||||||
path = filepath.Clean(path)
|
path := filepath.Join(root, "binaries/linux-x86_64/nfqws2")
|
||||||
if !strings.HasPrefix(path, filepath.Join(root, "binaries")+"/") || filepath.Base(path) != "nfqws2" || qualificationParents(path, boundary) != nil {
|
if qualificationParents(path, boundary) != nil {
|
||||||
return "", nil, errResourcePlan
|
return "", nil, errResourcePlan
|
||||||
}
|
}
|
||||||
return path, info, nil
|
return path, info, nil
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"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) {
|
func TestQualificationPackagedAliasProcExe(t *testing.T) {
|
||||||
root, path, alias := qualificationBinaryFixture(t)
|
root, path, alias := qualificationBinaryFixture(t)
|
||||||
bytes, e := os.ReadFile("/bin/sleep")
|
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 {
|
if b.running(fmt.Sprintf("/proc/%d/exe", cmd.Process.Pid)) != nil {
|
||||||
t.Fatal("real packaged worker inode rejected")
|
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 {
|
if b.running("/proc/self/exe") == nil {
|
||||||
t.Fatal("foreign running executable accepted")
|
t.Fatal("foreign running executable accepted")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user