hl-engine 0.1.15

Safe Rust lifecycle API for the standalone HL Linux guest engine
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
use super::{
    spec_error, validate_guest_path, BTreeMap, BTreeSet, EngineCapabilities, MachineSpec,
    SpecError, SpecErrorCategory,
};

pub(super) fn validate_extension_shapes(
    capabilities: &EngineCapabilities,
    spec: &MachineSpec,
) -> Result<(), SpecError> {
    let mut providers = BTreeSet::new();
    let mut environment = spec
        .process
        .env
        .iter()
        .map(|(name, _)| name.as_encoded_bytes().to_vec())
        .collect::<BTreeSet<_>>();
    let mut environment_bytes = spec
        .process
        .env
        .iter()
        .map(|(name, value)| name.as_encoded_bytes().len() + value.as_encoded_bytes().len() + 2)
        .sum::<usize>();
    for (index, extension) in spec.extensions.iter().enumerate() {
        if !providers.insert(&extension.provider) {
            return Err(spec_error(
                SpecErrorCategory::Conflict,
                format!("extensions[{index}].provider"),
                "a provider may appear only once per launch",
            ));
        }
        if extension.config.schema.is_empty()
            || extension.config.schema.len() > 256
            || extension.config.bytes.len() > capabilities.limits.request_bytes as usize
        {
            return Err(spec_error(
                SpecErrorCategory::Limit,
                format!("extensions[{index}].config"),
                "extension configuration schema or bytes exceed engine bounds",
            ));
        }
        if extension
            .required_features
            .intersection(&extension.optional_features)
            .next()
            .is_some()
        {
            return Err(spec_error(
                SpecErrorCategory::Conflict,
                format!("extensions[{index}].features"),
                "the same feature cannot be both required and optional",
            ));
        }
        validate_extension_namespace(capabilities, extension, index)?;
        validate_extension_services(capabilities, extension, index)?;
        validate_extension_memory(capabilities, extension, index)?;
        validate_extension_environment(extension, index, &mut environment, &mut environment_bytes)?;
    }
    if environment_bytes > capabilities.limits.environment_bytes as usize {
        return Err(spec_error(
            SpecErrorCategory::Limit,
            "extensions.environment",
            "combined guest environment exceeds the engine limit",
        ));
    }
    Ok(())
}

fn validate_extension_environment(
    extension: &crate::extension::ExtensionSpec,
    index: usize,
    environment: &mut BTreeSet<Vec<u8>>,
    environment_bytes: &mut usize,
) -> Result<(), SpecError> {
    for (name, value) in &extension.environment {
        let name_bytes = name.as_encoded_bytes();
        let value_bytes = value.as_encoded_bytes();
        if name_bytes.is_empty()
            || name_bytes.contains(&b'=')
            || name_bytes.contains(&b'\n')
            || name_bytes.contains(&0)
            || value_bytes.contains(&b'\n')
            || value_bytes.contains(&0)
        {
            return Err(spec_error(
                SpecErrorCategory::Invalid,
                format!("extensions[{index}].environment"),
                "extension environment contains an invalid record",
            ));
        }
        if !environment.insert(name_bytes.to_vec()) {
            return Err(spec_error(
                SpecErrorCategory::Conflict,
                format!("extensions[{index}].environment"),
                "guest environment keys must be unique across the complete launch",
            ));
        }
        *environment_bytes = environment_bytes
            .checked_add(name_bytes.len() + value_bytes.len() + 2)
            .ok_or_else(|| {
                spec_error(
                    SpecErrorCategory::Limit,
                    "extensions.environment",
                    "guest environment size overflow",
                )
            })?;
    }
    Ok(())
}

fn validate_extension_namespace(
    capabilities: &EngineCapabilities,
    extension: &crate::extension::ExtensionSpec,
    index: usize,
) -> Result<(), SpecError> {
    use crate::extension::{FileSource, NamespaceEntry};

    if extension.namespace.len() > capabilities.limits.namespace_entries as usize {
        return Err(spec_error(
            SpecErrorCategory::Limit,
            format!("extensions[{index}].namespace"),
            "namespace entry count exceeds the engine limit",
        ));
    }
    let mut paths = BTreeMap::new();
    let mut projected_bytes = 0_u64;
    for entry in &extension.namespace {
        let field = format!("extensions[{index}].namespace");
        validate_guest_path(
            entry.path(),
            capabilities.limits.path_bytes,
            "extensions.namespace",
        )
        .map_err(|mut error| {
            error.field.clone_from(&field);
            error
        })?;
        if entry.path() == std::path::Path::new("/") {
            return Err(spec_error(
                SpecErrorCategory::Conflict,
                field,
                "extensions cannot replace the guest namespace root",
            ));
        }
        if paths.insert(entry.path(), entry).is_some() {
            return Err(spec_error(
                SpecErrorCategory::Conflict,
                field,
                "namespace transaction contains duplicate guest paths",
            ));
        }
        validate_namespace_entry(capabilities, entry, index)?;
        if let NamespaceEntry::File(file) = entry {
            if let FileSource::Immutable(bytes) | FileSource::Mutable(bytes) = &file.source {
                projected_bytes =
                    projected_bytes
                        .checked_add(bytes.len() as u64)
                        .ok_or_else(|| {
                            spec_error(
                                SpecErrorCategory::Limit,
                                format!("extensions[{index}].namespace"),
                                "projected file bytes overflow the engine limit",
                            )
                        })?;
                if projected_bytes > capabilities.limits.projected_file_bytes {
                    return Err(spec_error(
                        SpecErrorCategory::Limit,
                        format!("extensions[{index}].namespace"),
                        "combined projected file bytes exceed the engine limit",
                    ));
                }
            }
        }
    }
    for path in paths.keys() {
        for ancestor in path
            .ancestors()
            .skip(1)
            .take_while(|path| *path != std::path::Path::new("/"))
        {
            if let Some(entry) = paths.get(ancestor) {
                if !matches!(entry, NamespaceEntry::Directory(_)) {
                    return Err(spec_error(
                        SpecErrorCategory::Conflict,
                        format!("extensions[{index}].namespace"),
                        "a non-directory projected entry cannot contain descendants",
                    ));
                }
            }
        }
    }
    Ok(())
}

fn validate_namespace_entry(
    capabilities: &EngineCapabilities,
    entry: &crate::extension::NamespaceEntry,
    index: usize,
) -> Result<(), SpecError> {
    use crate::extension::{FileSource, NamespaceEntry};

    let field = format!("extensions[{index}].namespace");
    let metadata = match entry {
        NamespaceEntry::Directory(value) => Some(value.metadata),
        NamespaceEntry::File(value) => Some(value.metadata),
        NamespaceEntry::Device(value) => Some(value.metadata),
        NamespaceEntry::Service(value) => Some(value.metadata),
        NamespaceEntry::Symlink(value) => {
            let target = value.target.as_os_str().as_encoded_bytes();
            if target.is_empty()
                || target.contains(&0)
                || target.len() > capabilities.limits.path_bytes as usize
            {
                return Err(spec_error(
                    SpecErrorCategory::Invalid,
                    field,
                    "symlink target is empty or exceeds path bounds",
                ));
            }
            None
        }
        NamespaceEntry::HostBind(value) => {
            validate_host_path(&value.host, &field)?;
            None
        }
        NamespaceEntry::Socket(value) => {
            validate_host_path(&value.host, &field)?;
            None
        }
    };
    if metadata.is_some_and(|metadata| metadata.mode & !0o7777 != 0) {
        return Err(spec_error(
            SpecErrorCategory::Invalid,
            field,
            "namespace modes contain unsupported file-type bits",
        ));
    }
    match entry {
        NamespaceEntry::File(value) => match &value.source {
            FileSource::Host(path) => validate_host_path(path, &field)?,
            FileSource::Shared(id) if id.0 == 0 => {
                return Err(spec_error(
                    SpecErrorCategory::Invalid,
                    field,
                    "shared-byte identity must be nonzero",
                ))
            }
            FileSource::Generated(id) if id.0 == 0 => {
                return Err(spec_error(
                    SpecErrorCategory::Invalid,
                    field,
                    "generator identity must be nonzero",
                ))
            }
            _ => {}
        },
        NamespaceEntry::Device(value) if value.major >= 4096 || value.minor >= (1 << 20) => {
            return Err(spec_error(
                SpecErrorCategory::Invalid,
                field,
                "device major or minor exceeds Linux dev_t bounds",
            ))
        }
        NamespaceEntry::Service(value) if value.service.0 == 0 => {
            return Err(spec_error(
                SpecErrorCategory::Invalid,
                field,
                "service identity must be nonzero",
            ))
        }
        _ => {}
    }
    Ok(())
}

fn validate_extension_services(
    capabilities: &EngineCapabilities,
    extension: &crate::extension::ExtensionSpec,
    index: usize,
) -> Result<(), SpecError> {
    use crate::extension::NamespaceEntry;

    let contract_limit = capabilities
        .extensions
        .iter()
        .find(|capability| capability.provider == extension.provider)
        .map_or(capabilities.limits.handles, |capability| {
            capability.limits.services
        });
    if extension.services.len() > contract_limit.min(capabilities.limits.handles) as usize {
        return Err(spec_error(
            SpecErrorCategory::Limit,
            format!("extensions[{index}].services"),
            "service count exceeds the engine limit",
        ));
    }
    let mut services = BTreeSet::new();
    for service in &extension.services {
        if service.id.0 == 0 || !services.insert(service.id) {
            return Err(spec_error(
                SpecErrorCategory::Conflict,
                format!("extensions[{index}].services"),
                "service identities must be nonzero and unique",
            ));
        }
        if service.operations.is_empty()
            || service.max_request_bytes == 0
            || service.max_request_bytes > capabilities.limits.request_bytes
            || capabilities
                .extensions
                .iter()
                .find(|capability| capability.provider == extension.provider)
                .is_some_and(|capability| {
                    service.max_request_bytes > capability.limits.request_bytes
                })
        {
            return Err(spec_error(
                SpecErrorCategory::Limit,
                format!("extensions[{index}].services"),
                "service operations or request bound are invalid",
            ));
        }
    }
    for entry in &extension.namespace {
        let service = match entry {
            NamespaceEntry::Service(value) => Some(value.service),
            NamespaceEntry::Device(value) => value.service,
            _ => None,
        };
        if service.is_some_and(|service| !services.contains(&service)) {
            return Err(spec_error(
                SpecErrorCategory::Invalid,
                format!("extensions[{index}].namespace"),
                "namespace entry references an unregistered service",
            ));
        }
    }
    Ok(())
}

fn validate_extension_memory(
    capabilities: &EngineCapabilities,
    extension: &crate::extension::ExtensionSpec,
    index: usize,
) -> Result<(), SpecError> {
    let contract_limit = capabilities
        .extensions
        .iter()
        .find(|capability| {
            capability.provider == extension.provider
                && capability.versions.contains(&extension.version)
        })
        .map_or(capabilities.limits.mappings, |capability| {
            capability.limits.mappings
        });
    if extension.memory.len() > capabilities.limits.mappings.min(contract_limit) as usize {
        return Err(spec_error(
            SpecErrorCategory::Limit,
            format!("extensions[{index}].memory"),
            "mapping count exceeds the engine limit",
        ));
    }
    let mut total = 0_u64;
    for memory in &extension.memory {
        if memory.size == 0
            || memory.alignment == 0
            || !memory.alignment.is_power_of_two()
            || memory.alignment > (1 << 30)
            || !(memory.protections.read || memory.protections.write || memory.protections.execute)
        {
            return Err(spec_error(
                SpecErrorCategory::Invalid,
                format!("extensions[{index}].memory"),
                "memory size, alignment, or protections are invalid",
            ));
        }
        total = total.checked_add(memory.size).ok_or_else(|| {
            spec_error(
                SpecErrorCategory::Limit,
                format!("extensions[{index}].memory"),
                "memory requirements overflow",
            )
        })?;
    }
    if total > capabilities.limits.mapped_bytes {
        return Err(spec_error(
            SpecErrorCategory::Limit,
            format!("extensions[{index}].memory"),
            "combined memory requirements exceed the engine limit",
        ));
    }
    Ok(())
}

fn validate_host_path(path: &std::path::Path, field: &str) -> Result<(), SpecError> {
    if !path.is_absolute() || path.as_os_str().as_encoded_bytes().contains(&0) {
        Err(spec_error(
            SpecErrorCategory::Invalid,
            field,
            "host capability paths must be absolute and contain no NUL",
        ))
    } else {
        Ok(())
    }
}

pub(super) fn namespace_defaults(namespaces: &crate::spec::NamespaceSpec) -> bool {
    use crate::spec::IsolationMode;
    matches!(namespaces.mount, IsolationMode::Private)
        && matches!(namespaces.pid, IsolationMode::Private)
        && matches!(namespaces.uts, IsolationMode::Private)
        && matches!(namespaces.ipc, IsolationMode::Private)
        && matches!(namespaces.network, IsolationMode::Private)
        && matches!(namespaces.user, IsolationMode::Private)
        && matches!(namespaces.cgroup, IsolationMode::Private)
}