diff --git a/.github/workflows/wine11-trusted-gate-stop-hil.yml b/.github/workflows/wine11-trusted-gate-stop-hil.yml index 8591447c..fd0d7569 100644 --- a/.github/workflows/wine11-trusted-gate-stop-hil.yml +++ b/.github/workflows/wine11-trusted-gate-stop-hil.yml @@ -4,6 +4,10 @@ on: pull_request: paths: - 'rdte/protected-control/wine_gate_hil_test.go' + - 'rdte/protected-control/writer_gate.go' + - 'rdte/protected-control/scope_keeper*.go' + - 'rdte/protected-control/lifecycle_journal*.go' + - 'rdte/protected-control/durable_stop*.go' - '.github/workflows/wine11-trusted-gate-stop-hil.yml' permissions: diff --git a/rdte/protected-control/scope_keeper.go b/rdte/protected-control/scope_keeper.go index 5ba3ddb2..59748199 100644 --- a/rdte/protected-control/scope_keeper.go +++ b/rdte/protected-control/scope_keeper.go @@ -22,8 +22,9 @@ type scopeKeeper struct { dir *os.File } -// The trusted caller must supply the exact descriptor used for the gate, -// not an arbitrary same-name scope. This is not a guest-facing API. +// Bind retention to the admitted gate's descriptor, never a fresh scope +// selected by a caller. A supplied descriptor is only a consistency check. +// This is not a guest-facing API. func retainScope(g *writerGate,dir *os.File,id cgroupIdentity)(*scopeKeeper,error){ g.mu.Lock();defer g.mu.Unlock() d,l,err:=acquire(g.root,g.uid) @@ -32,9 +33,12 @@ func retainScope(g *writerGate,dir *os.File,id cgroupIdentity)(*scopeKeeper,erro s,err:=lifecycleRead(g.root,g.uid) if err!=nil{return nil,err} if !g.bound(s) || s.Phase!="RESERVED"{return nil,errors.New("scope retention refused")} + if dir==nil || g.scopeDir==nil || id!=g.scopeID{return nil,errors.New("scope binding rejected")} actual,err:=cgroupID(dir) - if err!=nil || actual!=id{return nil,errors.New("scope identity rejected")} - fd,_,errno:=syscall.Syscall(syscall.SYS_FCNTL,dir.Fd(),uintptr(syscall.F_DUPFD_CLOEXEC),0) + if err!=nil || actual!=g.scopeID{return nil,errors.New("scope identity rejected")} + actual,err=cgroupID(g.scopeDir) + if err!=nil || actual!=g.scopeID{return nil,errors.New("admitted scope identity lost")} + fd,_,errno:=syscall.Syscall(syscall.SYS_FCNTL,g.scopeDir.Fd(),uintptr(syscall.F_DUPFD_CLOEXEC),0) if errno!=0{return nil,errno} retained:=os.NewFile(fd,"trusted retained scope") actual,err=cgroupID(retained) diff --git a/rdte/protected-control/scope_keeper_test.go b/rdte/protected-control/scope_keeper_test.go index df912ea9..0efe2845 100644 --- a/rdte/protected-control/scope_keeper_test.go +++ b/rdte/protected-control/scope_keeper_test.go @@ -52,6 +52,25 @@ func TestScopeKeeperRealSupervisorDeath(t *testing.T){ if err!=nil{t.Fatal(err)} bad:=id;bad.Inode^=1 if _,err=retainScope(g,dir,bad);err==nil{t.Fatal("bad scope identity admitted")} + // A second valid, empty cgroup must not be accepted under the + // first gate's generation, even with its own correct identity. + other,err:=os.MkdirTemp("/sys/fs/cgroup","winebot-unrelated-fixture-") + if err!=nil{t.Fatal(err)} + defer os.Remove(other) + otherDir,err:=os.Open(other) + if err!=nil{t.Fatal(err)} + defer otherDir.Close() + otherID,err:=cgroupID(otherDir) + if err!=nil || otherID==id{t.Fatal("independent fixture identity unavailable")} + if p,err:=cgroupPopulated(otherDir,otherID);err!=nil || p{t.Fatal("independent fixture not empty",err)} + if substitute,err:=retainScope(g,otherDir,otherID);err==nil{ + substitute.close() + t.Fatal("valid unrelated scope substituted") + } + if substitute,err:=retainScope(g,otherDir,id);err==nil{ + substitute.close() + t.Fatal("unrelated descriptor accepted with original identity") + } k,err:=retainScope(g,dir,id) if err!=nil{t.Fatal(err)} defer k.close() @@ -94,3 +113,14 @@ func TestScopeKeeperLostCustodyRefuses(t *testing.T){ k:=&scopeKeeper{root:filepath.Join(t.TempDir(),"absent")} if _,err:=k.quarantineAndObserve();err==nil{t.Fatal("lost custody accepted")} } + +func TestScopeKeeperRejectsUnboundGate(t *testing.T){ + root:=lifecycleTestRoot(t);uid:=uint32(os.Geteuid()) + if err:=lifecycleProvision(root,uid);err!=nil{t.Fatal(err)} + g,err:=reserveWriterGate(root,uid,0,func()(bool,error){return false,nil}) + if err!=nil{t.Fatal(err)} + if k,err:=retainScope(g,nil,cgroupIdentity{});err==nil{ + k.close() + t.Fatal("unbound gate admitted custody") + } +} diff --git a/rdte/protected-control/writer_gate.go b/rdte/protected-control/writer_gate.go index e6475810..b2f8e72e 100644 --- a/rdte/protected-control/writer_gate.go +++ b/rdte/protected-control/writer_gate.go @@ -20,6 +20,8 @@ type writerGate struct { generation int64 scope string observe func() (bool,error) + scopeDir *os.File + scopeID cgroupIdentity uncertain bool retired bool } @@ -27,9 +29,14 @@ type writerGate struct { func newWriterGate(root string,uid uint32,expected int64,dir *os.File,id cgroupIdentity)(*writerGate,error){ // The caller retains a fresh, protected scope descriptor throughout this // handle's lifetime. The caller must not close or reuse it concurrently. - return reserveWriterGate(root,uid,expected,func()(bool,error){ + if dir==nil{return nil,errors.New("missing scope descriptor")} + g,err:=reserveWriterGate(root,uid,expected,func()(bool,error){ return cgroupPopulated(dir,id) }) + if err!=nil{return nil,err} + g.scopeDir=dir + g.scopeID=id + return g,nil } // Internal observation seam for deterministic tests. Never exposed to IPC.