Class: Landlock::SafeExec
- Inherits:
-
Object
- Object
- Landlock::SafeExec
- 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
- .capture(*command, **options) ⇒ Object
- .capture!(*command, **options) ⇒ Object
- .default_execute_paths ⇒ Object
- .default_read_paths ⇒ Object
- .existing_paths(paths) ⇒ Object
- .helper_path ⇒ Object
- .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
- .sandboxing? ⇒ Boolean
- .supported? ⇒ Boolean
Class Method Details
.capture(*command, **options) ⇒ Object
66 67 68 |
# File 'lib/landlock/safe_exec.rb', line 66 def capture(*command, **) perform_capture(*command, raise_on_failure: false, **) end |
.capture!(*command, **options) ⇒ Object
70 71 72 |
# File 'lib/landlock/safe_exec.rb', line 70 def capture!(*command, **) perform_capture(*command, raise_on_failure: true, **) end |
.default_execute_paths ⇒ Object
178 179 180 |
# File 'lib/landlock/safe_exec.rb', line 178 def default_execute_paths existing_paths(DEFAULT_EXECUTE_PATHS) end |
.default_read_paths ⇒ Object
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_path ⇒ Object
165 166 167 168 169 170 171 172 |
# File 'lib/landlock/safe_exec.rb', line 165 def helper_path candidates = [ File.("landlock-safe-exec", __dir__), File.("../../tmp/#{RbConfig::CONFIG.fetch("arch")}/landlock/#{RUBY_VERSION}/landlock-safe-exec", __dir__), File.("../../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) = ( 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() if !use_helper && .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)) = [command.join(" "), , stderr].filter { |part| part.to_s != "" }.join("\n") raise CommandError.new(, stdout: stdout, stderr: stderr, status: status, result: result) end result rescue OutputTooLargeError => e = [command.join(" "), , e.].filter { |part| part.to_s != "" }.join("\n") raise CommandError.new() end |
.sandboxing? ⇒ Boolean
161 162 163 |
# File 'lib/landlock/safe_exec.rb', line 161 def sandboxing? supported? end |
.supported? ⇒ Boolean
157 158 159 |
# File 'lib/landlock/safe_exec.rb', line 157 def supported? helper_available? && Landlock.supported? end |