Skip to content

Commit

Permalink
platform: allow to expand COW-break on executable VMAs.
Browse files Browse the repository at this point in the history
The systrap platform needs to modify executable VMAs to replace syscall
instructions with function calls.

PiperOrigin-RevId: 626202797
  • Loading branch information
avagin authored and gvisor-bot committed Apr 19, 2024
1 parent cbcb4ec commit f39f6d1
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pkg/sentry/mm/pma.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@ func (mm *MemoryManager) getVecPMAsLocked(ctx context.Context, ars hostarch.Addr
return ars, nil
}

func (mm *MemoryManager) expandCOWBreakOnExec() bool {
if mm.as == nil {
return false
}
return mm.as.ExpandCOWBreakOnExec()
}

// getPMAsInternalLocked is equivalent to getPMAsLocked, with the following
// exceptions:
//
Expand Down Expand Up @@ -333,7 +340,7 @@ func (mm *MemoryManager) getPMAsInternalLocked(ctx context.Context, vseg vmaIter
}
}
var copyAR hostarch.AddrRange
if vma := vseg.ValuePtr(); vma.effectivePerms.Execute {
if vma := vseg.ValuePtr(); vma.effectivePerms.Execute && !mm.expandCOWBreakOnExec() {
// The majority of copy-on-write breaks on executable
// pages come from:
//
Expand Down
3 changes: 3 additions & 0 deletions pkg/sentry/platform/kvm/address_space.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,6 @@ func (as *addressSpace) PreFork() {}

// PostFork implements platform.AddressSpace.PostFork.
func (as *addressSpace) PostFork() {}

// ExpandCOWBreakOnExec implements platform.AddressSpace.ExpandCOWBreakOnExec.
func (as *addressSpace) ExpandCOWBreakOnExec() bool { return false }
6 changes: 6 additions & 0 deletions pkg/sentry/platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,12 @@ type AddressSpace interface {
// Platform.SupportsAddressSpaceIO() == true. AddressSpaces for which this
// does not hold may panic if AddressSpaceIO methods are invoked.
AddressSpaceIO

// ExpandCOWBreakOnExec returns true if MemoryManager needs to treat
// executable VMA-s like regular ones. It can be useful if a platform
// needs to modify executable vma-s. Look at
// MemoryManager.getPMAInternalMappingsLocked for more details.
ExpandCOWBreakOnExec() bool
}

// AddressSpaceIO supports IO through the memory mappings installed in an
Expand Down
3 changes: 3 additions & 0 deletions pkg/sentry/platform/ptrace/subprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,9 @@ func (s *subprocess) Unmap(addr hostarch.Addr, length uint64) {
}
}

// ExpandCOWBreakOnExec implements platform.AddressSpace.ExpandCOWBreakOnExec
func (s *subprocess) ExpandCOWBreakOnExec() bool { return false }

// PreFork implements platform.AddressSpace.PreFork.
func (s *subprocess) PreFork() {}

Expand Down
3 changes: 3 additions & 0 deletions pkg/sentry/platform/systrap/subprocess_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func (s *subprocess) resetSysemuRegs(regs *arch.Registers) {
regs.Gs = s.sysmsgInitRegs.Gs
}

// ExpandCOWBreakOnExec implements platform.AddressSpace.ExpandCOWBreakOnExec.
func (s *subprocess) ExpandCOWBreakOnExec() bool { return true }

// createSyscallRegs sets up syscall registers.
//
// This should be called to generate registers for a system call.
Expand Down
3 changes: 3 additions & 0 deletions pkg/sentry/platform/systrap/subprocess_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ const (
func (s *subprocess) resetSysemuRegs(regs *arch.Registers) {
}

// ExpandCOWBreakOnExec implements platform.AddressSpace.ExpandCOWBreakOnExec.
func (s *subprocess) ExpandCOWBreakOnExec() bool { return false }

// createSyscallRegs sets up syscall registers.
//
// This should be called to generate registers for a system call.
Expand Down

0 comments on commit f39f6d1

Please sign in to comment.