github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/syscalls/linux/sigset.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package linux
    16  
    17  import (
    18  	"github.com/SagerNet/gvisor/pkg/abi/linux"
    19  	"github.com/SagerNet/gvisor/pkg/errors/linuxerr"
    20  	"github.com/SagerNet/gvisor/pkg/hostarch"
    21  	"github.com/SagerNet/gvisor/pkg/sentry/kernel"
    22  	"github.com/SagerNet/gvisor/pkg/syserror"
    23  )
    24  
    25  // CopyInSigSet copies in a sigset_t, checks its size, and ensures that KILL and
    26  // STOP are clear.
    27  //
    28  // TODO(github.com/SagerNet/issue/1624): This is only exported because
    29  // syscalls/vfs2/signal.go depends on it. Once vfs1 is deleted and the vfs2
    30  // syscalls are moved into this package, then they can be unexported.
    31  func CopyInSigSet(t *kernel.Task, sigSetAddr hostarch.Addr, size uint) (linux.SignalSet, error) {
    32  	if size != linux.SignalSetSize {
    33  		return 0, linuxerr.EINVAL
    34  	}
    35  	b := t.CopyScratchBuffer(8)
    36  	if _, err := t.CopyInBytes(sigSetAddr, b); err != nil {
    37  		return 0, err
    38  	}
    39  	mask := hostarch.ByteOrder.Uint64(b[:])
    40  	return linux.SignalSet(mask) &^ kernel.UnblockableSignals, nil
    41  }
    42  
    43  // copyOutSigSet copies out a sigset_t.
    44  func copyOutSigSet(t *kernel.Task, sigSetAddr hostarch.Addr, mask linux.SignalSet) error {
    45  	b := t.CopyScratchBuffer(8)
    46  	hostarch.ByteOrder.PutUint64(b, uint64(mask))
    47  	_, err := t.CopyOutBytes(sigSetAddr, b)
    48  	return err
    49  }
    50  
    51  // copyInSigSetWithSize copies in a structure as below
    52  //
    53  //   struct {
    54  //       sigset_t* sigset_addr;
    55  //       size_t sizeof_sigset;
    56  //   };
    57  //
    58  // and returns sigset_addr and size.
    59  func copyInSigSetWithSize(t *kernel.Task, addr hostarch.Addr) (hostarch.Addr, uint, error) {
    60  	switch t.Arch().Width() {
    61  	case 8:
    62  		in := t.CopyScratchBuffer(16)
    63  		if _, err := t.CopyInBytes(addr, in); err != nil {
    64  			return 0, 0, err
    65  		}
    66  		maskAddr := hostarch.Addr(hostarch.ByteOrder.Uint64(in[0:]))
    67  		maskSize := uint(hostarch.ByteOrder.Uint64(in[8:]))
    68  		return maskAddr, maskSize, nil
    69  	default:
    70  		return 0, 0, syserror.ENOSYS
    71  	}
    72  }