From c038b80745e954b7e8f441df7f77e599f2c0649c Mon Sep 17 00:00:00 2001 From: 0721-art <0721-art@users.noreply.github.com> Date: Thu, 19 Nov 2026 09:36:52 +0800 Subject: [PATCH] fix(upgrades): never take the Windows service host for a supervisor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `Win32_Process` returns an empty `ExecutablePath` for processes the account may not read, and `svchost.exe` is one of them. The supervisor lookup filtered the process chain by that path, so the service host stayed a candidate; on a task-scheduled install the `Win32_Service` query then matched the `Schedule` service every task's tree hangs under, and the update refused with `not_controllable` — access denied on the wrong service. Carry the process name (Win32_Process.Name, /proc//comm, `ps` comm) and filter the service host by name, keeping the image-path check as the fallback for a listing that has no name. A regression test feeds the rows that a non-admin account actually gets: `svchost.exe` with no executable path. Fixes #537 --- lib/upgrade-process.test.mjs | 12 ++++++++++++ lib/upgrade-process.ts | 24 +++++++++++++++++------- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/lib/upgrade-process.test.mjs b/lib/upgrade-process.test.mjs index 5215e1a9b..34e20a606 100644 --- a/lib/upgrade-process.test.mjs +++ b/lib/upgrade-process.test.mjs @@ -528,3 +528,15 @@ test("a scheduled task that loops the host is stopped and started as that task", try { powershell(`Stop-ScheduledTask -TaskName '${name}' -ErrorAction SilentlyContinue; Unregister-ScheduledTask -TaskName '${name}' -Confirm:$false -ErrorAction SilentlyContinue`); } catch { /* not registered */ } } }); + +test("the Windows service host is never a supervisor, even where its ExecutablePath is unreadable", () => { + const serviceHost = (name, exe) => upgrade.isServiceHostProcess({ name, exe }); + // A non-admin account gets an empty ExecutablePath (and CommandLine) for svchost.exe, which used to make the + // Win32_Service lookup match the Schedule service and refuse a task-scheduled daemon with not_controllable. + assert.equal(serviceHost("svchost.exe", ""), true); + assert.equal(serviceHost("SVCHOST.EXE", ""), true); + assert.equal(serviceHost("svchost.exe", "C:/Windows/System32/svchost.exe"), true); + assert.equal(serviceHost("", "C:/Windows/System32/svchost.exe"), true); // name unavailable: fall back to the image path + assert.equal(serviceHost("node.exe", "E:/Nodev/node.exe"), false); + assert.equal(serviceHost("ompweb.exe", ""), false); +}); diff --git a/lib/upgrade-process.ts b/lib/upgrade-process.ts index 43f5ec82b..d29f3b0f8 100644 --- a/lib/upgrade-process.ts +++ b/lib/upgrade-process.ts @@ -93,7 +93,16 @@ function assertTrustedFile(path: string, what: string): void { // ---------------------------------------------------------------- processes -interface ProcessInfo { pid: number; ppid: number; state: string; exe: string; args: string[]; cwd: string | null; created: string | null } +export interface ProcessInfo { pid: number; ppid: number; state: string; name: string; exe: string; args: string[]; cwd: string | null; created: string | null } + +/** + * The Windows service host also parents the tree of a scheduled task, so it is never a candidate supervisor. + * Compare the process name: `Win32_Process.ExecutablePath` is empty for processes a non-admin account may not + * read (svchost included), and a service then looked like the supervisor of a task-scheduled daemon. + */ +export function isServiceHostProcess(row: Pick): boolean { + return /^svchost\.exe$/i.test(row.name || basename(row.exe)); +} /** CommandLineToArgvW's rules: whitespace separates arguments, quotes group, backslashes escape only before a quote. */ function splitWindowsCommandLine(line: string): string[] { @@ -186,14 +195,15 @@ async function listProcesses(): Promise { const stat = readFileSync(`/proc/${id}/stat`, "utf8"); const [state, ppid] = stat.slice(stat.lastIndexOf(")") + 2).split(" "); const link = (name: string) => { try { return readlinkSync(`/proc/${id}/${name}`).replace(/ \(deleted\)$/, ""); } catch { return null; } }; - rows.push({ pid, ppid: Number(ppid), state, exe: link("exe") ?? "", args: readFileSync(`/proc/${id}/cmdline`, "utf8").split("\0").filter(Boolean), cwd: link("cwd"), created: null }); + const comm = (() => { try { return readFileSync(`/proc/${id}/comm`, "utf8").trim(); } catch { return ""; } })(); + rows.push({ pid, ppid: Number(ppid), state, name: comm, exe: link("exe") ?? "", args: readFileSync(`/proc/${id}/cmdline`, "utf8").split("\0").filter(Boolean), cwd: link("cwd"), created: null }); } catch { /* exited while listing */ } } return rows; } if (process.platform === "win32") { - const out = await powershell(`$rows = @(Get-CimInstance Win32_Process | ForEach-Object { [pscustomobject]@{ pid = [int]$_.ProcessId; ppid = [int]$_.ParentProcessId; exe = [string]$_.ExecutablePath; cmd = [string]$_.CommandLine; created = $(if ($_.CreationDate) { [string]$_.CreationDate.ToUniversalTime().Ticks } else { '' }) } })\nConvertTo-Json -Compress -InputObject $rows`, 120000); - return parseJson<{ pid: number; ppid: number; exe: string; cmd: string; created: string }[]>(out, "process list").map((row) => ({ pid: row.pid, ppid: row.ppid, state: "R", exe: row.exe, args: splitWindowsCommandLine(row.cmd), cwd: null, created: row.created || null })); + const out = await powershell(`$rows = @(Get-CimInstance Win32_Process | ForEach-Object { [pscustomobject]@{ pid = [int]$_.ProcessId; ppid = [int]$_.ParentProcessId; name = [string]$_.Name; exe = [string]$_.ExecutablePath; cmd = [string]$_.CommandLine; created = $(if ($_.CreationDate) { [string]$_.CreationDate.ToUniversalTime().Ticks } else { '' }) } })\nConvertTo-Json -Compress -InputObject $rows`, 120000); + return parseJson<{ pid: number; ppid: number; name: string; exe: string; cmd: string; created: string }[]>(out, "process list").map((row) => ({ pid: row.pid, ppid: row.ppid, state: "R", name: row.name || "", exe: row.exe, args: splitWindowsCommandLine(row.cmd), cwd: null, created: row.created || null })); } const env = { ...process.env, LC_ALL: "C" }; const [commands, names] = await Promise.all([ @@ -207,7 +217,7 @@ async function listProcesses(): Promise { const match = line.match(/^\s*(\d+)\s+(\d+)\s+(\S+)\s+(.*)$/); if (match) { const exe = exes.get(Number(match[1])) ?? ""; - rows.push({ pid: Number(match[1]), ppid: Number(match[2]), state: match[3], exe, args: posixArguments(match[4].trim(), exe), cwd: null, created: null }); + rows.push({ pid: Number(match[1]), ppid: Number(match[2]), state: match[3], name: basename(exe), exe, args: posixArguments(match[4].trim(), exe), cwd: null, created: null }); } } return rows; @@ -517,8 +527,8 @@ async function inspectWindowsSupervisor(me: ProcessInfo, rows: ProcessInfo[], in const normal = (value: string) => value.toLowerCase().replaceAll("\\", "/"); const scripts = [...new Set(chain.slice(1).flatMap((row) => row.args.slice(1).filter((arg) => /\.(?:ps1|cmd|bat|vbs)$/i.test(arg))))]; const found = parseJson<{ services: WindowsService[]; tasks: WindowsTask[] }>(await powershell( - // A scheduled task's tree hangs under the Schedule service's svchost (shared by many services): never a candidate supervisor. - `$ids = @(${chain.filter((row) => !/(^|[\\/])svchost\.exe$/i.test(row.exe)).map((row) => row.pid).join(",")})\n$scripts = ${psStrings(scripts.map(normal))}\n` + + // Never a candidate supervisor: a scheduled task's tree hangs under the service host (see isServiceHostProcess). + `$ids = @(${chain.filter((row) => !isServiceHostProcess(row)).map((row) => row.pid).join(",")})\n$scripts = ${psStrings(scripts.map(normal))}\n` + `$services = @(Get-CimInstance Win32_Service | Where-Object { $_.ProcessId -and ($ids -contains [int]$_.ProcessId) } | ForEach-Object { ${SERVICE_FIELDS} })\n` + `$tasks = @()\nif ($scripts.Count -gt 0) { $tasks = @(Get-ScheduledTask | ForEach-Object { $t = $_; $text = ((@($t.Actions | ForEach-Object { $_.Execute + ' ' + $_.Arguments })) -join ' ').ToLowerInvariant().Replace('\\', '/'); $hit = $false; foreach ($s in $scripts) { if ($text.Contains($s)) { $hit = $true } }; if ($hit) { ${TASK_FIELDS} } }) }\n` + `ConvertTo-Json -Compress -Depth 6 -InputObject ([pscustomobject]@{ services = $services; tasks = $tasks })`), "supervisor query");