# Network objects

---

# Network objects

_Peios Kernel / Internals / KACS / Network objects_

> Find the Peios kernel articles about network objects.

Use these articles to understand network objects in the Peios kernel.

- [Port Reservations](</kernel/internals/kacs/network-objects/port-reservations.md>)
- [Socket Identity](</kernel/internals/kacs/network-objects/socket-identity.md>)

---

# Port Reservations

_Peios Kernel / Internals / KACS / Network objects_

> Binding a TCP or UDP port is an access check against a reservation — a security descriptor keyed by protocol and port range, read from the registry — not a privilege test against a port number.

<a id="trm-3-12-1"></a>

Ports are a shared, unowned namespace. Linux protects it with one rule:
`bind(2)` below port 1024 needs `CAP_NET_BIND_SERVICE`, and everything
above is anyone's. That is too coarse in one direction — any privileged
process gets every low port — and absent in the other: nothing protects
8080, 3306 or 5432 from whichever process asks first.

On Peios the only thing worth authorising about a port is the **claim**
— binding a non-zero port — and it is authorised the way everything
else is: by a security descriptor. `CAP_NET_BIND_SERVICE` is in the
always-allow set ([DAC Neutralisation](</kernel/internals/kacs/linux-credentials/dac-neutralisation.md#trm-3-10-2>)), so the Linux floor never refuses first, and
every claim reaches `security_socket_bind()`, where KACS decides. <a id="cite-net.port.floor-never-refuses"></a>

The precedent is not Linux but HTTP.sys's URL reservations on Windows:
a sparse table of namespace prefixes, each carrying a descriptor,
consulted when a name is claimed, default-permissive where nothing is
reserved. This chapter moves that model down to the port layer.

<a id="trm-3-12-1-1"></a>

## The object

A **reservation** is a selector mapped to a descriptor. The selector
names a set of protocols and an inclusive port range:

```
<proto>[,<proto>]:<lo>[-<hi>]      proto ∈ tcp | udp | *
```

`tcp:80`, `udp:53`, `tcp,udp:1-1023`, `*:8080`. Ports are decimal,
1–65535, no leading zeros, `lo <= hi`; a protocol may not repeat, so
every selector has one spelling. <a id="cite-net.port.selector-grammar"></a> The address family is deliberately not
part of the selector: a reservation covers IPv4 and IPv6 alike, or
dual-stack squatting — claiming the v6 side of a port reserved on v4 —
would bypass it. <a id="cite-net.port.family-agnostic"></a>

The rights are those of `<pkm/net.h>`:

| Right | Grants |
|---|---|
| `KACS_PORT_BIND` (`0x00000001`) | `bind(2)` to any port the selector contains |
| `READ_CONTROL` | reading the descriptor |

That is the whole mask. Editing a reservation is a registry write,
governed by the registry key's own descriptor — one edit policy for the
table — so `WRITE_DAC` and `WRITE_OWNER` inside a port descriptor mean
nothing and are never honoured. The generic mapping is `GENERIC_EXECUTE`
→ `PORT_BIND`, `GENERIC_READ` → `READ_CONTROL`, `GENERIC_WRITE` → nothing,
`GENERIC_ALL` → both. <a id="cite-net.port.generic-mapping"></a>

<a id="trm-3-12-1-2"></a>

## The table

Reservations live as **values** under one registry key:

```
Machine\System\Network\TcpIp\PortReservations\
    @                 REG_BINARY   SD     the default reservation
    tcp,udp:1-1023    REG_BINARY   SD
    tcp:80            REG_BINARY   SD
```

Each value's name is its selector and its data a self-relative
descriptor. The key's unnamed default value, `@`, is the **default
reservation**: the descriptor for every port no selector contains. <a id="cite-net.port.default-reservation"></a> The
shipped seed grants `PORT_BIND` to Everyone on `@` — an unreserved port
is anyone's, as on Windows — and to SYSTEM and the `bind-low-ports`
capability SID on `tcp,udp:1-1023`, which is the Linux convention
restated as policy. <a id="cite-net.port.shipped-seed"></a> Services then carry their own entries:
`tcp:80,443 → NT SERVICE\httpd: PORT_BIND`, which composes with
per-service SIDs so that a compromised `sshd` cannot take 443 even
though both are "privileged".

The kernel reads the key whole and **rejects it whole**. Every name must
parse, every descriptor must parse, exactly one `@` must exist, and no
two selectors of equal width may overlap — the most specific match
would be ambiguous, and the kernel refuses to guess. <a id="cite-net.port.table-all-or-nothing"></a> On any failure the
load is audited and the previous table stays in force. <a id="cite-net.port.rejected-load-keeps-previous"></a>
The rejection is recorded as `kacs.config.value.rejected` ([Audit Event Schemas](</kernel/internals/kacs/auditing/event-families-and-delivery.md#trm-3-c>)):
`config.key.path` names the key, `config.name` the value at fault where
one is, and `outcome.reason` why — `empty`, `too-large`, `bad-selector`,
`bad-descriptor`, `duplicate-default`, `missing-default` or `overlap` —
with `policy.previous-retained` true and `policy.fallback` saying whether
what stayed in force is the compiled-in table. <a id="cite-net.port.rejected-load-recorded"></a> Nested overlap,
and overlap between selectors of different widths, is how specificity
works and is accepted. <a id="cite-net.port.nested-overlap-accepted"></a>

<a id="trm-3-12-1-3"></a>

## The decision

At `bind(2)` on an `AF_INET` or `AF_INET6` socket whose protocol is TCP,
UDP or UDP-Lite:

1. If the requested port is 0 — ephemeral allocation — nothing is
   checked. Protocols no reservation covers (SCTP, raw) pass untouched. <a id="cite-net.port.ephemeral-unchecked"></a>
2. The **most specific** reservation containing `(protocol, port)` is
   selected: the one covering the fewest ports, regardless of how many
   protocols it names. None → the default reservation. <a id="cite-net.port.most-specific-match"></a>
3. The caller's effective token is evaluated against that descriptor
   for `PORT_BIND`, through the ordinary AccessCheck pipeline ([AccessCheck Overview](</kernel/internals/kacs/access-check/accesscheck-overview.md#trm-3-8-1>))
   with PIP context, privilege-use marking and audit events — a port
   descriptor may carry a SACL, and its decisions surface through the
   same KMES stream as any other object's, with `object.kind`
   `socket`. <a id="cite-net.port.access-check"></a>
4. Deny → `-EACCES`. Grant → the bind proceeds, and the caller's token is
   recorded on the socket as its **binder**. <a id="cite-net.port.deny-eacces"></a>

An explicit `bind(2)` before an outbound `connect(2)` meets the same
rule; it is not special-cased.

**Rebinding.** Binding onto a port that is already bound — with
`SO_REUSEADDR` or `SO_REUSEPORT` — is still a `bind(2)` and still meets
the reservation. <a id="cite-net.port.rebind-meets-reservation"></a> The rule beyond that is that the new binder's user SID
must equal the existing binder's, or the caller must hold
`SeTcbPrivilege`: rebind is a property of the current binding, not of
policy, and a right on the reservation that let one principal rebind
onto another's port is a thing nothing legitimately needs. <a id="cite-net.port.rebind-same-sid-or-tcb"></a> The binder
recorded on each socket is what makes that comparison local; the
comparison itself is applied where the stack resolves bind conflicts,
and Linux already applies the same-owner half of it for `SO_REUSEPORT`
by comparing the sockets' projected UIDs.

<a id="trm-3-12-1-4"></a>

## Before the registry

The table comes from the registry through the same self-configuration
path LCS uses for its own limits ([The Bootstrap Problem](</kernel/internals/lcs/bootstrap/dependencies-and-boot-sequence.md#trm-5-10-1>)): read at the first source's
registration, then watched, so a change to the key is live without a
restart. <a id="cite-net.port.table-watched-live"></a> Until the first successful load — early boot, or a registry
that never becomes available — a **compiled-in fallback** answers:
owner and group SYSTEM, one entry granting SYSTEM `PORT_BIND`, nobody
else. <a id="cite-net.port.compiled-in-fallback"></a> It is stricter than the shipped seed on purpose: nothing but
system components should be claiming ports before the registry exists.

The fallback is **replaced, never merged**. The moment the key loads it
is the sole authority; if a later load is rejected the last good table
stays, not the fallback. <a id="cite-net.port.fallback-replaced-not-merged"></a> Effective policy therefore never depends on
boot timing.

<a id="trm-3-12-1-5"></a>

## What this retired

`SeBindPrivilegedPortPrivilege` (bit 63) was the KACS privilege
`CAP_NET_BIND_SERVICE` mapped to before this object existed. A
reservation's grant is an ACE, and an ACE names a SID, not a privilege;
keeping the privilege would have been a second authorisation path
around the descriptor. It is retired and its bit is not reused. <a id="cite-net.port.privilege-retired"></a> Low
ports are granted through the capability SID on the `1-1023`
reservation, which reaches a token by confinement — peinit for a
service, or positive confinement from the authority at logon for a
user.

<a id="trm-3-12-1-6"></a>

## Tracing <a id="cite-net.port.tracing"></a>

Every decision emits `kacs:kacs_socket_bind` with reason `port-bind`:
`desired` carries `PORT_BIND`, `max_imp` carries the port, and `ret` the
verdict. Table loads emit reason `port-table`. See Appendix 3.C for the audit
events an evaluated descriptor's SACL can produce.

---

# Socket Identity

_Peios Kernel / Internals / KACS / Network objects_

> The governing identity KACS stamps on every inet socket for network policy — which token, taken when, and how the engine reads it.

<a id="trm-3-12-2"></a>

Network policy speaks of programs, and a packet carries no program. What
carries one is the socket: KACS records on every `AF_INET` and
`AF_INET6` socket the **identity that governs its traffic**, and the
network policy engine (Chapter 6, [The identity facts](</kernel/internals/ntfe/the-identity-facts.md#trm-6-9>)) reads it at the first judgment
of each flow. <a id="cite-net.socket.stamp-on-inet-sockets"></a> AF_UNIX sockets carry the peer-identity machinery of
[Impersonation Levels](</kernel/internals/kacs/impersonation/levels-and-admission.md#trm-3-5-1>) instead and are not stamped.

<a id="trm-3-12-2-1"></a>

## The stamp

The stamp is the caller's **effective token** — the impersonation token
during impersonation, the primary otherwise — held by a counted
reference, together with the process facts of that moment: the process
GUID ([PSB Fields](</kernel/internals/kacs/process-security/psb-fields.md#trm-3-3-2>)), the thread-group id, and the task's `comm`. <a id="cite-net.socket.stamp-contents"></a> Choosing the
effective token means a service thread acting for a client attributes
the socket to the client, exactly as file creation and audit do under
impersonation; the service's own identity is then not on the socket for
that flow.

It is taken at every act that commits the socket to a role, and the
last one governs: <a id="cite-net.socket.last-act-governs"></a>

| Act | Where |
|---|---|
| creation | `socket_post_create`; a kernel socket (`sock_create_kern`) is stamped as the **kernel's**, with no token <a id="cite-net.socket.kernel-socket-stamp"></a> |
| `bind(2)` | after the port reservation permits it ([Port Reservations](</kernel/internals/kacs/network-objects/port-reservations.md#trm-3-12-1>)) |
| `listen(2)` | `socket_listen` |
| `connect(2)` | `socket_connect` |
| `accept(2)` | `sk_clone_security`: the child inherits the listener's stamp, its own reference <a id="cite-net.socket.accept-inherits"></a> |
| `KACS_SO_RESTAMP` | any state: the caller's effective identity replaces the stamp |

Restamping is what makes a hand-off well defined. A listener opened by
one program and passed to another — socket activation, a descriptor
store, a broker — is governed by whichever program last committed it,
and a program that receives a socket it did not create can say so
explicitly with `KACS_SO_RESTAMP`, self-gated like every attestation of
one's own identity. <a id="cite-net.socket.restamp-self-gated"></a> Sockets that never pass through these hooks (a task
with no token in early boot) read as *unstamped*, which the engine
treats as the kernel's and confesses. <a id="cite-net.socket.unstamped-reads-kernel"></a>

<a id="trm-3-12-2-2"></a>

## Reading it

The engine reads the stamp with `pkm_kacs_socket_owner()`
(`<linux/peios_ntfe.h>`): a counted token reference the caller releases
with `pkm_kacs_socket_owner_put()`, and a copy of the kind, GUID, pid and
comm. <a id="cite-net.socket.owner-accessor"></a> The copy is what makes the facts outlive the process; the
reference is what makes them outlive the socket — a flow keeps its
identity for its life ([The identity facts](</kernel/internals/ntfe/the-identity-facts.md#trm-6-9>)) even after the socket that started it is
gone. The fields live under their own lock, taken with bottom halves
disabled, because both the accept path and the engine's read run in
softirq context.

<a id="trm-3-12-2-3"></a>

## Final release

Once the socket's last user is gone, KACS releases the token references
and security descriptor owned by the socket. This also releases any
peer, listener, binder and send-cache references; each field owns its
own reference even when several refer to the same token. Final cleanup
can run from an RCU callback and must not sleep or acquire the locks
used to coordinate live socket users. It has exclusive ownership at
this point. References already taken by a flow or another reader remain
valid until that reader releases them. <a id="cite-net.socket.final-release-rcu-safe"></a>

<a id="trm-3-12-2-4"></a>

## Tracing <a id="cite-net.socket.tracing"></a>

Every stamp emits `kacs:kacs_socket_token` with reason `owner`; the
`max_imp` field carries the kind (1 program, 2 kernel). See Appendix
3.C.
