Recompile gosu with latest Go version
Learn how to recompile gosu with the latest Go version (1.21.4) to mitigate security concerns.
Gosu is a small, open-source tool that allows you to run a command as a different user or group, similar to the sudo
command, but with more flexibility and control. It provides a way to execute a command with a specific user ID, group ID, and supplementary groups, making it useful for scenarios where you need to run a command with elevated privileges. Gosu is often used in Docker containers to run the application as non-root.
The issue at hand is that gosu
, a popular tool, is currently built with Go 1.18.x
, a deprecated version that is no longer receiving functional or vulnerability fixes.
In this article, we will walk you through a simple and effective solution to mitigate these security concerns by recompiling gosu
with the latest Go version (1.21.4).
Recompiling Gosu with the Latest Go Version
To begin, let's check out the gosu
Git repository and make a few modifications to the Dockerfile.
- Checkout the Gosu Git repo: Clone the Gosu repository from GitHub using the command
git clone https://github.com/tianon/gosu.git
. - Update the Dockerfile: Open the Dockerfile and update the
FROM
instruction toFROM golang:1.21.4-bookworm
. This will ensure that we're using the latest Go version to compile Gosu. - Build the image: Run the command
docker build -t my-gosu-image.
to build a new image with the updated Go version.
Using Multi-Stage Builds to Copy the Gosu Binary
Now that we have a new image with the latest Go version, let's use a multi-stage build to copy the Gosu binary out and use it in our image.
Here's an example Dockerfile:
# Stage 1: Get gosu from another image
FROM tianon/gosu:latest AS gosu
# Stage 2: Your actual build
FROM your-base-image
ARG PLATFORM
COPY --from=gosu /go/bin/gosu-${PLATFORM} /usr/local/bin/gosu
RUN chmod +x /usr/local/bin/gosu
In this example, we're using the FROM
instruction to create a new stage that copies the gosu
binary from the gosu
image. We then use the COPY
instruction to copy the binary to our actual build stage. Remember to use the right platform, like amd64
, arm64
, i386
, etc.
More information