microsandbox-protocol 0.6.10

Wire protocol types and serialization for the microsandbox project.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! Typed host-to-guest configuration delivered before agent initialization.

use std::net::{Ipv4Addr, Ipv6Addr};

use serde::{Deserialize, Serialize};

use crate::exec::ExecRlimit;

//--------------------------------------------------------------------------------------------------
// Types
//--------------------------------------------------------------------------------------------------

/// Complete one-shot configuration consumed by agentd during guest boot.
///
/// The runtime preloads this payload into the agent console before the VM
/// starts. The surrounding protocol envelope supplies the schema generation.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct GuestBootstrap {
    /// Block-backed root filesystem assembly, when required.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub block_root: Option<BootstrapBlockRoot>,

    /// Virtiofs directory mounts installed inside the guest.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub dir_mounts: Vec<BootstrapDirMount>,

    /// Virtiofs file mounts installed inside the guest.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub file_mounts: Vec<BootstrapFileMount>,

    /// Additional block-device mounts installed inside the guest.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub disk_mounts: Vec<BootstrapDiskMount>,

    /// Tmpfs mounts installed inside the guest.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tmpfs_mounts: Vec<BootstrapTmpfsMount>,

    /// Guest hostname.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub hostname: Option<String>,

    /// Host alias written into the guest's hosts file.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub host_alias: Option<String>,

    /// Guest network interface and address configuration.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub network: Option<BootstrapNetwork>,

    /// Sandbox-wide resource limits inherited by guest workloads.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub rlimits: Vec<ExecRlimit>,

    /// Default guest user for command execution.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub user: Option<String>,

    /// Default working directory for requests that omit one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default_cwd: Option<String>,

    /// Environment inherited by requests that do not override a key.
    ///
    /// Secret entries contain guest-visible placeholders, never host secret
    /// values. Explicit exec and handoff environment entries take precedence.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub default_env: Vec<BootstrapEnvVar>,

    /// In-guest security policy.
    #[serde(default)]
    pub security_profile: BootstrapSecurityProfile,

    /// Optional PID 1 handoff after agentd finishes guest initialization.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub handoff_init: Option<BootstrapHandoffInit>,
}

/// Block-backed root filesystem configuration.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum BootstrapBlockRoot {
    /// A single filesystem image mounted as the guest root.
    DiskImage {
        /// Guest block-device path.
        device: String,

        /// Filesystem type, or `None` to probe inside the guest.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        fstype: Option<String>,
    },

    /// An EROFS lower filesystem combined with a writable overlay upper.
    OciErofs {
        /// Read-only EROFS block-device path.
        lower: String,

        /// Writable overlay backing.
        upper: BootstrapBlockRootUpper,
    },
}

/// Writable backing for an OCI EROFS root.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum BootstrapBlockRootUpper {
    /// Writable filesystem supplied by a guest block device.
    Device {
        /// Guest block-device path.
        device: String,

        /// Filesystem type on the device.
        fstype: String,
    },

    /// RAM-backed writable upper.
    Tmpfs {
        /// Optional maximum size in MiB.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        size_mib: Option<u32>,
    },
}

/// Common guest mount flags.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct BootstrapMountFlags {
    /// Mount read-only.
    #[serde(default)]
    pub readonly: bool,

    /// Disallow execution from the mount.
    #[serde(default)]
    pub noexec: bool,

    /// Ignore set-user-ID and set-group-ID bits.
    #[serde(default)]
    pub nosuid: bool,

    /// Disallow device nodes.
    #[serde(default)]
    pub nodev: bool,
}

/// Guest-side virtiofs directory mount.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BootstrapDirMount {
    /// Virtiofs device tag.
    pub tag: String,

    /// Absolute guest mount path.
    pub guest_path: String,

    /// Guest mount flags.
    #[serde(default)]
    pub flags: BootstrapMountFlags,
}

/// Guest-side virtiofs file mount.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BootstrapFileMount {
    /// Virtiofs device tag.
    pub tag: String,

    /// Filename inside the staged virtiofs directory.
    pub filename: String,

    /// Absolute guest file path.
    pub guest_path: String,

    /// Guest mount flags.
    #[serde(default)]
    pub flags: BootstrapMountFlags,
}

/// Guest-side block-device mount.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BootstrapDiskMount {
    /// Virtio block-device identifier.
    pub id: String,

    /// Absolute guest mount path.
    pub guest_path: String,

    /// Filesystem type, or `None` to probe inside the guest.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub fstype: Option<String>,

    /// Guest mount flags.
    #[serde(default)]
    pub flags: BootstrapMountFlags,
}

/// Guest-side tmpfs mount.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BootstrapTmpfsMount {
    /// Absolute guest mount path.
    pub path: String,

    /// Optional maximum size in MiB.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub size_mib: Option<u32>,

    /// Optional Unix mode applied to the tmpfs root.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mode: Option<u32>,

    /// Guest mount flags.
    #[serde(default)]
    pub flags: BootstrapMountFlags,
}

/// Guest network interface and address configuration.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BootstrapNetwork {
    /// Guest interface name.
    pub interface: String,

    /// Guest interface MAC address.
    pub mac: [u8; 6],

    /// Guest interface MTU.
    pub mtu: u16,

    /// IPv4 address configuration when IPv4 is active.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ipv4: Option<BootstrapIpv4>,

    /// IPv6 address configuration when IPv6 is active.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ipv6: Option<BootstrapIpv6>,
}

/// Guest IPv4 address configuration.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct BootstrapIpv4 {
    /// Guest IPv4 address.
    pub address: Ipv4Addr,

    /// CIDR prefix length.
    pub prefix_len: u8,

    /// Default gateway.
    pub gateway: Ipv4Addr,

    /// DNS resolver address.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub dns: Option<Ipv4Addr>,
}

/// Guest IPv6 address configuration.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct BootstrapIpv6 {
    /// Guest IPv6 address.
    pub address: Ipv6Addr,

    /// CIDR prefix length.
    pub prefix_len: u8,

    /// Default gateway.
    pub gateway: Ipv6Addr,

    /// DNS resolver address.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub dns: Option<Ipv6Addr>,
}

/// A baseline guest environment entry.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BootstrapEnvVar {
    /// Environment variable name.
    pub key: String,

    /// Environment variable value.
    pub value: String,
}

/// In-guest security profile selected for a sandbox.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BootstrapSecurityProfile {
    /// Preserve normal guest-root behavior.
    #[default]
    Default,

    /// Restrict mount and process privileges inside the guest.
    Restricted,
}

/// Optional PID 1 handoff after agentd completes initialization.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BootstrapHandoffInit {
    /// Absolute init path inside the guest, or the `auto` sentinel.
    pub cmd: String,

    /// Arguments following `argv[0]`.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub args: Vec<String>,

    /// Working directory entered before the handoff.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cwd: Option<String>,

    /// Environment merged over the inherited runtime environment.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub env: Vec<BootstrapEnvVar>,
}

//--------------------------------------------------------------------------------------------------
// Tests
//--------------------------------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        codec,
        message::{Message, MessageType, PROTOCOL_VERSION},
    };

    #[test]
    fn guest_bootstrap_round_trips_transport_sensitive_values() {
        let bootstrap = GuestBootstrap {
            block_root: Some(BootstrapBlockRoot::OciErofs {
                lower: "/dev/vda".to_string(),
                upper: BootstrapBlockRootUpper::Tmpfs {
                    size_mib: Some(512),
                },
            }),
            dir_mounts: vec![BootstrapDirMount {
                tag: "workspace".to_string(),
                guest_path: "/workspace:with separators".to_string(),
                flags: BootstrapMountFlags {
                    noexec: true,
                    ..BootstrapMountFlags::default()
                },
            }],
            file_mounts: vec![BootstrapFileMount {
                tag: "config".to_string(),
                filename: "app.json".to_string(),
                guest_path: "/etc/app.json".to_string(),
                flags: BootstrapMountFlags {
                    readonly: true,
                    ..BootstrapMountFlags::default()
                },
            }],
            disk_mounts: vec![BootstrapDiskMount {
                id: "data".to_string(),
                guest_path: "/data".to_string(),
                fstype: Some("ext4".to_string()),
                flags: BootstrapMountFlags::default(),
            }],
            tmpfs_mounts: vec![BootstrapTmpfsMount {
                path: "/tmp".to_string(),
                size_mib: Some(64),
                mode: Some(0o1777),
                flags: BootstrapMountFlags::default(),
            }],
            hostname: Some("quoted-env-test".to_string()),
            host_alias: Some("host.microsandbox.internal".to_string()),
            network: Some(BootstrapNetwork {
                interface: "eth0".to_string(),
                mac: [0x02, 0x00, 0x00, 0x00, 0x00, 0x02],
                mtu: 1500,
                ipv4: Some(BootstrapIpv4 {
                    address: "172.16.0.2".parse().unwrap(),
                    prefix_len: 30,
                    gateway: "172.16.0.1".parse().unwrap(),
                    dns: Some("172.16.0.1".parse().unwrap()),
                }),
                ipv6: Some(BootstrapIpv6 {
                    address: "fd42:6d73:62::2".parse().unwrap(),
                    prefix_len: 64,
                    gateway: "fd42:6d73:62::1".parse().unwrap(),
                    dns: Some("fd42:6d73:62::1".parse().unwrap()),
                }),
            }),
            rlimits: vec![ExecRlimit {
                resource: "nofile".to_string(),
                soft: 1024,
                hard: 4096,
            }],
            user: Some("1000:1000".to_string()),
            default_cwd: Some("/workspace with spaces".to_string()),
            default_env: vec![
                BootstrapEnvVar {
                    key: "APP_CONFIG".to_string(),
                    value: "{\"message\":\"hello\"}".to_string(),
                },
                BootstrapEnvVar {
                    key: "UNICODE".to_string(),
                    value: "snowman: \u{2603}\nnext\tcolumn".to_string(),
                },
                BootstrapEnvVar {
                    key: "EMPTY".to_string(),
                    value: String::new(),
                },
            ],
            security_profile: BootstrapSecurityProfile::Restricted,
            handoff_init: Some(BootstrapHandoffInit {
                cmd: "/sbin/init".to_string(),
                args: vec!["--unit=multi user.target".to_string()],
                cwd: Some("/workspace with spaces".to_string()),
                env: vec![BootstrapEnvVar {
                    key: "HANDOFF_JSON".to_string(),
                    value: "{\"enabled\":true}".to_string(),
                }],
            }),
        };

        let message = Message::with_payload(MessageType::Bootstrap, 0, &bootstrap).unwrap();
        assert_eq!(message.v, PROTOCOL_VERSION);
        let mut frame = Vec::new();
        codec::encode_to_buf(&message, &mut frame).unwrap();
        let decoded = codec::decode_message_frame(&frame).unwrap();
        assert_eq!(decoded.payload::<GuestBootstrap>().unwrap(), bootstrap);
    }
}