@worldapptechnologies/node-addon-system 0.0.0-stage → 0.1.2
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.
- package/LICENSE +21 -0
- package/README.md +13 -2
- package/lib/flock.d.ts +13 -0
- package/lib/flock.js +51 -0
- package/lib/index.d.ts +73 -0
- package/lib/index.js +98 -0
- package/package.json +42 -3
- package/src/flock.c +164 -0
- package/src/main.c +298 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 DeepSeek
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
---
|
|
2
|
+
description: "JavaScript entry for the prebuilt Landlock launcher and asynchronous POSIX flock."
|
|
3
|
+
kind: "package-library"
|
|
4
|
+
---
|
|
5
|
+
# @worldapptechnologies/node-addon-system
|
|
2
6
|
|
|
3
|
-
|
|
7
|
+
|
|
8
|
+
The `./landlock-run` entry exports the Landlock launcher path, enforcement probe, grant arguments, and protocol constants. The independent `./flock` entry exports `tryLockExclusive(fd): Promise<void>`; importing either entry does not load `system.node`. The package has no root export.
|
|
9
|
+
|
|
10
|
+
The lock operation attempts `LOCK_EX | LOCK_NB` asynchronously. Keep the caller-owned descriptor open until completion; contention rejects with `EAGAIN`/`EWOULDBLOCK`, other syscall failures also reject, and errors carry their code, positive errno, and `syscall: 'flock'`. Native setup errors reject the same promise. Closing the last descriptor for the open file description releases the lock. The binding does not open, duplicate, close, or explicitly unlock descriptors.
|
|
11
|
+
|
|
12
|
+
Optional OS/CPU platform packages carry the binaries. Linux has `bin/landlock-run` and separate `bin/glibc/system.node` / `bin/musl/system.node`; macOS has `bin/system.node`. Missing or unloadable flock bindings reject acquisition, without installation-time compilation. Landlock remains a separate executable with its existing fail-closed protocol; unsupported kernels/platforms probe unusable.
|
|
13
|
+
|
|
14
|
+
The two C sources ship for auditability. See the workspace [architecture](../../docs/architecture.md), [support matrix](../../docs/support-matrix.md), and [CLI contract](../../docs/cli-contract.md).
|
package/lib/flock.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attempt an exclusive, nonblocking POSIX flock on the caller's descriptor.
|
|
3
|
+
* The syscall runs in asynchronous work, so acquisition can occur after this
|
|
4
|
+
* call returns. Keep fd open until the promise settles; the binding never
|
|
5
|
+
* opens, duplicates, or closes it. Closing the locked descriptor releases the
|
|
6
|
+
* lock once all descriptors for its open file description are closed.
|
|
7
|
+
* @param fd - Open file descriptor to lock; ownership remains with the caller.
|
|
8
|
+
* @returns A promise resolving to void on acquisition. Contention rejects with
|
|
9
|
+
* EAGAIN/EWOULDBLOCK; other syscall failures also reject. Syscall errors carry
|
|
10
|
+
* code, positive errno, and syscall='flock'. Native setup errors, unsupported
|
|
11
|
+
* platforms, and addon loading failures reject; importing alone does not load it.
|
|
12
|
+
*/
|
|
13
|
+
export declare function tryLockExclusive(fd: number): Promise<void>;
|
package/lib/flock.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/** Lazy POSIX flock entry; importing it does not load a native addon. */
|
|
2
|
+
import { createRequire } from 'node:module';
|
|
3
|
+
import { dirname, join } from 'node:path';
|
|
4
|
+
import { getSystemErrorName } from 'node:util';
|
|
5
|
+
let binding;
|
|
6
|
+
function loadBinding() {
|
|
7
|
+
if (binding)
|
|
8
|
+
return binding;
|
|
9
|
+
const { platform, arch } = process;
|
|
10
|
+
if (platform !== 'linux' && platform !== 'darwin') {
|
|
11
|
+
throw Object.assign(new Error(`flock is not supported on ${platform}-${arch}`), {
|
|
12
|
+
code: 'ERR_FLOCK_UNSUPPORTED_PLATFORM',
|
|
13
|
+
syscall: 'flock',
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
let filename = 'system.node';
|
|
17
|
+
if (platform === 'linux') {
|
|
18
|
+
// Node's report types omit the libc field supplied by Linux reports.
|
|
19
|
+
const report = process.report.getReport();
|
|
20
|
+
filename = join(report.header.glibcVersionRuntime ? 'glibc' : 'musl', filename);
|
|
21
|
+
}
|
|
22
|
+
const require = createRequire(import.meta.url);
|
|
23
|
+
const manifest = require.resolve(`@worldapptechnologies/node-addon-system-${platform}-${arch}/package.json`);
|
|
24
|
+
binding = require(join(dirname(manifest), 'bin', filename));
|
|
25
|
+
return binding;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Attempt an exclusive, nonblocking POSIX flock on the caller's descriptor.
|
|
29
|
+
* The syscall runs in asynchronous work, so acquisition can occur after this
|
|
30
|
+
* call returns. Keep fd open until the promise settles; the binding never
|
|
31
|
+
* opens, duplicates, or closes it. Closing the locked descriptor releases the
|
|
32
|
+
* lock once all descriptors for its open file description are closed.
|
|
33
|
+
* @param fd - Open file descriptor to lock; ownership remains with the caller.
|
|
34
|
+
* @returns A promise resolving to void on acquisition. Contention rejects with
|
|
35
|
+
* EAGAIN/EWOULDBLOCK; other syscall failures also reject. Syscall errors carry
|
|
36
|
+
* code, positive errno, and syscall='flock'. Native setup errors, unsupported
|
|
37
|
+
* platforms, and addon loading failures reject; importing alone does not load it.
|
|
38
|
+
*/
|
|
39
|
+
export async function tryLockExclusive(fd) {
|
|
40
|
+
const errno = await new Promise((resolve) => {
|
|
41
|
+
loadBinding().tryLock(fd, resolve);
|
|
42
|
+
});
|
|
43
|
+
if (errno === 0)
|
|
44
|
+
return;
|
|
45
|
+
const code = getSystemErrorName(-errno);
|
|
46
|
+
throw Object.assign(new Error(`${code}: flock failed`), {
|
|
47
|
+
code,
|
|
48
|
+
errno,
|
|
49
|
+
syscall: 'flock',
|
|
50
|
+
});
|
|
51
|
+
}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/** The launcher binary's file name inside each platform package's `bin/`. */
|
|
2
|
+
export declare const LAUNCHER_BIN = "landlock-run";
|
|
3
|
+
/**
|
|
4
|
+
* The exit code for every launcher-level failure (usage error, unenforcing
|
|
5
|
+
* kernel, unopenable grant root, failed exec). After a successful `exec`, the
|
|
6
|
+
* wrapped command may also return 125, so consumers also require a matching
|
|
7
|
+
* launcher-owned fatal diagnostic to attribute launcher failure. Part of the
|
|
8
|
+
* CLI contract.
|
|
9
|
+
*/
|
|
10
|
+
export declare const LAUNCHER_FAILURE_EXIT = 125;
|
|
11
|
+
/**
|
|
12
|
+
* The probe's verdict on this host: `full` when the running kernel enforces
|
|
13
|
+
* every access the launcher can govern, `partial` when an older Landlock ABI
|
|
14
|
+
* governs only a subset (still confined for everything it supports), and
|
|
15
|
+
* `unusable` when nothing can be enforced — a kernel without Landlock, a
|
|
16
|
+
* disabled LSM, or a missing binary, all indistinguishable on purpose
|
|
17
|
+
* because the consumer's answer is the same: do not trust this launcher.
|
|
18
|
+
*/
|
|
19
|
+
export type LandlockEnforcement = 'full' | 'partial' | 'unusable';
|
|
20
|
+
/**
|
|
21
|
+
* Filesystem grants for one confined run. Everything not granted is denied —
|
|
22
|
+
* Landlock rulesets are allow-lists.
|
|
23
|
+
*/
|
|
24
|
+
export interface LauncherGrants {
|
|
25
|
+
/** Roots granted read + execute beneath (the launcher's `--ro`). */
|
|
26
|
+
readonly readOnly?: readonly string[];
|
|
27
|
+
/** Roots granted full filesystem access beneath (the launcher's `--rw`). */
|
|
28
|
+
readonly readWrite?: readonly string[];
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Path of the launcher binary for this host: resolved from the per-platform
|
|
32
|
+
* npm package `@worldapptechnologies/node-addon-system-<platform>-<arch>` (npm's
|
|
33
|
+
* `os`/`cpu` fields make installers fetch only the matching one). When the
|
|
34
|
+
* package is not resolvable — a platform without one, or an install that
|
|
35
|
+
* skipped the optional dependency — the returned fallback path points inside
|
|
36
|
+
* this package's own `node_modules` and simply never exists. Existence is
|
|
37
|
+
* deliberately not checked either way: {@link probe} is the single
|
|
38
|
+
* availability signal (a missing binary probes `unusable` the same way an
|
|
39
|
+
* unenforcing kernel does).
|
|
40
|
+
* @param resolvePackageJson - test hook over `require.resolve` (the default
|
|
41
|
+
* covers real installs); receives the platform package's `package.json`
|
|
42
|
+
* specifier and returns its absolute path, throwing when unresolvable.
|
|
43
|
+
* @returns the absolute launcher path to probe and exec.
|
|
44
|
+
*/
|
|
45
|
+
export declare function launcherPath(resolvePackageJson?: (specifier: string) => string): string;
|
|
46
|
+
/**
|
|
47
|
+
* The launcher grant arguments for one set of filesystem grants — everything
|
|
48
|
+
* before the `--` argv separator. A caller spawns
|
|
49
|
+
* `[launcherPath(), ...grantArgs(grants), '--', ...command]`; the flag
|
|
50
|
+
* spellings stay private to this package.
|
|
51
|
+
* @param grants - the read-only and read-write roots to allow.
|
|
52
|
+
* @returns the `--ro <path>` / `--rw <path>` argument list, read-only roots
|
|
53
|
+
* first, in the caller's order.
|
|
54
|
+
*/
|
|
55
|
+
export declare function grantArgs(grants: LauncherGrants): string[];
|
|
56
|
+
/**
|
|
57
|
+
* Functional probe: `landlock-run --probe` builds and enforces a maximal
|
|
58
|
+
* ruleset in a short-lived child and exits 0 only when the running kernel
|
|
59
|
+
* actually enforces it — `--version`-style checks would miss a kernel that
|
|
60
|
+
* has the syscalls but refuses enforcement. The probe's one report line is
|
|
61
|
+
* part of the CLI contract and distinguishes complete from per-ABI-subset
|
|
62
|
+
* enforcement; a zero exit without the partial marker reads as `full`. A
|
|
63
|
+
* failed or timed-out spawn (missing binary, wrong architecture, unenforcing
|
|
64
|
+
* kernel) probes `unusable`. Synchronous by design: consumers run it once
|
|
65
|
+
* and cache the verdict.
|
|
66
|
+
* @param launcher - the launcher path to probe; defaults to
|
|
67
|
+
* {@link launcherPath}'s resolution for this host.
|
|
68
|
+
* @param options - `timeoutMs` bounds the probe child (default 2000).
|
|
69
|
+
* @returns the enforcement verdict for this host.
|
|
70
|
+
*/
|
|
71
|
+
export declare function probe(launcher?: string, options?: {
|
|
72
|
+
timeoutMs?: number;
|
|
73
|
+
}): LandlockEnforcement;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The JavaScript API over the prebuilt `landlock-run` launcher: resolve the
|
|
3
|
+
* binary for this host, build its grant argv, and run its functional probe.
|
|
4
|
+
*
|
|
5
|
+
* This module owns the launcher's CLI contract (`docs/cli-contract.md`) so
|
|
6
|
+
* consumers never parse launcher output or spell launcher flags themselves —
|
|
7
|
+
* the contract and the binaries version together in one package family,
|
|
8
|
+
* which makes probe-parsing drift against the binary structurally
|
|
9
|
+
* impossible. Policy stays with the consumer: this package does not know
|
|
10
|
+
* what a "sandbox mode" is, only which paths are granted read or write.
|
|
11
|
+
*
|
|
12
|
+
* Deliberately no environment-variable overrides anywhere in this module:
|
|
13
|
+
* which binary confines a process must never be decidable by the ambient
|
|
14
|
+
* environment. Test injection is by function parameter.
|
|
15
|
+
*/
|
|
16
|
+
import { spawnSync } from 'node:child_process';
|
|
17
|
+
import { createRequire } from 'node:module';
|
|
18
|
+
import { dirname, join } from 'node:path';
|
|
19
|
+
import { fileURLToPath } from 'node:url';
|
|
20
|
+
/** The launcher binary's file name inside each platform package's `bin/`. */
|
|
21
|
+
export const LAUNCHER_BIN = 'landlock-run';
|
|
22
|
+
/**
|
|
23
|
+
* The exit code for every launcher-level failure (usage error, unenforcing
|
|
24
|
+
* kernel, unopenable grant root, failed exec). After a successful `exec`, the
|
|
25
|
+
* wrapped command may also return 125, so consumers also require a matching
|
|
26
|
+
* launcher-owned fatal diagnostic to attribute launcher failure. Part of the
|
|
27
|
+
* CLI contract.
|
|
28
|
+
*/
|
|
29
|
+
export const LAUNCHER_FAILURE_EXIT = 125;
|
|
30
|
+
/**
|
|
31
|
+
* Path of the launcher binary for this host: resolved from the per-platform
|
|
32
|
+
* npm package `@worldapptechnologies/node-addon-system-<platform>-<arch>` (npm's
|
|
33
|
+
* `os`/`cpu` fields make installers fetch only the matching one). When the
|
|
34
|
+
* package is not resolvable — a platform without one, or an install that
|
|
35
|
+
* skipped the optional dependency — the returned fallback path points inside
|
|
36
|
+
* this package's own `node_modules` and simply never exists. Existence is
|
|
37
|
+
* deliberately not checked either way: {@link probe} is the single
|
|
38
|
+
* availability signal (a missing binary probes `unusable` the same way an
|
|
39
|
+
* unenforcing kernel does).
|
|
40
|
+
* @param resolvePackageJson - test hook over `require.resolve` (the default
|
|
41
|
+
* covers real installs); receives the platform package's `package.json`
|
|
42
|
+
* specifier and returns its absolute path, throwing when unresolvable.
|
|
43
|
+
* @returns the absolute launcher path to probe and exec.
|
|
44
|
+
*/
|
|
45
|
+
export function launcherPath(resolvePackageJson = createRequire(import.meta.url).resolve) {
|
|
46
|
+
const platformPackage = `@worldapptechnologies/node-addon-system-${process.platform}-${process.arch}`;
|
|
47
|
+
try {
|
|
48
|
+
return join(dirname(resolvePackageJson(`${platformPackage}/package.json`)), 'bin', LAUNCHER_BIN);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
// Unresolvable platform package: no such package exists for this host, or
|
|
52
|
+
// it was not installed. Fall back to the path pnpm's layout WOULD use —
|
|
53
|
+
// absolute, inside this package's boundary (never cwd-relative: a
|
|
54
|
+
// spawnable relative path here would hand cwd control over which binary
|
|
55
|
+
// confines), and nonexistent exactly when the package is absent.
|
|
56
|
+
return fileURLToPath(new URL(`../node_modules/${platformPackage}/bin/${LAUNCHER_BIN}`, import.meta.url));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The launcher grant arguments for one set of filesystem grants — everything
|
|
61
|
+
* before the `--` argv separator. A caller spawns
|
|
62
|
+
* `[launcherPath(), ...grantArgs(grants), '--', ...command]`; the flag
|
|
63
|
+
* spellings stay private to this package.
|
|
64
|
+
* @param grants - the read-only and read-write roots to allow.
|
|
65
|
+
* @returns the `--ro <path>` / `--rw <path>` argument list, read-only roots
|
|
66
|
+
* first, in the caller's order.
|
|
67
|
+
*/
|
|
68
|
+
export function grantArgs(grants) {
|
|
69
|
+
return [
|
|
70
|
+
...(grants.readOnly ?? []).flatMap(root => ['--ro', root]),
|
|
71
|
+
...(grants.readWrite ?? []).flatMap(root => ['--rw', root]),
|
|
72
|
+
];
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Functional probe: `landlock-run --probe` builds and enforces a maximal
|
|
76
|
+
* ruleset in a short-lived child and exits 0 only when the running kernel
|
|
77
|
+
* actually enforces it — `--version`-style checks would miss a kernel that
|
|
78
|
+
* has the syscalls but refuses enforcement. The probe's one report line is
|
|
79
|
+
* part of the CLI contract and distinguishes complete from per-ABI-subset
|
|
80
|
+
* enforcement; a zero exit without the partial marker reads as `full`. A
|
|
81
|
+
* failed or timed-out spawn (missing binary, wrong architecture, unenforcing
|
|
82
|
+
* kernel) probes `unusable`. Synchronous by design: consumers run it once
|
|
83
|
+
* and cache the verdict.
|
|
84
|
+
* @param launcher - the launcher path to probe; defaults to
|
|
85
|
+
* {@link launcherPath}'s resolution for this host.
|
|
86
|
+
* @param options - `timeoutMs` bounds the probe child (default 2000).
|
|
87
|
+
* @returns the enforcement verdict for this host.
|
|
88
|
+
*/
|
|
89
|
+
export function probe(launcher = launcherPath(), options = {}) {
|
|
90
|
+
const result = spawnSync(launcher, ['--probe'], {
|
|
91
|
+
timeout: options.timeoutMs ?? 2000,
|
|
92
|
+
encoding: 'utf8',
|
|
93
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
94
|
+
});
|
|
95
|
+
if (result.status !== 0)
|
|
96
|
+
return 'unusable';
|
|
97
|
+
return /partially enforced/.test(result.stdout) ? 'partial' : 'full';
|
|
98
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@worldapptechnologies/node-addon-system",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Prebuilt system primitives: a Linux Landlock launcher and asynchronous POSIX flock through stable Node-API",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/jinxlo/Nulu-harness.git",
|
|
9
|
+
"directory": "native/system/packages/entry"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
"./landlock-run": {
|
|
13
|
+
"types": "./lib/index.d.ts",
|
|
14
|
+
"default": "./lib/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./flock": {
|
|
17
|
+
"types": "./lib/flock.d.ts",
|
|
18
|
+
"default": "./lib/flock.js"
|
|
19
|
+
},
|
|
20
|
+
"./package.json": "./package.json"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"README.md",
|
|
24
|
+
"lib/",
|
|
25
|
+
"!lib/*.tsbuildinfo",
|
|
26
|
+
"src/main.c",
|
|
27
|
+
"src/flock.c"
|
|
28
|
+
],
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=20"
|
|
31
|
+
},
|
|
32
|
+
"license": "BSD-3-Clause",
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"optionalDependencies": {
|
|
37
|
+
"@worldapptechnologies/node-addon-system-darwin-x64": "0.1.2",
|
|
38
|
+
"@worldapptechnologies/node-addon-system-linux-arm64": "0.1.2",
|
|
39
|
+
"@worldapptechnologies/node-addon-system-darwin-arm64": "0.1.2",
|
|
40
|
+
"@worldapptechnologies/node-addon-system-linux-x64": "0.1.2"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build:js": "tsc -b"
|
|
44
|
+
}
|
|
6
45
|
}
|
package/src/flock.c
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Node-API v8 binding for asynchronous flock(LOCK_EX | LOCK_NB).
|
|
3
|
+
* The caller owns fd through completion; this module never opens, duplicates,
|
|
4
|
+
* closes, or explicitly unlocks it. The callback receives zero or a positive
|
|
5
|
+
* errno; JavaScript owns the promise and syscall error construction.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
#include <node_api.h>
|
|
9
|
+
#include <errno.h>
|
|
10
|
+
#include <limits.h>
|
|
11
|
+
#include <stdlib.h>
|
|
12
|
+
#include <sys/file.h>
|
|
13
|
+
|
|
14
|
+
typedef struct {
|
|
15
|
+
napi_env env;
|
|
16
|
+
napi_ref callback;
|
|
17
|
+
napi_async_work work;
|
|
18
|
+
napi_async_cleanup_hook_handle cleanup;
|
|
19
|
+
int fd;
|
|
20
|
+
int error;
|
|
21
|
+
bool closing;
|
|
22
|
+
} lock_request;
|
|
23
|
+
|
|
24
|
+
static void check_status(napi_status status, const char *message) {
|
|
25
|
+
if (status != napi_ok) {
|
|
26
|
+
napi_fatal_error("flock", NAPI_AUTO_LENGTH, message, NAPI_AUTO_LENGTH);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
static void release_request(lock_request *request) {
|
|
31
|
+
if (request->callback != NULL) {
|
|
32
|
+
(void)napi_delete_reference(request->env, request->callback);
|
|
33
|
+
}
|
|
34
|
+
if (request->work != NULL) {
|
|
35
|
+
(void)napi_delete_async_work(request->env, request->work);
|
|
36
|
+
}
|
|
37
|
+
if (request->cleanup != NULL) {
|
|
38
|
+
(void)napi_remove_async_cleanup_hook(request->cleanup);
|
|
39
|
+
}
|
|
40
|
+
free(request);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
static napi_value throw_setup_error(napi_env env, napi_status status,
|
|
44
|
+
const char *message) {
|
|
45
|
+
if (status != napi_pending_exception) {
|
|
46
|
+
status = napi_throw_error(env, "ERR_FLOCK_ASYNC_WORK", message);
|
|
47
|
+
bool pending;
|
|
48
|
+
/* Error construction can fail with both generic_failure and a JS exception. */
|
|
49
|
+
check_status(napi_is_exception_pending(env, &pending), "Cannot inspect flock setup exception");
|
|
50
|
+
if (!pending && status != napi_pending_exception) check_status(status, message);
|
|
51
|
+
}
|
|
52
|
+
return NULL;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static void execute_lock(napi_env env, void *data) {
|
|
56
|
+
(void)env;
|
|
57
|
+
lock_request *request = data;
|
|
58
|
+
request->error = flock(request->fd, LOCK_EX | LOCK_NB) == 0 ? 0 : errno;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
static void complete_lock(napi_env env, napi_status status, void *data) {
|
|
62
|
+
lock_request *request = data;
|
|
63
|
+
if (env != NULL && !request->closing) {
|
|
64
|
+
napi_value callback;
|
|
65
|
+
napi_value receiver;
|
|
66
|
+
napi_value result;
|
|
67
|
+
check_status(status, "flock async work did not complete");
|
|
68
|
+
check_status(napi_get_reference_value(env, request->callback, &callback),
|
|
69
|
+
"Cannot retrieve flock callback");
|
|
70
|
+
check_status(napi_get_undefined(env, &receiver), "Cannot create flock receiver");
|
|
71
|
+
check_status(napi_create_int32(env, request->error, &result),
|
|
72
|
+
"Cannot create flock result");
|
|
73
|
+
status = napi_call_function(env, receiver, callback, 1, &result, NULL);
|
|
74
|
+
} else {
|
|
75
|
+
status = napi_ok;
|
|
76
|
+
}
|
|
77
|
+
release_request(request);
|
|
78
|
+
/* Node's async-work dispatcher reports callback exceptions and handles termination. */
|
|
79
|
+
if (status != napi_pending_exception) {
|
|
80
|
+
check_status(status, "Cannot invoke flock callback");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
static void cleanup_lock(napi_async_cleanup_hook_handle handle, void *data) {
|
|
85
|
+
(void)handle;
|
|
86
|
+
lock_request *request = data;
|
|
87
|
+
request->closing = true;
|
|
88
|
+
/*
|
|
89
|
+
* Running work cannot be cancelled. The hook keeps the environment alive
|
|
90
|
+
* until completion releases both the work and this hook; it never frees
|
|
91
|
+
* memory that the execution thread can still access.
|
|
92
|
+
*/
|
|
93
|
+
(void)napi_cancel_async_work(request->env, request->work);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
static napi_value try_lock(napi_env env, napi_callback_info info) {
|
|
97
|
+
size_t argc = 2;
|
|
98
|
+
napi_value argv[2];
|
|
99
|
+
double fd;
|
|
100
|
+
napi_status status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
|
|
101
|
+
if (status != napi_ok) {
|
|
102
|
+
return throw_setup_error(env, status, "Cannot read flock arguments");
|
|
103
|
+
}
|
|
104
|
+
if (argc < 1 || napi_get_value_double(env, argv[0], &fd) != napi_ok) {
|
|
105
|
+
(void)napi_throw_type_error(env, NULL, "fd must be a number");
|
|
106
|
+
return NULL;
|
|
107
|
+
}
|
|
108
|
+
if (!(fd >= INT_MIN && fd <= INT_MAX) || fd != (int)fd) {
|
|
109
|
+
(void)napi_throw_range_error(env, NULL, "fd must be a signed C int");
|
|
110
|
+
return NULL;
|
|
111
|
+
}
|
|
112
|
+
napi_valuetype callback_type;
|
|
113
|
+
if (argc < 2 || napi_typeof(env, argv[1], &callback_type) != napi_ok ||
|
|
114
|
+
callback_type != napi_function) {
|
|
115
|
+
(void)napi_throw_type_error(env, NULL, "callback must be a function");
|
|
116
|
+
return NULL;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
lock_request *request = calloc(1, sizeof(*request));
|
|
120
|
+
if (request == NULL) {
|
|
121
|
+
(void)napi_throw_error(env, "ENOMEM", "Cannot allocate flock async work");
|
|
122
|
+
return NULL;
|
|
123
|
+
}
|
|
124
|
+
request->env = env;
|
|
125
|
+
request->fd = (int)fd;
|
|
126
|
+
|
|
127
|
+
status = napi_create_reference(env, argv[1], 1, &request->callback);
|
|
128
|
+
if (status != napi_ok) {
|
|
129
|
+
release_request(request);
|
|
130
|
+
return throw_setup_error(env, status, "Cannot retain flock callback");
|
|
131
|
+
}
|
|
132
|
+
napi_value name;
|
|
133
|
+
status = napi_create_string_utf8(env, "flock", NAPI_AUTO_LENGTH, &name);
|
|
134
|
+
if (status == napi_ok) {
|
|
135
|
+
status = napi_create_async_work(env, NULL, name, execute_lock, complete_lock,
|
|
136
|
+
request, &request->work);
|
|
137
|
+
}
|
|
138
|
+
if (status != napi_ok) {
|
|
139
|
+
release_request(request);
|
|
140
|
+
return throw_setup_error(env, status, "Cannot create flock async work");
|
|
141
|
+
}
|
|
142
|
+
status = napi_add_async_cleanup_hook(env, cleanup_lock, request, &request->cleanup);
|
|
143
|
+
if (status != napi_ok) {
|
|
144
|
+
release_request(request);
|
|
145
|
+
return throw_setup_error(env, status, "Cannot register flock environment cleanup");
|
|
146
|
+
}
|
|
147
|
+
status = napi_queue_async_work(env, request->work);
|
|
148
|
+
if (status != napi_ok) {
|
|
149
|
+
/* Queue failure schedules no completion callback. */
|
|
150
|
+
release_request(request);
|
|
151
|
+
return throw_setup_error(env, status, "Cannot queue flock async work");
|
|
152
|
+
}
|
|
153
|
+
return NULL;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
NAPI_MODULE_INIT() {
|
|
157
|
+
napi_value function;
|
|
158
|
+
if (napi_create_function(env, "tryLock", NAPI_AUTO_LENGTH, try_lock,
|
|
159
|
+
NULL, &function) != napi_ok ||
|
|
160
|
+
napi_set_named_property(env, exports, "tryLock", function) != napi_ok) {
|
|
161
|
+
return NULL;
|
|
162
|
+
}
|
|
163
|
+
return exports;
|
|
164
|
+
}
|
package/src/main.c
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* landlock-run: self-restrict-then-exec Landlock launcher.
|
|
3
|
+
*
|
|
4
|
+
* The Landlock rung of a consuming sandbox seam, for Linux hosts where
|
|
5
|
+
* `bwrap` is
|
|
6
|
+
* unusable (not installed, unprivileged user namespaces disabled, or an LSM
|
|
7
|
+
* profile that denies mount — Landlock is an independent syscall family and
|
|
8
|
+
* needs none of those). The launcher installs a Landlock
|
|
9
|
+
* ruleset on itself and `exec`s the wrapped command; the ruleset is inherited
|
|
10
|
+
* across `execve`, so the command (and every process it spawns) runs confined
|
|
11
|
+
* while the invoking process stays unrestricted.
|
|
12
|
+
*
|
|
13
|
+
* CLI contract (mirrors the `bwrap` runner argv shape the executor wraps):
|
|
14
|
+
*
|
|
15
|
+
* landlock-run [--ro <path>]... [--rw <path>]... -- <argv>...
|
|
16
|
+
* landlock-run --probe
|
|
17
|
+
*
|
|
18
|
+
* `--ro` grants read+execute beneath the path; `--rw` grants full filesystem
|
|
19
|
+
* access beneath the path. Everything else is denied (Landlock is an
|
|
20
|
+
* allow-list). `--probe` builds a maximal ruleset and reports whether the
|
|
21
|
+
* running kernel actually enforces it — the executor's functional probe.
|
|
22
|
+
*
|
|
23
|
+
* Fail-closed: if the ruleset cannot be created or is NOT enforced by the
|
|
24
|
+
* kernel, the launcher exits non-zero WITHOUT exec'ing the command. A partial
|
|
25
|
+
* (best-effort) enforcement on an older ABI is accepted and reported on
|
|
26
|
+
* stderr; the consumer's mode vocabulary keeps its file-effect promises
|
|
27
|
+
* honest per ABI level (surfaced as `full` vs `partial` by the entry
|
|
28
|
+
* package's probe).
|
|
29
|
+
*
|
|
30
|
+
* Plain C11 over the raw Landlock UAPI — no libraries beyond libc (musl,
|
|
31
|
+
* linked statically), so the whole audit surface is this file plus the
|
|
32
|
+
* kernel's stable syscall contract. Built natively per architecture by
|
|
33
|
+
* `scripts/build.ts` into the per-platform npm packages
|
|
34
|
+
* (`@worldapptechnologies/node-addon-system-linux-{x64,arm64}`); the argv grammar,
|
|
35
|
+
* exit codes, and report lines are pinned in `docs/cli-contract.md`.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
#define _GNU_SOURCE
|
|
39
|
+
#include <errno.h>
|
|
40
|
+
#include <fcntl.h>
|
|
41
|
+
#include <stdint.h>
|
|
42
|
+
#include <stdio.h>
|
|
43
|
+
#include <stdlib.h>
|
|
44
|
+
#include <string.h>
|
|
45
|
+
#include <sys/prctl.h>
|
|
46
|
+
#include <sys/stat.h>
|
|
47
|
+
#include <sys/syscall.h>
|
|
48
|
+
#include <unistd.h>
|
|
49
|
+
|
|
50
|
+
/*
|
|
51
|
+
* The Landlock UAPI, defined locally instead of via <linux/landlock.h>: the
|
|
52
|
+
* kernel's user-space ABI is stable by contract, self-defining it keeps the
|
|
53
|
+
* build independent of the toolchain's header vintage, and the definitions
|
|
54
|
+
* double as the audit record of exactly which kernel API this launcher
|
|
55
|
+
* touches. Layouts and values are verbatim from the kernel header (the
|
|
56
|
+
* path-beneath struct is packed there, so it must be packed here).
|
|
57
|
+
*/
|
|
58
|
+
struct landlock_ruleset_attr {
|
|
59
|
+
uint64_t handled_access_fs;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
struct landlock_path_beneath_attr {
|
|
63
|
+
uint64_t allowed_access;
|
|
64
|
+
int32_t parent_fd;
|
|
65
|
+
} __attribute__((packed));
|
|
66
|
+
|
|
67
|
+
#define LANDLOCK_CREATE_RULESET_VERSION (1U << 0)
|
|
68
|
+
#define LANDLOCK_RULE_PATH_BENEATH 1
|
|
69
|
+
|
|
70
|
+
/* Filesystem access bits, grouped by the Landlock ABI that introduced them. */
|
|
71
|
+
#define LL_FS_EXECUTE (UINT64_C(1) << 0) /* ABI 1 */
|
|
72
|
+
#define LL_FS_WRITE_FILE (UINT64_C(1) << 1)
|
|
73
|
+
#define LL_FS_READ_FILE (UINT64_C(1) << 2)
|
|
74
|
+
#define LL_FS_READ_DIR (UINT64_C(1) << 3)
|
|
75
|
+
#define LL_FS_REMOVE_DIR (UINT64_C(1) << 4)
|
|
76
|
+
#define LL_FS_REMOVE_FILE (UINT64_C(1) << 5)
|
|
77
|
+
#define LL_FS_MAKE_CHAR (UINT64_C(1) << 6)
|
|
78
|
+
#define LL_FS_MAKE_DIR (UINT64_C(1) << 7)
|
|
79
|
+
#define LL_FS_MAKE_REG (UINT64_C(1) << 8)
|
|
80
|
+
#define LL_FS_MAKE_SOCK (UINT64_C(1) << 9)
|
|
81
|
+
#define LL_FS_MAKE_FIFO (UINT64_C(1) << 10)
|
|
82
|
+
#define LL_FS_MAKE_BLOCK (UINT64_C(1) << 11)
|
|
83
|
+
#define LL_FS_MAKE_SYM (UINT64_C(1) << 12)
|
|
84
|
+
#define LL_FS_REFER (UINT64_C(1) << 13) /* ABI 2 */
|
|
85
|
+
#define LL_FS_TRUNCATE (UINT64_C(1) << 14) /* ABI 3 (ABI 4 added TCP bits only) */
|
|
86
|
+
#define LL_FS_IOCTL_DEV (UINT64_C(1) << 15) /* ABI 5 */
|
|
87
|
+
|
|
88
|
+
#define LL_ABI1_MASK (LL_FS_REFER - 1) /* bits 0..12: every ABI-1 access, nothing newer */
|
|
89
|
+
|
|
90
|
+
/*
|
|
91
|
+
* Newest ABI this build knows; the negotiation below scales the actual
|
|
92
|
+
* ruleset down to what the running kernel supports.
|
|
93
|
+
*/
|
|
94
|
+
#define MAX_ABI 5L
|
|
95
|
+
|
|
96
|
+
/*
|
|
97
|
+
* Landlock has no libc wrappers; these are the raw syscalls. The numbers are
|
|
98
|
+
* identical on every architecture (the post-2011 unified table) — the
|
|
99
|
+
* fallbacks only matter to a libc older than the feature.
|
|
100
|
+
*/
|
|
101
|
+
#ifndef __NR_landlock_create_ruleset
|
|
102
|
+
#define __NR_landlock_create_ruleset 444
|
|
103
|
+
#define __NR_landlock_add_rule 445
|
|
104
|
+
#define __NR_landlock_restrict_self 446
|
|
105
|
+
#endif
|
|
106
|
+
|
|
107
|
+
/*
|
|
108
|
+
* Every fatal launcher error prints `landlock-run: <message>` to stderr
|
|
109
|
+
* and exits 125 — a code the wrapped command itself is unlikely to use, so
|
|
110
|
+
* the executor can tell launcher failures from command failures.
|
|
111
|
+
*/
|
|
112
|
+
#define EXIT_LAUNCHER_FAILURE 125
|
|
113
|
+
|
|
114
|
+
static const char NOT_ENFORCED_MESSAGE[] =
|
|
115
|
+
"landlock is not enforced by this kernel (ABI unsupported or disabled)";
|
|
116
|
+
|
|
117
|
+
/* Print one fatal `landlock-run: ...` line; returns the fatal exit code. */
|
|
118
|
+
static int fail(const char *prefix, const char *detail) {
|
|
119
|
+
if (detail == NULL) {
|
|
120
|
+
fprintf(stderr, "landlock-run: %s\n", prefix);
|
|
121
|
+
} else {
|
|
122
|
+
fprintf(stderr, "landlock-run: %s: %s\n", prefix, detail);
|
|
123
|
+
}
|
|
124
|
+
return EXIT_LAUNCHER_FAILURE;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
static int fail_usage(const char *message, const char *detail) {
|
|
128
|
+
fprintf(stderr, "landlock-run: usage error: %s%s\n", message, detail == NULL ? "" : detail);
|
|
129
|
+
return EXIT_LAUNCHER_FAILURE;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/* Parsed CLI: either a probe, or grants plus the command argv after `--`. */
|
|
133
|
+
struct cli {
|
|
134
|
+
int probe;
|
|
135
|
+
const char **ro;
|
|
136
|
+
size_t ro_count;
|
|
137
|
+
const char **rw;
|
|
138
|
+
size_t rw_count;
|
|
139
|
+
char **command; /* NULL-terminated tail of main's argv */
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/*
|
|
143
|
+
* Hand-rolled argv parsing — four flags do not justify a parsing library.
|
|
144
|
+
* Returns 0 on success, else the process exit code (message already printed).
|
|
145
|
+
*/
|
|
146
|
+
static int parse(int argc, char **argv, struct cli *cli) {
|
|
147
|
+
/* argc bounds each grant list; the launcher execs or exits, so no free. */
|
|
148
|
+
cli->ro = calloc(argc > 0 ? (size_t)argc : 1, sizeof *cli->ro);
|
|
149
|
+
cli->rw = calloc(argc > 0 ? (size_t)argc : 1, sizeof *cli->rw);
|
|
150
|
+
if (cli->ro == NULL || cli->rw == NULL) return fail("out of memory", NULL);
|
|
151
|
+
|
|
152
|
+
int index = 1;
|
|
153
|
+
while (index < argc) {
|
|
154
|
+
const char *arg = argv[index];
|
|
155
|
+
if (strcmp(arg, "--probe") == 0) {
|
|
156
|
+
if (argc != 2) {
|
|
157
|
+
return fail_usage("--probe takes no other arguments", NULL);
|
|
158
|
+
}
|
|
159
|
+
cli->probe = 1;
|
|
160
|
+
index += 1;
|
|
161
|
+
} else if (strcmp(arg, "--ro") == 0 || strcmp(arg, "--rw") == 0) {
|
|
162
|
+
if (index + 1 >= argc) {
|
|
163
|
+
return fail_usage(arg, " requires a path");
|
|
164
|
+
}
|
|
165
|
+
if (strcmp(arg, "--ro") == 0) {
|
|
166
|
+
cli->ro[cli->ro_count++] = argv[index + 1];
|
|
167
|
+
} else {
|
|
168
|
+
cli->rw[cli->rw_count++] = argv[index + 1];
|
|
169
|
+
}
|
|
170
|
+
index += 2;
|
|
171
|
+
} else if (strcmp(arg, "--") == 0) {
|
|
172
|
+
cli->command = &argv[index + 1];
|
|
173
|
+
break;
|
|
174
|
+
} else {
|
|
175
|
+
return fail_usage("unknown argument: ", arg);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (!cli->probe && (cli->command == NULL || cli->command[0] == NULL)) {
|
|
179
|
+
return fail_usage("missing `-- <argv>...` command", NULL);
|
|
180
|
+
}
|
|
181
|
+
return 0;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/* The filesystem accesses the running kernel's ABI can govern. */
|
|
185
|
+
static uint64_t fs_mask_for_abi(long abi) {
|
|
186
|
+
uint64_t mask = LL_ABI1_MASK;
|
|
187
|
+
if (abi >= 2) mask |= LL_FS_REFER;
|
|
188
|
+
if (abi >= 3) mask |= LL_FS_TRUNCATE;
|
|
189
|
+
if (abi >= 5) mask |= LL_FS_IOCTL_DEV;
|
|
190
|
+
return mask;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/* Add one path-beneath rule; 0 on success, else the exit code. */
|
|
194
|
+
static int add_rule(int ruleset_fd, const char *path, uint64_t access) {
|
|
195
|
+
int path_fd = open(path, O_PATH | O_CLOEXEC);
|
|
196
|
+
if (path_fd < 0) {
|
|
197
|
+
/* Fail closed on an unopenable grant root: silently narrowing the
|
|
198
|
+
* granted set would be safe, but running with a profile the caller did
|
|
199
|
+
* not get is not worth the ambiguity. */
|
|
200
|
+
fprintf(stderr, "landlock-run: cannot open rule path: %s: %s\n", path, strerror(errno));
|
|
201
|
+
return EXIT_LAUNCHER_FAILURE;
|
|
202
|
+
}
|
|
203
|
+
/* The kernel rejects directory-only accesses on a non-directory rule
|
|
204
|
+
* (EINVAL), so a file grant keeps only the file-compatible bits — how the
|
|
205
|
+
* `--rw /dev/null` grant works. */
|
|
206
|
+
struct stat st;
|
|
207
|
+
if (fstat(path_fd, &st) == 0 && !S_ISDIR(st.st_mode)) {
|
|
208
|
+
access &= LL_FS_EXECUTE | LL_FS_WRITE_FILE | LL_FS_READ_FILE | LL_FS_TRUNCATE | LL_FS_IOCTL_DEV;
|
|
209
|
+
}
|
|
210
|
+
struct landlock_path_beneath_attr attr = { .allowed_access = access, .parent_fd = path_fd };
|
|
211
|
+
if (syscall(__NR_landlock_add_rule, ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, &attr, 0) != 0) {
|
|
212
|
+
int saved = errno;
|
|
213
|
+
close(path_fd);
|
|
214
|
+
return fail("landlock ruleset error", strerror(saved));
|
|
215
|
+
}
|
|
216
|
+
close(path_fd);
|
|
217
|
+
return 0;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/*
|
|
221
|
+
* Install the ruleset on the current thread, negotiating the kernel's ABI
|
|
222
|
+
* down from MAX_ABI. `--ro` paths get the read side of the vocabulary (read
|
|
223
|
+
* file/dir + execute — the wrapped `bash` and everything it spawns must
|
|
224
|
+
* remain executable); `--rw` paths get every filesystem access the
|
|
225
|
+
* negotiated ABI can grant. Sets `no_new_privs` first (mandatory for an
|
|
226
|
+
* unprivileged restrict, and it neutralizes setuid/setgid escalation inside
|
|
227
|
+
* the sandbox). On success `*partial` reports whether the kernel governs
|
|
228
|
+
* only a subset of MAX_ABI's accesses. Returns 0, else the exit code.
|
|
229
|
+
*/
|
|
230
|
+
static int restrict_self(const struct cli *cli, int *partial) {
|
|
231
|
+
long abi = syscall(__NR_landlock_create_ruleset, NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
|
|
232
|
+
if (abi < 0) {
|
|
233
|
+
/* ENOSYS: kernel built without Landlock; EOPNOTSUPP: built but disabled.
|
|
234
|
+
* Either way: not enforceable — fail CLOSED, never exec unconfined. */
|
|
235
|
+
return fail(NOT_ENFORCED_MESSAGE, NULL);
|
|
236
|
+
}
|
|
237
|
+
*partial = abi < MAX_ABI;
|
|
238
|
+
uint64_t handled = fs_mask_for_abi(abi < MAX_ABI ? abi : MAX_ABI);
|
|
239
|
+
|
|
240
|
+
struct landlock_ruleset_attr attr = { .handled_access_fs = handled };
|
|
241
|
+
int ruleset_fd = (int)syscall(__NR_landlock_create_ruleset, &attr, sizeof attr, 0);
|
|
242
|
+
if (ruleset_fd < 0) return fail("landlock ruleset error", strerror(errno));
|
|
243
|
+
|
|
244
|
+
const uint64_t read_side = LL_FS_EXECUTE | LL_FS_READ_FILE | LL_FS_READ_DIR;
|
|
245
|
+
for (size_t i = 0; i < cli->ro_count; i++) {
|
|
246
|
+
int code = add_rule(ruleset_fd, cli->ro[i], read_side & handled);
|
|
247
|
+
if (code != 0) return code;
|
|
248
|
+
}
|
|
249
|
+
for (size_t i = 0; i < cli->rw_count; i++) {
|
|
250
|
+
int code = add_rule(ruleset_fd, cli->rw[i], handled);
|
|
251
|
+
if (code != 0) return code;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) {
|
|
255
|
+
return fail("landlock ruleset error", strerror(errno));
|
|
256
|
+
}
|
|
257
|
+
if (syscall(__NR_landlock_restrict_self, ruleset_fd, 0) != 0) {
|
|
258
|
+
return fail("landlock ruleset error", strerror(errno));
|
|
259
|
+
}
|
|
260
|
+
close(ruleset_fd);
|
|
261
|
+
return 0;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
int main(int argc, char **argv) {
|
|
265
|
+
struct cli cli = { 0 };
|
|
266
|
+
int code = parse(argc, argv, &cli);
|
|
267
|
+
if (code != 0) return code;
|
|
268
|
+
|
|
269
|
+
if (cli.probe) {
|
|
270
|
+
/* The functional probe: build and enforce a maximal ruleset in THIS
|
|
271
|
+
* short-lived process (the probe run exits right after). `--version`
|
|
272
|
+
* style checks would miss a kernel that has the syscalls but refuses
|
|
273
|
+
* enforcement; actually restricting is the only honest signal. The one
|
|
274
|
+
* report line is part of the launcher CLI contract — the executor reads
|
|
275
|
+
* enforcement completeness from it. */
|
|
276
|
+
static const char *probe_root = "/";
|
|
277
|
+
struct cli probe = { .ro = &probe_root, .ro_count = 1 };
|
|
278
|
+
int partial = 0;
|
|
279
|
+
code = restrict_self(&probe, &partial);
|
|
280
|
+
if (code != 0) return code;
|
|
281
|
+
printf("landlock: %s\n", partial ? "partially enforced (older ABI)" : "fully enforced");
|
|
282
|
+
return 0;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
int partial = 0;
|
|
286
|
+
code = restrict_self(&cli, &partial);
|
|
287
|
+
if (code != 0) return code;
|
|
288
|
+
if (partial) {
|
|
289
|
+
/* Older ABI: some handled accesses are not governed (e.g. truncate
|
|
290
|
+
* before ABI 3). Still confined for everything the kernel supports —
|
|
291
|
+
* report, do not refuse. */
|
|
292
|
+
fprintf(stderr, "landlock-run: partial enforcement (older Landlock ABI)\n");
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
execvp(cli.command[0], cli.command);
|
|
296
|
+
/* exec only returns on failure. */
|
|
297
|
+
return fail("exec failed", strerror(errno));
|
|
298
|
+
}
|