From eb52c3de503ddefe64c53d954a8102dabc1bdceb Mon Sep 17 00:00:00 2001 From: Franz Hemmer <8227352+HemSoft@users.noreply.github.com> Date: Wed, 21 Oct 2026 10:21:54 -0400 Subject: [PATCH 01/12] Checkpoint kernel mount observation and fixed launch layout checks --- reviewer/git_mount_boundary_linux.go | 250 ++++++++++++++++++++++ reviewer/git_mount_boundary_linux_test.go | 155 ++++++++++++++ 2 files changed, 405 insertions(+) create mode 100644 reviewer/git_mount_boundary_linux.go create mode 100644 reviewer/git_mount_boundary_linux_test.go diff --git a/reviewer/git_mount_boundary_linux.go b/reviewer/git_mount_boundary_linux.go new file mode 100644 index 00000000..dd32122b --- /dev/null +++ b/reviewer/git_mount_boundary_linux.go @@ -0,0 +1,250 @@ +//go:build linux + +package reviewer + +import ( + "context" + "fmt" + "io" + "os" + "path/filepath" + "strconv" + "strings" + "time" + + "golang.org/x/sys/unix" +) + +const gitMountInfoBytes = 64 << 10 + +type gitMountRow struct { + ID, Parent uint64 + Device uint64 + Root, Point string + Filesystem string + Options, Super map[string]bool +} + +func gitMountPath(raw string) (string, error) { + var result strings.Builder + for i := 0; i < len(raw); i++ { + if raw[i] != '\\' { + result.WriteByte(raw[i]) + continue + } + if i+3 >= len(raw) { + return "", errGitUnit + } + switch raw[i+1 : i+4] { + case "040": + result.WriteByte(' ') + case "134": + result.WriteByte('\\') + default: + // Control-character pathnames are outside the fixed launch layout. + return "", errGitUnit + } + i += 3 + } + value := result.String() + if len(value) > 4096 || !filepath.IsAbs(value) || filepath.Clean(value) != value || strings.ContainsAny(value, "\x00\r\n\t") { + return "", errGitUnit + } + return value, nil +} + +func gitMountOptions(raw string) (map[string]bool, error) { + options := map[string]bool{} + for _, part := range strings.Split(raw, ",") { + if part == "" || len(part) > 4096 || options[part] { + return nil, errGitUnit + } + options[part] = true + } + if options["ro"] == options["rw"] { + return nil, errGitUnit + } + return options, nil +} + +// Kernel mountinfo only. Parsing never confers admission or source authority. +func parseGitMountTable(raw []byte) (map[string]gitMountRow, error) { + if len(raw) == 0 || len(raw) > gitMountInfoBytes || raw[len(raw)-1] != '\n' || strings.ContainsAny(string(raw), "\x00\r\t") { + return nil, errGitUnit + } + rows := map[string]gitMountRow{} + ids := map[uint64]bool{} + for _, line := range strings.Split(strings.TrimSuffix(string(raw), "\n"), "\n") { + left, right, found := strings.Cut(line, " - ") + a, b := strings.Fields(left), strings.Fields(right) + if !found || len(a) < 6 || len(a) > 7 || len(b) != 3 || len(rows) >= 32 || len(a) == 7 && a[6] != "unbindable" { + return nil, errGitUnit + } + id, e1 := strconv.ParseUint(a[0], 10, 64) + parent, e2 := strconv.ParseUint(a[1], 10, 64) + device := strings.Split(a[2], ":") + if e1 != nil || e2 != nil || id == 0 || parent == 0 || ids[id] || len(device) != 2 { + return nil, errGitUnit + } + major, e1 := strconv.ParseUint(device[0], 10, 32) + minor, e2 := strconv.ParseUint(device[1], 10, 32) + root, e3 := gitMountPath(a[3]) + point, e4 := gitMountPath(a[4]) + options, e5 := gitMountOptions(a[5]) + super, e6 := gitMountOptions(b[2]) + if e1 != nil || e2 != nil || e3 != nil || e4 != nil || e5 != nil || e6 != nil || rows[point].ID != 0 || b[0] == "" { + return nil, errGitUnit + } + rows[point] = gitMountRow{id, parent, unix.Mkdev(uint32(major), uint32(minor)), root, point, b[0], options, super} + ids[id] = true + } + return rows, nil +} + +func validateGitMountLayout(rows map[string]gitMountRow, binding gitMaterializationBinding) error { + if !binding.valid() || len(rows) != 7 || rows["/"].ID == 0 { + return errGitUnit + } + root := rows["/"] + wanted := map[string]bool{"/": true, "/broker": true, "/work": true, "/tmp": true, "/proc": true, "/dev/null": true, "/dev/urandom": true} + for point, row := range rows { + if !wanted[point] || point != "/" && row.Parent != root.ID { + return errGitUnit + } + if point == "/" || point == "/broker" || point == "/proc" || point == "/dev/urandom" { + if !row.Options["ro"] { + return errGitUnit + } + } + } + if rows["/proc"].Filesystem != "proc" || rows["/proc"].Root != "/" || !rows["/dev/null"].Options["rw"] { + return errGitUnit + } + for _, point := range []string{"/work", "/tmp"} { + row := rows[point] + if row.Filesystem != "tmpfs" || row.Root != "/git-work-"+binding.Attempt || !row.Options["rw"] || !row.Options["nosuid"] || !row.Options["nodev"] || !row.Options["noexec"] || !row.Super["noswap"] { + return errGitUnit + } + } + if rows["/work"].Device != rows["/tmp"].Device { + return errGitUnit + } + return nil +} + +func gitReadMountInfo(ctx context.Context, identity GateIdentity, pidfd int) ([]byte, error) { + if ctx == nil || ctx.Err() != nil || gitCheckProcessPIDFD(identity, pidfd) != nil { + return nil, errGitUnit + } + file, err := os.Open(fmt.Sprintf("/proc/%d/mountinfo", identity.PID)) + if err != nil { + return nil, errGitUnit + } + data, readErr := io.ReadAll(io.LimitReader(file, gitMountInfoBytes+1)) + closeErr := file.Close() + if readErr != nil || closeErr != nil || len(data) > gitMountInfoBytes || ctx.Err() != nil || gitCheckProcessPIDFD(identity, pidfd) != nil { + return nil, errGitUnit + } + return data, nil +} + +func gitBoundedWorkFilesystem(fs unix.Statfs_t) bool { + const size = uint64(268435456) + flags := int64(unix.ST_NOSUID | unix.ST_NODEV | unix.ST_NOEXEC) + return fs.Type == unix.TMPFS_MAGIC && fs.Bsize > 0 && size%uint64(fs.Bsize) == 0 && fs.Blocks == size/uint64(fs.Bsize) && fs.Files == 65536 && fs.Flags&flags == flags && fs.Flags&unix.ST_RDONLY == 0 +} + +// This proves only the mount/device subset for an already retained process +// boundary. The factory must still bind the exact unit, peer, descriptors and +// installed filter, and must never turn this private result into a caller flag. +func checkGitMountBoundary(ctx context.Context, b *gitProcessBoundary, broker *os.File, binding gitMaterializationBinding) error { + if ctx == nil || b == nil || broker == nil || !binding.valid() { + return errGitUnit + } + ctx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + b.mu.Lock() + defer b.mu.Unlock() + if b.checkWorker(ctx) != nil || verifyGitBrokerDirectory(broker) != nil { + return errGitUnit + } + raw, err := gitReadMountInfo(ctx, b.worker, b.pidfd) + if err != nil { + return errGitUnit + } + rows, err := parseGitMountTable(raw) + if err != nil || validateGitMountLayout(rows, binding) != nil { + return errGitUnit + } + procRoot := fmt.Sprintf("/proc/%d/root", b.worker.PID) + view, err := os.OpenRoot(procRoot) + if err != nil { + return errGitUnit + } + defer view.Close() + ids := map[string]gitKernelFileID{} + for _, point := range []string{"/", "/broker", "/work", "/tmp", "/proc", "/dev/null", "/dev/urandom"} { + name := strings.TrimPrefix(point, "/") + if name == "" { + name = "." + } + file, err := view.OpenFile(name, unix.O_PATH|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0) + if err != nil { + return errGitUnit + } + var stat unix.Stat_t + statErr := unix.Fstat(int(file.Fd()), &stat) + id, idErr := gitStatFileID(file) + file.Close() + if statErr != nil || idErr != nil || uint64(stat.Dev) != rows[point].Device { + return errGitUnit + } + ids[point] = id + if point == "/dev/null" || point == "/dev/urandom" { + minor := uint32(3) + if point == "/dev/urandom" { + minor = 9 + } + if stat.Mode&unix.S_IFMT != unix.S_IFCHR || stat.Uid != 0 || stat.Rdev != unix.Mkdev(1, minor) { + return errGitUnit + } + } else if stat.Mode&unix.S_IFMT != unix.S_IFDIR { + return errGitUnit + } + if point == "/work" || point == "/tmp" { + if stat.Uid != uint32(os.Geteuid()) || stat.Mode&0777 != 0700 { + return errGitUnit + } + file, err := view.OpenFile(name, os.O_RDONLY|unix.O_DIRECTORY|unix.O_NOFOLLOW|unix.O_NONBLOCK, 0) + if err != nil { + return errGitUnit + } + var fs unix.Statfs_t + fsErr := unix.Fstatfs(int(file.Fd()), &fs) + file.Close() + if fsErr != nil || !gitBoundedWorkFilesystem(fs) { + return errGitUnit + } + } + } + rootID, rootErr := gitStatFileID(b.root) + brokerID, brokerErr := gitStatFileID(broker) + if rootErr != nil || brokerErr != nil || ids["/"] != rootID || ids["/broker"] != brokerID || ids["/work"] != ids["/tmp"] { + return errGitUnit + } + pidNamespace, err := os.Open(procRoot + "/proc/1/ns/pid") + if err != nil { + return errGitUnit + } + id, idErr := gitStatFileID(pidNamespace) + kind, kindErr := unix.IoctlRetInt(int(pidNamespace.Fd()), unix.NS_GET_NSTYPE) + pidNamespace.Close() + if idErr != nil || kindErr != nil || kind != unix.CLONE_NEWPID || gitNamespaceIdentity(id) != b.namespaces["pid"] { + return errGitUnit + } + after, err := gitReadMountInfo(ctx, b.worker, b.pidfd) + if err != nil || string(after) != string(raw) || verifyGitBrokerDirectory(broker) != nil || b.checkWorker(ctx) != nil { + return errGitUnit + } + return nil +} diff --git a/reviewer/git_mount_boundary_linux_test.go b/reviewer/git_mount_boundary_linux_test.go new file mode 100644 index 00000000..728e110d --- /dev/null +++ b/reviewer/git_mount_boundary_linux_test.go @@ -0,0 +1,155 @@ +//go:build linux + +package reviewer + +import ( + "context" + "fmt" + "os" + "os/exec" + "strings" + "testing" + + "golang.org/x/sys/unix" +) + +// Synthetic kernel-format data tests parsing/policy only, never qualification. +func gitMountTableFixture(t *testing.T) (gitMaterializationBinding, string) { + t.Helper() + binding := gitMaterializationBinding{strings.Repeat("a", 32), Digest("release"), Digest("runtime"), testJob().Snapshot} + work := "/git-work-" + binding.Attempt + raw := "100 99 8:1 /private/runtime / ro,relatime - ext4 /dev/root rw\n" + + "101 100 8:1 /private/broker /broker ro,relatime - ext4 /dev/root rw\n" + + "102 100 0:42 " + work + " /work rw,nosuid,nodev,noexec - tmpfs tmpfs rw,noswap,size=262144k,nr_inodes=65536\n" + + "103 100 0:42 " + work + " /tmp rw,nosuid,nodev,noexec - tmpfs tmpfs rw,noswap,size=262144k,nr_inodes=65536\n" + + "104 100 0:43 / /proc ro,nosuid,nodev,noexec - proc proc rw\n" + + "105 100 0:5 /null /dev/null rw,nosuid,noexec - devtmpfs udev rw\n" + + "106 100 0:5 /urandom /dev/urandom ro,nosuid,noexec - devtmpfs udev rw\n" + return binding, raw +} + +func TestGitMountTableFixedLayoutAndRefusals(t *testing.T) { + binding, raw := gitMountTableFixture(t) + rows, err := parseGitMountTable([]byte(raw)) + if err != nil || len(rows) != 7 || validateGitMountLayout(rows, binding) != nil || rows["/work"].Device != unix.Mkdev(0, 42) { + t.Fatal("fixed mount policy", err) + } + for _, kind := range []string{"extra", "missing", "duplicate-point", "duplicate-id", "propagation", "root-writable", "broker-writable", "proc-writable", "wrong-proc", "random-writable", "wrong-parent", "different-alias", "different-device", "noexec", "nosuid", "nodev", "noswap", "fake-noswap", "duplicate-option", "conflicting-flags", "bad-escape", "trailing", "nul", "bounds"} { + t.Run(kind, func(t *testing.T) { + bad := raw + switch kind { + case "extra": + bad += "107 100 8:1 /home /home rw - ext4 /dev/root rw\n" + case "missing": + bad = strings.Split(raw, "106 ")[0] + case "duplicate-point": + bad = strings.Replace(raw, "/tmp rw,", "/work rw,", 1) + case "duplicate-id": + bad = strings.Replace(raw, "103 100", "102 100", 1) + case "propagation": + bad = strings.Replace(raw, "ro,relatime -", "ro,relatime shared:9 -", 1) + case "root-writable": + bad = strings.Replace(raw, "/ ro,relatime", "/ rw,relatime", 1) + case "broker-writable": + bad = strings.Replace(raw, "/broker ro,", "/broker rw,", 1) + case "proc-writable": + bad = strings.Replace(raw, "/proc ro,", "/proc rw,", 1) + case "wrong-proc": + bad = strings.Replace(raw, "- proc proc rw", "- ext4 /dev/root rw", 1) + case "random-writable": + bad = strings.Replace(raw, "/dev/urandom ro,", "/dev/urandom rw,", 1) + case "wrong-parent": + bad = strings.Replace(raw, "102 100", "102 101", 1) + case "different-alias": + bad = strings.Replace(raw, "103 100 0:42 /git-work-", "103 100 0:42 /other-", 1) + case "different-device": + bad = strings.Replace(raw, "103 100 0:42", "103 100 0:44", 1) + case "noexec", "nosuid", "nodev": + bad = strings.Replace(raw, ","+kind, "", 1) + case "noswap": + bad = strings.Replace(raw, ",noswap,", ",", 1) + case "fake-noswap": + bad = strings.Replace(raw, ",noswap,", ",noswap=1,", 1) + case "duplicate-option": + bad = strings.Replace(raw, "ro,relatime", "ro,ro,relatime", 1) + case "conflicting-flags": + bad = strings.Replace(raw, "ro,relatime", "ro,rw,relatime", 1) + case "bad-escape": + bad = strings.Replace(raw, "/private/runtime", `/private\057runtime`, 1) + case "trailing": + bad = strings.TrimSuffix(raw, "\n") + case "nul": + bad += "\x00" + case "bounds": + bad = strings.Repeat(raw, 256) + } + rows, err := parseGitMountTable([]byte(bad)) + if err == nil && validateGitMountLayout(rows, binding) == nil { + t.Fatal("unsafe or ambiguous mount layout accepted") + } + }) + } +} + +func TestGitMountWorkFilesystemIndependentBounds(t *testing.T) { + fs := unix.Statfs_t{Type: unix.TMPFS_MAGIC, Bsize: 4096, Blocks: 65536, Files: 65536, Flags: unix.ST_NOSUID | unix.ST_NODEV | unix.ST_NOEXEC} + if !gitBoundedWorkFilesystem(fs) { + t.Fatal("fixed scalar filesystem policy") + } + for _, kind := range []string{"filesystem", "zero-page", "capacity", "overflow", "inodes", "readonly", "exec", "devices", "suid"} { + bad := fs + switch kind { + case "filesystem": + bad.Type = unix.EXT4_SUPER_MAGIC + case "zero-page": + bad.Bsize = 0 + case "capacity": + bad.Blocks++ + case "overflow": + bad.Blocks += 1 << 52 + case "inodes": + bad.Files++ + case "readonly": + bad.Flags |= unix.ST_RDONLY + case "exec": + bad.Flags &^= unix.ST_NOEXEC + case "devices": + bad.Flags &^= unix.ST_NODEV + case "suid": + bad.Flags &^= unix.ST_NOSUID + } + if gitBoundedWorkFilesystem(bad) { + t.Fatal("wrong independent filesystem policy accepted", kind) + } + } +} + +func TestGitMountReadUsesOriginalLivePIDFD(t *testing.T) { + child := exec.Command("/usr/bin/sleep", "10") + child.Env = []string{} + if child.Start() != nil { + t.Fatal("inert mount observation child") + } + defer func() { child.Process.Kill(); child.Wait() }() + identity, err := processGateIdentity(child.Process.Pid, strings.Repeat("b", 32)) + if err != nil { + t.Fatal(err) + } + pidfd := gitTestPIDFD(t, child.Process.Pid) + before := gitPacketFDCount(t) + got, err := gitReadMountInfo(context.Background(), identity, int(pidfd.Fd())) + want, readErr := os.ReadFile(fmt.Sprintf("/proc/%d/mountinfo", child.Process.Pid)) + if err != nil || readErr != nil || len(got) == 0 || string(got) != string(want) || gitPacketFDCount(t) != before { + t.Fatal("actual mount observation changed bytes or leaked descriptors", err, readErr) + } + wrong := identity + wrong.StartTicks++ + if raw, err := gitReadMountInfo(context.Background(), wrong, int(pidfd.Fd())); err == nil || raw != nil { + t.Fatal("wrong process identity borrowed mount view") + } + child.Process.Kill() + child.Wait() + if raw, err := gitReadMountInfo(context.Background(), identity, int(pidfd.Fd())); err == nil || raw != nil { + t.Fatal("dead original process retained mount authority") + } +} From d4058c3714e7db05ed56502f5e9cc2c9db0a30af Mon Sep 17 00:00:00 2001 From: Franz Hemmer <8227352+HemSoft@users.noreply.github.com> Date: Wed, 21 Oct 2026 10:38:31 -0400 Subject: [PATCH 02/12] Check actual private Git mount views and propagation --- reviewer/GIT-MOUNT-BOUNDARY.md | 61 ++++++ reviewer/git_mount_boundary_linux.go | 48 ++++- reviewer/git_mount_boundary_linux_test.go | 223 +++++++++++++++++++++- 3 files changed, 323 insertions(+), 9 deletions(-) create mode 100644 reviewer/GIT-MOUNT-BOUNDARY.md diff --git a/reviewer/GIT-MOUNT-BOUNDARY.md b/reviewer/GIT-MOUNT-BOUNDARY.md new file mode 100644 index 00000000..87fff612 --- /dev/null +++ b/reviewer/GIT-MOUNT-BOUNDARY.md @@ -0,0 +1,61 @@ +# Git worker mount observation + +The private mount observer adds a kernel check for the unfinished acquisition +factory. It returns no Git source capability, token, worker grant or publication +authority. The factory still has to compose exact unit/cgroup ownership, +descriptor and installed-filter checks, authenticated export, broker drain and +physical stop. A caller's serialized mount table cannot qualify any of these. + +The observer starts from an already retained process boundary and its original +pidfd. It rechecks that worker before and after the operation. It reads bounded +`/proc/PID/mountinfo` and opens the worker's actual root view. That root includes +the target's mounted view, not the supervisor's pathname interpretation. +See the Linux [root-view](https://man7.org/linux/man-pages/man5/proc_pid_root.5.html) +and [mountinfo](https://man7.org/linux/man-pages/man5/proc_pid_mountinfo.5.html) +contracts. + +The fixed layout contains only the runtime root, read-only broker, writable +work/tmp aliases, read-only private proc and the two fixed device bindings. +Root and broker inodes must match the retained original directories. Work and +tmp must be the same owned 0700 directory on the exact 256-MiB, 65,536-inode +tmpfs, with nosuid/nodev/noexec, writable mount flags and the kernel's noswap +superblock option. Scalar capacity checks avoid multiplication overflow. +The device targets must be the actual root-owned null and urandom character +devices; urandom stays read-only. The private proc's PID1 namespace must match +the retained worker PID namespace. The mount table must remain unchanged across +the check. + +Parsing and policy are distinct. Known kernel optional fields are retained; +duplicate, unknown or inconsistent fields refuse. Shared propagation is refused. +Slave `master` fields are expected in the actual bubblewrap fixture and do not +describe outward propagation. They can receive mount events from the trusted +host, so this check does not claim a boundary against a hostile host service +identity or privileged administrator. The full factory must preserve the +existing trusted-host assumptions and recheck its retained assets and topology. +See [Linux mount propagation](https://man7.org/linux/man-pages/man7/mount_namespaces.7.html). + +## Validation and limits + +Static kernel-format fixtures exercise all fixed mount roles, read-only versus +superblock flags, aliases, noexec/nodev/nosuid/noswap, capacities and overflow, +unexpected mounts, propagation, malformed fields and byte bounds. They prove +parser/policy behavior, not actual storage qualification. + +An actual inert child and original pidfd test kernel mount reads, exact returned +bytes, descriptor lifetime and refusal after process death or identity change. +The namespace opt-in runs the ordinary measured Go fixture in a minimal runtime +under the fixed final filter with seven actual mounts. It verifies the worker's +root view exposes the same work/tmp aliases as the parent's retained descriptor. +An intentionally unqualified empty host directory must be rejected. It carries +only trusted fixture data and is never a production storage fallback. + +```sh +SFL_GIT_BOOTSTRAP_NAMESPACE=1 go -C reviewer test -race -count=1 -run '^TestGitMount' . +``` + +No successful aggregate mount admission is established on mini. The required +full-unit noswap tmpfs and sealed inner-worker execution still fail independently. +This fixture uses ordinary files and unqualified storage; it does not waive +either prerequisite. Full factory, descriptor/filter qualification, durable +source publication/recovery, credentials and live activation remain separate +work in #176 and its related issues. diff --git a/reviewer/git_mount_boundary_linux.go b/reviewer/git_mount_boundary_linux.go index dd32122b..e96333dd 100644 --- a/reviewer/git_mount_boundary_linux.go +++ b/reviewer/git_mount_boundary_linux.go @@ -18,11 +18,13 @@ import ( const gitMountInfoBytes = 64 << 10 type gitMountRow struct { - ID, Parent uint64 - Device uint64 - Root, Point string - Filesystem string - Options, Super map[string]bool + ID, Parent uint64 + Device uint64 + Root, Point string + Filesystem string + Options, Super map[string]bool + Shared, Master, Propagate uint64 + Unbindable bool } func gitMountPath(raw string) (string, error) { @@ -77,7 +79,7 @@ func parseGitMountTable(raw []byte) (map[string]gitMountRow, error) { for _, line := range strings.Split(strings.TrimSuffix(string(raw), "\n"), "\n") { left, right, found := strings.Cut(line, " - ") a, b := strings.Fields(left), strings.Fields(right) - if !found || len(a) < 6 || len(a) > 7 || len(b) != 3 || len(rows) >= 32 || len(a) == 7 && a[6] != "unbindable" { + if !found || len(a) < 6 || len(a) > 10 || len(b) != 3 || len(rows) >= 32 { return nil, errGitUnit } id, e1 := strconv.ParseUint(a[0], 10, 64) @@ -95,7 +97,37 @@ func parseGitMountTable(raw []byte) (map[string]gitMountRow, error) { if e1 != nil || e2 != nil || e3 != nil || e4 != nil || e5 != nil || e6 != nil || rows[point].ID != 0 || b[0] == "" { return nil, errGitUnit } - rows[point] = gitMountRow{id, parent, unix.Mkdev(uint32(major), uint32(minor)), root, point, b[0], options, super} + row := gitMountRow{ID: id, Parent: parent, Device: unix.Mkdev(uint32(major), uint32(minor)), Root: root, Point: point, Filesystem: b[0], Options: options, Super: super} + seen := map[string]bool{} + for _, field := range a[6:] { + key, value, found := strings.Cut(field, ":") + if seen[key] { + return nil, errGitUnit + } + seen[key] = true + if key == "unbindable" && !found { + row.Unbindable = true + continue + } + valueID, err := strconv.ParseUint(value, 10, 64) + if !found || err != nil || valueID == 0 { + return nil, errGitUnit + } + switch key { + case "shared": + row.Shared = valueID + case "master": + row.Master = valueID + case "propagate_from": + row.Propagate = valueID + default: + return nil, errGitUnit + } + } + if row.Unbindable && (row.Shared != 0 || row.Master != 0 || row.Propagate != 0) || row.Propagate != 0 && row.Master == 0 { + return nil, errGitUnit + } + rows[point] = row ids[id] = true } return rows, nil @@ -108,7 +140,7 @@ func validateGitMountLayout(rows map[string]gitMountRow, binding gitMaterializat root := rows["/"] wanted := map[string]bool{"/": true, "/broker": true, "/work": true, "/tmp": true, "/proc": true, "/dev/null": true, "/dev/urandom": true} for point, row := range rows { - if !wanted[point] || point != "/" && row.Parent != root.ID { + if !wanted[point] || point != "/" && row.Parent != root.ID || row.Shared != 0 { return errGitUnit } if point == "/" || point == "/broker" || point == "/proc" || point == "/dev/urandom" { diff --git a/reviewer/git_mount_boundary_linux_test.go b/reviewer/git_mount_boundary_linux_test.go index 728e110d..42b71439 100644 --- a/reviewer/git_mount_boundary_linux_test.go +++ b/reviewer/git_mount_boundary_linux_test.go @@ -3,12 +3,17 @@ package reviewer import ( + "bytes" "context" + "encoding/json" "fmt" + "net" "os" "os/exec" + "path/filepath" "strings" "testing" + "time" "golang.org/x/sys/unix" ) @@ -34,7 +39,12 @@ func TestGitMountTableFixedLayoutAndRefusals(t *testing.T) { if err != nil || len(rows) != 7 || validateGitMountLayout(rows, binding) != nil || rows["/work"].Device != unix.Mkdev(0, 42) { t.Fatal("fixed mount policy", err) } - for _, kind := range []string{"extra", "missing", "duplicate-point", "duplicate-id", "propagation", "root-writable", "broker-writable", "proc-writable", "wrong-proc", "random-writable", "wrong-parent", "different-alias", "different-device", "noexec", "nosuid", "nodev", "noswap", "fake-noswap", "duplicate-option", "conflicting-flags", "bad-escape", "trailing", "nul", "bounds"} { + slave := strings.Replace(raw, "ro,relatime -", "ro,relatime master:9 propagate_from:8 -", 1) + rows, err = parseGitMountTable([]byte(slave)) + if err != nil || rows["/"].Master != 9 || rows["/"].Propagate != 8 || validateGitMountLayout(rows, binding) != nil { + t.Fatal("kernel slave propagation fields were confused with shared propagation", err) + } + for _, kind := range []string{"extra", "missing", "duplicate-point", "duplicate-id", "propagation", "duplicate-master", "unknown-propagation", "invalid-propagation", "root-writable", "broker-writable", "proc-writable", "wrong-proc", "random-writable", "wrong-parent", "different-alias", "different-device", "noexec", "nosuid", "nodev", "noswap", "fake-noswap", "duplicate-option", "conflicting-flags", "bad-escape", "trailing", "nul", "bounds"} { t.Run(kind, func(t *testing.T) { bad := raw switch kind { @@ -48,6 +58,12 @@ func TestGitMountTableFixedLayoutAndRefusals(t *testing.T) { bad = strings.Replace(raw, "103 100", "102 100", 1) case "propagation": bad = strings.Replace(raw, "ro,relatime -", "ro,relatime shared:9 -", 1) + case "duplicate-master": + bad = strings.Replace(raw, "ro,relatime -", "ro,relatime master:9 master:8 -", 1) + case "unknown-propagation": + bad = strings.Replace(raw, "ro,relatime -", "ro,relatime unknown:9 -", 1) + case "invalid-propagation": + bad = strings.Replace(raw, "ro,relatime -", "ro,relatime propagate_from:9 -", 1) case "root-writable": bad = strings.Replace(raw, "/ ro,relatime", "/ rw,relatime", 1) case "broker-writable": @@ -153,3 +169,208 @@ func TestGitMountReadUsesOriginalLivePIDFD(t *testing.T) { t.Fatal("dead original process retained mount authority") } } + +func TestGitMountNamespaceChild(t *testing.T) { + if os.Getenv("SFL_GIT_MOUNT_CHILD") != "1" { + return + } + ctx, cancel := context.WithTimeout(context.Background(), 12*time.Second) + defer cancel() + conn, err := net.DialUnix("unixpacket", nil, &net.UnixAddr{Name: "/broker/worker.sock", Net: "unixpacket"}) + if err != nil { + t.Fatal(err) + } + defer conn.Close() + if unix.Prctl(unix.PR_SET_DUMPABLE, 0, 0, 0, 0) != nil || writeGitControlPacket(ctx, conn, []byte("ready"), nil) != nil { + t.Fatal("inert mount readiness") + } + packet, files, err := readGitControlPacket(ctx, conn, 0) + closeVMFiles(files) + if err != nil || string(packet) != "finish" { + t.Fatal("inert mount completion", err) + } + os.Exit(0) +} + +func gitMountInertRuntime(t *testing.T, ctx context.Context) (*os.File, *os.File, GitRuntimePins) { + t.Helper() + build := actualGitRuntime(t) + var manifest gitRuntimeManifest + if json.Unmarshal(build.Manifest, &manifest) != nil { + t.Fatal("runtime manifest") + } + prepared, err := prepareGitRuntime(ctx, nativeRuntimePrivateParent(t), bytes.NewReader(build.Artifact), build.Pins, nil) + if err != nil { + t.Fatal(err) + } + bodies := map[string][]byte{} + for _, file := range manifest.Files { + bodies[file.Path], err = prepared.root.ReadFile(file.Path) + if err != nil { + t.Fatal(err) + } + } + if prepared.Close() != nil { + t.Fatal("original runtime disposal") + } + image, err := os.ReadFile(os.Args[0]) + if err != nil { + t.Fatal(err) + } + manifest.Git = VMAssetPin{gitRawHash(image), int64(len(image))} + manifest.HTTP = manifest.Git + for i := range manifest.Files { + if manifest.Files[i].Path == gitRuntimeProgram || manifest.Files[i].Path == gitRuntimeHTTP { + manifest.Files[i].SHA256, manifest.Files[i].Bytes = manifest.Git.SHA256, manifest.Git.Bytes + bodies[manifest.Files[i].Path] = image + } + } + artifact, pins := gitRuntimeFrame(t, manifest, bodies) + root, err := prepareGitRuntime(ctx, nativeRuntimePrivateParent(t), bytes.NewReader(artifact), pins, nil) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { root.Close() }) + fd, err := root.directoryFD() + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { fd.Close() }) + exe, err := root.root.Open(gitRuntimeProgram) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { exe.Close() }) + return fd, exe, pins +} + +func TestGitMountActualPrivateViewRefusesUnqualifiedWorkspace(t *testing.T) { + if os.Getenv("SFL_GIT_BOOTSTRAP_NAMESPACE") != "1" { + t.Skip("explicit inert private-namespace mount observation") + } + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + defer cancel() + root, exe, pins := gitMountInertRuntime(t, ctx) + listener, _, directory := gitSupervisorListener(t) + gitSocket := filepath.Join(directory, "git.sock") + unusedBroker, err := net.ListenUnix("unix", &net.UnixAddr{Name: gitSocket, Net: "unix"}) + if err != nil { + t.Fatal(err) + } + defer unusedBroker.Close() + if os.Chmod(gitSocket, 0600) != nil || os.WriteFile(filepath.Join(directory, "git-read.used"), []byte("sfl-git-read-broker-1\n"), 0600) != nil { + t.Fatal("inert broker directory") + } + broker, err := os.Open(directory) + if err != nil { + t.Fatal(err) + } + defer broker.Close() + // This trusted empty fixture deliberately uses an unqualified host + // directory. It must NEVER pass the production workspace policy. + workspace := t.TempDir() + if os.Chmod(workspace, 0700) != nil { + t.Fatal("private empty workspace fixture") + } + work, err := os.Open(workspace) + if err != nil { + t.Fatal(err) + } + defer work.Close() + alias, err := duplicateGitControlFile(work) + if err != nil { + t.Fatal(err) + } + defer alias.Close() + filter, err := retainGitPayloadSeccomp() + if err != nil { + t.Fatal(err) + } + defer filter.Close() + helperBytes, err := os.ReadFile("/usr/bin/bwrap") + if err != nil { + t.Fatal(err) + } + helper, err := retainNativeBootstrap(ctx, VMBootAsset{Path: "/usr/bin/bwrap", SHA256: gitRawHash(helperBytes), Bytes: int64(len(helperBytes))}) + if err != nil { + t.Fatal(err) + } + defer helper.Close() + args := []string{"--unshare-all", "--unshare-user", "--disable-userns", "--die-with-parent", "--new-session", "--cap-drop", "ALL", "--clearenv", "--setenv", "SFL_GIT_MOUNT_CHILD", "1", "--ro-bind-fd", "3", "/", "--ro-bind-fd", "4", "/broker", "--bind-fd", "6", "/work", "--bind-fd", "7", "/tmp", "--proc", "/proc", "--remount-ro", "/proc", "--dev-bind", "/dev/null", "/dev/null", "--dev-bind", "/dev/urandom", "/dev/urandom", "--remount-ro", "/dev/urandom", "--chdir", "/", "--seccomp", "5", "--", "/usr/bin/git", "-test.run=^TestGitMountNamespaceChild$"} + command := exec.CommandContext(ctx, fmt.Sprintf("/proc/%d/fd/%d", os.Getpid(), helper.Fd()), args...) + command.Env = []string{} + command.ExtraFiles = []*os.File{root, broker, filter, work, alias} + var output bytes.Buffer + command.Stdout, command.Stderr = &output, &output + if command.Start() != nil { + t.Fatal("inert mount namespace launch") + } + waited := false + defer func() { + if !waited { + command.Process.Kill() + command.Wait() + } + if t.Failed() { + t.Log("inert mount fixture:", output.String()) + } + }() + listener.SetDeadline(time.Now().Add(8 * time.Second)) + conn, err := listener.AcceptUnix() + if err != nil { + t.Fatal("inert namespace control", err) + } + defer conn.Close() + packet, files, err := readGitControlPacket(ctx, conn, 0) + closeVMFiles(files) + if err != nil || string(packet) != "ready" { + t.Fatal("inert namespace readiness", err) + } + peer, err := gitBrokerPeer(conn) + if err != nil { + t.Fatal(err) + } + defer peer.kernel.Close() + identity, original, err := retainGitBrokerPeer(peer, strings.Repeat("b", 32)) + if err != nil { + t.Fatal(err) + } + defer unix.Close(original) + raw, err := gitReadMountInfo(ctx, identity, original) + if err != nil { + t.Fatal(err) + } + rows, err := parseGitMountTable(raw) + if err != nil || len(rows) != 7 { + t.Fatalf("actual fixed mount structure: %v\n%s", err, raw) + } + for _, point := range []string{"/", "/broker", "/work", "/tmp", "/proc", "/dev/null", "/dev/urandom"} { + if rows[point].ID == 0 { + t.Fatal("expected actual private mount missing", point) + } + } + view, err := os.OpenRoot(fmt.Sprintf("/proc/%d/root", identity.PID)) + if err != nil { + t.Fatal(err) + } + defer view.Close() + insideWork, err := view.Stat("work") + insideTmp, aliasErr := view.Stat("tmp") + outside, outsideErr := work.Stat() + if err != nil || aliasErr != nil || outsideErr != nil || !os.SameFile(insideWork, insideTmp) || !os.SameFile(insideWork, outside) { + t.Fatal("original worker root did not expose its actual mounted aliases", err, aliasErr, outsideErr) + } + boundary, err := retainGitProcessBoundary(ctx, identity, original, root, exe, pins.Git, pins) + if err != nil { + t.Fatal("actual process boundary", err) + } + defer boundary.Close() + binding, _ := gitMountTableFixture(t) + if checkGitMountBoundary(ctx, boundary, broker, binding) == nil { + t.Fatal("unqualified host workspace became a source admission proof") + } + if writeGitControlPacket(ctx, conn, []byte("finish"), nil) != nil || command.Wait() != nil { + t.Fatal("inert namespace completion") + } + waited = true +} From 516652cc722f8bafcad568e003d277d0ed48f911 Mon Sep 17 00:00:00 2001 From: Franz Hemmer <8227352+HemSoft@users.noreply.github.com> Date: Wed, 21 Oct 2026 10:54:55 -0400 Subject: [PATCH 03/12] Inspect original Git worker descriptors before factory grant --- reviewer/GIT-DESCRIPTOR-BOUNDARY.md | 64 ++++ reviewer/git_descriptor_boundary_linux.go | 324 ++++++++++++++++++ .../git_descriptor_boundary_linux_test.go | 158 +++++++++ reviewer/git_mount_boundary_linux_test.go | 104 +++++- 4 files changed, 649 insertions(+), 1 deletion(-) create mode 100644 reviewer/GIT-DESCRIPTOR-BOUNDARY.md create mode 100644 reviewer/git_descriptor_boundary_linux.go create mode 100644 reviewer/git_descriptor_boundary_linux_test.go diff --git a/reviewer/GIT-DESCRIPTOR-BOUNDARY.md b/reviewer/GIT-DESCRIPTOR-BOUNDARY.md new file mode 100644 index 00000000..a647aecd --- /dev/null +++ b/reviewer/GIT-DESCRIPTOR-BOUNDARY.md @@ -0,0 +1,64 @@ +# Git worker descriptor observation + +The private descriptor observer supplements process and mount checks before the +unfinished factory grants work. It returns no source or reusable admission +capability. The caller retains the original worker pidfd and rechecks its +identity before and after observation. + +The observer enumerates bounded kernel `fdinfo`, reads the original descriptors' +current flags, and duplicates actual objects through that original pidfd. A +duplicate always has CLOEXEC itself, so its descriptor flags cannot establish +the original CLOEXEC bit. Reading kernel `fdinfo` does. See Linux +[fdinfo](https://man7.org/linux/man-pages/man5/proc_pid_fdinfo.5.html) and +[pidfd_getfd](https://man7.org/linux/man-pages/man2/pidfd_getfd.2.html). + +Inspection never reads object contents or changes their offsets, status flags, +socket options or connection state. It checks the object again through the +original pidfd and requires stable original flags, identity and descriptor +numbers. Any permission error, unknown descriptor or observed change refuses. +This is a readiness check on the measured inert worker before candidate code +runs, not a general guarantee against an adversary changing a running process. + +The fixed worker has null standard streams. All private descriptors require +CLOEXEC. The exact export write pipe must match the factory's retained read +endpoint. Exactly one control connection must be an unnamed AF_UNIX SEQPACKET +client connected to the original host pathname of the factory's actual +accepted control connection, with the current supervisor's kernel peer +credentials. The namespace-local `/broker/worker.sock` alias does not change the +kernel's stored server pathname. The accepted connection's original peer pidfd +must match the retained worker. Exactly one pidfd must match the retained original supervisor. +The measured Go runtime's CLOEXEC eventpoll and eventfd handles are permitted. +Extra sockets, directories, regular files, pipes, pidfds and other anonymous +objects refuse, even when they have CLOEXEC. Socket domain checks never exercise +inherited Internet sockets. + +## Validation + +Static policy fixtures cover role replacement, missing or duplicate descriptors, +wrong access modes, missing CLOEXEC, extra host files/directories/sockets, +Internet domains and unknown anonymous objects. These are parser/policy tests. +The ordinary inert child test waits for its kernel sleep, then inspects actual +null descriptors without leaking handles. Waiting for sleep avoids inspecting +the dynamic loader's temporary library descriptors before inert readiness. + +An explicit private-namespace fixture runs an ordinary measured Go executable, +authenticates the original supervisor and disables dumpability. It supplies a +real export pipe and tests the fixed roles plus actual extra host file, +directory, Internet socket and inheritable descriptor cases. It uses trusted +empty fixture storage, never candidate input or a production storage fallback. + +```sh +SFL_GIT_DESCRIPTOR_KERNEL=1 SFL_GIT_BOOTSTRAP_NAMESPACE=1 \ + go -C reviewer test -race -count=1 -v -run '^TestGitDescriptorActualPrivateWorker$' . +``` + +Mini denies traversal of the nondumpable worker's `/proc/PID/fd` directory. +The implementation enumerates `fdinfo` and uses original-pidfd duplication; +it does not relax dumpability, change host policy or substitute pathname opens +for retained kernel object identities. The explicit fixture's actual result +must accompany any qualification claim. The ordinary suite skips this opt-in. + +This does not qualify sealed worker execution, the required whole-unit noswap +tmpfs, installed-filter provenance, unit/cgroup ownership, authenticated export +publication or controller recovery. Those remain separate factory requirements +in #176. No provider credentials, model inference or live activation occur here. diff --git a/reviewer/git_descriptor_boundary_linux.go b/reviewer/git_descriptor_boundary_linux.go new file mode 100644 index 00000000..e7f43771 --- /dev/null +++ b/reviewer/git_descriptor_boundary_linux.go @@ -0,0 +1,324 @@ +//go:build linux + +package reviewer + +import ( + "context" + "fmt" + "io" + "net" + "os" + "path/filepath" + "strconv" + "strings" + + "golang.org/x/sys/unix" +) + +const gitDescriptorLimit = 256 + +// fdinfo describes the ORIGINAL descriptor's current CLOEXEC bit. A duplicate +// from pidfd_getfd always has CLOEXEC itself and cannot establish that fact. +func gitDescriptorFlags(raw []byte) (uint64, error) { + if len(raw) == 0 || len(raw) > 16384 || raw[len(raw)-1] != '\n' || strings.ContainsAny(string(raw), "\x00\r") { + return 0, errGitUnit + } + found := false + var flags uint64 + for _, line := range strings.Split(string(raw), "\n") { + key, value, _ := strings.Cut(line, ":") + if key != "flags" { + continue + } + if found { + return 0, errGitUnit + } + found = true + var err error + flags, err = strconv.ParseUint(strings.TrimSpace(value), 8, 32) + if err != nil { + return 0, errGitUnit + } + } + if !found { + return 0, errGitUnit + } + return flags, nil +} + +type gitDescriptorObservation struct { + Number int + Flags uint64 + File gitKernelFileID + Mode uint32 + Device uint64 + Target string + SocketDomain, SocketType int + SocketLocal, SocketPeer string + Credentials unix.Ucred +} + +// Only the retained ORIGINAL pidfd selects the task for duplication. Reading +// descriptor metadata or duplicating a descriptor never mutates its shared +// file offset, status flags, socket options or contents. Missing access refuses. +func observeGitDescriptors(ctx context.Context, identity GateIdentity, original int) (_ []gitDescriptorObservation, result error) { + stage := "identity" + defer func() { + if result != nil { + result = fmt.Errorf("Git descriptor %s: %w", stage, errGitUnit) + } + }() + if ctx == nil || ctx.Err() != nil || gitCheckProcessPIDFD(identity, original) != nil { + return nil, errGitUnit + } + path := fmt.Sprintf("/proc/%d", identity.PID) + stage = "process view" + proc, err := os.OpenRoot(path) + if err != nil { + return nil, errGitUnit + } + defer proc.Close() + stage = "directory access" + // fdinfo supports current CLOEXEC observation and does not require + // traversing the nondumpable task's root-owned fd symlink directory. + // Actual file objects still come only from the original pidfd. + directory, err := proc.Open("fdinfo") + if err != nil { + return nil, errGitUnit + } + defer directory.Close() + names, err := directory.Readdirnames(gitDescriptorLimit + 1) + if err != nil && err != io.EOF || len(names) > gitDescriptorLimit { + return nil, errGitUnit + } + readFlags := func(name string) (uint64, error) { + file, err := proc.OpenFile("fdinfo/"+name, os.O_RDONLY|unix.O_NOFOLLOW|unix.O_NONBLOCK, 0) + if err != nil { + return 0, errGitUnit + } + raw, readErr := io.ReadAll(io.LimitReader(file, 16385)) + closeErr := file.Close() + if readErr != nil || closeErr != nil { + return 0, errGitUnit + } + return gitDescriptorFlags(raw) + } + observations := make([]gitDescriptorObservation, 0, len(names)) + for _, name := range names { + stage = "descriptor number" + fd, err := strconv.Atoi(name) + if err != nil || fd < 0 || strconv.Itoa(fd) != name || ctx.Err() != nil { + return nil, errGitUnit + } + stage = "original flags" + flags, err := readFlags(name) + if err != nil { + return nil, errGitUnit + } + stage = "original duplication" + duplicate, err := unix.PidfdGetfd(original, fd, 0) + if err != nil { + return nil, errGitUnit + } + observation, observeErr := gitObserveDescriptor(duplicate) + target, targetErr := os.Readlink(fmt.Sprintf("/proc/self/fd/%d", duplicate)) + closeErr := unix.Close(duplicate) + after, afterErr := readFlags(name) + var actual unix.Stat_t + second, statErr := unix.PidfdGetfd(original, fd, 0) + if statErr == nil { + statErr = unix.Fstat(second, &actual) + if err := unix.Close(second); statErr == nil { + statErr = err + } + } + stage = "descriptor stability" + if observeErr != nil { + stage = "descriptor metadata" + return nil, errGitUnit + } + if closeErr != nil || afterErr != nil || flags != after { + return nil, errGitUnit + } + if targetErr != nil { + stage = "target read" + return nil, errGitUnit + } + if statErr != nil { + stage = "original stat" + return nil, errGitUnit + } + if observation.File != (gitKernelFileID{uint64(actual.Dev), actual.Ino}) { + stage = "original inode changed" + return nil, errGitUnit + } + observation.Number, observation.Flags, observation.Target = fd, flags, target + observations = append(observations, observation) + } + // Refuse descriptor creation/removal while checking readiness. The measured + // inert worker must stay in its grant wait, before any candidate parser runs. + stage = "directory stability" + after, err := proc.Open("fdinfo") + if err != nil { + return nil, errGitUnit + } + defer after.Close() + last, err := after.Readdirnames(gitDescriptorLimit + 1) + if err != nil && err != io.EOF || len(last) != len(names) { + return nil, errGitUnit + } + seen := map[string]bool{} + for _, name := range names { + seen[name] = true + } + for _, name := range last { + if !seen[name] { + return nil, errGitUnit + } + } + if ctx.Err() != nil || gitCheckProcessPIDFD(identity, original) != nil { + return nil, errGitUnit + } + return observations, nil +} + +func gitObserveDescriptor(fd int) (gitDescriptorObservation, error) { + var stat unix.Stat_t + if unix.Fstat(fd, &stat) != nil || stat.Ino == 0 { + return gitDescriptorObservation{}, errGitUnit + } + o := gitDescriptorObservation{File: gitKernelFileID{uint64(stat.Dev), stat.Ino}, Mode: stat.Mode, Device: stat.Rdev} + if stat.Mode&unix.S_IFMT != unix.S_IFSOCK { + return o, nil + } + var err error + o.SocketDomain, err = unix.GetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_DOMAIN) + if err != nil { + return o, errGitUnit + } + o.SocketType, err = unix.GetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_TYPE) + if err != nil { + return o, errGitUnit + } + if o.SocketDomain != unix.AF_UNIX { + // Preserve actual foreign socket evidence for policy refusal. Never + // try to connect it or exercise its network authority. + return o, nil + } + local, err := unix.Getsockname(fd) + if err != nil { + return o, errGitUnit + } + peer, err := unix.Getpeername(fd) + if err != nil { + return o, errGitUnit + } + l, ok := local.(*unix.SockaddrUnix) + p, peerOK := peer.(*unix.SockaddrUnix) + if !ok || !peerOK { + return o, errGitUnit + } + o.SocketLocal, o.SocketPeer = l.Name, p.Name + credentials, err := unix.GetsockoptUcred(fd, unix.SOL_SOCKET, unix.SO_PEERCRED) + if err != nil || credentials == nil { + return o, errGitUnit + } + o.Credentials = *credentials + return o, nil +} + +// This is a private policy check, not reusable admission authority. Expected +// pipe/pidfd identities come from factory-owned retained kernel handles. +func validateGitDescriptors(rows []gitDescriptorObservation, export gitKernelFileID, supervisor gitPIDFDIdentity, controlPath string) error { + if len(rows) < 6 || len(rows) > gitDescriptorLimit || export == (gitKernelFileID{}) || supervisor == (gitPIDFDIdentity{}) || !filepath.IsAbs(controlPath) || filepath.Clean(controlPath) != controlPath || filepath.Base(controlPath) != "worker.sock" || len(controlPath) > 107 { + return errGitUnit + } + seen := map[int]bool{} + controlCount, exportCount, supervisorCount := 0, 0, 0 + for _, row := range rows { + if row.Number < 0 || seen[row.Number] { + return errGitUnit + } + seen[row.Number] = true + access := row.Flags & unix.O_ACCMODE + if row.Number <= 2 { + want := uint64(unix.O_WRONLY) + if row.Number == 0 { + want = unix.O_RDONLY + } + if row.Mode&unix.S_IFMT != unix.S_IFCHR || row.Device != unix.Mkdev(1, 3) || access != want && access != unix.O_RDWR { + return errGitUnit + } + continue + } + if row.Flags&unix.O_CLOEXEC == 0 { + return errGitUnit + } + switch { + case row.Mode&unix.S_IFMT == unix.S_IFIFO && row.File == export && access == unix.O_WRONLY: + exportCount++ + case row.Mode&unix.S_IFMT == unix.S_IFSOCK && row.SocketDomain == unix.AF_UNIX && row.SocketType == unix.SOCK_SEQPACKET && (row.SocketLocal == "" || row.SocketLocal == "@") && row.SocketPeer == controlPath && row.Credentials == (unix.Ucred{Pid: int32(os.Getpid()), Uid: uint32(os.Geteuid()), Gid: uint32(os.Getegid())}): + controlCount++ + case row.Mode&unix.S_IFMT == 0 && row.File == gitKernelFileID(supervisor): + supervisorCount++ + case row.Mode&unix.S_IFMT == 0 && (row.Target == "anon_inode:[eventpoll]" || row.Target == "anon_inode:[eventfd]"): + // The measured Go runtime's private CLOEXEC poller handles. + default: + return errGitUnit + } + } + if !seen[0] || !seen[1] || !seen[2] || controlCount != 1 || exportCount != 1 || supervisorCount != 1 { + return errGitUnit + } + return nil +} + +func checkGitDescriptorBoundary(ctx context.Context, boundary *gitProcessBoundary, exportRead, supervisor *os.File, control *net.UnixConn) error { + if ctx == nil || boundary == nil || exportRead == nil || supervisor == nil || control == nil { + return errGitUnit + } + boundary.mu.Lock() + defer boundary.mu.Unlock() + if boundary.checkWorker(ctx) != nil { + return errGitUnit + } + peer, err := gitBrokerPeer(control) + if err != nil { + return errGitUnit + } + defer peer.kernel.Close() + identity, pidfd, err := retainGitBrokerPeer(peer, boundary.worker.InvocationID) + if err != nil { + return errGitUnit + } + defer unix.Close(pidfd) + if identity != boundary.worker { + return errGitUnit + } + local, ok := control.LocalAddr().(*net.UnixAddr) + if !ok { + return errGitUnit + } + pipeID, err := gitStatFileID(exportRead) + if err != nil { + return errGitUnit + } + var pipe unix.Stat_t + flags, err := unix.FcntlInt(exportRead.Fd(), unix.F_GETFL, 0) + if err != nil || unix.Fstat(int(exportRead.Fd()), &pipe) != nil || pipe.Mode&unix.S_IFMT != unix.S_IFIFO || flags&unix.O_ACCMODE != unix.O_RDONLY { + return errGitUnit + } + want, err := gitLivePIDFDIdentity(supervisor) + if err != nil { + return errGitUnit + } + rows, err := observeGitDescriptors(ctx, boundary.worker, boundary.pidfd) + if err != nil || validateGitDescriptors(rows, pipeID, want, local.Name) != nil || gitCheckProcessPIDFD(identity, pidfd) != nil || boundary.checkWorker(ctx) != nil { + return errGitUnit + } + after, err := gitLivePIDFDIdentity(supervisor) + if err != nil || after != want || ctx.Err() != nil { + return errGitUnit + } + return nil +} diff --git a/reviewer/git_descriptor_boundary_linux_test.go b/reviewer/git_descriptor_boundary_linux_test.go new file mode 100644 index 00000000..634989f9 --- /dev/null +++ b/reviewer/git_descriptor_boundary_linux_test.go @@ -0,0 +1,158 @@ +//go:build linux + +package reviewer + +import ( + "context" + "fmt" + "os" + "os/exec" + "strings" + "testing" + "time" + + "golang.org/x/sys/unix" +) + +func TestGitDescriptorFlagsCurrentCloexecAndMalformedData(t *testing.T) { + for _, input := range []struct { + raw string + want uint64 + }{ + {"pos:\t0\nflags:\t02000002\nmnt_id:\t1\nino:\t42\n", unix.O_CLOEXEC | unix.O_RDWR}, + {"pos:\t0\nflags:\t02\n", unix.O_RDWR}, + } { + actual, err := gitDescriptorFlags([]byte(input.raw)) + if err != nil || actual != input.want { + t.Fatalf("current kernel flags %o: %v", actual, err) + } + } + for _, raw := range []string{"", "flags:\t02000002", "pos:\t0\n", "flags:\t08\n", "flags:\t-1\n", "flags:\t02000002\nflags:\t02\n", "flags:\t02\n\x00", strings.Repeat("x", 16385)} { + if _, err := gitDescriptorFlags([]byte(raw)); err == nil { + t.Fatalf("malformed fdinfo accepted: %q", raw[:min(30, len(raw))]) + } + } +} + +// Static observations prove the private policy only. The separate private +// namespace fixture must obtain every descriptor from the real original pidfd. +func TestGitDescriptorPolicyRefusesExtraCapabilities(t *testing.T) { + export := gitKernelFileID{1, 42} + supervisor := gitPIDFDIdentity{2, 43} + rows := []gitDescriptorObservation{ + {Number: 0, Flags: unix.O_RDONLY, Mode: unix.S_IFCHR, Device: unix.Mkdev(1, 3)}, + {Number: 1, Flags: unix.O_WRONLY, Mode: unix.S_IFCHR, Device: unix.Mkdev(1, 3)}, + {Number: 2, Flags: unix.O_WRONLY, Mode: unix.S_IFCHR, Device: unix.Mkdev(1, 3)}, + {Number: 6, Flags: unix.O_CLOEXEC | unix.O_WRONLY, Mode: unix.S_IFIFO, File: export}, + {Number: 7, Flags: unix.O_CLOEXEC | unix.O_RDWR, Mode: unix.S_IFSOCK, SocketDomain: unix.AF_UNIX, SocketType: unix.SOCK_SEQPACKET, SocketPeer: "/broker/worker.sock", Credentials: unix.Ucred{Pid: int32(os.Getpid()), Uid: uint32(os.Geteuid()), Gid: uint32(os.Getegid())}}, + {Number: 8, Flags: unix.O_CLOEXEC, File: gitKernelFileID(supervisor)}, + {Number: 9, Flags: unix.O_CLOEXEC, Target: "anon_inode:[eventpoll]"}, + {Number: 10, Flags: unix.O_CLOEXEC, Target: "anon_inode:[eventfd]"}, + } + if validateGitDescriptors(rows, export, supervisor, "/broker/worker.sock") != nil { + t.Fatal("fixed static descriptor policy") + } + for _, mode := range []string{"missing-standard", "stdio-socket", "stdio-read", "inheritable", "wrong-export", "export-read", "wrong-supervisor", "wrong-peer", "internet", "wrong-control-path", "unknown-anon", "extra-regular", "extra-directory", "extra-socket", "duplicate", "duplicate-control"} { + t.Run(mode, func(t *testing.T) { + bad := append([]gitDescriptorObservation(nil), rows...) + switch mode { + case "missing-standard": + bad = bad[1:] + case "stdio-socket": + bad[0].Mode = unix.S_IFSOCK + case "stdio-read": + bad[1].Flags = unix.O_RDONLY + case "inheritable": + bad[3].Flags &^= unix.O_CLOEXEC + case "wrong-export": + bad[3].File.Inode++ + case "export-read": + bad[3].Flags = unix.O_CLOEXEC | unix.O_RDONLY + case "wrong-supervisor": + bad[5].File.Inode++ + case "wrong-peer": + bad[4].Credentials.Pid++ + case "internet": + bad[4].SocketDomain = unix.AF_INET + case "wrong-control-path": + bad[4].SocketPeer = "/broker/git.sock" + case "unknown-anon": + bad[6].Target = "anon_inode:[io_uring]" + case "extra-regular": + bad = append(bad, gitDescriptorObservation{Number: 11, Flags: unix.O_CLOEXEC, Mode: unix.S_IFREG}) + case "extra-directory": + bad = append(bad, gitDescriptorObservation{Number: 11, Flags: unix.O_CLOEXEC, Mode: unix.S_IFDIR}) + case "extra-socket": + bad = append(bad, gitDescriptorObservation{Number: 11, Flags: unix.O_CLOEXEC, Mode: unix.S_IFSOCK}) + case "duplicate": + bad[7].Number = bad[6].Number + case "duplicate-control": + extra := bad[4] + extra.Number = 11 + bad = append(bad, extra) + } + if validateGitDescriptors(bad, export, supervisor, "/broker/worker.sock") == nil { + t.Fatal("extra/missing capability accepted") + } + }) + } +} + +func TestGitDescriptorOriginalPidfdAndNoLocalDescriptorMutation(t *testing.T) { + // Self-observation is deliberately refused if the descriptor list changes + // due to inspection's own temporary handles. It is not a worker fixture. + child := exec.Command("/usr/bin/sleep", "10") + child.Env = []string{} + if child.Start() != nil { + t.Fatal("inert descriptor child") + } + defer func() { child.Process.Kill(); child.Wait() }() + // Start returns after exec, before the dynamic loader necessarily closes + // its temporary library handles. Inspect only the inert sleeping state. + deadline := time.Now().Add(2 * time.Second) + for { + state, err := os.ReadFile(fmt.Sprintf("/proc/%d/wchan", child.Process.Pid)) + if err != nil { + t.Fatal(err) + } + if strings.Contains(string(state), "nanosleep") { + break + } + if time.Now().After(deadline) { + t.Fatal("inert child never reached its kernel sleep") + } + time.Sleep(time.Millisecond) + } + identity, err := processGateIdentity(child.Process.Pid, strings.Repeat("b", 32)) + if err != nil { + t.Fatal(err) + } + pidfd, err := unix.PidfdOpen(child.Process.Pid, 0) + if err != nil { + t.Fatal(err) + } + defer unix.Close(pidfd) + before := gitPacketFDCount(t) + rows, err := observeGitDescriptors(context.Background(), identity, pidfd) + if err != nil || len(rows) != 3 { + t.Fatalf("actual inert child descriptor read: %+v %v", rows, err) + } + for _, row := range rows { + if row.Number > 2 || row.Mode&unix.S_IFMT != unix.S_IFCHR || row.Device != unix.Mkdev(1, 3) { + t.Fatalf("actual null standard stream: %+v", row) + } + } + bad := identity + bad.StartTicks++ + if rows, err := observeGitDescriptors(context.Background(), bad, pidfd); err == nil || rows != nil { + t.Fatal("substituted process accepted") + } + ctx, cancel := context.WithCancel(context.Background()) + cancel() + if rows, err := observeGitDescriptors(ctx, identity, pidfd); err == nil || rows != nil { + t.Fatal("canceled observation accepted") + } + if after := gitPacketFDCount(t); before != after { + t.Fatalf("descriptor leak: %d -> %d", before, after) + } +} diff --git a/reviewer/git_mount_boundary_linux_test.go b/reviewer/git_mount_boundary_linux_test.go index 42b71439..de3e3070 100644 --- a/reviewer/git_mount_boundary_linux_test.go +++ b/reviewer/git_mount_boundary_linux_test.go @@ -11,6 +11,7 @@ import ( "os" "os/exec" "path/filepath" + "strconv" "strings" "testing" "time" @@ -181,6 +182,21 @@ func TestGitMountNamespaceChild(t *testing.T) { t.Fatal(err) } defer conn.Close() + if mode := os.Getenv("SFL_GIT_DESCRIPTOR_CHILD"); mode != "" { + supervisor := os.NewFile(3, "fixture-supervisor") + peer, err := authenticateGitSupervisor(ctx, conn, supervisor) + supervisor.Close() + if err != nil { + t.Fatal("fixture supervisor", err) + } + defer peer.Close() + unix.CloseOnExec(4) + if mode == "clean" { + unix.Close(5) + } else if mode != "inheritable" { + unix.CloseOnExec(5) + } + } if unix.Prctl(unix.PR_SET_DUMPABLE, 0, 0, 0, 0) != nil || writeGitControlPacket(ctx, conn, []byte("ready"), nil) != nil { t.Fatal("inert mount readiness") } @@ -245,6 +261,20 @@ func gitMountInertRuntime(t *testing.T, ctx context.Context) (*os.File, *os.File } func TestGitMountActualPrivateViewRefusesUnqualifiedWorkspace(t *testing.T) { + gitMountPrivateFixture(t, "") +} + +func TestGitDescriptorActualPrivateWorker(t *testing.T) { + if os.Getenv("SFL_GIT_DESCRIPTOR_KERNEL") != "1" { + t.Skip("explicit aggregate descriptor qualification using actual private worker roles") + } + for _, mode := range []string{"clean", "host-file", "host-directory", "internet-socket", "inheritable"} { + t.Run(mode, func(t *testing.T) { gitMountPrivateFixture(t, mode) }) + } +} + +func gitMountPrivateFixture(t *testing.T, descriptorMode string) { + t.Helper() if os.Getenv("SFL_GIT_BOOTSTRAP_NAMESPACE") != "1" { t.Skip("explicit inert private-namespace mount observation") } @@ -300,8 +330,49 @@ func TestGitMountActualPrivateViewRefusesUnqualifiedWorkspace(t *testing.T) { command := exec.CommandContext(ctx, fmt.Sprintf("/proc/%d/fd/%d", os.Getpid(), helper.Fd()), args...) command.Env = []string{} command.ExtraFiles = []*os.File{root, broker, filter, work, alias} + var exportRead, supervisor *os.File + if descriptorMode != "" { + var exportWrite *os.File + exportRead, exportWrite, err = os.Pipe() + if err != nil { + t.Fatal(err) + } + defer exportRead.Close() + defer exportWrite.Close() + fd, err := unix.PidfdOpen(os.Getpid(), 0) + if err != nil { + t.Fatal(err) + } + supervisor = os.NewFile(uintptr(fd), "fixture-supervisor") + defer supervisor.Close() + var extra *os.File + switch descriptorMode { + case "host-directory": + extra, err = os.Open(t.TempDir()) + case "internet-socket": + var fd int + fd, err = unix.Socket(unix.AF_INET, unix.SOCK_STREAM|unix.SOCK_CLOEXEC, 0) + if err == nil { + extra = os.NewFile(uintptr(fd), "fixture-inherited-internet") + } + default: + extra, err = os.CreateTemp(t.TempDir(), "outside-worker-") + } + if err != nil { + t.Fatal(err) + } + defer extra.Close() + args = []string{"--unshare-all", "--unshare-user", "--disable-userns", "--die-with-parent", "--new-session", "--cap-drop", "ALL", "--clearenv", "--setenv", "SFL_GIT_MOUNT_CHILD", "1", "--setenv", "SFL_GIT_DESCRIPTOR_CHILD", descriptorMode, "--ro-bind-fd", "6", "/", "--ro-bind-fd", "7", "/broker", "--bind-fd", "9", "/work", "--bind-fd", "10", "/tmp", "--proc", "/proc", "--remount-ro", "/proc", "--dev-bind", "/dev/null", "/dev/null", "--dev-bind", "/dev/urandom", "/dev/urandom", "--remount-ro", "/dev/urandom", "--chdir", "/", "--seccomp", "8", "--", "/usr/bin/git", "-test.run=^TestGitMountNamespaceChild$"} + command = exec.CommandContext(ctx, fmt.Sprintf("/proc/%d/fd/%d", os.Getpid(), helper.Fd()), args...) + command.Env = []string{} + command.ExtraFiles = []*os.File{supervisor, exportWrite, extra, root, broker, filter, work, alias} + // The fixed production launcher uses null standard streams. Preserve + // that actual descriptor policy in this inert fixture as well. + } var output bytes.Buffer - command.Stdout, command.Stderr = &output, &output + if descriptorMode == "" { + command.Stdout, command.Stderr = &output, &output + } if command.Start() != nil { t.Fatal("inert mount namespace launch") } @@ -365,6 +436,37 @@ func TestGitMountActualPrivateViewRefusesUnqualifiedWorkspace(t *testing.T) { t.Fatal("actual process boundary", err) } defer boundary.Close() + if descriptorMode != "" { + rows, err := observeGitDescriptors(ctx, identity, original) + if err != nil { + duplicate, duplicateErr := unix.PidfdGetfd(original, 0, 0) + if duplicateErr == nil { + unix.Close(duplicate) + } + t.Log("actual original-pidfd standard-input duplication", duplicateErr) + names, readErr := os.ReadDir(fmt.Sprintf("/proc/%d/fd", identity.PID)) + t.Log("actual fd directory access", readErr) + for _, name := range names { + fd, parseErr := strconv.Atoi(name.Name()) + if parseErr != nil { + t.Fatal(parseErr) + } + duplicate, duplicateErr := unix.PidfdGetfd(original, fd, 0) + if duplicateErr == nil { + unix.Close(duplicate) + } + t.Log("actual original-pidfd descriptor duplication", fd, duplicateErr) + } + t.Fatal("actual descriptor observation", err) + } + result := checkGitDescriptorBoundary(ctx, boundary, exportRead, supervisor, conn) + if descriptorMode == "clean" && result != nil { + t.Fatalf("fixed descriptor roles refused: %v\n%+v", result, rows) + } + if descriptorMode != "clean" && result == nil { + t.Fatalf("inherited %s accepted: %+v", descriptorMode, rows) + } + } binding, _ := gitMountTableFixture(t) if checkGitMountBoundary(ctx, boundary, broker, binding) == nil { t.Fatal("unqualified host workspace became a source admission proof") From 848d1deb5c14444d814ad83eefde111f31a897e7 Mon Sep 17 00:00:00 2001 From: Franz Hemmer <8227352+HemSoft@users.noreply.github.com> Date: Wed, 21 Oct 2026 11:29:33 -0400 Subject: [PATCH 04/12] Compose fixed Git acquisition and retained source recovery --- reviewer/GIT-DESCRIPTOR-BOUNDARY.md | 2 +- reviewer/GIT-EXPORT-ARCHIVE.md | 15 + reviewer/GIT-FACTORY.md | 128 +++++ reviewer/GIT-MOUNT-BOUNDARY.md | 6 +- reviewer/git_export_archive_linux.go | 13 +- reviewer/git_factory_acquisition_linux.go | 67 +++ .../git_factory_acquisition_linux_test.go | 172 +++++++ reviewer/git_factory_evidence_linux.go | 42 ++ reviewer/git_factory_recovery_linux.go | 116 +++++ reviewer/git_factory_recovery_linux_test.go | 115 +++++ reviewer/git_factory_run_linux.go | 447 ++++++++++++++++++ reviewer/git_factory_run_linux_test.go | 275 +++++++++++ reviewer/git_factory_unit_linux.go | 198 ++++++++ reviewer/git_factory_unit_linux_test.go | 70 +++ reviewer/git_source_factory_linux.go | 273 +++++++++++ reviewer/git_source_factory_linux_test.go | 137 ++++++ reviewer/git_unit_grant_linux.go | 8 +- reviewer/snapshot_evidence.go | 23 +- 18 files changed, 2089 insertions(+), 18 deletions(-) create mode 100644 reviewer/GIT-FACTORY.md create mode 100644 reviewer/git_factory_acquisition_linux.go create mode 100644 reviewer/git_factory_acquisition_linux_test.go create mode 100644 reviewer/git_factory_evidence_linux.go create mode 100644 reviewer/git_factory_recovery_linux.go create mode 100644 reviewer/git_factory_recovery_linux_test.go create mode 100644 reviewer/git_factory_run_linux.go create mode 100644 reviewer/git_factory_run_linux_test.go create mode 100644 reviewer/git_factory_unit_linux.go create mode 100644 reviewer/git_factory_unit_linux_test.go create mode 100644 reviewer/git_source_factory_linux.go create mode 100644 reviewer/git_source_factory_linux_test.go diff --git a/reviewer/GIT-DESCRIPTOR-BOUNDARY.md b/reviewer/GIT-DESCRIPTOR-BOUNDARY.md index a647aecd..0a2f6034 100644 --- a/reviewer/GIT-DESCRIPTOR-BOUNDARY.md +++ b/reviewer/GIT-DESCRIPTOR-BOUNDARY.md @@ -1,7 +1,7 @@ # Git worker descriptor observation The private descriptor observer supplements process and mount checks before the -unfinished factory grants work. It returns no source or reusable admission +private [factory](GIT-FACTORY.md) grants work. It returns no source or reusable admission capability. The caller retains the original worker pidfd and rechecks its identity before and after observation. diff --git a/reviewer/GIT-EXPORT-ARCHIVE.md b/reviewer/GIT-EXPORT-ARCHIVE.md index 6bfb525e..55a0dbce 100644 --- a/reviewer/GIT-EXPORT-ARCHIVE.md +++ b/reviewer/GIT-EXPORT-ARCHIVE.md @@ -93,3 +93,18 @@ qualification, physical-stop/recovery authority, generation leases, controller composition and live activation remain independent work. See [GIT-MATERIALIZATION.md](GIT-MATERIALIZATION.md), [GIT-UNIT.md](GIT-UNIT.md) and [GIT-RUNTIME.md](GIT-RUNTIME.md). + +## Factory recovery integration + +The private [factory](GIT-FACTORY.md) now binds staged exports to the exact +unit journal and bootstrap/worker grants before restored source can escape. +An incomplete receiving intake may become terminal `discarded` only after +the concrete owner independently proves physical whole-unit stop. That receipt +is fsynced before removing the bounded owned payload; a restart can complete +cleanup. The nonce stays spent and discarded intake cannot reopen as source. +A different nonce may stage after that recovery. Complete staged source is +retained. This is no public force-delete or arbitrary retry interface. + +Close disposes both lock and root descriptors once intake has joined, records +terminal state even if disposal reports an error, and reports that initial +error. A timeout acquiring the operation token still retains actual ownership. diff --git a/reviewer/GIT-FACTORY.md b/reviewer/GIT-FACTORY.md new file mode 100644 index 00000000..170c807a --- /dev/null +++ b/reviewer/GIT-FACTORY.md @@ -0,0 +1,128 @@ +# Git acquisition factory + +The private source factory composes the existing acquisition unit, pinned +credential generation, measured runtime, authenticated bootstrap, worker and +plain export archive. Its `Capture` and `Restore` methods fit the polling +controller callbacks. The trusted service must construct and retain the factory; +this change does not install a service, import credentials or activate polling. + +Construction takes exclusive lifecycle ownership of a newly opened unit owner, +export archive and prepared runtime. It validates the fixed manifest/helper and +runtime pins, then performs startup recovery before requesting any credential +generation. The composing service must call this before credential Refresh and +retain the factory until controller work has joined. Credentials remain shared +with the publisher; the source factory never exposes a raw token or refresh +capability to a worker or broker. + +## Capture and grant order + +Each capture spends a random 128-bit attempt nonce. Its binding fixes the +release hash, canonical runtime-manifest hash and immutable PR head/base/merge +base. A fresh private attempt directory contains separate bootstrap and broker +sockets. No failed or uncertain attempt automatically starts again. + +The factory seals the fixed non-executable manifest, duplicates measured runtime +and helper handles, and retains the original sealed worker image and supervisor +pidfd from the unit owner. The bootstrap receives only the existing eight +descriptor roles. The broker has the typed reader for the one pinned credential +generation; it has no publisher, refresh, raw token or arbitrary HTTP interface. + +After the broker is listening, the factory starts the fixed unit once and +authenticates the bootstrap's canonical hello against its actual original peer +pidfd. The unit owner verifies the root, manager, invocation, exact cgroup and +fixed resource properties and fsyncs the bootstrap grant before sending it. +The parent then closes its export write endpoint so it cannot manufacture a +missing EOF. + +Worker readiness is a canonical packet bound to that same manifest and attempt. +Before the worker grant, the factory checks: + +- The original live root and unchanged manager, invocation, unit object path, + resource policy and independently captured cgroup. +- A retained ancestor chain from the original worker through only the measured + namespace helper to the original unit root. Every retained ancestor maps to + the exact unit and invocation; parent edges and executable inodes are rechecked. +- The measured worker/runtime, six private namespaces, capability/map/network + policy and original peer identity in the existing process boundary. +- The actual fixed mount/device/private-proc layout and workspace limits in + [mount observation](GIT-MOUNT-BOUNDARY.md). +- Actual inherited objects, current CLOEXEC bits, exact export pipe, original + supervisor pidfd and the accepted control connection in + [descriptor observation](GIT-DESCRIPTOR-BOUNDARY.md). + +Installed-filter provenance comes from the measured fixed bootstrap and helper, +the sealed fixed filter in the authenticated bootstrap grant, and the fixed +worker launch path. `Seccomp=2` alone is not the filter's identity or a sandbox +qualification. The fixed code supplies no caller-selectable filter or argv. + +Each broker request separately checks measured Git/helper ancestry and fresh +unit ownership before any GitHub dispatch. Admission is a private concrete +composition. No exported option can replace these checks with a model claim or +caller-provided boolean. + +## Stop and publication + +The plain export is staged and fsynced while the acquisition still owns the +credential generation. Complete framing, valid hashes and EOF do not publish it. +The immutable stop capability first revokes work and cancels the acquisition, +then joins the sole broker, all its callbacks and the acquisition method. It +next proves whole-unit physical stop with the independent unit journal. Only +the credential owner can accept that proof and release the generation lease. +The factory closes retained run assets before returning a materialization. + +An early broker setup refusal with a joined zero-dispatch result has no live +broker to retain. A callback or serve goroutine that has not returned keeps +ownership. A lost start reply, populated group, failed physical stop or unknown +outcome keeps the generation, runtime and service locks for explicit recovery. +Close can retry that exact retained generation; it never replaces it with a +different credential pair or drops an unknown unit. + +Before returning SnapshotEvidence, capture reconstructs patch anchors from +verified plain blobs. It shares the same packet validation used for restoring +an admitted job, without inventing a job or running host Git. Restore matches +the exact admitted packet, snapshot, release and runtime, rechecks the durable +physical-stop journal and reopens the bounded archive into sealed blobs. There +is no network refetch, host Git parser, recapture or label-based fallback. + +## Restart recovery + +Startup joins the newly opened cross-process-locked unit owner with the export +archive. It independently stops every recorded old acquisition, then requires +exact binding equality between journals. A staged export needs both durable +bootstrap and worker grants from a captured unit. An orphan, different binding +or archive without worker admission refuses startup source recovery. + +An incomplete receiving frame can be discarded only after the concrete unit +owner proves physical stop. A canonical terminal `discarded` receipt is fsynced +before removing the bounded owned payload. Recovery can repeat after a crash; +the nonce remains spent, and discarded intake can never be reopened as source. +A later fresh nonce may stage after the incomplete attempt becomes terminal. +Staged admitted source is retained; this path does not discard a complete export. +All existing 128-record bounds remain. The factory retains its private working +directories as recovery artifacts; it does not recursively delete a replacement +or foreign directory. + +## Validation and remaining gates + +Offline tests use fake managers/cgroups and local synthetic credential transports. +Actual processes, pipes, original pidfds, flock, sealed files, archive fsync and +small trusted Git repositories are distinguished from those fake observations. +They cover stop-before-publication, unknown-start/stop retention, cancellation, +broker drain, unreturned broker work, setup refusal, exact live-unit rebinding, +interrupted intake retirement, orphan/missing-grant recovery refusal, startup +before credentials and retained packet/anchor restoration after deleting the +synthetic quarantine. The separate actual namespace fixtures measure mount and +descriptor subsets using an ordinary inert executable. + +The complete fixed unit has not passed physical qualification on mini. Its +required systemd noswap tmpfs fails before payload execution with +`226/NAMESPACE`, and sealed inner-worker execution independently fails with +`EACCES`. No production workspace fallback, policy relaxation or fabricated +qualification was added. Successful inert namespace checks do not waive these +failures. See [launch constraints](GIT-LAUNCH-CONSTRAINTS.md). + +Trusted service/release packaging and promotion, controller startup wiring, +provider public-target/account/auth/billing evidence, native-control evaluation, +rendered GitHub evidence and explicit owner authorization for live inference +and activation remain separate gates in #172, #173, #174, #176 and #177. This is +offline source implementation; it changes no App configuration or live service. diff --git a/reviewer/GIT-MOUNT-BOUNDARY.md b/reviewer/GIT-MOUNT-BOUNDARY.md index 87fff612..88f32cb1 100644 --- a/reviewer/GIT-MOUNT-BOUNDARY.md +++ b/reviewer/GIT-MOUNT-BOUNDARY.md @@ -1,10 +1,10 @@ # Git worker mount observation -The private mount observer adds a kernel check for the unfinished acquisition +The private mount observer adds a kernel check for the private acquisition factory. It returns no Git source capability, token, worker grant or publication -authority. The factory still has to compose exact unit/cgroup ownership, +authority. Full admission also requires exact unit/cgroup ownership, descriptor and installed-filter checks, authenticated export, broker drain and -physical stop. A caller's serialized mount table cannot qualify any of these. +physical stop, composed by the private [factory](GIT-FACTORY.md). A caller's serialized mount table cannot qualify any of these. The observer starts from an already retained process boundary and its original pidfd. It rechecks that worker before and after the operation. It reads bounded diff --git a/reviewer/git_export_archive_linux.go b/reviewer/git_export_archive_linux.go index c4b877a5..27710ab6 100644 --- a/reviewer/git_export_archive_linux.go +++ b/reviewer/git_export_archive_linux.go @@ -37,7 +37,7 @@ func (r gitExportRecord) valid() bool { return false } switch r.State { - case "receiving": + case "receiving", "discarded": return r.Bytes == 0 && r.WireHash == "" && r.PacketHash == "" case "staged": return r.Bytes > 0 && r.Bytes <= gitExportWireBytes && validAuthHash(r.WireHash) && validAuthHash(r.PacketHash) @@ -313,7 +313,7 @@ func (a *gitExportArchive) stage(ctx context.Context, binding gitMaterialization return nil, errGitExportArchive } for _, record := range records { - if record.State != "staged" || record.Binding.Attempt == binding.Attempt { + if record.State != "staged" && record.State != "discarded" || record.Binding.Attempt == binding.Attempt { return nil, errGitExportArchive } } @@ -446,12 +446,11 @@ func (a *gitExportArchive) Close(ctx context.Context) error { } // No intake/reader is active while this operation token is held. A timeout // acquiring it retains descriptors/flock for an explicit close retry. - if err := a.lock.Close(); err != nil { - return errGitExportArchive - } - if err := a.root.Close(); err != nil { + lockErr := a.lock.Close() + rootErr := a.root.Close() + a.closed = true + if lockErr != nil || rootErr != nil { return errGitExportArchive } - a.closed = true return nil } diff --git a/reviewer/git_factory_acquisition_linux.go b/reviewer/git_factory_acquisition_linux.go new file mode 100644 index 00000000..137b1d8a --- /dev/null +++ b/reviewer/git_factory_acquisition_linux.go @@ -0,0 +1,67 @@ +//go:build linux + +package reviewer + +import ( + "context" + "errors" + "time" +) + +// The only production implementation is gitFactoryRun. Unexported methods +// prevent an external caller from substituting an admission callback. Tests +// replace the lifecycle behind this private seam with explicitly fake units. +type gitFactoryAcquisition interface { + retainGeneration(*githubReaderGeneration) + acquire(context.Context, *gitGenerationReader) (*gitMaterialization, error) + stop(context.Context) error + close() error +} + +// The outer factory retains run in its attempt registry, including on error. +// An unknown stop keeps that run and the credential generation owned. A staged +// export never escapes while either the broker or acquisition unit is live. +func runGitFactoryAcquisition(ctx context.Context, credentials *GitHubCredentialOwner, binding gitMaterializationBinding, run gitFactoryAcquisition) (_ *gitMaterialization, result error) { + if ctx == nil || credentials == nil || !binding.valid() || run == nil { + return nil, errGitUnit + } + cleanup := func() (context.Context, context.CancelFunc) { + return context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second) + } + generation, err := credentials.retainReaderGeneration(ctx, run.stop) + if err != nil { + phase, cancel := cleanup() + defer cancel() + if run.stop(phase) == nil { + _ = run.close() + } + return nil, errGitUnit + } + run.retainGeneration(generation) + materialized, acquireErr := run.acquire(generation.ctx, &gitGenerationReader{generation: generation}) + good := false + defer func() { + if !good && materialized != nil { + materialized.Close() + } + }() + phase, cancel := cleanup() + defer cancel() + if generation.stop(phase) != nil { + return nil, errGitUnit + } + if run.close() != nil { + return nil, errGitUnit + } + if acquireErr != nil || materialized == nil || ctx.Err() != nil { + return nil, errors.Join(errGitUnit, ctx.Err()) + } + materialized.mu.Lock() + valid := !materialized.closed && materialized.manifest.Binding == binding + materialized.mu.Unlock() + if !valid { + return nil, errGitUnit + } + good = true + return materialized, nil +} diff --git a/reviewer/git_factory_acquisition_linux_test.go b/reviewer/git_factory_acquisition_linux_test.go new file mode 100644 index 00000000..586abc1d --- /dev/null +++ b/reviewer/git_factory_acquisition_linux_test.go @@ -0,0 +1,172 @@ +//go:build linux + +package reviewer + +import ( + "bytes" + "context" + "errors" + "io" + "testing" + "time" +) + +// Explicit fake lifecycle. Wire validation, archive fsync/sealed blobs and the +// credential owner are real; this never claims physical containment admission. +type fakeGitFactoryAcquisition struct { + archive *gitExportArchive + binding gitMaterializationBinding + wire []byte + materialized *gitMaterialization + stopEntered, allowStop chan struct{} + stopError error + closeError error + closed bool + acquireError error + generation *githubReaderGeneration +} + +func (f *fakeGitFactoryAcquisition) retainGeneration(g *githubReaderGeneration) { f.generation = g } + +func (f *fakeGitFactoryAcquisition) acquire(ctx context.Context, source *gitGenerationReader) (*gitMaterialization, error) { + f.generation = source.generation + m, err := f.archive.stage(ctx, f.binding, io.NopCloser(bytes.NewReader(f.wire))) + f.materialized = m + if err != nil { + return nil, err + } + return m, f.acquireError +} +func (f *fakeGitFactoryAcquisition) stop(ctx context.Context) error { + if f.stopEntered != nil { + close(f.stopEntered) + f.stopEntered = nil + } + if f.allowStop != nil { + select { + case <-ctx.Done(): + return ctx.Err() + case <-f.allowStop: + } + } + return f.stopError +} +func (f *fakeGitFactoryAcquisition) close() error { f.closed = true; return f.closeError } + +func TestGitFactoryStagedExportCannotPublishBeforeStopAndLeaseRelease(t *testing.T) { + wire, binding, _ := gitMaterializationFixture(t, "sha1") + archive, _ := exportArchiveFixture(t) + owner, _, _, _, _ := credentialOwnerFixture(t) + if owner.Refresh(context.Background()) != nil { + t.Fatal("fake pair qualification") + } + entered, allow := make(chan struct{}), make(chan struct{}) + f := &fakeGitFactoryAcquisition{archive: archive, binding: binding, wire: wire, stopEntered: entered, allowStop: allow} + type result struct { + m *gitMaterialization + err error + } + done := make(chan result, 1) + go func() { + m, err := runGitFactoryAcquisition(context.Background(), owner, binding, f) + done <- result{m, err} + }() + select { + case <-entered: + case <-time.After(3 * time.Second): + t.Fatal("staged export never reached stop") + } + select { + case result := <-done: + if result.m != nil { + result.m.Close() + } + t.Fatal("staged output escaped before physical stop", result.err) + default: + } + owner.state.mu.Lock() + owned, active := len(owner.state.acquisitions), len(owner.state.active) + owner.state.mu.Unlock() + if owned != 1 || active != 0 { + t.Fatal("EOF retired whole generation before physical stop", owned, active) + } + if f.materialized == nil { + t.Fatal("test never staged a complete valid export") + } + close(allow) + resultValue := <-done + if resultValue.err != nil || resultValue.m == nil || !f.closed { + t.Fatal("proven stop did not publish", resultValue.err) + } + defer resultValue.m.Close() + owner.state.mu.Lock() + owned = len(owner.state.acquisitions) + owner.state.mu.Unlock() + if owned != 0 { + t.Fatal("published source still owns acquisition lease") + } + if resultValue.m.manifest.Binding != binding || resultValue.m.packet == "" { + t.Fatal("published different or empty source") + } +} + +func TestGitFactoryUnknownStopRetainsLeaseAndClosesStagedObjects(t *testing.T) { + wire, binding, _ := gitMaterializationFixture(t, "sha256") + archive, _ := exportArchiveFixture(t) + owner, _, _, _, _ := credentialOwnerFixture(t) + if owner.Refresh(context.Background()) != nil { + t.Fatal("fake pair qualification") + } + f := &fakeGitFactoryAcquisition{archive: archive, binding: binding, wire: wire, stopError: errors.New("fake live broker/unit")} + if m, err := runGitFactoryAcquisition(context.Background(), owner, binding, f); err == nil || m != nil { + t.Fatal("unknown stop published source") + } + owner.state.mu.Lock() + owned := len(owner.state.acquisitions) + owner.state.mu.Unlock() + if owned != 1 || f.closed || f.materialized == nil || !f.materialized.closed || len(f.materialized.files) != 0 { + t.Fatal("unknown stop lost lease or staged descriptors", owned, f.closed) + } + f.stopError = nil + if f.generation.stop(context.Background()) != nil { + t.Fatal("explicit fake recovery failed") + } + if f.close() != nil { + t.Fatal("recovered fixture did not close") + } +} + +func TestGitFactoryCleanupOrAcquisitionFailureNeverPublishes(t *testing.T) { + for _, mode := range []string{"close", "acquire", "binding"} { + t.Run(mode, func(t *testing.T) { + wire, binding, _ := gitMaterializationFixture(t, "sha1") + archive, _ := exportArchiveFixture(t) + owner, _, _, _, _ := credentialOwnerFixture(t) + if owner.Refresh(context.Background()) != nil { + t.Fatal("fake pair qualification") + } + f := &fakeGitFactoryAcquisition{archive: archive, binding: binding, wire: wire} + expected := binding + switch mode { + case "close": + f.closeError = errors.New("fake cleanup failure") + case "acquire": + f.acquireError = errors.New("fake bad final worker status") + case "binding": + expected.Attempt = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" + } + if m, err := runGitFactoryAcquisition(context.Background(), owner, expected, f); err == nil || m != nil { + t.Fatal("failed acquisition published source") + } + if !f.closed || f.materialized == nil || !f.materialized.closed { + t.Fatal("failed publication leaked staged handles") + } + owner.state.mu.Lock() + owned := len(owner.state.acquisitions) + owner.state.mu.Unlock() + if owned != 0 { + t.Fatal("proven stopped failure retained generation") + } + }) + } +} diff --git a/reviewer/git_factory_evidence_linux.go b/reviewer/git_factory_evidence_linux.go new file mode 100644 index 00000000..8b85f3d1 --- /dev/null +++ b/reviewer/git_factory_evidence_linux.go @@ -0,0 +1,42 @@ +//go:build linux + +package reviewer + +import ( + "context" + "strings" +) + +// The factory calls this only after the acquisition's broker/unit stop and +// generation release. It reconstructs inline anchors without executing Git or +// trusting the worker's patch text as its own line-eligibility authority. +func captureGitMaterializationEvidence(ctx context.Context, m *gitMaterialization) (*SnapshotEvidence, error) { + if ctx == nil || ctx.Err() != nil || m == nil { + return nil, errGitMaterialization + } + m.mu.Lock() + if m.closed { + m.mu.Unlock() + return nil, errGitMaterialization + } + binding, packet := m.manifest.Binding, m.packet + p, err := validateGitMaterialization(m.manifest, []byte(packet), binding) + m.mu.Unlock() + if err != nil { + return nil, errGitMaterialization + } + inventory := append([]snapshotFile(nil), p.Files...) + for i := range inventory { + inventory[i].Patch = "" + } + return restoreSnapshotPacket(ctx, m, binding.Snapshot, packet, inventory, func(id string, remaining *int) ([]byte, error) { + if strings.Trim(id, "0") == "" { + return nil, nil + } + data, err := m.blob(ctx, id, *remaining) + if err == nil { + *remaining -= len(data) + } + return data, err + }) +} diff --git a/reviewer/git_factory_recovery_linux.go b/reviewer/git_factory_recovery_linux.go new file mode 100644 index 00000000..9f982e0f --- /dev/null +++ b/reviewer/git_factory_recovery_linux.go @@ -0,0 +1,116 @@ +//go:build linux + +package reviewer + +import ( + "context" + "os" + + "golang.org/x/sys/unix" +) + +// Terminally discard only incomplete plain intake, after the concrete owner +// independently proves whole-unit stop. A terminal receipt is fsynced before +// removing the bounded payload, so interruption never makes a missing archive +// record look like permission to recapture the spent attempt. +func (a *gitExportArchive) discardStoppedIntake(ctx context.Context, unit *gitUnitOwner, binding gitMaterializationBinding) error { + if ctx == nil || unit == nil || !binding.valid() || unit.stop(ctx, binding) != nil { + return errGitExportArchive + } + if !a.enter(ctx) { + return errGitExportArchive + } + defer a.leave() + root, err := a.attempt(binding.Attempt) + if err != nil { + return errGitExportArchive + } + defer root.Close() + record, err := readGitExportRecord(root) + if err != nil || record.Binding != binding || record.State != "receiving" && record.State != "discarded" { + return errGitExportArchive + } + var payload *os.File + if _, err := root.Lstat("payload.bin"); err == nil { + payload, err = root.OpenFile("payload.bin", os.O_RDONLY|unix.O_NOFOLLOW|unix.O_NONBLOCK, 0) + if err != nil { + return errGitExportArchive + } + defer payload.Close() + if privateAuthFile(payload) != nil { + return errGitExportArchive + } + var stat unix.Stat_t + if unix.Fstat(int(payload.Fd()), &stat) != nil || stat.Nlink != 1 || stat.Size < 0 || stat.Size > gitExportWireBytes { + return errGitExportArchive + } + } else if !os.IsNotExist(err) { + return errGitExportArchive + } + if a.verifyAttempt(root, binding.Attempt) != nil || ctx.Err() != nil { + return errGitExportArchive + } + record.State = "discarded" + if writeGitExportRecord(root, record) != nil { + return errGitExportArchive + } + if payload != nil { + opened, err := payload.Stat() + current, pathErr := root.Lstat("payload.bin") + if err != nil || pathErr != nil || !os.SameFile(opened, current) || root.Remove("payload.bin") != nil || syncAuthDirectory(root) != nil { + return errGitExportArchive + } + } + if a.verifyAttempt(root, binding.Attempt) != nil || ctx.Err() != nil { + return errGitExportArchive + } + return nil +} + +// Reopened owners never re-arm grants or re-fetch missing source. The outer +// factory must call recovery before credential Refresh/Ready and before any +// source exposure. Cross-journal equality binds retained export to its exact +// nonce, release, runtime and immutable snapshot, not merely its PR number. +func recoverGitFactory(ctx context.Context, unit *gitUnitOwner, archive *gitExportArchive) (map[string]gitMaterializationBinding, error) { + if ctx == nil || unit == nil || archive == nil || !unit.enter(ctx) { + return nil, errGitUnit + } + records, err := unit.records() + unit.leave() + if err != nil { + return nil, errGitUnit + } + byAttempt := map[string]gitUnitRecord{} + for _, record := range records { + if unit.stop(ctx, record.Binding) != nil { + return nil, errGitUnit + } + byAttempt[record.Binding.Attempt] = record + } + if !archive.enter(ctx) { + return nil, errGitUnit + } + exports, err := archive.records() + archive.leave() + if err != nil { + return nil, errGitUnit + } + retained := map[string]gitMaterializationBinding{} + for _, record := range exports { + unitRecord, found := byAttempt[record.Binding.Attempt] + if !found || unitRecord.Binding != record.Binding { + return nil, errGitUnit + } + if record.State == "receiving" || record.State == "discarded" { + if archive.discardStoppedIntake(ctx, unit, record.Binding) != nil { + return nil, errGitUnit + } + continue + } + if record.State != "staged" || !unitRecord.Captured || unitRecord.BootstrapGrantHash == "" || unitRecord.WorkerGrantHash == "" { + return nil, errGitUnit + } + retained[record.Binding.Attempt] = record.Binding + } + return retained, nil +} diff --git a/reviewer/git_factory_recovery_linux_test.go b/reviewer/git_factory_recovery_linux_test.go new file mode 100644 index 00000000..6b9af39a --- /dev/null +++ b/reviewer/git_factory_recovery_linux_test.go @@ -0,0 +1,115 @@ +//go:build linux + +package reviewer + +import ( + "bytes" + "context" + "io" + "os" + "path/filepath" + "strings" + "testing" +) + +func TestGitFactoryRecoveryDiscardsIncompleteIntakeOnlyAfterStop(t *testing.T) { + f := newGitUnitFixture(t) + f.prepareStart(t) + a, dir := exportArchiveFixture(t) + if m, err := a.stage(context.Background(), f.binding, io.NopCloser(strings.NewReader("partial"))); err == nil || m != nil { + t.Fatal("partial export unexpectedly staged") + } + f.manager.holdPopulated = true + if a.discardStoppedIntake(context.Background(), f.owner, f.binding) == nil { + t.Fatal("populated group allowed discard") + } + attempt, err := a.attempt(f.binding.Attempt) + if err != nil { + t.Fatal(err) + } + record, err := readGitExportRecord(attempt) + attempt.Close() + if err != nil || record.State != "receiving" { + t.Fatal("failed stop changed intake receipt", record, err) + } + f.manager.holdPopulated = false + if a.discardStoppedIntake(context.Background(), f.owner, f.binding) != nil { + t.Fatal("known stopped intake could not be discarded") + } + if _, err := os.Stat(filepath.Join(dir, gitExportDirectory(f.binding.Attempt), "payload.bin")); !os.IsNotExist(err) { + t.Fatal("discarded payload remains", err) + } + if m, err := a.reopen(context.Background(), f.binding); err == nil || m != nil { + t.Fatal("discarded intake became source") + } + if a.discardStoppedIntake(context.Background(), f.owner, f.binding) != nil { + t.Fatal("discard was not restart-idempotent") + } + if m, err := a.stage(context.Background(), f.binding, io.NopCloser(bytes.NewReader(nil))); err == nil || m != nil { + t.Fatal("spent nonce was recaptured") + } + _, next, _ := gitMaterializationFixture(t, "sha1") + next.Attempt = strings.Repeat("c", 32) + // A different nonce can reach intake again after recovery. Deliberately bad + // bytes create a distinct receiving record, proving it passed admission. + _, _ = a.stage(context.Background(), next, io.NopCloser(strings.NewReader("bad"))) + if _, err := os.Stat(filepath.Join(dir, gitExportDirectory(next.Attempt), "archive.json")); err != nil { + t.Fatal("terminal discarded receipt blocked all later nonces", err) + } +} + +func TestGitFactoryRecoveryRefusesStagedWireWithoutWorkerGrant(t *testing.T) { + wire, binding, _ := gitMaterializationFixture(t, "sha1") + f := newGitUnitFixture(t) + f.binding = binding + if f.owner.prepare(context.Background(), binding) != nil { + t.Fatal("prepare") + } + a, _ := exportArchiveFixture(t) + m, err := a.stage(context.Background(), binding, io.NopCloser(bytes.NewReader(wire))) + if err != nil { + t.Fatal(err) + } + m.Close() + if retained, err := recoverGitFactory(context.Background(), f.owner, a); err == nil || retained != nil { + t.Fatal("archive alone manufactured producer admission") + } +} + +func TestGitFactoryRecoveryRefusesOrphanOrMismatchedArchive(t *testing.T) { + for _, mode := range []string{"orphan", "binding"} { + t.Run(mode, func(t *testing.T) { + wire, binding, _ := gitMaterializationFixture(t, "sha1") + f := newGitUnitFixture(t) + a, _ := exportArchiveFixture(t) + if mode == "binding" { + f.binding = binding + f.binding.ReleaseHash = Digest("different release") + if f.owner.prepare(context.Background(), f.binding) != nil { + t.Fatal("prepare") + } + } + m, err := a.stage(context.Background(), binding, io.NopCloser(bytes.NewReader(wire))) + if err != nil { + t.Fatal(err) + } + m.Close() + if retained, err := recoverGitFactory(context.Background(), f.owner, a); err == nil || retained != nil { + t.Fatal("orphan/mismatched source recovered") + } + }) + } +} + +func TestGitFactoryArchiveClosedDescriptorIsTerminal(t *testing.T) { + a, _ := exportArchiveFixture(t) + if a.lock.Close() != nil { + t.Fatal("close actual lock descriptor") + } + if a.Close(context.Background()) == nil || !a.closed { + t.Fatal("failed close retained fictional ownership") + } + if a.Close(context.Background()) != nil { + t.Fatal("terminal close was not idempotent") + } +} diff --git a/reviewer/git_factory_run_linux.go b/reviewer/git_factory_run_linux.go new file mode 100644 index 00000000..24bd768a --- /dev/null +++ b/reviewer/git_factory_run_linux.go @@ -0,0 +1,447 @@ +//go:build linux + +package reviewer + +import ( + "bytes" + "context" + "encoding/json" + "net" + "os" + "path/filepath" + "sync" + "time" + + "golang.org/x/sys/unix" +) + +// Private fixed composition only. Neither a model nor an exported launcher can +// replace these checks with an Admit callback or supply qualification flags. +type gitFactoryRun struct { + mu sync.Mutex + unit *gitUnitOwner + archive *gitExportArchive + binding gitMaterializationBinding + manifest gitGrantManifest + directory string + bootstrap, worker *net.UnixListener + files []*os.File + exportRead *os.File + boundary *gitProcessBoundary + workerIdentity GateIdentity + generation *githubReaderGeneration + spent, revoked, stopped, closed bool + cancel context.CancelFunc + acquireDone, brokerDone chan struct{} + brokerAudit GitReadBrokerAudit + stopOperation chan struct{} +} + +func (r *gitFactoryRun) retainGeneration(g *githubReaderGeneration) { + r.mu.Lock() + r.generation = g + r.mu.Unlock() +} + +func retainGitFactoryManifest(binding gitMaterializationBinding, m gitGrantManifest) (*os.File, error) { + if !m.valid(binding) { + return nil, errGitUnit + } + raw, err := json.Marshal(m) + if err != nil || len(raw) > gitGrantManifestBytes { + return nil, errGitUnit + } + fd, err := unix.MemfdCreate("sfl-git-factory-manifest", unix.MFD_CLOEXEC|unix.MFD_ALLOW_SEALING|unix.MFD_NOEXEC_SEAL) + if err != nil { + return nil, errGitUnit + } + file := os.NewFile(uintptr(fd), "sfl-git-factory-manifest") + good := false + defer func() { + if !good { + file.Close() + } + }() + if n, err := file.Write(raw); err != nil || n != len(raw) || file.Chmod(0400) != nil { + return nil, errGitUnit + } + if _, err := unix.FcntlInt(file.Fd(), unix.F_ADD_SEALS, requiredStubSeals); err != nil { + return nil, errGitUnit + } + if _, err := file.Seek(0, 0); err != nil { + return nil, errGitUnit + } + good = true + return file, nil +} + +// directory is an existing empty private attempt directory owned by the outer +// factory's durable registry. Runtime/helper handles are duplicated; the outer +// factory must retain their protected backing until this run is physically +// stopped. Construction never starts a unit or dispatches GitHub HTTP. +func newGitFactoryRun(ctx context.Context, unit *gitUnitOwner, archive *gitExportArchive, binding gitMaterializationBinding, m gitGrantManifest, runtime, helper *os.File, directory string) (_ *gitFactoryRun, result error) { + if ctx == nil || ctx.Err() != nil || unit == nil || archive == nil || runtime == nil || helper == nil || !m.valid(binding) || !filepath.IsAbs(directory) || filepath.Clean(directory) != directory || len(directory) > 70 || privateDirectory(directory) != nil { + return nil, errGitUnit + } + resolved, err := filepath.EvalSymlinks(directory) + if err != nil || resolved != directory { + return nil, errGitUnit + } + entries, err := os.ReadDir(directory) + if err != nil || len(entries) != 0 { + return nil, errGitUnit + } + if _, err := verifyGitRuntimeDirectory(ctx, runtime, m.Runtime); err != nil || verifyNativeBootstrap(ctx, helper, m.NamespaceHelper) != nil { + return nil, errGitUnit + } + r := &gitFactoryRun{unit: unit, archive: archive, binding: binding, manifest: m, directory: directory, files: make([]*os.File, 8), stopOperation: make(chan struct{}, 1)} + r.stopOperation <- struct{}{} + defer func() { + if result != nil { + r.disposeFiles() + } + }() + r.files[0], err = retainGitFactoryManifest(binding, m) + if err != nil { + return nil, errGitUnit + } + for _, item := range []struct { + index int + file *os.File + }{{1, runtime}, {2, helper}} { + r.files[item.index], err = duplicateGitControlFile(item.file) + if err != nil { + return nil, errGitUnit + } + } + if !unit.enter(ctx) { + return nil, errGitUnit + } + if !unit.closed && unit.image != nil && unit.image.file != nil && unit.stubHash == m.Worker.SHA256 { + r.files[3], err = duplicateGitControlFile(unit.image.file) + if err == nil { + r.files[7], err = duplicateGitControlFile(unit.supervisor) + } + } else { + err = errGitUnit + } + unit.leave() + if err != nil || verifyGrantedVMAsset(ctx, r.files[3], m.Worker, 0) != nil { + return nil, errGitUnit + } + r.files[4], err = retainGitPayloadSeccomp() + if err != nil { + return nil, errGitUnit + } + if os.Mkdir(filepath.Join(directory, "broker"), 0700) != nil { + return nil, errGitUnit + } + r.files[5], err = os.Open(filepath.Join(directory, "broker")) + if err != nil { + return nil, errGitUnit + } + r.exportRead, r.files[6], err = os.Pipe() + if err != nil { + return nil, errGitUnit + } + r.bootstrap, err = gitFactoryListen(filepath.Join(directory, "bootstrap.sock")) + if err != nil { + return nil, errGitUnit + } + if unit.prepare(ctx, binding) != nil { + return nil, errGitUnit + } + return r, nil +} + +func gitFactoryListen(path string) (*net.UnixListener, error) { + if len(path) > 107 { + return nil, errGitUnit + } + listener, err := net.ListenUnix("unixpacket", &net.UnixAddr{Name: path, Net: "unixpacket"}) + if err != nil { + return nil, errGitUnit + } + listener.SetUnlinkOnClose(false) + if os.Chmod(path, 0600) != nil { + listener.Close() + return nil, errGitUnit + } + return listener, nil +} + +func gitFactoryAccept(ctx context.Context, listener *net.UnixListener) (*net.UnixConn, error) { + if ctx.Err() != nil || listener == nil { + return nil, errGitUnit + } + joined := make(chan struct{}) + stop := context.AfterFunc(ctx, func() { defer close(joined); listener.Close() }) + defer func() { + if !stop() { + <-joined + } + }() + conn, err := listener.AcceptUnix() + if err != nil { + return nil, errGitUnit + } + if ctx.Err() != nil { + conn.Close() + return nil, errGitUnit + } + return conn, nil +} + +func (r *gitFactoryRun) admit(ctx context.Context, peer GitReadPeer) error { + r.mu.Lock() + boundary, worker, revoked := r.boundary, r.workerIdentity, r.revoked + r.mu.Unlock() + if revoked || boundary == nil || boundary.checkHelper(ctx, peer) != nil || r.unit.checkFactoryReadPeer(ctx, r.binding, worker, peer) != nil { + return ErrGitReadBroker + } + return nil +} + +// acquire stages plain output only. Returning successfully is not publication +// or stop authority. The composing owner must invoke stop and release its +// generation lease before any evidence or ObjectStore leaves the factory. +func (r *gitFactoryRun) acquire(ctx context.Context, source *gitGenerationReader) (*gitMaterialization, error) { + if ctx == nil || ctx.Err() != nil || source == nil || source.generation == nil { + return nil, errGitUnit + } + r.mu.Lock() + if r.spent || r.revoked || r.closed { + r.mu.Unlock() + return nil, errGitUnit + } + r.spent = true + phase, cancel := context.WithTimeout(ctx, 2*time.Minute) + r.cancel = cancel + r.acquireDone = make(chan struct{}) + r.brokerDone = make(chan struct{}) + done, brokerDone := r.acquireDone, r.brokerDone + r.mu.Unlock() + defer close(done) + defer cancel() + ready := make(chan error, 1) + go func() { + audit, _ := ServeGitReadSocket(phase, GitReadBrokerOptions{Directory: filepath.Join(r.directory, "broker"), Source: source, Admit: r.admit}, func(GitReadEndpoint) error { + listener, err := gitFactoryListen(filepath.Join(r.directory, "broker", "worker.sock")) + r.mu.Lock() + r.worker = listener + r.mu.Unlock() + ready <- err + return err + }) + r.mu.Lock() + r.brokerAudit = audit + r.mu.Unlock() + close(brokerDone) + }() + select { + case err := <-ready: + if err != nil { + return nil, errGitUnit + } + case <-brokerDone: + return nil, errGitUnit + case <-phase.Done(): + return nil, errGitUnit + } + if r.unit.start(phase, r.binding, filepath.Join(r.directory, "bootstrap.sock")) != nil { + return nil, errGitUnit + } + conn, err := gitFactoryAccept(phase, r.bootstrap) + if err != nil { + return nil, errGitUnit + } + if err = r.bootstrapGrant(phase, conn); err != nil { + conn.Close() + return nil, errGitUnit + } + conn.Close() + // The authenticated root owns its received duplicate. Keeping our local + // writer would prevent exact export EOF after the root exits. + if r.files[6].Close() != nil { + return nil, errGitUnit + } + r.files[6] = nil + r.mu.Lock() + listener := r.worker + r.mu.Unlock() + worker, err := gitFactoryAccept(phase, listener) + if err != nil { + return nil, errGitUnit + } + defer worker.Close() + if r.workerGrant(phase, worker) != nil { + return nil, errGitUnit + } + return r.archive.stage(phase, r.binding, r.exportRead) +} + +func (r *gitFactoryRun) bootstrapGrant(ctx context.Context, conn *net.UnixConn) error { + raw, files, err := readGitControlPacket(ctx, conn, 0) + closeVMFiles(files) + if err != nil { + return errGitUnit + } + hello, err := DecodeStrict[gitBootstrapHello](raw, gitControlPacketBytes) + canonical, marshalErr := json.Marshal(hello) + if err != nil || marshalErr != nil || !bytes.Equal(raw, canonical) || hello.Version != 1 || hello.Binding != r.binding || hello.ManifestHash != r.binding.RuntimeHash { + return errGitUnit + } + peer, err := gitBrokerPeer(conn) + if err != nil { + return errGitUnit + } + defer peer.kernel.Close() + identity, fd, err := retainGitBrokerPeer(peer, hello.Identity.InvocationID) + if err != nil { + return errGitUnit + } + defer unix.Close(fd) + if identity != hello.Identity { + return errGitUnit + } + return r.unit.grant(ctx, r.binding, gitBootstrapGrant, conn, r.files) +} + +func (r *gitFactoryRun) workerGrant(ctx context.Context, conn *net.UnixConn) error { + raw, files, err := readGitControlPacket(ctx, conn, 0) + closeVMFiles(files) + if err != nil { + return errGitUnit + } + ready, err := DecodeStrict[gitWorkerReady](raw, gitControlPacketBytes) + canonical, marshalErr := json.Marshal(ready) + if err != nil || marshalErr != nil || !bytes.Equal(raw, canonical) || ready != (gitWorkerReady{1, r.binding, r.binding.RuntimeHash}) { + return errGitUnit + } + if !r.unit.enter(ctx) { + return errGitUnit + } + record, err := r.unit.factoryUnit(ctx, r.binding) + r.unit.leave() + if err != nil { + return errGitUnit + } + peer, err := gitBrokerPeer(conn) + if err != nil { + return errGitUnit + } + defer peer.kernel.Close() + identity, fd, err := retainGitBrokerPeer(peer, record.Identity.InvocationID) + if err != nil { + return errGitUnit + } + defer unix.Close(fd) + if r.unit.checkFactoryLaunch(ctx, r.binding, identity, fd, r.files[2], r.manifest.NamespaceHelper) != nil { + return errGitUnit + } + boundary, err := retainGitProcessBoundary(ctx, identity, fd, r.files[1], r.files[3], r.manifest.Worker, r.manifest.Runtime) + if err != nil { + return errGitUnit + } + r.mu.Lock() + r.boundary = boundary + r.workerIdentity = identity + r.mu.Unlock() + if checkGitMountBoundary(ctx, boundary, r.files[5], r.binding) != nil || checkGitDescriptorBoundary(ctx, boundary, r.exportRead, r.files[7], conn) != nil { + return errGitUnit + } + return r.unit.grant(ctx, r.binding, gitWorkerGrant, conn, nil) +} + +// This is the immutable stop capability held by the credential generation. +// Cancellation first revokes future work, then joins broker callbacks and the +// acquisition method. Only then can physical whole-unit stop release the lease. +func (r *gitFactoryRun) stop(ctx context.Context) error { + if ctx == nil { + return errGitUnit + } + select { + case <-ctx.Done(): + return errGitUnit + case <-r.stopOperation: + } + defer func() { r.stopOperation <- struct{}{} }() + r.mu.Lock() + r.revoked = true + cancel, acquireDone, brokerDone := r.cancel, r.acquireDone, r.brokerDone + already := r.stopped + r.mu.Unlock() + if already { + return nil + } + if cancel != nil { + cancel() + } + for _, done := range []chan struct{}{brokerDone, acquireDone} { + if done == nil { + continue + } + select { + case <-ctx.Done(): + return errGitUnit + case <-done: + } + } + r.mu.Lock() + // ServeGitReadSocket can refuse setup before its accept loop exists. Its + // returned zero-dispatch audit plus the joined sole serve goroutine proves + // there was no broker work to retain; a missing return never does. + brokerStopped := brokerDone == nil || r.brokerAudit.Version == 1 && (r.brokerAudit.Stopped || r.brokerAudit.Connections == 0 && r.brokerAudit.Requests == 0) + r.mu.Unlock() + if !brokerStopped || r.unit.stop(ctx, r.binding) != nil || ctx.Err() != nil { + return errGitUnit + } + r.mu.Lock() + r.stopped = true + r.mu.Unlock() + return nil +} + +// Caller must hold lifecycle ownership. An uncertain unit or unreturned broker +// never loses its runtime/control descriptors just because cleanup was asked. +func (r *gitFactoryRun) close() error { + r.mu.Lock() + defer r.mu.Unlock() + if r.closed { + return nil + } + if !r.stopped { + return errGitUnit + } + r.closed = true + return r.disposeFiles() +} +func (r *gitFactoryRun) disposeFiles() error { + failed := false + if r.bootstrap != nil { + r.bootstrap.Close() + r.bootstrap = nil + } + if r.worker != nil { + r.worker.Close() + r.worker = nil + } + if r.boundary != nil { + if r.boundary.Close() != nil { + failed = true + } + r.boundary = nil + } + if closeVMFiles(r.files) != nil { + failed = true + } + r.files = nil + if r.exportRead != nil { + r.exportRead.Close() + r.exportRead = nil + } + if failed { + return errGitUnit + } + return nil +} diff --git a/reviewer/git_factory_run_linux_test.go b/reviewer/git_factory_run_linux_test.go new file mode 100644 index 00000000..3dbdccf2 --- /dev/null +++ b/reviewer/git_factory_run_linux_test.go @@ -0,0 +1,275 @@ +//go:build linux + +package reviewer + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "io" + "os" + "path/filepath" + "testing" + "time" +) + +// Actual retained files, pipes, sealed manifest and original pidfds; fake +// manager/cgroup. The inert fixture never parses candidate Git or runs a model. +func gitFactoryRunFixture(t *testing.T) (*gitFactoryRun, gitUnitFixture) { + r, f, _ := gitFactoryAssetsFixture(t) + return r, f +} + +func gitFactoryAssetsFixture(t *testing.T, snapshot ...Snapshot) (*gitFactoryRun, gitUnitFixture, *gitRuntimeRoot) { + t.Helper() + f := newGitUnitFixture(t) + if len(snapshot) > 0 { + f.binding.Snapshot = snapshot[0] + } + manifest, bodies := gitRuntimeFixture(t) + artifact, pins := gitRuntimeFrame(t, manifest, bodies) + root, err := prepareGitRuntime(context.Background(), nativeRuntimePrivateParent(t), bytes.NewReader(artifact), pins, nil) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { root.Close() }) + runtime, err := root.directoryFD() + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { runtime.Close() }) + raw, err := os.ReadFile("/usr/bin/bwrap") + if err != nil { + t.Fatal(err) + } + helperPin := VMAssetPin{gitRawHash(raw), int64(len(raw))} + helper, err := retainNativeBootstrap(context.Background(), VMBootAsset{Path: "/usr/bin/bwrap", SHA256: helperPin.SHA256, Bytes: helperPin.Bytes}) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { helper.Close() }) + worker, err := f.owner.image.file.Stat() + if err != nil { + t.Fatal(err) + } + filter, err := gitFixedFilterPin() + if err != nil { + t.Fatal(err) + } + m := gitGrantManifest{2, "isolated-git-acquisition", pins, helperPin, VMAssetPin{f.stubHash, worker.Size()}, filter} + f.binding.RuntimeHash = Digest(m) + archive, _ := exportArchiveFixture(t) + directory, err := os.MkdirTemp("/tmp", "sfl-git-factory-") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { os.RemoveAll(directory) }) + r, err := newGitFactoryRun(context.Background(), f.owner, archive, f.binding, m, runtime, helper, directory) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + if r.stop(ctx) == nil { + r.close() + } + }) + return r, f, root +} + +func TestGitFactoryManifestSealedAndPreparationDoesNoWork(t *testing.T) { + r, f := gitFactoryRunFixture(t) + if f.manager.starts != 0 || f.manager.stops != 0 { + t.Fatal("construction dispatched manager work") + } + record, err := f.owner.read(f.binding.Attempt) + if err != nil || record.Started || record.Stopped || record.Binding != f.binding { + t.Fatal("prepared durable binding", record, err) + } + raw, err := io.ReadAll(io.NewSectionReader(r.files[0], 0, gitGrantManifestBytes+1)) + if err != nil { + t.Fatal(err) + } + want, _ := json.Marshal(r.manifest) + if !bytes.Equal(raw, want) || verifyGrantedVMAsset(context.Background(), r.files[0], VMAssetPin{f.binding.RuntimeHash, int64(len(raw))}, 1) != nil { + t.Fatal("factory manifest is not the exact sealed non-executable release binding") + } + if _, err := r.files[0].WriteAt([]byte("x"), 0); err == nil { + t.Fatal("sealed manifest mutated") + } + if r.close() == nil { + t.Fatal("unstopped attempt lost assets") + } + if r.stop(context.Background()) != nil || r.close() != nil { + t.Fatal("unstarted attempt failed terminal cleanup") + } + if f.manager.starts != 0 || f.manager.stops != 0 { + t.Fatal("unstarted stop dispatched a unit") + } +} + +func TestGitFactoryCancellationJoinsBrokerBeforePhysicalUnitStop(t *testing.T) { + r, f := gitFactoryRunFixture(t) + owner, _, _, _, _ := credentialOwnerFixture(t) + if owner.Refresh(context.Background()) != nil { + t.Fatal("fake credential qualification") + } + g, err := owner.retainReaderGeneration(context.Background(), r.stop) + if err != nil { + t.Fatal(err) + } + result := make(chan error, 1) + go func() { + m, err := r.acquire(g.ctx, &gitGenerationReader{generation: g}) + if m != nil { + m.Close() + } + result <- err + }() + deadline := time.Now().Add(3 * time.Second) + for { + if !f.owner.enter(context.Background()) { + t.Fatal("unit operation") + } + record, err := f.owner.read(f.binding.Attempt) + f.owner.leave() + if err != nil { + t.Fatal(err) + } + if record.Captured { + break + } + if time.Now().After(deadline) { + t.Fatal("fake unit never reached inert bootstrap wait") + } + time.Sleep(time.Millisecond) + } + if err := g.stop(context.Background()); err != nil { + t.Fatal("coordinated stop", err) + } + if err := <-result; err == nil { + t.Fatal("canceled acquisition published output") + } + r.mu.Lock() + audit, done, stopped := r.brokerAudit, r.brokerDone, r.stopped + r.mu.Unlock() + select { + case <-done: + default: + t.Fatal("broker callback still owned after stop") + } + if !audit.Stopped || !stopped || audit.Requests != 0 { + t.Fatal("broker/physical stop proof", audit, stopped) + } + f.manager.mu.Lock() + starts, stops := f.manager.starts, f.manager.stops + f.manager.mu.Unlock() + if starts != 1 || stops != 1 { + t.Fatal("unexpected manager replay", starts, stops) + } + if r.close() != nil { + t.Fatal("stopped runtime descriptors retained") + } + owner.state.mu.Lock() + owned := len(owner.state.acquisitions) + owner.state.mu.Unlock() + if owned != 0 { + t.Fatal("successful stop retained generation") + } +} + +func TestGitFactoryStopRetainsUnreturnedBrokerAndPreventsReentry(t *testing.T) { + r, f := gitFactoryRunFixture(t) + blocked := make(chan struct{}) + acquired := make(chan struct{}) + close(acquired) + r.mu.Lock() + r.spent = true + r.acquireDone = acquired + r.brokerDone = blocked + r.mu.Unlock() + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) + defer cancel() + if r.stop(ctx) == nil || r.close() == nil { + t.Fatal("unreturned broker discarded ownership") + } + f.owner.enter(context.Background()) + record, err := f.owner.read(f.binding.Attempt) + f.owner.leave() + if err != nil || record.Stopped || f.manager.stops != 0 { + t.Fatal("physical stop preceded broker drain", record, err) + } + if m, err := r.acquire(context.Background(), &gitGenerationReader{}); err == nil || m != nil { + t.Fatal("revoked acquisition reentered") + } + r.mu.Lock() + r.brokerAudit = GitReadBrokerAudit{Version: 1, Stopped: true} + r.mu.Unlock() + close(blocked) + if r.stop(context.Background()) != nil || r.close() != nil { + t.Fatal("returned broker could not finish stop") + } +} + +func TestGitFactoryFailedStartKeepsGenerationUntilKnownStop(t *testing.T) { + r, f := gitFactoryRunFixture(t) + f.manager.startErr = errors.New("synthetic missing accepted-start reply") + owner, _, _, _, _ := credentialOwnerFixture(t) + if owner.Refresh(context.Background()) != nil { + t.Fatal("fake credential qualification") + } + g, err := owner.retainReaderGeneration(context.Background(), r.stop) + if err != nil { + t.Fatal(err) + } + if m, err := r.acquire(g.ctx, &gitGenerationReader{generation: g}); err == nil || m != nil { + t.Fatal("unknown start returned source") + } + if g.stop(context.Background()) == nil { + t.Fatal("unknown unit birth released generation") + } + owner.state.mu.Lock() + owned := len(owner.state.acquisitions) + owner.state.mu.Unlock() + if owned != 1 || r.close() == nil { + t.Fatal("unknown start lost retained assets") + } + // This test's manager reports an error before any accepted start reply. + // The fake kernel's next-boot observation supplies an independent terminal + // branch for old work; it does not kill this fixture's actual inert child. + f.manager.kernel.mu.Lock() + f.manager.kernel.boot = "synthetic-next-boot" + f.manager.kernel.mu.Unlock() + if g.stop(context.Background()) != nil || r.close() != nil { + t.Fatal("independently terminal old boot did not recover") + } + if _, err := os.Stat(filepath.Join(r.directory, "bootstrap.sock")); err != nil { + t.Fatal("retained recovery directory unexpectedly removed", err) + } +} + +func TestGitFactoryBrokerSetupFailureRetiresOnlyKnownUnstartedLease(t *testing.T) { + r, f := gitFactoryRunFixture(t) + if os.WriteFile(filepath.Join(r.directory, "broker", "unexpected"), []byte("trusted setup fault"), 0600) != nil { + t.Fatal("setup fixture") + } + owner, _, _, _, _ := credentialOwnerFixture(t) + if owner.Refresh(context.Background()) != nil { + t.Fatal("fake credential qualification") + } + g, err := owner.retainReaderGeneration(context.Background(), r.stop) + if err != nil { + t.Fatal(err) + } + if m, err := r.acquire(g.ctx, &gitGenerationReader{generation: g}); err == nil || m != nil { + t.Fatal("invalid broker setup returned source") + } + if g.stop(context.Background()) != nil || r.close() != nil { + t.Fatal("joined setup refusal retained unstarted lease") + } + if f.manager.starts != 0 || f.manager.stops != 0 { + t.Fatal("setup refusal dispatched a unit") + } +} diff --git a/reviewer/git_factory_unit_linux.go b/reviewer/git_factory_unit_linux.go new file mode 100644 index 00000000..159cdfa8 --- /dev/null +++ b/reviewer/git_factory_unit_linux.go @@ -0,0 +1,198 @@ +//go:build linux + +package reviewer + +import ( + "context" + "fmt" + "io" + "os" + "strconv" + "strings" + + "golang.org/x/sys/unix" +) + +// Keep these checks under the unit owner's operation lock. They confer no +// serialized qualification and do not arm or replay a grant after recovery. +func (o *gitUnitOwner) factoryUnit(ctx context.Context, binding gitMaterializationBinding) (gitUnitRecord, error) { + r, err := o.read(binding.Attempt) + if err != nil || o.closed || ctx.Err() != nil || r.Binding != binding || !r.Captured || r.Revoked || r.Stopped || r.ManagerOwner != o.manager.Owner() || !o.grantEligible[binding.Attempt] || o.grantFailed[binding.Attempt] { + return r, errGitUnit + } + boot, err := o.kernel.BootID() + root, held := o.rootPIDFDs[binding.Attempt] + if err != nil || boot != r.BootID || !held || gitCheckProcessPIDFD(r.Identity, root) != nil { + return r, errGitUnit + } + current, err := o.manager.Observe(ctx, r.Unit) + if err != nil || !expectedGitUnit(r, current) || current.InvocationID != r.Identity.InvocationID || current.MainPID != uint32(r.Identity.PID) || current.ActiveState != "active" || r.Birth == nil || current.Path != r.Birth.Unit.UnitPath { + return r, errGitUnit + } + group, err := o.kernel.Capture(current.ControlGroup, r.Unit) + if err != nil || group != r.Cgroup || o.kernel.Executable(r.Identity.PID, r.StubHash) != nil { + return r, errGitUnit + } + return r, nil +} + +func (o *gitUnitOwner) checkFactoryReadPeer(ctx context.Context, binding gitMaterializationBinding, worker GateIdentity, peer GitReadPeer) error { + if ctx == nil || !binding.valid() || !o.enter(ctx) { + return errGitUnit + } + defer o.leave() + bounded, cancel := boundedManagerContext(ctx) + defer cancel() + r, err := o.factoryUnit(bounded, binding) + if err != nil || r.WorkerGrantHash == "" || r.WorkerIdentity != worker { + return errGitUnit + } + identity, fd, err := retainGitBrokerPeer(peer, r.Identity.InvocationID) + if err != nil { + return errGitUnit + } + defer unix.Close(fd) + unit, invocation, err := o.manager.UnitForPIDFD(bounded, fd) + if err != nil || unit != r.Unit || invocation != r.Identity.InvocationID || gitCheckProcessPIDFD(identity, fd) != nil { + return errGitUnit + } + after, err := o.factoryUnit(bounded, binding) + if err != nil || after.Identity != r.Identity || after.Cgroup != r.Cgroup || bounded.Err() != nil { + return errGitUnit + } + return nil +} + +func gitFactoryParent(ctx context.Context, identity GateIdentity, original int) (int, error) { + if ctx.Err() != nil || gitCheckProcessPIDFD(identity, original) != nil { + return 0, errGitUnit + } + file, err := os.Open(fmt.Sprintf("/proc/%d/stat", identity.PID)) + if err != nil { + return 0, errGitUnit + } + raw, readErr := io.ReadAll(io.LimitReader(file, 4097)) + closeErr := file.Close() + end := strings.LastIndexByte(string(raw), ')') + if readErr != nil || closeErr != nil || len(raw) > 4096 || end < 0 { + return 0, errGitUnit + } + parts := strings.Fields(string(raw[end+1:])) + if len(parts) < 20 { + return 0, errGitUnit + } + parent, err := strconv.Atoi(parts[1]) + start, startErr := strconv.ParseUint(parts[19], 10, 64) + if err != nil || startErr != nil || parent <= 0 || start != identity.StartTicks || ctx.Err() != nil || gitCheckProcessPIDFD(identity, original) != nil { + return 0, errGitUnit + } + return parent, nil +} + +// Bind a worker to the original live unit root through retained, measured +// namespace-helper ancestors. No worker-supplied parent or filter flag enters +// this check. The trusted fixed bootstrap passes only the sealed fixed filter +// to that measured helper; process Seccomp=2 alone is never its provenance. +func (o *gitUnitOwner) checkFactoryLaunch(ctx context.Context, binding gitMaterializationBinding, worker GateIdentity, original int, helper *os.File, pin VMAssetPin) error { + if ctx == nil || !binding.valid() || helper == nil || !o.enter(ctx) { + return errGitUnit + } + defer o.leave() + bounded, cancel := boundedManagerContext(ctx) + defer cancel() + r, err := o.factoryUnit(bounded, binding) + if err != nil || r.BootstrapGrantHash == "" || r.WorkerGrantHash != "" || worker.PID == r.Identity.PID || worker.BootID != r.BootID || worker.InvocationID != r.Identity.InvocationID || verifyNativeBootstrap(bounded, helper, pin) != nil { + return errGitUnit + } + helperID, err := gitStatFileID(helper) + if err != nil { + return errGitUnit + } + first, err := gitRetainProcessPIDFD(worker, original) + if err != nil { + return errGitUnit + } + type edge struct { + identity GateIdentity + fd, parent int + } + chain := []edge{{identity: worker, fd: first}} + defer func() { + for _, item := range chain { + unix.Close(item.fd) + } + }() + reached := false + for len(chain) <= 8 { + index := len(chain) - 1 + item := &chain[index] + parent, err := gitFactoryParent(bounded, item.identity, item.fd) + if err != nil { + return errGitUnit + } + item.parent = parent + unit, invocation, err := o.manager.UnitForPIDFD(bounded, item.fd) + if err != nil || unit != r.Unit || invocation != r.Identity.InvocationID { + return errGitUnit + } + if parent == r.Identity.PID { + if len(chain) < 2 { + return errGitUnit + } + reached = true + break + } + if parent <= 1 || len(chain) == 8 { + return errGitUnit + } + for _, prior := range chain { + if prior.identity.PID == parent { + return errGitUnit + } + } + identity, err := processGateIdentity(parent, r.Identity.InvocationID) + if err != nil || identity.BootID != r.BootID { + return errGitUnit + } + fd, err := unix.PidfdOpen(parent, 0) + if err != nil { + return errGitUnit + } + chain = append(chain, edge{identity: identity, fd: fd}) + executable, err := os.Open(fmt.Sprintf("/proc/%d/exe", parent)) + if err != nil { + return errGitUnit + } + id, statErr := gitStatFileID(executable) + hash, hashErr := gitBootstrapImageHash(bounded, executable, pin.Bytes) + executable.Close() + if statErr != nil || hashErr != nil || id != helperID || hash != pin.SHA256 || gitCheckProcessPIDFD(identity, fd) != nil { + return errGitUnit + } + } + if !reached { + return errGitUnit + } + for _, item := range chain { + parent, err := gitFactoryParent(bounded, item.identity, item.fd) + if err != nil || parent != item.parent { + return errGitUnit + } + if item.identity != worker { + executable, err := os.Open(fmt.Sprintf("/proc/%d/exe", item.identity.PID)) + if err != nil { + return errGitUnit + } + id, statErr := gitStatFileID(executable) + executable.Close() + if statErr != nil || id != helperID { + return errGitUnit + } + } + } + after, err := o.factoryUnit(bounded, binding) + if err != nil || after.Identity != r.Identity || after.Cgroup != r.Cgroup || verifyNativeBootstrap(bounded, helper, pin) != nil || bounded.Err() != nil { + return errGitUnit + } + return nil +} diff --git a/reviewer/git_factory_unit_linux_test.go b/reviewer/git_factory_unit_linux_test.go new file mode 100644 index 00000000..c7867f8a --- /dev/null +++ b/reviewer/git_factory_unit_linux_test.go @@ -0,0 +1,70 @@ +//go:build linux + +package reviewer + +import ( + "context" + "testing" + + "github.com/godbus/dbus/v5" +) + +func TestGitFactoryUnitRechecksBirthAndLiveOwnership(t *testing.T) { + f := newGitUnitFixture(t) + f.prepareStart(t) + check := func() error { + if !f.owner.enter(context.Background()) { + t.Fatal("operation lock") + } + defer f.owner.leave() + _, err := f.owner.factoryUnit(context.Background(), f.binding) + return err + } + if check() != nil { + t.Fatal("fresh captured fake unit refused") + } + for _, mode := range []string{"unit-path", "main-pid", "invocation", "inactive", "memory", "swap", "tasks", "description", "job", "owner", "eligibility", "group"} { + t.Run(mode, func(t *testing.T) { + f.manager.mu.Lock() + before, owner := f.manager.observation, f.manager.owner + switch mode { + case "unit-path": + f.manager.observation.Path = dbus.ObjectPath("/org/freedesktop/systemd1/unit/replacement") + case "main-pid": + f.manager.observation.MainPID++ + case "invocation": + f.manager.observation.InvocationID = "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + case "inactive": + f.manager.observation.ActiveState = "inactive" + case "memory": + f.manager.observation.MemoryMax++ + case "swap": + f.manager.observation.MemorySwapMax = 1 + case "tasks": + f.manager.observation.TasksMax++ + case "description": + f.manager.observation.Description = "unrelated" + case "job": + f.manager.observation.Job.ID = 99 + case "owner": + f.manager.owner = "replacement manager" + case "eligibility": + f.owner.grantEligible[f.binding.Attempt] = false + case "group": + f.manager.kernel.changed = true + } + f.manager.mu.Unlock() + if check() == nil { + t.Fatal("changed live unit qualified") + } + f.manager.mu.Lock() + f.manager.observation, f.manager.owner = before, owner + f.manager.mu.Unlock() + f.manager.kernel.changed = false + f.owner.grantEligible[f.binding.Attempt] = true + }) + } + if check() != nil { + t.Fatal("restored fixture did not requalify") + } +} diff --git a/reviewer/git_source_factory_linux.go b/reviewer/git_source_factory_linux.go new file mode 100644 index 00000000..37860db6 --- /dev/null +++ b/reviewer/git_source_factory_linux.go @@ -0,0 +1,273 @@ +//go:build linux + +package reviewer + +import ( + "context" + "crypto/rand" + "encoding/hex" + "os" + "path/filepath" + "sort" +) + +// Internal service composition, never a model capability or serialized gate. +// Only Capture/Restore callbacks are handed to the polling controller. The +// trusted service retains this owner until controller work has joined. +type gitSourceFactory struct { + operation chan struct{} + credentials *GitHubCredentialOwner + unit *gitUnitOwner + archive *gitExportArchive + runtime *gitRuntimeRoot + helper *os.File + manifest gitGrantManifest + release, releaseHash, directory string + root *os.Root + identity os.FileInfo + runs map[string]*gitFactoryRun + bindings map[string]gitMaterializationBinding + packets map[string]string + materialized map[string]*gitMaterialization + closed bool +} + +// Opening performs startup recovery before asking the credential owner for any +// ready generation. Caller transfers exclusive lifecycle ownership of unit, +// archive and prepared runtime; credentials remain shared with the publisher. +// Call this before credential Refresh. No refresh, App install or HTTP occurs. +func newGitSourceFactory(ctx context.Context, credentials *GitHubCredentialOwner, unit *gitUnitOwner, archive *gitExportArchive, runtime *gitRuntimeRoot, helper *os.File, m gitGrantManifest, release, releaseHash, directory string) (_ *gitSourceFactory, result error) { + if ctx == nil || ctx.Err() != nil || credentials == nil || credentials.state == nil || unit == nil || archive == nil || runtime == nil || helper == nil || !objectID.MatchString(release) || !validAuthHash(releaseHash) || !filepath.IsAbs(directory) || filepath.Clean(directory) != directory || len(directory) > 33 || privateDirectory(directory) != nil { + return nil, errGitUnit + } + resolved, err := filepath.EvalSymlinks(directory) + if err != nil || resolved != directory { + return nil, errGitUnit + } + credentials.state.mu.Lock() + busy := len(credentials.state.acquisitions) != 0 + credentials.state.mu.Unlock() + if busy { + return nil, errGitUnit + } + if !m.validAssets() || m.Worker.SHA256 != unit.stubHash || verifyNativeBootstrap(ctx, helper, m.NamespaceHelper) != nil { + return nil, errGitUnit + } + runtimeFD, err := runtime.directoryFD() + if err != nil { + return nil, errGitUnit + } + _, runtimeErr := verifyGitRuntimeDirectory(ctx, runtimeFD, m.Runtime) + runtimeFD.Close() + if runtimeErr != nil { + return nil, errGitUnit + } + // Recovery is startup-only. A live owner that has already started work is + // not interchangeable with a newly opened, cross-process-locked owner. + if !unit.enter(ctx) { + return nil, errGitUnit + } + active := len(unit.rootPIDFDs) != 0 || len(unit.grantEligible) != 0 || len(unit.grantFailed) != 0 + unit.leave() + if active { + return nil, errGitUnit + } + root, err := os.OpenRoot(directory) + if err != nil { + return nil, errGitUnit + } + f := &gitSourceFactory{operation: make(chan struct{}, 1), credentials: credentials, unit: unit, archive: archive, runtime: runtime, manifest: m, release: release, releaseHash: releaseHash, directory: directory, root: root, runs: map[string]*gitFactoryRun{}, packets: map[string]string{}, materialized: map[string]*gitMaterialization{}} + f.operation <- struct{}{} + defer func() { + if result != nil { + if f.helper != nil { + f.helper.Close() + } + root.Close() + } + }() + f.identity, err = root.Stat(".") + if err != nil { + return nil, errGitUnit + } + f.helper, err = duplicateGitControlFile(helper) + if err != nil { + return nil, errGitUnit + } + f.bindings, err = recoverGitFactory(ctx, unit, archive) + if err != nil { + return nil, errGitUnit + } + if !archive.enter(ctx) { + return nil, errGitUnit + } + exports, err := archive.records() + archive.leave() + if err != nil { + return nil, errGitUnit + } + for _, record := range exports { + if record.State == "staged" { + f.packets[record.Binding.Attempt] = record.PacketHash + } + } + return f, nil +} + +func (f *gitSourceFactory) enter(ctx context.Context) bool { + if f == nil || ctx == nil { + return false + } + select { + case <-ctx.Done(): + return false + case <-f.operation: + return true + } +} +func (f *gitSourceFactory) leave() { f.operation <- struct{}{} } + +func (f *gitSourceFactory) checkRoot() error { + anchored, err := f.root.Stat(".") + path, pathErr := os.Lstat(f.directory) + if err != nil || pathErr != nil || !path.IsDir() || path.Mode().Perm() != 0700 || !os.SameFile(f.identity, anchored) || !os.SameFile(f.identity, path) { + return errGitUnit + } + return nil +} + +func (f *gitSourceFactory) Capture(ctx context.Context, snapshot Snapshot) (*SnapshotEvidence, error) { + if !f.enter(ctx) { + return nil, errGitUnit + } + defer f.leave() + if f.closed || f.checkRoot() != nil || snapshot.Validate() != nil || snapshot.RepositoryID != f.credentials.state.options.Journal.enrollment.RepositoryID { + return nil, errGitUnit + } + var nonce [16]byte + if _, err := rand.Read(nonce[:]); err != nil { + return nil, errGitUnit + } + binding := gitMaterializationBinding{hex.EncodeToString(nonce[:]), f.releaseHash, Digest(f.manifest), snapshot} + name := "run-" + binding.Attempt + if f.root.Mkdir(name, 0700) != nil || syncAuthDirectory(f.root) != nil { + return nil, errGitUnit + } + runtime, err := f.runtime.directoryFD() + if err != nil { + return nil, errGitUnit + } + defer runtime.Close() + run, err := newGitFactoryRun(ctx, f.unit, f.archive, binding, f.manifest, runtime, f.helper, filepath.Join(f.directory, name)) + if err != nil { + return nil, errGitUnit + } + f.runs[binding.Attempt] = run + m, err := runGitFactoryAcquisition(ctx, f.credentials, binding, run) + if err != nil { + return nil, errGitUnit + } + evidence, err := captureGitMaterializationEvidence(ctx, m) + if err != nil { + m.Close() + return nil, errGitUnit + } + f.bindings[binding.Attempt] = binding + f.packets[binding.Attempt] = Digest(m.packet) + f.materialized[binding.Attempt] = m + return evidence, nil +} + +func (f *gitSourceFactory) Restore(ctx context.Context, job Job) (*JobEvidence, error) { + if !f.enter(ctx) { + return nil, errGitUnit + } + defer f.leave() + if f.closed || f.checkRoot() != nil || job.Validate() != nil || job.Release != f.release || job.Snapshot.RepositoryID != f.credentials.state.options.Journal.enrollment.RepositoryID { + return nil, ErrFenced + } + attempts := []string{} + for attempt, binding := range f.bindings { + if binding.Snapshot == job.Snapshot && binding.ReleaseHash == f.releaseHash && binding.RuntimeHash == Digest(f.manifest) && f.packets[attempt] == Digest(job.Packet) { + attempts = append(attempts, attempt) + } + } + sort.Strings(attempts) + if len(attempts) == 0 { + return nil, ErrFenced + } + attempt := attempts[0] + binding := f.bindings[attempt] + // Revalidate the durable physical-stop journal before using retained source. + // There is no network, recapture, Git parser or earlier-label fallback here. + if f.unit.stop(ctx, binding) != nil { + return nil, ErrFenced + } + m := f.materialized[attempt] + if m == nil { + var err error + m, err = f.archive.reopen(ctx, binding) + if err != nil { + return nil, ErrFenced + } + f.materialized[attempt] = m + } + return restoreGitMaterialization(ctx, m, job) +} + +func (f *gitSourceFactory) Close(ctx context.Context) error { + if !f.enter(ctx) { + return errGitUnit + } + defer f.leave() + if f.closed { + return nil + } + for _, run := range f.runs { + run.mu.Lock() + generation := run.generation + run.mu.Unlock() + if generation != nil { + if generation.stop(ctx) != nil { + return errGitUnit + } + } else if run.stop(ctx) != nil { + return errGitUnit + } + if run.close() != nil { + return errGitUnit + } + } + // A failed physical stop returns above with every shared root and service + // lock retained. Runtime cleanup cannot race an unknown live acquisition. + if f.unit.Close() != nil { + return errGitUnit + } + if f.archive.Close(ctx) != nil { + return errGitUnit + } + if f.runtime.Close() != nil { + return errGitUnit + } + failed := false + for attempt, m := range f.materialized { + if m.Close() != nil { + failed = true + } + delete(f.materialized, attempt) + } + if f.helper.Close() != nil { + failed = true + } + if f.root.Close() != nil { + failed = true + } + f.closed = true + if failed { + return errGitUnit + } + return nil +} + +// Compile-time guarantee that production uses the concrete fixed driver. +var _ gitFactoryAcquisition = (*gitFactoryRun)(nil) diff --git a/reviewer/git_source_factory_linux_test.go b/reviewer/git_source_factory_linux_test.go new file mode 100644 index 00000000..6066ea0b --- /dev/null +++ b/reviewer/git_source_factory_linux_test.go @@ -0,0 +1,137 @@ +//go:build linux + +package reviewer + +import ( + "bytes" + "context" + "io" + "os" + "reflect" + "strings" + "testing" +) + +func gitFactoryWorkRoot(t *testing.T) string { + t.Helper() + directory, err := os.MkdirTemp("/tmp", "sfl-source-") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { os.RemoveAll(directory) }) + return directory +} + +func TestGitSourceFactoryStartupPrecedesCredentialWork(t *testing.T) { + r, f, runtime := gitFactoryAssetsFixture(t) + owner, _, _, _, signs := credentialOwnerFixture(t) + factory, err := newGitSourceFactory(context.Background(), owner, f.owner, r.archive, runtime, r.files[2], r.manifest, testJob().Release, r.binding.ReleaseHash, gitFactoryWorkRoot(t)) + if err != nil { + t.Fatal("offline startup", err) + } + if signs.Load() != 0 || f.manager.starts != 0 { + t.Fatal("startup dispatched credentials or inference") + } + if r.stop(context.Background()) != nil || r.close() != nil { + t.Fatal("prepared fixture cleanup") + } + if evidence, err := factory.Capture(context.Background(), testSnapshot()); err == nil || evidence != nil { + t.Fatal("unready credentials created source") + } + if signs.Load() != 0 || f.manager.starts != 0 { + t.Fatal("unready capture refreshed credentials or launched a unit") + } + if factory.Close(context.Background()) != nil { + t.Fatal("factory close") + } +} + +func TestGitSourceFactoryRestoresAdmittedPacketWithoutHostGit(t *testing.T) { + _, binding, store := gitMaterializationFixture(t, "sha1") + original, err := CaptureSnapshotEvidence(context.Background(), store, binding.Snapshot) + if err != nil { + t.Fatal(err) + } + r, f, runtime := gitFactoryAssetsFixture(t, binding.Snapshot) + helper, err := duplicateGitControlFile(r.files[2]) + if err != nil { + t.Fatal(err) + } + defer helper.Close() + if f.owner.start(context.Background(), r.binding, "/private/bootstrap.sock") != nil { + t.Fatal("fake unit start") + } + // Explicit fake admission history: the manager/cgroup fixture is synthetic. + // These canonical receipts exercise recovery correlation, not containment. + if !f.owner.enter(context.Background()) { + t.Fatal("unit lock") + } + record, err := f.owner.read(r.binding.Attempt) + if err != nil { + f.owner.leave() + t.Fatal(err) + } + worker, err := processGateIdentity(os.Getpid(), record.Identity.InvocationID) + if err != nil { + f.owner.leave() + t.Fatal(err) + } + record.BootstrapGrantHash = Digest(gitUnitGrantPacket{1, gitBootstrapGrant, r.binding, record.Identity, r.binding.RuntimeHash}) + record.WorkerIdentity = worker + record.WorkerGrantHash = Digest(gitUnitGrantPacket{1, gitWorkerGrant, r.binding, worker, r.binding.RuntimeHash}) + err = f.owner.write(record) + f.owner.leave() + if err != nil { + t.Fatal(err) + } + var wire bytes.Buffer + if writeGitMaterialization(context.Background(), &wire, store, r.binding, DefaultGitAcquisitionBounds()) != nil { + t.Fatal("synthetic Git export") + } + m, err := r.archive.stage(context.Background(), r.binding, io.NopCloser(bytes.NewReader(wire.Bytes()))) + if err != nil { + t.Fatal(err) + } + packet := m.packet + m.Close() + if r.stop(context.Background()) != nil || r.close() != nil || f.owner.Close() != nil { + t.Fatal("fake unit/old owner shutdown") + } + reopened, err := newGitUnitOwner(context.Background(), f.root, f.stub, f.stubHash, f.manager, f.manager.kernel) + if err != nil { + t.Fatal("new startup owner", err) + } + owner, _, _, _, signs := credentialOwnerFixture(t) + factory, err := newGitSourceFactory(context.Background(), owner, reopened, r.archive, runtime, helper, r.manifest, testJob().Release, r.binding.ReleaseHash, gitFactoryWorkRoot(t)) + if err != nil { + t.Fatal("recovery", err) + } + defer factory.Close(context.Background()) + if signs.Load() != 0 { + t.Fatal("retained source recovery touched credentials") + } + if os.RemoveAll(store.GitDir) != nil { + t.Fatal("remove owned synthetic quarantine") + } + job := testJob() + job.Snapshot = r.binding.Snapshot + job.Packet = packet + bound, err := factory.Restore(context.Background(), job) + if err != nil || bound.evidence.Packet() != original.Packet() || !reflect.DeepEqual(bound.evidence.lines, original.lines) || !reflect.DeepEqual(bound.evidence.Coverage(), original.Coverage()) { + t.Fatal("restored evidence changed", err) + } + // The admission path uses the same blob-based anchor validation without + // fabricating a Job or executing a host Git fallback. + evidence, err := captureGitMaterializationEvidence(context.Background(), factory.materialized[r.binding.Attempt]) + if err != nil || evidence.Packet() != packet || !reflect.DeepEqual(evidence.lines, original.lines) { + t.Fatal("capture/restore evidence mismatch", err) + } + wrong := job + wrong.Release = strings.Repeat("e", 40) + if got, err := factory.Restore(context.Background(), wrong); err == nil || got != nil { + t.Fatal("other release reused retained source") + } + if signs.Load() != 0 { + t.Fatal("restore dispatched credential work") + } +} diff --git a/reviewer/git_unit_grant_linux.go b/reviewer/git_unit_grant_linux.go index 0cee872e..a4daf2b6 100644 --- a/reviewer/git_unit_grant_linux.go +++ b/reviewer/git_unit_grant_linux.go @@ -39,13 +39,17 @@ func gitFixedFilterPin() (VMAssetPin, error) { } return VMAssetPin{gitRawHash(raw.Bytes()), int64(raw.Len())}, nil } -func (m gitGrantManifest) valid(binding gitMaterializationBinding) bool { +func (m gitGrantManifest) validAssets() bool { filter, err := gitFixedFilterPin() - return err == nil && m.Version == 2 && m.Purpose == "isolated-git-acquisition" && binding.valid() && Digest(m) == binding.RuntimeHash && + return err == nil && m.Version == 2 && m.Purpose == "isolated-git-acquisition" && gitRuntimePin(m.Runtime.Git, 64<<20) && gitRuntimePin(m.Runtime.HTTP, 64<<20) && gitRuntimePin(m.Runtime.Artifact, gitRuntimeDataBytes+gitRuntimeManifestBytes+int64(len(gitRuntimeMagic))+4) && gitRuntimePin(m.NamespaceHelper, 64<<20) && gitRuntimePin(m.Worker, 64<<20) && m.Filter == filter } +func (m gitGrantManifest) valid(binding gitMaterializationBinding) bool { + return binding.valid() && Digest(m) == binding.RuntimeHash && m.validAssets() +} + type gitUnitGrantPacket struct { Version int Stage string diff --git a/reviewer/snapshot_evidence.go b/reviewer/snapshot_evidence.go index d00d2277..72d049d0 100644 --- a/reviewer/snapshot_evidence.go +++ b/reviewer/snapshot_evidence.go @@ -259,15 +259,28 @@ func RestoreSnapshotEvidence(ctx context.Context, git GitStore, job Job) (*JobEv // bytes. The caller owns inventory/source qualification. It preserves every // admitted omission and never recaptures a packet during recovery. func restoreSnapshotEvidencePacket(ctx context.Context, store ObjectStore, job Job, inventory []snapshotFile, blob func(string, *int) ([]byte, error)) (*JobEvidence, error) { - if job.Validate() != nil || len(job.Packet) > snapshotPacketBytes || store == nil || blob == nil { + if job.Validate() != nil { + return nil, ErrFenced + } + evidence, err := restoreSnapshotPacket(ctx, store, job.Snapshot, job.Packet, inventory, blob) + if err != nil { + return nil, err + } + return evidence.Bind(job) +} + +// Reconstruct packet anchors from verified plain blobs before admission as well +// as after restart. Capture must not invent a job to perform this validation. +func restoreSnapshotPacket(ctx context.Context, store ObjectStore, snapshot Snapshot, packet string, inventory []snapshotFile, blob func(string, *int) ([]byte, error)) (*SnapshotEvidence, error) { + if ctx == nil || ctx.Err() != nil || snapshot.Validate() != nil || len(packet) > snapshotPacketBytes || store == nil || blob == nil { return nil, ErrFenced } var p snapshotPacket - if json.Unmarshal([]byte(job.Packet), &p) != nil { + if json.Unmarshal([]byte(packet), &p) != nil { return nil, ErrFenced } canonical, err := json.Marshal(p) - if err != nil || string(canonical) != job.Packet || p.Version != "sfl-snapshot-evidence-1" || p.Snapshot != job.Snapshot || len(p.Files) != len(inventory) { + if err != nil || string(canonical) != packet || p.Version != "sfl-snapshot-evidence-1" || p.Snapshot != snapshot || len(p.Files) != len(inventory) { return nil, ErrFenced } omissions := map[string]string{} @@ -282,7 +295,7 @@ func restoreSnapshotEvidencePacket(ctx context.Context, store ObjectStore, job J } omissions[c.Path] = c.Reason } - e := &SnapshotEvidence{store: store, snapshot: job.Snapshot, packet: job.Packet, coverage: append([]Coverage(nil), p.Coverage...), lines: map[string]map[int]bool{}} + e := &SnapshotEvidence{store: store, snapshot: snapshot, packet: packet, coverage: append([]Coverage(nil), p.Coverage...), lines: map[string]map[int]bool{}} remaining := snapshotObjectBytes for i, file := range p.Files { identity := file @@ -326,7 +339,7 @@ func restoreSnapshotEvidencePacket(ctx context.Context, store ObjectStore, job J if err := ctx.Err(); err != nil { return nil, err } - return e.Bind(job) + return e, nil } func snapshotBlob(ctx context.Context, git GitStore, id string, remaining *int) ([]byte, error) { From 05e22de80019a15482db3189bfb1bdf0e1d341cd Mon Sep 17 00:00:00 2001 From: Franz Hemmer <8227352+HemSoft@users.noreply.github.com> Date: Wed, 21 Oct 2026 12:02:39 -0400 Subject: [PATCH 05/12] Correct factory failure recovery and descriptor inventory --- reviewer/GIT-DESCRIPTOR-BOUNDARY.md | 6 +- reviewer/GIT-FACTORY.md | 14 +- reviewer/git_descriptor_boundary_linux.go | 11 +- .../git_descriptor_boundary_linux_test.go | 11 +- reviewer/git_factory_recovery_linux.go | 64 ++++++++ reviewer/git_factory_run_linux.go | 15 +- reviewer/git_factory_run_linux_test.go | 13 ++ reviewer/git_mount_boundary_linux_test.go | 12 +- reviewer/git_source_factory_linux.go | 117 ++++++++++---- reviewer/git_source_factory_linux_test.go | 150 ++++++++++++++++++ 10 files changed, 373 insertions(+), 40 deletions(-) diff --git a/reviewer/GIT-DESCRIPTOR-BOUNDARY.md b/reviewer/GIT-DESCRIPTOR-BOUNDARY.md index 0a2f6034..39a90c4c 100644 --- a/reviewer/GIT-DESCRIPTOR-BOUNDARY.md +++ b/reviewer/GIT-DESCRIPTOR-BOUNDARY.md @@ -27,7 +27,9 @@ accepted control connection, with the current supervisor's kernel peer credentials. The namespace-local `/broker/worker.sock` alias does not change the kernel's stored server pathname. The accepted connection's original peer pidfd must match the retained worker. Exactly one pidfd must match the retained original supervisor. -The measured Go runtime's CLOEXEC eventpoll and eventfd handles are permitted. +The inventory must contain exactly eight descriptors, including exactly one +CLOEXEC eventpoll and one CLOEXEC eventfd for the pinned Go runtime. Extra +poller handles are not admitted merely because they share those anonymous types. Extra sockets, directories, regular files, pipes, pidfds and other anonymous objects refuse, even when they have CLOEXEC. Socket domain checks never exercise inherited Internet sockets. @@ -44,7 +46,7 @@ the dynamic loader's temporary library descriptors before inert readiness. An explicit private-namespace fixture runs an ordinary measured Go executable, authenticates the original supervisor and disables dumpability. It supplies a real export pipe and tests the fixed roles plus actual extra host file, -directory, Internet socket and inheritable descriptor cases. It uses trusted +directory, Internet socket, inheritable descriptor and extra eventfd/epoll cases. It uses trusted empty fixture storage, never candidate input or a production storage fallback. ```sh diff --git a/reviewer/GIT-FACTORY.md b/reviewer/GIT-FACTORY.md index 170c807a..4fc44ce6 100644 --- a/reviewer/GIT-FACTORY.md +++ b/reviewer/GIT-FACTORY.md @@ -10,7 +10,11 @@ Construction takes exclusive lifecycle ownership of a newly opened unit owner, export archive and prepared runtime. It validates the fixed manifest/helper and runtime pins, then performs startup recovery before requesting any credential generation. The composing service must call this before credential Refresh and -retain the factory until controller work has joined. Credentials remain shared +retain the factory until controller work has joined. Before local handles are retained, a nil factory with an error leaves inputs +owned by its caller. After ownership transfers, a failed startup returns a +complete nonnil factory with the error. Retain that handle for explicit Recover +or Close; Capture and Restore remain fenced until recovery succeeds. Unknown +units keep their runtime and owner lock. Credentials remain shared with the publisher; the source factory never exposes a raw token or refresh capability to a worker or broker. @@ -19,7 +23,9 @@ capability to a worker or broker. Each capture spends a random 128-bit attempt nonce. Its binding fixes the release hash, canonical runtime-manifest hash and immutable PR head/base/merge base. A fresh private attempt directory contains separate bootstrap and broker -sockets. No failed or uncertain attempt automatically starts again. +sockets. No failed or uncertain attempt automatically starts again. Unix pathname bounds +are checked against each actual bootstrap/worker/broker socket path, including +the fixed nonce directory, rather than an arbitrary short root limit. The factory seals the fixed non-executable manifest, duplicates measured runtime and helper handles, and retains the original sealed worker image and supervisor @@ -97,6 +103,10 @@ owner proves physical stop. A canonical terminal `discarded` receipt is fsynced before removing the bounded owned payload. Recovery can repeat after a crash; the nonce remains spent, and discarded intake can never be reopened as source. A later fresh nonce may stage after the incomplete attempt becomes terminal. +The same retirement runs after a failed capture in the current process, once +its physical stop and credential-generation acceptance are known. Unknown or +corrupt outcomes fence Capture/Restore until explicit Recover first drains the +retained local runs; recovery does not restart them. Staged admitted source is retained; this path does not discard a complete export. All existing 128-record bounds remain. The factory retains its private working directories as recovery artifacts; it does not recursively delete a replacement diff --git a/reviewer/git_descriptor_boundary_linux.go b/reviewer/git_descriptor_boundary_linux.go index e7f43771..03822d53 100644 --- a/reviewer/git_descriptor_boundary_linux.go +++ b/reviewer/git_descriptor_boundary_linux.go @@ -230,11 +230,12 @@ func gitObserveDescriptor(fd int) (gitDescriptorObservation, error) { // This is a private policy check, not reusable admission authority. Expected // pipe/pidfd identities come from factory-owned retained kernel handles. func validateGitDescriptors(rows []gitDescriptorObservation, export gitKernelFileID, supervisor gitPIDFDIdentity, controlPath string) error { - if len(rows) < 6 || len(rows) > gitDescriptorLimit || export == (gitKernelFileID{}) || supervisor == (gitPIDFDIdentity{}) || !filepath.IsAbs(controlPath) || filepath.Clean(controlPath) != controlPath || filepath.Base(controlPath) != "worker.sock" || len(controlPath) > 107 { + if len(rows) != 8 || export == (gitKernelFileID{}) || supervisor == (gitPIDFDIdentity{}) || !filepath.IsAbs(controlPath) || filepath.Clean(controlPath) != controlPath || filepath.Base(controlPath) != "worker.sock" || len(controlPath) > 107 { return errGitUnit } seen := map[int]bool{} controlCount, exportCount, supervisorCount := 0, 0, 0 + eventCount, pollCount := 0, 0 for _, row := range rows { if row.Number < 0 || seen[row.Number] { return errGitUnit @@ -261,13 +262,15 @@ func validateGitDescriptors(rows []gitDescriptorObservation, export gitKernelFil controlCount++ case row.Mode&unix.S_IFMT == 0 && row.File == gitKernelFileID(supervisor): supervisorCount++ - case row.Mode&unix.S_IFMT == 0 && (row.Target == "anon_inode:[eventpoll]" || row.Target == "anon_inode:[eventfd]"): - // The measured Go runtime's private CLOEXEC poller handles. + case row.Mode&unix.S_IFMT == 0 && row.Target == "anon_inode:[eventpoll]": + pollCount++ + case row.Mode&unix.S_IFMT == 0 && row.Target == "anon_inode:[eventfd]": + eventCount++ default: return errGitUnit } } - if !seen[0] || !seen[1] || !seen[2] || controlCount != 1 || exportCount != 1 || supervisorCount != 1 { + if !seen[0] || !seen[1] || !seen[2] || controlCount != 1 || exportCount != 1 || supervisorCount != 1 || eventCount != 1 || pollCount != 1 { return errGitUnit } return nil diff --git a/reviewer/git_descriptor_boundary_linux_test.go b/reviewer/git_descriptor_boundary_linux_test.go index 634989f9..f749499c 100644 --- a/reviewer/git_descriptor_boundary_linux_test.go +++ b/reviewer/git_descriptor_boundary_linux_test.go @@ -52,10 +52,19 @@ func TestGitDescriptorPolicyRefusesExtraCapabilities(t *testing.T) { if validateGitDescriptors(rows, export, supervisor, "/broker/worker.sock") != nil { t.Fatal("fixed static descriptor policy") } - for _, mode := range []string{"missing-standard", "stdio-socket", "stdio-read", "inheritable", "wrong-export", "export-read", "wrong-supervisor", "wrong-peer", "internet", "wrong-control-path", "unknown-anon", "extra-regular", "extra-directory", "extra-socket", "duplicate", "duplicate-control"} { + for _, mode := range []string{"missing-standard", "stdio-socket", "stdio-read", "inheritable", "wrong-export", "export-read", "wrong-supervisor", "wrong-peer", "internet", "wrong-control-path", "unknown-anon", "extra-regular", "extra-directory", "extra-socket", "duplicate", "duplicate-control", "extra-eventfd", "extra-epoll", "replace-poller"} { t.Run(mode, func(t *testing.T) { bad := append([]gitDescriptorObservation(nil), rows...) switch mode { + case "extra-eventfd", "extra-epoll": + extra := bad[6] + if mode == "extra-eventfd" { + extra = bad[7] + } + extra.Number = 11 + bad = append(bad, extra) + case "replace-poller": + bad[6].Target = "anon_inode:[eventfd]" case "missing-standard": bad = bad[1:] case "stdio-socket": diff --git a/reviewer/git_factory_recovery_linux.go b/reviewer/git_factory_recovery_linux.go index 9f982e0f..9d44d39b 100644 --- a/reviewer/git_factory_recovery_linux.go +++ b/reviewer/git_factory_recovery_linux.go @@ -114,3 +114,67 @@ func recoverGitFactory(ctx context.Context, unit *gitUnitOwner, archive *gitExpo } return retained, nil } + +// A known stopped failure must not poison the next fresh nonce until restart. +// The run stays registered, and no intake can retire while its generation is +// unaccepted or its owned broker/acquisition callbacks have not joined. +func (f *gitSourceFactory) retireFailedCapture(ctx context.Context, run *gitFactoryRun) error { + if ctx == nil || run == nil || run.unit != f.unit || run.archive != f.archive || f.runs[run.binding.Attempt] != run { + return errGitUnit + } + run.mu.Lock() + stopped, closed, generation := run.stopped, run.closed, run.generation + run.mu.Unlock() + if !stopped || !closed { + return errGitUnit + } + if generation != nil { + if generation.owner != f.credentials.state { + return errGitUnit + } + generation.owner.mu.Lock() + released := generation.released + generation.owner.mu.Unlock() + if !released { + return errGitUnit + } + } + if !f.archive.enter(ctx) { + return errGitUnit + } + var state string + inspect := func() error { + if f.archive.verifyRoot() != nil { + return errGitUnit + } + if _, err := f.archive.root.Lstat(gitExportDirectory(run.binding.Attempt)); os.IsNotExist(err) { + return nil + } else if err != nil { + return errGitUnit + } + root, err := f.archive.attempt(run.binding.Attempt) + if err != nil { + return errGitUnit + } + defer root.Close() + record, err := readGitExportRecord(root) + if err != nil || record.Binding != run.binding { + return errGitUnit + } + state = record.State + return nil + } + err := inspect() + f.archive.leave() + if err != nil { + return errGitUnit + } + switch state { + case "receiving", "discarded": + return f.archive.discardStoppedIntake(ctx, f.unit, run.binding) + case "", "staged": + return nil + default: + return errGitUnit + } +} diff --git a/reviewer/git_factory_run_linux.go b/reviewer/git_factory_run_linux.go index 24bd768a..e94c3c46 100644 --- a/reviewer/git_factory_run_linux.go +++ b/reviewer/git_factory_run_linux.go @@ -9,6 +9,7 @@ import ( "net" "os" "path/filepath" + "strings" "sync" "time" @@ -37,6 +38,18 @@ type gitFactoryRun struct { stopOperation chan struct{} } +func gitFactoryRunPaths(directory string) bool { + if !filepath.IsAbs(directory) || filepath.Clean(directory) != directory || strings.ContainsRune(directory, 0) { + return false + } + for _, name := range []string{"bootstrap.sock", "broker/worker.sock", "broker/git.sock"} { + if len(filepath.Join(directory, name)) > 107 { + return false + } + } + return true +} + func (r *gitFactoryRun) retainGeneration(g *githubReaderGeneration) { r.mu.Lock() r.generation = g @@ -80,7 +93,7 @@ func retainGitFactoryManifest(binding gitMaterializationBinding, m gitGrantManif // factory must retain their protected backing until this run is physically // stopped. Construction never starts a unit or dispatches GitHub HTTP. func newGitFactoryRun(ctx context.Context, unit *gitUnitOwner, archive *gitExportArchive, binding gitMaterializationBinding, m gitGrantManifest, runtime, helper *os.File, directory string) (_ *gitFactoryRun, result error) { - if ctx == nil || ctx.Err() != nil || unit == nil || archive == nil || runtime == nil || helper == nil || !m.valid(binding) || !filepath.IsAbs(directory) || filepath.Clean(directory) != directory || len(directory) > 70 || privateDirectory(directory) != nil { + if ctx == nil || ctx.Err() != nil || unit == nil || archive == nil || runtime == nil || helper == nil || !m.valid(binding) || !gitFactoryRunPaths(directory) || privateDirectory(directory) != nil { return nil, errGitUnit } resolved, err := filepath.EvalSymlinks(directory) diff --git a/reviewer/git_factory_run_linux_test.go b/reviewer/git_factory_run_linux_test.go index 3dbdccf2..71df24bc 100644 --- a/reviewer/git_factory_run_linux_test.go +++ b/reviewer/git_factory_run_linux_test.go @@ -10,6 +10,7 @@ import ( "io" "os" "path/filepath" + "strings" "testing" "time" ) @@ -273,3 +274,15 @@ func TestGitFactoryBrokerSetupFailureRetiresOnlyKnownUnstartedLease(t *testing.T t.Fatal("setup refusal dispatched a unit") } } + +func TestGitFactorySocketPathBounds(t *testing.T) { + longest := "/" + strings.Repeat("x", 87) + if len(filepath.Join(longest, "broker", "worker.sock")) != 107 || !gitFactoryRunPaths(longest) { + t.Fatal("valid maximum socket path refused") + } + for _, path := range []string{longest + "x", "relative", "/tmp/../other", "/tmp/invalid\x00"} { + if gitFactoryRunPaths(path) { + t.Fatal("invalid socket path accepted") + } + } +} diff --git a/reviewer/git_mount_boundary_linux_test.go b/reviewer/git_mount_boundary_linux_test.go index de3e3070..cd4ee5c0 100644 --- a/reviewer/git_mount_boundary_linux_test.go +++ b/reviewer/git_mount_boundary_linux_test.go @@ -268,7 +268,7 @@ func TestGitDescriptorActualPrivateWorker(t *testing.T) { if os.Getenv("SFL_GIT_DESCRIPTOR_KERNEL") != "1" { t.Skip("explicit aggregate descriptor qualification using actual private worker roles") } - for _, mode := range []string{"clean", "host-file", "host-directory", "internet-socket", "inheritable"} { + for _, mode := range []string{"clean", "host-file", "host-directory", "internet-socket", "inheritable", "extra-eventfd", "extra-epoll"} { t.Run(mode, func(t *testing.T) { gitMountPrivateFixture(t, mode) }) } } @@ -347,6 +347,16 @@ func gitMountPrivateFixture(t *testing.T, descriptorMode string) { defer supervisor.Close() var extra *os.File switch descriptorMode { + case "extra-eventfd", "extra-epoll": + var fd int + if descriptorMode == "extra-eventfd" { + fd, err = unix.Eventfd(0, unix.EFD_CLOEXEC) + } else { + fd, err = unix.EpollCreate1(unix.EPOLL_CLOEXEC) + } + if err == nil { + extra = os.NewFile(uintptr(fd), "fixture-extra-poller") + } case "host-directory": extra, err = os.Open(t.TempDir()) case "internet-socket": diff --git a/reviewer/git_source_factory_linux.go b/reviewer/git_source_factory_linux.go index 37860db6..194b2bd3 100644 --- a/reviewer/git_source_factory_linux.go +++ b/reviewer/git_source_factory_linux.go @@ -9,6 +9,8 @@ import ( "os" "path/filepath" "sort" + "strings" + "time" ) // Internal service composition, never a model capability or serialized gate. @@ -30,14 +32,17 @@ type gitSourceFactory struct { packets map[string]string materialized map[string]*gitMaterialization closed bool + ready bool } // Opening performs startup recovery before asking the credential owner for any -// ready generation. Caller transfers exclusive lifecycle ownership of unit, -// archive and prepared runtime; credentials remain shared with the publisher. +// ready generation. Basic validation failures return nil and leave inputs owned +// by the caller. Once a nonnil factory is returned, including with an error, it +// owns unit/archive/runtime: retain it for Recover or Close. Capture/Restore are +// fenced until startup recovery succeeds. Credentials remain caller-owned. // Call this before credential Refresh. No refresh, App install or HTTP occurs. -func newGitSourceFactory(ctx context.Context, credentials *GitHubCredentialOwner, unit *gitUnitOwner, archive *gitExportArchive, runtime *gitRuntimeRoot, helper *os.File, m gitGrantManifest, release, releaseHash, directory string) (_ *gitSourceFactory, result error) { - if ctx == nil || ctx.Err() != nil || credentials == nil || credentials.state == nil || unit == nil || archive == nil || runtime == nil || helper == nil || !objectID.MatchString(release) || !validAuthHash(releaseHash) || !filepath.IsAbs(directory) || filepath.Clean(directory) != directory || len(directory) > 33 || privateDirectory(directory) != nil { +func newGitSourceFactory(ctx context.Context, credentials *GitHubCredentialOwner, unit *gitUnitOwner, archive *gitExportArchive, runtime *gitRuntimeRoot, helper *os.File, m gitGrantManifest, release, releaseHash, directory string) (*gitSourceFactory, error) { + if ctx == nil || ctx.Err() != nil || credentials == nil || credentials.state == nil || unit == nil || archive == nil || runtime == nil || helper == nil || !objectID.MatchString(release) || !validAuthHash(releaseHash) || !filepath.IsAbs(directory) || filepath.Clean(directory) != directory || !gitFactoryRunPaths(filepath.Join(directory, "run-"+strings.Repeat("a", 32))) || privateDirectory(directory) != nil { return nil, errGitUnit } resolved, err := filepath.EvalSymlinks(directory) @@ -76,52 +81,99 @@ func newGitSourceFactory(ctx context.Context, credentials *GitHubCredentialOwner if err != nil { return nil, errGitUnit } - f := &gitSourceFactory{operation: make(chan struct{}, 1), credentials: credentials, unit: unit, archive: archive, runtime: runtime, manifest: m, release: release, releaseHash: releaseHash, directory: directory, root: root, runs: map[string]*gitFactoryRun{}, packets: map[string]string{}, materialized: map[string]*gitMaterialization{}} - f.operation <- struct{}{} - defer func() { - if result != nil { - if f.helper != nil { - f.helper.Close() - } - root.Close() - } - }() - f.identity, err = root.Stat(".") + identity, err := root.Stat(".") if err != nil { + root.Close() return nil, errGitUnit } - f.helper, err = duplicateGitControlFile(helper) + retainedHelper, err := duplicateGitControlFile(helper) if err != nil { + root.Close() return nil, errGitUnit } - f.bindings, err = recoverGitFactory(ctx, unit, archive) + // Ownership transfers only after every required local handle is retained. + // A returned recovery handle is therefore complete even when recovery fails. + f := &gitSourceFactory{operation: make(chan struct{}, 1), credentials: credentials, unit: unit, archive: archive, runtime: runtime, manifest: m, release: release, releaseHash: releaseHash, directory: directory, root: root, identity: identity, helper: retainedHelper, runs: map[string]*gitFactoryRun{}, packets: map[string]string{}, materialized: map[string]*gitMaterialization{}} + f.operation <- struct{}{} + if f.Recover(ctx) != nil { + return f, errGitUnit + } + return f, nil +} + +// Recover is a trusted explicit recovery action. It never refreshes credentials +// or restarts an acquisition. Local unknown runs first drain their actual broker +// and generation; only startup owners can rely on the old process's released +// cross-process lock as proof that its broker cannot still execute. +func (f *gitSourceFactory) Recover(ctx context.Context) error { + if !f.enter(ctx) { + return errGitUnit + } + defer f.leave() + if f.closed { + return errGitUnit + } + if f.ready { + return nil + } + if f.helper == nil || f.identity == nil || f.checkRoot() != nil { + return errGitUnit + } + for _, run := range f.runs { + run.mu.Lock() + generation := run.generation + run.mu.Unlock() + if generation != nil { + if generation.stop(ctx) != nil { + return errGitUnit + } + } else if run.stop(ctx) != nil { + return errGitUnit + } + if run.close() != nil { + return errGitUnit + } + } + f.credentials.state.mu.Lock() + busy := len(f.credentials.state.acquisitions) != 0 + f.credentials.state.mu.Unlock() + if busy { + return errGitUnit + } + bindings, err := recoverGitFactory(ctx, f.unit, f.archive) if err != nil { - return nil, errGitUnit + return errGitUnit } - if !archive.enter(ctx) { - return nil, errGitUnit + if !f.archive.enter(ctx) { + return errGitUnit } - exports, err := archive.records() - archive.leave() + exports, err := f.archive.records() + f.archive.leave() if err != nil { - return nil, errGitUnit + return errGitUnit } + packets := map[string]string{} for _, record := range exports { if record.State == "staged" { - f.packets[record.Binding.Attempt] = record.PacketHash + packets[record.Binding.Attempt] = record.PacketHash } } - return f, nil + f.bindings, f.packets, f.ready = bindings, packets, true + return nil } func (f *gitSourceFactory) enter(ctx context.Context) bool { - if f == nil || ctx == nil { + if f == nil || ctx == nil || ctx.Err() != nil { return false } select { case <-ctx.Done(): return false case <-f.operation: + if ctx.Err() != nil { + f.leave() + return false + } return true } } @@ -141,7 +193,7 @@ func (f *gitSourceFactory) Capture(ctx context.Context, snapshot Snapshot) (*Sna return nil, errGitUnit } defer f.leave() - if f.closed || f.checkRoot() != nil || snapshot.Validate() != nil || snapshot.RepositoryID != f.credentials.state.options.Journal.enrollment.RepositoryID { + if f.closed || !f.ready || f.checkRoot() != nil || snapshot.Validate() != nil || snapshot.RepositoryID != f.credentials.state.options.Journal.enrollment.RepositoryID { return nil, errGitUnit } var nonce [16]byte @@ -160,11 +212,18 @@ func (f *gitSourceFactory) Capture(ctx context.Context, snapshot Snapshot) (*Sna defer runtime.Close() run, err := newGitFactoryRun(ctx, f.unit, f.archive, binding, f.manifest, runtime, f.helper, filepath.Join(f.directory, name)) if err != nil { + f.ready = false return nil, errGitUnit } f.runs[binding.Attempt] = run m, err := runGitFactoryAcquisition(ctx, f.credentials, binding, run) if err != nil { + phase, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second) + retireErr := f.retireFailedCapture(phase, run) + cancel() + if retireErr != nil { + f.ready = false + } return nil, errGitUnit } evidence, err := captureGitMaterializationEvidence(ctx, m) @@ -183,7 +242,7 @@ func (f *gitSourceFactory) Restore(ctx context.Context, job Job) (*JobEvidence, return nil, errGitUnit } defer f.leave() - if f.closed || f.checkRoot() != nil || job.Validate() != nil || job.Release != f.release || job.Snapshot.RepositoryID != f.credentials.state.options.Journal.enrollment.RepositoryID { + if f.closed || !f.ready || f.checkRoot() != nil || job.Validate() != nil || job.Release != f.release || job.Snapshot.RepositoryID != f.credentials.state.options.Journal.enrollment.RepositoryID { return nil, ErrFenced } attempts := []string{} @@ -256,7 +315,7 @@ func (f *gitSourceFactory) Close(ctx context.Context) error { } delete(f.materialized, attempt) } - if f.helper.Close() != nil { + if f.helper != nil && f.helper.Close() != nil { failed = true } if f.root.Close() != nil { diff --git a/reviewer/git_source_factory_linux_test.go b/reviewer/git_source_factory_linux_test.go index 6066ea0b..c24834aa 100644 --- a/reviewer/git_source_factory_linux_test.go +++ b/reviewer/git_source_factory_linux_test.go @@ -5,11 +5,14 @@ package reviewer import ( "bytes" "context" + "errors" "io" "os" + "path/filepath" "reflect" "strings" "testing" + "time" ) func gitFactoryWorkRoot(t *testing.T) string { @@ -135,3 +138,150 @@ func TestGitSourceFactoryRestoresAdmittedPacketWithoutHostGit(t *testing.T) { t.Fatal("restore dispatched credential work") } } + +func TestGitSourceFactoryValidLongRootAndFailedStartupOwnership(t *testing.T) { + for _, mode := range []string{"long-root", "failed-recovery"} { + t.Run(mode, func(t *testing.T) { + r, f, runtime := gitFactoryAssetsFixture(t) + helper, err := duplicateGitControlFile(r.files[2]) + if err != nil { + t.Fatal(err) + } + defer helper.Close() + if r.stop(context.Background()) != nil || r.close() != nil { + t.Fatal("prepared fixture cleanup") + } + root := gitFactoryWorkRoot(t) + if mode == "long-root" { + root = filepath.Join(root, strings.Repeat("x", 40-len(root))) + if len(root) <= 33 || len(filepath.Join(root, "run-"+strings.Repeat("a", 32), "broker", "worker.sock")) > 107 || os.Mkdir(root, 0700) != nil { + t.Fatal("long valid Unix path fixture") + } + } else { + _, binding, store := gitMaterializationFixture(t, "sha1") + binding.Attempt = strings.Repeat("d", 32) + // Write a valid orphan archive through its real framing path. + // Re-encode the binding in trusted synthetic producer output. + var frame bytes.Buffer + if writeGitMaterialization(context.Background(), &frame, store, binding, DefaultGitAcquisitionBounds()) != nil { + t.Fatal("orphan fixture") + } + wire := frame.Bytes() + m, err := r.archive.stage(context.Background(), binding, io.NopCloser(bytes.NewReader(wire))) + if err != nil { + t.Fatal(err) + } + m.Close() + } + owner, _, _, _, _ := credentialOwnerFixture(t) + factory, err := newGitSourceFactory(context.Background(), owner, f.owner, r.archive, runtime, helper, r.manifest, testJob().Release, r.binding.ReleaseHash, root) + if mode == "long-root" { + if err != nil || factory == nil { + t.Fatal("valid deployment root refused", err) + } + } else { + if err == nil || factory == nil { + t.Fatal("failed startup lost transferred ownership handle", err) + } + } + if factory.Close(context.Background()) != nil { + t.Fatal("transferred startup resources not closed") + } + }) + } +} + +func TestGitSourceFactoryFailedStartupRetainsUnknownOwnerUntilRecovery(t *testing.T) { + r, f, runtime := gitFactoryAssetsFixture(t) + helper, err := duplicateGitControlFile(r.files[2]) + if err != nil { + t.Fatal(err) + } + defer helper.Close() + f.manager.startErr = errors.New("fake unknown accepted start") + if f.owner.start(context.Background(), r.binding, "/private/bootstrap.sock") == nil { + t.Fatal("unknown start fixture") + } + owner, _, _, _, signs := credentialOwnerFixture(t) + factory, err := newGitSourceFactory(context.Background(), owner, f.owner, r.archive, runtime, helper, r.manifest, testJob().Release, r.binding.ReleaseHash, gitFactoryWorkRoot(t)) + if err == nil || factory == nil { + t.Fatal("unknown startup lost owner handle", err) + } + if factory.Close(context.Background()) == nil || factory.closed || f.owner.closed { + t.Fatal("uncertain startup disposed live ownership") + } + if evidence, err := factory.Capture(context.Background(), testSnapshot()); err == nil || evidence != nil || signs.Load() != 0 { + t.Fatal("failed startup admitted new work") + } + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) + other, lockErr := newGitUnitOwner(ctx, f.root, f.stub, f.stubHash, f.manager, f.manager.kernel) + cancel() + if lockErr == nil || other != nil { + t.Fatal("failed startup released cross-process owner lock") + } + fd, err := runtime.directoryFD() + if err != nil { + t.Fatal("unknown startup lost runtime") + } + fd.Close() + // Explicit synthetic next-boot evidence resolves this fake manager branch. + // The fixture's separate actual inert child is still reaped by its cleanup. + f.manager.kernel.mu.Lock() + f.manager.kernel.boot = "synthetic-next-boot" + f.manager.kernel.mu.Unlock() + if factory.Recover(context.Background()) != nil || !factory.ready { + t.Fatal("explicit known-terminal startup recovery failed") + } + if r.stop(context.Background()) != nil || r.close() != nil || factory.Close(context.Background()) != nil { + t.Fatal("recovered startup cleanup") + } +} + +func TestGitSourceFactoryRetiresKnownStoppedIntakeAfterLeaseAcceptance(t *testing.T) { + r, f, runtime := gitFactoryAssetsFixture(t) + owner, _, _, _, _ := credentialOwnerFixture(t) + factory, err := newGitSourceFactory(context.Background(), owner, f.owner, r.archive, runtime, r.files[2], r.manifest, testJob().Release, r.binding.ReleaseHash, gitFactoryWorkRoot(t)) + if err != nil { + t.Fatal(err) + } + if owner.Refresh(context.Background()) != nil { + t.Fatal("fake pair qualification") + } + g, err := owner.retainReaderGeneration(context.Background(), r.stop) + if err != nil { + t.Fatal(err) + } + r.retainGeneration(g) + factory.runs[r.binding.Attempt] = r + if m, err := r.archive.stage(context.Background(), r.binding, io.NopCloser(strings.NewReader("incomplete"))); err == nil || m != nil { + t.Fatal("partial frame staged") + } + if r.stop(context.Background()) != nil || r.close() != nil { + t.Fatal("known physical stop") + } + if factory.retireFailedCapture(context.Background(), r) == nil { + t.Fatal("unaccepted generation allowed intake retirement") + } + if g.stop(context.Background()) != nil { + t.Fatal("accept stopped generation") + } + if factory.retireFailedCapture(context.Background(), r) != nil { + t.Fatal("known stopped failure poisoned intake") + } + r.archive.enter(context.Background()) + attempt, err := r.archive.attempt(r.binding.Attempt) + if err != nil { + r.archive.leave() + t.Fatal(err) + } + record, err := readGitExportRecord(attempt) + _, payloadErr := attempt.Lstat("payload.bin") + attempt.Close() + r.archive.leave() + if err != nil || record.State != "discarded" || !os.IsNotExist(payloadErr) { + t.Fatal("same-process recovery not durable", record, err, payloadErr) + } + if factory.Close(context.Background()) != nil { + t.Fatal("recovered failed capture cleanup") + } +} From bf3dce49d71e77850ea4dcbc6333245eeacc6b50 Mon Sep 17 00:00:00 2001 From: Franz Hemmer <8227352+HemSoft@users.noreply.github.com> Date: Wed, 21 Oct 2026 12:42:54 -0400 Subject: [PATCH 06/12] Preserve durable factory stops and gate nonce allocation on readiness --- reviewer/git_factory_recovery_linux.go | 4 +-- reviewer/git_factory_run_linux.go | 2 +- reviewer/git_factory_unit_linux.go | 23 +++++++++++++++++ reviewer/git_source_factory_linux.go | 4 +-- reviewer/git_source_factory_linux_test.go | 31 +++++++++++++++++++++-- 5 files changed, 57 insertions(+), 7 deletions(-) diff --git a/reviewer/git_factory_recovery_linux.go b/reviewer/git_factory_recovery_linux.go index 9d44d39b..9da9a288 100644 --- a/reviewer/git_factory_recovery_linux.go +++ b/reviewer/git_factory_recovery_linux.go @@ -14,7 +14,7 @@ import ( // removing the bounded payload, so interruption never makes a missing archive // record look like permission to recapture the spent attempt. func (a *gitExportArchive) discardStoppedIntake(ctx context.Context, unit *gitUnitOwner, binding gitMaterializationBinding) error { - if ctx == nil || unit == nil || !binding.valid() || unit.stop(ctx, binding) != nil { + if ctx == nil || unit == nil || !binding.valid() || unit.confirmFactoryStop(ctx, binding) != nil { return errGitExportArchive } if !a.enter(ctx) { @@ -82,7 +82,7 @@ func recoverGitFactory(ctx context.Context, unit *gitUnitOwner, archive *gitExpo } byAttempt := map[string]gitUnitRecord{} for _, record := range records { - if unit.stop(ctx, record.Binding) != nil { + if unit.confirmFactoryStop(ctx, record.Binding) != nil { return nil, errGitUnit } byAttempt[record.Binding.Attempt] = record diff --git a/reviewer/git_factory_run_linux.go b/reviewer/git_factory_run_linux.go index e94c3c46..e58a4381 100644 --- a/reviewer/git_factory_run_linux.go +++ b/reviewer/git_factory_run_linux.go @@ -406,7 +406,7 @@ func (r *gitFactoryRun) stop(ctx context.Context) error { // there was no broker work to retain; a missing return never does. brokerStopped := brokerDone == nil || r.brokerAudit.Version == 1 && (r.brokerAudit.Stopped || r.brokerAudit.Connections == 0 && r.brokerAudit.Requests == 0) r.mu.Unlock() - if !brokerStopped || r.unit.stop(ctx, r.binding) != nil || ctx.Err() != nil { + if !brokerStopped || r.unit.confirmFactoryStop(ctx, r.binding) != nil || ctx.Err() != nil { return errGitUnit } r.mu.Lock() diff --git a/reviewer/git_factory_unit_linux.go b/reviewer/git_factory_unit_linux.go index 159cdfa8..220ca36e 100644 --- a/reviewer/git_factory_unit_linux.go +++ b/reviewer/git_factory_unit_linux.go @@ -13,6 +13,29 @@ import ( "golang.org/x/sys/unix" ) +// A canonical protected stopped receipt is permanent: start and grant cannot +// re-arm that nonce. Reopening must not require systemd's collected unit or its +// old cgroup to still exist. Nonterminal records still need the full live stop. +func (o *gitUnitOwner) confirmFactoryStop(ctx context.Context, binding gitMaterializationBinding) error { + if ctx == nil || !binding.valid() || !o.enter(ctx) { + return errGitUnit + } + if o.closed || ctx.Err() != nil { + o.leave() + return errGitUnit + } + r, err := o.read(binding.Attempt) + terminal := err == nil && r.Binding == binding && r.Stopped && r.Revoked && ctx.Err() == nil + o.leave() + if terminal { + return nil + } + if err != nil { + return errGitUnit + } + return o.stop(ctx, binding) +} + // Keep these checks under the unit owner's operation lock. They confer no // serialized qualification and do not arm or replay a grant after recovery. func (o *gitUnitOwner) factoryUnit(ctx context.Context, binding gitMaterializationBinding) (gitUnitRecord, error) { diff --git a/reviewer/git_source_factory_linux.go b/reviewer/git_source_factory_linux.go index 194b2bd3..18497666 100644 --- a/reviewer/git_source_factory_linux.go +++ b/reviewer/git_source_factory_linux.go @@ -193,7 +193,7 @@ func (f *gitSourceFactory) Capture(ctx context.Context, snapshot Snapshot) (*Sna return nil, errGitUnit } defer f.leave() - if f.closed || !f.ready || f.checkRoot() != nil || snapshot.Validate() != nil || snapshot.RepositoryID != f.credentials.state.options.Journal.enrollment.RepositoryID { + if f.closed || !f.ready || f.checkRoot() != nil || snapshot.Validate() != nil || snapshot.RepositoryID != f.credentials.state.options.Journal.enrollment.RepositoryID || f.credentials.Ready(ctx) != nil { return nil, errGitUnit } var nonce [16]byte @@ -259,7 +259,7 @@ func (f *gitSourceFactory) Restore(ctx context.Context, job Job) (*JobEvidence, binding := f.bindings[attempt] // Revalidate the durable physical-stop journal before using retained source. // There is no network, recapture, Git parser or earlier-label fallback here. - if f.unit.stop(ctx, binding) != nil { + if f.unit.confirmFactoryStop(ctx, binding) != nil { return nil, ErrFenced } m := f.materialized[attempt] diff --git a/reviewer/git_source_factory_linux_test.go b/reviewer/git_source_factory_linux_test.go index c24834aa..7dc7f23d 100644 --- a/reviewer/git_source_factory_linux_test.go +++ b/reviewer/git_source_factory_linux_test.go @@ -38,8 +38,27 @@ func TestGitSourceFactoryStartupPrecedesCredentialWork(t *testing.T) { if r.stop(context.Background()) != nil || r.close() != nil { t.Fatal("prepared fixture cleanup") } - if evidence, err := factory.Capture(context.Background(), testSnapshot()); err == nil || evidence != nil { - t.Fatal("unready credentials created source") + f.owner.enter(context.Background()) + before, beforeErr := f.owner.records() + f.owner.leave() + if beforeErr != nil { + t.Fatal(beforeErr) + } + beforePaths, pathErr := os.ReadDir(factory.directory) + if pathErr != nil { + t.Fatal(pathErr) + } + for i := 0; i < 130; i++ { + if evidence, err := factory.Capture(context.Background(), testSnapshot()); err == nil || evidence != nil { + t.Fatal("unready credentials created source") + } + } + f.owner.enter(context.Background()) + after, afterErr := f.owner.records() + f.owner.leave() + afterPaths, pathErr := os.ReadDir(factory.directory) + if afterErr != nil || pathErr != nil || len(before) != len(after) || len(beforePaths) != len(afterPaths) { + t.Fatalf("unready captures consumed durable slots/directories: %d/%d -> %d/%d", len(before), len(beforePaths), len(after), len(afterPaths)) } if signs.Load() != 0 || f.manager.starts != 0 { t.Fatal("unready capture refreshed credentials or launched a unit") @@ -104,6 +123,14 @@ func TestGitSourceFactoryRestoresAdmittedPacketWithoutHostGit(t *testing.T) { if err != nil { t.Fatal("new startup owner", err) } + // A durable terminal receipt survives collection of the transient unit and + // its old cgroup. Restart and later Restore must not require them again. + f.manager.mu.Lock() + f.manager.observeErr = errors.New("fake NoSuchUnit after collection") + f.manager.mu.Unlock() + f.manager.kernel.mu.Lock() + f.manager.kernel.changed = true + f.manager.kernel.mu.Unlock() owner, _, _, _, signs := credentialOwnerFixture(t) factory, err := newGitSourceFactory(context.Background(), owner, reopened, r.archive, runtime, helper, r.manifest, testJob().Release, r.binding.ReleaseHash, gitFactoryWorkRoot(t)) if err != nil { From 56eebd1e14f27c63e55261e84faa86ce03267a79 Mon Sep 17 00:00:00 2001 From: Franz Hemmer <8227352+HemSoft@users.noreply.github.com> Date: Wed, 21 Oct 2026 13:15:14 -0400 Subject: [PATCH 07/12] Prove nonancestor inspection and retain source consumer lifetimes --- reviewer/GIT-DESCRIPTOR-BOUNDARY.md | 23 ++++ reviewer/git_mount_boundary_linux_test.go | 45 +++++- .../git_nonancestor_inspection_linux_test.go | 127 +++++++++++++++++ reviewer/git_source_factory_linux.go | 44 +++++- reviewer/git_source_factory_linux_test.go | 6 + reviewer/polling.go | 44 +++++- reviewer/polling_source_lifetime_test.go | 129 ++++++++++++++++++ 7 files changed, 410 insertions(+), 8 deletions(-) create mode 100644 reviewer/git_nonancestor_inspection_linux_test.go create mode 100644 reviewer/polling_source_lifetime_test.go diff --git a/reviewer/GIT-DESCRIPTOR-BOUNDARY.md b/reviewer/GIT-DESCRIPTOR-BOUNDARY.md index 39a90c4c..6c413fcb 100644 --- a/reviewer/GIT-DESCRIPTOR-BOUNDARY.md +++ b/reviewer/GIT-DESCRIPTOR-BOUNDARY.md @@ -60,6 +60,29 @@ it does not relax dumpability, change host policy or substitute pathname opens for retained kernel object identities. The explicit fixture's actual result must accompany any qualification claim. The ordinary suite skips this opt-in. +Inspection also has an explicit nonancestor fixture. An inert launcher exits +before observation, and the test checks the worker's actual ancestry through +PID 1. The worker verifies dumpability is zero before sending readiness. The +test checks the target user namespace's owner and its parent chain back to the +inspector, then exercises actual root, executable, mappings, mount and original +pidfd descriptor observation. No service, host capability or ptrace-policy +change is made. + +Linux grants a parent user namespace's matching owner capabilities in its child +and descendant namespaces. Ptrace's dumpability check permits `CAP_SYS_PTRACE` +in the target's user namespace. These rules differ from host capabilities and +do not depend on process ancestry. See +[user namespaces](https://man7.org/linux/man-pages/man7/user_namespaces.7.html) +and [ptrace access checks](https://man7.org/linux/man-pages/man2/ptrace.2.html). +The retained pidfd identifies the process; it does not override access checks. +Other host security policies can still refuse, and refusal never qualifies. + +```sh +SFL_GIT_DESCRIPTOR_KERNEL=1 SFL_GIT_BOOTSTRAP_NAMESPACE=1 \ + go -C reviewer test -race -count=1 -v \ + -run '^TestGitDescriptorActualNonancestorNondumpableWorker$' . +``` + This does not qualify sealed worker execution, the required whole-unit noswap tmpfs, installed-filter provenance, unit/cgroup ownership, authenticated export publication or controller recovery. Those remain separate factory requirements diff --git a/reviewer/git_mount_boundary_linux_test.go b/reviewer/git_mount_boundary_linux_test.go index cd4ee5c0..43280cf5 100644 --- a/reviewer/git_mount_boundary_linux_test.go +++ b/reviewer/git_mount_boundary_linux_test.go @@ -197,7 +197,11 @@ func TestGitMountNamespaceChild(t *testing.T) { unix.CloseOnExec(5) } } - if unix.Prctl(unix.PR_SET_DUMPABLE, 0, 0, 0, 0) != nil || writeGitControlPacket(ctx, conn, []byte("ready"), nil) != nil { + if unix.Prctl(unix.PR_SET_DUMPABLE, 0, 0, 0, 0) != nil { + t.Fatal("clear inert worker dumpability") + } + dumpable, err := unix.PrctlRetInt(unix.PR_GET_DUMPABLE, 0, 0, 0, 0) + if err != nil || dumpable != 0 || writeGitControlPacket(ctx, conn, []byte("ready"), nil) != nil { t.Fatal("inert mount readiness") } packet, files, err := readGitControlPacket(ctx, conn, 0) @@ -273,8 +277,9 @@ func TestGitDescriptorActualPrivateWorker(t *testing.T) { } } -func gitMountPrivateFixture(t *testing.T, descriptorMode string) { +func gitMountPrivateFixture(t *testing.T, descriptorMode string, nonancestor ...bool) { t.Helper() + detached := len(nonancestor) == 1 && nonancestor[0] if os.Getenv("SFL_GIT_BOOTSTRAP_NAMESPACE") != "1" { t.Skip("explicit inert private-namespace mount observation") } @@ -383,10 +388,32 @@ func gitMountPrivateFixture(t *testing.T, descriptorMode string) { if descriptorMode == "" { command.Stdout, command.Stderr = &output, &output } + if detached { + // Only this inert inspection fixture drops the parent-death option. + // Its worker has its own fixed deadline and is reaped by its helper. + args = append([]string(nil), args...) + for i, arg := range args { + if arg == "--die-with-parent" { + args = append(args[:i], args[i+1:]...) + break + } + } + raw, _ := json.Marshal(args) + launch := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestGitDescriptorNonancestorLauncher$") + launch.Env = []string{"SFL_GIT_NONANCESTOR_LAUNCHER=" + string(raw)} + launch.ExtraFiles = append(append([]*os.File(nil), command.ExtraFiles...), helper) + command = launch + } if command.Start() != nil { t.Fatal("inert mount namespace launch") } waited := false + if detached { + if command.Wait() != nil { + t.Fatal("inert nonancestor launcher") + } + waited = true + } defer func() { if !waited { command.Process.Kill() @@ -417,6 +444,10 @@ func gitMountPrivateFixture(t *testing.T, descriptorMode string) { t.Fatal(err) } defer unix.Close(original) + if detached { + defer unix.PidfdSendSignal(original, unix.SIGKILL, nil, 0) + gitAssertNonancestorInspector(t, identity, original) + } raw, err := gitReadMountInfo(ctx, identity, original) if err != nil { t.Fatal(err) @@ -481,7 +512,15 @@ func gitMountPrivateFixture(t *testing.T, descriptorMode string) { if checkGitMountBoundary(ctx, boundary, broker, binding) == nil { t.Fatal("unqualified host workspace became a source admission proof") } - if writeGitControlPacket(ctx, conn, []byte("finish"), nil) != nil || command.Wait() != nil { + if writeGitControlPacket(ctx, conn, []byte("finish"), nil) != nil { + t.Fatal("inert namespace completion") + } + if detached { + poll := []unix.PollFd{{Fd: int32(original), Events: unix.POLLIN}} + if n, err := unix.Poll(poll, 3000); err != nil || n != 1 || poll[0].Revents&unix.POLLIN == 0 { + t.Fatal("inert detached worker did not exit", n, err) + } + } else if command.Wait() != nil { t.Fatal("inert namespace completion") } waited = true diff --git a/reviewer/git_nonancestor_inspection_linux_test.go b/reviewer/git_nonancestor_inspection_linux_test.go new file mode 100644 index 00000000..b3626c11 --- /dev/null +++ b/reviewer/git_nonancestor_inspection_linux_test.go @@ -0,0 +1,127 @@ +//go:build linux + +package reviewer + +import ( + "encoding/json" + "fmt" + "os" + "os/exec" + "strconv" + "strings" + "testing" + + "golang.org/x/sys/unix" +) + +// This launcher exits before inspection. The inert namespace helper reparents, +// making the inspector a nonancestor without a service or host policy change. +func TestGitDescriptorNonancestorLauncher(t *testing.T) { + raw := os.Getenv("SFL_GIT_NONANCESTOR_LAUNCHER") + if raw == "" { + return + } + var args []string + if json.Unmarshal([]byte(raw), &args) != nil || len(args) > 128 || len(args) < 2 || args[len(args)-1] != "-test.run=^TestGitMountNamespaceChild$" { + os.Exit(2) + } + files := make([]*os.File, 8) + for i := range files { + files[i] = os.NewFile(uintptr(i+3), "inert-fixed-role") + unix.CloseOnExec(i + 3) + } + unix.CloseOnExec(11) + command := exec.Command("/proc/self/fd/11", args...) + command.Env = []string{} + command.ExtraFiles = files + if command.Start() != nil || command.Process.Release() != nil { + os.Exit(3) + } + os.Exit(0) +} + +func gitAssertNonancestorInspector(t *testing.T, identity GateIdentity, original int) { + t.Helper() + if gitCheckProcessPIDFD(identity, original) != nil { + t.Fatal("original detached worker identity") + } + pid := identity.PID + chain := []int{} + for steps := 0; steps < 32; steps++ { + if pid == os.Getpid() || pid <= 0 { + t.Fatal("inspector still an ancestor", chain) + } + chain = append(chain, pid) + if pid == 1 { + break + } + raw, err := os.ReadFile(fmt.Sprintf("/proc/%d/stat", pid)) + end := strings.LastIndexByte(string(raw), ')') + if err != nil || end < 0 { + t.Fatal("detached ancestry", err) + } + fields := strings.Fields(string(raw[end+1:])) + if len(fields) < 2 { + t.Fatal("detached ancestry fields") + } + pid, err = strconv.Atoi(fields[1]) + if err != nil { + t.Fatal(err) + } + } + if chain[len(chain)-1] != 1 || gitCheckProcessPIDFD(identity, original) != nil { + t.Fatal("ancestry or original identity not proven", chain) + } + namespace, err := os.Open(fmt.Sprintf("/proc/%d/ns/user", identity.PID)) + if err != nil { + t.Fatal("target user namespace", err) + } + defer namespace.Close() + owner, err := unix.IoctlGetUint32(int(namespace.Fd()), unix.NS_GET_OWNER_UID) + if err != nil || owner != uint32(os.Geteuid()) { + t.Fatal("inspector does not own target user namespace", owner, err) + } + inspector, err := os.Open("/proc/self/ns/user") + if err != nil { + t.Fatal(err) + } + defer inspector.Close() + inspectorID, err := gitStatFileID(inspector) + if err != nil { + t.Fatal(err) + } + current := namespace + parentOwned := false + for depth := 0; depth < 8; depth++ { + fd, err := unix.IoctlRetInt(int(current.Fd()), unix.NS_GET_PARENT) + if err != nil { + t.Fatal("target user namespace ancestry", err) + } + parent := os.NewFile(uintptr(fd), "inert-user-namespace-parent") + defer parent.Close() + parentID, err := gitStatFileID(parent) + if err != nil { + t.Fatal(err) + } + if parentID == inspectorID { + uid, err := unix.IoctlGetUint32(int(current.Fd()), unix.NS_GET_OWNER_UID) + if err != nil || uid != uint32(os.Geteuid()) { + t.Fatal("inspector's child user namespace owner", uid, err) + } + parentOwned = true + break + } + current = parent + } + if !parentOwned { + t.Fatal("target user namespace is not a descendant owned by inspector") + } + t.Logf("inspector=%d nonancestor worker chain=%v target user namespace owner=%d", os.Getpid(), chain, owner) +} + +func TestGitDescriptorActualNonancestorNondumpableWorker(t *testing.T) { + if os.Getenv("SFL_GIT_DESCRIPTOR_KERNEL") != "1" { + t.Skip("explicit inert nonancestor inspection qualification") + } + gitMountPrivateFixture(t, "clean", true) +} diff --git a/reviewer/git_source_factory_linux.go b/reviewer/git_source_factory_linux.go index 18497666..88f15d87 100644 --- a/reviewer/git_source_factory_linux.go +++ b/reviewer/git_source_factory_linux.go @@ -31,6 +31,8 @@ type gitSourceFactory struct { bindings map[string]gitMaterializationBinding packets map[string]string materialized map[string]*gitMaterialization + pendingCaptures map[*SnapshotEvidence]string + activeJobs map[*JobEvidence]string closed bool ready bool } @@ -234,6 +236,10 @@ func (f *gitSourceFactory) Capture(ctx context.Context, snapshot Snapshot) (*Sna f.bindings[binding.Attempt] = binding f.packets[binding.Attempt] = Digest(m.packet) f.materialized[binding.Attempt] = m + if f.pendingCaptures == nil { + f.pendingCaptures = map[*SnapshotEvidence]string{} + } + f.pendingCaptures[evidence] = binding.Attempt return evidence, nil } @@ -271,7 +277,39 @@ func (f *gitSourceFactory) Restore(ctx context.Context, job Job) (*JobEvidence, } f.materialized[attempt] = m } - return restoreGitMaterialization(ctx, m, job) + bound, err := restoreGitMaterialization(ctx, m, job) + if err != nil { + return nil, err + } + if f.activeJobs == nil { + f.activeJobs = map[*JobEvidence]string{} + } + f.activeJobs[bound] = attempt + return bound, nil +} + +func (f *gitSourceFactory) ReleaseCapture(ctx context.Context, evidence *SnapshotEvidence) error { + if !f.enter(ctx) { + return errGitUnit + } + defer f.leave() + if f.closed || evidence == nil || f.pendingCaptures[evidence] == "" { + return errGitUnit + } + delete(f.pendingCaptures, evidence) + return nil +} + +func (f *gitSourceFactory) ReleaseJob(ctx context.Context, bound *JobEvidence) error { + if !f.enter(ctx) { + return errGitUnit + } + defer f.leave() + if f.closed || bound == nil || f.activeJobs[bound] == "" { + return errGitUnit + } + delete(f.activeJobs, bound) + return nil } func (f *gitSourceFactory) Close(ctx context.Context) error { @@ -282,6 +320,9 @@ func (f *gitSourceFactory) Close(ctx context.Context) error { if f.closed { return nil } + if len(f.pendingCaptures) != 0 || len(f.activeJobs) != 0 { + return errGitUnit + } for _, run := range f.runs { run.mu.Lock() generation := run.generation @@ -330,3 +371,4 @@ func (f *gitSourceFactory) Close(ctx context.Context) error { // Compile-time guarantee that production uses the concrete fixed driver. var _ gitFactoryAcquisition = (*gitFactoryRun)(nil) +var _ PollSource = (*gitSourceFactory)(nil) diff --git a/reviewer/git_source_factory_linux_test.go b/reviewer/git_source_factory_linux_test.go index 7dc7f23d..d620a268 100644 --- a/reviewer/git_source_factory_linux_test.go +++ b/reviewer/git_source_factory_linux_test.go @@ -164,6 +164,12 @@ func TestGitSourceFactoryRestoresAdmittedPacketWithoutHostGit(t *testing.T) { if signs.Load() != 0 { t.Fatal("restore dispatched credential work") } + if factory.Close(context.Background()) == nil || factory.closed || factory.unit.closed { + t.Fatal("active restored evidence lost backing storage") + } + if factory.ReleaseJob(context.Background(), bound) != nil || factory.ReleaseJob(context.Background(), bound) == nil { + t.Fatal("restored evidence was not released exactly once") + } } func TestGitSourceFactoryValidLongRootAndFailedStartupOwnership(t *testing.T) { diff --git a/reviewer/polling.go b/reviewer/polling.go index d86e7373..f9a0211b 100644 --- a/reviewer/polling.go +++ b/reviewer/polling.go @@ -23,12 +23,26 @@ type PollDiscovery interface { PollScope(context.Context, Scope) (RemoteState, Discovery, error) } +// PollSource owns captured source until admission or work has finished. Legacy +// raw callbacks remain available to fake transports, with no retention hook. +type PollSource interface { + Capture(context.Context, Snapshot) (*SnapshotEvidence, error) + Restore(context.Context, Job) (*JobEvidence, error) + ReleaseCapture(context.Context, *SnapshotEvidence) error + ReleaseJob(context.Context, *JobEvidence) error +} + +type pollSourceRetention interface { + PruneSource(context.Context, *Store, time.Time) error +} + type PollOptions struct { Store *Store Enrollment Enrollment Release string Discovery PollDiscovery Publisher Publisher + Source PollSource Capture func(context.Context, Snapshot) (*SnapshotEvidence, error) Restore func(context.Context, Job) (*JobEvidence, error) Runtime func(context.Context, Job, *JobEvidence) (Engine, error) @@ -73,6 +87,12 @@ type PollingController struct { } func OpenPollingController(ctx context.Context, o PollOptions) (*PollingController, error) { + if o.Source != nil { + if o.Capture != nil || o.Restore != nil { + return nil, ErrLiveBlocked + } + o.Capture, o.Restore = o.Source.Capture, o.Source.Restore + } if o.Store == nil || o.Store.db == nil || !filepath.IsAbs(o.Store.path) || o.Enrollment.Validate() != nil || !objectID.MatchString(o.Release) || o.Discovery == nil || o.Publisher.Store != o.Store || o.Publisher.Remote == nil || o.Capture == nil || o.Restore == nil || o.Runtime == nil || o.Check == nil { return nil, ErrLiveBlocked } @@ -385,12 +405,24 @@ func (c *PollingController) selectWork(d DurableState, remote map[string]RemoteS return &pollWork{scope: scope, request: r.ID, generation: pr.Generation, snapshot: r.Snapshot, done: make(chan struct{})} } -func (c *PollingController) beforeWork(ctx context.Context, w *pollWork) (*JobEvidence, Job, error) { +func (c *PollingController) beforeWork(ctx context.Context, w *pollWork) (bound *JobEvidence, job Job, result error) { if err := c.options.Check(ctx, c.options.Enrollment, c.options.Release); err != nil { return nil, Job{}, errors.Join(ErrLiveBlocked, err) } var evidence *SnapshotEvidence + defer func() { + if c.options.Source != nil && evidence != nil { + phase, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second) + defer cancel() + result = errors.Join(result, c.options.Source.ReleaseCapture(phase, evidence)) + } + }() if w.job == "" { + if source, ok := c.options.Source.(pollSourceRetention); ok { + if err := source.PruneSource(ctx, c.options.Store, c.now()); err != nil { + return nil, Job{}, err + } + } var captureErr error evidence, captureErr = c.options.Capture(ctx, w.snapshot) if captureErr != nil { @@ -407,7 +439,6 @@ func (c *PollingController) beforeWork(ctx context.Context, w *pollWork) (*JobEv if !remote.Access || !remote.Open || remote.Snapshot != w.snapshot { return nil, Job{}, ErrFenced } - var job Job if w.job != "" { d, err := c.options.Store.Snapshot(ctx) if err != nil { @@ -429,13 +460,13 @@ func (c *PollingController) beforeWork(ctx context.Context, w *pollWork) (*JobEv } // Admit may also return an existing immutable job. Always restore its exact // persisted packet instead of substituting a new capture with different omissions. - bound, err := c.options.Restore(ctx, job) + bound, err = c.options.Restore(ctx, job) if err == nil { err = bound.validateJob(job) } if err == nil && job.Release != c.options.Release { if qualification := c.options.Check(ctx, c.options.Enrollment, job.Release); qualification != nil { - return nil, job, errors.Join(ErrLiveBlocked, qualification) + return bound, job, errors.Join(ErrLiveBlocked, qualification) } } return bound, job, err @@ -559,6 +590,11 @@ func (c *PollingController) runWork(ctx context.Context, w *pollWork) { } } + if c.options.Source != nil && bound != nil { + phase, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second) + err = errors.Join(err, c.options.Source.ReleaseJob(phase, bound)) + cancel() + } c.mu.Lock() w.err = err c.mu.Unlock() diff --git a/reviewer/polling_source_lifetime_test.go b/reviewer/polling_source_lifetime_test.go new file mode 100644 index 00000000..de73e7ed --- /dev/null +++ b/reviewer/polling_source_lifetime_test.go @@ -0,0 +1,129 @@ +package reviewer + +import ( + "context" + "errors" + "sync" + "testing" + "time" +) + +type pollingOwnedSource struct { + mu sync.Mutex + options PollOptions + pending map[*SnapshotEvidence]bool + active map[*JobEvidence]bool + prunes, capturesReleased, jobsReleased int + afterCapture func() + captureErr error +} + +func (s *pollingOwnedSource) Capture(ctx context.Context, snapshot Snapshot) (*SnapshotEvidence, error) { + e, err := s.options.Capture(ctx, snapshot) + if err != nil { + return nil, err + } + s.mu.Lock() + s.pending[e] = true + s.mu.Unlock() + if s.afterCapture != nil { + s.afterCapture() + } + return e, s.captureErr +} + +func (s *pollingOwnedSource) Restore(ctx context.Context, job Job) (*JobEvidence, error) { + e, err := s.options.Restore(ctx, job) + if err == nil { + s.mu.Lock() + s.active[e] = true + s.mu.Unlock() + } + return e, err +} + +func (s *pollingOwnedSource) ReleaseCapture(ctx context.Context, e *SnapshotEvidence) error { + s.mu.Lock() + defer s.mu.Unlock() + if ctx.Err() != nil || !s.pending[e] { + return ErrFenced + } + delete(s.pending, e) + s.capturesReleased++ + return nil +} + +func (s *pollingOwnedSource) ReleaseJob(ctx context.Context, e *JobEvidence) error { + s.mu.Lock() + defer s.mu.Unlock() + if ctx.Err() != nil || !s.active[e] { + return ErrFenced + } + delete(s.active, e) + s.jobsReleased++ + return nil +} + +func (s *pollingOwnedSource) PruneSource(ctx context.Context, store *Store, _ time.Time) error { + s.mu.Lock() + defer s.mu.Unlock() + if ctx.Err() != nil || store != s.options.Store || len(s.pending) != 0 || len(s.active) != 0 { + return ErrFenced + } + s.prunes++ + return nil +} + +func TestPollingOwnedSourceReleasesAfterAdmissionAndOnFencing(t *testing.T) { + for _, mode := range []string{"complete", "fenced-after-capture", "capture-error"} { + t.Run(mode, func(t *testing.T) { + old, f, calls := pollControllerFixture(t, nil) + source := &pollingOwnedSource{options: old.options, pending: map[*SnapshotEvidence]bool{}, active: map[*JobEvidence]bool{}} + if mode == "fenced-after-capture" { + source.afterCapture = func() { f.mu.Lock(); f.remote.Open = false; f.mu.Unlock() } + } else if mode == "capture-error" { + source.captureErr = errors.New("synthetic post-capture failure") + } + options := old.options + options.Source = source + if c, err := OpenPollingController(context.Background(), options); err == nil || c != nil { + t.Fatal("mixed source and raw callbacks accepted") + } + if old.Close(context.Background()) != nil { + t.Fatal("old fixture close") + } + options.Capture, options.Restore = nil, nil + originalRuntime := options.Runtime + options.Runtime = func(ctx context.Context, job Job, e *JobEvidence) (Engine, error) { + source.mu.Lock() + valid := len(source.pending) == 0 && source.active[e] + source.mu.Unlock() + if !valid { + return Engine{}, errors.New("source released before work or capture held past admission") + } + return originalRuntime(ctx, job, e) + } + c, err := OpenPollingController(context.Background(), options) + if err != nil { + t.Fatal(err) + } + defer c.Close(context.Background()) + if result, err := pollOnce(t, c); err != nil || !result.Active { + t.Fatal(result, err) + } + awaitPollWorker(t, c) + source.mu.Lock() + defer source.mu.Unlock() + if source.prunes != 1 || source.capturesReleased != 1 || len(source.pending) != 0 || len(source.active) != 0 { + t.Fatal("source lifetime leaked", source.prunes, source.capturesReleased, len(source.pending), len(source.active)) + } + if mode == "complete" { + if calls.Load() != 4 || source.jobsReleased != 1 { + t.Fatal("admitted source not held through work", calls.Load(), source.jobsReleased) + } + } else if calls.Load() != 0 || source.jobsReleased != 0 { + t.Fatal("fenced source reached review", calls.Load(), source.jobsReleased) + } + }) + } +} From c71fe10c0690e653469cc6fb19db5dc1b4d2d236 Mon Sep 17 00:00:00 2001 From: Franz Hemmer <8227352+HemSoft@users.noreply.github.com> Date: Wed, 21 Oct 2026 13:30:54 -0400 Subject: [PATCH 08/12] Retire unreferenced Git attempts with crash-safe journal cleanup --- reviewer/GIT-EXPORT-ARCHIVE.md | 14 +- reviewer/GIT-FACTORY.md | 58 +- reviewer/git_export_archive_linux.go | 40 +- reviewer/git_factory_retirement_linux.go | 535 ++++++++++++++++++ reviewer/git_factory_retirement_linux_test.go | 308 ++++++++++ reviewer/git_source_factory_linux.go | 41 ++ reviewer/git_source_factory_linux_test.go | 57 +- reviewer/polling.go | 19 +- reviewer/polling_source_lifetime_test.go | 2 +- 9 files changed, 1051 insertions(+), 23 deletions(-) create mode 100644 reviewer/git_factory_retirement_linux.go create mode 100644 reviewer/git_factory_retirement_linux_test.go diff --git a/reviewer/GIT-EXPORT-ARCHIVE.md b/reviewer/GIT-EXPORT-ARCHIVE.md index 55a0dbce..10f3f798 100644 --- a/reviewer/GIT-EXPORT-ARCHIVE.md +++ b/reviewer/GIT-EXPORT-ARCHIVE.md @@ -18,8 +18,9 @@ An existing canonical private service directory is anchored with `os.Root` and owned for the archive's lifetime by a process flock. A second process cannot open it concurrently. Root/lock identity, ownership and private permissions are checked; unknown root entries or malformed record directories require recovery. -At most 128 spent attempt directories are retained. The factory must implement -qualified retention/cleanup; this archive does not discard evidence to free space. +At most 128 source attempt directories are retained. Only the concrete factory +can retire unreferenced terminal attempts after durable job retention and +consumer-lifetime checks. The archive never discards evidence solely for space. Each `export-<32-hex-attempt>` directory contains only fixed `archive.json` and `payload.bin` names. Attempt names never come from a worker path. The canonical @@ -103,7 +104,14 @@ the concrete owner independently proves physical whole-unit stop. That receipt is fsynced before removing the bounded owned payload; a restart can complete cleanup. The nonce stays spent and discarded intake cannot reopen as source. A different nonce may stage after that recovery. Complete staged source is -retained. This is no public force-delete or arbitrary retry interface. +retained while a pending capture, active consumer or durable job needs it. +The factory can retire it after those references expire, using a fsynced intent +and atomic whole-directory moves into archive-owned retirement storage. The +archive recognizes at most 128 intents and their two fixed retirement directories +per intent, with exact original directory identities. Partial deletion stays +recoverable without parsing a half-deleted source record. Unexpected files or +replacements refuse cleanup. See [factory retention](GIT-FACTORY.md). +This is no public force-delete or arbitrary retry interface. Close disposes both lock and root descriptors once intake has joined, records terminal state even if disposal reports an error, and reports that initial diff --git a/reviewer/GIT-FACTORY.md b/reviewer/GIT-FACTORY.md index 4fc44ce6..8f0865c7 100644 --- a/reviewer/GIT-FACTORY.md +++ b/reviewer/GIT-FACTORY.md @@ -2,13 +2,16 @@ The private source factory composes the existing acquisition unit, pinned credential generation, measured runtime, authenticated bootstrap, worker and -plain export archive. Its `Capture` and `Restore` methods fit the polling -controller callbacks. The trusted service must construct and retain the factory; +plain export archive. The polling controller takes it through `PollOptions.Source` +so capture, restore, consumer release and retention share one owner. Raw +Capture/Restore callbacks remain for fakes; they cannot be mixed with Source. +The trusted service must construct and retain the factory; this change does not install a service, import credentials or activate polling. Construction takes exclusive lifecycle ownership of a newly opened unit owner, export archive and prepared runtime. It validates the fixed manifest/helper and -runtime pins, then performs startup recovery before requesting any credential +runtime pins and a shared filesystem for the unit/archive journals, then +performs startup recovery before requesting any credential generation. The composing service must call this before credential Refresh and retain the factory until controller work has joined. Before local handles are retained, a nil factory with an error leaves inputs owned by its caller. After ownership transfers, a failed startup returns a @@ -20,7 +23,8 @@ capability to a worker or broker. ## Capture and grant order -Each capture spends a random 128-bit attempt nonce. Its binding fixes the +Credential readiness is checked before allocating any durable slot or work +directory. Each admitted capture spends a random 128-bit attempt nonce. Its binding fixes the release hash, canonical runtime-manifest hash and immutable PR head/base/merge base. A fresh private attempt directory contains separate bootstrap and broker sockets. No failed or uncertain attempt automatically starts again. Unix pathname bounds @@ -87,13 +91,16 @@ Before returning SnapshotEvidence, capture reconstructs patch anchors from verified plain blobs. It shares the same packet validation used for restoring an admitted job, without inventing a job or running host Git. Restore matches the exact admitted packet, snapshot, release and runtime, rechecks the durable -physical-stop journal and reopens the bounded archive into sealed blobs. There +physical-stop journal and reopens the bounded archive into sealed blobs. A +canonical protected stopped receipt remains valid after systemd collects the +unit and cgroup; an unfinished stop still needs independent live proof. There is no network refetch, host Git parser, recapture or label-based fallback. ## Restart recovery Startup joins the newly opened cross-process-locked unit owner with the export -archive. It independently stops every recorded old acquisition, then requires +archive. It completes durable retirement intents, independently stops every +remaining recorded old acquisition, then requires exact binding equality between journals. A staged export needs both durable bootstrap and worker grants from a captured unit. An orphan, different binding or archive without worker admission refuses startup source recovery. @@ -107,11 +114,43 @@ The same retirement runs after a failed capture in the current process, once its physical stop and credential-generation acceptance are known. Unknown or corrupt outcomes fence Capture/Restore until explicit Recover first drains the retained local runs; recovery does not restart them. -Staged admitted source is retained; this path does not discard a complete export. +Staged admitted source is retained by the separate retention rules below. All existing 128-record bounds remain. The factory retains its private working directories as recovery artifacts; it does not recursively delete a replacement or foreign directory. +## Consumer lifetime and retention + +Capture pins its evidence until admission or fencing has finished. Restore pins +the returned job evidence until review and publication preparation have joined. +The polling controller releases both on success and failure, using a bounded +uncanceled cleanup context. Closing the factory refuses while either consumer +remains. A caller outside polling must perform the same releases. + +Before work and after consumer release, retention calls SQLite's existing Prune +and reads the remaining immutable jobs. Pending captures, active job evidence, +every retained job and every unaccepted acquisition keep their source. An +expired lease alone never permits deletion. Unused sealed caches can close +while the plain archive remains available for exact restore. Retained jobs with +an outstanding worker lease keep their cache as well. + +Only unreferenced, revoked, physically stopped attempts can retire. The factory +fsyncs an intent containing the exact terminal unit receipt, export receipt and +original directory/file identities and hashes. It atomically moves the complete +unit directory into archive-owned retirement storage, then the export directory, +before deleting known files. Both journal roots must share a filesystem. This +keeps an interrupted deletion out of the unit owner's normal record scan. +The intent remains until both directories are removed and synced. Recovery +resumes it before checking ordinary unit/export correspondence. Unexpected +entries, changed identities, mismatched content or cancellation retain the +intent and fence new work. No recursive deletion or external path is accepted. + +Retirement removes terminal journal slots and in-memory run/binding/cache entries, +so 128 is a simultaneous retained-record limit rather than a lifetime quota. +Fresh captures still generate their own random nonces; there is no model-facing +retry or nonce selector. Evaluation consumers must retain their capture or job +references throughout all sessions that need them, including recovery. + ## Validation and remaining gates Offline tests use fake managers/cgroups and local synthetic credential transports. @@ -123,6 +162,11 @@ interrupted intake retirement, orphan/missing-grant recovery refusal, startup before credentials and retained packet/anchor restoration after deleting the synthetic quarantine. The separate actual namespace fixtures measure mount and descriptor subsets using an ordinary inert executable. +Retention tests exceed 128 stopped attempts, preserve unreleased durable leases, +active consumers and pending admission, and reopen after six modeled deletion +boundaries. A real owner process is SIGKILLed after the durable unit-directory +rename; fresh locked owners complete its interrupted retirement. Foreign file +and directory replacements remain untouched. The complete fixed unit has not passed physical qualification on mini. Its required systemd noswap tmpfs fails before payload execution with diff --git a/reviewer/git_export_archive_linux.go b/reviewer/git_export_archive_linux.go index 27710ab6..a3bbe1a6 100644 --- a/reviewer/git_export_archive_linux.go +++ b/reviewer/git_export_archive_linux.go @@ -236,15 +236,48 @@ func (a *gitExportArchive) records() ([]gitExportRecord, error) { return nil, errGitExportArchive } defer dir.Close() - entries, err := dir.ReadDir(gitExportArchiveRecords + 2) - if err != nil && err != io.EOF || len(entries) > gitExportArchiveRecords+1 { + entries, err := dir.ReadDir(gitExportArchiveRecords*4 + 2) + if err != nil && err != io.EOF || len(entries) > gitExportArchiveRecords*4+1 { return nil, errGitExportArchive } + retirements, err := gitRetirementIntents(a) + if err != nil { + return nil, errGitExportArchive + } + intents := map[string]gitRetirement{} + for _, intent := range retirements { + intents[intent.Unit.Binding.Attempt] = intent + } out := []gitExportRecord{} for _, entry := range entries { if entry.Name() == "git-export.lock" { continue } + if strings.HasPrefix(entry.Name(), "retire-") { + attempt := strings.TrimSuffix(strings.TrimPrefix(entry.Name(), "retire-"), ".json") + if _, ok := intents[attempt]; !ok || entry.Name() != gitRetirementName(attempt) { + return nil, errGitExportArchive + } + continue + } + if strings.HasPrefix(entry.Name(), "retired-") { + matched := false + for attempt, intent := range intents { + for kind, id := range map[string]gitKernelFileID{"unit": intent.UnitDirectory, "export": intent.ExportDirectory} { + if entry.Name() == gitRetirementTrash(kind, attempt) && id.Inode != 0 { + present, err := gitRetirementPathIdentity(a.root, entry.Name(), id) + if err != nil || !present { + return nil, errGitExportArchive + } + matched = true + } + } + } + if !matched { + return nil, errGitExportArchive + } + continue + } attempt := strings.TrimPrefix(entry.Name(), "export-") if !strings.HasPrefix(entry.Name(), "export-") || !gitQuarantineHex(attempt, 32) || !entry.IsDir() { return nil, errGitExportArchive @@ -259,6 +292,9 @@ func (a *gitExportArchive) records() ([]gitExportRecord, error) { return nil, errGitExportArchive } out = append(out, r) + if len(out) > gitExportArchiveRecords { + return nil, errGitExportArchive + } } return out, nil } diff --git a/reviewer/git_factory_retirement_linux.go b/reviewer/git_factory_retirement_linux.go new file mode 100644 index 00000000..7e6b1cee --- /dev/null +++ b/reviewer/git_factory_retirement_linux.go @@ -0,0 +1,535 @@ +//go:build linux + +package reviewer + +import ( + "bytes" + "context" + "encoding/json" + "io" + "os" + "path/filepath" + "sort" + "strings" + "time" + + "golang.org/x/sys/unix" +) + +type gitRetirementFile struct { + Identity gitKernelFileID + Hash string + Bytes int64 +} + +// This protected intent is durable before either journal directory moves. An +// atomic rename keeps partial deletion outside the unit owner's record scan. +type gitRetirement struct { + Version int + Unit gitUnitRecord + Export *gitExportRecord `json:",omitempty"` + UnitDirectory, ExportDirectory gitKernelFileID + UnitFiles, ExportFiles map[string]gitRetirementFile +} + +func gitRetirementName(attempt string) string { return "retire-" + attempt + ".json" } +func gitRetirementTrash(kind, attempt string) string { return "retired-" + kind + "-" + attempt } + +func (r gitRetirement) valid(attempt string) bool { + if r.Version != 1 || !r.Unit.valid(attempt, r.Unit.StubHash) || !r.Unit.Stopped || !r.Unit.Revoked || r.UnitDirectory.Inode == 0 || len(r.UnitFiles) != 1 { + return false + } + if _, ok := r.UnitFiles["git-unit.json"]; !ok { + return false + } + unitJSON, err := json.Marshal(r.Unit) + if err != nil || r.UnitFiles["git-unit.json"].Hash != gitRawHash(unitJSON) || r.UnitFiles["git-unit.json"].Bytes != int64(len(unitJSON)) { + return false + } + if r.Export == nil { + if r.ExportDirectory != (gitKernelFileID{}) || len(r.ExportFiles) != 0 { + return false + } + } else { + if !r.Export.valid() || r.Export.Binding != r.Unit.Binding || r.ExportDirectory.Inode == 0 || len(r.ExportFiles) < 1 || len(r.ExportFiles) > 2 { + return false + } + if _, ok := r.ExportFiles["archive.json"]; !ok { + return false + } + exportJSON, err := json.Marshal(r.Export) + if err != nil || r.ExportFiles["archive.json"].Hash != gitRawHash(exportJSON) || r.ExportFiles["archive.json"].Bytes != int64(len(exportJSON)) { + return false + } + if r.Export.State == "staged" { + payload, ok := r.ExportFiles["payload.bin"] + if !ok || payload.Hash != r.Export.WireHash || payload.Bytes != r.Export.Bytes { + return false + } + } + } + for kind, files := range map[string]map[string]gitRetirementFile{"unit": r.UnitFiles, "export": r.ExportFiles} { + for name, file := range files { + if kind == "unit" && name != "git-unit.json" || kind == "export" && name != "archive.json" && name != "payload.bin" || file.Identity.Inode == 0 || !validAuthHash(file.Hash) || file.Bytes < 0 || file.Bytes > gitExportWireBytes { + return false + } + } + } + return true +} + +func readGitRetirement(root *os.Root, attempt string) (gitRetirement, error) { + var r gitRetirement + raw, err := readAuth(root, gitRetirementName(attempt), false) + if err != nil || len(raw) > 32768 || json.Unmarshal(raw, &r) != nil { + return r, errGitUnit + } + canonical, err := json.Marshal(r) + if err != nil || !bytes.Equal(raw, canonical) || !r.valid(attempt) { + return gitRetirement{}, errGitUnit + } + return r, nil +} + +func gitRetirementDirectory(root *os.Root, names map[string]bool) (gitKernelFileID, map[string]gitRetirementFile, error) { + directory, err := root.Open(".") + if err != nil { + return gitKernelFileID{}, nil, errGitUnit + } + defer directory.Close() + id, err := gitStatFileID(directory) + entries, readErr := directory.ReadDir(4) + if err != nil || readErr != nil && readErr != io.EOF || len(entries) > 2 { + return id, nil, errGitUnit + } + files := map[string]gitRetirementFile{} + for _, entry := range entries { + if !names[entry.Name()] || !entry.Type().IsRegular() { + return id, nil, errGitUnit + } + file, err := root.OpenFile(entry.Name(), os.O_RDONLY|unix.O_NOFOLLOW|unix.O_NONBLOCK, 0) + if err != nil { + return id, nil, errGitUnit + } + observed, err := gitRetirementFileIdentity(file) + file.Close() + if err != nil { + return id, nil, errGitUnit + } + files[entry.Name()] = observed + } + return id, files, nil +} + +func gitRetirementFileIdentity(file *os.File) (gitRetirementFile, error) { + var st unix.Stat_t + if privateAuthFile(file) != nil || unix.Fstat(int(file.Fd()), &st) != nil || st.Nlink != 1 || st.Size < 0 || st.Size > gitExportWireBytes { + return gitRetirementFile{}, errGitUnit + } + raw, err := io.ReadAll(io.LimitReader(file, gitExportWireBytes+1)) + if err != nil || int64(len(raw)) != st.Size { + return gitRetirementFile{}, errGitUnit + } + return gitRetirementFile{gitKernelFileID{uint64(st.Dev), st.Ino}, gitRawHash(raw), st.Size}, nil +} + +// Called under both owners' operation tokens and after the outer factory has +// excluded every pending capture, active consumer and retained durable job. +func prepareGitRetirement(unit *gitUnitOwner, archive *gitExportArchive, binding gitMaterializationBinding) (gitRetirement, error) { + r, err := unit.read(binding.Attempt) + if err != nil || r.Binding != binding || !r.Stopped || !r.Revoked || archive.verifyRoot() != nil { + return gitRetirement{}, errGitUnit + } + u, err := unit.root.OpenRoot(gitUnitDirectory(binding.Attempt)) + if err != nil { + return gitRetirement{}, errGitUnit + } + defer u.Close() + uid, ufiles, err := gitRetirementDirectory(u, map[string]bool{"git-unit.json": true}) + if err != nil { + return gitRetirement{}, errGitUnit + } + intent := gitRetirement{Version: 1, Unit: r, UnitDirectory: uid, UnitFiles: ufiles} + if _, err := archive.root.Lstat(gitExportDirectory(binding.Attempt)); err == nil { + a, err := archive.attempt(binding.Attempt) + if err != nil { + return intent, errGitUnit + } + defer a.Close() + export, err := readGitExportRecord(a) + if err != nil || export.Binding != binding { + return intent, errGitUnit + } + intent.Export = &export + intent.ExportDirectory, intent.ExportFiles, err = gitRetirementDirectory(a, map[string]bool{"archive.json": true, "payload.bin": true}) + if err != nil { + return intent, errGitUnit + } + } else if !os.IsNotExist(err) { + return intent, errGitUnit + } + if !intent.valid(binding.Attempt) { + return intent, errGitUnit + } + return intent, nil +} + +func gitRetirementRootFD(root *os.Root) (*os.File, gitKernelFileID, error) { + file, err := root.Open(".") + if err != nil { + return nil, gitKernelFileID{}, errGitUnit + } + id, err := gitStatFileID(file) + if err != nil { + file.Close() + return nil, id, errGitUnit + } + return file, id, nil +} + +func gitRetirementSharedFilesystem(unit, archive *os.Root) bool { + u, uid, err := gitRetirementRootFD(unit) + if err != nil { + return false + } + defer u.Close() + a, aid, err := gitRetirementRootFD(archive) + if err != nil { + return false + } + defer a.Close() + return uid.Device == aid.Device +} + +func gitRetirementPathIdentity(root *os.Root, name string, want gitKernelFileID) (bool, error) { + info, err := root.Lstat(name) + if os.IsNotExist(err) { + return false, nil + } + if err != nil || !info.IsDir() || info.Mode().Perm() != 0700 || privateDirectory(filepath.Join(root.Name(), name)) != nil { + return false, errGitUnit + } + dir, err := root.Open(name) + if err != nil { + return false, errGitUnit + } + defer dir.Close() + id, err := gitStatFileID(dir) + if err != nil || id != want { + return false, errGitUnit + } + return true, nil +} + +func moveGitRetirementDirectory(source, target *os.Root, old, trash string, expected gitKernelFileID) error { + original, err := gitRetirementPathIdentity(source, old, expected) + if err != nil { + return err + } + moved, err := gitRetirementPathIdentity(target, trash, expected) + if err != nil || original && moved { + return errGitUnit + } + if !original { + return nil + } + src, sid, err := gitRetirementRootFD(source) + if err != nil { + return err + } + defer src.Close() + dst, did, err := gitRetirementRootFD(target) + if err != nil { + return err + } + defer dst.Close() + if sid.Device != did.Device || unix.Renameat2(int(src.Fd()), old, int(dst.Fd()), trash, unix.RENAME_NOREPLACE) != nil || syncAuthDirectory(source) != nil || syncAuthDirectory(target) != nil { + return errGitUnit + } + return nil +} + +func removeGitRetirementDirectory(ctx context.Context, archive *gitExportArchive, trash string, id gitKernelFileID, expected map[string]gitRetirementFile) error { + present, err := gitRetirementPathIdentity(archive.root, trash, id) + if err != nil || !present { + return err + } + directory, err := archive.root.OpenRoot(trash) + if err != nil { + return errGitUnit + } + defer directory.Close() + _, files, err := gitRetirementDirectory(directory, map[string]bool{"git-unit.json": true, "archive.json": true, "payload.bin": true}) + if err != nil { + return err + } + names := []string{} + for name, file := range files { + if want, ok := expected[name]; !ok || file != want { + return errGitUnit + } + names = append(names, name) + } + sort.Strings(names) + for _, name := range names { + if ctx.Err() != nil { + return errGitUnit + } + // Recheck the original object immediately before each unlink. No + // recursive removal can follow a newly inserted file or symlink. + file, err := directory.OpenFile(name, os.O_RDONLY|unix.O_NOFOLLOW|unix.O_NONBLOCK, 0) + if err != nil { + return errGitUnit + } + got, err := gitRetirementFileIdentity(file) + opened, statErr := file.Stat() + atPath, pathErr := directory.Lstat(name) + file.Close() + if err != nil || statErr != nil || pathErr != nil || got != expected[name] || !os.SameFile(opened, atPath) || directory.Remove(name) != nil || syncAuthDirectory(directory) != nil { + return errGitUnit + } + } + present, err = gitRetirementPathIdentity(archive.root, trash, id) + if err != nil || !present || archive.root.Remove(trash) != nil || syncAuthDirectory(archive.root) != nil { + return errGitUnit + } + return nil +} + +func checkGitRetirementDirectory(root *os.Root, name string, id gitKernelFileID, expected map[string]gitRetirementFile, partial bool) error { + present, err := gitRetirementPathIdentity(root, name, id) + if err != nil || !present { + return err + } + directory, err := root.OpenRoot(name) + if err != nil { + return errGitUnit + } + defer directory.Close() + _, files, err := gitRetirementDirectory(directory, map[string]bool{"git-unit.json": true, "archive.json": true, "payload.bin": true}) + if err != nil || !partial && len(files) != len(expected) { + return errGitUnit + } + for name, file := range files { + if want, ok := expected[name]; !ok || want != file { + return errGitUnit + } + } + return nil +} + +// Startup can resume this intent before ordinary cross-journal recovery. Unit +// and archive roots must share a filesystem for the unit-directory rename. +func finishGitRetirement(ctx context.Context, unit *gitUnitOwner, archive *gitExportArchive, intent gitRetirement) error { + attempt := intent.Unit.Binding.Attempt + hash, err := unit.imageHash() + if ctx.Err() != nil || err != nil || !intent.valid(attempt) || intent.Unit.StubHash != hash || archive.verifyRoot() != nil { + return errGitUnit + } + // Verify both journals before deleting either. A foreign replacement in + // one directory must not make the other's retained payload disposable. + if checkGitRetirementDirectory(unit.root, gitUnitDirectory(attempt), intent.UnitDirectory, intent.UnitFiles, false) != nil || checkGitRetirementDirectory(archive.root, gitRetirementTrash("unit", attempt), intent.UnitDirectory, intent.UnitFiles, true) != nil { + return errGitUnit + } + if intent.Export != nil { + if checkGitRetirementDirectory(archive.root, gitExportDirectory(attempt), intent.ExportDirectory, intent.ExportFiles, false) != nil || checkGitRetirementDirectory(archive.root, gitRetirementTrash("export", attempt), intent.ExportDirectory, intent.ExportFiles, true) != nil { + return errGitUnit + } + } + if moveGitRetirementDirectory(unit.root, archive.root, gitUnitDirectory(attempt), gitRetirementTrash("unit", attempt), intent.UnitDirectory) != nil { + return errGitUnit + } + if intent.Export != nil { + if moveGitRetirementDirectory(archive.root, archive.root, gitExportDirectory(attempt), gitRetirementTrash("export", attempt), intent.ExportDirectory) != nil { + return errGitUnit + } + if removeGitRetirementDirectory(ctx, archive, gitRetirementTrash("export", attempt), intent.ExportDirectory, intent.ExportFiles) != nil { + return errGitUnit + } + } + if removeGitRetirementDirectory(ctx, archive, gitRetirementTrash("unit", attempt), intent.UnitDirectory, intent.UnitFiles) != nil { + return errGitUnit + } + if ctx.Err() != nil || archive.root.Remove(gitRetirementName(attempt)) != nil || syncAuthDirectory(archive.root) != nil { + return errGitUnit + } + if fd, ok := unit.rootPIDFDs[attempt]; ok { + unix.Close(fd) + delete(unit.rootPIDFDs, attempt) + } + delete(unit.grantEligible, attempt) + delete(unit.grantFailed, attempt) + return nil +} + +func gitRetirementIntents(archive *gitExportArchive) ([]gitRetirement, error) { + directory, err := archive.root.Open(".") + if err != nil { + return nil, errGitUnit + } + defer directory.Close() + entries, err := directory.ReadDir(gitExportArchiveRecords*4 + 2) + if err != nil && err != io.EOF || len(entries) > gitExportArchiveRecords*4+1 { + return nil, errGitUnit + } + intents := []gitRetirement{} + for _, entry := range entries { + if !strings.HasPrefix(entry.Name(), "retire-") { + continue + } + attempt := strings.TrimSuffix(strings.TrimPrefix(entry.Name(), "retire-"), ".json") + if entry.Name() != gitRetirementName(attempt) || !gitQuarantineHex(attempt, 32) { + return nil, errGitUnit + } + intent, err := readGitRetirement(archive.root, attempt) + if err != nil { + return nil, err + } + intents = append(intents, intent) + } + if len(intents) > gitExportArchiveRecords { + return nil, errGitUnit + } + return intents, nil +} + +func resumeGitRetirements(ctx context.Context, unit *gitUnitOwner, archive *gitExportArchive) error { + if !unit.enter(ctx) { + return errGitUnit + } + defer unit.leave() + if unit.closed || !archive.enter(ctx) { + return errGitUnit + } + defer archive.leave() + intents, err := gitRetirementIntents(archive) + if err != nil { + return err + } + for _, intent := range intents { + if finishGitRetirement(ctx, unit, archive, intent) != nil { + return errGitUnit + } + } + return nil +} + +func (f *gitSourceFactory) PruneSource(ctx context.Context, store *Store, now time.Time) error { + if !f.enter(ctx) { + return errGitUnit + } + defer f.leave() + if f.closed || !f.ready || store == nil || f.checkRoot() != nil { + return errGitUnit + } + state, err := store.Snapshot(ctx) + if err != nil || state.Enrollment != f.credentials.state.options.Journal.enrollment { + return errGitUnit + } + if store.Prune(ctx, now) != nil { + return errGitUnit + } + state, err = store.Snapshot(ctx) + if err != nil { + return errGitUnit + } + keep := map[string]bool{} + used := map[string]bool{} + for _, attempt := range f.pendingCaptures { + keep[attempt] = true + } + for _, attempt := range f.activeJobs { + keep[attempt] = true + } + for attempt := range keep { + used[attempt] = true + } + for _, job := range state.Jobs { + for attempt, binding := range f.bindings { + if job.Job.Snapshot == binding.Snapshot && Digest(job.Job.Packet) == f.packets[attempt] { + keep[attempt] = true + if job.LeaseToken != "" || job.Outstanding { + used[attempt] = true + } + } + } + } + for attempt, materialized := range f.materialized { + if !used[attempt] { + if materialized.Close() != nil { + return errGitUnit + } + delete(f.materialized, attempt) + } + } + if !f.unit.enter(ctx) { + return errGitUnit + } + defer f.unit.leave() + if f.unit.closed || !f.archive.enter(ctx) { + return errGitUnit + } + defer f.archive.leave() + records, err := f.unit.records() + if err != nil { + return errGitUnit + } + for _, record := range records { + attempt := record.Binding.Attempt + if keep[attempt] || !record.Stopped || !record.Revoked { + continue + } + if run := f.runs[attempt]; run != nil { + run.mu.Lock() + stopped, closed, generation := run.stopped, run.closed, run.generation + run.mu.Unlock() + if !stopped || !closed { + continue + } + if generation != nil { + generation.owner.mu.Lock() + released := generation.released + generation.owner.mu.Unlock() + if !released { + continue + } + } + } + intent, err := prepareGitRetirement(f.unit, f.archive, record.Binding) + if err != nil { + return errGitUnit + } + // Refuse different filesystems before writing an intent or closing cache. + root, id, err := gitRetirementRootFD(f.archive.root) + if err != nil { + return errGitUnit + } + root.Close() + if id.Device != intent.UnitDirectory.Device { + return errGitUnit + } + if _, err := f.archive.root.Lstat(gitRetirementName(attempt)); !os.IsNotExist(err) { + f.ready = false + return errGitUnit + } + raw, _ := json.Marshal(intent) + if writeAuth(f.archive.root, gitRetirementName(attempt), raw) != nil { + return errGitUnit + } + f.ready = false + if materialized := f.materialized[attempt]; materialized != nil { + if materialized.Close() != nil { + return errGitUnit + } + } + if finishGitRetirement(ctx, f.unit, f.archive, intent) != nil { + return errGitUnit + } + delete(f.materialized, attempt) + delete(f.runs, attempt) + delete(f.bindings, attempt) + delete(f.packets, attempt) + f.ready = true + } + return nil +} diff --git a/reviewer/git_factory_retirement_linux_test.go b/reviewer/git_factory_retirement_linux_test.go new file mode 100644 index 00000000..e2aa9ea0 --- /dev/null +++ b/reviewer/git_factory_retirement_linux_test.go @@ -0,0 +1,308 @@ +//go:build linux + +package reviewer + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "io" + "os" + "os/exec" + "strings" + "testing" + "time" +) + +type gitRetirementCrashInput struct { + UnitRoot, Stub, StubHash, ArchiveRoot string + Intent gitRetirement +} + +func TestGitFactoryRetirementCrashChild(t *testing.T) { + raw := os.Getenv("SFL_GIT_RETIREMENT_CRASH") + if raw == "" { + return + } + var input gitRetirementCrashInput + if json.Unmarshal([]byte(raw), &input) != nil { + os.Exit(2) + } + manager := &fakeContainmentManager{owner: input.Intent.Unit.ManagerOwner} + unit, err := newGitUnitOwner(context.Background(), input.UnitRoot, input.Stub, input.StubHash, manager, &fakeContainmentKernel{}) + if err != nil { + os.Exit(3) + } + archive, err := openGitExportArchive(context.Background(), input.ArchiveRoot) + if err != nil { + os.Exit(4) + } + attempt := input.Intent.Unit.Binding.Attempt + if moveGitRetirementDirectory(unit.root, archive.root, gitUnitDirectory(attempt), gitRetirementTrash("unit", attempt), input.Intent.UnitDirectory) != nil { + os.Exit(5) + } + if _, err := os.Stdout.Write([]byte("renamed\n")); err != nil { + os.Exit(6) + } + for { + time.Sleep(time.Second) + } +} + +func TestGitFactoryRetirementRecoversAfterActualOwnerCrash(t *testing.T) { + r, f, intent := gitRetirementFixture(t) + input, _ := json.Marshal(gitRetirementCrashInput{f.root, f.stub, f.stubHash, r.archive.directory, intent}) + if f.owner.Close() != nil || r.archive.Close(context.Background()) != nil { + t.Fatal("old owners close") + } + child := exec.Command(os.Args[0], "-test.run=^TestGitFactoryRetirementCrashChild$") + child.Env = []string{"SFL_GIT_RETIREMENT_CRASH=" + string(input)} + output, err := child.StdoutPipe() + if err != nil || child.Start() != nil { + t.Fatal("crash owner start", err) + } + waited := false + defer func() { + if !waited { + child.Process.Kill() + child.Wait() + } + }() + ready := make(chan error, 1) + go func() { + data := make([]byte, len("renamed\n")) + _, err := io.ReadFull(output, data) + if err == nil && string(data) != "renamed\n" { + err = fmt.Errorf("unexpected fixture readiness") + } + ready <- err + }() + select { + case err := <-ready: + if err != nil { + t.Fatal("crash boundary", err) + } + case <-time.After(5 * time.Second): + t.Fatal("crash owner did not reach durable rename") + } + ctx, cancel := context.WithTimeout(context.Background(), 25*time.Millisecond) + contender, err := newGitUnitOwner(ctx, f.root, f.stub, f.stubHash, f.manager, f.manager.kernel) + cancel() + if err == nil || contender != nil { + t.Fatal("crash owner did not retain unit lock") + } + if child.Process.Kill() != nil || child.Wait() == nil { + t.Fatal("owner was not killed") + } + waited = true + unit, err := newGitUnitOwner(context.Background(), f.root, f.stub, f.stubHash, f.manager, f.manager.kernel) + if err != nil { + t.Fatal("post-crash unit open", err) + } + defer unit.Close() + archive, err := openGitExportArchive(context.Background(), r.archive.directory) + if err != nil { + t.Fatal("post-crash archive open", err) + } + defer archive.Close(context.Background()) + if resumeGitRetirements(context.Background(), unit, archive) != nil { + t.Fatal("post-crash retirement") + } + units, unitErr := unit.records() + exports, exportErr := archive.records() + intents, intentErr := gitRetirementIntents(archive) + if unitErr != nil || exportErr != nil || intentErr != nil || len(units) != 0 || len(exports) != 0 || len(intents) != 0 { + t.Fatal("post-crash records survived", unitErr, exportErr, intentErr) + } +} + +func TestGitFactoryRetiresMoreThanLifetimeRecordLimit(t *testing.T) { + r, f, runtime := gitFactoryAssetsFixture(t) + credentials, _, _, _, signs := credentialOwnerFixture(t) + helper := gitFactoryTestHelper(t, r) + if r.stop(context.Background()) != nil || r.close() != nil { + t.Fatal("prepared fixture stop") + } + factory, err := newGitSourceFactory(context.Background(), credentials, f.owner, r.archive, runtime, helper, r.manifest, testJob().Release, r.binding.ReleaseHash, gitFactoryWorkRoot(t)) + if err != nil { + t.Fatal(err) + } + defer factory.Close(context.Background()) + store, _ := openFixtureStore(t) + for i := 0; i < 140; i++ { + if err := factory.PruneSource(context.Background(), store, controlTime.Add(time.Hour)); err != nil { + t.Fatal("retirement", i, err) + } + binding := r.binding + binding.Attempt = fmt.Sprintf("%032x", i+1) + if err := f.owner.prepare(context.Background(), binding); err != nil { + t.Fatal("lifetime limit still consumes slots", i, err) + } + if f.owner.stop(context.Background(), binding) != nil { + t.Fatal("prepared attempt stop", i) + } + } + if factory.PruneSource(context.Background(), store, controlTime.Add(time.Hour)) != nil { + t.Fatal("final retirement") + } + records, err := f.owner.records() + exports, exportErr := r.archive.records() + intents, intentErr := gitRetirementIntents(r.archive) + if err != nil || exportErr != nil || intentErr != nil || len(records) != 0 || len(exports) != 0 || len(intents) != 0 || signs.Load() != 0 || f.manager.starts != 0 { + t.Fatal("retirement leaked records or dispatched work", len(records), len(exports), len(intents), err, exportErr, intentErr) + } +} + +// Duplicate the measured helper before disposing the fixture run's file set. +func gitFactoryTestHelper(t *testing.T, r *gitFactoryRun) *os.File { + t.Helper() + file, err := duplicateGitControlFile(r.files[2]) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { file.Close() }) + return file +} + +func gitRetirementFixture(t *testing.T) (*gitFactoryRun, gitUnitFixture, gitRetirement) { + t.Helper() + _, binding, objects := gitMaterializationFixture(t, "sha1") + r, f, _ := gitFactoryAssetsFixture(t, binding.Snapshot) + if r.stop(context.Background()) != nil || r.close() != nil { + t.Fatal("prepared fixture stop") + } + var wire bytes.Buffer + if writeGitMaterialization(context.Background(), &wire, objects, r.binding, DefaultGitAcquisitionBounds()) != nil { + t.Fatal("synthetic export") + } + m, err := r.archive.stage(context.Background(), r.binding, io.NopCloser(bytes.NewReader(wire.Bytes()))) + if err != nil { + t.Fatal(err) + } + if m.Close() != nil { + t.Fatal("synthetic materialization close") + } + intent, err := prepareGitRetirement(f.owner, r.archive, r.binding) + if err != nil { + t.Fatal("prepare retirement", err) + } + raw, _ := json.Marshal(intent) + if writeAuth(r.archive.root, gitRetirementName(r.binding.Attempt), raw) != nil { + t.Fatal("write retirement intent") + } + return r, f, intent +} + +func TestGitFactoryRetirementResumesEveryDeletionBoundary(t *testing.T) { + for _, phase := range []string{"intent", "unit-renamed", "export-renamed", "payload-removed", "unit-record-removed", "directories-removed"} { + t.Run(phase, func(t *testing.T) { + r, f, intent := gitRetirementFixture(t) + attempt := r.binding.Attempt + unitTrash, exportTrash := gitRetirementTrash("unit", attempt), gitRetirementTrash("export", attempt) + if phase != "intent" { + if moveGitRetirementDirectory(f.owner.root, r.archive.root, gitUnitDirectory(attempt), unitTrash, intent.UnitDirectory) != nil { + t.Fatal("unit rename") + } + } + if phase != "intent" && phase != "unit-renamed" { + if moveGitRetirementDirectory(r.archive.root, r.archive.root, gitExportDirectory(attempt), exportTrash, intent.ExportDirectory) != nil { + t.Fatal("export rename") + } + } + if phase == "payload-removed" { + if r.archive.root.Remove(exportTrash+"/payload.bin") != nil { + t.Fatal("partial payload deletion") + } + } + if phase == "unit-record-removed" { + if r.archive.root.Remove(unitTrash+"/git-unit.json") != nil { + t.Fatal("partial unit receipt deletion") + } + } + if phase == "directories-removed" { + if removeGitRetirementDirectory(context.Background(), r.archive, exportTrash, intent.ExportDirectory, intent.ExportFiles) != nil || removeGitRetirementDirectory(context.Background(), r.archive, unitTrash, intent.UnitDirectory, intent.UnitFiles) != nil { + t.Fatal("directory deletion") + } + } + if f.owner.Close() != nil || r.archive.Close(context.Background()) != nil { + t.Fatal("old owners close") + } + unit, err := newGitUnitOwner(context.Background(), f.root, f.stub, f.stubHash, f.manager, f.manager.kernel) + if err != nil { + t.Fatal("unit reopen after interrupted deletion", err) + } + defer unit.Close() + archive, err := openGitExportArchive(context.Background(), r.archive.directory) + if err != nil { + t.Fatal("archive reopen after interrupted deletion", err) + } + defer archive.Close(context.Background()) + if err := resumeGitRetirements(context.Background(), unit, archive); err != nil { + t.Fatal("resume deletion", phase, err) + } + units, unitErr := unit.records() + exports, exportErr := archive.records() + intents, intentErr := gitRetirementIntents(archive) + if unitErr != nil || exportErr != nil || intentErr != nil || len(units) != 0 || len(exports) != 0 || len(intents) != 0 { + t.Fatal("partial retirement survived", phase, unitErr, exportErr, intentErr) + } + }) + } +} + +func TestGitFactoryRetirementRefusesForeignReplacementAndCancellation(t *testing.T) { + for _, kind := range []string{"new-file", "replacement-file", "replacement-directory", "changed-receipt", "canceled"} { + t.Run(kind, func(t *testing.T) { + r, f, intent := gitRetirementFixture(t) + attempt := r.binding.Attempt + trash := gitRetirementTrash("unit", attempt) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + if kind != "canceled" { + if moveGitRetirementDirectory(f.owner.root, r.archive.root, gitUnitDirectory(attempt), trash, intent.UnitDirectory) != nil { + t.Fatal("unit rename") + } + } + foreign := "" + switch kind { + case "new-file": + foreign = trash + "/foreign" + if r.archive.root.WriteFile(foreign, []byte("preserve"), 0600) != nil { + t.Fatal("foreign file") + } + case "replacement-file": + foreign = trash + "/git-unit.json" + raw, err := r.archive.root.ReadFile(foreign) + if err != nil { + t.Fatal(err) + } + if r.archive.root.Rename(foreign, trash+"/old.json") != nil || r.archive.root.WriteFile(foreign, raw, 0600) != nil || r.archive.root.Remove(trash+"/old.json") != nil { + t.Fatal("replace receipt inode") + } + case "replacement-directory": + if r.archive.root.Rename(trash, trash+"-held") != nil || r.archive.root.Mkdir(trash, 0700) != nil { + t.Fatal("replace directory inode") + } + case "changed-receipt": + intent.Unit.StubHash = strings.Repeat("e", 64) + case "canceled": + cancel() + } + if finishGitRetirement(ctx, f.owner, r.archive, intent) == nil { + t.Fatal("uncertain retirement completed") + } + if _, err := r.archive.root.Lstat(gitRetirementName(attempt)); err != nil { + t.Fatal("lost recovery intent", err) + } + if foreign != "" { + if _, err := r.archive.root.Lstat(foreign); err != nil { + t.Fatal("foreign replacement deleted", err) + } + } + if _, err := r.archive.root.Lstat(gitExportDirectory(attempt) + "/payload.bin"); err != nil { + t.Fatal("foreign unit evidence caused the other journal's payload to be deleted", err) + } + }) + } +} diff --git a/reviewer/git_source_factory_linux.go b/reviewer/git_source_factory_linux.go index 88f15d87..ae74c712 100644 --- a/reviewer/git_source_factory_linux.go +++ b/reviewer/git_source_factory_linux.go @@ -69,6 +69,9 @@ func newGitSourceFactory(ctx context.Context, credentials *GitHubCredentialOwner if runtimeErr != nil { return nil, errGitUnit } + if !gitRetirementSharedFilesystem(unit.root, archive.root) { + return nil, errGitUnit + } // Recovery is startup-only. A live owner that has already started work is // not interchangeable with a newly opened, cross-process-locked owner. if !unit.enter(ctx) { @@ -142,6 +145,9 @@ func (f *gitSourceFactory) Recover(ctx context.Context) error { if busy { return errGitUnit } + if resumeGitRetirements(ctx, f.unit, f.archive) != nil { + return errGitUnit + } bindings, err := recoverGitFactory(ctx, f.unit, f.archive) if err != nil { return errGitUnit @@ -160,6 +166,41 @@ func (f *gitSourceFactory) Recover(ctx context.Context) error { packets[record.Binding.Attempt] = record.PacketHash } } + if !f.unit.enter(ctx) { + return errGitUnit + } + units, err := f.unit.records() + f.unit.leave() + if err != nil { + return errGitUnit + } + remaining := map[string]bool{} + for _, record := range units { + remaining[record.Binding.Attempt] = true + } + for _, attempt := range f.pendingCaptures { + if !remaining[attempt] { + return errGitUnit + } + } + for _, attempt := range f.activeJobs { + if !remaining[attempt] { + return errGitUnit + } + } + for attempt, materialized := range f.materialized { + if !remaining[attempt] { + if materialized.Close() != nil { + return errGitUnit + } + delete(f.materialized, attempt) + } + } + for attempt := range f.runs { + if !remaining[attempt] { + delete(f.runs, attempt) + } + } f.bindings, f.packets, f.ready = bindings, packets, true return nil } diff --git a/reviewer/git_source_factory_linux_test.go b/reviewer/git_source_factory_linux_test.go index d620a268..f97e11ea 100644 --- a/reviewer/git_source_factory_linux_test.go +++ b/reviewer/git_source_factory_linux_test.go @@ -143,9 +143,17 @@ func TestGitSourceFactoryRestoresAdmittedPacketWithoutHostGit(t *testing.T) { if os.RemoveAll(store.GitDir) != nil { t.Fatal("remove owned synthetic quarantine") } - job := testJob() - job.Snapshot = r.binding.Snapshot - job.Packet = packet + durable, _ := openFixtureStore(t) + request := native(1, controlTime.Add(time.Minute)) + request.Snapshot = r.binding.Snapshot + mustCapture(t, durable, nil, controlTime.Add(2*time.Minute)) + if durable.RecordRequest(context.Background(), alphaScope(), request, true) != nil { + t.Fatal("source retention request") + } + job, err := durable.Admit(context.Background(), alphaScope(), 1, testJob().Release, packet, controlTime.Add(3*time.Minute)) + if err != nil { + t.Fatal("source retention admission", err) + } bound, err := factory.Restore(context.Background(), job) if err != nil || bound.evidence.Packet() != original.Packet() || !reflect.DeepEqual(bound.evidence.lines, original.lines) || !reflect.DeepEqual(bound.evidence.Coverage(), original.Coverage()) { t.Fatal("restored evidence changed", err) @@ -170,6 +178,49 @@ func TestGitSourceFactoryRestoresAdmittedPacketWithoutHostGit(t *testing.T) { if factory.ReleaseJob(context.Background(), bound) != nil || factory.ReleaseJob(context.Background(), bound) == nil { t.Fatal("restored evidence was not released exactly once") } + // Expired lease time does not prove a worker stopped. Retention must keep + // the exact source and its cache while that durable lease remains owned. + token, err := durable.Claim(context.Background(), job.ID, controlTime.Add(4*time.Minute)) + if err != nil { + t.Fatal(err) + } + later := controlTime.Add(40 * 24 * time.Hour) + if factory.PruneSource(context.Background(), durable, later) != nil || factory.materialized[r.binding.Attempt] == nil { + t.Fatal("unknown durable lease lost retained source") + } + state, err := durable.Snapshot(context.Background()) + if err != nil || state.Jobs[job.ID] == nil { + t.Fatal("expired lease was stolen", err) + } + bound, err = factory.Restore(context.Background(), job) + if err != nil { + t.Fatal("retained lease source could not restore", err) + } + // This is an explicitly synthetic pending admission using independently + // reconstructed source evidence, not a physical Capture qualification. + factory.pendingCaptures = map[*SnapshotEvidence]string{evidence: r.binding.Attempt} + if durable.ReleaseStopped(context.Background(), job.ID, token, "cancelled") != nil { + t.Fatal("known stopped fake lease release") + } + if factory.PruneSource(context.Background(), durable, later) != nil { + t.Fatal("retained consumer pruning") + } + state, err = durable.Snapshot(context.Background()) + if err != nil || state.Jobs[job.ID] != nil || len(factory.bindings) != 1 { + t.Fatal("active source or expired job retention incorrect", err) + } + if factory.ReleaseJob(context.Background(), bound) != nil || factory.PruneSource(context.Background(), durable, later) != nil || len(factory.bindings) != 1 { + t.Fatal("pending admission source retired") + } + if factory.ReleaseCapture(context.Background(), evidence) != nil || factory.PruneSource(context.Background(), durable, later) != nil || len(factory.bindings) != 0 || len(factory.materialized) != 0 { + t.Fatal("released expired source permanently consumed slots") + } + if _, err := factory.unit.root.Lstat(gitUnitDirectory(r.binding.Attempt)); !os.IsNotExist(err) { + t.Fatal("unit receipt not retired", err) + } + if _, err := factory.archive.root.Lstat(gitExportDirectory(r.binding.Attempt)); !os.IsNotExist(err) { + t.Fatal("export not retired", err) + } } func TestGitSourceFactoryValidLongRootAndFailedStartupOwnership(t *testing.T) { diff --git a/reviewer/polling.go b/reviewer/polling.go index f9a0211b..647eed8b 100644 --- a/reviewer/polling.go +++ b/reviewer/polling.go @@ -417,12 +417,12 @@ func (c *PollingController) beforeWork(ctx context.Context, w *pollWork) (bound result = errors.Join(result, c.options.Source.ReleaseCapture(phase, evidence)) } }() - if w.job == "" { - if source, ok := c.options.Source.(pollSourceRetention); ok { - if err := source.PruneSource(ctx, c.options.Store, c.now()); err != nil { - return nil, Job{}, err - } + if source, ok := c.options.Source.(pollSourceRetention); ok { + if err := source.PruneSource(ctx, c.options.Store, c.now()); err != nil { + return nil, Job{}, err } + } + if w.job == "" { var captureErr error evidence, captureErr = c.options.Capture(ctx, w.snapshot) if captureErr != nil { @@ -590,9 +590,14 @@ func (c *PollingController) runWork(ctx context.Context, w *pollWork) { } } - if c.options.Source != nil && bound != nil { + if c.options.Source != nil { phase, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second) - err = errors.Join(err, c.options.Source.ReleaseJob(phase, bound)) + if bound != nil { + err = errors.Join(err, c.options.Source.ReleaseJob(phase, bound)) + } + if source, ok := c.options.Source.(pollSourceRetention); ok { + err = errors.Join(err, source.PruneSource(phase, c.options.Store, c.now())) + } cancel() } c.mu.Lock() diff --git a/reviewer/polling_source_lifetime_test.go b/reviewer/polling_source_lifetime_test.go index de73e7ed..60ae23ae 100644 --- a/reviewer/polling_source_lifetime_test.go +++ b/reviewer/polling_source_lifetime_test.go @@ -114,7 +114,7 @@ func TestPollingOwnedSourceReleasesAfterAdmissionAndOnFencing(t *testing.T) { awaitPollWorker(t, c) source.mu.Lock() defer source.mu.Unlock() - if source.prunes != 1 || source.capturesReleased != 1 || len(source.pending) != 0 || len(source.active) != 0 { + if source.prunes != 2 || source.capturesReleased != 1 || len(source.pending) != 0 || len(source.active) != 0 { t.Fatal("source lifetime leaked", source.prunes, source.capturesReleased, len(source.pending), len(source.active)) } if mode == "complete" { From c59bb309895b8b8f4a947fad78fde98675113260 Mon Sep 17 00:00:00 2001 From: Franz Hemmer <8227352+HemSoft@users.noreply.github.com> Date: Wed, 21 Oct 2026 13:32:41 -0400 Subject: [PATCH 09/12] Assert unprivileged host capabilities in nonancestor inspection --- .../git_nonancestor_inspection_linux_test.go | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/reviewer/git_nonancestor_inspection_linux_test.go b/reviewer/git_nonancestor_inspection_linux_test.go index b3626c11..1fbc7375 100644 --- a/reviewer/git_nonancestor_inspection_linux_test.go +++ b/reviewer/git_nonancestor_inspection_linux_test.go @@ -42,6 +42,29 @@ func TestGitDescriptorNonancestorLauncher(t *testing.T) { func gitAssertNonancestorInspector(t *testing.T, identity GateIdentity, original int) { t.Helper() + status, err := os.ReadFile("/proc/self/status") + if err != nil || os.Geteuid() == 0 { + t.Fatal("nonancestor fixture requires an unprivileged inspector", err) + } + capabilities := map[string]bool{"CapPrm:": false, "CapEff:": false, "CapAmb:": false} + for _, line := range strings.Split(string(status), "\n") { + fields := strings.Fields(line) + if len(fields) != 2 { + continue + } + if _, selected := capabilities[fields[0]]; selected { + value, err := strconv.ParseUint(fields[1], 16, 64) + if err != nil || value != 0 { + t.Fatal("inspector has host capabilities", fields[0], err) + } + capabilities[fields[0]] = true + } + } + for name, seen := range capabilities { + if !seen { + t.Fatal("missing host capability evidence", name) + } + } if gitCheckProcessPIDFD(identity, original) != nil { t.Fatal("original detached worker identity") } @@ -116,7 +139,7 @@ func gitAssertNonancestorInspector(t *testing.T, identity GateIdentity, original if !parentOwned { t.Fatal("target user namespace is not a descendant owned by inspector") } - t.Logf("inspector=%d nonancestor worker chain=%v target user namespace owner=%d", os.Getpid(), chain, owner) + t.Logf("inspector=%d host permitted/effective/ambient capabilities=0 nonancestor worker chain=%v target user namespace owner=%d", os.Getpid(), chain, owner) } func TestGitDescriptorActualNonancestorNondumpableWorker(t *testing.T) { From b4345164b79e5287f32f18a024e8fbfce0465d74 Mon Sep 17 00:00:00 2001 From: Franz Hemmer <8227352+HemSoft@users.noreply.github.com> Date: Wed, 21 Oct 2026 14:06:14 -0400 Subject: [PATCH 10/12] Restore retained releases and prune source during idle polling --- reviewer/GIT-FACTORY.md | 17 +++-- reviewer/git_factory_run_linux.go | 5 ++ reviewer/git_factory_run_linux_test.go | 4 +- reviewer/git_source_factory_linux.go | 7 +- reviewer/git_source_factory_linux_test.go | 21 +++++- reviewer/polling.go | 15 ++-- reviewer/polling_source_lifetime_test.go | 88 ++++++++++++++++++++++- 7 files changed, 137 insertions(+), 20 deletions(-) diff --git a/reviewer/GIT-FACTORY.md b/reviewer/GIT-FACTORY.md index 8f0865c7..d9a10288 100644 --- a/reviewer/GIT-FACTORY.md +++ b/reviewer/GIT-FACTORY.md @@ -29,7 +29,8 @@ release hash, canonical runtime-manifest hash and immutable PR head/base/merge base. A fresh private attempt directory contains separate bootstrap and broker sockets. No failed or uncertain attempt automatically starts again. Unix pathname bounds are checked against each actual bootstrap/worker/broker socket path, including -the fixed nonce directory, rather than an arbitrary short root limit. +the fixed nonce directory. The bootstrap endpoint must fit the unit owner's +100-byte limit; broker endpoints must fit the kernel's 107-byte limit. The factory seals the fixed non-executable manifest, duplicates measured runtime and helper handles, and retains the original sealed worker image and supervisor @@ -90,8 +91,11 @@ different credential pair or drops an unknown unit. Before returning SnapshotEvidence, capture reconstructs patch anchors from verified plain blobs. It shares the same packet validation used for restoring an admitted job, without inventing a job or running host Git. Restore matches -the exact admitted packet, snapshot, release and runtime, rechecks the durable -physical-stop journal and reopens the bounded archive into sealed blobs. A +the exact admitted packet and snapshot, rechecks the retained attempt's original +release/runtime binding across both durable journals and reopens the bounded +archive into sealed blobs. New capture assets do not replace an older attempt's +provenance. The controller qualifies the job's admitting release before Restore, +including after an upgrade; a revoked release exposes no restored source. A canonical protected stopped receipt remains valid after systemd collects the unit and cgroup; an unfinished stop still needs independent live proof. There is no network refetch, host Git parser, recapture or label-based fallback. @@ -127,9 +131,10 @@ The polling controller releases both on success and failure, using a bounded uncanceled cleanup context. Closing the factory refuses while either consumer remains. A caller outside polling must perform the same releases. -Before work and after consumer release, retention calls SQLite's existing Prune -and reads the remaining immutable jobs. Pending captures, active job evidence, -every retained job and every unaccepted acquisition keep their source. An +During ordinary polling, before work and after consumer release, retention calls +SQLite's existing Prune and reads the remaining immutable jobs. Idle polls also +retire expired source without requiring another review job. Pending captures, +active job evidence, every retained job and every unaccepted acquisition keep their source. An expired lease alone never permits deletion. Unused sealed caches can close while the plain archive remains available for exact restore. Retained jobs with an outstanding worker lease keep their cache as well. diff --git a/reviewer/git_factory_run_linux.go b/reviewer/git_factory_run_linux.go index e58a4381..213bf3e6 100644 --- a/reviewer/git_factory_run_linux.go +++ b/reviewer/git_factory_run_linux.go @@ -42,6 +42,11 @@ func gitFactoryRunPaths(directory string) bool { if !filepath.IsAbs(directory) || filepath.Clean(directory) != directory || strings.ContainsRune(directory, 0) { return false } + // The unit owner imposes a stricter bootstrap endpoint limit than the + // kernel's sockaddr_un limit used by the two broker sockets. + if len(filepath.Join(directory, "bootstrap.sock")) > 100 { + return false + } for _, name := range []string{"bootstrap.sock", "broker/worker.sock", "broker/git.sock"} { if len(filepath.Join(directory, name)) > 107 { return false diff --git a/reviewer/git_factory_run_linux_test.go b/reviewer/git_factory_run_linux_test.go index 71df24bc..56bfab87 100644 --- a/reviewer/git_factory_run_linux_test.go +++ b/reviewer/git_factory_run_linux_test.go @@ -276,8 +276,8 @@ func TestGitFactoryBrokerSetupFailureRetiresOnlyKnownUnstartedLease(t *testing.T } func TestGitFactorySocketPathBounds(t *testing.T) { - longest := "/" + strings.Repeat("x", 87) - if len(filepath.Join(longest, "broker", "worker.sock")) != 107 || !gitFactoryRunPaths(longest) { + longest := "/" + strings.Repeat("x", 84) + if len(filepath.Join(longest, "bootstrap.sock")) != 100 || !gitFactoryRunPaths(longest) { t.Fatal("valid maximum socket path refused") } for _, path := range []string{longest + "x", "relative", "/tmp/../other", "/tmp/invalid\x00"} { diff --git a/reviewer/git_source_factory_linux.go b/reviewer/git_source_factory_linux.go index ae74c712..0a1b1f12 100644 --- a/reviewer/git_source_factory_linux.go +++ b/reviewer/git_source_factory_linux.go @@ -289,12 +289,15 @@ func (f *gitSourceFactory) Restore(ctx context.Context, job Job) (*JobEvidence, return nil, errGitUnit } defer f.leave() - if f.closed || !f.ready || f.checkRoot() != nil || job.Validate() != nil || job.Release != f.release || job.Snapshot.RepositoryID != f.credentials.state.options.Journal.enrollment.RepositoryID { + if f.closed || !f.ready || f.checkRoot() != nil || job.Validate() != nil || job.Snapshot.RepositoryID != f.credentials.state.options.Journal.enrollment.RepositoryID { return nil, ErrFenced } attempts := []string{} for attempt, binding := range f.bindings { - if binding.Snapshot == job.Snapshot && binding.ReleaseHash == f.releaseHash && binding.RuntimeHash == Digest(f.manifest) && f.packets[attempt] == Digest(job.Packet) { + // Recovery authenticated this retained attempt's original release and + // runtime binding against both journals. Current capture assets do not + // replace that provenance when restoring an older immutable packet. + if binding.Snapshot == job.Snapshot && f.packets[attempt] == Digest(job.Packet) { attempts = append(attempts, attempt) } } diff --git a/reviewer/git_source_factory_linux_test.go b/reviewer/git_source_factory_linux_test.go index f97e11ea..603c3bd2 100644 --- a/reviewer/git_source_factory_linux_test.go +++ b/reviewer/git_source_factory_linux_test.go @@ -132,7 +132,22 @@ func TestGitSourceFactoryRestoresAdmittedPacketWithoutHostGit(t *testing.T) { f.manager.kernel.changed = true f.manager.kernel.mu.Unlock() owner, _, _, _, signs := credentialOwnerFixture(t) - factory, err := newGitSourceFactory(context.Background(), owner, reopened, r.archive, runtime, helper, r.manifest, testJob().Release, r.binding.ReleaseHash, gitFactoryWorkRoot(t)) + // A new deployment changes its capture release and runtime manifest. The + // retained export still belongs to the old, independently stopped attempt. + // A different existing root-owned inert asset changes the manifest pin. + // It is never executed and does not qualify namespace behavior. + newHelperBytes, err := os.ReadFile("/usr/bin/true") + if err != nil { + t.Fatal(err) + } + newHelper, err := os.Open("/usr/bin/true") + if err != nil { + t.Fatal(err) + } + defer newHelper.Close() + newManifest := r.manifest + newManifest.NamespaceHelper = VMAssetPin{gitRawHash(newHelperBytes), int64(len(newHelperBytes))} + factory, err := newGitSourceFactory(context.Background(), owner, reopened, r.archive, runtime, newHelper, newManifest, strings.Repeat("e", 40), Digest("upgraded release"), gitFactoryWorkRoot(t)) if err != nil { t.Fatal("recovery", err) } @@ -165,9 +180,9 @@ func TestGitSourceFactoryRestoresAdmittedPacketWithoutHostGit(t *testing.T) { t.Fatal("capture/restore evidence mismatch", err) } wrong := job - wrong.Release = strings.Repeat("e", 40) + wrong.Packet += " " if got, err := factory.Restore(context.Background(), wrong); err == nil || got != nil { - t.Fatal("other release reused retained source") + t.Fatal("different admitted packet reused retained source") } if signs.Load() != 0 { t.Fatal("restore dispatched credential work") diff --git a/reviewer/polling.go b/reviewer/polling.go index 647eed8b..a324fbf8 100644 --- a/reviewer/polling.go +++ b/reviewer/polling.go @@ -458,17 +458,17 @@ func (c *PollingController) beforeWork(ctx context.Context, w *pollWork) (bound return nil, Job{}, err } } + if job.Release != c.options.Release { + if qualification := c.options.Check(ctx, c.options.Enrollment, job.Release); qualification != nil { + return nil, job, errors.Join(ErrLiveBlocked, qualification) + } + } // Admit may also return an existing immutable job. Always restore its exact // persisted packet instead of substituting a new capture with different omissions. bound, err = c.options.Restore(ctx, job) if err == nil { err = bound.validateJob(job) } - if err == nil && job.Release != c.options.Release { - if qualification := c.options.Check(ctx, c.options.Enrollment, job.Release); qualification != nil { - return bound, job, errors.Join(ErrLiveBlocked, qualification) - } - } return bound, job, err } @@ -777,6 +777,11 @@ func (c *PollingController) Poll(ctx context.Context) (result PollResult, err er if err = c.options.Store.Prune(ctx, c.now()); err != nil { return result, err } + if source, ok := c.options.Source.(pollSourceRetention); ok { + if err = source.PruneSource(ctx, c.options.Store, c.now()); err != nil { + return result, err + } + } d, err = c.options.Store.Snapshot(ctx) if err != nil { return result, err diff --git a/reviewer/polling_source_lifetime_test.go b/reviewer/polling_source_lifetime_test.go index 60ae23ae..baa453be 100644 --- a/reviewer/polling_source_lifetime_test.go +++ b/reviewer/polling_source_lifetime_test.go @@ -67,7 +67,7 @@ func (s *pollingOwnedSource) ReleaseJob(ctx context.Context, e *JobEvidence) err func (s *pollingOwnedSource) PruneSource(ctx context.Context, store *Store, _ time.Time) error { s.mu.Lock() defer s.mu.Unlock() - if ctx.Err() != nil || store != s.options.Store || len(s.pending) != 0 || len(s.active) != 0 { + if ctx.Err() != nil || store != s.options.Store { return ErrFenced } s.prunes++ @@ -114,7 +114,7 @@ func TestPollingOwnedSourceReleasesAfterAdmissionAndOnFencing(t *testing.T) { awaitPollWorker(t, c) source.mu.Lock() defer source.mu.Unlock() - if source.prunes != 2 || source.capturesReleased != 1 || len(source.pending) != 0 || len(source.active) != 0 { + if source.prunes != 3 || source.capturesReleased != 1 || len(source.pending) != 0 || len(source.active) != 0 { t.Fatal("source lifetime leaked", source.prunes, source.capturesReleased, len(source.pending), len(source.active)) } if mode == "complete" { @@ -127,3 +127,87 @@ func TestPollingOwnedSourceReleasesAfterAdmissionAndOnFencing(t *testing.T) { }) } } + +func TestPollingOwnedSourcePrunesWhileIdle(t *testing.T) { + old, f, calls := pollControllerFixture(t, nil) + options := old.options + // Retain an actual expired terminal job, with no worker or pending writes. + mustCapture(t, options.Store, nil, controlTime.Add(2*time.Minute)) + if options.Store.RecordRequest(context.Background(), alphaScope(), f.remote.Request, true) != nil { + t.Fatal("retained request fixture") + } + evidence, err := options.Capture(context.Background(), f.remote.Snapshot) + if err != nil { + t.Fatal(err) + } + job, err := options.Store.Admit(context.Background(), alphaScope(), f.remote.Request.ID, options.Release, evidence.Packet(), controlTime.Add(3*time.Minute)) + if err != nil { + t.Fatal(err) + } + if err := options.Store.update(context.Background(), func(d *DurableState) error { d.Jobs[job.ID].State = "cancelled"; return nil }); err != nil { + t.Fatal(err) + } + f.clock.Advance(40 * 24 * time.Hour) + if old.Close(context.Background()) != nil { + t.Fatal("old controller close") + } + source := &pollingOwnedSource{options: options, pending: map[*SnapshotEvidence]bool{}, active: map[*JobEvidence]bool{}} + options.Source, options.Capture, options.Restore = source, nil, nil + f.mu.Lock() + f.scopes = nil + f.remote.Open = false + f.mu.Unlock() + c, err := OpenPollingController(context.Background(), options) + if err != nil { + t.Fatal(err) + } + defer c.Close(context.Background()) + for i := 0; i < 2; i++ { + if result, err := pollOnce(t, c); err != nil || result.Active { + t.Fatal("idle poll", result, err) + } + f.clock.Advance(time.Second) + } + state, err := options.Store.Snapshot(context.Background()) + if err != nil || len(state.Jobs) != 0 { + t.Fatal("idle retention did not prune expired job", err) + } + source.mu.Lock() + defer source.mu.Unlock() + if source.prunes != 2 || source.capturesReleased != 0 || source.jobsReleased != 0 || calls.Load() != 0 { + t.Fatal("idle source retention did not run without work", source.prunes, calls.Load()) + } +} + +func TestPollingOldReleaseQualificationPrecedesRestore(t *testing.T) { + c, _, _ := pollControllerFixture(t, nil) + if result, err := pollOnce(t, c); err != nil || !result.Active { + t.Fatal(result, err) + } + awaitPollWorker(t, c) + state, err := c.options.Store.Snapshot(context.Background()) + if err != nil || len(state.Jobs) != 1 { + t.Fatal("durable job fixture", err) + } + var job Job + for _, stored := range state.Jobs { + job = stored.Job + } + c.options.Release = "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + oldChecks, restores := 0, 0 + c.options.Check = func(_ context.Context, _ Enrollment, release string) error { + if release == job.Release { + oldChecks++ + return errors.New("old release revoked") + } + return nil + } + c.options.Restore = func(context.Context, Job) (*JobEvidence, error) { + restores++ + return nil, errors.New("unexpected restore") + } + work := &pollWork{scope: alphaScope(), request: 1, generation: job.Generation, snapshot: job.Snapshot, job: job.ID} + if bound, _, err := c.beforeWork(context.Background(), work); bound != nil || !errors.Is(err, ErrLiveBlocked) || oldChecks != 1 || restores != 0 { + t.Fatal("old release source exposed before qualification", bound, err, oldChecks, restores) + } +} From 496ae4ea88650a2d70b89f31eabcc0c3e6b55134 Mon Sep 17 00:00:00 2001 From: Franz Hemmer <8227352+HemSoft@users.noreply.github.com> Date: Wed, 21 Oct 2026 14:42:10 -0400 Subject: [PATCH 11/12] Keep active polling nonblocking and refuse aliased source work roots --- reviewer/GIT-FACTORY.md | 9 +++- reviewer/git_source_factory_linux.go | 4 +- reviewer/git_source_factory_linux_test.go | 64 ++++++++++++++++++++++- reviewer/polling.go | 5 +- reviewer/polling_source_lifetime_test.go | 59 +++++++++++++++++++++ 5 files changed, 136 insertions(+), 5 deletions(-) diff --git a/reviewer/GIT-FACTORY.md b/reviewer/GIT-FACTORY.md index d9a10288..1dbc7c43 100644 --- a/reviewer/GIT-FACTORY.md +++ b/reviewer/GIT-FACTORY.md @@ -3,7 +3,9 @@ The private source factory composes the existing acquisition unit, pinned credential generation, measured runtime, authenticated bootstrap, worker and plain export archive. The polling controller takes it through `PollOptions.Source` -so capture, restore, consumer release and retention share one owner. Raw +so capture, restore, consumer release and retention share one owner. Work roots +must have different opened directory identities from both journal roots; aliases +refuse before transferring ownership. Raw Capture/Restore callbacks remain for fakes; they cannot be mixed with Source. The trusted service must construct and retain the factory; this change does not install a service, import credentials or activate polling. @@ -133,7 +135,10 @@ remains. A caller outside polling must perform the same releases. During ordinary polling, before work and after consumer release, retention calls SQLite's existing Prune and reads the remaining immutable jobs. Idle polls also -retire expired source without requiring another review job. Pending captures, +retire expired source without requiring another review job. Polling skips the +source retention hook while a worker owns capture, so it does not wait for the +factory operation token or turn a healthy long acquisition into tick backoff. +Before/after-work retention and later idle polls still collect eligible source. Pending captures, active job evidence, every retained job and every unaccepted acquisition keep their source. An expired lease alone never permits deletion. Unused sealed caches can close while the plain archive remains available for exact restore. Retained jobs with diff --git a/reviewer/git_source_factory_linux.go b/reviewer/git_source_factory_linux.go index 0a1b1f12..61e167f1 100644 --- a/reviewer/git_source_factory_linux.go +++ b/reviewer/git_source_factory_linux.go @@ -87,7 +87,9 @@ func newGitSourceFactory(ctx context.Context, credentials *GitHubCredentialOwner return nil, errGitUnit } identity, err := root.Stat(".") - if err != nil { + unitIdentity, unitErr := unit.root.Stat(".") + archiveIdentity, archiveErr := archive.root.Stat(".") + if err != nil || unitErr != nil || archiveErr != nil || os.SameFile(identity, unitIdentity) || os.SameFile(identity, archiveIdentity) { root.Close() return nil, errGitUnit } diff --git a/reviewer/git_source_factory_linux_test.go b/reviewer/git_source_factory_linux_test.go index 603c3bd2..a3673c2e 100644 --- a/reviewer/git_source_factory_linux_test.go +++ b/reviewer/git_source_factory_linux_test.go @@ -119,7 +119,17 @@ func TestGitSourceFactoryRestoresAdmittedPacketWithoutHostGit(t *testing.T) { if r.stop(context.Background()) != nil || r.close() != nil || f.owner.Close() != nil { t.Fatal("fake unit/old owner shutdown") } - reopened, err := newGitUnitOwner(context.Background(), f.root, f.stub, f.stubHash, f.manager, f.manager.kernel) + oldWorker, err := os.ReadFile(f.stub) + if err != nil { + t.Fatal(err) + } + newWorkerBytes := append(oldWorker, []byte("\nsynthetic upgraded worker\n")...) + newWorkerPath := filepath.Join(t.TempDir(), "upgraded-worker") + if os.WriteFile(newWorkerPath, newWorkerBytes, 0500) != nil || os.Remove(f.stub) != nil { + t.Fatal("upgrade worker fixture") + } + newWorkerPin := VMAssetPin{gitRawHash(newWorkerBytes), int64(len(newWorkerBytes))} + reopened, err := newGitUnitOwner(context.Background(), f.root, newWorkerPath, newWorkerPin.SHA256, f.manager, f.manager.kernel) if err != nil { t.Fatal("new startup owner", err) } @@ -146,6 +156,7 @@ func TestGitSourceFactoryRestoresAdmittedPacketWithoutHostGit(t *testing.T) { } defer newHelper.Close() newManifest := r.manifest + newManifest.Worker = newWorkerPin newManifest.NamespaceHelper = VMAssetPin{gitRawHash(newHelperBytes), int64(len(newHelperBytes))} factory, err := newGitSourceFactory(context.Background(), owner, reopened, r.archive, runtime, newHelper, newManifest, strings.Repeat("e", 40), Digest("upgraded release"), gitFactoryWorkRoot(t)) if err != nil { @@ -384,3 +395,54 @@ func TestGitSourceFactoryRetiresKnownStoppedIntakeAfterLeaseAcceptance(t *testin t.Fatal("recovered failed capture cleanup") } } + +func TestGitSourceFactoryRefusesJournalAliasedWorkRoots(t *testing.T) { + for _, kind := range []string{"unit", "archive"} { + t.Run(kind, func(t *testing.T) { + r, f, runtime := gitFactoryAssetsFixture(t) + helper, err := duplicateGitControlFile(r.files[2]) + if err != nil { + t.Fatal(err) + } + defer helper.Close() + if r.stop(context.Background()) != nil || r.close() != nil || f.owner.Close() != nil { + t.Fatal("old fixture disposal") + } + // Short real journal paths ensure the path-length checks do not + // accidentally reject this alias before directory identity is tested. + unit, err := newGitUnitOwner(context.Background(), gitFactoryWorkRoot(t), f.stub, f.stubHash, f.manager, f.manager.kernel) + if err != nil { + t.Fatal(err) + } + defer unit.Close() + archive, err := openGitExportArchive(context.Background(), gitFactoryWorkRoot(t)) + if err != nil { + t.Fatal(err) + } + defer archive.Close(context.Background()) + if unit.prepare(context.Background(), r.binding) != nil { + t.Fatal("caller-owned prepared receipt") + } + defer unit.stop(context.Background(), r.binding) + owner, _, _, _, signs := credentialOwnerFixture(t) + root := archive.root.Name() + if kind == "unit" { + root = unit.root.Name() + } + if !gitFactoryRunPaths(filepath.Join(root, "run-"+strings.Repeat("a", 32))) { + t.Fatal("alias fixture rejected by unrelated path bound") + } + factory, err := newGitSourceFactory(context.Background(), owner, unit, archive, runtime, helper, r.manifest, testJob().Release, r.binding.ReleaseHash, root) + if factory != nil { + _ = factory.Close(context.Background()) + } + if err == nil || factory != nil { + t.Fatal("aliased work root accepted ownership", kind, err) + } + record, err := unit.read(r.binding.Attempt) + if err != nil || record.Stopped || unit.closed || archive.closed || signs.Load() != 0 || f.manager.starts != 0 { + t.Fatal("alias refusal mutated caller-owned inputs", record, err) + } + }) + } +} diff --git a/reviewer/polling.go b/reviewer/polling.go index a324fbf8..fff6a93b 100644 --- a/reviewer/polling.go +++ b/reviewer/polling.go @@ -777,7 +777,10 @@ func (c *PollingController) Poll(ctx context.Context) (result PollResult, err er if err = c.options.Store.Prune(ctx, c.now()); err != nil { return result, err } - if source, ok := c.options.Source.(pollSourceRetention); ok { + c.mu.Lock() + activeSource := c.active != nil + c.mu.Unlock() + if source, ok := c.options.Source.(pollSourceRetention); ok && !activeSource { if err = source.PruneSource(ctx, c.options.Store, c.now()); err != nil { return result, err } diff --git a/reviewer/polling_source_lifetime_test.go b/reviewer/polling_source_lifetime_test.go index baa453be..6caedff8 100644 --- a/reviewer/polling_source_lifetime_test.go +++ b/reviewer/polling_source_lifetime_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "sync" + "sync/atomic" "testing" "time" ) @@ -211,3 +212,61 @@ func TestPollingOldReleaseQualificationPrecedesRestore(t *testing.T) { t.Fatal("old release source exposed before qualification", bound, err, oldChecks, restores) } } + +type pollingBusySource struct { + *pollingOwnedSource + entered, release chan struct{} + busy atomic.Bool +} + +func (s *pollingBusySource) Capture(ctx context.Context, snapshot Snapshot) (*SnapshotEvidence, error) { + s.busy.Store(true) + close(s.entered) + defer s.busy.Store(false) + select { + case <-ctx.Done(): + return nil, ctx.Err() + case <-s.release: + } + return s.pollingOwnedSource.Capture(ctx, snapshot) +} +func (s *pollingBusySource) PruneSource(ctx context.Context, store *Store, now time.Time) error { + if s.busy.Load() { + select { + case <-ctx.Done(): + return ctx.Err() + case <-s.release: + } + } + return s.pollingOwnedSource.PruneSource(ctx, store, now) +} +func TestPollingSkipsRetentionWhileCaptureHoldsSource(t *testing.T) { + old, f, calls := pollControllerFixture(t, nil) + options := old.options + if old.Close(context.Background()) != nil { + t.Fatal("old controller close") + } + source := &pollingBusySource{pollingOwnedSource: &pollingOwnedSource{options: options, pending: map[*SnapshotEvidence]bool{}, active: map[*JobEvidence]bool{}}, entered: make(chan struct{}), release: make(chan struct{})} + options.Source, options.Capture, options.Restore = source, nil, nil + c, err := OpenPollingController(context.Background(), options) + if err != nil { + t.Fatal(err) + } + defer func() { close(source.release); _ = c.Close(context.Background()) }() + if result, err := pollOnce(t, c); err != nil || !result.Active { + t.Fatal(result, err) + } + select { + case <-source.entered: + case <-time.After(time.Second): + t.Fatal("capture not entered") + } + f.clock.Advance(time.Second) + if result, err := pollOnce(t, c); err != nil || !result.Active { + t.Fatal("healthy capture caused retention backoff", result, err) + } + state, err := options.Store.Snapshot(context.Background()) + if err != nil || state.Polling.Failures != 0 || calls.Load() != 0 { + t.Fatal("active capture polling recorded spurious failure", err, calls.Load()) + } +} From b157c18e43c332c32d1d58f93a84ef28595a8337 Mon Sep 17 00:00:00 2001 From: Franz Hemmer <8227352+HemSoft@users.noreply.github.com> Date: Wed, 21 Oct 2026 14:48:04 -0400 Subject: [PATCH 12/12] Verify original cached worker images during Git retirement --- reviewer/git_factory_retirement_linux.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reviewer/git_factory_retirement_linux.go b/reviewer/git_factory_retirement_linux.go index 7e6b1cee..4a777e34 100644 --- a/reviewer/git_factory_retirement_linux.go +++ b/reviewer/git_factory_retirement_linux.go @@ -322,7 +322,7 @@ func checkGitRetirementDirectory(root *os.Root, name string, id gitKernelFileID, // and archive roots must share a filesystem for the unit-directory rename. func finishGitRetirement(ctx context.Context, unit *gitUnitOwner, archive *gitExportArchive, intent gitRetirement) error { attempt := intent.Unit.Binding.Attempt - hash, err := unit.imageHash() + hash, err := unit.recordImageHash(intent.Unit.StubHash) if ctx.Err() != nil || err != nil || !intent.valid(attempt) || intent.Unit.StubHash != hash || archive.verifyRoot() != nil { return errGitUnit }