Class: Landlock::SafeExec

Inherits:
Object
  • Object
show all
Defined in:
lib/landlock/safe_exec.rb

Defined Under Namespace

Classes: CommandError, Result

Constant Summary collapse

Error =
Class.new(StandardError)
OutputTooLargeError =
Class.new(Error)
DEFAULT_READ_PATHS =
%w[/bin /etc /lib /lib64 /usr].freeze
DEFAULT_EXECUTE_PATHS =
%w[/bin /lib /lib64 /usr].freeze
READ_CHUNK_BYTES =
16 * 1024

Class Method Summary collapse

Class Method Details

.capture(*command, **options) ⇒ Object



66
67
68
# File 'lib/landlock/safe_exec.rb', line 66

def capture(*command, **options)
  perform_capture(*command, raise_on_failure: false, **options)
end

.capture!(*command, **options) ⇒ Object



70
71
72
# File 'lib/landlock/safe_exec.rb', line 70

def capture!(*command, **options)
  perform_capture(*command, raise_on_failure: true, **options)
end

.default_execute_pathsObject



178
179
180
# File 'lib/landlock/safe_exec.rb', line 178

def default_execute_paths
  existing_paths(DEFAULT_EXECUTE_PATHS)
end

.default_read_pathsObject



174
175
176
# File 'lib/landlock/safe_exec.rb', line 174

def default_read_paths
  existing_paths(DEFAULT_READ_PATHS)
end

.existing_paths(paths) ⇒ Object



182
183
184
# File 'lib/landlock/safe_exec.rb', line 182

def existing_paths(paths)
  Array(paths).filter { |path| path.to_s != "" && File.exist?(path) }.uniq
end

.helper_pathObject



165
166
167
168
169
170
171
172
# File 'lib/landlock/safe_exec.rb', line 165

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

.perform_capture(*command, read: [], write: [], execute: [], timeout: nil, failure_message: "", success_status_codes: [0], env: {}, inherit_env: false, chdir: nil, stdin: nil, connect_tcp: nil, bind_tcp: [], rlimits: {}, seccomp_deny_network: false, max_output_bytes: nil, truncate_output: false, allow_all_known: true, raise_on_failure:) ⇒ Object



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
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/landlock/safe_exec.rb', line 74

def perform_capture(
  *command,
  read: [],
  write: [],
  execute: [],
  timeout: nil,
  failure_message: "",
  success_status_codes: [0],
  env: {},
  inherit_env: false,
  chdir: nil,
  stdin: nil,
  connect_tcp: nil,
  bind_tcp: [],
  rlimits: {},
  seccomp_deny_network: false,
  max_output_bytes: nil,
  truncate_output: false,
  allow_all_known: true,
  raise_on_failure:
)
  validate_sandbox_option_values!(connect_tcp: connect_tcp, bind_tcp: bind_tcp)

  unsupported_options = unsupported_sandbox_options(
    read: read,
    write: write,
    execute: execute,
    connect_tcp: connect_tcp,
    bind_tcp: bind_tcp,
    seccomp_deny_network: seccomp_deny_network
  )
  use_helper = helper_available?
  warn_unsupported_platform_once(unsupported_options) if !use_helper && unsupported_options.any?

  stdout, stderr, status, output_truncated, timed_out = if use_helper
    max_output_bytes = validate_output_limit!(max_output_bytes)
    capture_process(
      command,
      read: read,
      write: write,
      execute: execute,
      timeout: timeout,
      env: env,
      inherit_env: inherit_env,
      chdir: chdir,
      stdin: stdin,
      connect_tcp: connect_tcp,
      bind_tcp: bind_tcp,
      rlimits: rlimits,
      seccomp_deny_network: seccomp_deny_network,
      max_output_bytes: max_output_bytes,
      truncate_output: truncate_output,
      allow_all_known: allow_all_known
    )
  else
    max_output_bytes = validate_output_limit!(max_output_bytes)
    capture_process_without_helper(
      command,
      timeout: timeout,
      env: env,
      inherit_env: inherit_env,
      chdir: chdir,
      stdin: stdin,
      rlimits: rlimits,
      max_output_bytes: max_output_bytes,
      truncate_output: truncate_output
    )
  end

  result = Result.new(stdout: stdout, stderr: stderr, status: status, output_truncated: output_truncated, timed_out: timed_out)

  if raise_on_failure && (!status.exited? || !success_status_codes.include?(status.exitstatus))
    message = [command.join(" "), failure_message, stderr].filter { |part| part.to_s != "" }.join("\n")
    raise CommandError.new(message, stdout: stdout, stderr: stderr, status: status, result: result)
  end

  result
rescue OutputTooLargeError => e
  message = [command.join(" "), failure_message, e.message].filter { |part| part.to_s != "" }.join("\n")
  raise CommandError.new(message)
end

.sandboxing?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/landlock/safe_exec.rb', line 161

def sandboxing?
  supported?
end

.supported?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/landlock/safe_exec.rb', line 157

def supported?
  helper_available? && Landlock.supported?
end