From 230898ceb2e85ae11dbead2430a829babe2ab452 Mon Sep 17 00:00:00 2001 From: Nikolay Amiantov Date: Tue, 15 Dec 2015 16:46:37 +0300 Subject: [PATCH 1/3] chrootenv-user: don't unshare user namespace if we are root --- .../build-fhs-userenv/chroot-user.rb | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/pkgs/build-support/build-fhs-userenv/chroot-user.rb b/pkgs/build-support/build-fhs-userenv/chroot-user.rb index 97316ac4369..250e6a90843 100755 --- a/pkgs/build-support/build-fhs-userenv/chroot-user.rb +++ b/pkgs/build-support/build-fhs-userenv/chroot-user.rb @@ -53,6 +53,7 @@ $unshare = make_fcall 'unshare', [Fiddle::TYPE_INT], Fiddle::TYPE_INT MS_BIND = 0x1000 MS_REC = 0x4000 +MS_SLAVE = 0x80000 $mount = make_fcall 'mount', [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP, @@ -92,23 +93,31 @@ root = Dir.mktmpdir 'chrootenv' # we don't use threads at all. $cpid = $fork.call if $cpid == 0 - # Save user UID and GID - uid = Process.uid - gid = Process.gid + # If we are root, no need to create new user namespace. + if Process.uid == 0 + $unshare.call CLONE_NEWNS + # Mark all mounted filesystems as slave so changes + # don't propagate to the parent mount namespace. + $mount.call nil, '/', nil, MS_REC | MS_SLAVE, nil + else + # Save user UID and GID + uid = Process.uid + gid = Process.gid - # Create new mount and user namespaces - # CLONE_NEWUSER requires a program to be non-threaded, hence - # native fork above. - $unshare.call CLONE_NEWNS | CLONE_NEWUSER + # Create new mount and user namespaces + # CLONE_NEWUSER requires a program to be non-threaded, hence + # native fork above. + $unshare.call CLONE_NEWNS | CLONE_NEWUSER - # Map users and groups to the parent namespace - begin - # setgroups is only available since Linux 3.19 - write_file '/proc/self/setgroups', 'deny' - rescue + # Map users and groups to the parent namespace + begin + # setgroups is only available since Linux 3.19 + write_file '/proc/self/setgroups', 'deny' + rescue + end + write_file '/proc/self/uid_map', "#{uid} #{uid} 1" + write_file '/proc/self/gid_map', "#{gid} #{gid} 1" end - write_file '/proc/self/uid_map', "#{uid} #{uid} 1" - write_file '/proc/self/gid_map', "#{gid} #{gid} 1" # Do rbind mounts. mounts.each do |from, rto| @@ -117,6 +126,8 @@ if $cpid == 0 $mount.call from, to, nil, MS_BIND | MS_REC, nil end + # Don't make root private so privilege drops inside chroot are possible + File.chmod(0755, root) # Chroot! Dir.chroot root Dir.chdir '/' From d6c1150195817427dffa57740d183a3286e2a0f4 Mon Sep 17 00:00:00 2001 From: Nikolay Amiantov Date: Tue, 15 Dec 2015 21:31:12 +0300 Subject: [PATCH 2/3] chrootenv: symlink su and sudo stuff --- pkgs/build-support/build-fhs-chrootenv/env.nix | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkgs/build-support/build-fhs-chrootenv/env.nix b/pkgs/build-support/build-fhs-chrootenv/env.nix index c00d3865afa..610f450cfeb 100644 --- a/pkgs/build-support/build-fhs-chrootenv/env.nix +++ b/pkgs/build-support/build-fhs-chrootenv/env.nix @@ -81,6 +81,11 @@ let ln -s /host-etc/resolv.conf resolv.conf ln -s /host-etc/nsswitch.conf nsswitch.conf + # symlink sudo and su stuff + ln -s /host-etc/login.defs login.defs + ln -s /host-etc/sudoers sudoers + ln -s /host-etc/sudoers.d sudoers.d + # symlink other core stuff ln -s /host-etc/localtime localtime ln -s /host-etc/machine-id machine-id From ed4219964d9974aec463070bb639f7013d1ffedf Mon Sep 17 00:00:00 2001 From: Nikolay Amiantov Date: Tue, 15 Dec 2015 21:32:32 +0300 Subject: [PATCH 3/3] chrootenv: add setuid wrappers to path --- pkgs/build-support/build-fhs-chrootenv/env.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/build-support/build-fhs-chrootenv/env.nix b/pkgs/build-support/build-fhs-chrootenv/env.nix index 610f450cfeb..d28773f00ac 100644 --- a/pkgs/build-support/build-fhs-chrootenv/env.nix +++ b/pkgs/build-support/build-fhs-chrootenv/env.nix @@ -56,7 +56,7 @@ let export PS1='${name}-chrootenv:\u@\h:\w\$ ' export LOCALE_ARCHIVE='/usr/lib/locale/locale-archive' export LD_LIBRARY_PATH='/run/opengl-driver/lib:/run/opengl-driver-32/lib:/usr/lib:/usr/lib32' - export PATH='/usr/bin:/usr/sbin' + export PATH='/var/setuid-wrappers:/usr/bin:/usr/sbin' ${profile} '';