fix(windows): launch commands on the interactive desktop

This commit is contained in:
cabbage committed 2026-10-27 18:35:29 +00:00
1 parent 30e7bb8545
commit 8d28925e44
20 files changed
+1036 -28

No files matched your search

@@ -11,6 +11,7 @@ import (
"io"
"os"
"os/exec"
"runtime"
"strings"
"sync"
"syscall"
@@ -242,6 +243,13 @@ func openLauncherPipe(channel, suffix string, access uint32) (*os.File, error) {
}
func (manager *execSupervisor) startViaLauncher(ctx context.Context, spec supervisor.StartSpec, token winapi.Token, identity supervisor.EffectiveIdentity, launch LaunchPlan, cleanup func()) (supervisor.Process, error) {
desktop, err := desktopForIdentity(identity)
if err != nil {
if cleanup != nil {
cleanup()
}
return nil, err
}
pipes, err := newLauncherPipes(identity.UserSID)
if err != nil {
if cleanup != nil {
@@ -299,6 +307,7 @@ func (manager *execSupervisor) startViaLauncher(ctx context.Context, spec superv
return nil, err
}
startup := winapi.StartupInfoEx{StartupInfo: winapi.StartupInfo{Cb: uint32(unsafe.Sizeof(winapi.StartupInfoEx{}))}}
startup.Desktop = desktopPointer(desktop)
var launcherInfo winapi.ProcessInformation
flags := uint32(winapi.CREATE_SUSPENDED | winapi.CREATE_UNICODE_ENVIRONMENT | winapi.EXTENDED_STARTUPINFO_PRESENT | winapi.CREATE_NO_WINDOW)
if err := winapi.CreateProcessAsUser(token, launcherApplication, &launcherCommandUTF16[0], nil, nil, false, flags, nil, nil, &startup.StartupInfo, &launcherInfo); err != nil {
@@ -334,6 +343,9 @@ func (manager *execSupervisor) startViaLauncher(ctx context.Context, spec superv
return nil, fmt.Errorf("read launcher hello: %w", err)
}
if frame.Kind != launcherFrameHello {
if frame.Kind == launcherFrameAbort {
return nil, launcherAbortError(frame.Payload)
}
return nil, errors.New("launcher did not send hello first")
}
var hello launcherHello
@@ -343,6 +355,11 @@ func (manager *execSupervisor) startViaLauncher(ctx context.Context, spec superv
if hello.PID != expected.PID || hello.Creation != expected.Creation || hello.SessionID != expected.SessionID || hello.Effective != expected.UserSID {
return nil, errors.New("launcher hello identity mismatch")
}
if desktop != "" {
if err := validateInteractiveDesktop(hello.Desktop); err != nil {
return nil, err
}
}
requestPayload, err := marshalLauncherPayload(launcherRequest{ApplicationName: launch.ApplicationName, CommandLine: launch.CommandLine, WorkingDirectory: launch.WorkingDirectory, Environment: launch.Environment})
if err != nil {
return nil, err
@@ -355,6 +372,9 @@ func (manager *execSupervisor) startViaLauncher(ctx context.Context, spec superv
return nil, fmt.Errorf("read shell preparation: %w", err)
}
if frame.Kind != launcherFrameShellPrepared {
if frame.Kind == launcherFrameAbort {
return nil, launcherAbortError(frame.Payload)
}
return nil, errors.New("launcher did not prepare a shell")
}
var prepared launcherShellPrepared
@@ -457,7 +477,9 @@ func (manager *execSupervisor) startViaLauncher(ctx context.Context, spec superv
// private pipes, reports its immutable identity, creates the requested shell
// suspended, and waits for the service's durable release frame before running
// any command code.
func RunLauncher(_ context.Context, channel string) error {
func RunLauncher(_ context.Context, channel string) (resultErr error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
generation, err := launcherGeneration(channel)
if err != nil {
return err
@@ -467,6 +489,15 @@ func RunLauncher(_ context.Context, channel string) error {
return err
}
defer control.Close()
defer func() {
if resultErr != nil {
detail := resultErr.Error()
if len(detail) > 4096 {
detail = detail[:4096]
}
_ = writeLauncherFrame(control, launcherFrame{Kind: launcherFrameAbort, Generation: generation, Payload: []byte(detail)})
}
}()
stdin, err := openLauncherPipe(channel, "stdin", winapi.GENERIC_READ)
if err != nil {
return err
@@ -496,11 +527,15 @@ func RunLauncher(_ context.Context, channel string) error {
if err != nil {
return err
}
desktop, err := verifyCurrentInteractiveDesktop(sessionID)
if err != nil {
return err
}
creation, err := processCreation(winapi.CurrentProcess())
if err != nil {
return err
}
helloPayload, err := marshalLauncherPayload(launcherHello{PID: winapi.GetCurrentProcessId(), Creation: creation, SessionID: sessionID, Effective: user.User.Sid.String()})
helloPayload, err := marshalLauncherPayload(launcherHello{PID: winapi.GetCurrentProcessId(), Creation: creation, SessionID: sessionID, Effective: user.User.Sid.String(), Desktop: desktop})
if err != nil {
return err
}
@@ -550,14 +585,13 @@ func RunLauncher(_ context.Context, channel string) error {
if err := attributes.Update(winapi.PROC_THREAD_ATTRIBUTE_HANDLE_LIST, unsafe.Pointer(&childHandles[0]), uintptr(len(childHandles))*unsafe.Sizeof(childHandles[0])); err != nil {
return err
}
startup := winapi.StartupInfoEx{StartupInfo: winapi.StartupInfo{Cb: uint32(unsafe.Sizeof(winapi.StartupInfoEx{})), Flags: winapi.STARTF_USESTDHANDLES | winapi.STARTF_USESHOWWINDOW, ShowWindow: winapi.SW_HIDE, StdInput: childHandles[0], StdOutput: childHandles[1], StdErr: childHandles[2]}}
startup, flags := commandStartup(childHandles[0], childHandles[1], childHandles[2], desktop)
startup.ProcThreadAttributeList = attributes.List()
var shellInfo winapi.ProcessInformation
var environment *uint16
if len(request.Environment) > 0 {
environment = &request.Environment[0]
}
flags := uint32(winapi.CREATE_NEW_CONSOLE | winapi.CREATE_SUSPENDED | winapi.CREATE_UNICODE_ENVIRONMENT | winapi.EXTENDED_STARTUPINFO_PRESENT)
if err := winapi.CreateProcess(application, &commandLine[0], nil, nil, true, flags, environment, workingDirectory, &startup.StartupInfo, &shellInfo); err != nil {
return fmt.Errorf("create suspended shell: %w", err)
}
@@ -664,6 +698,10 @@ func openSignalPipe(channel string) (*os.File, error) {
}
func runSignalHelper(ctx context.Context, executable string, token winapi.Token, identity supervisor.EffectiveIdentity, targetPID uint32, targetCreation uint64) (bool, error) {
desktop, err := desktopForIdentity(identity)
if err != nil {
return false, err
}
if executable == "" {
var err error
executable, err = os.Executable()
@@ -695,6 +733,7 @@ func runSignalHelper(ctx context.Context, executable string, token winapi.Token,
return false, err
}
startup := winapi.StartupInfo{Cb: uint32(unsafe.Sizeof(winapi.StartupInfo{}))}
startup.Desktop = desktopPointer(desktop)
var info winapi.ProcessInformation
flags := uint32(winapi.CREATE_NO_WINDOW | winapi.CREATE_UNICODE_ENVIRONMENT)
if err := winapi.CreateProcessAsUser(token, application, &commandUTF16[0], nil, nil, false, flags, nil, nil, &startup, &info); err != nil {