minstall: work around broken environments with missing UIDs

Running some container-like mechanisms such as chroot(1) from sudo, can
result in a new isolated environment where the environment variables
exist but no users exist. From there, a build is performed as root but
installation fails when we try to look up the passwd database entry for
the user outside of the chroot.

Proper container mechanisms such as systemd-nspawn, and even improper
ones like docker, sanitize this and ensure those stale environment
variables don't exist anymore. But chroot is very low-level.

Avoid crashing when this happens.

Fixes #11662
pull/11667/head
Eli Schwartz 2 years ago
parent 9a77c45e41
commit 3bc2236c59
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
  1. 12
      mesonbuild/minstall.py

@ -778,10 +778,18 @@ def rebuild_all(wd: str, backend: str) -> bool:
orig_user = env.pop('SUDO_USER')
orig_uid = env.pop('SUDO_UID', 0)
orig_gid = env.pop('SUDO_GID', 0)
homedir = pwd.getpwuid(int(orig_uid)).pw_dir
try:
homedir = pwd.getpwuid(int(orig_uid)).pw_dir
except KeyError:
# `sudo chroot` leaves behind stale variable and builds as root without a user
return None, None
elif os.environ.get('DOAS_USER') is not None:
orig_user = env.pop('DOAS_USER')
pwdata = pwd.getpwnam(orig_user)
try:
pwdata = pwd.getpwnam(orig_user)
except KeyError:
# `doas chroot` leaves behind stale variable and builds as root without a user
return None, None
orig_uid = pwdata.pw_uid
orig_gid = pwdata.pw_gid
homedir = pwdata.pw_dir

Loading…
Cancel
Save