Module: Landlock::Runner::Fork

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

Class Method Summary collapse

Class Method Details

.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



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
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
142
# File 'lib/landlock/runner/fork.rb', line 54

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 =
    fork do
      begin
        stdout_reader.close
        stderr_reader.close
        stdin_writer.close
        ::Process.setpgrp
        STDIN.reopen(stdin_reader)
        STDOUT.reopen(stdout_writer)
        STDERR.reopen(stderr_writer)
        stdin_reader.close
        stdout_writer.close
        stderr_writer.close

        setup_child!(
          argv,
          read:,
          write:,
          execute:,
          connect_tcp:,
          bind_tcp:,
          paths:,
          scope:,
          chdir:,
          env:,
          unsetenv_others:,
          close_others:,
          allow_all_known:,
          rlimits:,
          seccomp_deny_network:
        )
      rescue Exception => error
        Runner.exit_child!(error)
      end
    end

  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

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



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

def setup_child!(
  argv,
  read:,
  write:,
  execute:,
  connect_tcp:,
  bind_tcp:,
  paths:,
  scope:,
  chdir:,
  env:,
  unsetenv_others:,
  close_others:,
  allow_all_known:,
  rlimits:,
  seccomp_deny_network:
)
  Dir.chdir(chdir) if chdir # rubocop:disable Discourse/NoChdir
  if Policy.requested?(read:, write:, execute:, connect_tcp:, bind_tcp:, paths:, scope:, allow_all_known:)
    Landlock.restrict!(read:, write:, execute:, connect_tcp:, bind_tcp:, paths:, scope:, allow_all_known:)
  end
  Landlock::Native.seccomp_deny_network! if seccomp_deny_network
  Rlimits.apply!(rlimits)
  Kernel.exec(*Runner.kernel_exec_args(argv, env, unsetenv_others:, close_others:))
end

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



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

def spawn(
  argv,
  read:,
  write:,
  execute:,
  connect_tcp:,
  bind_tcp:,
  paths:,
  scope:,
  chdir:,
  env:,
  unsetenv_others:,
  close_others:,
  allow_all_known:
)
  fork do
    begin
      setup_child!(
        argv,
        read:,
        write:,
        execute:,
        connect_tcp:,
        bind_tcp:,
        paths:,
        scope:,
        chdir:,
        env:,
        unsetenv_others:,
        close_others:,
        allow_all_known:,
        rlimits: [],
        seccomp_deny_network: false
      )
    rescue Exception => error
      Runner.exit_child!(error)
    end
  end
end