fix(runtime): keep ASLR updates responsive and follow runtime switches
This commit is contained in:
1 parent
b43cd9e16e
commit
f1eeff2ee3
20 files changed
+296
-105
No files matched your search
+19
-32
@@ -102,6 +102,7 @@ type App struct {
|
||||
vmNetworkLocksMu sync.Mutex
|
||||
ctx context.Context
|
||||
mu sync.Mutex
|
||||
configMu sync.Mutex // serializes config writes and data root changes
|
||||
cfg *config.Config
|
||||
transitions map[string]string
|
||||
pendingChanges map[string][]PendingChange
|
||||
@@ -829,9 +830,10 @@ func (a *App) GetState() AppState {
|
||||
}
|
||||
|
||||
func (a *App) SetLocale(raw string) error {
|
||||
if err := a.setPreference(func(cfg *config.Config) { cfg.Locale = i18n.ParsePreference(raw) }); err != nil {
|
||||
return err
|
||||
}
|
||||
a.mu.Lock()
|
||||
a.cfg.Locale = i18n.ParsePreference(raw)
|
||||
err := a.saveConfigLocked()
|
||||
eff := i18n.EffectiveLocale(a.cfg.Locale)
|
||||
mgr := a.pluginMgr
|
||||
a.mu.Unlock()
|
||||
@@ -841,30 +843,19 @@ func (a *App) SetLocale(raw string) error {
|
||||
if a.ctx != nil {
|
||||
wailsRuntime.EventsEmit(a.ctx, "reims:state-changed", true)
|
||||
}
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) SetAutoPinPCores(enabled bool) error {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
a.cfg.AutoPinPCores = enabled
|
||||
return a.saveConfigLocked()
|
||||
return a.setPreference(func(cfg *config.Config) { cfg.AutoPinPCores = enabled })
|
||||
}
|
||||
|
||||
func (a *App) SetMinimizeToTray(enabled bool) error {
|
||||
a.mu.Lock()
|
||||
v := enabled
|
||||
a.cfg.MinimizeToTray = &v
|
||||
err := a.saveConfigLocked()
|
||||
a.mu.Unlock()
|
||||
return err
|
||||
return a.setPreference(func(cfg *config.Config) { cfg.MinimizeToTray = &enabled })
|
||||
}
|
||||
|
||||
func (a *App) SetSyncHostMemorySMBIOS(enabled bool) error {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
a.cfg.SyncHostMemorySMBIOS = enabled
|
||||
return a.saveConfigLocked()
|
||||
return a.setPreference(func(cfg *config.Config) { cfg.SyncHostMemorySMBIOS = enabled })
|
||||
}
|
||||
|
||||
// ShowMainWindow restores and focuses the Wails window from the system tray.
|
||||
@@ -1021,6 +1012,8 @@ func (a *App) resetDataRootCaches() {
|
||||
}
|
||||
|
||||
func (a *App) SetDataRoot(dir string) error {
|
||||
a.configMu.Lock()
|
||||
defer a.configMu.Unlock()
|
||||
a.mu.Lock()
|
||||
loc := i18n.EffectiveLocale(a.cfg.Locale)
|
||||
prev := a.cfg.DataRoot
|
||||
@@ -1174,24 +1167,18 @@ func (a *App) UseRuntime(commit string) error {
|
||||
}
|
||||
|
||||
func (a *App) activateRuntime(dataRoot, commit string) error {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if err := a.requireWritableDataRootLocked(); err != nil {
|
||||
return err
|
||||
}
|
||||
if dataRoot != "" && a.cfg.DataRoot != dataRoot {
|
||||
return errors.New("data folder changed during runtime install")
|
||||
}
|
||||
return rt.Activate(a.baseCtx(), a.cfg, commit)
|
||||
return a.updateConfig(nil, func(cfg *config.Config) error {
|
||||
if dataRoot != "" && cfg.DataRoot != dataRoot {
|
||||
return errors.New("data folder changed during runtime install")
|
||||
}
|
||||
return rt.Activate(a.baseCtx(), cfg, commit)
|
||||
})
|
||||
}
|
||||
|
||||
func (a *App) SetQEMUASLR(enabled bool) error {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if err := a.requireWritableDataRootLocked(); err != nil {
|
||||
return err
|
||||
}
|
||||
return rt.SetQEMUASLR(a.baseCtx(), a.cfg, enabled)
|
||||
return a.updateConfig(nil, func(cfg *config.Config) error {
|
||||
return rt.SetQEMUASLR(a.baseCtx(), cfg, enabled)
|
||||
})
|
||||
}
|
||||
|
||||
func (a *App) RemoveRuntime(commit string) error {
|
||||
|
||||
@@ -66,23 +66,44 @@ func (a *App) requireWritableDataRootLocked() error {
|
||||
return dataroot.Error(i18n.EffectiveLocale(a.cfg.Locale), a.root)
|
||||
}
|
||||
|
||||
// saveConfigLocked persists a.cfg; a.mu must be held. Before first-run setup
|
||||
// a.cfg points at a suggested folder the user has not chosen, so changes stay
|
||||
// in memory until CompleteSetup writes them into the chosen folder. A folder
|
||||
// open read-only refuses the write.
|
||||
func (a *App) saveConfigLocked() error {
|
||||
if a.needsSetup {
|
||||
return nil
|
||||
}
|
||||
if err := dataroot.Error(i18n.EffectiveLocale(a.cfg.Locale), a.root); err != nil {
|
||||
func (a *App) setPreference(edit func(*config.Config)) error {
|
||||
return a.updateConfig(edit, rt.SavePreferences)
|
||||
}
|
||||
|
||||
// Settings can be edited in memory before setup. Runtime actions (no edit)
|
||||
// need a writable data root. Disk locks and UAC never run under a.mu.
|
||||
func (a *App) updateConfig(edit func(*config.Config), persist func(*config.Config) error) error {
|
||||
a.configMu.Lock()
|
||||
defer a.configMu.Unlock()
|
||||
a.mu.Lock()
|
||||
cfg := *a.cfg
|
||||
setup := a.needsSetup
|
||||
err := a.requireWritableDataRootLocked()
|
||||
a.mu.Unlock()
|
||||
if err != nil && !(setup && edit != nil) {
|
||||
return err
|
||||
}
|
||||
return rt.SavePreferences(a.cfg)
|
||||
if edit != nil {
|
||||
edit(&cfg)
|
||||
}
|
||||
if !setup {
|
||||
if err := persist(&cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
a.mu.Lock()
|
||||
a.cfg = &cfg
|
||||
a.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// runtimeConfig includes the last selection made by either the GUI or CLI.
|
||||
func (a *App) runtimeConfig() config.Config {
|
||||
a.mu.Lock()
|
||||
if a.cfg == nil {
|
||||
a.mu.Unlock()
|
||||
return config.Config{}
|
||||
}
|
||||
cfg := *a.cfg
|
||||
usable := !a.needsSetup && !a.root.ReadOnly
|
||||
a.mu.Unlock()
|
||||
@@ -99,6 +120,8 @@ func (a *App) runtimeConfig() config.Config {
|
||||
// a damaged version record on the user's request. When the folder becomes
|
||||
// writable, the parts that were held back start.
|
||||
func (a *App) ReopenDataRoot(repair bool) (dataroot.Status, error) {
|
||||
a.configMu.Lock()
|
||||
defer a.configMu.Unlock()
|
||||
a.mu.Lock()
|
||||
if a.needsSetup {
|
||||
st := a.root
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/Nwflower/reims-go/internal/config"
|
||||
"github.com/Nwflower/reims-go/internal/guesttools"
|
||||
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
)
|
||||
@@ -23,16 +24,15 @@ func (a *App) SetHostDNSConfig(cfg guesttools.HostDNSConfig) error {
|
||||
if err := cfg.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
prev := a.hostDNSConfigLocked()
|
||||
a.cfg.HostDNSMode, a.cfg.HostDNSDoHURL = cfg.Mode, cfg.DoHURL
|
||||
if err := a.saveConfigLocked(); err != nil {
|
||||
a.cfg.HostDNSMode, a.cfg.HostDNSDoHURL = prev.Mode, prev.DoHURL
|
||||
if err := a.setPreference(func(c *config.Config) {
|
||||
c.HostDNSMode, c.HostDNSDoHURL = cfg.Mode, cfg.DoHURL
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if a.guestTools != nil {
|
||||
a.guestTools.SetHostDNSConfig(cfg)
|
||||
a.guestTools.SetHostDNSConfig(a.hostDNSConfigLocked())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/Nwflower/reims-go/internal/config"
|
||||
"github.com/Nwflower/reims-go/internal/dataroot"
|
||||
"github.com/Nwflower/reims-go/internal/host"
|
||||
"github.com/Nwflower/reims-go/internal/i18n"
|
||||
@@ -21,19 +22,13 @@ var (
|
||||
// Hyper-V / WHPX boot switch in Settings. Toggling this flag alone never runs
|
||||
// any system virtualization or boot configuration command.
|
||||
func (a *App) SetEnableHypervisorSwitch(enabled bool) error {
|
||||
a.mu.Lock()
|
||||
prev := a.cfg.EnableHypervisorSwitch
|
||||
a.cfg.EnableHypervisorSwitch = enabled
|
||||
err := a.saveConfigLocked()
|
||||
if err != nil {
|
||||
a.cfg.EnableHypervisorSwitch = prev
|
||||
if err := a.setPreference(func(cfg *config.Config) { cfg.EnableHypervisorSwitch = enabled }); err != nil {
|
||||
return err
|
||||
}
|
||||
ctx := a.ctx
|
||||
a.mu.Unlock()
|
||||
if err == nil && ctx != nil {
|
||||
wailsRuntime.EventsEmit(ctx, "reims:state-changed", true)
|
||||
if a.ctx != nil {
|
||||
wailsRuntime.EventsEmit(a.ctx, "reims:state-changed", true)
|
||||
}
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
// EnableWHPX enables Windows Hypervisor Platform (if missing) and sets Windows
|
||||
|
||||
@@ -5,15 +5,60 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Nwflower/reims-go/internal/config"
|
||||
"github.com/Nwflower/reims-go/internal/driver"
|
||||
vmlifecycle "github.com/Nwflower/reims-go/internal/lifecycle"
|
||||
"github.com/Nwflower/reims-go/internal/macos/macosdriver"
|
||||
"github.com/Nwflower/reims-go/internal/plugin"
|
||||
rt "github.com/Nwflower/reims-go/internal/runtime"
|
||||
"github.com/Nwflower/reims-go/internal/tray"
|
||||
)
|
||||
|
||||
func TestMacOSLaunchUsesLatestRuntimeSelection(t *testing.T) {
|
||||
cfg := config.Config{DataRoot: t.TempDir(), ActiveRuntime: "current", QEMUASLR: true}
|
||||
for _, version := range []string{"current", "backup"} {
|
||||
dir := folderTestDir(t, cfg.RuntimesDir(), version)
|
||||
if err := os.WriteFile(filepath.Join(dir, "manifest.json"), []byte("{}"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
a := &App{cfg: &cfg}
|
||||
d := macosdriver.NewReimsMacOSDriver(a.runtimeConfig, macosdriver.Options{OwnerID: "test.macos"})
|
||||
ref := driver.NewVMRef(d.ID(), "test")
|
||||
for _, version := range []string{"current", "backup", "current"} {
|
||||
saved := cfg
|
||||
saved.ActiveRuntime = version
|
||||
if err := config.Save("", &saved); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := rt.Installed{Dir: filepath.Join(cfg.RuntimesDir(), version)}
|
||||
prep, err := a.prep(ref.Name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c, err := a.client()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c.Binary != want.ControlPath() || prep.Client.Binary != c.Binary || a.activeQemuPath() != want.QemuPath() {
|
||||
t.Fatalf("%s: native clients or bridge selected the wrong runtime", version)
|
||||
}
|
||||
// Only manifests are installed. The attempted control executable
|
||||
// identifies which runtime the same driver used for each new request.
|
||||
_, startErr := d.Start(t.Context(), driver.StartRequest{Ref: ref})
|
||||
_, resumeErr := d.Resume(t.Context(), driver.ResumeRequest{Ref: ref})
|
||||
for _, err := range []error{startErr, resumeErr} {
|
||||
if err == nil || !strings.Contains(err.Error(), want.ControlPath()) {
|
||||
t.Fatalf("expected a launch attempt with %q, got %v", want.ControlPath(), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type appSecondDriver struct {
|
||||
id string
|
||||
vms []driver.VMInfo
|
||||
|
||||
@@ -43,14 +43,12 @@ func (a *App) client() (*reims.Client, error) {
|
||||
if err := a.requireMacOSEnabled(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a.mu.Lock()
|
||||
cfg := a.cfg
|
||||
a.mu.Unlock()
|
||||
active, err := rt.ResolveActive(cfg)
|
||||
cfg := a.runtimeConfig()
|
||||
active, err := rt.ResolveActive(&cfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s", i18n.T(cfg.Locale, "err.runtime_not_installed"))
|
||||
}
|
||||
return reims.NewClient(cfg, *active), nil
|
||||
return reims.NewClient(&cfg, *active), nil
|
||||
}
|
||||
|
||||
func enrichMacOSVMListStates(vms []driver.VMListItem) {
|
||||
@@ -96,14 +94,11 @@ func (a *App) tempDir() string {
|
||||
}
|
||||
|
||||
func (a *App) activeQemuPath() string {
|
||||
a.mu.Lock()
|
||||
if a.cfg == nil {
|
||||
a.mu.Unlock()
|
||||
cfg := a.runtimeConfig()
|
||||
if cfg.DataRoot == "" {
|
||||
return ""
|
||||
}
|
||||
cfgCopy := *a.cfg
|
||||
a.mu.Unlock()
|
||||
active, err := rt.ResolveActive(&cfgCopy)
|
||||
active, err := rt.ResolveActive(&cfg)
|
||||
if err != nil || active == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -34,10 +34,7 @@ func (a *App) prep(name string) (*startprep.Prep, error) {
|
||||
if err := a.requireMacOSEnabled(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a.mu.Lock()
|
||||
cfg := *a.cfg
|
||||
a.mu.Unlock()
|
||||
return startprep.New(cfg, func(line string) { a.emitVMLog(name, line) })
|
||||
return startprep.New(a.runtimeConfig(), func(line string) { a.emitVMLog(name, line) })
|
||||
}
|
||||
|
||||
// GetVMSMBIOS reads the VM's OpenCore PlatformInfo > Generic identity
|
||||
|
||||
@@ -77,11 +77,11 @@ func (a *App) SetRuntimeChannel(channel string) error {
|
||||
default:
|
||||
return fmt.Errorf("unknown channel %q", channel)
|
||||
}
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
a.cfg.RuntimeChannel = channel
|
||||
if err := a.setPreference(func(cfg *config.Config) { cfg.RuntimeChannel = channel }); err != nil {
|
||||
return err
|
||||
}
|
||||
forgetRemoteRuntimes()
|
||||
return a.saveConfigLocked()
|
||||
return nil
|
||||
}
|
||||
|
||||
// InstallRuntimeOnline downloads, verifies and installs a published runtime
|
||||
@@ -198,11 +198,7 @@ func (a *App) SetAppUpdateChannel(channel string) error {
|
||||
default:
|
||||
return fmt.Errorf("unknown app update channel %q", channel)
|
||||
}
|
||||
a.mu.Lock()
|
||||
a.cfg.AppUpdateChannel = channel
|
||||
err := a.saveConfigLocked()
|
||||
a.mu.Unlock()
|
||||
if err != nil {
|
||||
if err := a.setPreference(func(cfg *config.Config) { cfg.AppUpdateChannel = channel }); err != nil {
|
||||
return err
|
||||
}
|
||||
appUpdateCache.Lock()
|
||||
|
||||
@@ -4,8 +4,10 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
|
||||
"github.com/Nwflower/reims-go/internal/config"
|
||||
"github.com/Nwflower/reims-go/internal/filelock"
|
||||
)
|
||||
|
||||
func TestRuntimeConfigFollowsExternalSelection(t *testing.T) {
|
||||
@@ -46,3 +48,80 @@ func TestPreferencesDoNotOverwriteDamagedConfig(t *testing.T) {
|
||||
t.Fatalf("damaged config replaced: %q, %v", data, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigWaitDoesNotBlockApp(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
save func(*App) error
|
||||
}{
|
||||
{"ASLR", func(a *App) error { return a.SetQEMUASLR(true) }},
|
||||
{"runtime", func(a *App) error { return a.UseRuntime("backup") }},
|
||||
{"preference", func(a *App) error { return a.SetSyncHostMemorySMBIOS(true) }},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
cfg := &config.Config{DataRoot: t.TempDir()}
|
||||
if err := cfg.EnsureDirs(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// All saves will stop before a registry write after the lock releases.
|
||||
if err := os.WriteFile(filepath.Join(cfg.DataRoot, "reims-go.json"), []byte("{"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
release, err := filelock.Acquire(t.Context(), filepath.Join(cfg.RuntimesDir(), ".reims-go-runtime.lock"), 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
a := &App{cfg: cfg}
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- tc.save(a) }()
|
||||
synctest.Wait()
|
||||
available := a.mu.TryLock()
|
||||
if available {
|
||||
a.mu.Unlock()
|
||||
a.emitLog("test", "while waiting")
|
||||
}
|
||||
release()
|
||||
if err := <-done; err == nil || !available {
|
||||
t.Fatalf("global lock available = %v, save error = %v", available, err)
|
||||
}
|
||||
if a.cfg.QEMUASLR || a.cfg.ActiveRuntime != "" || a.cfg.SyncHostMemorySMBIOS {
|
||||
t.Fatalf("failed save published %+v", a.cfg)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestConcurrentSelectionAndPreferenceSurvive(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
a := &App{cfg: &config.Config{DataRoot: dir}}
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
resume := make(chan struct{})
|
||||
results := make(chan error, 2)
|
||||
go func() {
|
||||
results <- a.updateConfig(nil, func(cfg *config.Config) error {
|
||||
<-resume
|
||||
cfg.ActiveRuntime, cfg.QEMUASLR = "backup", true
|
||||
return config.Save("", cfg)
|
||||
})
|
||||
}()
|
||||
synctest.Wait()
|
||||
started := make(chan struct{})
|
||||
go func() {
|
||||
close(started)
|
||||
results <- a.SetSyncHostMemorySMBIOS(true)
|
||||
}()
|
||||
<-started
|
||||
close(resume)
|
||||
for range 2 {
|
||||
if err := <-results; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
saved, err := config.LoadExisting(filepath.Join(dir, "reims-go.json"))
|
||||
if err != nil || saved.ActiveRuntime != "backup" || !saved.QEMUASLR || !saved.SyncHostMemorySMBIOS {
|
||||
t.Fatalf("concurrent updates lost: %+v, %v", saved, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -103,6 +103,8 @@ func (a *App) CheckDataRoot(dir string) DataRootCheck {
|
||||
// existing reims-go config there (its runtimes and VMs), records the choice for
|
||||
// the next launch and leaves first-launch setup.
|
||||
func (a *App) CompleteSetup(dir string) error {
|
||||
a.configMu.Lock()
|
||||
defer a.configMu.Unlock()
|
||||
a.mu.Lock()
|
||||
locale := a.cfg.Locale
|
||||
a.mu.Unlock()
|
||||
|
||||
@@ -88,7 +88,6 @@ func (a *App) ResumeVMWithDriver(driverID, name string) error {
|
||||
}
|
||||
a.mu.Lock()
|
||||
svc := a.lifecycleSvc
|
||||
cfgCopy := *a.cfg
|
||||
a.mu.Unlock()
|
||||
if svc == nil {
|
||||
return errors.New("driver lifecycle is not initialized")
|
||||
@@ -113,7 +112,8 @@ func (a *App) ResumeVMWithDriver(driverID, name string) error {
|
||||
if ref.DriverID == driver.BuiltinReimsMacOSDriverID {
|
||||
a.cancelRebootWatcherKey(key)
|
||||
epoch = a.vmDeleteEpoch(ref.Name)
|
||||
active, rerr := rt.ResolveActive(&cfgCopy)
|
||||
cfg := a.runtimeConfig()
|
||||
active, rerr := rt.ResolveActive(&cfg)
|
||||
hasRuntime := rerr == nil && active != nil
|
||||
unicodeInput := hasRuntime && active.Manifest.HasCapability("win32-unicode-input")
|
||||
windowDrop := hasRuntime && active.Manifest.HasCapability("window-file-drop")
|
||||
|
||||
@@ -11,9 +11,4 @@ func ApplyQEMUASLR(_, _ string) error {
|
||||
return errors.New("QEMU ASLR requires Windows")
|
||||
}
|
||||
|
||||
func SyncQEMUASLR(_ context.Context, runtimesDir, qemuPath string) error {
|
||||
if qemuPath == "" {
|
||||
return nil
|
||||
}
|
||||
return ApplyQEMUASLR(runtimesDir, qemuPath)
|
||||
}
|
||||
func SyncQEMUASLR(ctx context.Context, _, _ string) error { return ctx.Err() }
|
||||
@@ -31,7 +31,7 @@ func SyncQEMUASLR(ctx context.Context, runtimesDir, qemuPath string) error {
|
||||
return err
|
||||
}
|
||||
err := ApplyQEMUASLR(runtimesDir, qemuPath)
|
||||
if !errors.Is(err, windows.ERROR_ACCESS_DENIED) {
|
||||
if !errors.Is(err, windows.ERROR_ACCESS_DENIED) || windows.GetCurrentProcessToken().IsElevated() {
|
||||
return err
|
||||
}
|
||||
exe, err := os.Executable()
|
||||
@@ -66,7 +66,7 @@ func ApplyQEMUASLR(runtimesDir, qemuPath string) error {
|
||||
return errors.New("timed out waiting for the QEMU policy update")
|
||||
}
|
||||
defer windows.ReleaseMutex(mutex)
|
||||
return reconcileQEMUASLR(registry.LOCAL_MACHINE, qemuIFEO, runtimesDir, qemuPath)
|
||||
return reconcileQEMUASLR(registry.LOCAL_MACHINE, qemuIFEO, runtimesDir, qemuPath, windows.GetCurrentProcessToken().IsElevated())
|
||||
}
|
||||
|
||||
func runtimeQEMUPath(root, path string) bool {
|
||||
@@ -79,7 +79,9 @@ func runtimeQEMUPath(root, path string) bool {
|
||||
strings.EqualFold(filepath.Join(parts[len(parts)-3:]...), filepath.Join("qemu", "bin", qemuImage))
|
||||
}
|
||||
|
||||
func reconcileQEMUASLR(hive registry.Key, imagePath, root, target string) error {
|
||||
// Without elevation the same checks run read-only, even if a custom ACL would
|
||||
// allow writing some of the keys. Declining UAC must leave every key untouched.
|
||||
func reconcileQEMUASLR(hive registry.Key, imagePath, root, target string, canWrite bool) error {
|
||||
if !filepath.IsAbs(root) || !strings.EqualFold(filepath.Base(root), "runtimes") ||
|
||||
(target != "" && (!filepath.IsAbs(target) || filepath.Clean(target) != target || !runtimeQEMUPath(root, target))) {
|
||||
return fmt.Errorf("invalid QEMU ASLR runtime path %q in %q", target, root)
|
||||
@@ -90,6 +92,9 @@ func reconcileQEMUASLR(hive registry.Key, imagePath, root, target string) error
|
||||
if target == "" {
|
||||
return nil
|
||||
}
|
||||
if !canWrite {
|
||||
return windows.ERROR_ACCESS_DENIED
|
||||
}
|
||||
var existed bool
|
||||
image, existed, err = registry.CreateKey(hive, imagePath, registry.READ|registry.WRITE)
|
||||
createdImage = !existed
|
||||
@@ -135,28 +140,28 @@ func reconcileQEMUASLR(hive registry.Key, imagePath, root, target string) error
|
||||
}
|
||||
}
|
||||
if target != "" {
|
||||
if err := enableASLRPathFilters(image, createdImage, active == "", len(names)); err != nil {
|
||||
if err := enableASLRPathFilters(image, createdImage, active == "", len(names), canWrite); err != nil {
|
||||
return err
|
||||
}
|
||||
if active == "" {
|
||||
active = fmt.Sprintf("Reims-Go-%x", sha256.Sum256([]byte(strings.ToLower(target))))
|
||||
}
|
||||
if err := updateASLRFilter(image, active, target); err != nil {
|
||||
if err := updateASLRFilter(image, active, target, canWrite); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, name := range stale {
|
||||
if err := updateASLRFilter(image, name, ""); err != nil {
|
||||
if err := updateASLRFilter(image, name, "", canWrite); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if target != "" || otherOwner {
|
||||
return nil
|
||||
}
|
||||
return restoreASLRFilterMode(hive, imagePath, image)
|
||||
return restoreASLRFilterMode(hive, imagePath, image, canWrite)
|
||||
}
|
||||
|
||||
func enableASLRPathFilters(image registry.Key, createdImage, newPath bool, filters int) error {
|
||||
func enableASLRPathFilters(image registry.Key, createdImage, newPath bool, filters int, canWrite bool) error {
|
||||
if newPath {
|
||||
values, err := image.ReadValueNames(-1)
|
||||
if err != nil {
|
||||
@@ -178,6 +183,9 @@ func enableASLRPathFilters(image registry.Key, createdImage, newPath bool, filte
|
||||
if filters != 0 || useFilter != 0 {
|
||||
return errors.New("QEMU has incompatible Windows path filter settings")
|
||||
}
|
||||
if !canWrite {
|
||||
return windows.ERROR_ACCESS_DENIED
|
||||
}
|
||||
// 1 = missing UseFilter, 2 = existing zero, 3 = newly created image.
|
||||
mode := uint32(1)
|
||||
if createdImage {
|
||||
@@ -196,7 +204,7 @@ func enableASLRPathFilters(image registry.Key, createdImage, newPath bool, filte
|
||||
return w.SetDWordValue("UseFilter", 1)
|
||||
}
|
||||
|
||||
func restoreASLRFilterMode(hive registry.Key, imagePath string, image registry.Key) error {
|
||||
func restoreASLRFilterMode(hive registry.Key, imagePath string, image registry.Key, canWrite bool) error {
|
||||
mode, _, err := image.GetIntegerValue(filterMode)
|
||||
if errors.Is(err, registry.ErrNotExist) {
|
||||
return nil
|
||||
@@ -204,6 +212,9 @@ func restoreASLRFilterMode(hive registry.Key, imagePath string, image registry.K
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !canWrite {
|
||||
return windows.ERROR_ACCESS_DENIED
|
||||
}
|
||||
info, err := image.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -233,10 +244,13 @@ func restoreASLRFilterMode(hive registry.Key, imagePath string, image registry.K
|
||||
return w.DeleteValue(filterMode)
|
||||
}
|
||||
|
||||
func updateASLRFilter(image registry.Key, name, target string) error {
|
||||
func updateASLRFilter(image registry.Key, name, target string, canWrite bool) error {
|
||||
k, err := registry.OpenKey(image, name, registry.READ)
|
||||
create := errors.Is(err, registry.ErrNotExist)
|
||||
if create && target != "" {
|
||||
if !canWrite {
|
||||
return windows.ERROR_ACCESS_DENIED
|
||||
}
|
||||
var existed bool
|
||||
k, existed, err = registry.CreateKey(image, name, registry.READ|registry.WRITE)
|
||||
create = !existed
|
||||
@@ -275,6 +289,9 @@ func updateASLRFilter(image registry.Key, name, target string) error {
|
||||
if target != "" && options[2]&0x33 == 0x11 {
|
||||
return nil
|
||||
}
|
||||
if !canWrite {
|
||||
return windows.ERROR_ACCESS_DENIED
|
||||
}
|
||||
w, err := registry.OpenKey(image, name, registry.READ|registry.WRITE)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
"golang.org/x/sys/windows/registry"
|
||||
)
|
||||
|
||||
@@ -37,7 +38,7 @@ func TestQEMUASLRFollowsRuntimeAndRestoresOnlyOwnedRules(t *testing.T) {
|
||||
backup := filepath.Join(root, "backup", "qemu", "bin", qemuImage)
|
||||
update := func(target string) {
|
||||
t.Helper()
|
||||
if err := reconcileQEMUASLR(hive, imagePath, root, target); err != nil {
|
||||
if err := reconcileQEMUASLR(hive, imagePath, root, target, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -67,6 +68,15 @@ func TestQEMUASLRFollowsRuntimeAndRestoresOnlyOwnedRules(t *testing.T) {
|
||||
}
|
||||
readByte(active, 0x11)
|
||||
active.Close()
|
||||
probe := func(target string, want error) {
|
||||
t.Helper()
|
||||
if err := reconcileQEMUASLR(hive, imagePath, root, target, false); !errors.Is(err, want) {
|
||||
t.Fatalf("read-only check for %q: got %v, want %v", target, err, want)
|
||||
}
|
||||
}
|
||||
probe(current, nil)
|
||||
probe("", windows.ERROR_ACCESS_DENIED)
|
||||
countFilters(1)
|
||||
|
||||
// One manual OFF override and unrelated bits on the backup runtime.
|
||||
manual, _, err := registry.CreateKey(image, "User filter", registry.ALL_ACCESS)
|
||||
@@ -82,6 +92,9 @@ func TestQEMUASLRFollowsRuntimeAndRestoresOnlyOwnedRules(t *testing.T) {
|
||||
if err := manual.SetBinaryValue("MitigationOptions", b); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
probe(backup, windows.ERROR_ACCESS_DENIED)
|
||||
countFilters(2)
|
||||
readByte(manual, 0x82)
|
||||
update(backup)
|
||||
countFilters(1)
|
||||
readByte(manual, 0x91)
|
||||
@@ -110,7 +123,7 @@ func TestQEMUASLRRestoresImageKey(t *testing.T) {
|
||||
root := filepath.Join(t.TempDir(), "runtimes")
|
||||
target := filepath.Join(root, "current", "qemu", "bin", qemuImage)
|
||||
hive := registry.CURRENT_USER
|
||||
update := func(target string) error { return reconcileQEMUASLR(hive, imagePath, root, target) }
|
||||
update := func(target string) error { return reconcileQEMUASLR(hive, imagePath, root, target, true) }
|
||||
t.Cleanup(func() {
|
||||
if err := update(""); err != nil {
|
||||
t.Error(err)
|
||||
@@ -119,6 +132,13 @@ func TestQEMUASLRRestoresImageKey(t *testing.T) {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
if err := reconcileQEMUASLR(hive, imagePath, root, target, false); !errors.Is(err, windows.ERROR_ACCESS_DENIED) {
|
||||
t.Fatalf("missing image must require elevation: %v", err)
|
||||
}
|
||||
if k, err := registry.OpenKey(hive, imagePath, registry.READ); !errors.Is(err, registry.ErrNotExist) {
|
||||
k.Close()
|
||||
t.Fatalf("read-only check created the image key: %v", err)
|
||||
}
|
||||
if err := update(target); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -177,6 +177,7 @@ var catalog = map[Locale]map[string]string{
|
||||
"err.plugin.in_use": "插件「{id}」正在被虚拟机使用,无法停用或卸载:{reason}",
|
||||
"err.plugin.safe_mode": "当前处于安全模式,已暂停加载和变更第三方插件",
|
||||
"err.plugin.system_locked": "系统核心组件「{id}」不可停用或卸载",
|
||||
"err.qemu_aslr.cancelled": "已取消 Windows 管理员授权,QEMU ASLR 设置保持原样",
|
||||
"err.quit.stop_failed": "无法关闭虚拟机「{name}」,已取消退出。",
|
||||
"err.quit.timeout": "虚拟机「{name}」关机超时,已取消退出。",
|
||||
"err.runtime_not_installed": "未找到已激活的便携运行时,请先执行 `reims-go runtime install <归档路径>`",
|
||||
@@ -866,6 +867,7 @@ var catalog = map[Locale]map[string]string{
|
||||
"err.plugin.in_use": "Plugin “{id}” is currently in use and cannot be disabled or removed: {reason}",
|
||||
"err.plugin.safe_mode": "Safe mode is active; third-party plugin changes are paused",
|
||||
"err.plugin.system_locked": "Core system component “{id}” cannot be disabled or removed",
|
||||
"err.qemu_aslr.cancelled": "Windows authorization was canceled before the QEMU ASLR changes were applied",
|
||||
"err.quit.stop_failed": "Could not shut down “{name}”. Quit canceled.",
|
||||
"err.quit.timeout": "Shutdown timed out for “{name}”. Quit canceled.",
|
||||
"err.runtime_not_installed": "no active portable runtime found; run `reims-go runtime install <archive>` first",
|
||||
|
||||
@@ -3,6 +3,7 @@ package runtime
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"runtime"
|
||||
|
||||
"github.com/Nwflower/reims-go/internal/config"
|
||||
"github.com/Nwflower/reims-go/internal/host"
|
||||
@@ -11,6 +12,9 @@ import (
|
||||
// CheckLaunchASLR repairs the selected runtime's rule after a data folder
|
||||
// move and avoids reactivating a version superseded during launch prep.
|
||||
func CheckLaunchASLR(ctx context.Context, cfg config.Config, qemuPath string) error {
|
||||
if runtime.GOOS != "windows" {
|
||||
return ctx.Err()
|
||||
}
|
||||
if err := RefreshSelection(&cfg); err != nil || !cfg.QEMUASLR {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/Nwflower/reims-go/internal/config"
|
||||
"github.com/Nwflower/reims-go/internal/host"
|
||||
"github.com/Nwflower/reims-go/internal/i18n"
|
||||
)
|
||||
|
||||
func Activate(ctx context.Context, cfg *config.Config, commit string) error {
|
||||
@@ -100,7 +101,10 @@ func saveSelection(ctx context.Context, cfg *config.Config, commit string, aslr
|
||||
return pathErr
|
||||
}
|
||||
err = syncPolicy(ctx, root, target)
|
||||
if errors.Is(err, host.ErrElevationDeclined) || errors.Is(err, context.Canceled) {
|
||||
if errors.Is(err, host.ErrElevationDeclined) {
|
||||
return i18n.NewCodedError(i18n.EffectiveLocale(cfg.Locale), "E_QEMU_ASLR_CANCELLED", "err.qemu_aslr.cancelled", nil, err)
|
||||
}
|
||||
if errors.Is(err, context.Canceled) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
@@ -87,6 +88,9 @@ func TestSelectionFailureKeepsPreviousRuntime(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLaunchPolicyRejectsSupersededRuntime(t *testing.T) {
|
||||
if runtime.GOOS != "windows" {
|
||||
t.Skip("Windows launch policy")
|
||||
}
|
||||
cfg := config.Config{DataRoot: t.TempDir(), ActiveRuntime: "backup", QEMUASLR: true}
|
||||
current := Installed{Dir: filepath.Join(cfg.RuntimesDir(), "current")}
|
||||
writeFakeRuntime(t, current.Dir, Manifest{Commit: "current"})
|
||||
@@ -100,3 +104,27 @@ func TestLaunchPolicyRejectsSupersededRuntime(t *testing.T) {
|
||||
t.Fatal("launch would reapply policy to the superseded runtime")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNonWindowsIgnoresSavedQEMUASLR(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("non-Windows host")
|
||||
}
|
||||
cfg := config.Config{DataRoot: t.TempDir(), ActiveRuntime: "current", QEMUASLR: true}
|
||||
for _, commit := range []string{"current", "backup"} {
|
||||
writeFakeRuntime(t, filepath.Join(cfg.RuntimesDir(), commit), Manifest{Commit: commit})
|
||||
}
|
||||
if err := config.Save("", &cfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := Activate(t.Context(), &cfg, "backup"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
current := Installed{Dir: filepath.Join(cfg.RuntimesDir(), "current")}
|
||||
if err := CheckLaunchASLR(t.Context(), cfg, current.QemuPath()); err != nil {
|
||||
t.Fatalf("Windows policy blocked launch on %s: %v", runtime.GOOS, err)
|
||||
}
|
||||
saved, err := config.LoadExisting(filepath.Join(cfg.DataRoot, "reims-go.json"))
|
||||
if err != nil || !saved.QEMUASLR || saved.ActiveRuntime != "backup" {
|
||||
t.Fatalf("lost runtime selection or portable preference: %+v, %v", saved, err)
|
||||
}
|
||||
}
|
||||
@@ -298,6 +298,7 @@
|
||||
]
|
||||
},
|
||||
"err.runtime_not_installed": "no active portable runtime found; run `reims-go runtime install <archive>` first",
|
||||
"err.qemu_aslr.cancelled": "Windows authorization was canceled before the QEMU ASLR changes were applied",
|
||||
"err.macos.disabled": "macOS VM Support is disabled; enable it on the Plugins page first",
|
||||
"err.vm_not_running": "VM {name} is not running",
|
||||
"err.computer_use.disabled": "Virtual machine interaction is disabled. Enable it on the Plugins page and try again",
|
||||
|
||||
@@ -298,6 +298,7 @@
|
||||
]
|
||||
},
|
||||
"err.runtime_not_installed": "未找到已激活的便携运行时,请先执行 `reims-go runtime install <归档路径>`",
|
||||
"err.qemu_aslr.cancelled": "已取消 Windows 管理员授权,QEMU ASLR 设置保持原样",
|
||||
"err.macos.disabled": "「macOS 虚拟机支持」插件已停用,请先在插件页启用",
|
||||
"err.vm_not_running": "虚拟机 {name} 未运行",
|
||||
"err.computer_use.disabled": "虚拟机交互服务已停用,请在插件页启用后重试",
|
||||
|
||||
Reference in new issue
Block a user