Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions rdte/protected-control/scope_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions rdte/protected-control/scope_keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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")
}
}
9 changes: 8 additions & 1 deletion rdte/protected-control/writer_gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,23 @@ type writerGate struct {
generation int64
scope string
observe func() (bool,error)
scopeDir *os.File
scopeID cgroupIdentity
uncertain bool
retired bool
}

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.
Expand Down