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
|
#!/usr/bin/env bun
import { ptr } from "bun:ffi";
import { realpath, stat } from "node:fs/promises";
import { resolve } from "node:path";
import { cstrPath, invalidOptionMessage, libc, localeQuotedDiagnostic, orderedGroupIds, pathDisplayName, readGroup, readPasswd, shellEscapeLsName, supplementaryGroupsForUser, systemErrorMessage } from "../shared/common.js";
import { InvocationError, fail, stderr } from "../shared/diagnostics.js";
import { commandSpawnErrorMessage, execCommand, isDirectCommandInvocation, isKnownUnexecutableCommand, normalizeInvocationLongOptionByPrefix } from "../shared/process.js";
import { defineCommand, runAsMain } from "../shared/command.js";
export const CHROOT_LONG_OPTIONS = ["groups", "userspec", "skip-chdir", "help", "version"];
export function chrootMetaOption(args) {
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === "--") return null;
let normalized = arg;
if (arg.startsWith("--")) normalized = normalizeInvocationLongOptionByPrefix(arg, CHROOT_LONG_OPTIONS);
const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined];
if (name === "help" || name === "version") {
if (inlineValue !== undefined) throw new InvocationError(`option '--${name}' doesn't allow an argument`);
return normalized;
}
if (name === "skip-chdir") {
if (inlineValue !== undefined) throw new InvocationError("option '--skip-chdir' doesn't allow an argument");
continue;
}
if (name === "userspec" || name === "groups") {
if (inlineValue !== undefined) continue;
i++;
continue;
}
if (arg.startsWith("-")) throw new InvocationError(invalidOptionMessage(arg));
return null;
}
return null;
}
export async function chrootCmd(args) {
const { opts, operands } = parseChrootArgs(normalizeChrootArgs(args));
if (!operands.length) throw new InvocationError("missing operand");
const [root, ...requestedCommand] = operands;
if (opts["skip-chdir"] && !(await chrootTargetIsOldRoot(root))) {
throw new InvocationError("option --skip-chdir only permitted if NEWROOT is old '/'");
}
const outsideCredentials = await resolveChrootCredentials(opts).catch(() => null);
if (libc.symbols.chroot(cstrPath(root)) !== 0) {
const rootError = await chrootRootError(root) ?? ((process.getuid?.() ?? 1) !== 0 ? "Operation not permitted" : "Operation not permitted");
return fail("chroot", `cannot change root directory to ${shellEscapeLsName(pathDisplayName(root), true)}: ${rootError}`, 125);
}
if (!opts["skip-chdir"]) {
try {
process.chdir("/");
} catch (error) {
return fail("chroot", `cannot chdir to root directory: ${systemErrorMessage(error)}`, 125);
}
}
let credentials;
try {
credentials = await resolveChrootCredentials(opts, outsideCredentials);
} catch (error) {
return fail("chroot", error.message || String(error), 125);
}
const credentialError = applyChrootCredentials(credentials);
if (credentialError) return fail("chroot", credentialError, 125);
const command = requestedCommand.length ? requestedCommand : [process.env.SHELL || "/bin/sh", "-i"];
if (!isDirectCommandInvocation()) {
try {
if (await isKnownUnexecutableCommand(command[0])) throw Object.assign(new Error("Permission denied"), { code: "EACCES" });
const proc = Bun.spawn(command, { stdin: "inherit", stdout: "inherit", stderr: "inherit" });
return await proc.exited;
} catch (error) {
const status = error?.code === "ENOENT" || String(error?.message ?? "").includes("not found") ? 127 : 126;
stderr(`chroot: failed to run command '${command[0]}': ${commandSpawnErrorMessage(error)}\n`);
return status;
}
}
const execError = execCommand(command);
const status = execError.errno === 2 ? 127 : 126;
stderr(`chroot: failed to run command '${command[0]}': ${execError.message}\n`);
return status;
}
export async function resolveChrootCredentials(opts, fallback = null) {
const userspec = opts.userspec;
const groupsSpec = opts.groups;
if (userspec == null && groupsSpec == null) return { uid: null, gid: null, groups: null };
let uid = null;
let gid = null;
let username = null;
let usedFallbackUser = false;
if (userspec != null) {
const separator = userspec.indexOf(":");
const userToken = separator < 0 ? userspec : userspec.slice(0, separator);
const groupToken = separator < 0 || separator === userspec.length - 1 ? null : userspec.slice(separator + 1);
if (userToken !== "") {
let user = await chrootUserToken(userToken).catch(() => null);
if (!user && fallback?.user) {
user = fallback.user;
usedFallbackUser = true;
}
if (!user) throw new InvocationError(`invalid user ${localeQuotedDiagnostic(userToken)}`);
uid = user.uid;
username = user.name;
if (groupToken == null) gid = user.gid;
}
if (groupToken != null && groupToken !== "") {
gid = await chrootGroupToken(groupToken).catch(() => null) ?? fallback?.gid ?? null;
if (gid == null) throw new InvocationError(`invalid group ${localeQuotedDiagnostic(groupToken)}`);
}
if (uid != null && gid == null && username == null) throw new InvocationError(`no group specified for unknown uid: ${uid}`);
}
let groups = null;
if (groupsSpec != null) groups = await chrootSupplementaryGroups(groupsSpec, fallback?.groups);
else if (uid != null && username != null) {
groups = usedFallbackUser && fallback?.groups != null
? fallback.groups
: orderedGroupIds(gid, await supplementaryGroupsForUser(username));
}
return { uid, gid, groups, user: uid == null ? null : { uid, gid, name: username } };
}
export async function chrootUserToken(token) {
const passwd = await readPasswd();
if (!token.startsWith("+") && passwd.has(token)) return { ...passwd.get(token), name: token };
const numeric = token.startsWith("+") ? token.slice(1) : token;
if (!/^\d+$/.test(numeric)) return null;
const uid = Number(numeric);
if (!Number.isSafeInteger(uid) || uid < 0 || uid > 0xffffffff) return null;
if (!token.startsWith("+")) {
for (const [name, row] of passwd) if (row.uid === uid) return { ...row, name };
}
return { uid, gid: null, name: null };
}
export async function chrootGroupToken(token) {
const groups = await readGroup();
if (!token.startsWith("+") && groups.has(token)) return groups.get(token);
const numeric = token.startsWith("+") ? token.slice(1) : token;
if (!/^\d+$/.test(numeric)) return null;
const gid = Number(numeric);
return Number.isSafeInteger(gid) && gid >= 0 && gid <= 0xffffffff ? gid : null;
}
export async function chrootSupplementaryGroups(spec, fallback = null) {
if (spec === "") return [];
const tokens = spec.split(",").map((token) => token.trim()).filter(Boolean);
if (!tokens.length) throw new InvocationError(`invalid group list ${localeQuotedDiagnostic(spec)}`);
const groups = [];
for (const token of tokens) {
const gid = await chrootGroupToken(token).catch(() => null);
if (gid == null) {
if (fallback != null) return fallback;
throw new InvocationError(`invalid group ${localeQuotedDiagnostic(token)}`);
}
groups.push(gid);
}
return groups;
}
export function applyChrootCredentials(credentials) {
if (credentials.groups != null || credentials.uid != null) {
const groups = credentials.groups ?? [];
const buffer = Buffer.alloc(groups.length * 4);
groups.forEach((gid, index) => buffer.writeUInt32LE(gid >>> 0, index * 4));
if (libc.symbols.setgroups(groups.length, groups.length ? ptr(buffer) : null) !== 0) return "failed to set supplemental groups";
}
if (credentials.gid != null && libc.symbols.setgid(credentials.gid) !== 0) return "failed to set group-ID";
if (credentials.uid != null && libc.symbols.setuid(credentials.uid) !== 0) return "failed to set user-ID";
return null;
}
export async function chrootRootError(root) {
try {
const info = await stat(root);
return info.isDirectory() ? null : "Not a directory";
} catch (error) {
return systemErrorMessage(error);
}
}
export async function chrootTargetIsOldRoot(root) {
try {
return await realpath(root) === "/";
} catch {
return resolve(root) === "/";
}
}
export function normalizeChrootArgs(args) {
const out = [];
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === "--") {
out.push(...args.slice(i));
break;
}
if (arg.startsWith("--")) {
const normalized = normalizeInvocationLongOptionByPrefix(arg, CHROOT_LONG_OPTIONS);
out.push(normalized);
if ((normalized === "--groups" || normalized === "--userspec") && i + 1 < args.length) out.push(args[++i]);
continue;
}
out.push(arg);
}
return out;
}
export function parseChrootArgs(args) {
const opts = {};
const operands = [];
const requireValue = (index, option) => {
if (index + 1 < args.length) return args[index + 1];
throw new InvocationError(`option '${option}' requires an argument`);
};
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === "--") {
operands.push(...args.slice(i + 1));
break;
}
if (arg === "--skip-chdir") {
opts["skip-chdir"] = true;
continue;
}
if (arg === "--userspec" || arg === "--groups") {
opts[arg.slice(2)] = requireValue(i, arg);
i++;
continue;
}
if (arg.startsWith("--userspec=")) {
opts.userspec = arg.slice("--userspec=".length);
continue;
}
if (arg.startsWith("--groups=")) {
opts.groups = arg.slice("--groups=".length);
continue;
}
if (arg.startsWith("-")) throw new InvocationError(invalidOptionMessage(arg));
operands.push(arg, ...args.slice(i + 1));
break;
}
return { opts, operands };
}
const singleCall = defineCommand("chroot", chrootCmd, chrootMetaOption);
export default singleCall;
if (import.meta.main) await runAsMain(singleCall);
|