Kernel: Do a POSIX-correct signal handler reset on exec

This commit is contained in:
Tim Schumacher 2022-07-02 11:42:17 +02:00 committed by Idan Horowitz
parent a4e3fff3fb
commit add4dd3589
Notes: sideshowbarker 2024-07-17 09:41:55 +09:00
2 changed files with 22 additions and 0 deletions
Kernel

View file

@ -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<void> remap_range_as_stack(FlatPtr address, size_t size);

View file

@ -437,6 +437,25 @@ Process::load(NonnullRefPtr<OpenFileDescription> 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<FlatPtr>(SIG_IGN)) {
m_signal_action_data[signal] = {};
m_signal_action_data[signal].handler_or_sigaction.set(reinterpret_cast<FlatPtr>(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<void> Process::do_exec(NonnullRefPtr<OpenFileDescription> main_program_description, NonnullOwnPtrVector<KString> arguments, NonnullOwnPtrVector<KString> environment,
RefPtr<OpenFileDescription> interpreter_description, Thread*& new_main_thread, u32& prev_flags, const ElfW(Ehdr) & main_program_header)
{
@ -532,6 +551,8 @@ ErrorOr<void> Process::do_exec(NonnullRefPtr<OpenFileDescription> 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) {