diff --git a/Dockerfile b/Dockerfile index 8aad504d3..eca051954 100644 --- a/Dockerfile +++ b/Dockerfile @@ -94,8 +94,8 @@ RUN echo 'deb [signed-by=/etc/apt/keyrings/pgdg.asc] http://apt.postgresql.org/p rm -rf /var/lib/apt/lists/* /usr/local/lib/node_modules/npm /usr/local/bin/npm /usr/local/bin/npx && \ chown -R node:node /app -# gosu rebuilt with a current Go toolchain (stage 0) — the entrypoint script uses -# it to drop to node. +# gosu rebuilt with a current Go toolchain (stage 0). The entrypoint drops to node +# with setpriv since #3135; gosu stays in the image for anyone who calls it by hand. COPY --from=gosu-build /out/gosu /usr/local/bin/gosu ENV XDG_CACHE_HOME=/tmp/kf6-cache diff --git a/server/scripts/entrypoint.sh b/server/scripts/entrypoint.sh index d5f63b666..0180e32f6 100644 --- a/server/scripts/entrypoint.sh +++ b/server/scripts/entrypoint.sh @@ -25,7 +25,7 @@ fi cd /app/server # Already started as a non-root user (Kubernetes runAsUser, docker --user): there -# is nothing to chown and no privilege to drop, and gosu would only fail to switch +# is nothing to chown and no privilege to drop, and setpriv would only fail to switch # users. Run Node as whoever we are; the volumes are that user's to provide. if [ "$(id -u)" != "0" ]; then exec node --require tsconfig-paths/register dist/index.js @@ -42,7 +42,7 @@ fi # Troubleshooting page of the wiki has the one-off chown for that. # # Only the owning user has to be node, because that is what Node runs as and -# what gosu leaves it with. The group is left to whoever set it: a Kubernetes +# what the drop below leaves it with. The group is left to whoever set it: a Kubernetes # fsGroup other than 1000 gives every file its own gid, and taking that back # here would make the kubelet's OnRootMismatch walk the volume again on the # next start, and this script after it, every time. Only a root group (a fresh @@ -65,4 +65,11 @@ for dir in /app/data /app/uploads; do fi done -exec gosu node node --require tsconfig-paths/register dist/index.js +# setpriv, not gosu: gosu is Go, and Go changes the ids of a process by signalling +# every runtime thread with a realtime signal and spinning until each one has +# answered. A host whose security module drops that signal inside the container +# (AppArmor on recent Ubuntu under Podman) left it spinning at full CPU, before +# Node had printed a line (#3135). setpriv is single-threaded C and makes the same +# id changes without any signal. It leaves HOME alone, which gosu set from passwd. +export HOME=/home/node +exec setpriv --reuid=node --regid=node --init-groups -- node --require tsconfig-paths/register dist/index.js diff --git a/server/tests/unit/docker-entrypoint.test.ts b/server/tests/unit/docker-entrypoint.test.ts index bd0b595b7..0c7486776 100644 --- a/server/tests/unit/docker-entrypoint.test.ts +++ b/server/tests/unit/docker-entrypoint.test.ts @@ -87,20 +87,26 @@ describe('entrypoint script', () => { }); // Under runAsUser: 1000 there is no privilege to drop and gosu cannot switch - // users, so the non-root branch has to exec Node itself before any chown or gosu. - it('starts Node directly, without chown or gosu, when not running as root', () => { + // users, so the non-root branch has to exec Node itself before any chown or privilege drop. + it('starts Node directly, without chown or a privilege drop, when not running as root', () => { const nonRootCheck = code.indexOf('if [ "$(id -u)" != "0" ]; then'); expect(nonRootCheck).toBeGreaterThan(-1); const branchEnd = code.indexOf('\nfi\n', nonRootCheck); expect(branchEnd).toBeGreaterThan(nonRootCheck); const branch = code.slice(nonRootCheck, branchEnd); expect(branch).toContain('exec node --require tsconfig-paths/register dist/index.js'); - expect(branch).not.toContain('gosu'); + expect(branch).not.toContain('setpriv'); expect(code.indexOf('chown')).toBeGreaterThan(branchEnd); - expect(code.indexOf('gosu')).toBeGreaterThan(branchEnd); + expect(code.indexOf('setpriv')).toBeGreaterThan(branchEnd); }); - it('still drops to node through gosu when started as root', () => { - expect(source).toContain('exec gosu node node --require tsconfig-paths/register dist/index.js'); + // gosu is Go: it changes the ids by signalling every runtime thread and spinning until + // each answers, so a host that drops that signal hung it at full CPU (#3135). + it('drops to node through setpriv, not gosu, when started as root', () => { + expect(source).toContain( + 'exec setpriv --reuid=node --regid=node --init-groups -- node --require tsconfig-paths/register dist/index.js', + ); + expect(source).toContain('export HOME=/home/node'); + expect(source).not.toMatch(/^\s*exec gosu/m); }); });