mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
Port signalfd to vfs2.
PiperOrigin-RevId: 310404113
This commit is contained in:
committed by
gVisor bot
parent
08f4846ebe
commit
26c60d7d5d
@@ -0,0 +1,20 @@
|
||||
load("//tools:defs.bzl", "go_library")
|
||||
|
||||
package(licenses = ["notice"])
|
||||
|
||||
go_library(
|
||||
name = "signalfd",
|
||||
srcs = ["signalfd.go"],
|
||||
visibility = ["//pkg/sentry:internal"],
|
||||
deps = [
|
||||
"//pkg/abi/linux",
|
||||
"//pkg/binary",
|
||||
"//pkg/context",
|
||||
"//pkg/sentry/kernel",
|
||||
"//pkg/sentry/vfs",
|
||||
"//pkg/sync",
|
||||
"//pkg/syserror",
|
||||
"//pkg/usermem",
|
||||
"//pkg/waiter",
|
||||
],
|
||||
)
|
||||
@@ -0,0 +1,135 @@
|
||||
// Copyright 2019 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 signalfd
|
||||
|
||||
import (
|
||||
"gvisor.dev/gvisor/pkg/abi/linux"
|
||||
"gvisor.dev/gvisor/pkg/binary"
|
||||
"gvisor.dev/gvisor/pkg/context"
|
||||
"gvisor.dev/gvisor/pkg/sentry/kernel"
|
||||
"gvisor.dev/gvisor/pkg/sentry/vfs"
|
||||
"gvisor.dev/gvisor/pkg/sync"
|
||||
"gvisor.dev/gvisor/pkg/syserror"
|
||||
"gvisor.dev/gvisor/pkg/usermem"
|
||||
"gvisor.dev/gvisor/pkg/waiter"
|
||||
)
|
||||
|
||||
// SignalFileDescription implements FileDescriptionImpl for signal fds.
|
||||
type SignalFileDescription struct {
|
||||
vfsfd vfs.FileDescription
|
||||
vfs.FileDescriptionDefaultImpl
|
||||
vfs.DentryMetadataFileDescriptionImpl
|
||||
|
||||
// target is the original signal target task.
|
||||
//
|
||||
// The semantics here are a bit broken. Linux will always use current
|
||||
// for all reads, regardless of where the signalfd originated. We can't
|
||||
// do exactly that because we need to plumb the context through
|
||||
// EventRegister in order to support proper blocking behavior. This
|
||||
// will undoubtedly become very complicated quickly.
|
||||
target *kernel.Task
|
||||
|
||||
// mu protects mask.
|
||||
mu sync.Mutex
|
||||
|
||||
// mask is the signal mask. Protected by mu.
|
||||
mask linux.SignalSet
|
||||
}
|
||||
|
||||
var _ vfs.FileDescriptionImpl = (*SignalFileDescription)(nil)
|
||||
|
||||
// New creates a new signal fd.
|
||||
func New(vfsObj *vfs.VirtualFilesystem, target *kernel.Task, mask linux.SignalSet, flags uint32) (*vfs.FileDescription, error) {
|
||||
vd := vfsObj.NewAnonVirtualDentry("[signalfd]")
|
||||
defer vd.DecRef()
|
||||
sfd := &SignalFileDescription{
|
||||
target: target,
|
||||
mask: mask,
|
||||
}
|
||||
if err := sfd.vfsfd.Init(sfd, flags, vd.Mount(), vd.Dentry(), &vfs.FileDescriptionOptions{
|
||||
UseDentryMetadata: true,
|
||||
DenyPRead: true,
|
||||
DenyPWrite: true,
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &sfd.vfsfd, nil
|
||||
}
|
||||
|
||||
// Mask returns the signal mask.
|
||||
func (sfd *SignalFileDescription) Mask() linux.SignalSet {
|
||||
sfd.mu.Lock()
|
||||
defer sfd.mu.Unlock()
|
||||
return sfd.mask
|
||||
}
|
||||
|
||||
// SetMask sets the signal mask.
|
||||
func (sfd *SignalFileDescription) SetMask(mask linux.SignalSet) {
|
||||
sfd.mu.Lock()
|
||||
defer sfd.mu.Unlock()
|
||||
sfd.mask = mask
|
||||
}
|
||||
|
||||
// Read implements FileDescriptionImpl.Read.
|
||||
func (sfd *SignalFileDescription) Read(ctx context.Context, dst usermem.IOSequence, _ vfs.ReadOptions) (int64, error) {
|
||||
// Attempt to dequeue relevant signals.
|
||||
info, err := sfd.target.Sigtimedwait(sfd.Mask(), 0)
|
||||
if err != nil {
|
||||
// There must be no signal available.
|
||||
return 0, syserror.ErrWouldBlock
|
||||
}
|
||||
|
||||
// Copy out the signal info using the specified format.
|
||||
var buf [128]byte
|
||||
binary.Marshal(buf[:0], usermem.ByteOrder, &linux.SignalfdSiginfo{
|
||||
Signo: uint32(info.Signo),
|
||||
Errno: info.Errno,
|
||||
Code: info.Code,
|
||||
PID: uint32(info.Pid()),
|
||||
UID: uint32(info.Uid()),
|
||||
Status: info.Status(),
|
||||
Overrun: uint32(info.Overrun()),
|
||||
Addr: info.Addr(),
|
||||
})
|
||||
n, err := dst.CopyOut(ctx, buf[:])
|
||||
return int64(n), err
|
||||
}
|
||||
|
||||
// Readiness implements waiter.Waitable.Readiness.
|
||||
func (sfd *SignalFileDescription) Readiness(mask waiter.EventMask) waiter.EventMask {
|
||||
sfd.mu.Lock()
|
||||
defer sfd.mu.Unlock()
|
||||
if mask&waiter.EventIn != 0 && sfd.target.PendingSignals()&sfd.mask != 0 {
|
||||
return waiter.EventIn // Pending signals.
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// EventRegister implements waiter.Waitable.EventRegister.
|
||||
func (sfd *SignalFileDescription) EventRegister(entry *waiter.Entry, _ waiter.EventMask) {
|
||||
sfd.mu.Lock()
|
||||
defer sfd.mu.Unlock()
|
||||
// Register for the signal set; ignore the passed events.
|
||||
sfd.target.SignalRegister(entry, waiter.EventMask(sfd.mask))
|
||||
}
|
||||
|
||||
// EventUnregister implements waiter.Waitable.EventUnregister.
|
||||
func (sfd *SignalFileDescription) EventUnregister(entry *waiter.Entry) {
|
||||
// Unregister the original entry.
|
||||
sfd.target.SignalUnregister(entry)
|
||||
}
|
||||
|
||||
// Release implements FileDescriptionImpl.Release()
|
||||
func (sfd *SignalFileDescription) Release() {}
|
||||
@@ -21,9 +21,13 @@ import (
|
||||
"gvisor.dev/gvisor/pkg/usermem"
|
||||
)
|
||||
|
||||
// copyInSigSet copies in a sigset_t, checks its size, and ensures that KILL and
|
||||
// CopyInSigSet copies in a sigset_t, checks its size, and ensures that KILL and
|
||||
// STOP are clear.
|
||||
func copyInSigSet(t *kernel.Task, sigSetAddr usermem.Addr, size uint) (linux.SignalSet, error) {
|
||||
//
|
||||
// TODO(gvisor.dev/issue/1624): This is only exported because
|
||||
// syscalls/vfs2/signal.go depends on it. Once vfs1 is deleted and the vfs2
|
||||
// syscalls are moved into this package, then they can be unexported.
|
||||
func CopyInSigSet(t *kernel.Task, sigSetAddr usermem.Addr, size uint) (linux.SignalSet, error) {
|
||||
if size != linux.SignalSetSize {
|
||||
return 0, syserror.EINVAL
|
||||
}
|
||||
|
||||
@@ -131,7 +131,7 @@ func EpollPwait(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.Sy
|
||||
maskSize := uint(args[5].Uint())
|
||||
|
||||
if maskAddr != 0 {
|
||||
mask, err := copyInSigSet(t, maskAddr, maskSize)
|
||||
mask, err := CopyInSigSet(t, maskAddr, maskSize)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
|
||||
@@ -443,7 +443,7 @@ func Ppoll(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.Syscall
|
||||
}
|
||||
|
||||
if maskAddr != 0 {
|
||||
mask, err := copyInSigSet(t, maskAddr, maskSize)
|
||||
mask, err := CopyInSigSet(t, maskAddr, maskSize)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
@@ -525,7 +525,7 @@ func Pselect(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.Sysca
|
||||
}
|
||||
|
||||
if maskAddr != 0 {
|
||||
mask, err := copyInSigSet(t, maskAddr, size)
|
||||
mask, err := CopyInSigSet(t, maskAddr, size)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
|
||||
@@ -295,7 +295,7 @@ func RtSigprocmask(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel
|
||||
}
|
||||
oldmask := t.SignalMask()
|
||||
if setaddr != 0 {
|
||||
mask, err := copyInSigSet(t, setaddr, sigsetsize)
|
||||
mask, err := CopyInSigSet(t, setaddr, sigsetsize)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
@@ -366,7 +366,7 @@ func RtSigtimedwait(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kerne
|
||||
timespec := args[2].Pointer()
|
||||
sigsetsize := args[3].SizeT()
|
||||
|
||||
mask, err := copyInSigSet(t, sigset, sigsetsize)
|
||||
mask, err := CopyInSigSet(t, sigset, sigsetsize)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
@@ -518,7 +518,7 @@ func RestartSyscall(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kerne
|
||||
// sharedSignalfd is shared between the two calls.
|
||||
func sharedSignalfd(t *kernel.Task, fd int32, sigset usermem.Addr, sigsetsize uint, flags int32) (uintptr, *kernel.SyscallControl, error) {
|
||||
// Copy in the signal mask.
|
||||
mask, err := copyInSigSet(t, sigset, sigsetsize)
|
||||
mask, err := CopyInSigSet(t, sigset, sigsetsize)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ go_library(
|
||||
"poll.go",
|
||||
"read_write.go",
|
||||
"setstat.go",
|
||||
"signal.go",
|
||||
"socket.go",
|
||||
"stat.go",
|
||||
"stat_amd64.go",
|
||||
@@ -39,6 +40,7 @@ go_library(
|
||||
"//pkg/sentry/arch",
|
||||
"//pkg/sentry/fsbridge",
|
||||
"//pkg/sentry/fsimpl/pipefs",
|
||||
"//pkg/sentry/fsimpl/signalfd",
|
||||
"//pkg/sentry/kernel",
|
||||
"//pkg/sentry/kernel/auth",
|
||||
"//pkg/sentry/kernel/pipe",
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
// Copyright 2018 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 vfs2
|
||||
|
||||
import (
|
||||
"gvisor.dev/gvisor/pkg/abi/linux"
|
||||
"gvisor.dev/gvisor/pkg/sentry/arch"
|
||||
"gvisor.dev/gvisor/pkg/sentry/fsimpl/signalfd"
|
||||
"gvisor.dev/gvisor/pkg/sentry/kernel"
|
||||
slinux "gvisor.dev/gvisor/pkg/sentry/syscalls/linux"
|
||||
"gvisor.dev/gvisor/pkg/syserror"
|
||||
"gvisor.dev/gvisor/pkg/usermem"
|
||||
)
|
||||
|
||||
// sharedSignalfd is shared between the two calls.
|
||||
func sharedSignalfd(t *kernel.Task, fd int32, sigset usermem.Addr, sigsetsize uint, flags int32) (uintptr, *kernel.SyscallControl, error) {
|
||||
// Copy in the signal mask.
|
||||
mask, err := slinux.CopyInSigSet(t, sigset, sigsetsize)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
|
||||
// Always check for valid flags, even if not creating.
|
||||
if flags&^(linux.SFD_NONBLOCK|linux.SFD_CLOEXEC) != 0 {
|
||||
return 0, nil, syserror.EINVAL
|
||||
}
|
||||
|
||||
// Is this a change to an existing signalfd?
|
||||
//
|
||||
// The spec indicates that this should adjust the mask.
|
||||
if fd != -1 {
|
||||
file := t.GetFileVFS2(fd)
|
||||
if file == nil {
|
||||
return 0, nil, syserror.EBADF
|
||||
}
|
||||
defer file.DecRef()
|
||||
|
||||
// Is this a signalfd?
|
||||
if sfd, ok := file.Impl().(*signalfd.SignalFileDescription); ok {
|
||||
sfd.SetMask(mask)
|
||||
return 0, nil, nil
|
||||
}
|
||||
|
||||
// Not a signalfd.
|
||||
return 0, nil, syserror.EINVAL
|
||||
}
|
||||
|
||||
fileFlags := uint32(linux.O_RDWR)
|
||||
if flags&linux.SFD_NONBLOCK != 0 {
|
||||
fileFlags |= linux.O_NONBLOCK
|
||||
}
|
||||
|
||||
// Create a new file.
|
||||
vfsObj := t.Kernel().VFS()
|
||||
file, err := signalfd.New(vfsObj, t, mask, fileFlags)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
defer file.DecRef()
|
||||
|
||||
// Create a new descriptor.
|
||||
fd, err = t.NewFDFromVFS2(0, file, kernel.FDFlags{
|
||||
CloseOnExec: flags&linux.SFD_CLOEXEC != 0,
|
||||
})
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
|
||||
// Done.
|
||||
return uintptr(fd), nil, nil
|
||||
}
|
||||
|
||||
// Signalfd implements the linux syscall signalfd(2).
|
||||
func Signalfd(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
|
||||
fd := args[0].Int()
|
||||
sigset := args[1].Pointer()
|
||||
sigsetsize := args[2].SizeT()
|
||||
return sharedSignalfd(t, fd, sigset, sigsetsize, 0)
|
||||
}
|
||||
|
||||
// Signalfd4 implements the linux syscall signalfd4(2).
|
||||
func Signalfd4(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
|
||||
fd := args[0].Int()
|
||||
sigset := args[1].Pointer()
|
||||
sigsetsize := args[2].SizeT()
|
||||
flags := args[3].Int()
|
||||
return sharedSignalfd(t, fd, sigset, sigsetsize, flags)
|
||||
}
|
||||
@@ -139,14 +139,14 @@ func Override() {
|
||||
s.Table[277] = syscalls.Supported("sync_file_range", SyncFileRange)
|
||||
s.Table[280] = syscalls.Supported("utimensat", Utimensat)
|
||||
s.Table[281] = syscalls.Supported("epoll_pwait", EpollPwait)
|
||||
delete(s.Table, 282) // signalfd
|
||||
s.Table[282] = syscalls.Supported("signalfd", Signalfd)
|
||||
s.Table[283] = syscalls.Supported("timerfd_create", TimerfdCreate)
|
||||
s.Table[284] = syscalls.Supported("eventfd", Eventfd)
|
||||
delete(s.Table, 285) // fallocate
|
||||
s.Table[286] = syscalls.Supported("timerfd_settime", TimerfdSettime)
|
||||
s.Table[287] = syscalls.Supported("timerfd_gettime", TimerfdGettime)
|
||||
s.Table[288] = syscalls.Supported("accept4", Accept4)
|
||||
delete(s.Table, 289) // signalfd4
|
||||
s.Table[289] = syscalls.Supported("signalfd4", Signalfd4)
|
||||
s.Table[290] = syscalls.Supported("eventfd2", Eventfd2)
|
||||
s.Table[291] = syscalls.Supported("epoll_create1", EpollCreate1)
|
||||
s.Table[292] = syscalls.Supported("dup3", Dup3)
|
||||
|
||||
@@ -43,6 +43,7 @@ go_library(
|
||||
visibility = ["//pkg/sentry:internal"],
|
||||
deps = [
|
||||
"//pkg/abi/linux",
|
||||
"//pkg/binary",
|
||||
"//pkg/context",
|
||||
"//pkg/fd",
|
||||
"//pkg/fdnotifier",
|
||||
|
||||
Reference in New Issue
Block a user