Validate FS_BASE in Task.Clone

arch_prctl already verified that the new FS_BASE was canonical, but
Task.Clone did not. Centralize these checks in the arch packages.

Failure to validate could cause an error in PTRACE_SET_REGS when we try
to switch to the app.

PiperOrigin-RevId: 224862398
Change-Id: Iefe63b3f9aa6c4810326b8936e501be3ec407f14
This commit is contained in:
Michael Pratt
2018-12-10 12:37:16 -08:00
committed by Shentubot
parent 25b8424d75
commit 99d5958693
6 changed files with 38 additions and 11 deletions
+6
View File
@@ -115,6 +115,12 @@ type Context interface {
// SetStack sets the current stack pointer.
SetStack(value uintptr)
// TLS returns the current TLS pointer.
TLS() uintptr
// SetTLS sets the current TLS pointer. Returns false if value is invalid.
SetTLS(value uintptr) bool
// SetRSEQInterruptedIP sets the register that contains the old IP when a
// restartable sequence is interrupted.
SetRSEQInterruptedIP(value uintptr)
+16
View File
@@ -158,6 +158,22 @@ func (c *context64) SetStack(value uintptr) {
c.Regs.Rsp = uint64(value)
}
// TLS returns the current TLS pointer.
func (c *context64) TLS() uintptr {
return uintptr(c.Regs.Fs_base)
}
// SetTLS sets the current TLS pointer. Returns false if value is invalid.
func (c *context64) SetTLS(value uintptr) bool {
if !isValidSegmentBase(uint64(value)) {
return false
}
c.Regs.Fs = 0
c.Regs.Fs_base = uint64(value)
return true
}
// SetRSEQInterruptedIP implements Context.SetRSEQInterruptedIP.
func (c *context64) SetRSEQInterruptedIP(value uintptr) {
c.Regs.R10 = uint64(value)
+8 -2
View File
@@ -353,10 +353,10 @@ func (s *State) PtraceSetRegs(src io.Reader) (int, error) {
if !isUserSegmentSelector(regs.Ss) {
return 0, syscall.EIO
}
if regs.Fs_base >= uint64(maxAddr64) {
if !isValidSegmentBase(regs.Fs_base) {
return 0, syscall.EIO
}
if regs.Gs_base >= uint64(maxAddr64) {
if !isValidSegmentBase(regs.Gs_base) {
return 0, syscall.EIO
}
// CS and SS are validated, but changes to them are otherwise silently
@@ -389,6 +389,12 @@ func isUserSegmentSelector(reg uint64) bool {
return reg&3 == 3
}
// isValidSegmentBase returns true if the given segment base specifies a
// canonical user address.
func isValidSegmentBase(reg uint64) bool {
return reg < uint64(maxAddr64)
}
// ptraceFPRegsSize is the size in bytes of Linux's user_i387_struct, the type
// manipulated by PTRACE_GETFPREGS and PTRACE_SETFPREGS on x86. Equivalently,
// ptraceFPRegsSize is the size in bytes of the x86 FXSAVE area.
+3 -1
View File
@@ -210,7 +210,9 @@ func (t *Task) Clone(opts *CloneOptions) (ThreadID, *SyscallControl, error) {
tc.Arch.SetStack(uintptr(opts.Stack))
}
if opts.SetTLS {
tc.Arch.StateData().Regs.Fs_base = uint64(opts.TLS)
if !tc.Arch.SetTLS(uintptr(opts.TLS)) {
return 0, nil, syserror.EPERM
}
}
var fsc *FSContext
+2 -2
View File
@@ -480,10 +480,10 @@ func (s *subprocess) switchToApp(c *context, ac arch.Context) bool {
// Set registers.
if err := t.setRegs(regs); err != nil {
panic(fmt.Sprintf("ptrace set regs failed: %v", err))
panic(fmt.Sprintf("ptrace set regs (%+v) failed: %v", regs, err))
}
if err := t.setFPRegs(fpState, uint64(fpLen), useXsave); err != nil {
panic(fmt.Sprintf("ptrace set fpregs failed: %v", err))
panic(fmt.Sprintf("ptrace set fpregs (%+v) failed: %v", fpState, err))
}
for {
+3 -6
View File
@@ -22,7 +22,6 @@ import (
"gvisor.googlesource.com/gvisor/pkg/abi/linux"
"gvisor.googlesource.com/gvisor/pkg/sentry/arch"
"gvisor.googlesource.com/gvisor/pkg/sentry/kernel"
"gvisor.googlesource.com/gvisor/pkg/sentry/usermem"
)
// ArchPrctl implements linux syscall arch_prctl(2).
@@ -31,19 +30,17 @@ func ArchPrctl(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.Sys
switch args[0].Int() {
case linux.ARCH_GET_FS:
addr := args[1].Pointer()
_, err := t.CopyOut(addr, &t.Arch().StateData().Regs.Fs_base)
fsbase := t.Arch().TLS()
_, err := t.CopyOut(addr, uint64(fsbase))
if err != nil {
return 0, nil, err
}
case linux.ARCH_SET_FS:
fsbase := args[1].Uint64()
if _, ok := t.MemoryManager().CheckIORange(usermem.Addr(fsbase), 0); !ok {
if !t.Arch().SetTLS(uintptr(fsbase)) {
return 0, nil, syscall.EPERM
}
regs := &t.Arch().StateData().Regs
regs.Fs = 0
regs.Fs_base = fsbase
case linux.ARCH_GET_GS, linux.ARCH_SET_GS:
t.Kernel().EmitUnimplementedEvent(t)