@@ -278,12 +278,24 @@ type recoveryCleanupRecord struct { |
278 | 278 | VolumeSerial uint32 `json:"volumeSerial"` |
279 | 279 | FileIndexHigh uint32 `json:"fileIndexHigh"` |
280 | 280 | 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"` |
281 | 286 | } |
282 | 287 |
|
283 | 288 | type recoveryCleanupQueue struct { |
284 | 289 | Records []recoveryCleanupRecord `json:"records"` |
285 | 290 | } |
286 | 291 |
|
| 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. |
287 | 299 | var recoveryCleanupStateDir = func() (string, error) { |
288 | 300 | root, err := config.UserConfigDir() |
289 | 301 | if err != nil { |
@@ -335,27 +347,36 @@ type recoveryCleanupCandidate struct { |
335 | 347 | record recoveryCleanupRecord |
336 | 348 | } |
337 | 349 |
|
338 | | -func loadRecoveryCleanupQueue(targetPath string) recoveryCleanupQueue { |
| 350 | +func loadRecoveryCleanupQueue(targetPath string) (recoveryCleanupQueue, error) { |
339 | 351 | recordPath, err := recoveryCleanupRecordPath(targetPath) |
340 | 352 | if err != nil { |
341 | | - return recoveryCleanupQueue{} |
| 353 | + return recoveryCleanupQueue{}, err |
342 | 354 | } |
343 | 355 | data, err := os.ReadFile(recordPath) |
344 | 356 | 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) |
346 | 361 | } |
347 | 362 | 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 |
353 | 369 | } |
354 | | - return queue |
| 370 | + return recoveryCleanupQueue{}, fmt.Errorf("parse recovery cleanup record %s", recordPath) |
355 | 371 | } |
356 | 372 |
|
357 | 373 | 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 | + } |
359 | 380 | var candidates []recoveryCleanupCandidate |
360 | 381 | // Records outlive a single attempt so a temporarily locked copy is still |
361 | 382 | // deleted on a later run, but a record that can never become actionable has |
@@ -373,7 +394,7 @@ func prepareRecoveryCleanup(targetPath string) []recoveryCleanupCandidate { |
373 | 394 | continue |
374 | 395 | } |
375 | 396 | retained = append(retained, record) |
376 | | - if oldBinaryPreserved(record.Path) { |
| 397 | + if oldBinaryPreserved(record.Path) || record.Unresolved { |
377 | 398 | continue |
378 | 399 | } |
379 | 400 | file, err := openRecoveryCopy(record.Path) |
@@ -419,6 +440,49 @@ func recoveryFileIdentity(file *os.File) (recoveryIdentity, error) { |
419 | 440 | // recorded as updater-owned: the next run reopens recoveryPath and only deletes |
420 | 441 | // it if it is still that same object. |
421 | 442 | 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 { |
422 | 486 | if !validUpdaterRecoveryPath(targetPath, recoveryPath) { |
423 | 487 | return fmt.Errorf("invalid updater recovery path %s", recoveryPath) |
424 | 488 | } |
@@ -427,12 +491,50 @@ func appendRecoveryCleanupRecord(targetPath string, recoveryPath string, identit |
427 | 491 | VolumeSerial: identity.VolumeSerial, |
428 | 492 | FileIndexHigh: identity.FileIndexHigh, |
429 | 493 | FileIndexLow: identity.FileIndexLow, |
| 494 | + Unresolved: unresolved, |
| 495 | + } |
| 496 | + queue, err := loadRecoveryCleanupQueue(targetPath) |
| 497 | + if err != nil { |
| 498 | + return err |
430 | 499 | } |
431 | | - queue := loadRecoveryCleanupQueue(targetPath) |
432 | 500 | queue.Records = append(queue.Records, record) |
433 | 501 | return writeRecoveryCleanupQueue(targetPath, queue) |
434 | 502 | } |
435 | 503 |
|
| 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 | + |
436 | 538 | func writeRecoveryCleanupQueue(targetPath string, queue recoveryCleanupQueue) error { |
437 | 539 | data, err := json.Marshal(queue) |
438 | 540 | if err != nil { |
@@ -460,10 +562,18 @@ func writeRecoveryCleanupQueue(targetPath string, queue recoveryCleanupQueue) er |
460 | 562 | _ = temporary.Close() |
461 | 563 | return err |
462 | 564 | } |
| 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 | + } |
463 | 573 | if err := temporary.Close(); err != nil { |
464 | 574 | return err |
465 | 575 | } |
466 | | - return os.Rename(temporaryPath, recordPath) |
| 576 | + return replaceRecoveryCleanupFile(temporaryPath, recordPath) |
467 | 577 | } |
468 | 578 |
|
469 | 579 | type fileDispositionInfo struct { |
@@ -503,7 +613,10 @@ func closeRecoveryCleanupCandidates(candidates []recoveryCleanupCandidate) { |
503 | 613 | // full binaries. |
504 | 614 | func cleanupSupersededRecoveryCopies(targetPath string, candidates []recoveryCleanupCandidate) { |
505 | 615 | defer closeRecoveryCleanupCandidates(candidates) |
506 | | - queue := loadRecoveryCleanupQueue(targetPath) |
| 616 | + queue, err := loadRecoveryCleanupQueue(targetPath) |
| 617 | + if err != nil { |
| 618 | + return |
| 619 | + } |
507 | 620 | deleted := make(map[recoveryCleanupRecord]bool) |
508 | 621 | for index := range candidates { |
509 | 622 | candidate := &candidates[index] |
|