From 71876b8b4901327aec9fbad8b9be060160476a58 Mon Sep 17 00:00:00 2001 From: japabu Date: Thu, 12 Nov 2026 07:18:32 +0200 Subject: [PATCH 1/2] kernel: a sent handle of another kind is InvalidArgument whatever its rights `HandleTable::get_sent`, the lookup of a handle a peer sent (`SYS_CONNECT`'s connector, `SYS_SPAWN`'s place and job), checked the handle's rights before its kind, so a handle of another kind that also lacked the right answered `PermissionDenied`. Its own contract, `connect`'s and the spawn fields' ABI docs all answer such a handle `InvalidArgument`, and a right means something only on its own kind. `connect` through an acceptor, which carries no `WRITE`, is the case `abuse_listener_hijack` holds, and it went red in `testcases_members_exit_0` once #1252 made connecting need `WRITE`. The kind is now checked first. A place or a job of another kind without `WRITE` answers `InvalidArgument` too, as their docs already say. `WrongType`'s doc says `get_sent` never builds it, and `process_tree`'s `a_pipe_is_no_place` no longer explains its refusal by the old order. Co-Authored-By: Claude Opus 5.5 --- kernel/src/object/handle.rs | 12 +++++++----- tests/guest/rust-tests/src/bin/process_tree.rs | 5 ++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/kernel/src/object/handle.rs b/kernel/src/object/handle.rs index 7ea3944a1b..afc92fab4d 100644 --- a/kernel/src/object/handle.rs +++ b/kernel/src/object/handle.rs @@ -41,7 +41,7 @@ pub enum HandleError { BadHandle, /// The slot has moved past this handle: closed, or its generations ran out. Stale, - /// Ends the caller like the two above, except through [`HandleTable::get_sent`], the one lookup of a handle a peer sent — `SYS_CONNECT`'s connector and `SYS_SPAWN`'s place and job — where it is `InvalidArgument`. + /// Ends the caller like the two above. [`HandleTable::get_sent`], the one lookup of a handle a peer sent (`SYS_CONNECT`'s connector and `SYS_SPAWN`'s place and job), answers a wrong type `InvalidArgument` and never builds this. WrongType { held: &'static str, wanted: &'static str }, /// The handle is fine and does not carry what the call needs. Rights { held: Rights, needed: Rights }, @@ -318,16 +318,18 @@ impl HandleTable { .ok_or(HandleError::WrongType { held: entry.object.kind(), wanted: T::NAME }) } - /// [`get`](Self::get) for a handle a peer sent, whose type is that peer's claim rather than proof of a bug in this caller: a wrong type refuses `InvalidArgument`, and every other failure is `get`'s. + /// [`get`](Self::get) for a handle a peer sent, whose type is that peer's claim rather than proof of a bug in this caller: a wrong type refuses `InvalidArgument` whatever rights the handle carries, since a right means something only on its kind, and every other failure is `get`'s. pub fn get_sent( &self, h: RawHandle, need: Rights, ) -> Result, Refusal> { - match self.get::(h, need) { - Err(HandleError::WrongType { .. }) => Err(SyscallError::InvalidArgument.into()), - other => other.map_err(Refusal::from), + let entry = self.entry_of(h)?; + let object = T::from_ref(&entry.object).ok_or(SyscallError::InvalidArgument)?; + if !entry.rights.contains(need) { + return Err(HandleError::Rights { held: entry.rights, needed: need }.into()); } + Ok(object.clone()) } /// The borrowing accessor, for a call that completes while still holding the guard. diff --git a/tests/guest/rust-tests/src/bin/process_tree.rs b/tests/guest/rust-tests/src/bin/process_tree.rs index ca5245bec3..a43e5d573f 100644 --- a/tests/guest/rust-tests/src/bin/process_tree.rs +++ b/tests/guest/rust-tests/src/bin/process_tree.rs @@ -274,9 +274,8 @@ fn a_manage_only_handle_is_no_place() { println!(" a MANAGE-only handle is no place: PermissionDenied"); } -/// A pipe's write end carries `WRITE`, so the kernel reaches the type it is -/// not: the supervisor answers the launch refused, and the arms after this one are the supervisor -/// answering the next. +/// A pipe's write end is no place: the supervisor answers the launch refused, +/// and the arms after this one are the supervisor answering the next. fn a_pipe_is_no_place() { let (_read, write) = toyos::pipe_pair().expect("a pipe of our own"); let place = write.as_handle().try_clone_to_owned().expect("a duplicate to send"); From 8daef7f509239634558dcbf52cccea95560e727c Mon Sep 17 00:00:00 2001 From: japabu Date: Thu, 12 Nov 2026 07:18:32 +0200 Subject: [PATCH 2/2] adopted_services_stay checks only what its own copy of toyos holds The adopter, which std's `adopt_services` gives `fs:/system`, then checked that `toyos::endow::services()` listed it. std links its own copy of `toyos`, and each copy keeps its own adopted services: std's adoption filled std's copy, and the program's listing read its own, which adopted nothing. So the check went red in `testcases_members_exit_0` (run 43156966054, `assert_failed::, [&str; 1]>`). - The adopter still checks, through its own copy, that it was endowed no service: that copy never adopts, so the check holds at any point. The check after the adoption goes. The helper's spawn proves the adoption, since std reads the helper's image through `/system`, which std holds only by adopting it. - std's `adopt_services` says what ends its adoption: std's own resolution of a file or lookup of a service. - `issues/a-programs-own-toyos-never-sees-the-services-std-adopted.md` records the two adopted sets, the defect #1226 closed for stdio's state. - The owed-on-metal record for #1252 says `testcases_members_exit_0` runs the members #1252 adapted on QEMU. Co-Authored-By: Claude Opus 5.5 --- ...yos-never-sees-the-services-std-adopted.md | 37 +++++++++++++++++++ ...-small-interrupts-post-and-threads-wait.md | 9 ++--- sdk/std/os/fs.rs | 4 +- .../src/bin/adopted_services_stay.rs | 4 +- 4 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 issues/a-programs-own-toyos-never-sees-the-services-std-adopted.md diff --git a/issues/a-programs-own-toyos-never-sees-the-services-std-adopted.md b/issues/a-programs-own-toyos-never-sees-the-services-std-adopted.md new file mode 100644 index 0000000000..75e2ee7937 --- /dev/null +++ b/issues/a-programs-own-toyos-never-sees-the-services-std-adopted.md @@ -0,0 +1,37 @@ +--- +status: open +kind: defect +opened: 2026-11-12 +--- + +# A program's own `toyos` never sees the services std adopted + +std links a copy of `toyos` of its own (`sdk/toyos/src/lib.rs`, `std_io`), +and a program that uses the SDK links another. Each copy keeps its own +adopted services (`toyos::endow::adopt`, `ADOPTED` in +`sdk/toyos/src/endow.rs`), though `adopt`'s contract speaks of "this +process": +- std's `adopt_services` (`sdk/std/os/fs.rs`) fills std's copy, which std's + file resolution and spawns read. +- The program's `toyos::endow::services` and `connector` read its own copy, + which never adopted anything. Its `adopt` fills a set std never reads. + +Seen in `adopted_services_stay` (guest run 43156966054, main 302fb3698): the +adopter's `services()` listed nothing after std had adopted `fs:/system`, and +the check on it went red; #1280 removed that check. The supervisor is the +one adopter today and keeps its own connectors, so nothing else reads the +wrong copy yet. + +This is the defect #1226 closed for the stdio stream state, which each copy +kept for itself. That state now lives once, in the copy that owns the +descriptor table (the `fd-table` feature: std's in a Rust program, libc's in +a C one), which defines `toyos_stdio_*` beside `toyos_fd_*`, and every copy +reaches it through those. + +**Owner**: the orchestrator, which assigns the fix after #1227 lands. + +**Exit**: one adopted set per process, held by the copy that owns the +descriptor table and reached by every copy through it, as #1226's stdio state +is. A program's `toyos::endow::services` and `connector` then see what std +adopted, and an adoption through either copy is the one std resolves files +through. diff --git a/issues/the-kernel-is-small-interrupts-post-and-threads-wait.md b/issues/the-kernel-is-small-interrupts-post-and-threads-wait.md index f5e9a97004..0a5c04e86d 100644 --- a/issues/the-kernel-is-small-interrupts-post-and-threads-wait.md +++ b/issues/the-kernel-is-small-interrupts-post-and-threads-wait.md @@ -416,11 +416,10 @@ By mechanisms eliminated: connect leaves the caller's table as it was), `abuse_listener_hijack`, `user_copy_faults`, `adopted_services_stay`, `handle_lifetime`, `process_tree` and `launch_toctou` among them; - - the `testcases` members #1252 adapted, which have only been compiled - in that form, passing: `poll_belongs_to_its_handle` (#1261's, which - connects through its connector), `messages_are_whole`, `moved_rights` - and `mutual_kill`. The shared-boot guest row #1271 adds runs them - too, once it lands; + - the `testcases` members #1252 adapted passing on metal: + `poll_belongs_to_its_handle` (#1261's, which connects through its + connector), `messages_are_whole`, `moved_rights` and `mutual_kill`. + On QEMU, `testcases_members_exit_0` runs them; - no kernel panic from the slot-order assertions; - `spawn_cost` within its guard. - **Owed on metal:** the spawn A/B of main before #1252 against main diff --git a/sdk/std/os/fs.rs b/sdk/std/os/fs.rs index 0b4c04100e..3170630fc7 100644 --- a/sdk/std/os/fs.rs +++ b/sdk/std/os/fs.rs @@ -69,8 +69,8 @@ pub fn symlink, Q: AsRef>(original: P, link: Q) -> io::Resu /// /// For the one process no parent endows a service: the supervisor makes the /// machine's directory capabilities itself. No child inherits them. Refused, -/// and the connectors closed, when this process has adopted some already or -/// has resolved a file. +/// and the connectors closed, when this process has adopted some already, or +/// once std has resolved a file or looked up a service of its own. /// /// # Safety /// Each handle names a connector: it is taken as one on the caller's word. diff --git a/tests/guest/rust-tests/src/bin/adopted_services_stay.rs b/tests/guest/rust-tests/src/bin/adopted_services_stay.rs index 5b600f8007..7d047e0edf 100644 --- a/tests/guest/rust-tests/src/bin/adopted_services_stay.rs +++ b/tests/guest/rust-tests/src/bin/adopted_services_stay.rs @@ -55,8 +55,8 @@ fn adopter() { let system: Connector = Endowments::get().take(GIVEN).expect("the adopter is given /system"); // SAFETY: a connector, as this process was endowed it. unsafe { std::os::toyos::fs::adopt_services(vec![(SYSTEM.to_string(), system.into())]) }.expect("the adopter adopts /system"); - // Non-vacuity: the adopter holds it, and its spawn reads the helper's image through it. - assert_eq!(toyos::endow::services().collect::>(), [SYSTEM], "what the adopter holds"); + // Non-vacuity: std reads the helper's image through `/system`, which it + // holds only by adopting it. let status = Command::new(SELF).arg("helper").status().expect("spawn the helper through the adopted /system"); assert_eq!(status.code(), Some(HELD_NONE), "the helper held a service: {status:?}"); }