@kawaz/sockat 0.0.0 → 0.1.0
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/README-ja.md +192 -3
- package/README.md +192 -3
- package/dist/bind.d.ts +54 -0
- package/dist/bind.js +255 -0
- package/dist/bun.d.ts +25 -0
- package/dist/bun.js +46 -0
- package/dist/bunapi.d.ts +12 -0
- package/dist/bunapi.js +76 -0
- package/dist/config.d.ts +70 -0
- package/dist/config.js +93 -0
- package/dist/connect.d.ts +33 -0
- package/dist/connect.js +260 -0
- package/dist/drive.d.ts +49 -0
- package/dist/drive.js +122 -0
- package/dist/errors.d.ts +62 -0
- package/dist/errors.js +76 -0
- package/dist/fsx.d.ts +87 -0
- package/dist/fsx.js +149 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.js +69 -0
- package/dist/lock.d.ts +28 -0
- package/dist/lock.js +61 -0
- package/dist/names.d.ts +19 -0
- package/dist/names.js +55 -0
- package/dist/netapi.d.ts +12 -0
- package/dist/netapi.js +80 -0
- package/dist/paths.d.ts +46 -0
- package/dist/paths.js +111 -0
- package/dist/resolve.d.ts +7 -0
- package/dist/resolve.js +98 -0
- package/dist/route.d.ts +79 -0
- package/dist/route.js +108 -0
- package/dist/socketfile.d.ts +38 -0
- package/dist/socketfile.js +91 -0
- package/dist/sweep.d.ts +14 -0
- package/dist/sweep.js +172 -0
- package/dist/types.d.ts +100 -0
- package/dist/types.js +1 -0
- package/dist/world.d.ts +198 -0
- package/dist/world.js +666 -0
- package/package.json +36 -5
package/dist/world.js
ADDED
|
@@ -0,0 +1,666 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { envConfig } from "./config.js";
|
|
3
|
+
import { systemError, Unusable } from "./errors.js";
|
|
4
|
+
import { attempt, closeQuietly, dirOpenFlags, entry, euid, fstat, fstatId, isOurDir, lstat, lstatId, modeBits, oPath, procAt, realDir, sameId, } from "./fsx.js";
|
|
5
|
+
import { openLocked } from "./lock.js";
|
|
6
|
+
import { createAt, nameAttempts } from "./names.js";
|
|
7
|
+
import { isAbs, joinUnder, sameComponents, splitPath, below } from "./paths.js";
|
|
8
|
+
const spellingTooLong = new Unusable("spelling too long");
|
|
9
|
+
const noBase = new Unusable("no base");
|
|
10
|
+
/** The fd-free key of a (st_dev, st_ino). */
|
|
11
|
+
function key(id) {
|
|
12
|
+
return `${id.dev}:${id.ino}`;
|
|
13
|
+
}
|
|
14
|
+
/** D as a directory held, through which its E are named. */
|
|
15
|
+
export function dHeld(b, d) {
|
|
16
|
+
return { real: joinUnder(b.real, d.name), at: d.at };
|
|
17
|
+
}
|
|
18
|
+
/** E's real path, for nothing but its removal's error. */
|
|
19
|
+
export function linkEntry(e) {
|
|
20
|
+
return entry(dHeld(e.base, e.d), e.name);
|
|
21
|
+
}
|
|
22
|
+
/** Unlinks E; a missing E is no error (removed already). */
|
|
23
|
+
export function removeLink(e) {
|
|
24
|
+
const r = attempt(() => fs.unlinkSync(linkEntry(e)));
|
|
25
|
+
return r.ok || r.code === "ENOENT" ? { ok: true, value: undefined } : r;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Whether the directory fd, of (st_dev, st_ino) id, was removed: st_nlink 0,
|
|
29
|
+
* or its name (looked up by lookup, not following a symlink) gone or naming
|
|
30
|
+
* something else. The name is looked up even when st_nlink is not 0: a
|
|
31
|
+
* removed directory keeps st_nlink 2 on macOS (APFS).
|
|
32
|
+
*/
|
|
33
|
+
function dirGone(fd, id, lookup) {
|
|
34
|
+
const st = fstat(fd);
|
|
35
|
+
if (!st.ok || st.value.nlink === 0n) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
const now = lookup();
|
|
39
|
+
return now === null || !sameId(now, id);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Checks every directory from the root down to real, a real path
|
|
43
|
+
* (SPEC-routes "B の作り方" step 2): each is owned by root or the effective
|
|
44
|
+
* uid, and others cannot write to it (group included) unless it is sticky.
|
|
45
|
+
* With otherX, each must also let others search it. null when all pass;
|
|
46
|
+
* otherwise the failing path and EPERM, or the lstat's errno.
|
|
47
|
+
*/
|
|
48
|
+
export function checkAncestors(real, otherX) {
|
|
49
|
+
const us = BigInt(euid());
|
|
50
|
+
let p = "";
|
|
51
|
+
for (const c of real.split("/")) {
|
|
52
|
+
if (p === "" && c === "") {
|
|
53
|
+
p = "/";
|
|
54
|
+
}
|
|
55
|
+
else if (c === "") {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
p = p === "/" ? `/${c}` : `${p}/${c}`;
|
|
60
|
+
}
|
|
61
|
+
const st = lstat(p);
|
|
62
|
+
if (!st.ok) {
|
|
63
|
+
return { code: st.code, path: p };
|
|
64
|
+
}
|
|
65
|
+
const mode = modeBits(st.value);
|
|
66
|
+
let ok = st.value.isDirectory() && (st.value.uid === 0n || st.value.uid === us) && ((mode & 0o022) === 0 || (mode & 0o1000) !== 0);
|
|
67
|
+
if (ok && otherX) {
|
|
68
|
+
ok = (mode & 0o001) !== 0;
|
|
69
|
+
}
|
|
70
|
+
if (!ok) {
|
|
71
|
+
return { code: "EPERM", path: p };
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
/** The form of Options.base (SPEC "エラー"): absolute and ending with a name. */
|
|
77
|
+
export function givenShapeOk(b) {
|
|
78
|
+
return isAbs(b) && splitPath(b) !== null;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* What a process keeps across calls: the B it made, each held by its fd, and
|
|
82
|
+
* the places whose ancestors passed their check. A B that could be made is
|
|
83
|
+
* kept and used by every later call; one that could not is not remembered.
|
|
84
|
+
*/
|
|
85
|
+
export class World {
|
|
86
|
+
cfg;
|
|
87
|
+
/** The B of the places 1 to 3 (/tmp, $XDG_RUNTIME_DIR, $TMPDIR). */
|
|
88
|
+
auto = null;
|
|
89
|
+
/** The B of a usableCrossUid bind; auto itself when it is such a B. */
|
|
90
|
+
autoCross = null;
|
|
91
|
+
/** The B of the place 4, per socket's directory. */
|
|
92
|
+
sockdir = new Map();
|
|
93
|
+
/** The B of Options.base, by its (st_dev, st_ino). */
|
|
94
|
+
given = new Map();
|
|
95
|
+
/** The real paths of the places whose ancestors passed the check (once per process). */
|
|
96
|
+
checked = new Set();
|
|
97
|
+
constructor(cfg) {
|
|
98
|
+
this.cfg = cfg;
|
|
99
|
+
}
|
|
100
|
+
/** Every B the world holds, each once. */
|
|
101
|
+
bases() {
|
|
102
|
+
return [...new Set([this.auto, this.autoCross, ...this.sockdir.values(), ...this.given.values()])].filter((b) => b !== null);
|
|
103
|
+
}
|
|
104
|
+
/** The places 1 to 3 in order; the last two only when set to an absolute path. */
|
|
105
|
+
autoPlaces() {
|
|
106
|
+
const ps = [{ kind: "tmp", path: this.cfg.tmp }];
|
|
107
|
+
const xdg = this.cfg.getenv("XDG_RUNTIME_DIR");
|
|
108
|
+
if (xdg !== undefined && isAbs(xdg)) {
|
|
109
|
+
ps.push({ kind: "runtime", path: xdg });
|
|
110
|
+
}
|
|
111
|
+
const tmpdir = this.cfg.getenv("TMPDIR");
|
|
112
|
+
if (tmpdir !== undefined && isAbs(tmpdir)) {
|
|
113
|
+
ps.push({ kind: "tmpdir", path: tmpdir });
|
|
114
|
+
}
|
|
115
|
+
return ps;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* The world's B of the places 1 to 3 (cross: one others can follow): the
|
|
119
|
+
* first place in order where B is or can be made, whatever the length of
|
|
120
|
+
* the path, so every process of the user finds its B in the same place.
|
|
121
|
+
* Before B is made there, fits estimates the spelling through it: where it
|
|
122
|
+
* cannot fit, nothing is made and the next place is not tried.
|
|
123
|
+
*/
|
|
124
|
+
autoBase(cross, fits) {
|
|
125
|
+
this.dropGoneAuto();
|
|
126
|
+
if (!cross && this.auto !== null) {
|
|
127
|
+
return this.auto;
|
|
128
|
+
}
|
|
129
|
+
if (cross) {
|
|
130
|
+
if (this.autoCross !== null) {
|
|
131
|
+
return this.autoCross;
|
|
132
|
+
}
|
|
133
|
+
if (this.auto !== null && crossOf(this.auto)) {
|
|
134
|
+
this.autoCross = this.auto;
|
|
135
|
+
return this.auto;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
for (const p of this.autoPlaces()) {
|
|
139
|
+
const real = realDir(p.path);
|
|
140
|
+
if (!real.ok || !this.ancestorsPass(real.value)) {
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
if (cross && !crossPlace(real.value, bName(p.kind))) {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (!fits(p.kind, joinUnder(real.value, bName(p.kind)))) {
|
|
147
|
+
return spellingTooLong;
|
|
148
|
+
}
|
|
149
|
+
const made = this.makeBase(p, real.value, false);
|
|
150
|
+
if (!made.ok) {
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
const b = this.known(made.value);
|
|
154
|
+
if (cross) {
|
|
155
|
+
if (!crossOf(b)) {
|
|
156
|
+
// Not for this call; held only when it is auto already.
|
|
157
|
+
if (b !== this.auto) {
|
|
158
|
+
closeQuietly(b.fd);
|
|
159
|
+
}
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
this.autoCross = b;
|
|
163
|
+
return b;
|
|
164
|
+
}
|
|
165
|
+
this.auto = b;
|
|
166
|
+
return b;
|
|
167
|
+
}
|
|
168
|
+
return noBase;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Forgets the B of the places 1 to 3 when it was removed, so this call
|
|
172
|
+
* makes B again (an old systemd-tmpfiles, macOS's cleaning of $TMPDIR, or
|
|
173
|
+
* a hand may remove it).
|
|
174
|
+
*
|
|
175
|
+
* Design rationale: the fds of a B (and its D) forgotten are left open,
|
|
176
|
+
* not closed: an E handed out through it (SocketFile's cleanup) is still
|
|
177
|
+
* unlinked through D's fd on Linux, which a closed and reused fd number
|
|
178
|
+
* would point elsewhere.
|
|
179
|
+
*/
|
|
180
|
+
dropGoneAuto() {
|
|
181
|
+
if (this.auto !== null && gone(this.auto)) {
|
|
182
|
+
if (this.autoCross === this.auto) {
|
|
183
|
+
this.autoCross = null;
|
|
184
|
+
}
|
|
185
|
+
this.auto = null;
|
|
186
|
+
}
|
|
187
|
+
if (this.autoCross !== null && gone(this.autoCross)) {
|
|
188
|
+
this.autoCross = null;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/** b, or the B already held at the same (st_dev, st_ino) (b's fd then closed). */
|
|
192
|
+
known(b) {
|
|
193
|
+
for (const k of [this.auto, this.autoCross]) {
|
|
194
|
+
if (k !== null && sameId(k.id, b.id)) {
|
|
195
|
+
closeQuietly(b.fd);
|
|
196
|
+
return k;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return b;
|
|
200
|
+
}
|
|
201
|
+
/** Whether the directories down to real pass checkAncestors, checked once per process. */
|
|
202
|
+
ancestorsPass(real) {
|
|
203
|
+
if (this.checked.has(real)) {
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
206
|
+
if (checkAncestors(real, false) !== null) {
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
this.checked.add(real);
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Checks Options.base before any route is tried, at every call, making
|
|
214
|
+
* nothing (SPEC-routes "B の作り方"): its parent exists (ENOENT) and is a
|
|
215
|
+
* directory (ENOTDIR), every ancestor passes checkAncestors (EPERM), and B
|
|
216
|
+
* itself, when there, is a directory of ours others cannot write to (a
|
|
217
|
+
* symlink is EPERM, anything else ENOTDIR). With cross, every one of them
|
|
218
|
+
* must let others search it (EPERM): looked at now while the world does not
|
|
219
|
+
* hold the B yet, and taken from when it was first held once it does.
|
|
220
|
+
* The form (absolute, ending with a name) was checked before.
|
|
221
|
+
*/
|
|
222
|
+
checkGiven(b, cross, site) {
|
|
223
|
+
const fail = (code) => {
|
|
224
|
+
throw systemError(code, site.syscall, site.path, `Options.base ${b}`);
|
|
225
|
+
};
|
|
226
|
+
const parts = splitPath(b);
|
|
227
|
+
if (parts === null) {
|
|
228
|
+
return fail("EINVAL");
|
|
229
|
+
}
|
|
230
|
+
const real = realDir(parts.dir);
|
|
231
|
+
if (!real.ok) {
|
|
232
|
+
return fail(real.code);
|
|
233
|
+
}
|
|
234
|
+
const pst = attempt(() => fs.statSync(real.value, { bigint: true }));
|
|
235
|
+
if (!pst.ok) {
|
|
236
|
+
return fail(pst.code);
|
|
237
|
+
}
|
|
238
|
+
if (!pst.value.isDirectory()) {
|
|
239
|
+
return fail("ENOTDIR");
|
|
240
|
+
}
|
|
241
|
+
const anc = checkAncestors(real.value, false);
|
|
242
|
+
if (anc !== null) {
|
|
243
|
+
return fail(anc.code);
|
|
244
|
+
}
|
|
245
|
+
const st = lstat(joinUnder(real.value, parts.name));
|
|
246
|
+
if (!st.ok) {
|
|
247
|
+
if (st.code !== "ENOENT") {
|
|
248
|
+
return fail(st.code);
|
|
249
|
+
}
|
|
250
|
+
if (cross && checkAncestors(real.value, true) !== null) {
|
|
251
|
+
return fail("EPERM");
|
|
252
|
+
}
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
if (st.value.isSymbolicLink()) {
|
|
256
|
+
return fail("EPERM");
|
|
257
|
+
}
|
|
258
|
+
if (!st.value.isDirectory()) {
|
|
259
|
+
return fail("ENOTDIR");
|
|
260
|
+
}
|
|
261
|
+
if (!isOurDir(st.value) || (modeBits(st.value) & 0o022) !== 0) {
|
|
262
|
+
return fail("EPERM");
|
|
263
|
+
}
|
|
264
|
+
if (!cross) {
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
const held = this.given.get(key({ dev: st.value.dev, ino: st.value.ino }));
|
|
268
|
+
if (held !== undefined) {
|
|
269
|
+
if (!crossOf(held)) {
|
|
270
|
+
fail("EPERM");
|
|
271
|
+
}
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
if ((modeBits(st.value) & 0o001) === 0 || checkAncestors(real.value, true) !== null) {
|
|
275
|
+
fail("EPERM");
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* The world's B at b (Options.base, checked by checkGiven), made when there
|
|
280
|
+
* is none there yet. Where the spelling is estimated not to fit, nothing is
|
|
281
|
+
* made and the route is unusable. Otherwise an error making or opening it
|
|
282
|
+
* is thrown, with EPERM for what is not a private directory of ours.
|
|
283
|
+
*/
|
|
284
|
+
givenBase(b, fits, site) {
|
|
285
|
+
const fail = (code) => {
|
|
286
|
+
throw systemError(code, site.syscall, site.path, `Options.base ${b}`);
|
|
287
|
+
};
|
|
288
|
+
const parts = splitPath(b);
|
|
289
|
+
if (parts === null) {
|
|
290
|
+
return fail("EINVAL");
|
|
291
|
+
}
|
|
292
|
+
const real = realDir(parts.dir);
|
|
293
|
+
if (!real.ok) {
|
|
294
|
+
return fail(real.code);
|
|
295
|
+
}
|
|
296
|
+
const at = joinUnder(real.value, parts.name);
|
|
297
|
+
const id = lstatId(at);
|
|
298
|
+
if (id !== null) {
|
|
299
|
+
const k = this.given.get(key(id));
|
|
300
|
+
if (k !== undefined) {
|
|
301
|
+
if (!gone(k)) {
|
|
302
|
+
return k;
|
|
303
|
+
}
|
|
304
|
+
this.given.delete(key(id));
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
if (!fits("given", at)) {
|
|
308
|
+
return spellingTooLong;
|
|
309
|
+
}
|
|
310
|
+
const made = this.makeBase({ kind: "given", path: b }, real.value, true);
|
|
311
|
+
if (!made.ok) {
|
|
312
|
+
return fail(made.code === "notOurs" ? "EPERM" : made.code);
|
|
313
|
+
}
|
|
314
|
+
this.given.set(key(made.value.id), made.value);
|
|
315
|
+
return made.value;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* The world's B in the socket's directory (real, its real path; id, its
|
|
319
|
+
* (st_dev, st_ino)), made when there is none yet: the place 4. null when
|
|
320
|
+
* it cannot be made.
|
|
321
|
+
*/
|
|
322
|
+
sockdirBase(real, id) {
|
|
323
|
+
const k = this.sockdir.get(key(id));
|
|
324
|
+
if (k !== undefined) {
|
|
325
|
+
if (!gone(k)) {
|
|
326
|
+
return k;
|
|
327
|
+
}
|
|
328
|
+
this.sockdir.delete(key(id));
|
|
329
|
+
}
|
|
330
|
+
const made = this.makeBase({ kind: "sockdir", path: real }, real, false);
|
|
331
|
+
if (!made.ok) {
|
|
332
|
+
return null;
|
|
333
|
+
}
|
|
334
|
+
this.sockdir.set(key(id), made.value);
|
|
335
|
+
return made.value;
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Makes (or opens) B in the place p, whose real path is real (SPEC-routes
|
|
339
|
+
* "B の作り方" steps 2 to 5). B is named sockat-<euid> (sockat under
|
|
340
|
+
* $XDG_RUNTIME_DIR, .sockat.<random> in the socket's directory); one
|
|
341
|
+
* already there that fails the check is passed over for
|
|
342
|
+
* sockat-<euid>.<random>, except for Options.base (given), whose failure is
|
|
343
|
+
* returned.
|
|
344
|
+
*/
|
|
345
|
+
makeBase(p, real, given) {
|
|
346
|
+
if (!this.ancestorsPass(real)) {
|
|
347
|
+
const anc = checkAncestors(real, false);
|
|
348
|
+
return { ok: false, code: anc === null ? "EPERM" : anc.code };
|
|
349
|
+
}
|
|
350
|
+
const parent = this.holdParent(real);
|
|
351
|
+
if (!parent.ok) {
|
|
352
|
+
return parent;
|
|
353
|
+
}
|
|
354
|
+
try {
|
|
355
|
+
let name;
|
|
356
|
+
let made;
|
|
357
|
+
if (p.kind === "sockdir") {
|
|
358
|
+
({ name, made } = this.makeFresh(parent.value, ".sockat."));
|
|
359
|
+
}
|
|
360
|
+
else if (given) {
|
|
361
|
+
const parts = splitPath(p.path);
|
|
362
|
+
if (parts === null) {
|
|
363
|
+
return { ok: false, code: "EINVAL" };
|
|
364
|
+
}
|
|
365
|
+
name = parts.name;
|
|
366
|
+
made = makeOrOpen(parent.value, name);
|
|
367
|
+
}
|
|
368
|
+
else {
|
|
369
|
+
name = bName(p.kind);
|
|
370
|
+
made = makeOrOpen(parent.value, name);
|
|
371
|
+
if (!made.ok && made.code === "notOurs") {
|
|
372
|
+
({ name, made } = this.makeFresh(parent.value, `sockat-${euid()}.`));
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
if (!made.ok) {
|
|
376
|
+
return made;
|
|
377
|
+
}
|
|
378
|
+
const fd = made.value;
|
|
379
|
+
const st = fstat(fd);
|
|
380
|
+
if (!st.ok) {
|
|
381
|
+
closeQuietly(fd);
|
|
382
|
+
return st;
|
|
383
|
+
}
|
|
384
|
+
const id = { dev: st.value.dev, ino: st.value.ino };
|
|
385
|
+
const breal = joinUnder(real, name);
|
|
386
|
+
return {
|
|
387
|
+
ok: true,
|
|
388
|
+
value: {
|
|
389
|
+
fd,
|
|
390
|
+
id,
|
|
391
|
+
real: breal,
|
|
392
|
+
at: this.cfg.os === "linux" ? procAt(this.cfg.proc, fd, id) : null,
|
|
393
|
+
kind: p.kind,
|
|
394
|
+
ancestorsX: checkAncestors(real, true) === null,
|
|
395
|
+
selfX: (modeBits(st.value) & 0o001) !== 0,
|
|
396
|
+
tmpPath: this.throughPrivateTmp(p.path) || this.throughPrivateTmp(breal),
|
|
397
|
+
d: null,
|
|
398
|
+
nextE: 0,
|
|
399
|
+
},
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
finally {
|
|
403
|
+
if (parent.value.fd !== null) {
|
|
404
|
+
closeQuietly(parent.value.fd);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* The parent of a B (a checked real path) as a directory held: on Linux
|
|
410
|
+
* with /proc, opened O_PATH and named through /proc/self/fd (as mkdirat(2)
|
|
411
|
+
* on its fd would); otherwise by its real path, which nobody else can
|
|
412
|
+
* replace (its ancestors passed their check).
|
|
413
|
+
*/
|
|
414
|
+
holdParent(real) {
|
|
415
|
+
if (this.cfg.os !== "linux" || this.cfg.proc === "") {
|
|
416
|
+
return { ok: true, value: { real, at: null, fd: null } };
|
|
417
|
+
}
|
|
418
|
+
const r = attempt(() => fs.openSync(real, oPath | fs.constants.O_DIRECTORY | fs.constants.O_NOFOLLOW));
|
|
419
|
+
if (!r.ok) {
|
|
420
|
+
return r;
|
|
421
|
+
}
|
|
422
|
+
const id = fstatId(r.value);
|
|
423
|
+
return { ok: true, value: { real, at: id === null ? null : procAt(this.cfg.proc, r.value, id), fd: r.value } };
|
|
424
|
+
}
|
|
425
|
+
/** Makes a directory named prefix + random characters in parent, as openMade does. */
|
|
426
|
+
makeFresh(parent, prefix) {
|
|
427
|
+
const r = attempt(() => createAt(this.cfg.random, (n) => fs.mkdirSync(entry(parent, prefix + n), 0o700)));
|
|
428
|
+
if (!r.ok) {
|
|
429
|
+
return { name: "", made: r };
|
|
430
|
+
}
|
|
431
|
+
const name = prefix + r.value;
|
|
432
|
+
return { name, made: openMade(parent, name) };
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Whether p goes through a directory a private /tmp replaces: component by
|
|
436
|
+
* component one of cfg.privateTmps or below it, or below its real path.
|
|
437
|
+
*/
|
|
438
|
+
throughPrivateTmp(p) {
|
|
439
|
+
for (const t of this.cfg.privateTmps) {
|
|
440
|
+
if (sameComponents(p, t) || below(p, t) !== null) {
|
|
441
|
+
return true;
|
|
442
|
+
}
|
|
443
|
+
const real = realDir(t);
|
|
444
|
+
if (real.ok && (sameComponents(p, real.value) || below(p, real.value) !== null)) {
|
|
445
|
+
return true;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
return false;
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* The process's D in b, made the first time and made again when the D held
|
|
452
|
+
* was removed (its fd left open, as a forgotten B's). null when it cannot
|
|
453
|
+
* be made (not remembered: the next call tries again).
|
|
454
|
+
*/
|
|
455
|
+
ownD(b) {
|
|
456
|
+
if (b.d !== null) {
|
|
457
|
+
const d = b.d;
|
|
458
|
+
if (!dirGone(d.fd, d.id, () => lstatId(entry(b, d.name)))) {
|
|
459
|
+
return d;
|
|
460
|
+
}
|
|
461
|
+
b.d = null;
|
|
462
|
+
}
|
|
463
|
+
for (let i = 0; i < nameAttempts; i++) {
|
|
464
|
+
const d = this.makeOwnD(b);
|
|
465
|
+
if (d === "takenAway") {
|
|
466
|
+
continue;
|
|
467
|
+
}
|
|
468
|
+
b.d = d;
|
|
469
|
+
return d;
|
|
470
|
+
}
|
|
471
|
+
return null;
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Makes a D in b (SPEC-routes "D と E" steps 1 to 4): mkdir 0700 under a
|
|
475
|
+
* random name, set to 0711, opened without following a symlink with a
|
|
476
|
+
* shared lock taken (openLocked) and checked to be a directory of ours, and
|
|
477
|
+
* its name looked up again. A D taken away before it was locked (another
|
|
478
|
+
* process's sweep saw it unlocked: the chmod or the open finds nothing,
|
|
479
|
+
* the lock is busy, or the name names another) is "takenAway", and the
|
|
480
|
+
* next name is tried; what this call made of it is removed first.
|
|
481
|
+
*/
|
|
482
|
+
makeOwnD(b) {
|
|
483
|
+
const r = attempt(() => createAt(this.cfg.random, (n) => fs.mkdirSync(entry(b, n), 0o700)));
|
|
484
|
+
if (!r.ok) {
|
|
485
|
+
return null;
|
|
486
|
+
}
|
|
487
|
+
const name = r.value;
|
|
488
|
+
const at = entry(b, name);
|
|
489
|
+
const drop = () => {
|
|
490
|
+
const st = lstat(at);
|
|
491
|
+
if (st.ok && isOurDir(st.value)) {
|
|
492
|
+
attempt(() => fs.rmdirSync(at));
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
const ch = attempt(() => fs.chmodSync(at, 0o711));
|
|
496
|
+
if (!ch.ok) {
|
|
497
|
+
drop();
|
|
498
|
+
return ch.code === "ENOENT" ? "takenAway" : null;
|
|
499
|
+
}
|
|
500
|
+
const locked = openLocked(at, this.cfg.lock, "shared");
|
|
501
|
+
if (!locked.ok) {
|
|
502
|
+
// An empty D of ours is removed either way: a lock that keeps being
|
|
503
|
+
// busy (flock(1) failing otherwise than on a conflict) must not leave
|
|
504
|
+
// one behind per name tried.
|
|
505
|
+
drop();
|
|
506
|
+
return locked.why === "busy" || locked.code === "ENOENT" ? "takenAway" : null;
|
|
507
|
+
}
|
|
508
|
+
const fd = locked.fd;
|
|
509
|
+
const held = fstat(fd);
|
|
510
|
+
if (!held.ok || !isOurDir(held.value)) {
|
|
511
|
+
closeQuietly(fd);
|
|
512
|
+
return null;
|
|
513
|
+
}
|
|
514
|
+
const id = { dev: held.value.dev, ino: held.value.ino };
|
|
515
|
+
const now = lstatId(at);
|
|
516
|
+
if (now === null || !sameId(now, id)) {
|
|
517
|
+
closeQuietly(fd);
|
|
518
|
+
return "takenAway";
|
|
519
|
+
}
|
|
520
|
+
return { fd, name, id, real: joinUnder(b.real, name), at: this.cfg.os === "linux" ? procAt(this.cfg.proc, fd, id) : null };
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* Makes a resolve E in d (b's D) pointing to target, named by b's counter
|
|
524
|
+
* in base 36 (D is this process's alone, so a short counter is unique). A
|
|
525
|
+
* name taken is passed over for the next. null when none could be made.
|
|
526
|
+
*/
|
|
527
|
+
resolveE(b, d, target) {
|
|
528
|
+
for (let i = 0; i < nameAttempts; i++) {
|
|
529
|
+
const name = b.nextE.toString(36);
|
|
530
|
+
b.nextE++;
|
|
531
|
+
const r = attempt(() => fs.symlinkSync(target, entry(dHeld(b, d), name)));
|
|
532
|
+
if (r.ok) {
|
|
533
|
+
return { base: b, d, name };
|
|
534
|
+
}
|
|
535
|
+
if (r.code !== "EEXIST") {
|
|
536
|
+
return null;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
return null;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
/** The name of B in a place of kind: sockat-<euid>, sockat under $XDG_RUNTIME_DIR. */
|
|
543
|
+
export function bName(kind) {
|
|
544
|
+
return kind === "runtime" ? "sockat" : `sockat-${euid()}`;
|
|
545
|
+
}
|
|
546
|
+
/**
|
|
547
|
+
* Whether others can follow a real-path spelling through b: every directory
|
|
548
|
+
* on the way and B itself let them search (SPEC-routes "実体の置き場"). B's own
|
|
549
|
+
* mode is looked at again by its fd while it was found without it.
|
|
550
|
+
*/
|
|
551
|
+
export function crossOf(b) {
|
|
552
|
+
if (!b.selfX) {
|
|
553
|
+
const st = fstat(b.fd);
|
|
554
|
+
b.selfX = st.ok && (modeBits(st.value) & 0o001) !== 0;
|
|
555
|
+
}
|
|
556
|
+
return b.ancestorsX && b.selfX;
|
|
557
|
+
}
|
|
558
|
+
/** Whether B was removed since it was opened. */
|
|
559
|
+
function gone(b) {
|
|
560
|
+
return dirGone(b.fd, b.id, () => lstatId(b.real));
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Whether a B named name in the place real (whose ancestors passed) can be
|
|
564
|
+
* one others can follow, before it is made: every directory down to real
|
|
565
|
+
* lets others search it, and a B of ours already there does too (one that
|
|
566
|
+
* fails B's check is passed over for a fresh B of mode 0711, which does).
|
|
567
|
+
*/
|
|
568
|
+
function crossPlace(real, name) {
|
|
569
|
+
if (checkAncestors(real, true) !== null) {
|
|
570
|
+
return false;
|
|
571
|
+
}
|
|
572
|
+
const st = lstat(joinUnder(real, name));
|
|
573
|
+
if (!st.ok || !isOurDir(st.value) || (modeBits(st.value) & 0o022) !== 0) {
|
|
574
|
+
return true;
|
|
575
|
+
}
|
|
576
|
+
return (modeBits(st.value) & 0o001) !== 0;
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* Makes B named name in parent, or opens the one there (SPEC-routes "B の作り方"
|
|
580
|
+
* steps 3 to 5): mkdir 0700, then openMade. One already there is used when
|
|
581
|
+
* it is a directory owned by us that others cannot write to, whatever its
|
|
582
|
+
* mode otherwise (left as it is); notOurs when it is not.
|
|
583
|
+
*/
|
|
584
|
+
function makeOrOpen(parent, name) {
|
|
585
|
+
const at = entry(parent, name);
|
|
586
|
+
const mk = attempt(() => fs.mkdirSync(at, 0o700));
|
|
587
|
+
if (mk.ok) {
|
|
588
|
+
return openMade(parent, name);
|
|
589
|
+
}
|
|
590
|
+
if (mk.code !== "EEXIST") {
|
|
591
|
+
return mk;
|
|
592
|
+
}
|
|
593
|
+
const r = attempt(() => fs.openSync(at, dirOpenFlags));
|
|
594
|
+
if (!r.ok) {
|
|
595
|
+
// A symlink (ELOOP), a file (ENOTDIR), ...
|
|
596
|
+
return { ok: false, code: "notOurs" };
|
|
597
|
+
}
|
|
598
|
+
const st = fstat(r.value);
|
|
599
|
+
if (!st.ok || !isOurDir(st.value) || (modeBits(st.value) & 0o022) !== 0 || !sameEntry(parent, at, st.value)) {
|
|
600
|
+
closeQuietly(r.value);
|
|
601
|
+
return { ok: false, code: "notOurs" };
|
|
602
|
+
}
|
|
603
|
+
return r;
|
|
604
|
+
}
|
|
605
|
+
/**
|
|
606
|
+
* Sets the directory name just made in parent to 0711 by its name (whatever
|
|
607
|
+
* a umask took off is put back), opens it without following a symlink and
|
|
608
|
+
* checks it is a directory owned by us (SPEC-routes "B の作り方" step 4).
|
|
609
|
+
* When the mode cannot be set or the directory cannot be opened, the
|
|
610
|
+
* directory made is removed (when it is still one of ours) and the error
|
|
611
|
+
* returned.
|
|
612
|
+
*/
|
|
613
|
+
function openMade(parent, name) {
|
|
614
|
+
const at = entry(parent, name);
|
|
615
|
+
const drop = () => {
|
|
616
|
+
const st = lstat(at);
|
|
617
|
+
if (st.ok && isOurDir(st.value)) {
|
|
618
|
+
attempt(() => fs.rmdirSync(at));
|
|
619
|
+
}
|
|
620
|
+
};
|
|
621
|
+
const ch = attempt(() => fs.chmodSync(at, 0o711));
|
|
622
|
+
if (!ch.ok) {
|
|
623
|
+
drop();
|
|
624
|
+
return ch;
|
|
625
|
+
}
|
|
626
|
+
const r = attempt(() => fs.openSync(at, dirOpenFlags));
|
|
627
|
+
if (!r.ok) {
|
|
628
|
+
drop();
|
|
629
|
+
return r;
|
|
630
|
+
}
|
|
631
|
+
const st = fstat(r.value);
|
|
632
|
+
if (!st.ok || !isOurDir(st.value) || !sameEntry(parent, at, st.value)) {
|
|
633
|
+
closeQuietly(r.value);
|
|
634
|
+
return { ok: false, code: "notOurs" };
|
|
635
|
+
}
|
|
636
|
+
return r;
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* Whether the entry at (named through parent) is the directory st was taken
|
|
640
|
+
* from. Where parent is named by its real path rather than an fd, this is
|
|
641
|
+
* the check after the fact that stands in for *at (DR-0002): lstat of the
|
|
642
|
+
* name and fstat of the fd opened agree on (st_dev, st_ino).
|
|
643
|
+
*/
|
|
644
|
+
function sameEntry(parent, at, st) {
|
|
645
|
+
if (parent.at !== null) {
|
|
646
|
+
return true;
|
|
647
|
+
}
|
|
648
|
+
const now = lstatId(at);
|
|
649
|
+
return now !== null && sameId(now, { dev: st.dev, ino: st.ino });
|
|
650
|
+
}
|
|
651
|
+
let defaultWorldHeld = null;
|
|
652
|
+
/** The process's world: the public API's calls share its B and D. */
|
|
653
|
+
export function defaultWorld() {
|
|
654
|
+
if (defaultWorldHeld === null) {
|
|
655
|
+
defaultWorldHeld = new World(envConfig());
|
|
656
|
+
}
|
|
657
|
+
return defaultWorldHeld;
|
|
658
|
+
}
|
|
659
|
+
/**
|
|
660
|
+
* Points the public API at w instead of the process's environment. For the
|
|
661
|
+
* tests only (not exported from the package): they keep the public API away
|
|
662
|
+
* from the real /tmp/sockat-<uid>.
|
|
663
|
+
*/
|
|
664
|
+
export function setDefaultWorld(w) {
|
|
665
|
+
defaultWorldHeld = w;
|
|
666
|
+
}
|