mirror of
https://github.com/hanzoai/visor.git
synced 2026-09-24 17:04:14 +00:00
github.com/google/gvisor/pull/14531 exposed a race window with stub threads setting ThreadID and the sentry reading it. If the sentry attempted to interrupt a stub that in the process of switching a context back, it could have read an invalid thread ID along with a context state that indicated it was still being worked on. This window has existed before #14531, but was relatively benign; with that CL, the window became punishable by subprocess death. The (mostly generated) test seems to reliably test the race-window in case things get reordered in some future refactor. Fixes #14531. PiperOrigin-RevId: 974868738
158 lines
5.0 KiB
Go
158 lines
5.0 KiB
Go
// Copyright 2026 The gVisor Authors.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package systrap
|
|
|
|
import (
|
|
"os"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"golang.org/x/sys/unix"
|
|
pkgcontext "gvisor.dev/gvisor/pkg/context"
|
|
"gvisor.dev/gvisor/pkg/cpuid"
|
|
"gvisor.dev/gvisor/pkg/hostarch"
|
|
"gvisor.dev/gvisor/pkg/safemem"
|
|
"gvisor.dev/gvisor/pkg/sentry/arch"
|
|
"gvisor.dev/gvisor/pkg/sentry/memmap"
|
|
"gvisor.dev/gvisor/pkg/sentry/pgalloc"
|
|
"gvisor.dev/gvisor/pkg/sentry/platform"
|
|
"gvisor.dev/gvisor/pkg/sentry/platform/systrap/sysmsg"
|
|
"gvisor.dev/gvisor/pkg/sentry/platform/systrap/testutil"
|
|
"gvisor.dev/gvisor/pkg/sentry/usage"
|
|
"gvisor.dev/gvisor/pkg/usermem"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
cpuid.Initialize()
|
|
os.Exit(m.Run())
|
|
}
|
|
|
|
// testMemoryManager implements the parts of platform.MemoryManager that
|
|
// platformContext.Switch touches on the syscall path. Syscall patching is
|
|
// disabled in the tests, so MMap and friends are never reached.
|
|
type testMemoryManager struct {
|
|
usermem.IO // Never used.
|
|
as platform.AddressSpace
|
|
}
|
|
|
|
func (*testMemoryManager) MMap(pkgcontext.Context, memmap.MMapOpts) (hostarch.Addr, error) {
|
|
panic("unexpected MMap call")
|
|
}
|
|
|
|
func (mm *testMemoryManager) AddressSpace() platform.AddressSpace { return mm.as }
|
|
|
|
func (*testMemoryManager) FindVMAByName(hostarch.AddrRange, string) (hostarch.Addr, uint64, error) {
|
|
panic("unexpected FindVMAByName call")
|
|
}
|
|
|
|
const userCodeAddr = hostarch.Addr(0x100000)
|
|
|
|
// newSystrapTest creates a real Systrap instance, maps userCode into a new
|
|
// address space, and returns a context ready to Switch into it.
|
|
func newSystrapTest(t *testing.T) (*Systrap, platform.AddressSpace, *platformContext, *arch.Context64) {
|
|
t.Helper()
|
|
|
|
s, err := New(platform.Options{
|
|
DisableSyscallPatching: true,
|
|
// Only slow-path for now in order to not have to use a real
|
|
// memory manager.
|
|
DisableFastPath: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("failed to create Systrap platform: %v", err)
|
|
}
|
|
|
|
as, err := s.NewAddressSpace()
|
|
if err != nil {
|
|
t.Fatalf("failed to create address space: %v", err)
|
|
}
|
|
t.Cleanup(as.Release)
|
|
|
|
fr, err := s.memoryFile.Allocate(hostarch.PageSize, pgalloc.AllocOpts{Kind: usage.System})
|
|
if err != nil {
|
|
t.Fatalf("failed to allocate user code page: %v", err)
|
|
}
|
|
bs, err := s.memoryFile.MapInternal(fr, hostarch.Write)
|
|
if err != nil {
|
|
t.Fatalf("failed to map user code page: %v", err)
|
|
}
|
|
if _, err := safemem.CopySeq(bs, safemem.BlockSeqOf(safemem.BlockFromSafeSlice(testutil.UserCode))); err != nil {
|
|
t.Fatalf("failed to write user code: %v", err)
|
|
}
|
|
if err := as.MapFile(userCodeAddr, s.memoryFile, fr, hostarch.AccessType{Read: true, Execute: true}, false); err != nil {
|
|
t.Fatalf("failed to map user code into the stub address space: %v", err)
|
|
}
|
|
|
|
pctx := s.NewContext(pkgcontext.Background()).(*platformContext)
|
|
t.Cleanup(pctx.Release)
|
|
|
|
ac := arch.New(arch.Host)
|
|
ac.SetIP(uintptr(userCodeAddr))
|
|
return s, as, pctx, ac
|
|
}
|
|
|
|
// TestSwitchThreadIDVisibility runs a real Systrap instance and verifies
|
|
// that there is no window during which the ContextState indicates it's being
|
|
// worked on AND there is no thread currently working on it.
|
|
func TestSwitchThreadIDVisibility(t *testing.T) {
|
|
_, as, pctx, ac := newSystrapTest(t)
|
|
mm := &testMemoryManager{as: as}
|
|
ctx := pkgcontext.Background()
|
|
violations := atomic.Uint64{}
|
|
stop := atomic.Bool{}
|
|
samplerDone := make(chan struct{})
|
|
startSampler := func(shared *sysmsg.ThreadContext) {
|
|
go func() {
|
|
defer close(samplerDone)
|
|
for !stop.Load() {
|
|
a1 := atomic.LoadUint64(&shared.AckedTime)
|
|
|
|
// The stub ThreadID must always be set first,
|
|
// and cleared last.
|
|
tid := atomic.LoadUint32(&shared.ThreadID)
|
|
state := shared.State.Get()
|
|
|
|
a2 := atomic.LoadUint64(&shared.AckedTime)
|
|
if a1 != 0 && a1 == a2 && state == sysmsg.ContextStateNone && tid == invalidThreadID {
|
|
violations.Add(1)
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
for i := 0; i < 4000; i++ {
|
|
si, _, err := pctx.Switch(ctx, mm, ac, 0)
|
|
if err != nil {
|
|
t.Fatalf("Switch failed at iteration %d: %v (si: %v)", i, err, si)
|
|
}
|
|
if sysno := ac.SyscallNo(); sysno != unix.SYS_GETPID {
|
|
t.Fatalf("unexpected syscall %d at iteration %d", sysno, i)
|
|
}
|
|
ac.SetReturn(0) // Emulate the syscall return value.
|
|
|
|
if i == 0 {
|
|
startSampler(pctx.sharedContext.shared)
|
|
}
|
|
}
|
|
|
|
stop.Store(true)
|
|
<-samplerDone
|
|
if n := violations.Load(); n != 0 {
|
|
t.Errorf("sampler observed %d acked contexts with an invalid ThreadID; "+
|
|
"the stub published acked_time before ThreadID or cleared ThreadID "+
|
|
"before publishing the context state", n)
|
|
}
|
|
}
|