@@ -16,6 +16,43 @@ trap "{ rm -r $IMGDIRECTORY; }" EXIT
1616
1717DIFFOSCOPE=" diffoscope"
1818
19+ function get_subguids() {
20+ local user=$( id -u)
21+ local subuids
22+ local subgids
23+ while IFS=: read uid start count ; do
24+ if [[ $user == $( id -u $uid ) ]] ; then
25+ subuids=" 1:$start :$count "
26+ break
27+ fi
28+ done < /etc/subuid
29+ while IFS=: read uid start count ; do
30+ if [[ $user == $( id -u $uid ) ]] ; then
31+ subgids=" 1:$start :$count "
32+ break
33+ fi
34+ done < /etc/subgid
35+ [[ $subuids && $subgids ]] || return 1
36+ printf " --uid_mapping %s --gid_mapping %s " " $subuids " " $subgids "
37+ }
38+
39+ # Desc: Enter a user namespace with virtual privileges
40+ function become_rootless() {
41+ (( rootless_userns)) || return
42+ (( __REPRO_NSJAIL == 1 )) && return
43+ local subguids=$( get_subguids)
44+ if (( $? )) ; then
45+ error " Your user has no subuids or subgids"
46+ exit 1
47+ fi
48+ exec nsjail -Mo --quiet --skip_setsid \
49+ --disable_clone_newnet --disable_clone_newpid \
50+ --disable_rlimit --disable_proc --keep_caps \
51+ --chroot / --cwd " $( pwd) " --rw \
52+ --uid 0 --gid 0 $subguids \
53+ --keep_env -E ' __REPRO_NSJAIL=1' -- " ${orig_argv[@]} "
54+ # exec become-root unshare --mount "${orig_argv[@]}"
55+ }
1956# Desc: Escalates privileges
2057orig_argv=(" $0 " " $@ " )
2158src_owner=${SUDO_USER:- $USER }
@@ -31,14 +68,16 @@ function check_root() {
3168}
3269
3370function require_userns_tools() {
34- if command -v become-root > /dev/null \
71+ # if command -v become-root >/dev/null \
72+ if command -v unshare > /dev/null \
3573 && command -v nsjail > /dev/null \
3674 && command -v fuse-overlayfs > /dev/null
3775 then
3876 return 0
3977 fi
40- warning " nsjail, fuse-overlayfs and become-root are necessary for rootless operation"
41- warning " https://github.com/giuseppe/become-root"
78+ warning " nsjail, fuse-overlayfs and unshare (util-linux) are necessary for rootless operation"
79+ # warning "nsjail, fuse-overlayfs and become-root are necessary for rootless operation"
80+ # warning "https://github.com/giuseppe/become-root"
4281 warning " https://github.com/containers/fuse-overlayfs"
4382 warning " https://github.com/google/nsjail"
4483 return 1
@@ -61,11 +100,11 @@ function umountoverlay() {
61100
62101# Use a private gpg keyring
63102function gpg() {
64- command gpg --homedir=" $BUILDDIRECTORY /_gnupg " " $@ "
103+ command gpg --homedir=" $BUILDDIRECTORY /gnupg " " $@ "
65104}
66105
67106function init_gnupg() {
68- [ ! -d " $BUILDDIRECTORY /_gnupg " ] && mkdir -p " $BUILDDIRECTORY /_gnupg "
107+ [ ! -d " $BUILDDIRECTORY /gnupg " ] && mkdir -p " $BUILDDIRECTORY /gnupg "
69108
70109 # ensure signing key is available
71110 gpg --auto-key-locate nodefault,wkd --locate-keys
[email protected] @@ -158,7 +197,6 @@ lock_close() {
158197 # shellcheck disable=2034
159198 exec {fd}>& -
160199}
161-
162200# Desc: Executes an command inside a given nspawn container
163201# 1: Container name
164202# 2: Command to execute
@@ -171,6 +209,8 @@ function exec_nspawn(){
171209 -E " PATH=/usr/local/sbin:/usr/local/bin:/usr/bin" \
172210 -D " $BUILDDIRECTORY /$container " " ${@: 2} "
173211}
212+
213+
174214# Desc: Executes an command inside a given nsjail container
175215# 1: Container name
176216# 2: Optional: one --bind=... bindmount option
@@ -203,6 +243,9 @@ function exec_container(){
203243 fi
204244}
205245
246+
247+
248+
206249# Desc: Removes the root container
207250function cleanup_root_volume(){
208251 warning " Removing root container..."
@@ -291,7 +334,9 @@ function init_chroot(){
291334 exec_container root pacman -R arch-install-scripts --noconfirm
292335 exec_container root locale-gen
293336
294- printf ' builduser ALL = NOPASSWD: /usr/bin/pacman\n' > " $BUILDDIRECTORY " /root/etc/sudoers.d/builduser-pacman
337+ printf ' %s\n\n' ' Defaults preserve_groups' \
338+ ' builduser ALL = NOPASSWD: /usr/bin/pacman' \
339+ > " $BUILDDIRECTORY " /root/etc/sudoers.d/builduser-pacman
295340 exec_container root useradd -m -G wheel -s /bin/bash -d /build builduser
296341 echo " keyserver-options auto-key-retrieve" | install -Dm644 /dev/stdin " $BUILDDIRECTORY /root" /build/.gnupg/gpg.conf
297342 exec_container root chown -R builduser /build/.gnupg
@@ -438,6 +483,7 @@ Usage:
438483General Options:
439484 -h Print this help message
440485 -d Run diffoscope if packages are not reproducible
486+ -r Run without root privileges in nsjail containers
441487__END__
442488}
443489
@@ -468,6 +514,7 @@ while getopts :hdorC:P:M: arg; do
468514 d) run_diffoscope=1;;
469515 r) rootless_userns=1;
470516 require_userns_tools || exit 1
517+ become_rootless
471518 # TODO: better detection for valid writable build directory
472519 [[ $BUILDDIRECTORY == /var/lib/repro ]] && BUILDDIRECTORY=" ${XDG_CACHE_HOME:- $HOME / .cache} /archlinux-repro"
473520 ;;
0 commit comments