Peios Learn
Documentation

Peios

Peios Peios Kernel Universal Directory Project

Developer Tools

Trail Acta Provium clackatoo CAF GCSL Pekit

Applications

Framewright

Standards and Conventions

Standards and Conventions Peios Specifications
Peios Kernel

Backup and restore

Single-page view · as markdown

Backup and restore

Peios Kernel / Internals / LCS / Backup and restore

Use these articles to understand backup and restore in the Peios kernel.

  • The Stream
  • Backup
  • Restore

The Stream

Peios Kernel / Internals / LCS / Backup and restore

The registry backup format is a streamable binary representation of a key and everything beneath it, with full layer fidelity. It is used by REG_IOC_BACKUP and REG_IOC_RESTORE, by first-boot seeding, by disaster recovery and by offline migration.

It is an LCS-level format. Sources never see it: LCS serialises from source data on the way out and deserialises into RSI operations on the way in.

Because a third party writes and reads these streams directly — that is what migration and recovery mean — the format is a normative specification rather than a description. It is a chapter of PSPK, and every byte layout, record type, ordering rule and validation requirement lives there. This section covers what LCS does with it.

What the design buys #

Streamable. It is written to an arbitrary fd — a file, a pipe, a socket — with no seeking, in a single pass, and read back the same way.

Full layer fidelity. Every path entry, value, tombstone and blanket tombstone is stored with its layer tag, so restoring reconstructs the layered state rather than a flattened snapshot of it.

Depth-first pre-order. A parent always appears before its children, so a restore can create keys top-down without buffering a tree.

Descriptors inline. Each key record carries its own descriptor with no deduplication. Redundancy is external compression's problem; piping through zstd handles it.

Self-verifying. A trailer carries a record count and a SHA-256 over everything before it, so truncation and corruption are detected.

Versioning #

The header carries a format version and a minimum reader version. A writer that used only older features sets a lower minimum, letting older readers restore the stream.

A reader that finds a minimum above its own supported version rejects the stream outright, before touching anything.

Both are 21 in the current implementation, and the reader supports 21.

Unknown record types are skipped when the minimum reader version allows it, and they still count toward the record count and the checksum. A writer that adds a record type a restore genuinely needs must raise the minimum reader version, so that an older reader refuses the stream rather than restoring an incomplete one.

Extension is by new record types only. Existing record payloads must be consumed exactly; trailing bytes inside a known record are an error. This is the opposite of the RSI's request convention (Validation), and the difference is deliberate: a stream is replayed into mutations long after it was written, and a field silently ignored there is data silently lost.

Backup

Peios Kernel / Internals / LCS / Backup and restore

REG_IOC_BACKUP exports the key on the fd and its entire subtree to another fd, preserving parent/child relationships even when traversal must grow its internal storage.

It requires SeBackupPrivilege and performs no per-key AccessCheck whatsoever. The privilege is the whole authorisation, which is why the operation is audited unconditionally (Audit).

The output fd must be writable, or EBADF.

The snapshot #

Before reading anything, LCS opens a read-only source transaction — RSI_BEGIN_TRANSACTION with mode RSI_TXN_READ_ONLY — so that the whole export is a point-in-time snapshot. Concurrent mutations do not appear part-way through the stream.

That transaction is released with RSI_ABORT_TRANSACTION and never committed. There is no commit call anywhere in the backup path; a read-only transaction has nothing to commit.

A source that does not support read-only snapshots answers RSI_TXN_NOT_SUPPORTED and the backup fails ENOTSUP.

A source already holding MaxReadOnlyTransactionsPerSource snapshots (default 16) yields EBUSY — which bounds snapshot-holding without treating backups as write-lock holders, since they are not.

Backing up an orphaned key is ENOENT: it is no longer a reachable subtree root (Deletion and Orphans).

Audit #

lcs.audit.backup.started is emitted before any subtree data is read, and if it cannot be emitted the backup returns EIO and does not start.

After privilege, output-fd and key validation, START precedes the request for the read-only snapshot. A refused or failed snapshot therefore emits both START and END, with END carrying the failure errno; no subtree is exported and no unopened snapshot is released. A START emission failure prevents the snapshot request itself.

lcs.audit.backup.ended is emitted afterwards, carrying the result, and a failure to emit it cannot change a result that has already happened.

What is written #

The stream is a header, the layer manifest, then each key in depth-first pre-order with its path entries, values and blanket tombstones, then the trailer. The exact shapes are in the PSPK chapter.

Two things about the exporter are worth stating here, because they constrain a reader more tightly than the format does.

The exporter writes no GUID-bearing path entries for the backup root. The root's section contains only its hidden entries.

A reader tolerates and skips them if some other writer produces them, but this one does not emit them, because on restore the target key's existing name is authoritative and they would be discarded anyway.

Hidden entries belong to the parent's section, not to a section of their own — a hidden entry has no key to have a section for.

A hidden entry masking a name where no key exists in any layer is still valid; it expresses "this layer hides this name" whether or not anything is there to hide.

The layer manifest is written from the live layer table, and it is a manifest: it records what the layers looked like at backup time so that a restore can validate the stream against them. It is not a backup of the layer definitions.

A layer definition is backed up only when Machine\System\Registry\Layers\<Name>\ is itself inside the subtree being exported, in which case it is ordinary key and value data like anything else.

This includes definitions for unused layers in a whole-hive export. Their metadata remains ordinary registry data; an unused definition does not by itself require an entry in the layer manifest.

Restore

Peios Kernel / Internals / LCS / Backup and restore

REG_IOC_RESTORE replaces the key on the fd and its entire subtree from a stream. It is not a merge: the target's contents and descendants are torn down before the stream's contents are written.

It requires SeRestorePrivilege and, like backup, performs no per-key AccessCheck and is audited unconditionally.

The input fd must be readable, or EBADF. Restoring onto an orphaned key is ENOENT.

Because restore rewrites every descriptor in the subtree, SeRestorePrivilege effectively confers WRITE_DAC and WRITE_OWNER over everything within reach of one (The Source Model).

One transaction #

The entire restore — teardown and rebuild together — is wrapped in a single read-write source transaction.

A source that answers RSI_TXN_NOT_SUPPORTED for RSI_TXN_READ_WRITE cannot be a restore target: restore requires atomicity and there is no partial-restore mode.

Every failure path aborts the transaction, so a failed restore rolls back the teardown as well.

The target key survives #

The stream's header names a root GUID, and that GUID is remapped to the already-open target key everywhere it appears — in parent references, in child references, in value key references — before anything is validated or dispatched.

The target key object is not replaced. Its GUID, parent, name, volatile flag and symlink flag remain what they were; they are never taken from the stream.

What the stream's root record supplies is the mutable part: the Security Descriptor and the last write time, written to the target with RSI_WRITE_KEY inside the transaction.

The root record's immutable flags must match the target's. A backup of a volatile key restored onto a non-volatile one, or a symlink onto a non-symlink, is EINVAL.

Descendants keep their backup GUIDs. Those are written into the target verbatim.

Order of operations #

  1. Validate the whole stream, including the trailer's record count and checksum, and retain every replayable record.
  2. Read the layer manifest and apply the precedence gate (below).
  3. Verify the root record and its immutable flags against the live target.
  4. Tear down the target's contents and descendants — path entries, values, blanket tombstones, and descendant key records — inside the transaction.
  5. Write the root's mutable fields, then replay its section's values and blanket tombstones.
  6. For each non-root key in stream order: create it, write its last write time, then replay its path entries, values and blanket tombstones.
  7. Commit.

The stream is read from the fd exactly once, sequentially. Nothing seeks, so a pipe is a valid input.

Validating the whole stream first is stronger than the format requires: a checksum failure aborts before any source mutation rather than after some. The cost is memory rather than seeking — the replayable records are retained in kernel memory across the teardown.

The precedence gate #

Before any key record is written, LCS checks the layer manifest. If any declared layer has a precedence above 0, or any existing cached layer table entry with the same folded identity does, and the caller does not hold SeTcbPrivilege, the restore aborts with EPERM before a single byte reaches the source.

And if the stream contains ordinary key and value records for Machine\System\Registry\Layers\<Name>\, writes that create or raise persisted metadata above precedence 0 hit the ordinary inline SeTcbPrivilege check as well (Writing Into a Layer).

Both exist so that SeRestorePrivilege cannot be used to smuggle a Group Policy-tier layer past the defence in depth that guards precedence.

Layers in a restored stream #

Manifest records create, update, delete, enable, disable and authorise nothing.

If restored entries reference a layer that is not in the current table, and the stream does not also restore that layer's metadata subtree as ordinary registry data, those entries become latent unknown-layer entries and are ignored during resolution until real metadata exists (Resolution).

If the metadata subtree is included, it is restored through the ordinary path, and those records — not the manifest — are what define the layer.

Sequence remapping #

Backup sequence numbers preserve the backup's internal ordering, but a restore is a new mutation and its entries must outrank everything already present. So the numbers are remapped rather than written through.

Before dispatching the first layer-qualified record, LCS takes the global sequence-allocation gate and records the current next sequence as the offset.

Every restored record is then written with offset + backup_sequence, which preserves relative order while placing the whole set above pre-restore state.

The gate is held until the restore reaches a terminal state. Other sequence-allocating mutations wait; reads are not blocked.

A restore with no layer-qualified records at all never takes it.

If a remapped value would overflow, the restore fails EOVERFLOW — and it fails at validation, before teardown, rather than part-way through. The valid remapped range stops just below U64_MAX, which is never handed out (The Sequence Counter).

When the restore reaches any terminal state — commit, abort, failure or cancellation — LCS advances the global counter past the highest number it dispatched, and does not roll that back. Sequence numbers a failed restore dispatched become unused gaps, exactly like those of any other failed write.

GUID collisions #

A GUID that appears twice in one stream, other than the root, is EINVAL. So is a non-root GUID equal to the target root's.

A non-root GUID that already exists outside the subtree being replaced is EEXIST — but it is discovered by the source rejecting the create during replay, not by a check beforehand. LCS has no index of which GUIDs exist elsewhere, so the collision surfaces mid-restore, and the transaction rolls back.

The parent of every path entry, after remapping, must be either the restore root or a key record already processed earlier in the stream. That check is made up front, and it is what stops a crafted backup injecting path entries into arbitrary parts of the existing namespace outside the subtree being replaced.

Watches #

A restore is an arbitrary subtree replacement and LCS retains no exact before-and-after diff for it. On a successful commit it publishes the affected hive's generation increment and dispatches a no-name OVERFLOW to the armed watches on that source (Dispatch).

A restore that fails or aborts before commit emits nothing.

Peios Learn — documentation for Peios software.

Built with Trail.