From add4dd35899cfd7b7fd298cdf6660ad7234f15c5 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Sat, 2 Jul 2022 11:42:17 +0200 Subject: [PATCH] Kernel: Do a POSIX-correct signal handler reset on exec --- Kernel/Process.h | 1 + Kernel/Syscalls/execve.cpp | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/Kernel/Process.h b/Kernel/Process.h index 686c39ec00..c5dfc4a96e 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -570,6 +570,7 @@ private: bool has_tracee_thread(ProcessID tracer_pid); + void clear_signal_handlers_for_exec(); void clear_futex_queues_on_exec(); ErrorOr remap_range_as_stack(FlatPtr address, size_t size); diff --git a/Kernel/Syscalls/execve.cpp b/Kernel/Syscalls/execve.cpp index c79fd18928..0ec380a6ee 100644 --- a/Kernel/Syscalls/execve.cpp +++ b/Kernel/Syscalls/execve.cpp @@ -437,6 +437,25 @@ Process::load(NonnullRefPtr main_program_description, return interpreter_load_result; } +void Process::clear_signal_handlers_for_exec() +{ + // Comments are as they are presented in the POSIX specification, but slightly out of order. + for (size_t signal = 0; signal < m_signal_action_data.size(); signal++) { + // Except for SIGCHLD, signals set to be ignored by the calling process image shall be set to be ignored by the new process image. + // If the SIGCHLD signal is set to be ignored by the calling process image, it is unspecified whether the SIGCHLD signal is set + // to be ignored or to the default action in the new process image. + if (signal != SIGCHLD && m_signal_action_data[signal].handler_or_sigaction.get() == reinterpret_cast(SIG_IGN)) { + m_signal_action_data[signal] = {}; + m_signal_action_data[signal].handler_or_sigaction.set(reinterpret_cast(SIG_IGN)); + continue; + } + + // Signals set to the default action in the calling process image shall be set to the default action in the new process image. + // Signals set to be caught by the calling process image shall be set to the default action in the new process image. + m_signal_action_data[signal] = {}; + } +} + ErrorOr Process::do_exec(NonnullRefPtr main_program_description, NonnullOwnPtrVector arguments, NonnullOwnPtrVector environment, RefPtr interpreter_description, Thread*& new_main_thread, u32& prev_flags, const ElfW(Ehdr) & main_program_header) { @@ -532,6 +551,8 @@ ErrorOr Process::do_exec(NonnullRefPtr main_program_d auto* current_thread = Thread::current(); current_thread->reset_signals_for_exec(); + clear_signal_handlers_for_exec(); + clear_futex_queues_on_exec(); m_fds.with_exclusive([&](auto& fds) {