kobako 0.31.0 → 0.32.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.githooks/linux-tests.sh +29 -0
- data/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +125 -0
- data/Cargo.lock +457 -494
- data/README.md +2 -3
- data/ROADMAP.md +0 -1
- data/crates/kobako-runtime/CHANGELOG.md +90 -0
- data/crates/kobako-runtime/Cargo.toml +14 -15
- data/crates/kobako-runtime/README.md +1 -1
- data/crates/kobako-runtime/src/cred.rs +32 -20
- data/crates/kobako-runtime/src/filesystem/bind/fid.rs +300 -22
- data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +56 -22
- data/crates/kobako-runtime/src/filesystem/bind/unicode.rs +109 -0
- data/crates/kobako-runtime/src/filesystem/bind/walk.rs +33 -14
- data/crates/kobako-runtime/src/filesystem/bind.rs +393 -220
- data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +2 -0
- data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +9 -10
- data/crates/kobako-runtime/src/filesystem/overlay.rs +277 -46
- data/crates/kobako-runtime/src/filesystem/rdir.rs +8 -27
- data/crates/kobako-runtime/src/filesystem/testing.rs +31 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs.rs +47 -13
- data/crates/kobako-runtime/src/filesystem.rs +2 -0
- data/crates/kobako-runtime/src/fork.rs +239 -16
- data/crates/kobako-runtime/src/limits.rs +17 -41
- data/crates/kobako-runtime/src/locking/seqlock.rs +29 -3
- data/crates/kobako-runtime/src/locking.rs +508 -113
- data/crates/kobako-runtime/src/memcg.rs +83 -160
- data/crates/kobako-runtime/src/mount.rs +0 -30
- data/crates/kobako-runtime/src/net/connections.rs +53 -13
- data/crates/kobako-runtime/src/net/egress.rs +44 -24
- data/crates/kobako-runtime/src/net/http.rs +888 -456
- data/crates/kobako-runtime/src/net/lookup.rs +150 -50
- data/crates/kobako-runtime/src/net/namespace.rs +97 -95
- data/crates/kobako-runtime/src/net/reactor.rs +298 -203
- data/crates/kobako-runtime/src/net/resolv/darwin.rs +8 -3
- data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +50 -0
- data/crates/kobako-runtime/src/net/resolv/stub.rs +29 -23
- data/crates/kobako-runtime/src/net/resolv/windows.rs +14 -44
- data/crates/kobako-runtime/src/net/resolv.rs +39 -15
- data/crates/kobako-runtime/src/net/skbuff.rs +109 -0
- data/crates/kobako-runtime/src/net/socket.rs +51 -52
- data/crates/kobako-runtime/src/net/task.rs +223 -0
- data/crates/kobako-runtime/src/net/tcp.rs +410 -158
- data/crates/kobako-runtime/src/net/tls.rs +65 -3
- data/crates/kobako-runtime/src/net/transport.rs +72 -47
- data/crates/kobako-runtime/src/net/udp.rs +231 -129
- data/crates/kobako-runtime/src/net.rs +3 -0
- data/crates/kobako-runtime/src/sched.rs +244 -58
- data/crates/kobako-runtime/src/security.rs +27 -16
- data/crates/kobako-runtime/src/vfs/dentry.rs +80 -41
- data/crates/kobako-runtime/src/vfs/file.rs +52 -53
- data/crates/kobako-runtime/src/vfs/kern_path.rs +24 -11
- data/crates/kobako-runtime/src/vfs/namei.rs +399 -184
- data/crates/kobako-runtime/src/vfs/namespace.rs +123 -40
- data/crates/kobako-runtime/src/vfs/stat.rs +105 -16
- data/crates/kobako-runtime/src/vfs.rs +1 -0
- data/crates/kobako-runtime/src/wait.rs +75 -50
- data/crates/kobako-transport/CHANGELOG.md +7 -0
- data/crates/kobako-transport/Cargo.toml +1 -1
- data/crates/kobako-transport/README.md +1 -1
- data/crates/kobako-wasmtime/CHANGELOG.md +47 -0
- data/crates/kobako-wasmtime/Cargo.toml +27 -6
- data/crates/kobako-wasmtime/README.md +3 -3
- data/crates/kobako-wasmtime/src/abi.rs +2 -1
- data/crates/kobako-wasmtime/src/ambient.rs +11 -2
- data/crates/kobako-wasmtime/src/cache/unix.rs +95 -0
- data/crates/kobako-wasmtime/src/cache/windows.rs +359 -0
- data/crates/kobako-wasmtime/src/cache.rs +88 -103
- data/crates/kobako-wasmtime/src/driver.rs +40 -27
- data/crates/kobako-wasmtime/src/invocation.rs +21 -7
- data/crates/kobako-wasmtime/src/lib.rs +2 -0
- data/crates/kobako-wasmtime/src/limiter.rs +78 -25
- data/crates/kobako-wasmtime/src/preview1/descriptors.rs +13 -17
- data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +4 -5
- data/crates/kobako-wasmtime/src/preview1/differential.rs +1 -1
- data/crates/kobako-wasmtime/src/preview1/fd.rs +23 -19
- data/crates/kobako-wasmtime/src/preview1/mount_view.rs +130 -197
- data/crates/kobako-wasmtime/src/preview1/path.rs +3 -3
- data/crates/kobako-wasmtime/src/preview1/poll.rs +28 -22
- data/crates/kobako-wasmtime/src/preview1/random.rs +125 -0
- data/crates/kobako-wasmtime/src/preview1/testing.rs +31 -0
- data/crates/kobako-wasmtime/src/preview1.rs +85 -9
- data/crates/kobako-wasmtime/src/preview2/filesystem.rs +2 -2
- data/crates/kobako-wasmtime/src/preview2/host.rs +609 -249
- data/crates/kobako-wasmtime/src/preview2/http.rs +299 -215
- data/crates/kobako-wasmtime/src/preview2/io.rs +17 -4
- data/crates/kobako-wasmtime/src/preview2/table.rs +24 -72
- data/crates/kobako-wasmtime/src/trap.rs +68 -23
- data/crates/kobako-wasmtime/src/waits.rs +59 -0
- data/crates/kobako-wasmtime/src/wasi.rs +91 -10
- data/crates/kobako-wasmtime/witx/wasi_snapshot_preview1.witx +13 -0
- data/data/kobako.wasm +0 -0
- data/ext/kobako/Cargo.toml +6 -2
- data/ext/kobako/src/escape.rs +332 -26
- data/ext/kobako/src/filesystem/grants.rs +5 -7
- data/ext/kobako/src/filesystem/ruby.rs +10 -3
- data/ext/kobako/src/filesystem.rs +47 -8
- data/ext/kobako/src/fork.rs +72 -0
- data/ext/kobako/src/gvl.rs +499 -76
- data/ext/kobako/src/handoff.rs +490 -0
- data/ext/kobako/src/lib.rs +10 -0
- data/ext/kobako/src/network.rs +5 -3
- data/ext/kobako/src/pinned_value.rs +28 -15
- data/ext/kobako/src/runtime/bridge.rs +32 -25
- data/ext/kobako/src/runtime.rs +74 -43
- data/lib/kobako/catalog/handles.rb +9 -5
- data/lib/kobako/codec/decoder.rb +1 -1
- data/lib/kobako/codec/state.rb +2 -18
- data/lib/kobako/codec/value_budget.rb +4 -4
- data/lib/kobako/codec.rb +5 -6
- data/lib/kobako/errors.rb +4 -7
- data/lib/kobako/filesystem.rb +1 -1
- data/lib/kobako/fork.rb +42 -0
- data/lib/kobako/handle.rb +1 -2
- data/lib/kobako/sandbox.rb +17 -36
- data/lib/kobako/sandbox_options.rb +7 -9
- data/lib/kobako/usage.rb +4 -2
- data/lib/kobako/version.rb +1 -1
- data/lib/kobako.rb +2 -2
- data/sig/kobako/catalog/handles.rbs +2 -0
- data/sig/kobako/codec/state.rbs +0 -4
- data/sig/kobako/codec/value_budget.rbs +1 -1
- data/sig/kobako/codec.rbs +2 -2
- data/sig/kobako/errors.rbs +0 -3
- data/sig/kobako/fork.rbs +17 -0
- data/sig/kobako/runtime.rbs +2 -2
- data/sig/kobako/sandbox.rbs +3 -12
- data/sig/kobako/sandbox_options.rbs +0 -2
- metadata +15 -4
- data/lib/kobako/pool.rb +0 -162
- data/sig/core/thread.rbs +0 -6
- data/sig/kobako/pool.rbs +0 -46
|
@@ -1,27 +1,26 @@
|
|
|
1
|
-
//! Which mount stands on which dentry, as Linux's mount
|
|
2
|
-
//! tree `__lookup_mnt` answers from, so a walk reaching a
|
|
3
|
-
//! spelling or through any link steps onto what is mounted
|
|
1
|
+
//! Which mount stands on which dentry beneath one root, as Linux's mount
|
|
2
|
+
//! namespace holds the tree `__lookup_mnt` answers from, so a walk reaching a
|
|
3
|
+
//! mount point by any spelling or through any link steps onto what is mounted
|
|
4
|
+
//! there.
|
|
4
5
|
|
|
5
6
|
use std::sync::Arc;
|
|
6
7
|
|
|
7
8
|
use super::dentry::Dentry;
|
|
8
9
|
use super::mount::VfsMount;
|
|
9
|
-
use super::namei::walk_mountpoint;
|
|
10
|
+
use super::namei::{filename_mkdirat, walk_mountpoint};
|
|
10
11
|
use super::path::Path;
|
|
11
12
|
use crate::cred::Cred;
|
|
12
|
-
use crate::filesystem::FileType;
|
|
13
|
-
use crate::mount::
|
|
13
|
+
use crate::filesystem::{FileType, Filesystem, Tmpfs};
|
|
14
|
+
use crate::mount::UnplacedMountError;
|
|
14
15
|
use crate::Errno;
|
|
15
16
|
|
|
16
17
|
/// One mount standing on a dentry of another, as Linux's `struct mount` keeps
|
|
17
18
|
/// `mnt_parent` and `mnt_mountpoint`. The mount point is held while the mount
|
|
18
|
-
/// stands, so a walk finds that one dentry and no other.
|
|
19
|
-
/// led through are kept too, as each Invocation places it by that path again.
|
|
19
|
+
/// stands, so a walk finds that one dentry and no other.
|
|
20
20
|
struct Mounted {
|
|
21
21
|
parent: Arc<VfsMount>,
|
|
22
22
|
mountpoint: Arc<Dentry>,
|
|
23
23
|
mnt: Arc<VfsMount>,
|
|
24
|
-
trail: Vec<Arc<Dentry>>,
|
|
25
24
|
}
|
|
26
25
|
|
|
27
26
|
impl Drop for Mounted {
|
|
@@ -31,44 +30,54 @@ impl Drop for Mounted {
|
|
|
31
30
|
}
|
|
32
31
|
|
|
33
32
|
/// Where a mount is to stand, as Linux's `pinned_mountpoint` holds the parent
|
|
34
|
-
/// and the mount point
|
|
33
|
+
/// and the mount point.
|
|
35
34
|
struct PinnedMountpoint {
|
|
36
35
|
parent: Arc<VfsMount>,
|
|
37
36
|
mountpoint: Arc<Dentry>,
|
|
38
|
-
trail: Vec<Arc<Dentry>>,
|
|
39
37
|
}
|
|
40
38
|
|
|
41
|
-
/// The mounts one Invocation reaches
|
|
42
|
-
///
|
|
39
|
+
/// The mounts one Invocation reaches, each standing on a dentry beneath one
|
|
40
|
+
/// root, as Linux's `mnt_namespace` holds its `root` and links each mount to
|
|
41
|
+
/// its parent.
|
|
43
42
|
#[derive(Default, Clone)]
|
|
44
43
|
pub(crate) struct MntNamespace {
|
|
44
|
+
root: Option<Arc<VfsMount>>,
|
|
45
45
|
mounted: Vec<Arc<Mounted>>,
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
impl MntNamespace {
|
|
49
|
-
/// The tree the mounts at their absolute guest paths make
|
|
50
|
-
///
|
|
51
|
-
///
|
|
52
|
-
///
|
|
53
|
-
///
|
|
54
|
-
///
|
|
55
|
-
///
|
|
56
|
-
///
|
|
57
|
-
///
|
|
49
|
+
/// The tree the mounts at their absolute guest paths make beneath one
|
|
50
|
+
/// root: the mount at `/`, or else a read-only root holding only the
|
|
51
|
+
/// directories leading to each mount, as a container runtime makes its
|
|
52
|
+
/// mount points; none where no mount is made. Each is placed as Linux's
|
|
53
|
+
/// `mount` places it: its path walked from the root, through the mounts
|
|
54
|
+
/// placed before it, never through a link, as `LOOKUP_NO_SYMLINKS` refuses
|
|
55
|
+
/// one. Shallower paths go first, as a container runtime mounts a parent
|
|
56
|
+
/// before its children, and of two landing where one stands, the later
|
|
57
|
+
/// stands over the earlier and hides it. A mount Linux's `mount` refuses,
|
|
58
|
+
/// its walk failing or landing on no directory, refuses the whole tree, as
|
|
59
|
+
/// a container fails to start.
|
|
58
60
|
pub(crate) fn new<'a>(
|
|
59
61
|
cred: &Arc<Cred>,
|
|
60
62
|
mounts: impl IntoIterator<Item = (&'a str, &'a Arc<VfsMount>)>,
|
|
61
63
|
) -> Result<Self, UnplacedMountError> {
|
|
62
64
|
let mut mounts: Vec<_> = mounts.into_iter().collect();
|
|
63
65
|
mounts.sort_by_key(|(at, _)| at.split('/').filter(|name| !name.is_empty()).count());
|
|
64
|
-
let mut namespace = Self
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
66
|
+
let mut namespace = Self {
|
|
67
|
+
root: match mounts.first() {
|
|
68
|
+
None => None,
|
|
69
|
+
Some(&("/", root)) => Some(Arc::clone(root)),
|
|
70
|
+
Some(_) => Some(init_rootfs(cred, mounts.iter().map(|(at, _)| *at))),
|
|
71
|
+
},
|
|
72
|
+
mounted: Vec::new(),
|
|
73
|
+
};
|
|
74
|
+
let Some(root) = namespace.root.clone() else {
|
|
75
|
+
return Ok(namespace);
|
|
76
|
+
};
|
|
77
|
+
for &(at, mnt) in mounts.iter().filter(|(at, _)| *at != "/") {
|
|
78
|
+
let path = at.trim_start_matches('/');
|
|
70
79
|
let walker = cred.with_mnt_ns(Arc::new(namespace.clone()));
|
|
71
|
-
let root = Path::root(&namespace.topmost_overmount(root));
|
|
80
|
+
let root = Path::root(&namespace.topmost_overmount(&root));
|
|
72
81
|
let pinned = namespace
|
|
73
82
|
.where_to_mount(&walker, &root, path)
|
|
74
83
|
.map_err(|errno| UnplacedMountError {
|
|
@@ -87,7 +96,6 @@ impl MntNamespace {
|
|
|
87
96
|
parent: pinned.parent,
|
|
88
97
|
mountpoint: pinned.mountpoint,
|
|
89
98
|
mnt: Arc::clone(mnt),
|
|
90
|
-
trail: pinned.trail,
|
|
91
99
|
}));
|
|
92
100
|
}
|
|
93
101
|
|
|
@@ -111,10 +119,15 @@ impl MntNamespace {
|
|
|
111
119
|
Ok(PinnedMountpoint {
|
|
112
120
|
parent: nd.mnt.clone(),
|
|
113
121
|
mountpoint: dentry,
|
|
114
|
-
trail: nd.trail,
|
|
115
122
|
})
|
|
116
123
|
}
|
|
117
124
|
|
|
125
|
+
/// The mount every guest path starts from, as `mnt_namespace` holds its
|
|
126
|
+
/// `root`; none where no mount is made.
|
|
127
|
+
pub(crate) fn root(&self) -> Option<&Arc<VfsMount>> {
|
|
128
|
+
self.root.as_ref()
|
|
129
|
+
}
|
|
130
|
+
|
|
118
131
|
/// The mount standing over the root of `mnt`, and over that one's, to the
|
|
119
132
|
/// last, as Linux's `topmost_overmount` follows each `overmount`; `mnt`
|
|
120
133
|
/// itself when none does.
|
|
@@ -140,16 +153,14 @@ impl MntNamespace {
|
|
|
140
153
|
}
|
|
141
154
|
|
|
142
155
|
/// Whether a mount of this namespace pins `dentry`, its path leading
|
|
143
|
-
/// through it: the mount stands on it or beneath it
|
|
144
|
-
///
|
|
145
|
-
///
|
|
146
|
-
///
|
|
147
|
-
/// hides to the next.
|
|
156
|
+
/// through it: the mount stands on it or beneath it. A removal or a move
|
|
157
|
+
/// asks it where Linux asks `is_local_mountpoint`: each Invocation places a
|
|
158
|
+
/// mount again by its path, so a pinned name going would open what the
|
|
159
|
+
/// mount hides to the next.
|
|
148
160
|
pub(crate) fn pins(&self, dentry: &Arc<Dentry>) -> bool {
|
|
149
|
-
self.mounted
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
})
|
|
161
|
+
self.mounted
|
|
162
|
+
.iter()
|
|
163
|
+
.any(|child| child.mountpoint.is_subdir(dentry))
|
|
153
164
|
}
|
|
154
165
|
|
|
155
166
|
/// Whether any mount stands beneath `mnt`, as Linux finds its `mnt_mounts`
|
|
@@ -160,3 +171,75 @@ impl MntNamespace {
|
|
|
160
171
|
.any(|child| Arc::ptr_eq(&child.parent, mnt))
|
|
161
172
|
}
|
|
162
173
|
}
|
|
174
|
+
|
|
175
|
+
/// A read-only root holding the directory at each of `paths` and those above
|
|
176
|
+
/// it, made as `cred`, as Linux's `init_mount_tree` mounts a rootfs and
|
|
177
|
+
/// `init_mkdir` makes the directories a mount stands on. A directory two paths
|
|
178
|
+
/// share is made once.
|
|
179
|
+
fn init_rootfs<'a>(cred: &Arc<Cred>, paths: impl IntoIterator<Item = &'a str>) -> Arc<VfsMount> {
|
|
180
|
+
let sb = Filesystem::new(Tmpfs::new()).expect("an empty Tmpfs stands");
|
|
181
|
+
let made = Path::root(&VfsMount::kern_mount(Arc::clone(&sb)));
|
|
182
|
+
for path in paths {
|
|
183
|
+
let mut above = String::new();
|
|
184
|
+
for name in path.split('/').filter(|name| !name.is_empty()) {
|
|
185
|
+
above.push('/');
|
|
186
|
+
above.push_str(name);
|
|
187
|
+
match filename_mkdirat(cred, &made, above.trim_start_matches('/')) {
|
|
188
|
+
Ok(()) | Err(Errno::AlreadyExists) => {}
|
|
189
|
+
Err(errno) => unreachable!("an empty Tmpfs makes {above}: {errno:?}"),
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
VfsMount::new(sb, true)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
#[cfg(test)]
|
|
197
|
+
mod tests {
|
|
198
|
+
use super::*;
|
|
199
|
+
use crate::grant::Grant;
|
|
200
|
+
use crate::mount::{Access, Mount};
|
|
201
|
+
use crate::security::{prepare_creds, restrict_self};
|
|
202
|
+
use crate::vfs::OpenHow;
|
|
203
|
+
|
|
204
|
+
// A host folding case lands two paths on one directory; two mounts at one
|
|
205
|
+
// path land alike on every host, so the rule is witnessed where names
|
|
206
|
+
// match exactly too.
|
|
207
|
+
// @behavior MT-042
|
|
208
|
+
#[test]
|
|
209
|
+
fn two_mounts_landing_on_one_directory_show_the_later() {
|
|
210
|
+
let tmpfs = |files: &[(&str, &str)]| {
|
|
211
|
+
Filesystem::new(files.iter().copied().collect::<Tmpfs>())
|
|
212
|
+
.expect("the instance must stand")
|
|
213
|
+
};
|
|
214
|
+
let mount = |at: &str, filesystem| {
|
|
215
|
+
Grant::Mount(Mount::new(at, filesystem, Access::Read).expect("a guest path"))
|
|
216
|
+
};
|
|
217
|
+
let grants = [
|
|
218
|
+
mount("/data", tmpfs(&[("real/.keep", "")])),
|
|
219
|
+
mount("/data/real", tmpfs(&[("note", "earlier")])),
|
|
220
|
+
mount("/data/real", tmpfs(&[("note", "later")])),
|
|
221
|
+
];
|
|
222
|
+
let cred = restrict_self(&prepare_creds(&[], None), &grants).expect("every Mount stands");
|
|
223
|
+
let root = cred.root().expect("Mounts make a root");
|
|
224
|
+
let read = OpenHow {
|
|
225
|
+
read: true,
|
|
226
|
+
..Default::default()
|
|
227
|
+
};
|
|
228
|
+
let mut note = crate::vfs::kern_path::filp_open(
|
|
229
|
+
&cred,
|
|
230
|
+
&Path::root(&cred.topmost_overmount(&root)),
|
|
231
|
+
"data/real/note",
|
|
232
|
+
&read,
|
|
233
|
+
)
|
|
234
|
+
.unwrap_or_else(|_| panic!("the note must open"));
|
|
235
|
+
let mut buf = [0; 16];
|
|
236
|
+
let len = note
|
|
237
|
+
.read(&mut buf)
|
|
238
|
+
.unwrap_or_else(|_| panic!("the note must read"));
|
|
239
|
+
assert_eq!(
|
|
240
|
+
&buf[..len],
|
|
241
|
+
b"later",
|
|
242
|
+
"of two Mounts landing on one directory, a walk must reach the later"
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
//! Looking at what stands at a path, as Linux's `fstatat` and
|
|
2
2
|
//! `readlinkat` do.
|
|
3
3
|
|
|
4
|
+
use std::collections::HashMap;
|
|
4
5
|
use std::hash::{BuildHasher, RandomState};
|
|
5
6
|
use std::sync::atomic::{AtomicU64, Ordering};
|
|
6
|
-
use std::sync::{Arc, LazyLock};
|
|
7
|
+
use std::sync::{Arc, LazyLock, Mutex, PoisonError};
|
|
7
8
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
8
9
|
|
|
9
10
|
use super::inode::Identity;
|
|
@@ -160,9 +161,10 @@ pub fn do_readlinkat(cred: &Arc<Cred>, start: &impl Start, path: &str) -> Result
|
|
|
160
161
|
/// keeps one record; a hermetic caller sees every time at the Unix epoch,
|
|
161
162
|
/// where its wall clock stands, so a time a write left behind is no clock it
|
|
162
163
|
/// can read. A link reports no size, so nothing of where it points reaches the
|
|
163
|
-
/// caller, and an entry is told apart only by what `ino_of` answers
|
|
164
|
-
|
|
165
|
-
|
|
164
|
+
/// caller, and an entry is told apart only by what `ino_of` answers, EOVERFLOW
|
|
165
|
+
/// where it has no number left to give.
|
|
166
|
+
pub fn cp_new_stat(cred: &Cred, stat: Kstat) -> Result<Kstat, Errno> {
|
|
167
|
+
let ino = ino_of(cred, Identity::of(&stat))?;
|
|
166
168
|
let masked = match cred.profile() {
|
|
167
169
|
Profile::Permissive => stat,
|
|
168
170
|
Profile::Hermetic => {
|
|
@@ -170,25 +172,59 @@ pub fn cp_new_stat(cred: &Cred, stat: Kstat) -> Kstat {
|
|
|
170
172
|
stat.with_times(epoch(stat.atime), epoch(stat.mtime), epoch(stat.ctime))
|
|
171
173
|
}
|
|
172
174
|
};
|
|
173
|
-
Kstat {
|
|
175
|
+
Ok(Kstat {
|
|
174
176
|
size: match masked.file_type {
|
|
175
177
|
FileType::Symlink => 0,
|
|
176
178
|
_ => masked.size,
|
|
177
179
|
},
|
|
178
180
|
..masked.with_identity(0, ino)
|
|
179
|
-
}
|
|
181
|
+
})
|
|
180
182
|
}
|
|
181
183
|
|
|
182
184
|
/// The inode number the caller `cred` is made for sees for `identity`, in a
|
|
183
|
-
/// stat or a listing. A hermetic caller sees
|
|
184
|
-
/// number
|
|
185
|
-
/// sees a hash of it under a key the process draws
|
|
186
|
-
/// entries apart and cannot work back to a host's inode
|
|
187
|
-
|
|
185
|
+
/// stat or a listing, 0 for an entry naming none. A hermetic caller sees the
|
|
186
|
+
/// number its `InoNamespace` hands the entry, which nothing of the process or
|
|
187
|
+
/// the host decides. Any other sees a hash of it under a key the process draws
|
|
188
|
+
/// once, so it can tell entries apart and cannot work back to a host's inode
|
|
189
|
+
/// number.
|
|
190
|
+
pub fn ino_of(cred: &Cred, identity: Option<Identity>) -> Result<u64, Errno> {
|
|
188
191
|
static KEY: LazyLock<RandomState> = LazyLock::new(RandomState::new);
|
|
189
192
|
match (cred.profile(), identity) {
|
|
190
|
-
(
|
|
191
|
-
(Profile::
|
|
193
|
+
(_, None) => Ok(0),
|
|
194
|
+
(Profile::Hermetic, Some(identity)) => cred.ino_ns().number_of(identity),
|
|
195
|
+
(Profile::Permissive, Some(identity)) => Ok(KEY.hash_one(identity)),
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/// The most inode numbers one `InoNamespace` hands out, as Linux's
|
|
200
|
+
/// `PID_MAX_DEFAULT` bounds the numbers a PID namespace hands its tasks.
|
|
201
|
+
const INO_MAX: usize = 32_768;
|
|
202
|
+
|
|
203
|
+
/// The inode numbers one Invocation's entries are known by, each the next in the
|
|
204
|
+
/// order the Invocation first met it, as a PID namespace numbers its own tasks.
|
|
205
|
+
/// A Filesystem's device number depends on how many were made before it in the
|
|
206
|
+
/// process, and a host's inode number on the host, so neither reaches the
|
|
207
|
+
/// numbering: two runs meeting entries in one order read one numbering.
|
|
208
|
+
#[derive(Default)]
|
|
209
|
+
pub(crate) struct InoNamespace {
|
|
210
|
+
handed: Mutex<HashMap<Identity, u64>>,
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
impl InoNamespace {
|
|
214
|
+
/// The number `identity` was handed, or the next one, EOVERFLOW once
|
|
215
|
+
/// `INO_MAX` are handed, as `cp_new_stat` answers an inode number the
|
|
216
|
+
/// caller cannot hold.
|
|
217
|
+
fn number_of(&self, identity: Identity) -> Result<u64, Errno> {
|
|
218
|
+
let mut handed = self.handed.lock().unwrap_or_else(PoisonError::into_inner);
|
|
219
|
+
if let Some(number) = handed.get(&identity) {
|
|
220
|
+
return Ok(*number);
|
|
221
|
+
}
|
|
222
|
+
if handed.len() >= INO_MAX {
|
|
223
|
+
return Err(Errno::ValueTooLarge);
|
|
224
|
+
}
|
|
225
|
+
let number = handed.len() as u64 + 1;
|
|
226
|
+
handed.insert(identity, number);
|
|
227
|
+
Ok(number)
|
|
192
228
|
}
|
|
193
229
|
}
|
|
194
230
|
|
|
@@ -196,13 +232,19 @@ pub fn ino_of(cred: &Cred, identity: Option<Identity>) -> u64 {
|
|
|
196
232
|
mod tests {
|
|
197
233
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
198
234
|
|
|
199
|
-
use super::{cp_new_stat, ino_of, FileType, Kstat};
|
|
235
|
+
use super::{cp_new_stat, ino_of, FileType, Kstat, INO_MAX};
|
|
200
236
|
use crate::cred::Cred;
|
|
201
237
|
use crate::profile::Profile;
|
|
202
238
|
use crate::vfs::Identity;
|
|
239
|
+
use crate::Errno;
|
|
203
240
|
|
|
204
241
|
fn seen_by(profile: Profile, stat: Kstat) -> Kstat {
|
|
205
242
|
cp_new_stat(&Cred::default().with_profile(profile), stat)
|
|
243
|
+
.expect("a fresh `Cred` has every inode number left to give")
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
fn identity(ino: u64) -> Option<Identity> {
|
|
247
|
+
Identity::from_numbers(7, ino)
|
|
206
248
|
}
|
|
207
249
|
|
|
208
250
|
// The Wasm Driver only encodes this, so another one shows the same, and a
|
|
@@ -219,9 +261,10 @@ mod tests {
|
|
|
219
261
|
);
|
|
220
262
|
assert_eq!(
|
|
221
263
|
(hermetic.size, permissive.size, hermetic.ino, hermetic.atime),
|
|
222
|
-
(0, 0,
|
|
264
|
+
(0, 0, 1, Some(UNIX_EPOCH)),
|
|
223
265
|
"a link's stat through cp_new_stat must report no size on any profile, and on the \
|
|
224
|
-
hermetic profile
|
|
266
|
+
hermetic profile the first number its invocation hands out and every time at the \
|
|
267
|
+
Unix epoch"
|
|
225
268
|
);
|
|
226
269
|
assert!(
|
|
227
270
|
permissive.ino != 0 && permissive.ino != 9 && permissive.atime == reported.atime,
|
|
@@ -265,6 +308,7 @@ mod tests {
|
|
|
265
308
|
ino_of(&permissive, identity),
|
|
266
309
|
"ino_of one identity must answer one number within a process"
|
|
267
310
|
);
|
|
311
|
+
let seen = seen.expect("a permissive `Cred` hashes every identity");
|
|
268
312
|
assert_ne!(
|
|
269
313
|
seen,
|
|
270
314
|
unkeyed.finish(),
|
|
@@ -273,6 +317,51 @@ mod tests {
|
|
|
273
317
|
);
|
|
274
318
|
}
|
|
275
319
|
|
|
320
|
+
// An invocation's numbering is the `Cred` it starts from, so every `Cred`
|
|
321
|
+
// restricted from it numbers one entry alike, and another invocation's
|
|
322
|
+
// starts again from 1.
|
|
323
|
+
// @behavior RT-428
|
|
324
|
+
#[test]
|
|
325
|
+
fn a_hermetic_invocation_numbers_entries_in_the_order_it_meets_them() {
|
|
326
|
+
let hermetic = Cred::default();
|
|
327
|
+
let restricted = hermetic.with_modules(Vec::new());
|
|
328
|
+
let numbers = [9, 3, 9, 5].map(|ino| ino_of(&hermetic, identity(ino)));
|
|
329
|
+
assert_eq!(
|
|
330
|
+
numbers,
|
|
331
|
+
[Ok(1), Ok(2), Ok(1), Ok(3)],
|
|
332
|
+
"a stat on the hermetic profile must give each entry the next inode number the \
|
|
333
|
+
first time the invocation meets it, and that number again after"
|
|
334
|
+
);
|
|
335
|
+
assert_eq!(
|
|
336
|
+
(
|
|
337
|
+
ino_of(&restricted, identity(5)),
|
|
338
|
+
ino_of(&Cred::default(), identity(5))
|
|
339
|
+
),
|
|
340
|
+
(Ok(3), Ok(1)),
|
|
341
|
+
"a `Cred` restricted from a hermetic one must share its numbering, and a fresh \
|
|
342
|
+
invocation's must start from 1"
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// @behavior RT-430
|
|
347
|
+
#[test]
|
|
348
|
+
fn a_hermetic_invocation_past_its_last_number_answers_eoverflow_for_a_new_entry() {
|
|
349
|
+
let hermetic = Cred::default();
|
|
350
|
+
for ino in 1..=INO_MAX as u64 {
|
|
351
|
+
ino_of(&hermetic, identity(ino)).expect("every number up to INO_MAX is handed out");
|
|
352
|
+
}
|
|
353
|
+
assert_eq!(
|
|
354
|
+
(
|
|
355
|
+
ino_of(&hermetic, identity(INO_MAX as u64 + 1)),
|
|
356
|
+
ino_of(&hermetic, identity(1)),
|
|
357
|
+
ino_of(&hermetic, None),
|
|
358
|
+
),
|
|
359
|
+
(Err(Errno::ValueTooLarge), Ok(1), Ok(0)),
|
|
360
|
+
"a stat on the hermetic profile after 32,768 inode numbers were given must answer \
|
|
361
|
+
EOVERFLOW for a new entry, the number of an entry already met, and 0 for none"
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
|
|
276
365
|
// A guest reads the count on any ABI, so the count starts in the runtime
|
|
277
366
|
// rather than in what a Wasm Driver encodes.
|
|
278
367
|
// @behavior RT-193
|
|
@@ -50,6 +50,7 @@ pub use path::{Path, Start};
|
|
|
50
50
|
pub use readdir::{
|
|
51
51
|
dir_emit_dots, generic_read_dir, verify_dirent_name, DirContext, DirEmit, DirEntry,
|
|
52
52
|
};
|
|
53
|
+
pub(crate) use stat::InoNamespace;
|
|
53
54
|
pub use stat::{cp_new_stat, do_readlinkat, get_anon_bdev, ino_of, vfs_fstatat, FileType, Kstat};
|
|
54
55
|
pub(crate) use super_block::MADE_ROOT;
|
|
55
56
|
pub use super_block::{FileSystemType, SuperBlock};
|
|
@@ -110,9 +110,14 @@ impl Drop for PollTable {
|
|
|
110
110
|
}
|
|
111
111
|
|
|
112
112
|
/// Have the poll `table` stands for woken by `queue`, as a file's `->poll`
|
|
113
|
-
/// calls `poll_wait`; a poll that will not sleep passes no table.
|
|
113
|
+
/// calls `poll_wait`; a poll that will not sleep passes no table. A table
|
|
114
|
+
/// stands on a queue once, however many of its sources name it, where Linux
|
|
115
|
+
/// adds an entry for each, so whoever wakes the queue wakes the poll once.
|
|
114
116
|
pub fn poll_wait(queue: &Arc<WaitQueueHead>, table: Option<&mut PollTable>) {
|
|
115
117
|
if let Some(table) = table {
|
|
118
|
+
if table.queues.iter().any(|stood| Arc::ptr_eq(stood, queue)) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
116
121
|
queue.waiters().push(table.waiter.clone());
|
|
117
122
|
table.queues.push(queue.clone());
|
|
118
123
|
}
|
|
@@ -139,6 +144,7 @@ pub fn do_poll<T>(
|
|
|
139
144
|
let mut answer = Ok(None);
|
|
140
145
|
sched::sleep_on(&table.waiter, || {
|
|
141
146
|
sched::schedule(&mut || loop {
|
|
147
|
+
sched::handle_signal();
|
|
142
148
|
if sched::fatal_signal_pending() {
|
|
143
149
|
answer = Err(Errno::Interrupted);
|
|
144
150
|
return;
|
|
@@ -168,7 +174,7 @@ mod tests {
|
|
|
168
174
|
use std::time::Duration;
|
|
169
175
|
|
|
170
176
|
use super::*;
|
|
171
|
-
use crate::sched::testing::{count_waits, join, within_max_wait};
|
|
177
|
+
use crate::sched::testing::{count_waits, join, wait_until, within_max_wait, MAX_WAIT};
|
|
172
178
|
use crate::sched::{run_task, Task};
|
|
173
179
|
|
|
174
180
|
/// An end `after` from now, the same each time it is read.
|
|
@@ -182,38 +188,60 @@ mod tests {
|
|
|
182
188
|
fn a_poll_looks_again_once_the_queue_it_registered_on_wakes() {
|
|
183
189
|
let queue = Arc::new(WaitQueueHead::default());
|
|
184
190
|
let ready = Arc::new(AtomicBool::new(false));
|
|
191
|
+
let waits = count_waits();
|
|
185
192
|
let producer = {
|
|
186
|
-
let (queue, ready) = (queue.clone(), ready.clone());
|
|
193
|
+
let (queue, ready, waits) = (queue.clone(), ready.clone(), waits.clone());
|
|
187
194
|
thread::spawn(move || {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
thread::sleep(Duration::from_millis(50));
|
|
195
|
+
// The poller has asked once and waits through the `Blocking`, so
|
|
196
|
+
// only the wake makes it look again before its run's deadline.
|
|
197
|
+
wait_until("the poll waiting on its queue", || {
|
|
198
|
+
!queue.waiters().is_empty() && waits.load(Ordering::SeqCst) > 0
|
|
199
|
+
});
|
|
194
200
|
ready.store(true, Ordering::SeqCst);
|
|
195
201
|
queue.wake_up();
|
|
196
202
|
})
|
|
197
203
|
};
|
|
198
|
-
let started = Instant::now();
|
|
199
204
|
let answer = within_max_wait(|| {
|
|
200
205
|
do_poll(
|
|
201
206
|
|| None,
|
|
202
207
|
|table| {
|
|
203
208
|
poll_wait(&queue, table);
|
|
204
|
-
|
|
209
|
+
// What the poll finds once its run's deadline passed is
|
|
210
|
+
// what the deadline woke it to, not the wake.
|
|
211
|
+
let woken = !crate::sched::fatal_signal_pending();
|
|
212
|
+
(woken && ready.load(Ordering::SeqCst)).then_some("ready")
|
|
205
213
|
},
|
|
206
214
|
)
|
|
207
215
|
});
|
|
208
|
-
let spent = started.elapsed();
|
|
209
216
|
join("the producer", producer);
|
|
210
217
|
assert!(
|
|
211
|
-
answer == Ok(Some("ready"))
|
|
212
|
-
&& queue.waiters().is_empty()
|
|
213
|
-
&& spent < Duration::from_secs(5),
|
|
218
|
+
answer == Ok(Some("ready")) && queue.waiters().is_empty(),
|
|
214
219
|
"do_poll with no timeout over a queue another thread wakes once its event arrives \
|
|
215
|
-
must answer what the poll then finds
|
|
216
|
-
|
|
220
|
+
must answer what the poll then finds before its run's deadline ends it with \
|
|
221
|
+
EINTR, and leave the queue, got {answer:?}"
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// A guest may list one source many times over, each through a handle of
|
|
226
|
+
// its own, and the reactor wakes every waiter a queue holds on each
|
|
227
|
+
// event, so the wait must stand there once.
|
|
228
|
+
// @behavior RT-427
|
|
229
|
+
#[test]
|
|
230
|
+
fn a_poll_naming_one_queue_many_times_stands_on_it_once() {
|
|
231
|
+
let queue = Arc::new(WaitQueueHead::default());
|
|
232
|
+
let mut standing = None;
|
|
233
|
+
let answer = do_poll(end_after(Duration::from_millis(20)), |table| {
|
|
234
|
+
match table {
|
|
235
|
+
Some(table) => (0..2000).for_each(|_| poll_wait(&queue, Some(&mut *table))),
|
|
236
|
+
None => standing = Some(queue.waiters().len()),
|
|
237
|
+
}
|
|
238
|
+
None::<()>
|
|
239
|
+
});
|
|
240
|
+
assert_eq!(
|
|
241
|
+
(answer, standing, queue.waiters().len()),
|
|
242
|
+
(Ok(None), Some(1), 0),
|
|
243
|
+
"a poll whose 2000 sources all stand on one queue must stand on it once while it \
|
|
244
|
+
sleeps, and leave it as the poll ends"
|
|
217
245
|
);
|
|
218
246
|
}
|
|
219
247
|
|
|
@@ -263,18 +291,17 @@ mod tests {
|
|
|
263
291
|
let queue = Arc::new(WaitQueueHead::default());
|
|
264
292
|
let moved = Arc::new(Mutex::new(None::<Instant>));
|
|
265
293
|
let far = Instant::now() + Duration::from_secs(30);
|
|
294
|
+
let waits = count_waits();
|
|
266
295
|
let waker = {
|
|
267
|
-
let (queue, moved) = (queue.clone(), moved.clone());
|
|
296
|
+
let (queue, moved, waits) = (queue.clone(), moved.clone(), waits.clone());
|
|
268
297
|
thread::spawn(move || {
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
}
|
|
272
|
-
thread::sleep(Duration::from_millis(50));
|
|
298
|
+
wait_until("the poll waiting on its queue", || {
|
|
299
|
+
!queue.waiters().is_empty() && waits.load(Ordering::SeqCst) > 0
|
|
300
|
+
});
|
|
273
301
|
*moved.lock().unwrap() = Some(Instant::now() + Duration::from_millis(20));
|
|
274
302
|
queue.wake_up();
|
|
275
303
|
})
|
|
276
304
|
};
|
|
277
|
-
let started = Instant::now();
|
|
278
305
|
let answer = within_max_wait(|| {
|
|
279
306
|
do_poll(
|
|
280
307
|
|| Some(moved.lock().unwrap().unwrap_or(far)),
|
|
@@ -284,46 +311,44 @@ mod tests {
|
|
|
284
311
|
},
|
|
285
312
|
)
|
|
286
313
|
});
|
|
287
|
-
let spent = started.elapsed();
|
|
288
314
|
join("the waker", waker);
|
|
289
|
-
|
|
290
|
-
answer
|
|
315
|
+
assert_eq!(
|
|
316
|
+
answer,
|
|
317
|
+
Ok(None),
|
|
291
318
|
"do_poll over nothing ready, its 30 s end moved to 20 ms after a wake, must answer \
|
|
292
|
-
None once the moved end passes,
|
|
319
|
+
None once the moved end passes, before its run's deadline ends it with EINTR"
|
|
293
320
|
);
|
|
294
321
|
}
|
|
295
322
|
|
|
296
323
|
// @behavior RT-267
|
|
297
324
|
#[test]
|
|
298
325
|
fn a_poll_ends_with_eintr_once_its_thread_is_interrupted() {
|
|
299
|
-
let
|
|
326
|
+
let polling = Arc::new(Mutex::new(None));
|
|
300
327
|
let poller = {
|
|
301
|
-
let
|
|
328
|
+
let polling = polling.clone();
|
|
302
329
|
thread::spawn(move || {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
waiting.store(true, Ordering::SeqCst);
|
|
309
|
-
None::<()>
|
|
310
|
-
},
|
|
311
|
-
)
|
|
330
|
+
// Past the longest a test waits, so only the interrupt ends
|
|
331
|
+
// the poll before the test gives up on it.
|
|
332
|
+
run_task(Some(Instant::now() + MAX_WAIT * 3), || {
|
|
333
|
+
*polling.lock().unwrap() = Some((Task::current(), count_waits()));
|
|
334
|
+
do_poll(|| None, |_| None::<()>)
|
|
312
335
|
})
|
|
313
336
|
})
|
|
314
337
|
};
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
338
|
+
wait_until("the poll going to sleep", || {
|
|
339
|
+
polling
|
|
340
|
+
.lock()
|
|
341
|
+
.unwrap()
|
|
342
|
+
.as_ref()
|
|
343
|
+
.is_some_and(|(_, waits)| waits.load(Ordering::SeqCst) == 1)
|
|
344
|
+
});
|
|
345
|
+
polling.lock().unwrap().as_ref().unwrap().0.interrupt();
|
|
346
|
+
let answer = join("the interrupted poll ending", poller);
|
|
347
|
+
assert_eq!(
|
|
348
|
+
answer,
|
|
349
|
+
Err(Errno::Interrupted),
|
|
350
|
+
"a poll with no timeout, its thread interrupted as it sleeps, must answer EINTR \
|
|
351
|
+
before its deadline"
|
|
327
352
|
);
|
|
328
353
|
}
|
|
329
354
|
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.22.0](https://github.com/elct9620/kobako/compare/kobako-transport-v0.21.0...kobako-transport-v0.22.0) (2026-11-27)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Miscellaneous Chores
|
|
7
|
+
|
|
8
|
+
* **kobako-transport:** Synchronize kobako crates versions
|
|
9
|
+
|
|
3
10
|
## [0.21.0](https://github.com/elct9620/kobako/compare/kobako-transport-v0.20.0...kobako-transport-v0.21.0) (2026-11-18)
|
|
4
11
|
|
|
5
12
|
|