omnet
aien-dev/omnet
Brought by drakestapleton
omnet / om / store / README.md
| 1 | # Descriptor-secure snapshot store |
| 2 | |
| 3 | Program-mode Linux ARM64 storage. Include `om/blake2s/blake2s.om`, `store.om`, then |
| 4 | `storepool.om` once. Production logic and native I/O are Omega. The companion |
| 5 | owns fixed global descriptor/state cells; callers must serialize all operations. |
| 6 | |
| 7 | ## API |
| 8 | |
| 9 | All `int` results are `0` on success, `1` on refusal or I/O failure, except |
| 10 | `stcommit`, which additionally returns `2` for an **uncertain commit**. |
| 11 | |
| 12 | - `stopen(path view uchar 65536, n ulong, init int)`: open a provisioned directory, |
| 13 | validate its descriptor, acquire the writer lock, and `fchdir` into it. `init=1` |
| 14 | requires no `snapshot` entry; it does not create a default state. The caller |
| 15 | must commit its explicit initial semantic state before serving. `init=0` |
| 16 | requires an existing fully valid snapshot and calls `stload`. Other modes, |
| 17 | embedded zero bytes, empty paths and paths over 4095 bytes are refused. |
| 18 | - `stbegin()`: create `snapshot.tmp` exclusively. No active transaction is allowed. |
| 19 | - `stwrite(bytes view uchar 65536, n ulong)`: append one frame, preserving chunk |
| 20 | boundaries. Zero length is a no-op. Maximum 65536 bytes per call, 65536 nonempty |
| 21 | calls and 33554432 payload bytes per snapshot. An error poisons the transaction; |
| 22 | it must be aborted, not committed or retried. |
| 23 | - `stcommit()`: append the terminator, fsync the staged file, reread and validate |
| 24 | its entire framing and checksums, rename it over `snapshot`, then fsync the |
| 25 | opened directory. Only result `0` permits in-memory publication and success |
| 26 | replies. A pre-rename failure returns `1` and permits `stabort`. After successful |
| 27 | rename, directory-sync or close failure returns `2`; the store rejects further |
| 28 | begin/load/read/abort operations. The daemon must exit without serving, replying |
| 29 | success, or attempting rollback. A retry on the same staged file is refused. |
| 30 | - `stabort()`: close the staged file and remove a validated reserved temporary |
| 31 | entry, while holding the lock. Never changes the committed snapshot. |
| 32 | - `stload()`: validate the complete committed snapshot, fsync it and the directory, |
| 33 | then reset the reader. Startup thus re-establishes durability of the selected |
| 34 | snapshot after an earlier uncertain rename. Never falls back to a temporary file. |
| 35 | - `stread(out edit uchar 65536) -> long`: next exact nonempty write frame; zero at |
| 36 | EOF, `-1` on error. Callers accumulate across frames when reading semantic records. |
| 37 | Framing and checksums are also checked on this pass. Errors poison the store. |
| 38 | - `stclose()`: close file descriptors and directory, then release the abstract lock. |
| 39 | Call only **after** the daemon has closed/removed its RPC socket and finished all |
| 40 | storage activity. A failed `stopen` cleans its own descriptors; an already-open |
| 41 | store is not closed by a second `stopen` call. |
| 42 | - `stpass(dirtgt int, locktgt int)`: first-epoch controlled-exec handoff. Requires a |
| 43 | healthy store with committed initialization, no writer, and no recovery read in |
| 44 | progress. An exhausted reader is closed. Targets must be distinct, at least 3, |
| 45 | and unused (`fstat` must return exactly `EBADF`); occupied targets are never |
| 46 | overwritten. Duplicates directory and lock with flags zero, retaining original |
| 47 | close-on-exec descriptors and tracking the copies for shutdown. Once duplication |
| 48 | begins, further storage operations are poisoned even on successful handoff: |
| 49 | immediately exec or exit, never resume serving. Any failure is fatal to rollover. |
| 50 | - `stresume(dirfd int, lockfd int)`: adopt structurally validated inherited |
| 51 | descriptors. Requires a 0700/effective-UID directory and an effective-UID socket |
| 52 | whose complete 34-byte abstract name exactly matches that directory's device |
| 53 | and inode. Anchors cwd and calls ordinary `stload`, including full validation and |
| 54 | recovery fsync. No bind, initialization, pathname reopen, stale-file removal or |
| 55 | fallback occurs. Missing, swapped, non-socket and wrong-directory descriptors |
| 56 | refuse. Subsequent resumed epochs retain these descriptors directly; they must |
| 57 | not call `stpass` or attempt `dup3(fd, fd, 0)`. |
| 58 | |
| 59 | Ordinary opened descriptors are close-on-exec. The sole exception is explicit |
| 60 | quiescent handoff, used by the daemon with directory FD 64 and lock FD 65 (listener |
| 61 | FD 66 belongs to the daemon). Controlled `/proc/self/exe --resume` must keep all |
| 62 | three references alive continuously and recover semantic state before serving. |
| 63 | The launcher must provide only standard I/O, trusted procfs and a trusted executable |
| 64 | without privilege changes. The service must not fork children retaining these |
| 65 | descriptors. Structural resume validation does not authenticate a hostile same-UID |
| 66 | launcher; same-UID/root trust remains a prerequisite. A returned exec failure must |
| 67 | exit, not clear poison, spin, unlink the RPC entry or reacquire the writer lock. |
| 68 | No environment variable, request field, or production API injects failures or |
| 69 | bypasses validation. |
| 70 | |
| 71 | ## Sole writer and filesystem boundary |
| 72 | |
| 73 | The directory must be owned by effective UID, directory type, exactly mode 0700 |
| 74 | (including refusal of special permission bits). Files must be owned by effective |
| 75 | UID, regular, exactly 0600, single-link and bounded in size. Files use |
| 76 | `O_NOFOLLOW|O_NONBLOCK`; special files cannot block startup. The directory uses |
| 77 | `O_DIRECTORY|O_NOFOLLOW`. Metadata is checked on the opened descriptor, not by a |
| 78 | path check preceding open. Existing entries are never repaired to acceptable |
| 79 | permissions. An incompatible umask can cause staged-file validation to refuse. |
| 80 | |
| 81 | The bind-only abstract AF_UNIX lock name is the bytes `omnet.store.v1.` followed |
| 82 | by little-endian 64-bit `st_dev` and `st_ino`. No PID, configurable name, or software |
| 83 | version influences this identity. It is never listened on and accepts no requests. |
| 84 | The descriptor lives across all storage and RPC activity. Different spellings of |
| 85 | the same directory contend for the same lock; process death releases it. A failed |
| 86 | bind refuses startup. Only after acquisition may a daemon remove its reserved |
| 87 | stale RPC pathname. No connect-failed or stale-PID ownership heuristic is used. |
| 88 | |
| 89 | **Deployment prerequisites:** all writers must follow this protocol in the same |
| 90 | network namespace; the operator must provision a trusted ancestor chain; the |
| 91 | process is single-threaded and must not change cwd later. `O_NOFOLLOW` protects |
| 92 | only the final directory component, not ancestor symlinks or path traversal through |
| 93 | a symlink followed by `/`. Same-UID processes and root are trusted. Abstract names |
| 94 | have no filesystem permissions, so an unrelated local process can deny service by |
| 95 | pre-binding a predicted name, but cannot thereby authorize an RPC. A separate-UID |
| 96 | headscale adapter cannot traverse this 0700 directory; this is not a claim of that |
| 97 | integration or of a separate privilege boundary. |
| 98 | |
| 99 | A leftover `snapshot.tmp` is opened without following symlinks, validated as a |
| 100 | private regular single-link file, and removed only under the lock. Its contents |
| 101 | need not be valid because interrupted writes are expected. Unsafe temporary entry |
| 102 | types/ownership/permissions cause refusal, not deletion. In recovery mode the |
| 103 | committed snapshot must validate before stale cleanup. Missing/malformed committed |
| 104 | state never means disabled or empty, and a valid temporary file is never promoted. |
| 105 | |
| 106 | ## Version 1 framing |
| 107 | |
| 108 | All integers are unsigned little-endian 64-bit. The file begins with the exact |
| 109 | 16 ASCII bytes `OMNETSNAP0000001`. Each nonempty frame contains: |
| 110 | |
| 111 | 1. payload byte length (1..65536); |
| 112 | 2. BLAKE2s-256 of that frame's payload (32 bytes); |
| 113 | 3. payload bytes. |
| 114 | |
| 115 | Maintain a 32-byte zero-initialized chain. For each frame, replace it with |
| 116 | `BLAKE2s-256(previous_chain || payload_digest || length || cumulative_payload_bytes |
| 117 | || cumulative_frame_count)`. The final record is zero length, final chain, total |
| 118 | payload length, and frame count (56 bytes). Empty snapshots have an all-zero chain |
| 119 | and zero totals. No bytes may follow the terminator. Maximum file size is 36175944 |
| 120 | bytes (32 MiB payload + 65536 frame headers + header and terminator). |
| 121 | |
| 122 | This commits payload, ordering, frame boundaries and completeness; it is corruption |
| 123 | integrity, not owner authentication or anti-rollback. An owner can reconstruct a |
| 124 | valid file, and restoration of an old valid whole-store backup is undetectable. |
| 125 | Service semantics and reconstruction correctness are the daemon's responsibility. |
| 126 | |
| 127 | ## Tests and scope |
| 128 | |
| 129 | `sh om/store/test/run.sh` compiles with the repository-pinned compiler and executes |
| 130 | isolated native processes. Python independently constructs BLAKE2s framing. Cases |
| 131 | cover restart, explicit initialization, abort, alias writer contention, kill and |
| 132 | stale-temp recovery, unsafe metadata/types/links, truncation, corruption, oversized |
| 133 | files, maximum frame/payload capacity, self-exec with continuous writer exclusion, |
| 134 | invalid inherited descriptors, partial I/O, bounded EINTR handling and |
| 135 | injected write/read/fsync/rename failures. Ownership-changing fixtures run only |
| 136 | when the test runner is root; non-root runs report that subset as skipped. Injection replaces |
| 137 | thin syscall wrappers only in a separately built test binary; production source |
| 138 | has no fault controls. A completed-result checksum mutant must differ, rather than |
| 139 | crash. Process-kill/restart and syscall-failure tests do not prove power-loss |
| 140 | behavior of any specific filesystem. No live state or services are touched. |
| 141 | |
| 142 | ## Third-party material and notices |
| 143 | |
| 144 | No third-party code or vectors are copied here. BLAKE2s uses the repository's |
| 145 | existing Omega unit. Linux ABI layouts and operation semantics are referenced in |
| 146 | the pinned compiler's `docs/one_layer/OMEGESE.md` section 10 and Linux `unix(7)`, |
| 147 | `open(2)`, `stat(2)`, `rename(2)` and `fsync(2)`. |