2023-04-09 17:50:02 +00:00
|
|
|
{ lib
|
2023-04-22 12:31:27 +00:00
|
|
|
, stdenv
|
2023-04-09 17:50:02 +00:00
|
|
|
, callPackage
|
|
|
|
, runCommandLocal
|
|
|
|
, writeShellScript
|
|
|
|
, glibc
|
|
|
|
, pkgsi686Linux
|
2024-09-13 16:53:16 +00:00
|
|
|
, runCommandCC
|
2023-04-09 17:50:02 +00:00
|
|
|
, coreutils
|
|
|
|
, bubblewrap
|
|
|
|
}:
|
2020-10-30 18:22:04 +00:00
|
|
|
|
2023-11-30 00:28:38 +00:00
|
|
|
{ runScript ? "bash"
|
2024-09-25 19:43:00 +00:00
|
|
|
, nativeBuildInputs ? []
|
2020-12-09 02:20:44 +00:00
|
|
|
, extraInstallCommands ? ""
|
|
|
|
, meta ? {}
|
|
|
|
, passthru ? {}
|
2023-12-09 05:29:32 +00:00
|
|
|
, extraPreBwrapCmds ? ""
|
2021-12-29 20:30:44 +00:00
|
|
|
, extraBwrapArgs ? []
|
buildFHSEnv: disable security features by default
The implicit contract of buildFHSUserEnv was that it allows to run
software built for a typical GNU/Linux distribution (not NixOS) without
patching it (patchelf, autoPatchelfHook, etc.). Note that this does not
inherently imply running untrusted programs.
buildFHSUserEnv was implemented by using chroot and assembling a
standard-compliant FHS environment in the new root. As expected, this
did not provide any kind of isolation between the system and the
programs.
However, when it was later reimplemented using bubblewrap
(PR #225748), which *is* a security tool, several isolation features
involving detaches Linux namespaces were turned on by default.
This decision has introduced a number of breakages that are very
difficult to debug and trace back to this change.
For example: `unshareIPC` breaks software audio mixing in programs using
ALSA (dmix) and `unsharePID` breaks gdb,
Since:
1. the security features were enable without any clear threat model;
2. `buildFHSEnvBubblewrap` is supposed to be a drop-in replacement of
`buildFHSEnvChrootenv` (see the release notes for NixOS 23.05);
3. the change is breaking in several common cases (security does not
come for free);
4. the contract was not changed, or at least communicated in a clear
way to the users;
all security features should be turned off by default.
P.S. It would be useful to create a variant of buildFHSEnv that does
provide some isolation. This could unshare some namespaces and mount
only limited parts of the filesystem.
Note that buildFHSEnv mounts every directory in / under the new root, so
again, very little is gained by unsharing alone.
2023-09-08 06:58:31 +00:00
|
|
|
, unshareUser ? false
|
|
|
|
, unshareIpc ? false
|
|
|
|
, unsharePid ? false
|
2020-12-09 02:20:44 +00:00
|
|
|
, unshareNet ? false
|
buildFHSEnv: disable security features by default
The implicit contract of buildFHSUserEnv was that it allows to run
software built for a typical GNU/Linux distribution (not NixOS) without
patching it (patchelf, autoPatchelfHook, etc.). Note that this does not
inherently imply running untrusted programs.
buildFHSUserEnv was implemented by using chroot and assembling a
standard-compliant FHS environment in the new root. As expected, this
did not provide any kind of isolation between the system and the
programs.
However, when it was later reimplemented using bubblewrap
(PR #225748), which *is* a security tool, several isolation features
involving detaches Linux namespaces were turned on by default.
This decision has introduced a number of breakages that are very
difficult to debug and trace back to this change.
For example: `unshareIPC` breaks software audio mixing in programs using
ALSA (dmix) and `unsharePID` breaks gdb,
Since:
1. the security features were enable without any clear threat model;
2. `buildFHSEnvBubblewrap` is supposed to be a drop-in replacement of
`buildFHSEnvChrootenv` (see the release notes for NixOS 23.05);
3. the change is breaking in several common cases (security does not
come for free);
4. the contract was not changed, or at least communicated in a clear
way to the users;
all security features should be turned off by default.
P.S. It would be useful to create a variant of buildFHSEnv that does
provide some isolation. This could unshare some namespaces and mount
only limited parts of the filesystem.
Note that buildFHSEnv mounts every directory in / under the new root, so
again, very little is gained by unsharing alone.
2023-09-08 06:58:31 +00:00
|
|
|
, unshareUts ? false
|
|
|
|
, unshareCgroup ? false
|
2023-12-09 05:29:32 +00:00
|
|
|
, privateTmp ? false
|
2021-04-08 05:16:30 +00:00
|
|
|
, dieWithParent ? true
|
2020-12-09 02:20:44 +00:00
|
|
|
, ...
|
2023-04-09 17:50:02 +00:00
|
|
|
} @ args:
|
|
|
|
|
2023-11-30 00:28:38 +00:00
|
|
|
assert (!args ? pname || !args ? version) -> (args ? name); # You must provide name if pname or version (preferred) is missing.
|
2020-07-31 12:58:03 +00:00
|
|
|
|
|
|
|
let
|
2024-03-12 23:03:30 +00:00
|
|
|
inherit (lib)
|
|
|
|
concatLines
|
|
|
|
concatStringsSep
|
|
|
|
escapeShellArgs
|
|
|
|
filter
|
|
|
|
optionalString
|
|
|
|
splitString
|
|
|
|
;
|
|
|
|
|
|
|
|
inherit (lib.attrsets) removeAttrs;
|
|
|
|
|
2023-11-30 00:28:38 +00:00
|
|
|
name = args.name or "${args.pname}-${args.version}";
|
|
|
|
executableName = args.pname or args.name;
|
|
|
|
# we don't know which have been supplied, and want to avoid defaulting missing attrs to null. Passed into runCommandLocal
|
|
|
|
nameAttrs = lib.filterAttrs (key: value: builtins.elem key [ "name" "pname" "version" ]) args;
|
2023-04-09 17:50:02 +00:00
|
|
|
|
2023-03-07 19:35:30 +00:00
|
|
|
buildFHSEnv = callPackage ./buildFHSEnv.nix { };
|
2020-12-09 02:20:44 +00:00
|
|
|
|
2023-12-10 11:19:26 +00:00
|
|
|
fhsenv = buildFHSEnv (removeAttrs args [
|
2023-12-09 05:29:32 +00:00
|
|
|
"runScript" "extraInstallCommands" "meta" "passthru" "extraPreBwrapCmds" "extraBwrapArgs" "dieWithParent"
|
|
|
|
"unshareUser" "unshareCgroup" "unshareUts" "unshareNet" "unsharePid" "unshareIpc" "privateTmp"
|
2019-02-20 15:22:17 +00:00
|
|
|
]);
|
2020-07-31 12:58:03 +00:00
|
|
|
|
2023-03-17 17:00:03 +00:00
|
|
|
etcBindEntries = let
|
2019-09-15 13:29:53 +00:00
|
|
|
files = [
|
|
|
|
# NixOS Compatibility
|
|
|
|
"static"
|
2021-04-08 05:16:30 +00:00
|
|
|
"nix" # mainly for nixUnstable users, but also for access to nix/netrc
|
|
|
|
# Shells
|
2022-10-06 11:53:05 +00:00
|
|
|
"shells"
|
2021-04-08 05:16:30 +00:00
|
|
|
"bashrc"
|
|
|
|
"zshenv"
|
|
|
|
"zshrc"
|
|
|
|
"zinputrc"
|
|
|
|
"zprofile"
|
2019-09-15 13:29:53 +00:00
|
|
|
# Users, Groups, NSS
|
|
|
|
"passwd"
|
|
|
|
"group"
|
|
|
|
"shadow"
|
|
|
|
"hosts"
|
|
|
|
"resolv.conf"
|
|
|
|
"nsswitch.conf"
|
2021-01-26 00:41:50 +00:00
|
|
|
# User profiles
|
|
|
|
"profiles"
|
2019-09-15 13:29:53 +00:00
|
|
|
# Sudo & Su
|
|
|
|
"login.defs"
|
|
|
|
"sudoers"
|
|
|
|
"sudoers.d"
|
|
|
|
# Time
|
|
|
|
"localtime"
|
|
|
|
"zoneinfo"
|
|
|
|
# Other Core Stuff
|
|
|
|
"machine-id"
|
|
|
|
"os-release"
|
|
|
|
# PAM
|
|
|
|
"pam.d"
|
|
|
|
# Fonts
|
|
|
|
"fonts"
|
|
|
|
# ALSA
|
2021-07-10 10:50:52 +00:00
|
|
|
"alsa"
|
2019-09-15 13:29:53 +00:00
|
|
|
"asound.conf"
|
|
|
|
# SSL
|
|
|
|
"ssl/certs"
|
2021-10-27 16:55:29 +00:00
|
|
|
"ca-certificates"
|
2019-09-15 13:29:53 +00:00
|
|
|
"pki"
|
|
|
|
];
|
2023-03-17 17:00:03 +00:00
|
|
|
in map (path: "/etc/${path}") files;
|
2019-09-15 13:29:53 +00:00
|
|
|
|
2024-09-13 16:53:16 +00:00
|
|
|
# Here's the problem case:
|
|
|
|
# - we need to run bash to run the init script
|
|
|
|
# - LD_PRELOAD may be set to another dynamic library, requiring us to discover its dependencies
|
|
|
|
# - oops! ldconfig is part of the init script, and it hasn't run yet
|
|
|
|
# - everything explodes
|
|
|
|
#
|
|
|
|
# In particular, this happens with fhsenvs in fhsenvs, e.g. when running
|
|
|
|
# a wrapped game from Steam.
|
|
|
|
#
|
|
|
|
# So, instead of doing that, we build a tiny static (important!) shim
|
|
|
|
# that executes ldconfig in a completely clean environment to generate
|
|
|
|
# the initial cache, and then execs into the "real" init, which is the
|
|
|
|
# first time we see anything dynamically linked at all.
|
|
|
|
#
|
|
|
|
# Also, the real init is placed strategically at /init, so we don't
|
|
|
|
# have to recompile this every time.
|
|
|
|
containerInit = runCommandCC "container-init" {
|
|
|
|
buildInputs = [ stdenv.cc.libc.static or null ];
|
|
|
|
} ''
|
|
|
|
$CXX -static -s -o $out ${./container-init.cc}
|
2020-10-30 18:22:04 +00:00
|
|
|
'';
|
2024-09-13 16:53:16 +00:00
|
|
|
|
|
|
|
realInit = run: writeShellScript "${name}-init" ''
|
2019-02-20 15:22:17 +00:00
|
|
|
source /etc/profile
|
2023-05-21 10:43:19 +00:00
|
|
|
exec ${run} "$@"
|
2019-02-20 15:22:17 +00:00
|
|
|
'';
|
|
|
|
|
2024-03-12 23:03:30 +00:00
|
|
|
indentLines = str: concatLines (map (s: " " + s) (filter (s: s != "") (splitString "\n" str)));
|
2020-08-17 07:49:34 +00:00
|
|
|
bwrapCmd = { initArgs ? "" }: ''
|
2024-03-12 23:03:30 +00:00
|
|
|
ignored=(/nix /dev /proc /etc ${optionalString privateTmp "/tmp"})
|
2020-09-20 15:02:08 +00:00
|
|
|
ro_mounts=()
|
2020-10-24 15:23:37 +00:00
|
|
|
symlinks=()
|
2023-03-17 17:00:03 +00:00
|
|
|
etc_ignored=()
|
2024-02-04 20:30:24 +00:00
|
|
|
|
2024-09-25 09:32:41 +00:00
|
|
|
${extraPreBwrapCmds}
|
|
|
|
|
2024-02-04 20:30:24 +00:00
|
|
|
# loop through all entries of root in the fhs environment, except its /etc.
|
2023-03-07 19:35:30 +00:00
|
|
|
for i in ${fhsenv}/*; do
|
2020-07-31 12:58:03 +00:00
|
|
|
path="/''${i##*/}"
|
2019-09-15 13:29:53 +00:00
|
|
|
if [[ $path == '/etc' ]]; then
|
2020-10-24 15:23:37 +00:00
|
|
|
:
|
|
|
|
elif [[ -L $i ]]; then
|
2021-03-11 13:27:14 +00:00
|
|
|
symlinks+=(--symlink "$(${coreutils}/bin/readlink "$i")" "$path")
|
2023-03-17 17:00:14 +00:00
|
|
|
ignored+=("$path")
|
2020-10-24 15:23:37 +00:00
|
|
|
else
|
|
|
|
ro_mounts+=(--ro-bind "$i" "$path")
|
2023-03-17 17:00:14 +00:00
|
|
|
ignored+=("$path")
|
2019-09-15 13:29:53 +00:00
|
|
|
fi
|
2020-07-31 12:58:03 +00:00
|
|
|
done
|
|
|
|
|
2024-02-04 20:30:24 +00:00
|
|
|
# loop through the entries of /etc in the fhs environment.
|
2023-03-07 19:35:30 +00:00
|
|
|
if [[ -d ${fhsenv}/etc ]]; then
|
|
|
|
for i in ${fhsenv}/etc/*; do
|
2019-09-15 13:29:53 +00:00
|
|
|
path="/''${i##*/}"
|
2021-02-22 19:54:04 +00:00
|
|
|
# NOTE: we're binding /etc/fonts and /etc/ssl/certs from the host so we
|
|
|
|
# don't want to override it with a path from the FHS environment.
|
|
|
|
if [[ $path == '/fonts' || $path == '/ssl' ]]; then
|
2021-01-26 00:42:32 +00:00
|
|
|
continue
|
|
|
|
fi
|
2024-02-04 20:30:24 +00:00
|
|
|
if [[ -L $i ]]; then
|
|
|
|
symlinks+=(--symlink "$i" "/etc$path")
|
|
|
|
else
|
|
|
|
ro_mounts+=(--ro-bind "$i" "/etc$path")
|
|
|
|
fi
|
2023-03-17 17:00:03 +00:00
|
|
|
etc_ignored+=("/etc$path")
|
2019-09-15 13:29:53 +00:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2023-12-16 07:04:09 +00:00
|
|
|
# propagate /etc from the actual host if nested
|
|
|
|
if [[ -e /.host-etc ]]; then
|
|
|
|
ro_mounts+=(--ro-bind /.host-etc /.host-etc)
|
|
|
|
else
|
|
|
|
ro_mounts+=(--ro-bind /etc /.host-etc)
|
|
|
|
fi
|
|
|
|
|
2024-02-04 20:30:24 +00:00
|
|
|
# link selected etc entries from the actual root
|
2024-03-12 23:03:30 +00:00
|
|
|
for i in ${escapeShellArgs etcBindEntries}; do
|
2023-03-17 17:00:03 +00:00
|
|
|
if [[ "''${etc_ignored[@]}" =~ "$i" ]]; then
|
|
|
|
continue
|
|
|
|
fi
|
2023-12-09 05:26:55 +00:00
|
|
|
if [[ -e $i ]]; then
|
|
|
|
symlinks+=(--symlink "/.host-etc/''${i#/etc/}" "$i")
|
2023-03-17 17:00:03 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2020-10-28 20:00:17 +00:00
|
|
|
declare -a auto_mounts
|
2019-02-20 15:22:17 +00:00
|
|
|
# loop through all directories in the root
|
|
|
|
for dir in /*; do
|
2023-03-17 17:00:14 +00:00
|
|
|
# if it is a directory and it is not ignored
|
|
|
|
if [[ -d "$dir" ]] && [[ ! "''${ignored[@]}" =~ "$dir" ]]; then
|
2019-02-20 15:22:17 +00:00
|
|
|
# add it to the mount list
|
2020-10-28 20:00:17 +00:00
|
|
|
auto_mounts+=(--bind "$dir" "$dir")
|
2019-02-20 15:22:17 +00:00
|
|
|
fi
|
|
|
|
done
|
2020-07-31 12:58:03 +00:00
|
|
|
|
2023-02-11 14:30:18 +00:00
|
|
|
declare -a x11_args
|
|
|
|
# Always mount a tmpfs on /tmp/.X11-unix
|
|
|
|
# Rationale: https://github.com/flatpak/flatpak/blob/be2de97e862e5ca223da40a895e54e7bf24dbfb9/common/flatpak-run.c#L277
|
|
|
|
x11_args+=(--tmpfs /tmp/.X11-unix)
|
|
|
|
|
|
|
|
# Try to guess X socket path. This doesn't cover _everything_, but it covers some things.
|
2024-05-16 15:14:07 +00:00
|
|
|
if [[ "$DISPLAY" == *:* ]]; then
|
|
|
|
# recover display number from $DISPLAY formatted [host]:num[.screen]
|
|
|
|
display_nr=''${DISPLAY/#*:} # strip host
|
|
|
|
display_nr=''${display_nr/%.*} # strip screen
|
2023-02-11 14:30:18 +00:00
|
|
|
local_socket=/tmp/.X11-unix/X$display_nr
|
|
|
|
x11_args+=(--ro-bind-try "$local_socket" "$local_socket")
|
|
|
|
fi
|
|
|
|
|
2024-03-12 23:03:30 +00:00
|
|
|
${optionalString privateTmp ''
|
2023-12-21 16:25:21 +00:00
|
|
|
# sddm places XAUTHORITY in /tmp
|
|
|
|
if [[ "$XAUTHORITY" == /tmp/* ]]; then
|
|
|
|
x11_args+=(--ro-bind-try "$XAUTHORITY" "$XAUTHORITY")
|
2024-01-09 17:29:53 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# dbus-run-session puts the socket in /tmp
|
|
|
|
IFS=";" read -ra addrs <<<"$DBUS_SESSION_BUS_ADDRESS"
|
|
|
|
for addr in "''${addrs[@]}"; do
|
|
|
|
[[ "$addr" == unix:* ]] || continue
|
|
|
|
IFS="," read -ra parts <<<"''${addr#unix:}"
|
|
|
|
for part in "''${parts[@]}"; do
|
|
|
|
printf -v part '%s' "''${part//\\/\\\\}"
|
|
|
|
printf -v part '%b' "''${part//%/\\x}"
|
|
|
|
[[ "$part" == path=/tmp/* ]] || continue
|
|
|
|
x11_args+=(--ro-bind-try "''${part#path=}" "''${part#path=}")
|
|
|
|
done
|
|
|
|
done
|
|
|
|
''}
|
2023-12-21 16:25:21 +00:00
|
|
|
|
2020-09-20 15:02:08 +00:00
|
|
|
cmd=(
|
|
|
|
${bubblewrap}/bin/bwrap
|
|
|
|
--dev-bind /dev /dev
|
|
|
|
--proc /proc
|
|
|
|
--chdir "$(pwd)"
|
2024-03-12 23:03:30 +00:00
|
|
|
${optionalString unshareUser "--unshare-user"}
|
|
|
|
${optionalString unshareIpc "--unshare-ipc"}
|
|
|
|
${optionalString unsharePid "--unshare-pid"}
|
|
|
|
${optionalString unshareNet "--unshare-net"}
|
|
|
|
${optionalString unshareUts "--unshare-uts"}
|
|
|
|
${optionalString unshareCgroup "--unshare-cgroup"}
|
|
|
|
${optionalString dieWithParent "--die-with-parent"}
|
2020-09-20 15:02:08 +00:00
|
|
|
--ro-bind /nix /nix
|
2024-03-12 23:03:30 +00:00
|
|
|
${optionalString privateTmp "--tmpfs /tmp"}
|
2021-02-23 14:44:16 +00:00
|
|
|
# Our glibc will look for the cache in its own path in `/nix/store`.
|
|
|
|
# As such, we need a cache to exist there, because pressure-vessel
|
|
|
|
# depends on the existence of an ld cache. However, adding one
|
|
|
|
# globally proved to be a bad idea (see #100655), the solution we
|
|
|
|
# settled on being mounting one via bwrap.
|
|
|
|
# Also, the cache needs to go to both 32 and 64 bit glibcs, for games
|
|
|
|
# of both architectures to work.
|
2020-10-30 18:22:04 +00:00
|
|
|
--tmpfs ${glibc}/etc \
|
2023-11-13 01:02:53 +00:00
|
|
|
--tmpfs /etc \
|
2020-10-30 18:22:04 +00:00
|
|
|
--symlink /etc/ld.so.conf ${glibc}/etc/ld.so.conf \
|
|
|
|
--symlink /etc/ld.so.cache ${glibc}/etc/ld.so.cache \
|
|
|
|
--ro-bind ${glibc}/etc/rpc ${glibc}/etc/rpc \
|
|
|
|
--remount-ro ${glibc}/etc \
|
2024-09-13 16:53:16 +00:00
|
|
|
--symlink ${realInit runScript} /init \
|
2024-04-06 20:11:48 +00:00
|
|
|
'' + optionalString fhsenv.isMultiBuild (indentLines ''
|
2021-02-04 01:16:20 +00:00
|
|
|
--tmpfs ${pkgsi686Linux.glibc}/etc \
|
|
|
|
--symlink /etc/ld.so.conf ${pkgsi686Linux.glibc}/etc/ld.so.conf \
|
|
|
|
--symlink /etc/ld.so.cache ${pkgsi686Linux.glibc}/etc/ld.so.cache \
|
|
|
|
--ro-bind ${pkgsi686Linux.glibc}/etc/rpc ${pkgsi686Linux.glibc}/etc/rpc \
|
|
|
|
--remount-ro ${pkgsi686Linux.glibc}/etc \
|
2023-04-22 12:31:27 +00:00
|
|
|
'') + ''
|
2020-09-20 15:02:08 +00:00
|
|
|
"''${ro_mounts[@]}"
|
2020-10-24 15:23:37 +00:00
|
|
|
"''${symlinks[@]}"
|
2020-09-20 15:02:08 +00:00
|
|
|
"''${auto_mounts[@]}"
|
2023-02-11 14:30:18 +00:00
|
|
|
"''${x11_args[@]}"
|
2021-12-29 20:30:44 +00:00
|
|
|
${concatStringsSep "\n " extraBwrapArgs}
|
2024-09-13 16:53:16 +00:00
|
|
|
${containerInit} ${initArgs}
|
2020-09-20 15:02:08 +00:00
|
|
|
)
|
|
|
|
exec "''${cmd[@]}"
|
2020-07-31 12:58:03 +00:00
|
|
|
'';
|
|
|
|
|
2023-04-09 17:50:02 +00:00
|
|
|
bin = writeShellScript "${name}-bwrap" (bwrapCmd { initArgs = ''"$@"''; });
|
2023-11-30 00:28:38 +00:00
|
|
|
in runCommandLocal name (nameAttrs // {
|
2024-09-25 19:43:00 +00:00
|
|
|
inherit nativeBuildInputs meta;
|
2020-07-31 12:58:03 +00:00
|
|
|
|
|
|
|
passthru = passthru // {
|
|
|
|
env = runCommandLocal "${name}-shell-env" {
|
2020-08-17 07:49:34 +00:00
|
|
|
shellHook = bwrapCmd {};
|
2020-07-31 12:58:03 +00:00
|
|
|
} ''
|
|
|
|
echo >&2 ""
|
|
|
|
echo >&2 "*** User chroot 'env' attributes are intended for interactive nix-shell sessions, not for building! ***"
|
|
|
|
echo >&2 ""
|
|
|
|
exit 1
|
|
|
|
'';
|
2023-03-07 19:35:30 +00:00
|
|
|
inherit args fhsenv;
|
2020-07-31 12:58:03 +00:00
|
|
|
};
|
2023-11-30 00:28:38 +00:00
|
|
|
}) ''
|
2020-07-31 12:58:03 +00:00
|
|
|
mkdir -p $out/bin
|
2023-11-30 00:28:38 +00:00
|
|
|
ln -s ${bin} $out/bin/${executableName}
|
2023-04-09 17:50:02 +00:00
|
|
|
|
2020-07-31 12:58:03 +00:00
|
|
|
${extraInstallCommands}
|
|
|
|
''
|