-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathnsd
More file actions
executable file
·86 lines (70 loc) · 2.94 KB
/
Copy pathnsd
File metadata and controls
executable file
·86 lines (70 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative '../lib/auto_hck'
module AutoHCK
# rubocop:disable Metrics/BlockLength
run do
# rubocop:enable Metrics/BlockLength
require 'English'
require 'etc'
euid = Process.euid
argv = if euid.zero?
# Create namespaces of the following types:
# - Network
# AutoHCK requires several bridges and an isolated network namespace
# allows setting up them reliably without requiring privileges and
# polluting the root namespace.
# - Mount
# This allows mounting /etc/resolv.conf so that it points to
# slirp4netns.
%w[unshare -m -n]
else
# If the current user is not root, create a user namespace as well and
# map the current user to root. Group mapping is delegated to
# newgidmap.
%w[unshare -m -n --map-user=0]
end
argv << File.join(__dir__, 'ns_unshared')
e_read, e_write = IO.pipe
$stderr.write "[nsd] Running (e pipe): #{e_read.fileno} => #{e_write.fileno}\n"
r_read, r_write = IO.pipe
$stderr.write "[nsd] Running (r pipe): #{r_read.fileno} => #{r_write.fileno}\n"
parent = Process.pid.to_s
$stderr.write "[nsd] Parent PID: #{parent}\n"
fork do
e_write.close
exit false if e_read.read(1).nil?
if euid != 0
pwname = Etc.getpwuid(Process.uid).name
# getsubids is not available on some supported systems.
subid_start, subid_count = File.open('/etc/subgid') do |file|
begin
file.readline while file.readline(':', chomp: true) != pwname
rescue EOFError
abort <<~MESSAGE
subgid delegation is not set up for the current user.
See newgidmap(1) for details.
MESSAGE
end
[file.readline(':', chomp: true), file.readline(chomp: true)]
end
# Call newgidmap so that virtiofsd can use setgroups() to drop subgroups.
# unshare command in util-linux 2.38 can call newgidmap by its own, but
# unfortunately it is not available on some supported systems.
command = ['newgidmap', parent, '0', Process.gid.to_s, '1', subid_start, subid_start, subid_count]
$stderr.write "[nsd child] Running (system): #{command.join(' ')}\n"
system(*command, exception: true, out: :err)
end
e_read.close_on_exec = false
r_write.close_on_exec = false
command = ['slirp4netns', '-e', e_read.fileno.to_s, '-r', r_write.fileno.to_s,
'-a', 'slirp.sock', parent, 'tap_host']
$stderr.write "[nsd child] Running (exec): #{command.join(' ')}\n"
exec(*command, out: :err)
end
e_read.close
r_write.close
$stderr.write "[nsd] Running (exec, 3 => #{e_write.fileno}, 4 => #{r_read.fileno}): #{argv.join(' ')}\n"
exec(*argv, 3 => e_write, 4 => r_read)
end
end