mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 11:36:05 +00:00
fix(docker): start under rootful Podman on hosts that block gosu's signals
Started as root, the entrypoint dropped to the node user with gosu. gosu is a Go program, and Go changes a process's ids by sending every runtime thread a realtime signal and spinning until each one has answered, with no limit. On a host whose security module drops that signal inside the container (AppArmor on recent Ubuntu, under rootful Podman) gosu spun at full CPU forever, before Node printed a line: no logs, no database, port 3000 never served. Running as uid 1000 worked only because that path skips gosu. The entrypoint now drops privileges with setpriv from util-linux, which is already in the image: single-threaded C that makes the same id changes without any signal. Node still runs as uid and gid 1000 with no capabilities and HOME=/home/node. gosu stays in the image for anyone who calls it by hand. Closes #3135
This commit is contained in:
+2
-2
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user