From e0659b9908729701406aac04423e5a8da64fba7c Mon Sep 17 00:00:00 2001 From: Guillaume GILL Date: Thu, 27 Oct 2022 16:18:04 +0000 Subject: [PATCH] x/sys/windows/svc: Update IsWindowsService() to be aligned with .NET 6 The current IsWindowsService() function was inherited from the legacy .NET framework and not compatible inside windows containers. We need to update it so it works inside containers. References to: https://github.com/dotnet/runtime/issues/52416 Fixes golang/go#56335 --- windows/svc/security.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/windows/svc/security.go b/windows/svc/security.go index 1c51006ea..6fa295b40 100644 --- a/windows/svc/security.go +++ b/windows/svc/security.go @@ -68,10 +68,9 @@ func IsAnInteractiveSession() (bool, error) { // as a Windows service. func IsWindowsService() (bool, error) { // The below technique looks a bit hairy, but it's actually - // exactly what the .NET framework does for the similarly named function: - // https://github.com/dotnet/extensions/blob/f4066026ca06984b07e90e61a6390ac38152ba93/src/Hosting/WindowsServices/src/WindowsServiceHelpers.cs#L26-L31 - // Specifically, it looks up whether the parent process has session ID zero - // and is called "services". + // exactly what the .NET runtime (>= 6.0.10) does for the similarly named function: + // https://github.com/dotnet/runtime/blob/36bf84fc4a89209f4fdbc1fc201e81afd8be49b0/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/src/WindowsServiceHelpers.cs#L20-L33 + // Specifically, it looks up whether the parent process is called "services". var currentProcess windows.PROCESS_BASIC_INFORMATION infoSize := uint32(unsafe.Sizeof(currentProcess)) @@ -91,7 +90,7 @@ func IsWindowsService() (bool, error) { } for ; ; parentProcess = (*windows.SYSTEM_PROCESS_INFORMATION)(unsafe.Pointer(uintptr(unsafe.Pointer(parentProcess)) + uintptr(parentProcess.NextEntryOffset))) { if parentProcess.UniqueProcessID == currentProcess.InheritedFromUniqueProcessId { - return parentProcess.SessionID == 0 && strings.EqualFold("services.exe", parentProcess.ImageName.String()), nil + return strings.EqualFold("services.exe", parentProcess.ImageName.String()), nil } if parentProcess.NextEntryOffset == 0 { break