Files
tailscale/clientupdate/clientupdate_windows.go
T
Nick Khyl cd4f621399 clientupdate: don't uninstall the Windows client after a failed update
If msiexec failed to install an update for any reason, installMSI
assumed it was a downgrade, which msiexec does not permit, uninstalled
the current version and tried again. If the second attempt failed too,
such as when a custom action in the new package kept failing,
the device was left without Tailscale.

Uninstall the current version only if the requested version is older
than it, and do so before running the installer. Otherwise, return
the error and let Windows Installer roll back to the current version.

If the current version has been uninstalled and Windows Installer
is busy with another installation, keep retrying with a backoff
for up to 30 minutes rather than leaving the device without Tailscale.

Fixes tailscale/corp#45537
Updates tailscale/corp#52316

Change-Id: I066480c718b3bfca53a65c54ebf948562bc6ba2b
Signed-off-by: Nick Khyl <nickk@tailscale.com>
2026-12-01 14:43:23 -08:00

354 lines
12 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
// Windows-specific stuff that can't go in clientupdate.go because it needs
// x/sys/windows.
package clientupdate
import (
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/google/uuid"
"golang.org/x/sys/windows"
"tailscale.com/util/backoff"
"tailscale.com/util/cmpver"
"tailscale.com/util/winutil"
"tailscale.com/util/winutil/authenticode"
)
const (
// winMSIEnv is the environment variable that, if set, is the MSI file for
// the update command to install. It's passed like this so we can stop the
// tailscale.exe process from running before the msiexec process runs and
// tries to overwrite ourselves.
winMSIEnv = "TS_UPDATE_WIN_MSI"
// winVersionEnv is the environment variable that is set along with
// winMSIEnv and carries the version of tailscale that is being installed.
// It is used to name the log files and to tell whether the update is a downgrade,
// in which case the current version is uninstalled first.
winVersionEnv = "TS_UPDATE_WIN_VERSION"
// updaterPrefix is the prefix for the temporary executable created by [makeSelfCopy].
updaterPrefix = "tailscale-updater"
)
func makeCmdTailscaleCopy() (origPathExe, tmpPathExe string, err error) {
srcExe, err := findCmdTailscale()
if err != nil {
return "", "", err
}
f, err := os.Open(srcExe)
if err != nil {
return "", "", err
}
defer f.Close()
f2, err := os.CreateTemp("", updaterPrefix+"-*.exe")
if err != nil {
return "", "", err
}
if err := markTempFileWindows(f2.Name()); err != nil {
return "", "", err
}
if _, err := io.Copy(f2, f); err != nil {
f2.Close()
return "", "", err
}
return srcExe, f2.Name(), f2.Close()
}
// findCmdTailscale returns the path to the binary that should be copied for the update
// re-execution. The copy is re-executed with "update" as a subcommand, so it must be
// a binary that handles "update" (ie tailscale.exe, not tailscaled.exe)
func findCmdTailscale() (string, error) {
selfExe, err := os.Executable()
if err != nil {
return "", err
}
if strings.EqualFold(filepath.Base(selfExe), "tailscale.exe") {
return selfExe, nil
}
ts := filepath.Join(filepath.Dir(selfExe), "tailscale.exe")
if _, err := os.Stat(ts); err != nil {
return "", fmt.Errorf("cannot find tailscale.exe alongside %s: %w", selfExe, err)
}
return ts, nil
}
func markTempFileWindows(name string) error {
name16 := windows.StringToUTF16Ptr(name)
return windows.MoveFileEx(name16, nil, windows.MOVEFILE_DELAY_UNTIL_REBOOT)
}
const certSubjectTailscale = "Tailscale Inc."
func verifyAuthenticode(path string) error {
return authenticode.Verify(path, certSubjectTailscale)
}
func (up *Updater) updateWindows() error {
if msi := os.Getenv(winMSIEnv); msi != "" {
// stdout/stderr from this part of the install could be lost since the
// parent tailscaled is replaced. Create a temp log file to have some
// output to debug with in case update fails.
close, err := up.switchOutputToFile()
if err != nil {
up.Logf("failed to create log file for installation: %v; proceeding with existing outputs", err)
} else {
defer close.Close()
}
up.Logf("installing %v ...", msi)
if err := up.installMSI(msi); err != nil {
up.Logf("MSI install failed: %v", err)
return err
}
up.Logf("success.")
return nil
}
if !winutil.IsCurrentProcessElevated() {
return errors.New(`update must be run as Administrator
you can run the command prompt as Administrator one of these ways:
* right-click cmd.exe, select 'Run as administrator'
* press Windows+x, then press a
* press Windows+r, type in "cmd", then press Ctrl+Shift+Enter`)
}
ver, err := requestedTailscaleVersion(up.Version, up.Track)
if err != nil {
return err
}
arch := runtime.GOARCH
if arch == "386" {
arch = "x86"
}
if !up.confirm(ver) {
return nil
}
tsDir := filepath.Join(os.Getenv("ProgramData"), "Tailscale")
msiDir := filepath.Join(tsDir, "MSICache")
if fi, err := os.Stat(tsDir); err != nil {
return fmt.Errorf("expected %s to exist, got stat error: %w", tsDir, err)
} else if !fi.IsDir() {
return fmt.Errorf("expected %s to be a directory; got %v", tsDir, fi.Mode())
}
if err := os.MkdirAll(msiDir, 0700); err != nil {
return err
}
up.cleanupOldDownloads(filepath.Join(msiDir, "*.msi"))
pkgsPath := fmt.Sprintf("%s/tailscale-setup-%s-%s.msi", up.Track, ver, arch)
msiTarget := filepath.Join(msiDir, path.Base(pkgsPath))
if err := up.downloadURLToFile(pkgsPath, msiTarget); err != nil {
return err
}
up.Logf("verifying MSI authenticode...")
if err := verifyAuthenticode(msiTarget); err != nil {
return fmt.Errorf("authenticode verification of %s failed: %w", msiTarget, err)
}
up.Logf("authenticode verification succeeded")
up.Logf("making tailscale.exe copy to switch to...")
up.cleanupOldDownloads(filepath.Join(os.TempDir(), updaterPrefix+"-*.exe"))
cmdTailscale, cmdTailscaleCopy, err := makeCmdTailscaleCopy()
if err != nil {
return err
}
defer os.Remove(cmdTailscaleCopy)
up.Logf("running tailscale.exe copy for final install...")
cmd := exec.Command(cmdTailscaleCopy, "update")
cmd.Env = append(os.Environ(), winMSIEnv+"="+msiTarget, winVersionEnv+"="+ver)
cmd.Stdout = up.Stderr
cmd.Stderr = up.Stderr
cmd.Stdin = os.Stdin
if err := cmd.Start(); err != nil {
return err
}
if selfExe, err := os.Executable(); err == nil && selfExe != cmdTailscale {
// We're tailscaled (the service's child process) rather
// than tailscale (the CLI). If we exited now, the service
// would restart us right away (see babysitProc in cmd/tailscaled),
// so keep running until the installer stops the service.
return cmd.Wait()
}
// Once it's started, exit ourselves, so the binary is free
// to be replaced.
os.Exit(0)
panic("unreachable")
}
// installBusyRetryTimeout is how long installMSI keeps retrying while
// Windows Installer is busy, if it has already uninstalled the current version.
const installBusyRetryTimeout = 30 * time.Minute
func (up *Updater) installMSI(msi string) error {
// msiexec won't permit a downgrade, so uninstall the current version first.
// Only do it if we know it is a downgrade. A failed upgrade is rolled back
// by Windows Installer, whereas uninstalling the current version after
// a failure would leave the device without Tailscale if the install fails again.
var uninstalled bool
if ver := os.Getenv(winVersionEnv); ver != "" && cmpver.Compare(ver, up.currentVersion) < 0 {
uninstalled = up.uninstallCurrentMSI() == nil
}
if !uninstalled {
return up.runMSIInstall(msi)
}
// The device has no Tailscale until the install succeeds,
// so don't give up right away if Windows Installer is busy.
ctx, cancel := context.WithTimeout(context.Background(), installBusyRetryTimeout)
defer cancel()
bo := backoff.NewBackoff("msiexec", up.Logf, time.Minute, backoff.WithInitialBackoff(10*time.Second))
for {
err := up.runMSIInstall(msi)
if err == nil || ctx.Err() != nil {
return err
}
if !isRetryableInstallError(err) {
return err
}
bo.BackOff(ctx, err)
}
}
func isRetryableInstallError(err error) bool {
ee, ok := err.(*exec.ExitError)
if !ok {
return false
}
// ERROR_INSTALL_ALREADY_RUNNING indicates that the Windows Installer
// is busy installing/uninstalling/updating another package.
// We can retry the installation later.
return windows.Errno(ee.ExitCode()) == windows.ERROR_INSTALL_ALREADY_RUNNING
}
// runMSIInstall runs msiexec to install msi once.
func (up *Updater) runMSIInstall(msi string) error {
// msiexec.exe requires exclusive access to the log file, so create a dedicated one for each run.
installLogPath := up.startNewLogFile("tailscale-installer", os.Getenv(winVersionEnv))
up.Logf("Install log: %s", installLogPath)
return up.runMSIExec(filepath.Dir(msi), "/i", filepath.Base(msi), "/quiet", "/norestart", "/qn", "/L*v", installLogPath)
}
// runMSIExec runs msiexec.exe with the specified args in dir,
// or in the current directory if dir is empty.
// It returns nil if msiexec succeeded, even if a reboot is required
// to complete the installation or uninstallation.
func (up *Updater) runMSIExec(dir string, args ...string) error {
cmd := exec.Command("msiexec.exe", args...)
cmd.Dir = dir
cmd.Stdout = up.Stdout
cmd.Stderr = up.Stderr
cmd.Stdin = os.Stdin
err := cmd.Run()
if err, ok := err.(*exec.ExitError); ok {
// For possible error codes returned by Windows Installer, see
// https://web.archive.org/web/20250409144914/https://learn.microsoft.com/en-us/windows/win32/msi/error-codes
switch windows.Errno(err.ExitCode()) {
case windows.ERROR_SUCCESS_REBOOT_REQUIRED:
// In most cases, updating Tailscale should not require a reboot.
// If it does, it might be because we failed to close the GUI
// and the installer couldn't replace its executable.
// The old GUI will continue to run until the next reboot.
// Not ideal, but also not an error.
up.Logf("[unexpected] reboot required")
return nil
case windows.ERROR_SUCCESS_REBOOT_INITIATED:
// Same as above, but perhaps the device is configured to prompt
// the user to reboot and the user has chosen to reboot now.
up.Logf("[unexpected] reboot initiated")
return nil
case windows.ERROR_INSTALL_ALREADY_RUNNING:
// The Windows Installer service is currently busy.
// It could be our own install initiated by user/MDM/GP, another MSI install or perhaps a Windows Update install.
// installMSI retries if it has uninstalled the current version.
// Otherwise, the user (or tailscaled) can retry later.
up.Logf("another installation is already in progress")
}
}
return err
}
// uninstallCurrentMSI uninstalls the currently installed version of Tailscale.
func (up *Updater) uninstallCurrentMSI() error {
uninstallVersion := up.currentVersion
if v := os.Getenv("TS_DEBUG_UNINSTALL_VERSION"); v != "" {
uninstallVersion = v
}
uninstallLogPath := up.startNewLogFile("tailscale-uninstaller", uninstallVersion)
up.Logf("Uninstalling current version %q for downgrade...", uninstallVersion)
up.Logf("Uninstall log: %s", uninstallLogPath)
err := up.runMSIExec("", "/x", msiUUIDForVersion(uninstallVersion), "/norestart", "/qn", "/L*v", uninstallLogPath)
up.Logf("msiexec uninstall: %v", err)
return err
}
func msiUUIDForVersion(ver string) string {
arch := runtime.GOARCH
if arch == "386" {
arch = "x86"
}
track, err := versionToTrack(ver)
if err != nil {
track = UnstableTrack
}
msiURL := fmt.Sprintf("https://pkgs.tailscale.com/%s/tailscale-setup-%s-%s.msi", track, ver, arch)
return "{" + strings.ToUpper(uuid.NewSHA1(uuid.NameSpaceURL, []byte(msiURL)).String()) + "}"
}
func (up *Updater) switchOutputToFile() (io.Closer, error) {
var logFilePath string
exePath, err := os.Executable()
if err != nil {
logFilePath = up.startNewLogFile(updaterPrefix, os.Getenv(winVersionEnv))
} else {
// Use the same suffix as the self-copy executable.
suffix := strings.TrimSuffix(strings.TrimPrefix(filepath.Base(exePath), updaterPrefix), ".exe")
logFilePath = up.startNewLogFile(updaterPrefix, os.Getenv(winVersionEnv)+suffix)
}
up.Logf("writing update output to: %s", logFilePath)
logFile, err := os.Create(logFilePath)
if err != nil {
return nil, err
}
up.Logf = func(m string, args ...any) {
fmt.Fprintf(logFile, m+"\n", args...)
}
up.Stdout = logFile
up.Stderr = logFile
return logFile, nil
}
// startNewLogFile returns a name for a new log file.
// It cleans up any old log files with the same baseNamePrefix.
func (up *Updater) startNewLogFile(baseNamePrefix, baseNameSuffix string) string {
baseName := fmt.Sprintf("%s-%s-%s.log", baseNamePrefix,
time.Now().Format("20060102T150405"), baseNameSuffix)
dir := filepath.Join(os.Getenv("ProgramData"), "Tailscale", "Logs")
if err := os.MkdirAll(dir, 0700); err != nil {
up.Logf("failed to create log directory: %v", err)
return filepath.Join(os.TempDir(), baseName)
}
// TODO(nickkhyl): preserve up to N old log files?
up.cleanupOldDownloads(filepath.Join(dir, baseNamePrefix+"-*.log"))
return filepath.Join(dir, baseName)
}