fix: harden guest driver host APIs
- run elevated actions as fixed System32 tools instead of a hidden subcommand - require a SHA-256 for plugin downloads - keep records of secret files that could not be deleted, and never overwrite files the host did not create - stop two concurrent requests from starting the same VM - drop invalid endpoints instead of failing the whole VM list, and reject ssh users and IPv6 zones that could inject command-line text - accept driver screenshots only at the requested path - do not treat an unreadable guest phase as normal Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
58cf86d5eb
commit
44e2174fff
32 files changed
+431
-689
No files matched your search
@@ -45,10 +45,6 @@ func IsCommand(args []string) bool {
|
||||
}
|
||||
|
||||
func run(args []string) (i18n.Locale, error) {
|
||||
// The elevated helper must not read or create anything in the data folder.
|
||||
if len(args) > 0 && args[0] == "__elevate" {
|
||||
return i18n.Detect(), cmdElevate(args[1:])
|
||||
}
|
||||
fs := flag.NewFlagSet("reims-go", flag.ContinueOnError)
|
||||
fs.SetOutput(io.Discard)
|
||||
langFlag := fs.String("lang", "", "UI/CLI language: auto | zh-CN | en-US | <BCP-47> | en-XA")
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/Nwflower/reims-go/internal/host"
|
||||
)
|
||||
|
||||
// elevateExec runs one whitelisted action; tests replace it.
|
||||
var elevateExec = host.ExecuteElevatedAction
|
||||
|
||||
// cmdElevate is the hidden `__elevate` subcommand the desktop app starts through
|
||||
// a UAC prompt. It runs before any data folder is opened, so an elevated run
|
||||
// never creates or changes files in the user's data folder. It is not listed
|
||||
// in the usage text.
|
||||
func cmdElevate(args []string) error {
|
||||
fs := flag.NewFlagSet("__elevate", flag.ContinueOnError)
|
||||
fs.SetOutput(io.Discard)
|
||||
action := fs.String("action", "", "whitelisted elevation action")
|
||||
user := fs.String("user", "", "account the action applies to")
|
||||
resultFile := fs.String("result-file", "", "file the JSON result is written to")
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return writeElevateFailure(*resultFile, *action, err)
|
||||
}
|
||||
if _, ok := host.LookupElevationAction(*action); !ok {
|
||||
return writeElevateFailure(*resultFile, *action, errors.New("unsupported elevation action"))
|
||||
}
|
||||
res, err := elevateExec(context.Background(), *action, *user)
|
||||
if err != nil {
|
||||
if res.Action == "" {
|
||||
res.Action = *action
|
||||
}
|
||||
if res.Error == "" {
|
||||
res.Error = err.Error()
|
||||
}
|
||||
res.OK = false
|
||||
}
|
||||
if werr := writeElevateResult(*resultFile, res); werr != nil && err == nil {
|
||||
return werr
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func writeElevateFailure(path, action string, err error) error {
|
||||
_ = writeElevateResult(path, host.ElevationResult{Action: action, Error: err.Error()})
|
||||
return err
|
||||
}
|
||||
|
||||
// writeElevateResult fills the file in place: the caller created it, and
|
||||
// replacing it would drop the access the caller holds.
|
||||
func writeElevateResult(path string, res host.ElevationResult) error {
|
||||
if path == "" {
|
||||
return nil
|
||||
}
|
||||
data, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(path, data, 0o600)
|
||||
}
|
||||
@@ -1,125 +0,0 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Nwflower/reims-go/internal/host"
|
||||
"github.com/Nwflower/reims-go/internal/i18n"
|
||||
)
|
||||
|
||||
func readElevateResult(t *testing.T, path string) host.ElevationResult {
|
||||
t.Helper()
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("read result: %v", err)
|
||||
}
|
||||
var res host.ElevationResult
|
||||
if err := json.Unmarshal(data, &res); err != nil {
|
||||
t.Fatalf("decode result %q: %v", data, err)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func TestElevateRejectsActionsOutsideTheWhitelist(t *testing.T) {
|
||||
called := false
|
||||
prev := elevateExec
|
||||
elevateExec = func(context.Context, string, string) (host.ElevationResult, error) {
|
||||
called = true
|
||||
return host.ElevationResult{OK: true}, nil
|
||||
}
|
||||
t.Cleanup(func() { elevateExec = prev })
|
||||
|
||||
// Point every data-folder lookup at an empty folder: the helper must not touch it.
|
||||
home := t.TempDir()
|
||||
t.Setenv("APPDATA", home)
|
||||
t.Setenv("LOCALAPPDATA", home)
|
||||
t.Setenv("USERPROFILE", home)
|
||||
t.Setenv("HOME", home)
|
||||
|
||||
resPath := filepath.Join(t.TempDir(), "result.json")
|
||||
for _, action := range []string{"rm-rf", ""} {
|
||||
if _, err := run([]string{"__elevate", "--action", action, "--result-file", resPath}); err == nil {
|
||||
t.Fatalf("action %q was accepted", action)
|
||||
}
|
||||
if res := readElevateResult(t, resPath); res.OK {
|
||||
t.Fatalf("action %q reported success: %+v", action, res)
|
||||
}
|
||||
}
|
||||
if called {
|
||||
t.Fatal("an action outside the whitelist reached the executor")
|
||||
}
|
||||
entries, err := os.ReadDir(home)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 0 {
|
||||
t.Fatalf("the elevated helper wrote into the data folder candidates: %v", entries)
|
||||
}
|
||||
}
|
||||
|
||||
func TestElevateRunsAWhitelistedActionAndWritesTheResult(t *testing.T) {
|
||||
prev := elevateExec
|
||||
var gotAction, gotUser string
|
||||
elevateExec = func(_ context.Context, action, user string) (host.ElevationResult, error) {
|
||||
gotAction, gotUser = action, user
|
||||
return host.ElevationResult{OK: true, Action: action, RebootRequired: true}, nil
|
||||
}
|
||||
t.Cleanup(func() { elevateExec = prev })
|
||||
|
||||
resPath := filepath.Join(t.TempDir(), "result.json")
|
||||
if _, err := run([]string{"__elevate", "--action", "hyperv.enable", "--user", `PC\alice`, "--result-file", resPath}); err != nil {
|
||||
t.Fatalf("run: %v", err)
|
||||
}
|
||||
if gotAction != "hyperv.enable" || gotUser != `PC\alice` {
|
||||
t.Fatalf("executor got %q, %q", gotAction, gotUser)
|
||||
}
|
||||
res := readElevateResult(t, resPath)
|
||||
if !res.OK || res.Action != "hyperv.enable" || !res.RebootRequired {
|
||||
t.Fatalf("result = %+v", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestElevateReportsExecutorFailures(t *testing.T) {
|
||||
prev := elevateExec
|
||||
elevateExec = func(_ context.Context, action, _ string) (host.ElevationResult, error) {
|
||||
return host.ElevationResult{Action: action, ExitCode: 5}, io.ErrUnexpectedEOF
|
||||
}
|
||||
t.Cleanup(func() { elevateExec = prev })
|
||||
|
||||
resPath := filepath.Join(t.TempDir(), "result.json")
|
||||
if _, err := run([]string{"__elevate", "--action", "sandbox.enable", "--result-file", resPath}); err == nil {
|
||||
t.Fatal("a failed action returned no error")
|
||||
}
|
||||
res := readElevateResult(t, resPath)
|
||||
if res.OK || res.Error == "" || res.ExitCode != 5 {
|
||||
t.Fatalf("result = %+v", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestElevateIsHiddenFromUsage(t *testing.T) {
|
||||
if !IsCommand([]string{"__elevate", "--action", "hyperv.enable"}) {
|
||||
t.Fatal("__elevate must run as a command, not open the window")
|
||||
}
|
||||
old := os.Stdout
|
||||
r, w, err := os.Pipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
os.Stdout = w
|
||||
printUsage(i18n.EffectiveLocale(i18n.EnUS))
|
||||
os.Stdout = old
|
||||
_ = w.Close()
|
||||
out, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(string(out), "__elevate") {
|
||||
t.Fatalf("usage mentions the hidden subcommand:\n%s", out)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user