Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omnet/blob/main/cmd/tkad/README.md # omnet aien-dev/omnet Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omnet / cmd / tkad / README.md` 225 lines ยท 13302 bytes Raw Blame History Rendered 1 # tkad: bounded local Tailnet Lock service 2 3 Production code is Omega, compiled by `tools/build-tkad.sh` with `tools/omc.sh`. 4 Go and Python under `test/` are test-only. Protocol types are pinned to Tailscale 5 v1.102.0 `tailcfg/tka.go` and `ipn/ipnlocal/tailnet-lock.go`. 6 7 ## Invocation and trust boundary 8 9 Provision an empty directory owned by the effective UID, mode0700, under a trusted 10 ancestor chain. Run `build/tkad /absolute/state/directory --init` exactly once. 11 Subsequent starts use `build/tkad /absolute/state/directory`. Missing or corrupt 12 snapshots refuse startup; initialization refuses an existing snapshot. 13 14 The service creates `tkad.sock` inside its opened directory, mode0600. The 15 0700directory and0600socket enforce a same-UID/root **trusted local adapter** 16 boundary. There is no SO_PEERCRED operation in the pinned native ABI and no claim 17 of peer-credential inspection. Same-UID/root processes can act as the adapter, 18 read secrets and change storage. This is not Noise authentication, HTTP serving, 19 a deployed Headscale integration, or a distinct-UID privilege boundary. The 20 adapter must authenticate remote clients and derive node IDs/admin assertions 21 from its trusted identity model, never from remote JSON. 22 23 A descriptor-held abstract AF_UNIX bind lock, derived from directory device and 24 inode, admits one writer. All writers must use the same network namespace. Only 25 a lock holder removes the reserved stale socket pathname. No PID-file or 26 connect-failure lock inference is used. A process crash leaves a pathname that a 27 new lock holder can remove safely. 28 29 ## IPC version1 30 31 One request and one response per AF_UNIX stream connection. Integers are unsigned 32 little endian. Header fields must be complete; there is no implicit terminator. 33 34 Request header,20bytes: 35 36 | Offset | Size | Meaning | 37 |---|---|---| 38 |0|4|Bytes `54 4b 41 01` (`TKA` and version1)| 39 |4|1|Route ID below| 40 |5|1|Trusted adapter admin assertion, exactly0 or1| 41 |6|2|Reserved, zero| 42 |8|8|Authenticated NodeID; RPCs require a registered positive signed64-bit ID| 43 |16|4|Raw JSON byte length,0..65536| 44 45 The body is the unmodified upstream JSON request. Response header,12bytes, has 46 the same magic at0, HTTP-style status as u16at4, reserved zero u16at6, and JSON 47 length u32at8. A200body follows the exact upstream response field shapes. Errors 48 have a short fixed JSON body, never request details or secrets. Bad/truncated 49 headers and oversized payload declarations close that connection without a reply. 50 Each receive/send phase has an absolute3second deadline, nonblocking descriptors 51 and kernel polling. A slow client can occupy one serial dispatch for that bounded 52 period; this is not a high-throughput concurrent server. 53 54 | ID | Route | 55 |---|---| 56 |1|`/machine/tka/init/begin`| 57 |2|`/machine/tka/init/finish`| 58 |3|`/machine/tka/bootstrap`| 59 |4|`/machine/tka/sync/offer`| 60 |5|`/machine/tka/sync/send`| 61 |6|`/machine/tka/disable`| 62 |7|`/machine/tka/sign`| 63 |8|`/machine/tka/affected-sigs`| 64 |9|Trusted registry upsert| 65 |10|Trusted registry remove| 66 |11|Trusted map/list read| 67 68 RPCs1,2,6 require admin. All eight bind JSON `NodeKey` to the registered key of 69 the header NodeID. `/sign` authenticates that caller but resolves the signed 70 target from the signature's embedded node public key; it need not be the caller. 71 Registry and map commands require admin and may use header NodeID0. 72 73 Registry upsert takes `{NodeID,NodeKey,RotationPubkey,Signature?}`. `NodeKey` is 74 `nodekey:`hex and `RotationPubkey` is a32byte base64 Ed25519 public key. IDs and 75 node keys are unique. Optional `Signature` is a base64/byte-array upstream NKS; 76 nonempty signatures require an active authority and valid authorization. A key 77 change clears the old signature. Omitting Signature preserves it only when the 78 key is unchanged. Unsigned nodes may be registered and signed later; this does 79 not itself authorize them to peers. Remove takes `{NodeID}`. Map/list takes `{}` 80 and returns `{TKAInfo,Revision,StoreID,Nodes:[{NodeID,NodePublic,RotationPubkey,KeySignature}]}`. 81 `RotationPubkey` is the stored32byte base64 public key, exposed for adapter reconciliation. 82 Inactive and pending authority produce `TKAInfo:null`; active produces `{Head}`; disabled 83 produces `{Disabled:true}`. Unsigned nodes have `KeySignature:null`. 84 Revision advances after each durable mutation. An exact registry upsert retry (same ID, 85 node key, rotation key and omitted or identical signature) is nonmutating: it preserves the 86 revision and pending initialization, including after restart. This also permits replay of an 87 already accepted historical signature without reauthorizing it under a later authority. 88 `StoreID` is the 128-bit identity of the store, 32 lowercase hex digits, drawn once from the 89 kernel CSPRNG by initialization and never changed afterwards. It names the store and not the 90 path: it survives restarts, an epoch boundary, and a rename of the store directory, and two 91 independent stores never share one. A client that follows a path can therefore tell a 92 replacement apart from the store it was talking to: the directory may be swapped under it and 93 the identity is the only thing that reports it. 94 Adapters must propagate these full values to initial/delta map responses and arrange notifications. 95 No notification transport or Headscale integration is claimed here. 96 97 ## State transitions and durable publication 98 99 Initialization binds a signed pending genesis to the initiating NodeID and the 100 frozen registry. Any actual registry change invalidates pending initialization; an exact 101 upsert retry does not. Finish requires exactly one valid direct signature for every frozen 102 node, including its registered wrapping public key. A different initiator, partial roster, stale 103 roster, invalid signature or invalid support-disablement secret rejects. Optional 104 SupportDisablement is retained separately and never disables the authority. 105 Pending genesis is not visible in map or bootstrap responses. 106 107 Sync uses the authority unit's fork choice, offer sampling and intersection. 108 Interactive sync requires the supplied Head to equal the server's **pre-update** 109 head; mismatch rejects before staging. Stock `TailnetLockModify` sends its current 110 head with newly generated AUMs, then checks that the reply names its last new AUM. 111 Noninteractive sync validates the head encoding but permits a divergent remote 112 head. Disable requires admin, the 113 current head and a valid bounded secret. A disabled authority remains available 114 for bootstrap's disablement secret and cannot be reinitialized in this profile. 115 116 Each mutating request operates on an inactive candidate slot, including registry, 117 signatures, pending state, support/disablement secrets and authority. The complete 118 response is encoded and checked against capacity **before** persistence. A 119 candidate whose registry snapshot would not fit the map response is rejected with 120 413 and never committed, so a successful mutation cannot make the registry 121 unreadable. Submitted node-key signatures are stored in canonical typed form: 122 ignored CBOR fields are dropped before storage, so they cannot pad the snapshot. 123 Only then is a complete snapshot streamed to storage, synced, atomically renamed and 124 the directory synced. A successful commit is followed by an infallible live-slot 125 switch. Pre-rename errors discard the candidate and return503. Post-rename fsync 126 uncertainty exits74 immediately, without rollback or further serving. Exit74 also 127 denotes failed storage cleanup, including during initialization; it means a 128 storage fail-stop, not a definite successful commit. A transport 129 failure after publication remains an inherently ambiguous client outcome; the 130 adapter must reconcile through state reads. 131 132 The semantic snapshot is version1: one128byte little-endian metadata frame, 133 then each node's88byte record and optional signature frame, optional support and 134 disablement secret frames, then each retained typed AUM frame in insertion order. 135 Metadata holds status, node count, pending initiator, secret lengths, revision, 136 authority count, a version marker, the header schema word and the store identity, 137 listed in the word table below. Node records contain ID, signature length, 138 authorization-state index, raw node key and raw rotation key. Storage adds its 139 own framing, version, ordered BLAKE2s integrity chain and strict permission/type 140 checks; see `om/store/README.md`. 141 142 The 128-byte metadata frame is sixteen little-endian 64-bit words: 143 144 |Word|Meaning|Rule| 145 |---|---|---| 146 |0|state status|0 pending init through 3 disabled| 147 |1|registered node count|at most 64| 148 |2|pending initiator NodeID|0 when none, otherwise a registered node| 149 |3|support-disablement secret length|at most 1024| 150 |4|disablement secret length|at most 1024, stored only when disabled| 151 |5|reserved|must be 0| 152 |6|Revision|saturates, never wraps| 153 |7|retained authority count|at most 128| 154 |8|header schema|must be exactly 1| 155 |9,10|store identity, low then high word|128 random bits from init, never both 0| 156 |11..14|reserved|must be 0| 157 |15|marker|must be 1| 158 159 A header whose schema word, identity or reserved words were altered is refused 160 rather than served or repaired. There is no migration from a header without a 161 schema word: no build that writes one has been released. 162 163 AUMs retain typed reconstruction (`tkamodel keep=1`), not merely canonical wire 164 serialization: an empty-but-present update metadata map must survive restart. 165 Recovery reboots/replays and verifies every AUM, registry uniqueness, structural 166 bounds, state status and every stored node signature against the historical 167 state at which it was accepted. This permits retaining a now-revoked signature 168 for affected-sigs/map propagation without treating it as currently authorized. 169 Recovery rechecks support secrets against genesis and disablement secrets against 170 the current authority. Corruption never 171 falls back to a temporary snapshot or an empty installation. 172 173 ## Native loop lifecycle 174 175 Every Omega loop has a finite safety bound. To avoid an eventual request-count 176 trap, a quiescent epoch of100000dispatch iterations ends in a controlled exec of 177 `/proc/self/exe --resume`. The running inode, not an installation pathname or 178 argv0, is used. FD64holds the directory,65the writer lock and66the listener across 179 exec; first-epoch handoff refuses occupied targets. Resume validates inherited 180 descriptors, exact lock/listener addresses, directory/socket ownership and modes, 181 then reloads the committed snapshot. It never binds, initializes or unlinks. 182 Queued connections stay on the same listener and the PID is unchanged. Every 183 accepted connection is closed before rollover. Exec failure exits76 and never 184 retries or unlinks. The native exec operation supplies an empty environment; 185 this service does not depend on environment configuration. Linux procfs and an 186 unchanging effective UID are prerequisites. 187 188 ## Limits and tests 189 190 Single tailnet;64registered nodes;128AUMs per authority;65536bytes per raw JSON 191 request/response, with a4095token/depth32JSON parser and256members per object; individual crypto/model 192 objects at most65536bytes, further constrained by JSON/base64 envelope capacity; 193 secrets at most1024bytes. Response overflow is413before mutation. Resource limits 194 are explicit rejections, not silent truncation or history eviction. Known-field 195 matching follows Go's ASCII case folding plus the Kelvin-sign and long-s Unicode 196 aliases; duplicate aliases are rejected. Exact decoded JSON key duplicates are 197 rejected globally. Signature-map NodeID keys follow Go signed-decimal parsing, 198 including leading plus/zeros, with duplicate decoded IDs rejected and registry 199 IDs restricted to positive int64 values. Unknown fields are ignored. The 200 32byte rotation-key registry requirement and disabled-state reinitialization 201 restriction are explicit local profile limits, not general upstream limits. 202 203 Run `GO=/path/to/go1.26.8 REGEN=1 sh cmd/tkad/test/run.sh` to regenerate fixtures 204 through real pinned `tailscale.com/tka`, build the native binary, and exercise 205 all eight RPCs plus registry/map, authorization errors, malformed frames, 206 initialization races, restart, historical revocation and disk-write failure. 207 Test-only syscall-boundary variants prove one-shot begin/commit failure recovery 208 without process restart and fatal handling of post-rename directory-fsync failure. 209 A two-dispatch epoch variant checks queued connections, stable PID/listener, 210 retained writer exclusion and missing/swapped resume-descriptor refusal. 211 A directory-replacement fixture proves the held descriptor, not the path, is the 212 store: after init, replacing the directory at the same path does not redirect 213 commits, and the replacement stays empty and refuses resume. 214 A completed-response admin-bypass mutant must be detected; a trapping mutant 215 must be rejected as an invalid test completion. Tests use isolated temporary 216 state directories and no live services. These are host/socket regression tests, 217 not stock-client end-to-end or constant-time qualification. 218 219 ### Third-party material 220 221 The service composes Omega units adapted from Tailscale; their BSD-3-Clause 222 notices remain in `om/tka/tailscale.LICENSE` and 223 `om/tkasig/test/tailscale.LICENSE`. The Go fixture generator imports the pinned 224 upstream module and contains no copied upstream source. Its deterministic 225 private test keys are public fixtures, never deployment keys.