github.com/ttpreport/gvisor-ligolo@v0.0.0-20240123134145-a858404967ba/pkg/sentry/strace/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 strace
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/ttpreport/gvisor-ligolo/pkg/abi"
    22  	"github.com/ttpreport/gvisor-ligolo/pkg/abi/linux"
    23  	"github.com/ttpreport/gvisor-ligolo/pkg/sentry/kernel"
    24  
    25  	"github.com/ttpreport/gvisor-ligolo/pkg/hostarch"
    26  )
    27  
    28  // signalNames contains the names of all named signals.
    29  var signalNames = abi.ValueSet{
    30  	uint64(linux.SIGABRT):   "SIGABRT",
    31  	uint64(linux.SIGALRM):   "SIGALRM",
    32  	uint64(linux.SIGBUS):    "SIGBUS",
    33  	uint64(linux.SIGCHLD):   "SIGCHLD",
    34  	uint64(linux.SIGCONT):   "SIGCONT",
    35  	uint64(linux.SIGFPE):    "SIGFPE",
    36  	uint64(linux.SIGHUP):    "SIGHUP",
    37  	uint64(linux.SIGILL):    "SIGILL",
    38  	uint64(linux.SIGINT):    "SIGINT",
    39  	uint64(linux.SIGIO):     "SIGIO",
    40  	uint64(linux.SIGKILL):   "SIGKILL",
    41  	uint64(linux.SIGPIPE):   "SIGPIPE",
    42  	uint64(linux.SIGPROF):   "SIGPROF",
    43  	uint64(linux.SIGPWR):    "SIGPWR",
    44  	uint64(linux.SIGQUIT):   "SIGQUIT",
    45  	uint64(linux.SIGSEGV):   "SIGSEGV",
    46  	uint64(linux.SIGSTKFLT): "SIGSTKFLT",
    47  	uint64(linux.SIGSTOP):   "SIGSTOP",
    48  	uint64(linux.SIGSYS):    "SIGSYS",
    49  	uint64(linux.SIGTERM):   "SIGTERM",
    50  	uint64(linux.SIGTRAP):   "SIGTRAP",
    51  	uint64(linux.SIGTSTP):   "SIGTSTP",
    52  	uint64(linux.SIGTTIN):   "SIGTTIN",
    53  	uint64(linux.SIGTTOU):   "SIGTTOU",
    54  	uint64(linux.SIGURG):    "SIGURG",
    55  	uint64(linux.SIGUSR1):   "SIGUSR1",
    56  	uint64(linux.SIGUSR2):   "SIGUSR2",
    57  	uint64(linux.SIGVTALRM): "SIGVTALRM",
    58  	uint64(linux.SIGWINCH):  "SIGWINCH",
    59  	uint64(linux.SIGXCPU):   "SIGXCPU",
    60  	uint64(linux.SIGXFSZ):   "SIGXFSZ",
    61  }
    62  
    63  var signalMaskActions = abi.ValueSet{
    64  	linux.SIG_BLOCK:   "SIG_BLOCK",
    65  	linux.SIG_UNBLOCK: "SIG_UNBLOCK",
    66  	linux.SIG_SETMASK: "SIG_SETMASK",
    67  }
    68  
    69  var sigActionFlags = abi.FlagSet{
    70  	{
    71  		Flag: linux.SA_NOCLDSTOP,
    72  		Name: "SA_NOCLDSTOP",
    73  	},
    74  	{
    75  		Flag: linux.SA_NOCLDWAIT,
    76  		Name: "SA_NOCLDWAIT",
    77  	},
    78  	{
    79  		Flag: linux.SA_SIGINFO,
    80  		Name: "SA_SIGINFO",
    81  	},
    82  	{
    83  		Flag: linux.SA_RESTORER,
    84  		Name: "SA_RESTORER",
    85  	},
    86  	{
    87  		Flag: linux.SA_ONSTACK,
    88  		Name: "SA_ONSTACK",
    89  	},
    90  	{
    91  		Flag: linux.SA_RESTART,
    92  		Name: "SA_RESTART",
    93  	},
    94  	{
    95  		Flag: linux.SA_NODEFER,
    96  		Name: "SA_NODEFER",
    97  	},
    98  	{
    99  		Flag: linux.SA_RESETHAND,
   100  		Name: "SA_RESETHAND",
   101  	},
   102  }
   103  
   104  func sigSet(t *kernel.Task, addr hostarch.Addr) string {
   105  	if addr == 0 {
   106  		return "null"
   107  	}
   108  
   109  	var b [linux.SignalSetSize]byte
   110  	if _, err := t.CopyInBytes(addr, b[:]); err != nil {
   111  		return fmt.Sprintf("%#x (error copying sigset: %v)", addr, err)
   112  	}
   113  
   114  	set := linux.SignalSet(hostarch.ByteOrder.Uint64(b[:]))
   115  
   116  	return fmt.Sprintf("%#x %s", addr, formatSigSet(set))
   117  }
   118  
   119  func formatSigSet(set linux.SignalSet) string {
   120  	var signals []string
   121  	linux.ForEachSignal(set, func(sig linux.Signal) {
   122  		signals = append(signals, signalNames.ParseDecimal(uint64(sig)))
   123  	})
   124  
   125  	return fmt.Sprintf("[%v]", strings.Join(signals, " "))
   126  }
   127  
   128  func sigAction(t *kernel.Task, addr hostarch.Addr) string {
   129  	if addr == 0 {
   130  		return "null"
   131  	}
   132  
   133  	var sa linux.SigAction
   134  	if _, err := sa.CopyIn(t, addr); err != nil {
   135  		return fmt.Sprintf("%#x (error copying sigaction: %v)", addr, err)
   136  	}
   137  
   138  	var handler string
   139  	switch sa.Handler {
   140  	case linux.SIG_IGN:
   141  		handler = "SIG_IGN"
   142  	case linux.SIG_DFL:
   143  		handler = "SIG_DFL"
   144  	default:
   145  		handler = fmt.Sprintf("%#x", sa.Handler)
   146  	}
   147  
   148  	return fmt.Sprintf("%#x {Handler: %s, Flags: %s, Restorer: %#x, Mask: %s}", addr, handler, sigActionFlags.Parse(sa.Flags), sa.Restorer, formatSigSet(sa.Mask))
   149  }