github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/pkg/abi/linux/signal.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/nicocha30/gvisor-ligolo/pkg/bits"
    19  	"github.com/nicocha30/gvisor-ligolo/pkg/hostarch"
    20  )
    21  
    22  const (
    23  	// SignalMaximum is the highest valid signal number.
    24  	SignalMaximum = 64
    25  
    26  	// FirstStdSignal is the lowest standard signal number.
    27  	FirstStdSignal = 1
    28  
    29  	// LastStdSignal is the highest standard signal number.
    30  	LastStdSignal = 31
    31  
    32  	// FirstRTSignal is the lowest real-time signal number.
    33  	//
    34  	// 32 (SIGCANCEL) and 33 (SIGSETXID) are used internally by glibc.
    35  	FirstRTSignal = 32
    36  
    37  	// LastRTSignal is the highest real-time signal number.
    38  	LastRTSignal = 64
    39  
    40  	// NumStdSignals is the number of standard signals.
    41  	NumStdSignals = LastStdSignal - FirstStdSignal + 1
    42  
    43  	// NumRTSignals is the number of realtime signals.
    44  	NumRTSignals = LastRTSignal - FirstRTSignal + 1
    45  )
    46  
    47  // Signal is a signal number.
    48  type Signal int
    49  
    50  // IsValid returns true if s is a valid standard or realtime signal. (0 is not
    51  // considered valid; interfaces special-casing signal number 0 should check for
    52  // 0 first before asserting validity.)
    53  func (s Signal) IsValid() bool {
    54  	return s > 0 && s <= SignalMaximum
    55  }
    56  
    57  // IsStandard returns true if s is a standard signal.
    58  //
    59  // Preconditions: s.IsValid().
    60  func (s Signal) IsStandard() bool {
    61  	return s <= LastStdSignal
    62  }
    63  
    64  // IsRealtime returns true if s is a realtime signal.
    65  //
    66  // Preconditions: s.IsValid().
    67  func (s Signal) IsRealtime() bool {
    68  	return s >= FirstRTSignal
    69  }
    70  
    71  // Index returns the index for signal s into arrays of both standard and
    72  // realtime signals (e.g. signal masks).
    73  //
    74  // Preconditions: s.IsValid().
    75  func (s Signal) Index() int {
    76  	return int(s - 1)
    77  }
    78  
    79  // Signals.
    80  const (
    81  	SIGABRT   = Signal(6)
    82  	SIGALRM   = Signal(14)
    83  	SIGBUS    = Signal(7)
    84  	SIGCHLD   = Signal(17)
    85  	SIGCLD    = Signal(17)
    86  	SIGCONT   = Signal(18)
    87  	SIGFPE    = Signal(8)
    88  	SIGHUP    = Signal(1)
    89  	SIGILL    = Signal(4)
    90  	SIGINT    = Signal(2)
    91  	SIGIO     = Signal(29)
    92  	SIGIOT    = Signal(6)
    93  	SIGKILL   = Signal(9)
    94  	SIGPIPE   = Signal(13)
    95  	SIGPOLL   = Signal(29)
    96  	SIGPROF   = Signal(27)
    97  	SIGPWR    = Signal(30)
    98  	SIGQUIT   = Signal(3)
    99  	SIGSEGV   = Signal(11)
   100  	SIGSTKFLT = Signal(16)
   101  	SIGSTOP   = Signal(19)
   102  	SIGSYS    = Signal(31)
   103  	SIGTERM   = Signal(15)
   104  	SIGTRAP   = Signal(5)
   105  	SIGTSTP   = Signal(20)
   106  	SIGTTIN   = Signal(21)
   107  	SIGTTOU   = Signal(22)
   108  	SIGUNUSED = Signal(31)
   109  	SIGURG    = Signal(23)
   110  	SIGUSR1   = Signal(10)
   111  	SIGUSR2   = Signal(12)
   112  	SIGVTALRM = Signal(26)
   113  	SIGWINCH  = Signal(28)
   114  	SIGXCPU   = Signal(24)
   115  	SIGXFSZ   = Signal(25)
   116  )
   117  
   118  // SignalSet is a signal mask with a bit corresponding to each signal.
   119  //
   120  // +marshal
   121  type SignalSet uint64
   122  
   123  // SignalSetSize is the size in bytes of a SignalSet.
   124  const SignalSetSize = 8
   125  
   126  // MakeSignalSet returns SignalSet with the bit corresponding to each of the
   127  // given signals set.
   128  func MakeSignalSet(sigs ...Signal) SignalSet {
   129  	indices := make([]int, len(sigs))
   130  	for i, sig := range sigs {
   131  		indices[i] = sig.Index()
   132  	}
   133  	return SignalSet(bits.Mask64(indices...))
   134  }
   135  
   136  // SignalSetOf returns a SignalSet with a single signal set.
   137  func SignalSetOf(sig Signal) SignalSet {
   138  	return SignalSet(bits.MaskOf64(sig.Index()))
   139  }
   140  
   141  // ForEachSignal invokes f for each signal set in the given mask.
   142  func ForEachSignal(mask SignalSet, f func(sig Signal)) {
   143  	bits.ForEachSetBit64(uint64(mask), func(i int) {
   144  		f(Signal(i + 1))
   145  	})
   146  }
   147  
   148  // 'how' values for rt_sigprocmask(2).
   149  const (
   150  	// SIG_BLOCK blocks the signals in the set.
   151  	SIG_BLOCK = 0
   152  
   153  	// SIG_UNBLOCK blocks the signals in the set.
   154  	SIG_UNBLOCK = 1
   155  
   156  	// SIG_SETMASK sets the signal mask to set.
   157  	SIG_SETMASK = 2
   158  )
   159  
   160  // Signal actions for rt_sigaction(2), from uapi/asm-generic/signal-defs.h.
   161  const (
   162  	// SIG_DFL performs the default action.
   163  	SIG_DFL = 0
   164  
   165  	// SIG_IGN ignores the signal.
   166  	SIG_IGN = 1
   167  )
   168  
   169  // Signal action flags for rt_sigaction(2), from uapi/asm-generic/signal.h.
   170  const (
   171  	SA_NOCLDSTOP = 0x00000001
   172  	SA_NOCLDWAIT = 0x00000002
   173  	SA_SIGINFO   = 0x00000004
   174  	SA_RESTORER  = 0x04000000
   175  	SA_ONSTACK   = 0x08000000
   176  	SA_RESTART   = 0x10000000
   177  	SA_NODEFER   = 0x40000000
   178  	SA_RESETHAND = 0x80000000
   179  	SA_NOMASK    = SA_NODEFER
   180  	SA_ONESHOT   = SA_RESETHAND
   181  )
   182  
   183  // Signal stack flags for signalstack(2), from include/uapi/linux/signal.h.
   184  const (
   185  	SS_ONSTACK = 1
   186  	SS_DISABLE = 2
   187  )
   188  
   189  // SIGPOLL si_codes.
   190  const (
   191  	// SI_POLL is defined as __SI_POLL in Linux 2.6.
   192  	SI_POLL = 2 << 16
   193  
   194  	// POLL_IN indicates that data input available.
   195  	POLL_IN = SI_POLL | 1
   196  
   197  	// POLL_OUT indicates that output buffers available.
   198  	POLL_OUT = SI_POLL | 2
   199  
   200  	// POLL_MSG indicates that an input message available.
   201  	POLL_MSG = SI_POLL | 3
   202  
   203  	// POLL_ERR indicates that there was an i/o error.
   204  	POLL_ERR = SI_POLL | 4
   205  
   206  	// POLL_PRI indicates that a high priority input available.
   207  	POLL_PRI = SI_POLL | 5
   208  
   209  	// POLL_HUP indicates that a device disconnected.
   210  	POLL_HUP = SI_POLL | 6
   211  )
   212  
   213  // Possible values for si_code.
   214  const (
   215  	// SI_USER is sent by kill, sigsend, raise.
   216  	SI_USER = 0
   217  
   218  	// SI_KERNEL is sent by the kernel from somewhere.
   219  	SI_KERNEL = 0x80
   220  
   221  	// SI_QUEUE is sent by sigqueue.
   222  	SI_QUEUE = -1
   223  
   224  	// SI_TIMER is sent by timer expiration.
   225  	SI_TIMER = -2
   226  
   227  	// SI_MESGQ is sent by real time mesq state change.
   228  	SI_MESGQ = -3
   229  
   230  	// SI_ASYNCIO is sent by AIO completion.
   231  	SI_ASYNCIO = -4
   232  
   233  	// SI_SIGIO is sent by queued SIGIO.
   234  	SI_SIGIO = -5
   235  
   236  	// SI_TKILL is sent by tkill system call.
   237  	SI_TKILL = -6
   238  
   239  	// SI_DETHREAD is sent by execve() killing subsidiary threads.
   240  	SI_DETHREAD = -7
   241  
   242  	// SI_ASYNCNL is sent by glibc async name lookup completion.
   243  	SI_ASYNCNL = -60
   244  )
   245  
   246  // CLD_* codes are only meaningful for SIGCHLD.
   247  const (
   248  	// CLD_EXITED indicates that a task exited.
   249  	CLD_EXITED = 1
   250  
   251  	// CLD_KILLED indicates that a task was killed by a signal.
   252  	CLD_KILLED = 2
   253  
   254  	// CLD_DUMPED indicates that a task was killed by a signal and then dumped
   255  	// core.
   256  	CLD_DUMPED = 3
   257  
   258  	// CLD_TRAPPED indicates that a task was stopped by ptrace.
   259  	CLD_TRAPPED = 4
   260  
   261  	// CLD_STOPPED indicates that a thread group completed a group stop.
   262  	CLD_STOPPED = 5
   263  
   264  	// CLD_CONTINUED indicates that a group-stopped thread group was continued.
   265  	CLD_CONTINUED = 6
   266  )
   267  
   268  // SYS_* codes are only meaningful for SIGSYS.
   269  const (
   270  	// SYS_SECCOMP indicates that a signal originates from seccomp.
   271  	SYS_SECCOMP = 1
   272  )
   273  
   274  // Possible values for Sigevent.Notify, aka struct sigevent::sigev_notify.
   275  const (
   276  	SIGEV_SIGNAL    = 0
   277  	SIGEV_NONE      = 1
   278  	SIGEV_THREAD    = 2
   279  	SIGEV_THREAD_ID = 4
   280  )
   281  
   282  // Sigevent represents struct sigevent.
   283  //
   284  // +marshal
   285  type Sigevent struct {
   286  	Value  uint64 // union sigval {int, void*}
   287  	Signo  int32
   288  	Notify int32
   289  
   290  	// struct sigevent here contains 48-byte union _sigev_un. However, only
   291  	// member _tid is significant to the kernel.
   292  	Tid         int32
   293  	UnRemainder [44]byte
   294  }
   295  
   296  // SigAction represents struct sigaction.
   297  //
   298  // +marshal
   299  // +stateify savable
   300  type SigAction struct {
   301  	Handler  uint64
   302  	Flags    uint64
   303  	Restorer uint64
   304  	Mask     SignalSet
   305  }
   306  
   307  // SignalStack represents information about a user stack, and is equivalent to
   308  // stack_t.
   309  //
   310  // +marshal
   311  // +stateify savable
   312  type SignalStack struct {
   313  	Addr  uint64
   314  	Flags uint32
   315  	_     uint32
   316  	Size  uint64
   317  }
   318  
   319  // Contains checks if the stack pointer is within this stack.
   320  func (s *SignalStack) Contains(sp hostarch.Addr) bool {
   321  	return hostarch.Addr(s.Addr) < sp && sp <= hostarch.Addr(s.Addr+s.Size)
   322  }
   323  
   324  // Top returns the stack's top address.
   325  func (s *SignalStack) Top() hostarch.Addr {
   326  	return hostarch.Addr(s.Addr + s.Size)
   327  }
   328  
   329  // IsEnabled returns true iff this signal stack is marked as enabled.
   330  func (s *SignalStack) IsEnabled() bool {
   331  	return s.Flags&SS_DISABLE == 0
   332  }
   333  
   334  // SignalInfo represents information about a signal being delivered, and is
   335  // equivalent to struct siginfo in linux kernel(linux/include/uapi/asm-generic/siginfo.h).
   336  //
   337  // +marshal
   338  // +stateify savable
   339  type SignalInfo struct {
   340  	Signo int32 // Signal number
   341  	Errno int32 // Errno value
   342  	Code  int32 // Signal code
   343  	_     uint32
   344  
   345  	// struct siginfo::_sifields is a union. In SignalInfo, fields in the union
   346  	// are accessed through methods.
   347  	//
   348  	// For reference, here is the definition of _sifields: (_sigfault._trapno,
   349  	// which does not exist on x86, omitted for clarity)
   350  	//
   351  	// union {
   352  	// 	int _pad[SI_PAD_SIZE];
   353  	//
   354  	// 	/* kill() */
   355  	// 	struct {
   356  	// 		__kernel_pid_t _pid;	/* sender's pid */
   357  	// 		__ARCH_SI_UID_T _uid;	/* sender's uid */
   358  	// 	} _kill;
   359  	//
   360  	// 	/* POSIX.1b timers */
   361  	// 	struct {
   362  	// 		__kernel_timer_t _tid;	/* timer id */
   363  	// 		int _overrun;		/* overrun count */
   364  	// 		char _pad[sizeof( __ARCH_SI_UID_T) - sizeof(int)];
   365  	// 		sigval_t _sigval;	/* same as below */
   366  	// 		int _sys_private;       /* not to be passed to user */
   367  	// 	} _timer;
   368  	//
   369  	// 	/* POSIX.1b signals */
   370  	// 	struct {
   371  	// 		__kernel_pid_t _pid;	/* sender's pid */
   372  	// 		__ARCH_SI_UID_T _uid;	/* sender's uid */
   373  	// 		sigval_t _sigval;
   374  	// 	} _rt;
   375  	//
   376  	// 	/* SIGCHLD */
   377  	// 	struct {
   378  	// 		__kernel_pid_t _pid;	/* which child */
   379  	// 		__ARCH_SI_UID_T _uid;	/* sender's uid */
   380  	// 		int _status;		/* exit code */
   381  	// 		__ARCH_SI_CLOCK_T _utime;
   382  	// 		__ARCH_SI_CLOCK_T _stime;
   383  	// 	} _sigchld;
   384  	//
   385  	// 	/* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
   386  	// 	struct {
   387  	// 		void *_addr; /* faulting insn/memory ref. */
   388  	// 		short _addr_lsb; /* LSB of the reported address */
   389  	// 	} _sigfault;
   390  	//
   391  	// 	/* SIGPOLL */
   392  	// 	struct {
   393  	// 		__ARCH_SI_BAND_T _band;	/* POLL_IN, POLL_OUT, POLL_MSG */
   394  	// 		int _fd;
   395  	// 	} _sigpoll;
   396  	//
   397  	// 	/* SIGSYS */
   398  	// 	struct {
   399  	// 		void *_call_addr; /* calling user insn */
   400  	// 		int _syscall;	/* triggering system call number */
   401  	// 		unsigned int _arch;	/* AUDIT_ARCH_* of syscall */
   402  	// 	} _sigsys;
   403  	// } _sifields;
   404  	//
   405  	// _sifields is padded so that the size of siginfo is SI_MAX_SIZE = 128
   406  	// bytes.
   407  	Fields [128 - 16]byte
   408  }
   409  
   410  // FixSignalCodeForUser fixes up si_code.
   411  //
   412  // The si_code we get from Linux may contain the kernel-specific code in the
   413  // top 16 bits if it's positive (e.g., from ptrace). Linux's
   414  // copy_siginfo_to_user does:
   415  // err |= __put_user((short)from->si_code, &to->si_code);
   416  // to mask out those bits and we need to do the same.
   417  func (s *SignalInfo) FixSignalCodeForUser() {
   418  	if s.Code > 0 {
   419  		s.Code &= 0x0000ffff
   420  	}
   421  }
   422  
   423  // PID returns the si_pid field.
   424  func (s *SignalInfo) PID() int32 {
   425  	return int32(hostarch.ByteOrder.Uint32(s.Fields[0:4]))
   426  }
   427  
   428  // SetPID mutates the si_pid field.
   429  func (s *SignalInfo) SetPID(val int32) {
   430  	hostarch.ByteOrder.PutUint32(s.Fields[0:4], uint32(val))
   431  }
   432  
   433  // UID returns the si_uid field.
   434  func (s *SignalInfo) UID() int32 {
   435  	return int32(hostarch.ByteOrder.Uint32(s.Fields[4:8]))
   436  }
   437  
   438  // SetUID mutates the si_uid field.
   439  func (s *SignalInfo) SetUID(val int32) {
   440  	hostarch.ByteOrder.PutUint32(s.Fields[4:8], uint32(val))
   441  }
   442  
   443  // Sigval returns the sigval field, which is aliased to both si_int and si_ptr.
   444  func (s *SignalInfo) Sigval() uint64 {
   445  	return hostarch.ByteOrder.Uint64(s.Fields[8:16])
   446  }
   447  
   448  // SetSigval mutates the sigval field.
   449  func (s *SignalInfo) SetSigval(val uint64) {
   450  	hostarch.ByteOrder.PutUint64(s.Fields[8:16], val)
   451  }
   452  
   453  // TimerID returns the si_timerid field.
   454  func (s *SignalInfo) TimerID() TimerID {
   455  	return TimerID(hostarch.ByteOrder.Uint32(s.Fields[0:4]))
   456  }
   457  
   458  // SetTimerID sets the si_timerid field.
   459  func (s *SignalInfo) SetTimerID(val TimerID) {
   460  	hostarch.ByteOrder.PutUint32(s.Fields[0:4], uint32(val))
   461  }
   462  
   463  // Overrun returns the si_overrun field.
   464  func (s *SignalInfo) Overrun() int32 {
   465  	return int32(hostarch.ByteOrder.Uint32(s.Fields[4:8]))
   466  }
   467  
   468  // SetOverrun sets the si_overrun field.
   469  func (s *SignalInfo) SetOverrun(val int32) {
   470  	hostarch.ByteOrder.PutUint32(s.Fields[4:8], uint32(val))
   471  }
   472  
   473  // Addr returns the si_addr field.
   474  func (s *SignalInfo) Addr() uint64 {
   475  	return hostarch.ByteOrder.Uint64(s.Fields[0:8])
   476  }
   477  
   478  // SetAddr sets the si_addr field.
   479  func (s *SignalInfo) SetAddr(val uint64) {
   480  	hostarch.ByteOrder.PutUint64(s.Fields[0:8], val)
   481  }
   482  
   483  // Status returns the si_status field.
   484  func (s *SignalInfo) Status() int32 {
   485  	return int32(hostarch.ByteOrder.Uint32(s.Fields[8:12]))
   486  }
   487  
   488  // SetStatus mutates the si_status field.
   489  func (s *SignalInfo) SetStatus(val int32) {
   490  	hostarch.ByteOrder.PutUint32(s.Fields[8:12], uint32(val))
   491  }
   492  
   493  // CallAddr returns the si_call_addr field.
   494  func (s *SignalInfo) CallAddr() uint64 {
   495  	return hostarch.ByteOrder.Uint64(s.Fields[0:8])
   496  }
   497  
   498  // SetCallAddr mutates the si_call_addr field.
   499  func (s *SignalInfo) SetCallAddr(val uint64) {
   500  	hostarch.ByteOrder.PutUint64(s.Fields[0:8], val)
   501  }
   502  
   503  // Syscall returns the si_syscall field.
   504  func (s *SignalInfo) Syscall() int32 {
   505  	return int32(hostarch.ByteOrder.Uint32(s.Fields[8:12]))
   506  }
   507  
   508  // SetSyscall mutates the si_syscall field.
   509  func (s *SignalInfo) SetSyscall(val int32) {
   510  	hostarch.ByteOrder.PutUint32(s.Fields[8:12], uint32(val))
   511  }
   512  
   513  // Arch returns the si_arch field.
   514  func (s *SignalInfo) Arch() uint32 {
   515  	return hostarch.ByteOrder.Uint32(s.Fields[12:16])
   516  }
   517  
   518  // SetArch mutates the si_arch field.
   519  func (s *SignalInfo) SetArch(val uint32) {
   520  	hostarch.ByteOrder.PutUint32(s.Fields[12:16], val)
   521  }
   522  
   523  // Band returns the si_band field.
   524  func (s *SignalInfo) Band() int64 {
   525  	return int64(hostarch.ByteOrder.Uint64(s.Fields[0:8]))
   526  }
   527  
   528  // SetBand mutates the si_band field.
   529  func (s *SignalInfo) SetBand(val int64) {
   530  	// Note: this assumes the platform uses `long` as `__ARCH_SI_BAND_T`.
   531  	// On some platforms, which gVisor doesn't support, `__ARCH_SI_BAND_T` is
   532  	// `int`. See siginfo.h.
   533  	hostarch.ByteOrder.PutUint64(s.Fields[0:8], uint64(val))
   534  }
   535  
   536  // FD returns the si_fd field.
   537  func (s *SignalInfo) FD() uint32 {
   538  	return hostarch.ByteOrder.Uint32(s.Fields[8:12])
   539  }
   540  
   541  // SetFD mutates the si_fd field.
   542  func (s *SignalInfo) SetFD(val uint32) {
   543  	hostarch.ByteOrder.PutUint32(s.Fields[8:12], val)
   544  }