Module: Landlock::Runner::Native

Defined in:
lib/landlock/runner/native.rb

Class Method Summary collapse

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/landlock/runner/native.rb', line 16

def available?
  File.executable?(helper_path)
end

.call(argv, read:, write:, execute:, connect_tcp:, bind_tcp:, paths:, scope:, chdir:, env:, unsetenv_others:, close_others:, allow_all_known:, timeout:, stdin:, rlimits:, seccomp_deny_network:, max_output_bytes:, truncate_output:) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/landlock/runner/native.rb', line 66

def call(
  argv,
  read:,
  write:,
  execute:,
  connect_tcp:,
  bind_tcp:,
  paths:,
  scope:,
  chdir:,
  env:,
  unsetenv_others:,
  close_others:,
  allow_all_known:,
  timeout:,
  stdin:,
  rlimits:,
  seccomp_deny_network:,
  max_output_bytes:,
  truncate_output:
)
  stdout_reader, stdout_writer = IO.pipe
  stderr_reader, stderr_writer = IO.pipe
  stdin_reader, stdin_writer = IO.pipe

  pid =
    spawn_helper(
      argv,
      read:,
      write:,
      execute:,
      connect_tcp:,
      bind_tcp:,
      paths:,
      scope:,
      chdir:,
      env:,
      unsetenv_others:,
      close_others:,
      allow_all_known:,
      rlimits:,
      seccomp_deny_network:,
      stdin_reader:,
      stdout_writer:,
      stderr_writer:,
      pgroup: true
    )

  stdin_reader.close
  stdout_writer.close
  stderr_writer.close

  ProcessIO.complete_pipe_capture(
    pid,
    stdout_reader,
    stderr_reader,
    stdin_writer,
    stdin,
    timeout,
    max_output_bytes,
    truncate_output
  )
rescue OutputTooLargeError
  raise
rescue Exception
  if pid
    ProcessIO.terminate_process(pid)
    ProcessIO.wait_for_pid(pid)
  end
  raise
ensure
  [stdin_reader, stdin_writer, stdout_reader, stdout_writer, stderr_reader, stderr_writer].each do |io|
    io&.close unless io.closed?
  rescue IOError
  end
end

.helper_argv(argv, read:, write:, execute:, connect_tcp:, bind_tcp:, paths:, scope:, allow_all_known:, rlimits:, seccomp_deny_network:, close_others:) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/landlock/runner/native.rb', line 190

def helper_argv(
  argv,
  read:,
  write:,
  execute:,
  connect_tcp:,
  bind_tcp:,
  paths:,
  scope:,
  allow_all_known:,
  rlimits:,
  seccomp_deny_network:,
  close_others:
)
  args = [helper_path]
  Array(read).each { |path| args << "--read" << path.to_s }
  Array(write).each { |path| args << "--write" << path.to_s }
  Array(execute).each { |path| args << "--execute" << path.to_s }
  Array(paths).each do |rule|
    path, rights = Policy.normalize_path_rule(rule)
    args << "--path" << path.to_s << Array(rights).map(&:to_s).join(",")
  end
  Array(connect_tcp).each { |port| args << "--connect-tcp" << port.to_s }
  Array(bind_tcp).each { |port| args << "--bind-tcp" << port.to_s }
  Array(scope).each { |name| args << "--scope" << name.to_s }
  Array(rlimits).each { |key, value| args << "--rlimit" << "#{key}=#{value}" }
  args << "--allow-all-known" if allow_all_known
  args << "--seccomp-deny-network" if seccomp_deny_network
  args << "--keep-fds" unless close_others
  args << "--"
  args.concat(argv.map(&:to_s))
  args
end

.helper_pathObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/landlock/runner/native.rb', line 20

def helper_path
  candidates = [
    File.expand_path("../landlock-safe-exec", __dir__),
    File.expand_path(
      "../../../tmp/#{RbConfig::CONFIG.fetch("arch")}/landlock/#{RUBY_VERSION}/landlock-safe-exec",
      __dir__
    ),
    File.expand_path("../../../ext/landlock/landlock-safe-exec", __dir__)
  ]
  candidates.find { |path| File.executable?(path) } || candidates.first
end

.spawn(argv, read:, write:, execute:, connect_tcp:, bind_tcp:, paths:, scope:, chdir:, env:, unsetenv_others:, close_others:, allow_all_known:) ⇒ Object



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
# File 'lib/landlock/runner/native.rb', line 32

def spawn(
  argv,
  read:,
  write:,
  execute:,
  connect_tcp:,
  bind_tcp:,
  paths:,
  scope:,
  chdir:,
  env:,
  unsetenv_others:,
  close_others:,
  allow_all_known:
)
  spawn_helper(
    argv,
    read:,
    write:,
    execute:,
    connect_tcp:,
    bind_tcp:,
    paths:,
    scope:,
    chdir:,
    env:,
    unsetenv_others:,
    close_others:,
    allow_all_known:,
    rlimits: [],
    seccomp_deny_network: false
  )
end

.spawn_helper(argv, read:, write:, execute:, connect_tcp:, bind_tcp:, paths:, scope:, chdir:, env:, unsetenv_others:, close_others:, allow_all_known:, rlimits:, seccomp_deny_network:, stdin_reader: nil, stdout_writer: nil, stderr_writer: nil, pgroup: false) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/landlock/runner/native.rb', line 143

def spawn_helper(
  argv,
  read:,
  write:,
  execute:,
  connect_tcp:,
  bind_tcp:,
  paths:,
  scope:,
  chdir:,
  env:,
  unsetenv_others:,
  close_others:,
  allow_all_known:,
  rlimits:,
  seccomp_deny_network:,
  stdin_reader: nil,
  stdout_writer: nil,
  stderr_writer: nil,
  pgroup: false
)
  spawn_options = { close_others: }
  spawn_options[:unsetenv_others] = true if unsetenv_others
  spawn_options[:chdir] = chdir if chdir
  spawn_options[:in] = stdin_reader if stdin_reader
  spawn_options[:out] = stdout_writer if stdout_writer
  spawn_options[:err] = stderr_writer if stderr_writer
  spawn_options[:pgroup] = true if pgroup

  spawn_args =
    helper_argv(
      argv,
      read:,
      write:,
      execute:,
      connect_tcp:,
      bind_tcp:,
      paths:,
      scope:,
      allow_all_known:,
      rlimits:,
      seccomp_deny_network:,
      close_others:
    )
  env ? ::Process.spawn(env, *spawn_args, spawn_options) : ::Process.spawn(*spawn_args, spawn_options)
end