Truly independent web browser
LibCore+LibWebView: Keep Ctrl+C from reaching helper processes
Shannon Boothf0bbd2a170da
diff --git a/Libraries/LibCore/Process.cpp b/Libraries/LibCore/Process.cppindex 62712745ac6..e548dbdb5a7 100644--- a/Libraries/LibCore/Process.cpp+++ b/Libraries/LibCore/Process.cpp@@ -271,6 +271,7 @@ struct SpawnedChildContext {ReadonlySpan<int> descriptors_to_keep;pid_t parent_pid { 0 };sigset_t parent_signal_mask;+ sigset_t child_signal_mask;// Written by the child, which shares our memory until it calls exec().int child_errno { 0 };@@ -311,12 +312,15 @@ NO_SANITIZE_ADDRESS static int spawned_child_main(void* argument)_exit(127);}+ if (context.options.new_process_group && setpgid(0, 0) < 0)+ report_errno_and_exit(errno);+if (auto error = run_file_actions_in_child(context.options.file_actions); error.has_value())report_errno_and_exit(error.value());if (auto error = close_undeclared_descriptors(context.descriptors_to_keep); error.has_value())report_errno_and_exit(error.value());- if (syscall(SYS_rt_sigprocmask, SIG_SETMASK, &context.parent_signal_mask, nullptr, kernel_signal_set_size) < 0)+ if (syscall(SYS_rt_sigprocmask, SIG_SETMASK, &context.child_signal_mask, nullptr, kernel_signal_set_size) < 0)report_errno_and_exit(errno);if (context.options.search_for_executable_in_path)@@ -342,6 +346,7 @@ static ErrorOr<pid_t> fork_and_exec(ProcessSpawnOptions const& options, Span<cha.descriptors_to_keep = descriptors_to_keep,.parent_pid = getpid(),.parent_signal_mask = {},+ .child_signal_mask = {},};// execvpe() builds another argv array on the stack when falling back to /bin/sh after ENOEXEC.@@ -369,6 +374,10 @@ static ErrorOr<pid_t> fork_and_exec(ProcessSpawnOptions const& options, Span<chaif (auto result = pthread_sigmask(SIG_SETMASK, &all_signals, &context.parent_signal_mask); result != 0)return Error::from_syscall("pthread_sigmask"sv, result);+ context.child_signal_mask = context.parent_signal_mask;+ if (options.new_process_group)+ sigaddset(&context.child_signal_mask, SIGTTOU);+// CLONE_VFORK suspends this thread until the child calls exec() or exits.auto* child_stack_top = static_cast<u8*>(child_stack) + mapping_size;auto pid = clone(spawned_child_main, child_stack_top, CLONE_VM | CLONE_VFORK | SIGCHLD, &context);@@ -410,18 +419,27 @@ ErrorOr<Process> Process::spawn(ProcessSpawnOptions const& options)posix_spawn_file_actions_destroy(&spawn_actions);};- posix_spawnattr_t* spawn_attributes_pointer = nullptr;-#if defined(AK_OS_MACOS)- // A descriptor that is not close-on-exec at the moment of the spawn would otherwise reach the child, including one- // that another thread has just received or duplicated. Only pass the standard streams and the descriptors that the- // file actions name.posix_spawnattr_t spawn_attributes;CHECK(posix_spawnattr_init(&spawn_attributes));ScopeGuard cleanup_spawn_attributes = [&] {posix_spawnattr_destroy(&spawn_attributes);};- CHECK(posix_spawnattr_setflags(&spawn_attributes, POSIX_SPAWN_CLOEXEC_DEFAULT));- spawn_attributes_pointer = &spawn_attributes;+ short spawn_flags = 0;++ // The process group defaults to 0, which makes a new one, named after the child.+ if (options.new_process_group) {+ spawn_flags |= POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSIGMASK;+ sigset_t signal_mask;+ CHECK(pthread_sigmask(SIG_SETMASK, nullptr, &signal_mask));+ sigaddset(&signal_mask, SIGTTOU);+ CHECK(posix_spawnattr_setsigmask(&spawn_attributes, &signal_mask));+ }++#if defined(AK_OS_MACOS)+ // A descriptor that is not close-on-exec at the moment of the spawn would otherwise reach the child, including one+ // that another thread has just received or duplicated. Only pass the standard streams and the descriptors that the+ // file actions name.+ spawn_flags |= POSIX_SPAWN_CLOEXEC_DEFAULT;// The inherit actions must come before the file actions, which may replace the same descriptors.for (int fd : { STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO }) {@@ -431,6 +449,8 @@ ErrorOr<Process> Process::spawn(ProcessSpawnOptions const& options)}#endif+ CHECK(posix_spawnattr_setflags(&spawn_attributes, spawn_flags));+for (auto const& file_action : options.file_actions) {TRY(file_action.visit([&](FileAction::OpenFile const& action) -> ErrorOr<void> {@@ -465,9 +485,9 @@ ErrorOr<Process> Process::spawn(ProcessSpawnOptions const& options)auto environment = environment_for_child(options);pid_t pid;if (options.search_for_executable_in_path) {- pid = TRY(System::posix_spawnp(options.executable.view(), &spawn_actions, spawn_attributes_pointer, const_cast<char**>(argv_list.get().data()), environment_pointer(environment)));+ pid = TRY(System::posix_spawnp(options.executable.view(), &spawn_actions, &spawn_attributes, const_cast<char**>(argv_list.get().data()), environment_pointer(environment)));} else {- pid = TRY(System::posix_spawn(options.executable.view(), &spawn_actions, spawn_attributes_pointer, const_cast<char**>(argv_list.get().data()), environment_pointer(environment)));+ pid = TRY(System::posix_spawn(options.executable.view(), &spawn_actions, &spawn_attributes, const_cast<char**>(argv_list.get().data()), environment_pointer(environment)));}return Process { pid };}diff --git a/Libraries/LibCore/Process.h b/Libraries/LibCore/Process.hindex 2c1c230b5f2..08790cbff0c 100644--- a/Libraries/LibCore/Process.h+++ b/Libraries/LibCore/Process.h@@ -45,6 +45,10 @@ struct ProcessSpawnOptions {bool search_for_executable_in_path { false };// On supported platforms, ask the kernel to terminate this process when its parent dies.bool die_with_parent { false };+ // Start this process in a process group of its own, so that the signals a terminal sends to its foreground job,+ // such as SIGINT for Ctrl+C, do not reach it. SIGTTOU starts out blocked, so that writing to a terminal with tostop+ // set does not stop it. On Windows, the new process group only keeps Ctrl+C from it.+ bool new_process_group { false };ReadonlySpan<ByteString> arguments {};// The child's whole environment, as NAME=value strings. Without it, the child inherits the environment of this// process.diff --git a/Libraries/LibCore/ProcessWindows.cpp b/Libraries/LibCore/ProcessWindows.cppindex 95c9aac128f..78a87845208 100644--- a/Libraries/LibCore/ProcessWindows.cpp+++ b/Libraries/LibCore/ProcessWindows.cpp@@ -159,7 +159,7 @@ ErrorOr<Process> Process::spawn(ProcessSpawnOptions const& options)NULL, // process security attributesNULL, // primary thread security attributes!inherited_handles.is_empty(),- EXTENDED_STARTUPINFO_PRESENT,+ EXTENDED_STARTUPINFO_PRESENT | (options.new_process_group ? CREATE_NEW_PROCESS_GROUP : 0),NULL, // use parent's environmentNULL, // working directory&startup_info.StartupInfo,diff --git a/Libraries/LibWebView/Process.cpp b/Libraries/LibWebView/Process.cppindex f1cc104856c..53389fb08b3 100644--- a/Libraries/LibWebView/Process.cpp+++ b/Libraries/LibWebView/Process.cpp@@ -208,6 +208,9 @@ ErrorOr<Process::ProcessAndIPCTransport> Process::spawn_and_connect_to_process([auto spawn_options = options;spawn_options.die_with_parent = true;+ // Ctrl+C in the terminal that the Browser was started from must reach only the Browser, which ends its helpers once+ // it has quit. A helper that died of it first would look like a crash to a Browser that is still running.+ spawn_options.new_process_group = true;Vector<ByteString> arguments_with_stats;if (auto* stats_log = Core::StatsLog::the()) {arguments_with_stats = Vector<ByteString> { options.arguments };diff --git a/Libraries/LibWebView/ProcessReaper.cpp b/Libraries/LibWebView/ProcessReaper.cppindex 338d1c5b594..cc554aa5d83 100644--- a/Libraries/LibWebView/ProcessReaper.cpp+++ b/Libraries/LibWebView/ProcessReaper.cpp@@ -41,6 +41,8 @@ ErrorOr<NonnullOwnPtr<ProcessReaper>> ProcessReaper::start(ByteString const& exeauto process = TRY(Core::Process::spawn({.name = "ProcessReaper"sv,.executable = executable_path,+ // The reaper has to outlive the Browser, so a Ctrl+C meant for the Browser must not end it first.+ .new_process_group = true,.arguments = arguments,.environment = Vector<ByteString> {},.file_actions = {diff --git a/Tests/LibCore/TestLibCoreProcess.cpp b/Tests/LibCore/TestLibCoreProcess.cppindex 432a97cb6a3..936877e36ed 100644--- a/Tests/LibCore/TestLibCoreProcess.cpp+++ b/Tests/LibCore/TestLibCoreProcess.cpp@@ -19,8 +19,9 @@# include <sys/resource.h># include <sys/stat.h># include <sys/syscall.h>-# include <sys/wait.h>#endif+#include <signal.h>+#include <sys/wait.h>#include <unistd.h>static ByteString run_and_read_output(Core::ProcessSpawnOptions options)@@ -246,3 +247,27 @@ TEST_CASE(spawned_process_gets_only_the_given_descriptors_without_close_range)EXPECT_EQ(WEXITSTATUS(status), 0);}#endif++#if defined(AK_OS_MACOS) || defined(AK_OS_LINUX)+TEST_CASE(spawned_process_in_a_new_process_group_is_not_stopped_by_sigttou)+{+ Vector<ByteString> arguments { "-c"sv, "kill -TTOU $$"sv };+ auto process = MUST(Core::Process::spawn({+ .executable = "/bin/sh"sv,+ .new_process_group = true,+ .arguments = arguments,+ }));++ int status = 0;+ EXPECT_EQ(waitpid(process.pid(), &status, WUNTRACED), process.pid());+ if (WIFSTOPPED(status)) {+ (void)kill(process.pid(), SIGKILL);+ (void)waitpid(process.pid(), &status, 0);+ FAIL("The child was stopped by SIGTTOU");+ return;+ }+ EXPECT(WIFEXITED(status));+ if (WIFEXITED(status))+ EXPECT_EQ(WEXITSTATUS(status), 0);+}+#endif