From 80ec5356cca5bffa0df75480cbb00a2d3d63425d Mon Sep 17 00:00:00 2001 From: Richard Fawcett Date: Wed, 17 Feb 2021 11:38:14 +0000 Subject: [PATCH] Ensure we close correct handle after checking if service exists (#793) Previously, this code would close the Service Control Manager (SCM) handle instead of the service handle, meaning: - any future calls to the SCM would fail due to invalid handle - a handle to the service was left open in error Also, reduce the access rights given to the handle, as we don't need ALL rights just to check if the service exists. --- src/WinSW.Core/Native/Service.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/WinSW.Core/Native/Service.cs b/src/WinSW.Core/Native/Service.cs index 944f083..6bafd53 100644 --- a/src/WinSW.Core/Native/Service.cs +++ b/src/WinSW.Core/Native/Service.cs @@ -164,13 +164,13 @@ namespace WinSW.Native internal bool ServiceExists(string serviceName) { - var serviceHandle = ServiceApis.OpenService(this.handle, serviceName, ServiceAccess.All); + var serviceHandle = ServiceApis.OpenService(this.handle, serviceName, ServiceAccess.QueryStatus); if (serviceHandle == IntPtr.Zero) { return false; } - _ = CloseServiceHandle(this.handle); + _ = CloseServiceHandle(serviceHandle); return true; }