2015-02-05 15:14:28 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
2015-10-06 13:30:17 +01:00
|
|
|
# Bind mounts hierarchy: from => to (relative)
|
2015-02-05 15:14:28 +00:00
|
|
|
# If 'to' is nil, path will be the same
|
2016-06-05 02:44:06 +01:00
|
|
|
mounts = { '/' => 'host',
|
2015-10-06 13:30:17 +01:00
|
|
|
'/proc' => nil,
|
|
|
|
'/sys' => nil,
|
2016-06-05 02:44:06 +01:00
|
|
|
'/nix' => nil,
|
|
|
|
'/tmp' => nil,
|
2015-10-06 13:30:17 +01:00
|
|
|
'/var' => nil,
|
|
|
|
'/run' => nil,
|
2016-06-05 02:44:06 +01:00
|
|
|
'/dev' => nil,
|
|
|
|
'/home' => nil,
|
2015-10-06 13:30:17 +01:00
|
|
|
}
|
2015-02-05 15:14:28 +00:00
|
|
|
|
2015-04-22 13:48:52 +01:00
|
|
|
# Propagate environment variables
|
|
|
|
envvars = [ 'TERM',
|
|
|
|
'DISPLAY',
|
|
|
|
'HOME',
|
|
|
|
'XDG_RUNTIME_DIR',
|
|
|
|
'LANG',
|
|
|
|
'SSL_CERT_FILE',
|
2016-06-28 22:51:19 +01:00
|
|
|
'DBUS_SESSION_BUS_ADDRESS',
|
2015-04-22 13:48:52 +01:00
|
|
|
]
|
|
|
|
|
2015-02-05 15:14:28 +00:00
|
|
|
require 'tmpdir'
|
|
|
|
require 'fileutils'
|
|
|
|
require 'pathname'
|
|
|
|
require 'set'
|
|
|
|
require 'fiddle'
|
|
|
|
|
|
|
|
def write_file(path, str)
|
|
|
|
File.open(path, 'w') { |file| file.write str }
|
|
|
|
end
|
|
|
|
|
|
|
|
# Import C standard library and several needed calls
|
|
|
|
$libc = Fiddle.dlopen nil
|
|
|
|
|
|
|
|
def make_fcall(name, args, output)
|
|
|
|
c = Fiddle::Function.new $libc[name], args, output
|
|
|
|
lambda do |*args|
|
|
|
|
ret = c.call *args
|
|
|
|
raise SystemCallError.new Fiddle.last_error if ret < 0
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
$fork = make_fcall 'fork', [], Fiddle::TYPE_INT
|
|
|
|
|
|
|
|
CLONE_NEWNS = 0x00020000
|
|
|
|
CLONE_NEWUSER = 0x10000000
|
|
|
|
$unshare = make_fcall 'unshare', [Fiddle::TYPE_INT], Fiddle::TYPE_INT
|
|
|
|
|
|
|
|
MS_BIND = 0x1000
|
|
|
|
MS_REC = 0x4000
|
2015-12-15 13:46:37 +00:00
|
|
|
MS_SLAVE = 0x80000
|
2015-02-05 15:14:28 +00:00
|
|
|
$mount = make_fcall 'mount', [Fiddle::TYPE_VOIDP,
|
|
|
|
Fiddle::TYPE_VOIDP,
|
|
|
|
Fiddle::TYPE_VOIDP,
|
|
|
|
Fiddle::TYPE_LONG,
|
|
|
|
Fiddle::TYPE_VOIDP],
|
|
|
|
Fiddle::TYPE_INT
|
|
|
|
|
|
|
|
# Read command line args
|
2016-06-05 02:44:06 +01:00
|
|
|
abort "Usage: chrootenv program args..." unless ARGV.length >= 1
|
|
|
|
execp = ARGV
|
2015-02-05 15:14:28 +00:00
|
|
|
|
2015-10-06 13:30:17 +01:00
|
|
|
# Populate extra mounts
|
|
|
|
if not ENV["CHROOTENV_EXTRA_BINDS"].nil?
|
2016-06-07 12:22:38 +01:00
|
|
|
$stderr.puts "CHROOTENV_EXTRA_BINDS is discussed for deprecation."
|
|
|
|
$stderr.puts "If you have a usecase, please drop a note in issue #16030."
|
|
|
|
$stderr.puts "Notice that we now bind-mount host FS to '/host' and symlink all directories from it to '/' by default."
|
|
|
|
|
2015-10-06 13:30:17 +01:00
|
|
|
for extra in ENV["CHROOTENV_EXTRA_BINDS"].split(':')
|
|
|
|
paths = extra.split('=')
|
|
|
|
if not paths.empty?
|
|
|
|
if paths.size <= 2
|
|
|
|
mounts[paths[0]] = paths[1]
|
|
|
|
else
|
|
|
|
$stderr.puts "Ignoring invalid entry in CHROOTENV_EXTRA_BINDS: #{extra}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-22 13:49:23 +01:00
|
|
|
# Set destination paths for mounts
|
2015-10-06 13:30:17 +01:00
|
|
|
mounts = mounts.map { |k, v| [k, v.nil? ? k.sub(/^\/*/, '') : v] }.to_h
|
2015-04-22 13:49:23 +01:00
|
|
|
|
2015-02-05 15:14:28 +00:00
|
|
|
# Create temporary directory for root and chdir
|
|
|
|
root = Dir.mktmpdir 'chrootenv'
|
|
|
|
|
|
|
|
# Fork process; we need this to do a proper cleanup because
|
|
|
|
# child process will chroot into temporary directory.
|
|
|
|
# We use imported 'fork' instead of native to overcome
|
|
|
|
# CRuby's meddling with threads; this should be safe because
|
|
|
|
# we don't use threads at all.
|
|
|
|
$cpid = $fork.call
|
|
|
|
if $cpid == 0
|
2015-12-15 13:46:37 +00:00
|
|
|
# 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
|
|
|
|
|
|
|
|
# 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"
|
2015-03-10 17:11:25 +00:00
|
|
|
end
|
2015-02-05 15:14:28 +00:00
|
|
|
|
|
|
|
# Do rbind mounts.
|
2015-10-06 13:30:17 +01:00
|
|
|
mounts.each do |from, rto|
|
|
|
|
to = "#{root}/#{rto}"
|
2015-02-05 15:14:28 +00:00
|
|
|
FileUtils.mkdir_p to
|
2015-10-06 13:30:17 +01:00
|
|
|
$mount.call from, to, nil, MS_BIND | MS_REC, nil
|
2015-02-05 15:14:28 +00:00
|
|
|
end
|
|
|
|
|
2015-12-15 13:46:37 +00:00
|
|
|
# Don't make root private so privilege drops inside chroot are possible
|
|
|
|
File.chmod(0755, root)
|
2015-02-05 15:14:28 +00:00
|
|
|
# Chroot!
|
|
|
|
Dir.chroot root
|
|
|
|
Dir.chdir '/'
|
|
|
|
|
|
|
|
# New environment
|
2015-12-10 16:01:04 +00:00
|
|
|
new_env = Hash[ envvars.map { |x| [x, ENV[x]] } ]
|
2015-02-05 15:14:28 +00:00
|
|
|
|
|
|
|
# Finally, exec!
|
2015-12-10 16:01:04 +00:00
|
|
|
exec(new_env, *execp, close_others: true, unsetenv_others: true)
|
2015-02-05 15:14:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Wait for a child. If we catch a signal, resend it to child and continue
|
|
|
|
# waiting.
|
|
|
|
def wait_child
|
|
|
|
begin
|
|
|
|
Process.wait
|
|
|
|
|
|
|
|
# Return child's exit code
|
|
|
|
if $?.exited?
|
|
|
|
exit $?.exitstatus
|
|
|
|
else
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
rescue SignalException => e
|
|
|
|
Process.kill e.signo, $cpid
|
|
|
|
wait_child
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
wait_child
|
|
|
|
ensure
|
|
|
|
# Cleanup
|
|
|
|
FileUtils.rm_rf root, secure: true
|
|
|
|
end
|