diff --git a/internal/guest/bridge/bridge_signal_test.go b/internal/guest/bridge/bridge_signal_test.go new file mode 100644 index 0000000000..4760c500e3 --- /dev/null +++ b/internal/guest/bridge/bridge_signal_test.go @@ -0,0 +1,87 @@ +//go:build linux + +package bridge + +import ( + "context" + "encoding/json" + "io" + "syscall" + "testing" + "time" + + "github.com/Microsoft/hcsshim/internal/bridgeutils/gcserr" + "github.com/Microsoft/hcsshim/internal/guest/prot" + "github.com/Microsoft/hcsshim/internal/guest/runtime/hcsv2" + "github.com/Microsoft/hcsshim/internal/guest/stdio" + "github.com/Microsoft/hcsshim/pkg/securitypolicy" +) + +func signalRequest(t *testing.T, cid string, pid uint32, signal int) *Request { + t.Helper() + message, err := json.Marshal(map[string]interface{}{ + "ContainerId": cid, "ProcessId": pid, "Options": map[string]int{"Signal": signal}, + }) + if err != nil { + t.Fatal(err) + } + return &Request{Context: context.Background(), Message: message, ContainerID: cid} +} + +func TestSignalExternalProcess(t *testing.T) { + for _, signal := range []int{0, int(syscall.SIGTERM)} { + t.Run(syscall.Signal(signal).String(), func(t *testing.T) { + host := hcsv2.NewHost(nil, nil, &securitypolicy.OpenDoorSecurityPolicyEnforcer{}, io.Discard) + b := &Bridge{hostState: host} + pid, err := host.ExecProcess(context.Background(), hcsv2.UVMContainerID, + prot.ProcessParameters{CommandArgs: []string{"/bin/sleep", "60"}, WorkingDirectory: "/"}, stdio.ConnectionSettings{}) + if err != nil { + t.Fatal(err) + } + p, err := host.GetExternalProcess(pid) + if err != nil { + t.Fatal(err) + } + exit, done := p.Wait() + defer close(done) + defer p.Kill(context.Background(), syscall.SIGKILL) + // A container request must retain container lookup/policy semantics, even + // when the PID happens to identify an external process. + _, err = b.signalProcessV2(signalRequest(t, "missing-container", uint32(pid), 0)) + if err == nil { + t.Fatal("container lookup was bypassed") + } + select { + case <-exit: + t.Fatal("container request killed external process") + default: + } + if _, err := b.signalProcessV2(signalRequest(t, hcsv2.UVMContainerID, uint32(pid), signal)); err != nil { + t.Fatal(err) + } + select { + case code := <-exit: + if code == 0 { + t.Fatal("signal returned a successful exit") + } + case <-time.After(5 * time.Second): + t.Fatal("external process did not exit") + } + _, err = b.signalProcessV2(signalRequest(t, hcsv2.UVMContainerID, uint32(pid), 0)) + if hr, _ := gcserr.GetHresult(err); hr != gcserr.HrErrNotFound { + t.Fatalf("exited process: %v", err) + } + }) + } +} + +func TestSignalUnknownExternalProcess(t *testing.T) { + b := &Bridge{hostState: hcsv2.NewHost(nil, nil, nil, io.Discard)} + _, err := b.signalProcessV2(signalRequest(t, hcsv2.UVMContainerID, 0, 0)) + if hr, _ := gcserr.GetHresult(err); hr != gcserr.HrErrNotFound { + t.Fatalf("unknown process: %v", err) + } + if !capabilities.GuestDefinedCapabilities.ExternalProcessSignalSupported { + t.Fatal("missing external signal capability") + } +} diff --git a/internal/guest/bridge/bridge_v2.go b/internal/guest/bridge/bridge_v2.go index 19631bef4f..2ed06f3335 100644 --- a/internal/guest/bridge/bridge_v2.go +++ b/internal/guest/bridge/bridge_v2.go @@ -37,10 +37,11 @@ var capabilities = prot.GcsCapabilities{ }, RuntimeOsType: prot.OsTypeLinux, GuestDefinedCapabilities: prot.GcsGuestCapabilities{ - NamespaceAddRequestSupported: true, - SignalProcessSupported: true, - DumpStacksSupported: true, - DeleteContainerStateSupported: true, + NamespaceAddRequestSupported: true, + SignalProcessSupported: true, + ExternalProcessSignalSupported: true, + DumpStacksSupported: true, + DeleteContainerStateSupported: true, }, } @@ -285,8 +286,18 @@ func (b *Bridge) signalProcessV2(r *Request) (_ RequestResponse, err error) { signal = syscall.Signal(request.Options.Signal) } - if err := b.hostState.SignalContainerProcess(ctx, request.ContainerID, request.ProcessID, signal); err != nil { - return nil, err + if request.ContainerID == hcsv2.UVMContainerID { + p, err := b.hostState.GetExternalProcess(int(request.ProcessID)) + if err != nil { + return nil, err + } + if err := p.Kill(ctx, signal); err != nil { + return nil, err + } + } else { + if err := b.hostState.SignalContainerProcess(ctx, request.ContainerID, request.ProcessID, signal); err != nil { + return nil, err + } } return &prot.MessageResponseBase{}, nil diff --git a/internal/guest/prot/protocol.go b/internal/guest/prot/protocol.go index ea9e9a7d10..568a9e17df 100644 --- a/internal/guest/prot/protocol.go +++ b/internal/guest/prot/protocol.go @@ -275,10 +275,12 @@ type GcsCapabilities struct { // GcsGuestCapabilities represents the customized guest capabilities supported // by this GCS. type GcsGuestCapabilities struct { - NamespaceAddRequestSupported bool `json:",omitempty"` - SignalProcessSupported bool `json:",omitempty"` - DumpStacksSupported bool `json:",omitempty"` - DeleteContainerStateSupported bool `json:",omitempty"` + NamespaceAddRequestSupported bool `json:",omitempty"` + SignalProcessSupported bool `json:",omitempty"` + // ExternalProcessSignalSupported includes SignalProcess for UVMContainerID. + ExternalProcessSignalSupported bool `json:",omitempty"` + DumpStacksSupported bool `json:",omitempty"` + DeleteContainerStateSupported bool `json:",omitempty"` } // otelspancontext is the W3C TraceContext propagation headers used by diff --git a/internal/guest/runtime/hcsv2/process.go b/internal/guest/runtime/hcsv2/process.go index 23d5fa6298..bbd5b840c5 100644 --- a/internal/guest/runtime/hcsv2/process.go +++ b/internal/guest/runtime/hcsv2/process.go @@ -6,6 +6,7 @@ package hcsv2 import ( "context" "fmt" + "os" "os/exec" "sync" "syscall" @@ -270,8 +271,8 @@ type externalProcess struct { var _ Process = &externalProcess{} func (ep *externalProcess) Kill(_ context.Context, signal syscall.Signal) error { - if err := syscall.Kill(ep.cmd.Process.Pid, signal); err != nil { - if errors.Is(err, syscall.ESRCH) { + if err := ep.cmd.Process.Signal(signal); err != nil { + if errors.Is(err, syscall.ESRCH) || errors.Is(err, os.ErrProcessDone) { return gcserr.NewHresultError(gcserr.HrErrNotFound) } return err