launcher.test.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * Behavioral tests against the REAL launcher binary on a real kernel: the
  3. * CLI contract (usage errors, exit codes, argv passthrough) and the
  4. * confinement world-proofs (denied writes stay off disk, grants land).
  5. *
  6. * Preconditions and their skip semantics:
  7. * - Non-Linux host: skips entirely (exit 0) — there is nothing to build here.
  8. * - Linux without the built binary: FAILS — run `pnpm build:native` first.
  9. * - Linux whose kernel does not enforce Landlock: skips the enforcement
  10. * half, unless `NALR_REQUIRE_LANDLOCK=1` (set on CI, where a silent skip on
  11. * the very platform that exists to prove enforcement would be a false
  12. * green).
  13. */
  14. import assert from 'node:assert/strict';
  15. import fs from 'node:fs';
  16. import os from 'node:os';
  17. import path from 'node:path';
  18. import { spawnSync } from 'node:child_process';
  19. import {
  20. LAUNCHER_FAILURE_EXIT,
  21. grantArgs,
  22. launcherPath,
  23. probe,
  24. } from 'node-addon-landlock-run';
  25. const requireLandlock = process.env.NALR_REQUIRE_LANDLOCK === '1';
  26. if (process.platform !== 'linux') {
  27. console.log(`launcher.test: SKIP — the launcher only exists on linux (host: ${process.platform})`);
  28. process.exit(0);
  29. }
  30. const launcher = launcherPath();
  31. assert.ok(
  32. fs.existsSync(launcher),
  33. `launcher.test: no built launcher at ${launcher} — run \`pnpm build:native\` (apt-get install musl-tools) first`,
  34. );
  35. const run = (args, options = {}) => spawnSync(launcher, args, { encoding: 'utf8', ...options });
  36. // --- usage errors: parse failures exit LAUNCHER_FAILURE_EXIT before any restriction ---
  37. {
  38. const noCommand = run([]);
  39. assert.equal(noCommand.status, LAUNCHER_FAILURE_EXIT);
  40. assert.match(noCommand.stderr, /usage error: missing `-- <argv>\.\.\.` command/);
  41. const unknownFlag = run(['--bogus', '--', 'true']);
  42. assert.equal(unknownFlag.status, LAUNCHER_FAILURE_EXIT);
  43. assert.match(unknownFlag.stderr, /usage error: unknown argument: --bogus/);
  44. const danglingPath = run(['--ro']);
  45. assert.equal(danglingPath.status, LAUNCHER_FAILURE_EXIT);
  46. assert.match(danglingPath.stderr, /--ro requires a path/);
  47. for (const args of [
  48. ['--probe', '--ro', '/'],
  49. ['--probe', '--'],
  50. ['--probe', '--probe'],
  51. ]) {
  52. const probeWithExtras = run(args);
  53. assert.equal(probeWithExtras.status, LAUNCHER_FAILURE_EXIT);
  54. assert.match(probeWithExtras.stderr, /--probe takes no other arguments/);
  55. }
  56. }
  57. // --- probe: the functional availability signal ---
  58. const enforcement = probe(launcher);
  59. console.log(`launcher.test: probe → ${enforcement}`);
  60. if (enforcement === 'unusable') {
  61. if (requireLandlock) {
  62. console.error('launcher.test: NALR_REQUIRE_LANDLOCK=1 but the probe reports unusable — this kernel cannot prove enforcement');
  63. process.exit(1);
  64. }
  65. console.log('launcher.test: SKIP enforcement half — kernel does not enforce Landlock');
  66. process.exit(0);
  67. }
  68. {
  69. const probeRun = run(['--probe']);
  70. assert.equal(probeRun.status, 0);
  71. assert.match(probeRun.stdout, /^landlock: (fully enforced|partially enforced \(older ABI\))\n$/);
  72. }
  73. // --- confined exec: the command runs, its exit code passes through ---
  74. {
  75. const echo = run([...grantArgs({ readOnly: ['/'] }), '--', '/bin/sh', '-c', 'echo confined-ok']);
  76. assert.equal(echo.status, 0, echo.stderr);
  77. assert.equal(echo.stdout, 'confined-ok\n');
  78. const exitCode = run([...grantArgs({ readOnly: ['/'] }), '--', '/bin/sh', '-c', 'exit 7']);
  79. assert.equal(exitCode.status, 7, 'the wrapped command exit code must pass through unchanged');
  80. }
  81. // --- world-proofs: denied writes stay off disk, grants land, inheritance crosses exec ---
  82. {
  83. const work = fs.mkdtempSync(path.join(os.tmpdir(), 'nalr-launcher-test-'));
  84. const denied = path.join(work, 'denied.txt');
  85. const deniedRun = run([...grantArgs({ readOnly: ['/'] }), '--', '/bin/sh', '-c', `echo x > ${denied}`]);
  86. assert.notEqual(deniedRun.status, 0, 'a write outside the grants must fail');
  87. assert.ok(!fs.existsSync(denied), 'the denied write must not land on disk');
  88. const granted = path.join(work, 'granted.txt');
  89. const grantedRun = run([...grantArgs({ readOnly: ['/'], readWrite: [work] }), '--', '/bin/sh', '-c', `echo ok > ${granted}`]);
  90. assert.equal(grantedRun.status, 0, grantedRun.stderr);
  91. assert.equal(fs.readFileSync(granted, 'utf8'), 'ok\n');
  92. // The ruleset is inherited across execve: a CHILD of the wrapped command
  93. // is confined too, not just the direct exec target.
  94. const nested = path.join(work, 'nested.txt');
  95. const nestedRun = run([...grantArgs({ readOnly: ['/'] }), '--', '/bin/sh', '-c', `/bin/sh -c 'echo x > ${nested}'; true`]);
  96. assert.equal(nestedRun.status, 0, nestedRun.stderr);
  97. assert.ok(!fs.existsSync(nested), 'a denied write from a nested child must not land either');
  98. fs.rmSync(work, { recursive: true, force: true });
  99. }
  100. // --- fail closed: an unopenable grant root refuses to exec at all ---
  101. {
  102. const marker = path.join(os.tmpdir(), `nalr-should-not-exist-${process.pid}`);
  103. const badGrant = run(['--ro', '/no/such/grant/root', '--', '/bin/sh', '-c', `echo x > ${marker}`]);
  104. assert.equal(badGrant.status, LAUNCHER_FAILURE_EXIT);
  105. assert.match(badGrant.stderr, /cannot open rule path/);
  106. assert.ok(!fs.existsSync(marker), 'the command must never run when the launcher fails');
  107. }
  108. console.log('launcher.test: ok');