GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(update): keep Windows failed-restore state in trusted recovery re… · Twigpine/zero@a3852ba

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • docs
    • UPDATE.md
  • internal/update
    • replace_windows.go
    • stage_promote_windows_test.go
    • stage_windows.go
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎docs/UPDATE.md‎

Copy file name to clipboardExpand all lines: docs/UPDATE.md
+13-3Lines changed: 13 additions & 3 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,24 @@ binary:
9393

9494
| State on disk | Meaning |
9595
|---|---|
96-
| `<binary>.old` (or `<binary>.<suffix>.old`) plus a `.keep` marker | A previous update could not restore the original binary. The `.old` file may be the last binary the updater verified; the installed one may be unverified. |
96+
| `<binary>.old` (or `<binary>.<suffix>.old`) plus a `.keep` marker, or a per-user update-recovery record naming that copy | A previous update could not restore the original binary. The `.old` file may be the last binary the updater verified; the installed one may be unverified. The `.keep` marker is in the installation directory and can be deleted; the identity-bound record is not, so deleting the marker alone does not end the refusal. |
9797
| `<binary>.…old.<suffix>.recovery` | A previous update could not even write the marker, so it moved the last verified binary to that name. |
9898
| The binary is missing and one or more `*.old` files exist | The previous attempt was interrupted between moves. |
9999

100100
The refusal names the paths involved and the two moves that end the state:
101101
either move the recovery binary back over the executable path, or — if the
102-
installed binary is the one you want — delete the `.keep` marker (or the
103-
`.recovery` copy, once you have verified the installed binary) and update again.
102+
installed binary is the one you want — delete the recovery copy itself (not
103+
only the `.keep` marker) after verifying the installed binary, then update
104+
again. Deleting the marker alone is not enough: a failed restore also records
105+
that copy in per-user state outside the installation directory, and the next
106+
update consults that record even if the marker is gone.
107+
108+
That trusted record lives under `%AppData%\zero\update-recovery` (per-user).
109+
The recovery copy and its `.keep` marker sit in the installation directory
110+
(per-machine). A second Windows account on the same machine does not see the
111+
first account's unresolved record; the sibling marker is the machine-visible
112+
signal for every account. There is no separate machine-wide store: writing one
113+
would need a shared writable location and ACL rules the updater does not own.
104114

105115
This is deliberately fail-closed and differs from older releases, which deleted
106116
`<binary>.old` and proceeded. The trade-off is that anyone who can write in the
Collapse file

‎internal/update/replace_windows.go‎

Copy file name to clipboardExpand all lines: internal/update/replace_windows.go
+127-14Lines changed: 127 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,24 @@ type recoveryCleanupRecord struct {
278278
VolumeSerial uint32 `json:"volumeSerial"`
279279
FileIndexHigh uint32 `json:"fileIndexHigh"`
280280
FileIndexLow uint32 `json:"fileIndexLow"`
281+
// Unresolved marks a failed restore. Preflight consults these records
282+
// alongside the sibling .keep marker so deleting that marker cannot
283+
// silence the next run's tamper refusal (#868). Cleanup records from a
284+
// successful promotion leave this false.
285+
Unresolved bool `json:"unresolved,omitempty"`
281286
}
282287

283288
type recoveryCleanupQueue struct {
284289
Records []recoveryCleanupRecord `json:"records"`
285290
}
286291

292+
// recoveryCleanupStateDir is per-user (%AppData% via UserConfigDir). The
293+
// recovery copy and its .keep marker live in the installation directory, which
294+
// is per-machine. A second Windows account therefore does not see the first
295+
// account's unresolved record; the sibling marker is the machine-visible
296+
// signal. A machine-wide store would need a writable shared location and ACL
297+
// rules this updater does not own, so the record stays in the existing
298+
// per-user update-recovery directory.
287299
var recoveryCleanupStateDir = func() (string, error) {
288300
root, err := config.UserConfigDir()
289301
if err != nil {
@@ -335,27 +347,36 @@ type recoveryCleanupCandidate struct {
335347
record recoveryCleanupRecord
336348
}
337349

338-
func loadRecoveryCleanupQueue(targetPath string) recoveryCleanupQueue {
350+
func loadRecoveryCleanupQueue(targetPath string) (recoveryCleanupQueue, error) {
339351
recordPath, err := recoveryCleanupRecordPath(targetPath)
340352
if err != nil {
341-
return recoveryCleanupQueue{}
353+
return recoveryCleanupQueue{}, err
342354
}
343355
data, err := os.ReadFile(recordPath)
344356
if err != nil {
345-
return recoveryCleanupQueue{}
357+
if errors.Is(err, os.ErrNotExist) {
358+
return recoveryCleanupQueue{}, nil
359+
}
360+
return recoveryCleanupQueue{}, fmt.Errorf("read recovery cleanup record %s: %w", recordPath, err)
346361
}
347362
var queue recoveryCleanupQueue
348-
if json.Unmarshal(data, &queue) != nil || queue.Records == nil {
349-
var legacy recoveryCleanupRecord
350-
if json.Unmarshal(data, &legacy) == nil && legacy.Path != "" {
351-
queue.Records = []recoveryCleanupRecord{legacy}
352-
}
363+
if err := json.Unmarshal(data, &queue); err == nil && queue.Records != nil {
364+
return queue, nil
365+
}
366+
var legacy recoveryCleanupRecord
367+
if json.Unmarshal(data, &legacy) == nil && legacy.Path != "" {
368+
return recoveryCleanupQueue{Records: []recoveryCleanupRecord{legacy}}, nil
353369
}
354-
return queue
370+
return recoveryCleanupQueue{}, fmt.Errorf("parse recovery cleanup record %s", recordPath)
355371
}
356372

357373
func prepareRecoveryCleanup(targetPath string) []recoveryCleanupCandidate {
358-
queue := loadRecoveryCleanupQueue(targetPath)
374+
queue, err := loadRecoveryCleanupQueue(targetPath)
375+
if err != nil {
376+
// Unreadable trusted state is not "nothing to clean": skip destructive
377+
// cleanup rather than treat the queue as empty.
378+
return nil
379+
}
359380
var candidates []recoveryCleanupCandidate
360381
// Records outlive a single attempt so a temporarily locked copy is still
361382
// deleted on a later run, but a record that can never become actionable has
@@ -373,7 +394,7 @@ func prepareRecoveryCleanup(targetPath string) []recoveryCleanupCandidate {
373394
continue
374395
}
375396
retained = append(retained, record)
376-
if oldBinaryPreserved(record.Path) {
397+
if oldBinaryPreserved(record.Path) || record.Unresolved {
377398
continue
378399
}
379400
file, err := openRecoveryCopy(record.Path)
@@ -419,6 +440,49 @@ func recoveryFileIdentity(file *os.File) (recoveryIdentity, error) {
419440
// recorded as updater-owned: the next run reopens recoveryPath and only deletes
420441
// it if it is still that same object.
421442
func appendRecoveryCleanupRecord(targetPath string, recoveryPath string, identity recoveryIdentity) error {
443+
return appendRecoveryCleanupRecordWithState(targetPath, recoveryPath, identity, false)
444+
}
445+
446+
// appendUnresolvedRecoveryRecord records that a failed restore left recoveryPath
447+
// as the last verified binary. Unlike the sibling .keep marker in the
448+
// installation directory, this lives in per-user state an install-directory
449+
// writer cannot delete. Preflight consults it alongside the marker (#868).
450+
//
451+
// A var so a test can force the failure branch. Making the real per-user store
452+
// unwritable also takes down cleanup-queue writes this path does not own.
453+
var appendUnresolvedRecoveryRecord = func(targetPath string, recoveryPath string, identity recoveryIdentity) error {
454+
return appendRecoveryCleanupRecordWithState(targetPath, recoveryPath, identity, true)
455+
}
456+
457+
// persistFailedRestoreSignal keeps a durable fail-closed preflight signal after
458+
// restoreOriginalBinary has already written the sibling .keep marker. The
459+
// preferred signal is the per-user unresolved record. If that write fails, the
460+
// marker is the only remaining protection and an install-directory writer can
461+
// delete it; relocate the last verified copy to a .recovery name so the next
462+
// preflight still refuses (#868).
463+
//
464+
// Relocation is bound to the object still named by asidePath. restoreOriginalBinary
465+
// may already have moved the verified bytes back onto targetPath (the live
466+
// binary) or to a .recovery name; keepUnmarkedRecoveryCopy renames through the
467+
// handle without re-checking that path, so compensation must not run unless
468+
// verifyPromotedTarget says the handle still is the aside copy. Making a failed
469+
// record write worse by pulling the restored executable out of targetPath is
470+
// not an acceptable fallback.
471+
func persistFailedRestoreSignal(file *os.File, targetPath string, asidePath string, identity recoveryIdentity, restoreErr error) error {
472+
if err := appendUnresolvedRecoveryRecord(targetPath, asidePath, identity); err == nil {
473+
return restoreErr
474+
} else if verifyPromotedTarget(file, asidePath) != nil {
475+
return fmt.Errorf("%w (unresolved recovery record could not be written: %v)", restoreErr, err)
476+
} else if kept, keepErr := keepUnmarkedRecoveryCopy(file, asidePath); keepErr == nil {
477+
return fmt.Errorf("%w (unresolved recovery record could not be written: %v; the last binary this updater verified was moved to the distinct recovery path %s)", restoreErr, err, kept)
478+
} else if kept != "" {
479+
return fmt.Errorf("%w (unresolved recovery record could not be written: %v; the last binary this updater verified was moved to %s but could not be verified there: %v)", restoreErr, err, kept, keepErr)
480+
} else {
481+
return fmt.Errorf("%w (unresolved recovery record could not be written: %v)", restoreErr, err)
482+
}
483+
}
484+
485+
func appendRecoveryCleanupRecordWithState(targetPath string, recoveryPath string, identity recoveryIdentity, unresolved bool) error {
422486
if !validUpdaterRecoveryPath(targetPath, recoveryPath) {
423487
return fmt.Errorf("invalid updater recovery path %s", recoveryPath)
424488
}
@@ -427,12 +491,50 @@ func appendRecoveryCleanupRecord(targetPath string, recoveryPath string, identit
427491
VolumeSerial: identity.VolumeSerial,
428492
FileIndexHigh: identity.FileIndexHigh,
429493
FileIndexLow: identity.FileIndexLow,
494+
Unresolved: unresolved,
495+
}
496+
queue, err := loadRecoveryCleanupQueue(targetPath)
497+
if err != nil {
498+
return err
430499
}
431-
queue := loadRecoveryCleanupQueue(targetPath)
432500
queue.Records = append(queue.Records, record)
433501
return writeRecoveryCleanupQueue(targetPath, queue)
434502
}
435503

504+
// unresolvedRecordedRecoveryPaths returns live recovery copies vouched for by
505+
// per-user unresolved-state records. Absence of the sibling .keep marker does
506+
// not drop them: that marker is attacker-deletable in the install directory.
507+
func unresolvedRecordedRecoveryPaths(targetPath string) ([]string, error) {
508+
queue, err := loadRecoveryCleanupQueue(targetPath)
509+
if err != nil {
510+
return nil, err
511+
}
512+
var paths []string
513+
for _, record := range queue.Records {
514+
if !record.Unresolved || !validUpdaterRecoveryPath(targetPath, record.Path) {
515+
continue
516+
}
517+
if _, err := os.Lstat(record.Path); errors.Is(err, os.ErrNotExist) {
518+
continue
519+
}
520+
paths = append(paths, record.Path)
521+
}
522+
return paths, nil
523+
}
524+
525+
var (
526+
syncRecoveryCleanupFile = (*os.File).Sync
527+
moveRecoveryCleanupFile = windows.MoveFileEx
528+
)
529+
530+
func replaceRecoveryCleanupFile(sourcePath string, targetPath string) error {
531+
return moveRecoveryCleanupFile(
532+
windows.StringToUTF16Ptr(sourcePath),
533+
windows.StringToUTF16Ptr(targetPath),
534+
windows.MOVEFILE_REPLACE_EXISTING|windows.MOVEFILE_WRITE_THROUGH,
535+
)
536+
}
537+
436538
func writeRecoveryCleanupQueue(targetPath string, queue recoveryCleanupQueue) error {
437539
data, err := json.Marshal(queue)
438540
if err != nil {
@@ -460,10 +562,18 @@ func writeRecoveryCleanupQueue(targetPath string, queue recoveryCleanupQueue) er
460562
_ = temporary.Close()
461563
return err
462564
}
565+
// This queue may carry an unresolved fail-closed promotion signal, not merely
566+
// an advisory cleanup backlog. Flush its complete contents before publishing,
567+
// then use Windows' write-through replacement so success is not reported in
568+
// the crash window between the namespace update and durable storage.
569+
if err := syncRecoveryCleanupFile(temporary); err != nil {
570+
_ = temporary.Close()
571+
return err
572+
}
463573
if err := temporary.Close(); err != nil {
464574
return err
465575
}
466-
return os.Rename(temporaryPath, recordPath)
576+
return replaceRecoveryCleanupFile(temporaryPath, recordPath)
467577
}
468578

469579
type fileDispositionInfo struct {
@@ -503,7 +613,10 @@ func closeRecoveryCleanupCandidates(candidates []recoveryCleanupCandidate) {
503613
// full binaries.
504614
func cleanupSupersededRecoveryCopies(targetPath string, candidates []recoveryCleanupCandidate) {
505615
defer closeRecoveryCleanupCandidates(candidates)
506-
queue := loadRecoveryCleanupQueue(targetPath)
616+
queue, err := loadRecoveryCleanupQueue(targetPath)
617+
if err != nil {
618+
return
619+
}
507620
deleted := make(map[recoveryCleanupRecord]bool)
508621
for index := range candidates {
509622
candidate := &candidates[index]

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.