fix(nesinit): do not mount over the share tree, and check who serves an address

Four findings from review, all of them real.

The relay's directory was mounted on the tree a session's shares live in. A
fresh tmpfs there hides every directory the image prepared underneath it: the
install, the user state, the work directory, and the mount point the log share
is attached to from fstab. A box would have come up with a socket and without
any of the places its workload looks for its files, and the exact-path check
could not notice, because what fstab mounts is a directory inside that tree
rather than the tree itself. It moves to /run, which is where a runtime socket
belongs, is a tmpfs already, and has nothing else mounted inside it.

It was also owned by this process and closed to everyone else, which stopped
the workload traversing it to reach the relay at all. The directory is now
readable and searchable, and still writable by nothing but this process, which
is what makes the socket in it unreplaceable; the socket itself is what the
workload is allowed to connect to. The permission belongs on the socket rather
than on the path.

The address served to a reader was built once at startup and served forever, so
a reader that polls for a better one could only ever get the first. An endpoint
does not know all of its own addresses when it binds: the first is the one that
works on the same network and fails from anywhere else. It is now rebuilt per
read, which is what makes polling for it worth doing.

And the address was taken from whoever held a path in a directory the workload
can write. Workload code could unlink the socket a service was listening on,
bind its own, and every read afterwards would hand the client an address of its
choosing -- a session given to somebody else rather than a session that fails.
The peer's credentials are now checked before a byte is read, from the kernel
rather than from anything the peer says about itself, and an address served by
the workload's own user is refused and said loudly.

That check is only worth something while the workload has a user of its own, so
the image grows one. Two users, and they must stay two: one runs the services
that ship in the image, the other is who a workload runs as. Sharing one does
not weaken the check, it makes every session fail it.

A workload running as root is every user at once and cannot be told apart from
anything; the check stands down there and says so at boot instead, because
refusing root would refuse whatever legitimately serves the address as well.

Also bumps tinyvec by a patch release. It does not build on this toolchain --
`vec` resolves to the module and not the macro -- which made every crate that
depends on an endpoint, including this one, unbuildable. Pre-existing and
nothing to do with this change; the lockfile said the same version before it.
This commit is contained in:
Wanjohi
2026-09-06 18:20:30 +03:00
parent 00a2bdab30
commit f74de9beb8
9 changed files with 460 additions and 66 deletions

View File

@@ -63,14 +63,42 @@ const EARLY: &[Early] = &[
cost: "whatever serves this session's address cannot bind its socket, \
so the session never gets one",
},
// `/run` before anything under it, for the same reason `/proc` comes first:
// a directory cannot be created inside a mount that is not there, and the
// root it would otherwise land on is read-only.
Early {
source: "tmpfs",
target: "/run",
fstype: "tmpfs",
flags: NOSUID_NODEV,
// Octal, and without a leading zero on purpose: the kernel parses a
// tmpfs mode as octal either way, and this is the spelling `mount`
// itself documents.
data: "mode=755",
cost: "there is nowhere for a runtime socket to live, so neither the \
payload relay nor this session's address can be served",
},
// The relay's own directory, and it is deliberately **not** in the tree the
// session's shares live in.
//
// It was, and that was wrong in a way no test here would have caught: a
// fresh tmpfs over the share tree hides every directory the image prepared
// underneath it — the install, the user state, the work directory, and the
// mount point the log share is attached to from `fstab`. The box then has a
// socket and none of the places its workload expects to find its files, and
// the exact-path check below cannot notice, because what `fstab` mounts is
// a directory *inside* that tree rather than the tree itself.
//
// Owned by this process and writable by nothing else, which is what makes
// the socket in it unreplaceable. The workload reaches it because the
// directory is traversable and the socket itself is not restricted; see
// `payload::serve`.
Early {
source: "tmpfs",
target: crate::payload::DIRECTORY,
fstype: "tmpfs",
flags: NOSUID_NODEV | libc::MS_NOEXEC,
// Only this process and the workload it starts, and they are the only
// two that ever have business here.
data: "mode=0770",
data: "mode=755",
cost: "the payload relay cannot bind, so nothing reaches the workload \
over the channel",
},
@@ -198,6 +226,39 @@ mod tests {
assert_eq!(EARLY[0].target, "/proc");
}
/// A mount has to come after whatever it lives inside, or it is a
/// directory created on a read-only root and the mount fails.
#[test]
fn nothing_is_mounted_before_the_mount_it_lives_inside() {
for (i, early) in EARLY.iter().enumerate() {
for other in &EARLY[i + 1..] {
assert!(
!early.target.starts_with(&format!("{}/", other.target)),
"{} is mounted before {}, which contains it",
early.target,
other.target
);
}
}
}
/// **Nothing here may be mounted over the tree the session's shares live
/// in.** A fresh tmpfs there hides every directory the image prepared
/// underneath — the install, the user state, the work directory, and the
/// mount point the log share attaches to — and the exact-path check cannot
/// notice, because what is mounted from `fstab` is a directory inside that
/// tree rather than the tree itself. So a box would come up with a socket
/// and without any of the places its workload looks for its files.
#[test]
fn the_share_tree_is_never_mounted_over() {
for early in EARLY {
assert_ne!(
early.target, "/nestri",
"this hides the directories the image prepared for a session"
);
}
}
/// The relay's directory is the one this cannot hardcode: it belongs to
/// `payload`, and a rename there that missed this file would take the
/// relay down again in exactly the way this exists to prevent.