Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/docs/developer/building-an-external-extension.md
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,8 @@ threads (`deploy.resources.limits.pids`) and `no-new-privileges`, which stops a
privileges through a setuid binary or file capability. Neither has a manifest
field. An image that starts as root and drops to its own user, for example with
`gosu`, keeps working; one that relies on `sudo` or a setuid helper does not, so
run it as an unprivileged user from the start. First-party services are not
affected. See the [service manifest](./service-manifest.md#quality-tiers) for
run it as an unprivileged user from the start. First-party services get no
process limit, and only some of them get the privilege lock. See the [service manifest](./service-manifest.md#quality-tiers) for
the full community sandbox.

### Migrate idempotently on boot
Expand Down
11 changes: 9 additions & 2 deletions docs/docs/developer/service-manifest.md
Original file line number Diff line number Diff line change
Expand Up @@ -745,8 +745,15 @@ service:
that starts as root and drops to its own user, for example with `gosu`, still
works; an image that relies on `sudo` or a setuid helper does not.

First-party services are unaffected: they keep whatever limits their manifests
declare, and the renderer adds neither field to them.
First-party services keep whatever memory and process limits their manifests
declare; the renderer adds no process limit to them. It does give them the same
`no-new-privileges` lock, except for the services in
`BUILT_IN_SERVICES_WITHOUT_PRIVILEGE_LOCK` in
`packages/core/src/services/compose-renderer.ts`. Those run third-party images
whose start-up no automated check exercises yet. A service leaves that list once
it has been started with the lock and found healthy, and a new first-party
service gets the lock unless it is shown to need privilege escalation. The lock
has no manifest field.

Community services retain namespaced named-volume persistence and outbound
internet access through their isolated bridge network. They cannot reach the
Expand Down
70 changes: 70 additions & 0 deletions packages/core/src/__tests__/built-in-privilege-lock.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { load as parseYaml } from "js-yaml";
import { describe, expect, it } from "vitest";
import {
BUILT_IN_SERVICES_WITHOUT_PRIVILEGE_LOCK,
renderCompose,
} from "../services/compose-renderer";
import { findServiceManifestDirs } from "../services/manifest-discovery";
import { serviceManifestSchema } from "../services/manifest-schema";
import type { LoadedService, ServiceManifest } from "../services/types";

const REPO_ROOT = join(import.meta.dirname, "..", "..", "..", "..");

function loadBuiltIns(): LoadedService[] {
return findServiceManifestDirs(join(REPO_ROOT, "services")).map((directory) => ({
manifest: serviceManifestSchema.parse(
JSON.parse(readFileSync(join(directory, "service.json"), "utf8")),
) as unknown as ServiceManifest,
directory,
isBuiltIn: true,
enabled: true,
}));
}

/** The whole built-in stack, rendered once with every service enabled. */
function renderedSecurityOptions(): Map<string, string[] | undefined> {
const compose = renderCompose(loadBuiltIns(), {
domain: "maps.example.test",
existsSync: () => true,
});
const services = (
parseYaml(compose.composeYaml) as {
services: Record<string, { security_opt?: string[] }>;
}
).services;
return new Map(Object.entries(services).map(([id, service]) => [id, service.security_opt]));
}

describe("privilege lock on built-in services", () => {
const rendered = renderedSecurityOptions();
const builtInIds = loadBuiltIns().map((service) => service.manifest.id);

it("renders every built-in service", () => {
expect([...rendered.keys()].sort()).toEqual([...builtInIds].sort());
});

it("locks every built-in service that is not listed as running without the lock", () => {
const neitherLockedNorListed = builtInIds.filter(
(id) =>
!BUILT_IN_SERVICES_WITHOUT_PRIVILEGE_LOCK.has(id) &&
!rendered.get(id)?.includes("no-new-privileges:true"),
);
expect(neitherLockedNorListed).toEqual([]);
});

it("leaves exactly the listed services without the lock", () => {
const listedButLocked = [...BUILT_IN_SERVICES_WITHOUT_PRIVILEGE_LOCK].filter(
(id) => rendered.get(id) !== undefined,
);
expect(listedButLocked).toEqual([]);
});

it("lists only services that still exist", () => {
const unknown = [...BUILT_IN_SERVICES_WITHOUT_PRIVILEGE_LOCK].filter(
(id) => !builtInIds.includes(id),
);
expect(unknown).toEqual([]);
});
});
28 changes: 24 additions & 4 deletions packages/core/src/__tests__/community-resource-limits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import { load } from "js-yaml";
import { describe, expect, it } from "vitest";
import { renderCompose, renderServiceSnippet } from "../services/compose-renderer";
import {
BUILT_IN_SERVICES_WITHOUT_PRIVILEGE_LOCK,
renderCompose,
renderServiceSnippet,
} from "../services/compose-renderer";
import { validateServiceManifest } from "../services/manifest-schema";
import type { LoadedService, ServiceManifest } from "../services/types";

Expand Down Expand Up @@ -113,16 +117,29 @@ describe("community container hardening", () => {
});
});

it("leaves a first-party service as the manifest declares it", () => {
it("gives a first-party service the privilege lock but only its declared limits", () => {
const snippet = renderServiceSnippet(service({}, { builtIn: true }), {});
expect(snippet).not.toHaveProperty("pids_limit");
expect(snippet).not.toHaveProperty("security_opt");
expect(snippet.security_opt).toEqual(["no-new-privileges:true"]);
expect(snippet.deploy).toBeUndefined();

const sized = renderServiceSnippet(service({ memory: "2g" }, { builtIn: true }), {});
expect(sized.deploy?.resources?.limits).toEqual({ memory: "2g" });
expect(sized).not.toHaveProperty("pids_limit");
expect(sized).not.toHaveProperty("security_opt");
expect(sized.security_opt).toEqual(["no-new-privileges:true"]);
});

it("leaves a listed first-party service without the privilege lock", () => {
const listed = service({}, { builtIn: true });
listed.manifest = { ...listed.manifest, id: "nominatim" };
expect(BUILT_IN_SERVICES_WITHOUT_PRIVILEGE_LOCK.has("nominatim")).toBe(true);
expect(renderServiceSnippet(listed, {})).not.toHaveProperty("security_opt");
});

it("locks a community service even when its id matches a listed first-party one", () => {
const community = service({ memory: "256m" }, { builtIn: false });
community.manifest = { ...community.manifest, id: "nominatim" };
expect(renderServiceSnippet(community, {}).security_opt).toEqual(["no-new-privileges:true"]);
});
});

Expand Down Expand Up @@ -196,6 +213,9 @@ describe.skipIf(!composeAvailable)(
expect(
project.services["first-party-limits"]?.deploy?.resources?.limits,
).not.toHaveProperty("pids");
expect(project.services["first-party-limits"]?.security_opt).toEqual([
"no-new-privileges:true",
]);
} finally {
rmSync(directory, { recursive: true, force: true });
}
Expand Down
42 changes: 42 additions & 0 deletions packages/core/src/services/compose-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,46 @@ export interface ComposeServiceSnippet {
*/
const COMMUNITY_PIDS_LIMIT = 512;

/**
* Built-in services that still run without the privilege lock
* (`no-new-privileges`). Every other built-in service gets it, as every
* community service does.
*
* The lock stops a process from gaining privileges through a setuid binary or a
* file capability. A service that starts as root and drops to its own user
* keeps working; one whose non-root process calls `sudo`, `su` or another
* setuid helper does not. The services listed here run third-party images
* whose start-up no automated check exercises, so nothing has shown yet that
* they start with the lock.
*
* Remove an id once the service has been started with the lock and found
* healthy. Never add an id unless the service is shown to need privilege
* escalation, and say why next to it.
*/
export const BUILT_IN_SERVICES_WITHOUT_PRIVILEGE_LOCK: ReadonlySet<string> = new Set([
"brouter",
"dawarich-app",
"dawarich-postgis",
"dawarich-redis",
"dawarich-sidekiq",
"elasticsearch",
"local-ai",
"martin",
"motis",
"motis-feed-proxy",
"motis-staging",
"nominatim",
"osrm",
"otp",
"overpass",
"pelias",
"pelias-pip",
"pelias-placeholder",
"photon",
"tileserver",
"valhalla",
]);

export function escapeComposeInterpolation(value: string): string {
return value.replaceAll("$", () => "$$");
}
Expand Down Expand Up @@ -932,6 +972,8 @@ export function renderServiceSnippet(
snippet.deploy = { ...snippet.deploy, resources };
snippet.security_opt = ["no-new-privileges:true"];
deepEscapeCommunitySnippet(snippet);
} else if (!BUILT_IN_SERVICES_WITHOUT_PRIVILEGE_LOCK.has(m.id)) {
snippet.security_opt = ["no-new-privileges:true"];
}
writeServiceConfigEnvReferences(snippet, m, ctx.serviceConfigEnvKeys?.get(m.id) ?? []);

Expand Down
24 changes: 24 additions & 0 deletions release-notes/built-in-privilege-lock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
impacts: [security]
---

## What changes

The core platform services now start with Docker's `no-new-privileges` option,
which community services already had. A process in these containers can no
longer gain privileges through a setuid binary or file capability. This covers
the API, web app, data manager, operations agent, Transitous runner, Traefik,
the well-known file server, PostGIS and Redis. The other bundled engines keep
their previous settings for now.

## Who is affected

All self-hosted installations. The listed containers are recreated on the first
`compose up` after the update, because their configuration has changed.

## Required action

None. The recreate happens as part of the normal update. Installations that
replaced one of the listed services' images with a custom image that runs
`sudo`, `su` or another setuid helper as a non-root user must change that image
to start as the user it needs.
Loading