From 0b66862738a333605dcaff8dcac8a19ffc616df3 Mon Sep 17 00:00:00 2001 From: magnuguli Date: Tue, 26 Nov 2024 09:07:07 +0100 Subject: [PATCH] Bugfix: Rewrote addProcessToGroup. Fixed possible invalid process handle by using windows.OpenProcess instead. Fixes issue: 3980 --- internal/externalcmd/cmd_win.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/internal/externalcmd/cmd_win.go b/internal/externalcmd/cmd_win.go index e424d49d806..5e4863be15d 100644 --- a/internal/externalcmd/cmd_win.go +++ b/internal/externalcmd/cmd_win.go @@ -44,13 +44,21 @@ func closeProcessGroup(h windows.Handle) error { } func addProcessToGroup(h windows.Handle, p *os.Process) error { - type process struct { - Pid int - Handle uintptr + + // Combine the required access rights + access := uint32(windows.PROCESS_SET_QUOTA | windows.PROCESS_TERMINATE) + + processHandle, err := windows.OpenProcess(access, false, uint32(p.Pid)) + if err != nil { + return fmt.Errorf("failed to open process: %v", err) } + defer windows.CloseHandle(processHandle) - return windows.AssignProcessToJobObject(h, - windows.Handle((*process)(unsafe.Pointer(p)).Handle)) + err = windows.AssignProcessToJobObject(h, processHandle) + if err != nil { + return fmt.Errorf("failed to assign process to job object: %v", err) + } + return nil } func (e *Cmd) runOSSpecific(env []string) error {