···5354MS_BIND = 0x1000
55MS_REC = 0x4000
056$mount = make_fcall 'mount', [Fiddle::TYPE_VOIDP,
57 Fiddle::TYPE_VOIDP,
58 Fiddle::TYPE_VOIDP,
···92# we don't use threads at all.
93$cpid = $fork.call
94if $cpid == 0
95- # Save user UID and GID
96- uid = Process.uid
97- gid = Process.gid
00000009899- # Create new mount and user namespaces
100- # CLONE_NEWUSER requires a program to be non-threaded, hence
101- # native fork above.
102- $unshare.call CLONE_NEWNS | CLONE_NEWUSER
103104- # Map users and groups to the parent namespace
105- begin
106- # setgroups is only available since Linux 3.19
107- write_file '/proc/self/setgroups', 'deny'
108- rescue
000109 end
110- write_file '/proc/self/uid_map', "#{uid} #{uid} 1"
111- write_file '/proc/self/gid_map', "#{gid} #{gid} 1"
112113 # Do rbind mounts.
114 mounts.each do |from, rto|
···117 $mount.call from, to, nil, MS_BIND | MS_REC, nil
118 end
11900120 # Chroot!
121 Dir.chroot root
122 Dir.chdir '/'
···5354MS_BIND = 0x1000
55MS_REC = 0x4000
56+MS_SLAVE = 0x80000
57$mount = make_fcall 'mount', [Fiddle::TYPE_VOIDP,
58 Fiddle::TYPE_VOIDP,
59 Fiddle::TYPE_VOIDP,
···93# we don't use threads at all.
94$cpid = $fork.call
95if $cpid == 0
96+ # If we are root, no need to create new user namespace.
97+ if Process.uid == 0
98+ $unshare.call CLONE_NEWNS
99+ # Mark all mounted filesystems as slave so changes
100+ # don't propagate to the parent mount namespace.
101+ $mount.call nil, '/', nil, MS_REC | MS_SLAVE, nil
102+ else
103+ # Save user UID and GID
104+ uid = Process.uid
105+ gid = Process.gid
106107+ # Create new mount and user namespaces
108+ # CLONE_NEWUSER requires a program to be non-threaded, hence
109+ # native fork above.
110+ $unshare.call CLONE_NEWNS | CLONE_NEWUSER
111112+ # Map users and groups to the parent namespace
113+ begin
114+ # setgroups is only available since Linux 3.19
115+ write_file '/proc/self/setgroups', 'deny'
116+ rescue
117+ end
118+ write_file '/proc/self/uid_map', "#{uid} #{uid} 1"
119+ write_file '/proc/self/gid_map', "#{gid} #{gid} 1"
120 end
00121122 # Do rbind mounts.
123 mounts.each do |from, rto|
···126 $mount.call from, to, nil, MS_BIND | MS_REC, nil
127 end
128129+ # Don't make root private so privilege drops inside chroot are possible
130+ File.chmod(0755, root)
131 # Chroot!
132 Dir.chroot root
133 Dir.chdir '/'