mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 13:45:30 +00:00
* fix(process): recognize permission-denied user namespace peers * fix(process): fall back to UID maps when namespace links are denied --------- Co-authored-by: dhruvac29 <dhruvac29@users.noreply.github.com> Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
98 lines
3.4 KiB
TypeScript
98 lines
3.4 KiB
TypeScript
/** Real, effective, saved and filesystem IDs from one Linux status row. */
|
|
export type LinuxProcessIds = readonly [number, number, number, number];
|
|
|
|
/** Parse the single `Uid:` or `Gid:` row of one Linux `/proc/<pid>/status` record. */
|
|
export const parseLinuxProcessIds = (
|
|
value: string,
|
|
row: "Uid" | "Gid",
|
|
): LinuxProcessIds | undefined => {
|
|
let ids: LinuxProcessIds | undefined;
|
|
for (const line of value.split("\n")) {
|
|
if (!line.startsWith(`${row}:`)) continue;
|
|
if (ids !== undefined) return undefined;
|
|
const match =
|
|
/^[UG]id:\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s*$/u.exec(line);
|
|
if (match === null) return undefined;
|
|
const [real, effective, saved, filesystem] = match.slice(1).map(Number);
|
|
if (
|
|
real === undefined ||
|
|
effective === undefined ||
|
|
saved === undefined ||
|
|
filesystem === undefined ||
|
|
[real, effective, saved, filesystem].some(
|
|
(id) => !Number.isSafeInteger(id) || id < 0,
|
|
)
|
|
)
|
|
return undefined;
|
|
ids = [real, effective, saved, filesystem];
|
|
}
|
|
return ids;
|
|
};
|
|
|
|
/**
|
|
* Why Linux withholds `/proc/<pid>/environ` from a caller without
|
|
* CAP_SYS_PTRACE (PTRACE_MODE_READ_FSCREDS): the target's real, effective or
|
|
* saved UID/GID differs from the caller's, the target is non-dumpable (which
|
|
* procfs reports as root ownership of `/proc/<pid>/environ`), or the target
|
|
* occupies another user namespace.
|
|
*/
|
|
export type LinuxEnvironmentRestriction =
|
|
| "credentials"
|
|
| "non-dumpable"
|
|
| "user-namespace";
|
|
|
|
/** Different readable UID maps prove distinct namespaces; equal maps do not prove sameness. */
|
|
export const linuxUidMapsDiffer = (
|
|
target: string | undefined,
|
|
caller: string | undefined,
|
|
): boolean | undefined => {
|
|
const normalized = (value: string | undefined): string | undefined => {
|
|
if (value === undefined || value.trim() === "") return undefined;
|
|
const rows: string[] = [];
|
|
for (const line of value.trim().split("\n")) {
|
|
const match = /^\s*([0-9]+)\s+([0-9]+)\s+([0-9]+)\s*$/u.exec(line);
|
|
if (match === null) return undefined;
|
|
const numbers = match.slice(1).map(Number);
|
|
if (numbers.some((number) => !Number.isSafeInteger(number)))
|
|
return undefined;
|
|
rows.push(numbers.join(" "));
|
|
}
|
|
return rows.join("\n");
|
|
};
|
|
const targetMap = normalized(target);
|
|
const callerMap = normalized(caller);
|
|
return targetMap === undefined || callerMap === undefined
|
|
? undefined
|
|
: targetMap !== callerMap;
|
|
};
|
|
|
|
/** Derive the kernel's environment restriction from one generation's procfs facts. */
|
|
export const linuxEnvironmentRestriction = (facts: {
|
|
readonly uids: LinuxProcessIds;
|
|
readonly gids: LinuxProcessIds | undefined;
|
|
readonly procfsOwnerUid: number | undefined;
|
|
readonly callerUid: number | undefined;
|
|
readonly callerGid: number | undefined;
|
|
readonly userNamespaceDiffers?: boolean | undefined;
|
|
}): LinuxEnvironmentRestriction | undefined => {
|
|
const {
|
|
uids,
|
|
gids,
|
|
procfsOwnerUid,
|
|
callerUid,
|
|
callerGid,
|
|
userNamespaceDiffers,
|
|
} = facts;
|
|
if (
|
|
(callerUid !== undefined &&
|
|
uids.slice(0, 3).some((uid) => uid !== callerUid)) ||
|
|
(callerGid !== undefined &&
|
|
gids !== undefined &&
|
|
gids.slice(0, 3).some((gid) => gid !== callerGid))
|
|
)
|
|
return "credentials";
|
|
if (procfsOwnerUid !== undefined && procfsOwnerUid !== uids[1])
|
|
return "non-dumpable";
|
|
return userNamespaceDiffers === true ? "user-namespace" : undefined;
|
|
};
|