A platform for community discussion. Free, open, simple.
DEV: Security harden image processing by denying child processes (#44007)
Alan Guo Xiang Tan1470aec65a9c
diff --git a/Gemfile.lock b/Gemfile.lockindex 389f591dde7..44f5c815197 100644--- a/Gemfile.lock+++ b/Gemfile.lock@@ -309,7 +309,7 @@ GEMsimpleidn (~> 0.2)jwt (3.2.0)base64- landlock (0.5.1)+ landlock (0.6.0)language_server-protocol (3.17.0.6)libv8-node (24.12.0.1-aarch64-linux)libv8-node (24.12.0.1-aarch64-linux-musl)@@ -1102,7 +1102,7 @@ CHECKSUMSjson_completer (1.2.0) sha256=4665749172634eccb208c562eb59ea81dd7a612a1fa9a36df441ac473ff177b1json_schemer (2.5.0) sha256=2f01fb4cce721a4e08dd068fc2030cffd0702a7f333f1ea2be6e8991f00ae396jwt (3.2.0) sha256=5419b1fe37b1da0982bd07051f573a8b8789ab724c2aa7e785e4784a3ed217d7- landlock (0.5.1) sha256=4c66c5ea238ce0fb4dbf0ea8697da3af01f396f86b34f1f5c323969bc524eba0+ landlock (0.6.0) sha256=fb8ae70640636a170770dcd28c77a5a0e29d4edce29f9f421b6d641f02f6f3b6language_server-protocol (3.17.0.6) sha256=5ef2c0c138f8267e1bc631d3328347d354f96724b0af22f2c79516120443b7f0libv8-node (24.12.0.1-aarch64-linux) sha256=22bd0246cde85d70c88cf772727ac3677a20ac1d169105f82efe5f1f7c39f755libv8-node (24.12.0.1-aarch64-linux-musl) sha256=791b5960f79525e87d1bd1e5f2bf3715ef2a38bac6c97f297853e98a8411ba89diff --git a/lib/discourse/safe_exec.rb b/lib/discourse/safe_exec.rbindex dc32954245c..3e5e6a46c73 100644--- a/lib/discourse/safe_exec.rb+++ b/lib/discourse/safe_exec.rb@@ -22,6 +22,7 @@ module Discoursebind_tcp: nil,rlimits: {},seccomp_deny_network: false,+ seccomp_deny_child_processes: false,max_output_bytes: nil,truncate_output: false)@@ -42,6 +43,7 @@ module Discoursebind_tcp: bind_tcp,rlimits: rlimits,seccomp_deny_network: seccomp_deny_network,+ seccomp_deny_child_processes: seccomp_deny_child_processes,max_output_bytes: max_output_bytes,truncate_output: truncate_output,)diff --git a/lib/file_helper.rb b/lib/file_helper.rbindex 8ff5e9625cd..a92fec69e0f 100644--- a/lib/file_helper.rb+++ b/lib/file_helper.rb@@ -191,6 +191,7 @@ class FileHelpertimeout: 15,rlimits: ImageMagick::RLIMITS,seccomp_deny_network: true,+ seccomp_deny_child_processes: true,)rescue Discourse::Utils::CommandError, Errno::ENOENTnextdiff --git a/lib/image_magick.rb b/lib/image_magick.rbindex 1a4c33a9cee..c1ac39d850a 100644--- a/lib/image_magick.rb+++ b/lib/image_magick.rb@@ -70,6 +70,7 @@ module ImageMagickrlimits: RLIMITS,failure_message:,seccomp_deny_network: true,+ seccomp_deny_child_processes: true,)endenddiff --git a/script/discourse_vips_worker b/script/discourse_vips_workerindex 631981b9c63..e06958ee505 100644--- a/script/discourse_vips_worker+++ b/script/discourse_vips_worker@@ -527,6 +527,7 @@ class DiscourseVipsWorkerunsetenv_others: true,rlimits: operation.sandbox_rlimits(defaults: RLIMITS),seccomp_deny_network: LINUX_PLATFORM,+ seccomp_deny_child_processes: LINUX_PLATFORM,on_unsupported: :run_without_landlock,) do |stdout, _stderr|apply_nice(request)diff --git a/spec/lib/discourse/safe_exec_spec.rb b/spec/lib/discourse/safe_exec_spec.rbindex 3521e23c502..fbfb49329e8 100644--- a/spec/lib/discourse/safe_exec_spec.rb+++ b/spec/lib/discourse/safe_exec_spec.rb@@ -33,6 +33,7 @@ RSpec.describe Discourse::SafeExec docpu_seconds: 1,},seccomp_deny_network: true,+ seccomp_deny_child_processes: true,max_output_bytes: 1024,truncate_output: true,),@@ -57,6 +58,7 @@ RSpec.describe Discourse::SafeExec docpu_seconds: 1,},seccomp_deny_network: true,+ seccomp_deny_child_processes: true,max_output_bytes: 1024,truncate_output: true,)@@ -234,6 +236,35 @@ RSpec.describe Discourse::SafeExec do}.to raise_error(Discourse::Utils::CommandError)end+ it "denies child processes when requested" do+ skip "Landlock is not supported" if !described_class.landlock_supported?++ command = [RbConfig.ruby, "--disable-gems", "-e", "Process.wait(Process.spawn('/bin/true'))"]+ paths = described_class.default_read_paths+ execute_paths = described_class.default_execute_paths+ env = { "PATH" => ENV["PATH"].to_s }++ expect(+ described_class.capture(+ *command,+ read: paths,+ execute: execute_paths,+ env: env,+ unsetenv_others: true,+ ),+ ).to eq("")+ expect {+ described_class.capture(+ *command,+ read: paths,+ execute: execute_paths,+ env: env,+ unsetenv_others: true,+ seccomp_deny_child_processes: true,+ )+ }.to raise_error(Discourse::Utils::CommandError)+ end+it "terminates commands that exceed the output limit" doskip "Landlock is not supported" if !described_class.landlock_supported?