Skip to content

Commit a66d091

Browse files
fix(server): root under sudo does not reach its own user manager
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4edd982 commit a66d091

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

‎apps/server/src/cloud/bootService.test.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,16 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
120120
/** `undefined` makes `loginctl show-user` fail, as when logind does not know the user. */
121121
linger: string | undefined;
122122
managerReachable: boolean;
123+
/** The user bus is found only through `XDG_RUNTIME_DIR`, as without a login session. */
124+
busNeedsRuntimeDir: boolean;
123125
lingerStartsManager: boolean;
124126
enabled: boolean;
125127
active: boolean;
126128
} = {
127129
failCommand: undefined,
128130
linger: "yes",
129131
managerReachable: true,
132+
busNeedsRuntimeDir: false,
130133
lingerStartsManager: true,
131134
enabled: true,
132135
active: true,
@@ -186,7 +189,8 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
186189
failed ||
187190
unitOutsideManager ||
188191
((command === "systemctl --user show-environment" || input.command === "busctl") &&
189-
!control.managerReachable) ||
192+
(!control.managerReachable ||
193+
(control.busNeedsRuntimeDir && input.env?.XDG_RUNTIME_DIR === undefined))) ||
190194
(input.command === "loginctl" &&
191195
input.args[0] === "show-user" &&
192196
control.linger === undefined) ||
@@ -377,6 +381,20 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
377381
}),
378382
);
379383

384+
it.effect("does not reach a running root user manager under sudo", () =>
385+
Effect.gen(function* () {
386+
const { makeService, commands, control } = yield* makeHarness();
387+
control.busNeedsRuntimeDir = true;
388+
const service = yield* makeService(undefined, "1.2.3", undefined, false, undefined, true);
389+
390+
expect(yield* service.install().pipe(Effect.flip)).toMatchObject({
391+
_tag: "BootServicePrerequisiteError",
392+
problem: "user-manager-unavailable",
393+
});
394+
expect(commands).not.toContain("systemctl --user enable t3code.service");
395+
}),
396+
);
397+
380398
it.effect("does not enable lingering for root under sudo", () =>
381399
Effect.gen(function* () {
382400
const { makeService, commands, control } = yield* makeHarness();

‎apps/server/src/cloud/bootService.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,9 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
581581
const xdgConfigHome = yield* Config.String("XDG_CONFIG_HOME").pipe(Config.withDefault(""));
582582
const installerPath = yield* Config.String("PATH").pipe(Config.withDefault(""));
583583
const xdgRuntimeDir = yield* Config.String("XDG_RUNTIME_DIR").pipe(Config.withDefault(""));
584-
const sudoUser = yield* Config.String("SUDO_USER").pipe(Config.withDefault(""));
584+
// Root under sudo must not reach or start a separate root service.
585+
const rootUnderSudo =
586+
uid === 0 && (yield* Config.String("SUDO_USER").pipe(Config.withDefault(""))) !== "";
585587
const runningInsideService =
586588
(yield* Config.String(BOOT_SERVICE_UNIT_ENV).pipe(Config.withDefault(""))) ===
587589
BOOT_SERVICE_UNIT_FILE;
@@ -591,7 +593,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
591593
// Without a login session nothing sets this, so `--user` commands cannot find the user bus.
592594
// The directory only appears once lingering starts the manager, so it need not exist yet.
593595
const userManagerEnv =
594-
platform === "linux" && xdgRuntimeDir === "" && uid !== undefined
596+
platform === "linux" && xdgRuntimeDir === "" && uid !== undefined && !rootUnderSudo
595597
? { XDG_RUNTIME_DIR: `/run/user/${uid}` }
596598
: undefined;
597599
const runner: ProcessRunner.ProcessRunner["Service"] =
@@ -773,11 +775,10 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
773775
const firstProblem = problems[0];
774776
if (firstProblem === undefined) return;
775777
// Logind reports a user without a session, whose manager is not running yet, as linger-unavailable.
776-
// Root under sudo has no session either, but must not get a separate root service.
777778
const withoutSession =
778779
problems.includes("linger-unavailable") &&
779780
problems.includes("user-manager-unavailable") &&
780-
!(uid === 0 && sudoUser !== "");
781+
!rootUnderSudo;
781782
const lingerDisabled = problems.includes("linger-disabled");
782783
if (!lingerDisabled && !withoutSession) {
783784
return yield* new BootServicePrerequisiteError({ problem: firstProblem });

0 commit comments

Comments
 (0)