Configuring a repeatable chroot jail for consistent development builds
0 reputation · 23 Jan 2021, 12:35 UTC
0 reputation · 23 Jan 2021, 12:35 UTC
The goal is to establish a repeatable development environment by constructing a chroot jail that contains a minimal root filesystem (bin, lib, etc.) mirrored from the host. Inside this jail, a shell or build toolchain is launched so that all file accesses are confined to the jail’s directory tree, providing a consistent view of dependencies across different machines.
An unresolved decision concerns the privilege model to use after the chroot transition. Retaining root privileges inside the jail simplifies installing missing libraries but weakens the isolation boundary, whereas dropping to an unprivileged user improves security but may require pre‑provisioning all needed files and handling pseudo‑filesystems such as /proc and /sys that chroot does not isolate. This raises questions about the optimal privilege strategy and the reproducible population of special directories.
Should the jail be entered as root and then drop privileges, or should an unprivileged user be used from the start? How can /dev and /proc be populated in a repeatable way without compromising the isolation guarantee? What is the minimal set of directories and symlinks required for typical build tools to function while preserving the chroot boundary?
Enter the chroot as root, then immediately drop to an unprivileged user for the build session. This gives you the convenience of installing missing packages while keeping the runtime isolated.
Root is needed only to perform the chroot system call and to set up bind‑mounts for special filesystems. After the chroot, switching to a non‑root UID removes the ability to break out via ptrace or to modify the jail’s mount namespace, preserving the isolation boundary.
/srv/devjail.debootstrap (or equivalent) into the directory.ln -s lib/x86_64-linux-gnu/libc.so.6 lib64/ld-linux-x86-64.so.2 (adjust to your distro)ln -s ../usr/bin make if needed, etc.devtmpfs on dev: mount -t devtmpfs none /srv/devjail/devmount -t proc proc /srv/devjail/proc -o hidepid=2mount -t sysfs sysfs /srv/devjail/syschroot /srv/devjail /usr/bin/env -i HOME=/root PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin /bin/bash
builder with your account):
su - builder
or use setuidgid builder /bin/bash if available.
/srv/devjail.After dropping to the builder user, run:
ls -l / to confirm only the expected directories (bin, lib, lib64, usr, etc.) are present.cat /proc/version – it will show the host kernel, confirming the proc mount is active.strace -f -e trace=chroot make to see that the chroot call is made once and subsequent open calls stay under the jail path.If you need to install additional libraries while still root, do it before the su - builder step; otherwise pre‑provision all required packages in the base rootfs.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.