Skip to content

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.lock
index 389f591dde7..44f5c815197 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -309,7 +309,7 @@ GEM
simpleidn (~> 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 @@ CHECKSUMS
json_completer (1.2.0) sha256=4665749172634eccb208c562eb59ea81dd7a612a1fa9a36df441ac473ff177b1
json_schemer (2.5.0) sha256=2f01fb4cce721a4e08dd068fc2030cffd0702a7f333f1ea2be6e8991f00ae396
jwt (3.2.0) sha256=5419b1fe37b1da0982bd07051f573a8b8789ab724c2aa7e785e4784a3ed217d7
- landlock (0.5.1) sha256=4c66c5ea238ce0fb4dbf0ea8697da3af01f396f86b34f1f5c323969bc524eba0
+ landlock (0.6.0) sha256=fb8ae70640636a170770dcd28c77a5a0e29d4edce29f9f421b6d641f02f6f3b6
language_server-protocol (3.17.0.6) sha256=5ef2c0c138f8267e1bc631d3328347d354f96724b0af22f2c79516120443b7f0
libv8-node (24.12.0.1-aarch64-linux) sha256=22bd0246cde85d70c88cf772727ac3677a20ac1d169105f82efe5f1f7c39f755
libv8-node (24.12.0.1-aarch64-linux-musl) sha256=791b5960f79525e87d1bd1e5f2bf3715ef2a38bac6c97f297853e98a8411ba89
diff --git a/lib/discourse/safe_exec.rb b/lib/discourse/safe_exec.rb
index dc32954245c..3e5e6a46c73 100644
--- a/lib/discourse/safe_exec.rb
+++ b/lib/discourse/safe_exec.rb
@@ -22,6 +22,7 @@ module Discourse
bind_tcp: nil,
rlimits: {},
seccomp_deny_network: false,
+ seccomp_deny_child_processes: false,
max_output_bytes: nil,
truncate_output: false
)
@@ -42,6 +43,7 @@ module Discourse
bind_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.rb
index 8ff5e9625cd..a92fec69e0f 100644
--- a/lib/file_helper.rb
+++ b/lib/file_helper.rb
@@ -191,6 +191,7 @@ class FileHelper
timeout: 15,
rlimits: ImageMagick::RLIMITS,
seccomp_deny_network: true,
+ seccomp_deny_child_processes: true,
)
rescue Discourse::Utils::CommandError, Errno::ENOENT
next
diff --git a/lib/image_magick.rb b/lib/image_magick.rb
index 1a4c33a9cee..c1ac39d850a 100644
--- a/lib/image_magick.rb
+++ b/lib/image_magick.rb
@@ -70,6 +70,7 @@ module ImageMagick
rlimits: RLIMITS,
failure_message:,
seccomp_deny_network: true,
+ seccomp_deny_child_processes: true,
)
end
end
diff --git a/script/discourse_vips_worker b/script/discourse_vips_worker
index 631981b9c63..e06958ee505 100644
--- a/script/discourse_vips_worker
+++ b/script/discourse_vips_worker
@@ -527,6 +527,7 @@ class DiscourseVipsWorker
unsetenv_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.rb
index 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 do
cpu_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 do
cpu_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" do
skip "Landlock is not supported" if !described_class.landlock_supported?