diff --git a/PCL.Core/App/Essentials/PromoteService.cs b/PCL.Core/App/Essentials/PromoteService.cs index 34ffdfab58..412adb240f 100644 --- a/PCL.Core/App/Essentials/PromoteService.cs +++ b/PCL.Core/App/Essentials/PromoteService.cs @@ -132,6 +132,13 @@ private static void _PerformAsPromoteProcess(string pid) var pipeName = _GetPromotePipeName(process.Id); var pipe = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut); pipe.Connect(10000); + var serverProcessId = (int)KernelInterop.GetNamedPipeServerProcessId(pipe.SafePipeHandle.DangerousGetHandle()); + if (serverProcessId != process.Id) + { + Context.Error("管道服务端验证失败,正在退出"); + pipe.Dispose(); + return; + } Context.Info("已连接,开始通信"); var reader = new StreamReader(pipe); var writer = new StreamWriter(pipe); diff --git a/PCL.Core/Utils/OS/KernelInterop.cs b/PCL.Core/Utils/OS/KernelInterop.cs index 229258e69e..7e6c865d5c 100644 --- a/PCL.Core/Utils/OS/KernelInterop.cs +++ b/PCL.Core/Utils/OS/KernelInterop.cs @@ -21,6 +21,10 @@ public static partial class KernelInterop [return: MarshalAs(UnmanagedType.Bool)] private static partial bool _GetNamedPipeClientProcessId(IntPtr pipeHandle, out uint clientProcessId); + [LibraryImport("kernel32.dll", EntryPoint = "GetNamedPipeServerProcessId", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + private static partial bool _GetNamedPipeServerProcessId(IntPtr pipeHandle, out uint serverProcessId); + [LibraryImport("kernel32.dll", EntryPoint = "GetLogicalProcessorInformationEx", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static partial bool _GetLogicalProcessorInformationEx( @@ -98,6 +102,17 @@ public static uint GetNamedPipeClientProcessId(IntPtr pipeHandle) return clientProcessId; } + /// + /// 获取指定命名管道当前连接的服务端进程 ID + /// + /// 命名管道句柄 + /// 获取到的进程 ID + public static uint GetNamedPipeServerProcessId(IntPtr pipeHandle) + { + if (!_GetNamedPipeServerProcessId(pipeHandle, out var serverProcessId)) _ThrowLastWin32Error(); + return serverProcessId; + } + /// /// 获取仅包含性能核(P-core)的逻辑处理器数量。 /// 在不支持 EfficiencyClass(旧 OS 或非混合架构)时,会退回到 Environment.ProcessorCount。