From 17962e58c1130cd150b4a1475f4e8cad3ae67423 Mon Sep 17 00:00:00 2001 From: Konstantin Bogomolov Date: Fri, 10 Jan 2025 15:07:01 -0800 Subject: [PATCH] Replace unsafeSlice with unsafe.Slice. reflect.SliceHeader has been deprecated. PiperOrigin-RevId: 714222929 --- nogo.yaml | 1 + pkg/sentry/platform/ptrace/stub_unsafe.go | 14 ++------------ pkg/sentry/platform/systrap/stub_unsafe.go | 16 +++------------- pkg/sentry/platform/systrap/subprocess_unsafe.go | 4 ++-- 4 files changed, 8 insertions(+), 27 deletions(-) diff --git a/nogo.yaml b/nogo.yaml index b6262ac06..8d4d4eeb5 100644 --- a/nogo.yaml +++ b/nogo.yaml @@ -198,6 +198,7 @@ analyzers: - pkg/sentry/platform/kvm/machine_unsafe.go # Special case. - pkg/sentry/platform/kvm/bluepill_amd64_unsafe.go # Special case. - pkg/sentry/platform/pgalloc/pgalloc_unsafe.go # Special case. + - pkg/sentry/platform/ptrace/stub_unsafe.go # Special case. - pkg/sentry/platform/systrap/stub_unsafe.go # Special case. - pkg/sentry/platform/systrap/syscall_thread_unsafe.go # Special case. - pkg/sentry/platform/systrap/sysmsg_thread_unsafe.go # Special case. diff --git a/pkg/sentry/platform/ptrace/stub_unsafe.go b/pkg/sentry/platform/ptrace/stub_unsafe.go index 1fbdea898..39ad0e165 100644 --- a/pkg/sentry/platform/ptrace/stub_unsafe.go +++ b/pkg/sentry/platform/ptrace/stub_unsafe.go @@ -15,7 +15,6 @@ package ptrace import ( - "reflect" "unsafe" "golang.org/x/sys/unix" @@ -36,21 +35,12 @@ func addrOfStub() uintptr // stubCall calls the stub at the given address with the given pid. func stubCall(addr, pid uintptr) -// unsafeSlice returns a slice for the given address and length. -func unsafeSlice(addr uintptr, length int) (slice []byte) { - sh := (*reflect.SliceHeader)(unsafe.Pointer(&slice)) - sh.Data = addr - sh.Len = length - sh.Cap = length - return -} - // stubInit initializes the stub. func stubInit() { // Grab the existing stub. stubBegin := addrOfStub() stubLen := int(safecopy.FindEndAddress(stubBegin) - stubBegin) - stubSlice := unsafeSlice(stubBegin, stubLen) + stubSlice := unsafe.Slice((*byte)(unsafe.Pointer(stubBegin)), stubLen) mapLen := uintptr(stubLen) if offset := mapLen % hostarch.PageSize; offset != 0 { mapLen += hostarch.PageSize - offset @@ -82,7 +72,7 @@ func stubInit() { } // Copy the stub to the address. - targetSlice := unsafeSlice(addr, stubLen) + targetSlice := unsafe.Slice((*byte)(unsafe.Pointer(addr)), stubLen) copy(targetSlice, stubSlice) // Make the stub executable. diff --git a/pkg/sentry/platform/systrap/stub_unsafe.go b/pkg/sentry/platform/systrap/stub_unsafe.go index 995bbe8aa..5c9f83b8b 100644 --- a/pkg/sentry/platform/systrap/stub_unsafe.go +++ b/pkg/sentry/platform/systrap/stub_unsafe.go @@ -46,15 +46,6 @@ func addrOfInitStubProcess() uintptr // stubCall calls the stub at the given address with the given pid. func stubCall(addr, pid uintptr) -// unsafeSlice returns a slice for the given address and length. -func unsafeSlice(addr uintptr, length int) (slice []byte) { - sh := (*reflect.SliceHeader)(unsafe.Pointer(&slice)) - sh.Data = addr - sh.Len = length - sh.Cap = length - return -} - // prepareSeccompRules compiles stub process seccomp filters and fill // the sock_fprog structure. So the stub process will only need to call // seccomp system call to apply these filters. @@ -199,7 +190,7 @@ func stubInit() { // Grab the existing stub. procStubBegin := addrOfInitStubProcess() procStubLen := int(safecopy.FindEndAddress(procStubBegin) - procStubBegin) - procStubSlice := unsafeSlice(procStubBegin, procStubLen) + procStubSlice := unsafe.Slice((*byte)(unsafe.Pointer(procStubBegin)), procStubLen) mapLen, _ := hostarch.PageRoundUp(uintptr(procStubLen)) stubSysmsgStart = mapLen @@ -281,7 +272,7 @@ func stubInit() { stubContextRegion += uintptr(gap) // Copy the stub to the address. - targetSlice := unsafeSlice(stubStart, procStubLen) + targetSlice := unsafe.Slice((*byte)(unsafe.Pointer(stubStart)), procStubLen) copy(targetSlice, procStubSlice) stubInitProcess = stubStart @@ -299,8 +290,7 @@ func stubInit() { } stubSysmsgRules += stubStart stubSyscallRules += stubStart - - targetSlice = unsafeSlice(stubSysmsgStart, stubSysmsgLen) + targetSlice = unsafe.Slice((*byte)(unsafe.Pointer(stubSysmsgStart)), stubSysmsgLen) copy(targetSlice, sysmsg.SighandlerBlob) // Initialize stub globals diff --git a/pkg/sentry/platform/systrap/subprocess_unsafe.go b/pkg/sentry/platform/systrap/subprocess_unsafe.go index c711865e7..38112c494 100644 --- a/pkg/sentry/platform/systrap/subprocess_unsafe.go +++ b/pkg/sentry/platform/systrap/subprocess_unsafe.go @@ -81,7 +81,7 @@ func mmapContextQueueForSentry(memoryFile *pgalloc.MemoryFile, opts pgalloc.Allo func saveFPState(ctx *sharedContext, ac *arch.Context64) { fpState := ac.FloatingPointData().BytePointer() - dst := unsafeSlice(uintptr(unsafe.Pointer(fpState)), archState.FpLen()) + dst := unsafe.Slice(fpState, archState.FpLen()) src := ctx.shared.FPState[:] copy(dst, src) } @@ -96,7 +96,7 @@ func restoreFPState(ctx *sharedContext, c *platformContext, ac *arch.Context64) ctx.setFPStateChanged() fpState := ac.FloatingPointData().BytePointer() - src := unsafeSlice(uintptr(unsafe.Pointer(fpState)), archState.FpLen()) + src := unsafe.Slice(fpState, archState.FpLen()) dst := ctx.shared.FPState[:] copy(dst, src) }