northstar-runtime 0.9.2

Northstar is an container runtime for Linux targetting embedded systems
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
use super::mount::{Bind, Mount, MountOption, Resource, Tmpfs};
use crate::{common::version::VersionReq, npk::manifest::*, seccomp::SyscallRule};
use anyhow::Result;
use std::{
    convert::{TryFrom, TryInto},
    iter::FromIterator,
};

fn nn(s: &str) -> NonNulString {
    unsafe { NonNulString::from_str_unchecked(s) }
}

#[test]
fn parse() -> Result<()> {
    let manifest = "
name: hello
version: 0.0.0
init: /binary
args:
  - one
  - two
env:
  LD_LIBRARY_PATH: /lib
uid: 1000
gid: 1001
sched:
  policy:
    !other
      nice: 10
suppl_groups:
  - inet
  - log
capabilities:
  - CAP_NET_RAW
  - CAP_MKNOD
  - CAP_SYS_TIME
rlimits:
  nproc:
    soft: 1000
    hard: 1000
mounts:
  /dev:
    type: dev
  /tmp:
    type: tmpfs
    size: 42
  /lib:
    type: bind
    host: /lib
    options: rw
  /data:
    type: persist
  /resource:
    type: resource
    name: bla-blah.foo
    version: '>=1.0.0'
    dir: /bin/foo
    options: noexec
autostart: critical
seccomp:
  allow:
    fork: any
    waitpid: any
sockets:
  foo:
    type: stream
    mode: 0o600
    uid: 100
    gid: 1000
  bar:
    type: datagram
    mode: 0o600
    uid: 100
    gid: 1000
  baz:
    type: seq_packet
    mode: 0o600
    uid: 100
    gid: 1000
cgroups:
  memory:
    oom_monitor: true
    memory_hard_limit: 1000000
    memory_soft_limit: 1000000
    swappiness: 0
    attrs: {}
  cpu:
    cpus: 0,1
    shares: 1024
    attrs: {}
";

    let manifest = Manifest::from_str(manifest)?;

    assert_eq!(manifest.init, NonNulString::try_from("/binary").ok());
    assert_eq!(manifest.name.to_string(), "hello");
    assert_eq!(manifest.args.len(), 2);
    assert_eq!(manifest.args[0].to_string(), "one");
    assert_eq!(manifest.args[1].to_string(), "two");

    assert_eq!(manifest.autostart, Some(autostart::Autostart::Critical));
    assert_eq!(
        manifest.env.get(&"LD_LIBRARY_PATH".try_into()?),
        Some("/lib".try_into()?).as_ref()
    );
    assert_eq!(manifest.uid, 1000);
    assert_eq!(manifest.gid, 1001);
    let mut mounts = HashMap::new();
    mounts.insert(
        nn("/lib"),
        Mount::Bind(Bind {
            host: nn("/lib"),
            options: [MountOption::Rw].iter().cloned().collect(),
        }),
    );
    mounts.insert(nn("/data"), Mount::Persist);
    mounts.insert(
        nn("/resource"),
        Mount::Resource(Resource {
            name: "bla-blah.foo".try_into()?,
            version: VersionReq::parse(">=1.0.0")?,
            dir: unsafe { NonNulString::from_str_unchecked("/bin/foo") },
            options: [MountOption::NoExec].iter().cloned().collect(),
        }),
    );
    mounts.insert(nn("/tmp"), Mount::Tmpfs(Tmpfs { size: 42 }));
    mounts.insert(nn("/dev"), Mount::Dev);
    assert_eq!(manifest.mounts, mounts);

    let mut syscalls: HashMap<NonNulString, SyscallRule> = HashMap::new();
    syscalls.insert(
        NonNulString::try_from("fork".to_string())?,
        SyscallRule::Any,
    );
    syscalls.insert(
        NonNulString::try_from("waitpid".to_string())?,
        SyscallRule::Any,
    );
    assert_eq!(
        manifest.seccomp,
        Some(Seccomp {
            profile: None,
            allow: Some(syscalls)
        })
    );

    assert_eq!(
        manifest.capabilities,
        HashSet::from_iter(
            vec!(
                capabilities::Capability::CAP_NET_RAW,
                capabilities::Capability::CAP_MKNOD,
                capabilities::Capability::CAP_SYS_TIME,
            )
            .drain(..)
        )
    );
    let suppl_groups = unsafe {
        ["inet", "log"]
            .into_iter()
            .map(|s| NonNulString::from_str_unchecked(s))
            .collect()
    };
    assert_eq!(manifest.suppl_groups, suppl_groups);

    Ok(())
}

/// Invalid init too short.
#[test]
fn invalid_init_too_short() -> Result<()> {
    let manifest = "name: hello\nversion: 0.0.0\ninit: \"\"\nuid: 1\ngid: 1001";
    assert!(Manifest::from_str(manifest).is_err());
    Ok(())
}

/// Invalid init too long.
#[test]
fn invalid_init_too_long() -> Result<()> {
    let manifest = format!(
        "name: hello\nversion: 0.0.0\ninit: {}\nuid: 1\ngid: 1001",
        "b".repeat(4097)
    );

    assert!(dbg!(Manifest::from_str(&manifest)).is_err());
    Ok(())
}

/// Invalid uid
#[test]
fn invalid_uid_zero() -> Result<()> {
    let manifest = "name: hello\nversion: 0.0.0\ninit: /binary\nuid: 0\ngid: 1001";
    assert!(Manifest::from_str(manifest).is_err());
    Ok(())
}

/// Invalid gid
#[test]
fn invalid_gid_zero() -> Result<()> {
    let manifest = "name: hello\nversion: 0.0.0\ninit: /binary\nuid: 1\ngid: 0";
    assert!(Manifest::from_str(manifest).is_err());
    Ok(())
}

/// Invalid selinux context
#[test]
fn invalid_selinux_context() -> Result<()> {
    let manifest =
        "name: hello\nversion: 0.0.0\ninit: /binary\nuid: 1\ngid: 1\nselinux:\n    mount_context: fo@o";
    assert!(Manifest::from_str(manifest).is_err());
    Ok(())
}

/// Invalid suppl group with nul byte
#[test]
fn invalid_suppl_group_nul() -> Result<()> {
    let manifest =
        "name: hello\nversion: 0.0.0\ninit: /binary\nuid: 1\ngid: 1\nsuppl_groups: [fo\0o]";
    assert!(Manifest::from_str(manifest).is_err());
    Ok(())
}

/// Invalid too long suppl group
#[test]
fn invalid_suppl_group_too_long() -> Result<()> {
    let manifest =
            "name: hello\nversion: 0.0.0\ninit: /binary\nuid: 1\ngid: 1\nsuppl_groups: [looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong]";
    assert!(Manifest::from_str(manifest).is_err());
    Ok(())
}

/// Too many suppl groups
#[test]
fn invalid_suppl_group_duplicate() -> Result<()> {
    let manifest =
        "name: hello\nversion: 0.0.0\ninit: /binary\nuid: 1\ngid: 1\nsuppl_groups: [foo, foo]";
    assert!(Manifest::from_str(manifest).is_err());
    Ok(())
}

/// Two mounts on the same target are invalid
#[test]
fn duplicate_mount() -> Result<()> {
    let manifest = "name: hello\nversion: 0.0.0\ninit: /binary\nuid: 1000\ngid: 1001
mounts:
  /dev:
    type: dev
  /dev:
    type: dev
";
    assert!(Manifest::from_str(manifest).is_err());
    Ok(())
}

/// Overlapping mounts are invalid
#[test]
fn overlapping_mount() -> Result<()> {
    let manifest = "name: hello\nversion: 0.0.0\ninit: /binary\nuid: 1000\ngid: 1001
mounts:
  /lib/overlapping:
    type: bind
    host: /lib
  /lib/non_overlapping1:
    type: bind
    host: /lib
  /lib/non_overlapping2:
    type: bind
    host: /lib
  /lib/overlapping/foo:
    type: bind
    host: /lib
";
    assert!(Manifest::from_str(manifest).is_err());
    Ok(())
}

/// Non-overlapping mounts are invalid
#[test]
fn non_overlapping_mount() -> Result<()> {
    let manifest = "name: hello\nversion: 0.0.0\ninit: /binary\nuid: 1000\ngid: 1001
mounts:
  /other_lib1:
    type: bind
    host: /lib
  /lib/non_overlapping1:
    type: bind
    host: /lib
  /other_lib2:
    type: bind
    host: /lib
  /lib/non_overlapping2:
    type: bind
    host: /lib
";
    assert!(Manifest::from_str(manifest).is_ok());
    Ok(())
}

/// Resource mount with realtive dir
#[test]
#[should_panic]
fn resource_relative_dir() {
    let manifest = "name: hello\nversion: 0.0.0\ninit: /binary\nuid: 1000\ngid: 1001
mounts:
  /resource:
    type: resource
    name: bla-blah.foo
    version: '>=1.0.0'
    dir: bin/foo
";
    Manifest::from_str(manifest).expect("failed to parse manifest");
}

/// Resource mount with absolute dir
#[test]
fn resource_absolute() {
    let manifest = "name: hello\nversion: 0.0.0\ninit: /binary\nuid: 1000\ngid: 1001
mounts:
  /resource:
    type: resource
    name: bla-blah.foo
    version: '>=1.0.0'
    dir: /bin/foo
";
    Manifest::from_str(manifest).expect("failed to parse manifest");
}

#[test]
fn tmpfs() {
    let manifest = "name: hello\nversion: 0.0.0\ninit: /binary\nuid: 1000\ngid: 1001
mounts:
  /a:
    type: tmpfs
    size: 100
  /b:
    type: tmpfs
    size: 100kB
  /c:
    type: tmpfs
    size: 100MB
  /d:
    type: tmpfs
    size: 100GB
";
    let mountpoint = |s| -> NonNulString { unsafe { NonNulString::from_str_unchecked(s) } };
    let manifest = Manifest::from_str(manifest).expect("failed to parse manifest");
    assert_eq!(
        manifest.mounts.get(&mountpoint("/a")),
        Some(&Mount::Tmpfs(Tmpfs { size: 100 }))
    );
    assert_eq!(
        manifest.mounts.get(&mountpoint("/b")),
        Some(&Mount::Tmpfs(Tmpfs { size: 100000 }))
    );
    assert_eq!(
        manifest.mounts.get(&mountpoint("/c")),
        Some(&Mount::Tmpfs(Tmpfs { size: 100000000 }))
    );
    assert_eq!(
        manifest.mounts.get(&mountpoint("/d")),
        Some(&Mount::Tmpfs(Tmpfs { size: 100000000000 }))
    );

    // Test a invalid tmpfs size string
    let manifest = "name: hello\nversion: 0.0.0\ninit: /binary\n uid: 1000\ngid: 1001
mounts:
  /tmp:
    type: tmpfs
    size: 100MB
";
    assert!(Manifest::from_str(manifest).is_err());
}

#[test]
fn dev_minimal() {
    let manifest = "name: hello\nversion: 0.0.0\ninit: /binary\nuid: 1000\ngid: 1001\nmounts:\n  /dev:\n    type: dev";
    assert!(Manifest::from_str(manifest).is_ok());
}

#[test]
fn mount_resource() {
    let manifest = "name: hello\nversion: 0.0.0\ninit: /binary\nuid: 1000\ngid: 1001
mounts:
  /foo:
    type: resource
    name: foo-bar.qwerty12
    version: '>=0.0.1'
    dir: /
    options: rw,noexec,nosuid
";
    Manifest::from_str(manifest).expect("failed to parse manifest");
}

const ROUNDTRIP_MANIFEST: &str = "
name: hello
version: 0.0.0
init: /binary
uid: 1000
gid: 1001
console:
  permissions: full
args:
  - one
  - two
env:
  LD_LIBRARY_PATH: /lib
sched:
  policy: idle
mounts:
  /dev:
    type: dev
  /lib:
    type: bind
    host: /lib
    options: rw,nosuid,nodev,noexec
  /no_option:
    type: bind
    host: /foo
  /data:
    type: persist
  /resource:
    type: resource
    name: bla-bar.blah1234
    version: '>=1.0.0'
    dir: /bin/foo
    options: rw,nosuid,nodev,noexec
autostart: relaxed
seccomp:
  allow:
    fork: any
    waitpid: any
capabilities:
  - CAP_NET_ADMIN
io:
  stdout: pipe
  stderr: pipe
cgroups:
    memory:
      memory_hard_limit: 1000000
      memory_soft_limit: 1000000
      swappiness: 0
      attrs: {}
    cpu:
      cpus: 0,1
      shares: 1024
      attrs: {}
custom:
    blah: foo
    foo: 234
    test:
      - one
      - two
      - three
";

#[test]
fn roundtrip_yaml() -> Result<()> {
    let manifest = serde_yaml::from_str::<Manifest>(ROUNDTRIP_MANIFEST)?;
    let yaml = serde_yaml::to_string(&manifest)?;
    let deserialized = serde_yaml::from_str::<Manifest>(&yaml)?;
    assert_eq!(manifest, deserialized);

    Ok(())
}

#[test]
fn roundtrip_toml() -> Result<()> {
    let manifest = serde_yaml::from_str::<Manifest>(ROUNDTRIP_MANIFEST)?;
    manifest.validate()?;
    let toml_value = toml::Value::try_from(&manifest)?;
    let toml = toml::to_string(&toml_value)?;
    println!("{toml}");
    let deserialized = toml::from_str::<Manifest>(&toml)?;
    deserialized.validate()?;
    assert_eq!(manifest, deserialized);
    Ok(())
}

#[test]
fn roundtrip_json() -> Result<()> {
    let manifest = serde_yaml::from_str::<Manifest>(ROUNDTRIP_MANIFEST)?;
    manifest.validate()?;
    let json = serde_json::to_string(&manifest)?;
    let deserialized = serde_json::from_str::<Manifest>(&json)?;
    deserialized.validate()?;
    assert_eq!(manifest, deserialized);
    Ok(())
}

/// Check reserved env keys
#[test]
fn env() -> Result<()> {
    let manifest = "name: hello\nversion: 0.0.0\ninit: /binary\nuid: 1000\ngid: 1001\n
env:
  LD_LIBRARY_PATH: /lib
  PATH: /bin";

    assert!(Manifest::from_str(manifest).is_ok());

    let manifest = r"name: hello\nversion: 0.0.0\ninit: /binary\nuid: 1000\ngid: 1001\n
env:
  NORTHSTAR_CONSOLE: foo";
    assert!(Manifest::from_str(manifest).is_err());

    let manifest = r"name: hello\nversion: 0.0.0\ninit: /binary\nuid: 1000\ngid: 1001\n
env:
  NORTHSTAR_NAME: foo";
    assert!(Manifest::from_str(manifest).is_err());

    let manifest = r"name: hello\nversion: 0.0.0\ninit: /binary\nuid: 1000\ngid: 1001\n
env:
  NORTHSTAR_CONTAINER: foo";
    assert!(Manifest::from_str(manifest).is_err());

    let manifest = r"name: hello\nversion: 0.0.0\ninit: /binary\nuid: 1000\ngid: 1001\n
env:
  NORTHSTAR_VERSION: foo";
    assert!(Manifest::from_str(manifest).is_err());
    Ok(())
}