2020-11-06 11:09:51 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019-2020, Jesse Buhagiar <jooster669@gmail.com>
|
|
|
|
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
2021-04-22 23:51:19 +03:00
|
|
|
* Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
|
2020-11-06 11:09:51 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-11-06 11:09:51 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/ByteBuffer.h>
|
2021-01-15 22:21:03 +03:00
|
|
|
#include <AK/JsonArray.h>
|
2020-12-30 17:20:30 +03:00
|
|
|
#include <AK/JsonObject.h>
|
2020-11-06 11:09:51 +03:00
|
|
|
#include <Kernel/CoreDump.h>
|
|
|
|
#include <Kernel/FileSystem/Custody.h>
|
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
#include <Kernel/RTC.h>
|
|
|
|
#include <Kernel/SpinLock.h>
|
|
|
|
#include <Kernel/VM/ProcessPagingScope.h>
|
2021-04-16 22:53:43 +03:00
|
|
|
#include <LibC/elf.h>
|
2020-11-06 11:09:51 +03:00
|
|
|
#include <LibELF/CoreDump.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2020-12-27 02:51:48 +03:00
|
|
|
OwnPtr<CoreDump> CoreDump::create(NonnullRefPtr<Process> process, const String& output_path)
|
2020-11-06 11:09:51 +03:00
|
|
|
{
|
2020-12-27 02:51:48 +03:00
|
|
|
if (!process->is_dumpable()) {
|
|
|
|
dbgln("Refusing to generate CoreDump for non-dumpable process {}", process->pid().value());
|
2021-01-11 02:29:28 +03:00
|
|
|
return {};
|
2020-12-25 20:27:42 +03:00
|
|
|
}
|
|
|
|
|
2020-11-13 23:38:58 +03:00
|
|
|
auto fd = create_target_file(process, output_path);
|
2020-11-06 11:09:51 +03:00
|
|
|
if (!fd)
|
2021-01-11 02:29:28 +03:00
|
|
|
return {};
|
2021-06-20 11:21:16 +03:00
|
|
|
return adopt_own_if_nonnull(new (nothrow) CoreDump(move(process), fd.release_nonnull()));
|
2020-11-06 11:09:51 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 02:51:48 +03:00
|
|
|
CoreDump::CoreDump(NonnullRefPtr<Process> process, NonnullRefPtr<FileDescription>&& fd)
|
|
|
|
: m_process(move(process))
|
2020-11-06 11:09:51 +03:00
|
|
|
, m_fd(move(fd))
|
2021-02-08 17:45:40 +03:00
|
|
|
, m_num_program_headers(m_process->space().region_count() + 1) // +1 for NOTE segment
|
2020-11-06 11:09:51 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-12-15 13:17:01 +03:00
|
|
|
RefPtr<FileDescription> CoreDump::create_target_file(const Process& process, const String& output_path)
|
2020-11-06 11:09:51 +03:00
|
|
|
{
|
2020-12-15 13:17:01 +03:00
|
|
|
LexicalPath lexical_path(output_path);
|
2021-02-25 13:32:35 +03:00
|
|
|
const auto& output_directory = lexical_path.dirname();
|
2021-01-10 13:27:02 +03:00
|
|
|
auto dump_directory = VFS::the().open_directory(output_directory, VFS::the().root_custody());
|
|
|
|
if (dump_directory.is_error()) {
|
|
|
|
dbgln("Can't find directory '{}' for core dump", output_directory);
|
|
|
|
return nullptr;
|
2020-11-06 11:09:51 +03:00
|
|
|
}
|
2021-01-10 13:27:02 +03:00
|
|
|
auto dump_directory_metadata = dump_directory.value()->inode().metadata();
|
2021-03-26 18:46:20 +03:00
|
|
|
if (dump_directory_metadata.uid != 0 || dump_directory_metadata.gid != 0 || dump_directory_metadata.mode != 040777) {
|
2021-01-10 13:27:02 +03:00
|
|
|
dbgln("Refusing to put core dump in sketchy directory '{}'", output_directory);
|
2020-11-06 11:09:51 +03:00
|
|
|
return nullptr;
|
2021-01-10 13:27:02 +03:00
|
|
|
}
|
2020-11-06 11:09:51 +03:00
|
|
|
auto fd_or_error = VFS::the().open(
|
2020-12-15 13:17:01 +03:00
|
|
|
lexical_path.basename(),
|
2020-11-06 11:09:51 +03:00
|
|
|
O_CREAT | O_WRONLY | O_EXCL,
|
2021-01-23 19:57:21 +03:00
|
|
|
S_IFREG, // We will enable reading from userspace when we finish generating the coredump file
|
2021-01-10 13:27:02 +03:00
|
|
|
*dump_directory.value(),
|
2020-11-06 11:09:51 +03:00
|
|
|
UidAndGid { process.uid(), process.gid() });
|
|
|
|
|
2021-01-10 13:27:02 +03:00
|
|
|
if (fd_or_error.is_error()) {
|
|
|
|
dbgln("Failed to open core dump '{}' for writing", output_path);
|
2020-11-06 11:09:51 +03:00
|
|
|
return nullptr;
|
2021-01-10 13:27:02 +03:00
|
|
|
}
|
2020-11-06 11:09:51 +03:00
|
|
|
|
|
|
|
return fd_or_error.value();
|
|
|
|
}
|
|
|
|
|
2020-12-22 11:59:14 +03:00
|
|
|
KResult CoreDump::write_elf_header()
|
2020-11-06 11:09:51 +03:00
|
|
|
{
|
2021-06-28 18:24:08 +03:00
|
|
|
ElfW(Ehdr) elf_file_header;
|
2020-11-06 11:09:51 +03:00
|
|
|
elf_file_header.e_ident[EI_MAG0] = 0x7f;
|
|
|
|
elf_file_header.e_ident[EI_MAG1] = 'E';
|
|
|
|
elf_file_header.e_ident[EI_MAG2] = 'L';
|
|
|
|
elf_file_header.e_ident[EI_MAG3] = 'F';
|
2021-06-29 18:48:01 +03:00
|
|
|
#if ARCH(I386)
|
2020-11-06 11:09:51 +03:00
|
|
|
elf_file_header.e_ident[EI_CLASS] = ELFCLASS32;
|
2021-06-29 18:48:01 +03:00
|
|
|
#else
|
|
|
|
elf_file_header.e_ident[EI_CLASS] = ELFCLASS64;
|
|
|
|
#endif
|
2020-11-06 11:09:51 +03:00
|
|
|
elf_file_header.e_ident[EI_DATA] = ELFDATA2LSB;
|
|
|
|
elf_file_header.e_ident[EI_VERSION] = EV_CURRENT;
|
|
|
|
elf_file_header.e_ident[EI_OSABI] = 0; // ELFOSABI_NONE
|
|
|
|
elf_file_header.e_ident[EI_ABIVERSION] = 0;
|
|
|
|
elf_file_header.e_ident[EI_PAD + 1] = 0;
|
|
|
|
elf_file_header.e_ident[EI_PAD + 2] = 0;
|
|
|
|
elf_file_header.e_ident[EI_PAD + 3] = 0;
|
|
|
|
elf_file_header.e_ident[EI_PAD + 4] = 0;
|
|
|
|
elf_file_header.e_ident[EI_PAD + 5] = 0;
|
|
|
|
elf_file_header.e_ident[EI_PAD + 6] = 0;
|
|
|
|
elf_file_header.e_type = ET_CORE;
|
2021-06-29 18:48:01 +03:00
|
|
|
#if ARCH(I386)
|
2020-11-06 11:09:51 +03:00
|
|
|
elf_file_header.e_machine = EM_386;
|
2021-06-29 18:48:01 +03:00
|
|
|
#else
|
|
|
|
elf_file_header.e_machine = EM_X86_64;
|
|
|
|
#endif
|
2020-11-06 11:09:51 +03:00
|
|
|
elf_file_header.e_version = 1;
|
|
|
|
elf_file_header.e_entry = 0;
|
2021-06-28 18:24:08 +03:00
|
|
|
elf_file_header.e_phoff = sizeof(ElfW(Ehdr));
|
2020-11-06 11:09:51 +03:00
|
|
|
elf_file_header.e_shoff = 0;
|
|
|
|
elf_file_header.e_flags = 0;
|
2021-06-28 18:24:08 +03:00
|
|
|
elf_file_header.e_ehsize = sizeof(ElfW(Ehdr));
|
|
|
|
elf_file_header.e_shentsize = sizeof(ElfW(Shdr));
|
|
|
|
elf_file_header.e_phentsize = sizeof(ElfW(Phdr));
|
2020-11-06 11:09:51 +03:00
|
|
|
elf_file_header.e_phnum = m_num_program_headers;
|
|
|
|
elf_file_header.e_shnum = 0;
|
|
|
|
elf_file_header.e_shstrndx = SHN_UNDEF;
|
|
|
|
|
2021-06-28 18:24:08 +03:00
|
|
|
auto result = m_fd->write(UserOrKernelBuffer::for_kernel_buffer(reinterpret_cast<uint8_t*>(&elf_file_header)), sizeof(ElfW(Ehdr)));
|
2020-12-22 11:59:14 +03:00
|
|
|
if (result.is_error())
|
|
|
|
return result.error();
|
|
|
|
return KSuccess;
|
2020-11-06 11:09:51 +03:00
|
|
|
}
|
|
|
|
|
2020-12-22 11:59:14 +03:00
|
|
|
KResult CoreDump::write_program_headers(size_t notes_size)
|
2020-11-06 11:09:51 +03:00
|
|
|
{
|
2021-06-28 18:24:08 +03:00
|
|
|
size_t offset = sizeof(ElfW(Ehdr)) + m_num_program_headers * sizeof(ElfW(Phdr));
|
2021-02-08 17:45:40 +03:00
|
|
|
for (auto& region : m_process->space().regions()) {
|
2021-06-28 18:24:08 +03:00
|
|
|
ElfW(Phdr) phdr {};
|
2020-11-06 11:09:51 +03:00
|
|
|
|
|
|
|
phdr.p_type = PT_LOAD;
|
|
|
|
phdr.p_offset = offset;
|
2021-04-07 02:20:29 +03:00
|
|
|
phdr.p_vaddr = region->vaddr().get();
|
2020-11-06 11:09:51 +03:00
|
|
|
phdr.p_paddr = 0;
|
|
|
|
|
2021-04-07 02:20:29 +03:00
|
|
|
phdr.p_filesz = region->page_count() * PAGE_SIZE;
|
|
|
|
phdr.p_memsz = region->page_count() * PAGE_SIZE;
|
2020-11-06 11:09:51 +03:00
|
|
|
phdr.p_align = 0;
|
|
|
|
|
2021-04-07 02:20:29 +03:00
|
|
|
phdr.p_flags = region->is_readable() ? PF_R : 0;
|
|
|
|
if (region->is_writable())
|
2020-11-06 11:09:51 +03:00
|
|
|
phdr.p_flags |= PF_W;
|
2021-04-07 02:20:29 +03:00
|
|
|
if (region->is_executable())
|
2020-11-06 11:09:51 +03:00
|
|
|
phdr.p_flags |= PF_X;
|
|
|
|
|
|
|
|
offset += phdr.p_filesz;
|
|
|
|
|
2021-06-28 18:24:08 +03:00
|
|
|
[[maybe_unused]] auto rc = m_fd->write(UserOrKernelBuffer::for_kernel_buffer(reinterpret_cast<uint8_t*>(&phdr)), sizeof(ElfW(Phdr)));
|
2020-11-06 11:09:51 +03:00
|
|
|
}
|
|
|
|
|
2021-06-28 18:24:08 +03:00
|
|
|
ElfW(Phdr) notes_pheader {};
|
2020-11-06 11:09:51 +03:00
|
|
|
notes_pheader.p_type = PT_NOTE;
|
|
|
|
notes_pheader.p_offset = offset;
|
|
|
|
notes_pheader.p_vaddr = 0;
|
|
|
|
notes_pheader.p_paddr = 0;
|
|
|
|
notes_pheader.p_filesz = notes_size;
|
2020-12-27 23:33:32 +03:00
|
|
|
notes_pheader.p_memsz = notes_size;
|
2020-11-06 11:09:51 +03:00
|
|
|
notes_pheader.p_align = 0;
|
|
|
|
notes_pheader.p_flags = 0;
|
|
|
|
|
2021-06-28 18:24:08 +03:00
|
|
|
auto result = m_fd->write(UserOrKernelBuffer::for_kernel_buffer(reinterpret_cast<uint8_t*>(¬es_pheader)), sizeof(ElfW(Phdr)));
|
2020-12-22 11:59:14 +03:00
|
|
|
if (result.is_error())
|
|
|
|
return result.error();
|
|
|
|
return KSuccess;
|
2020-11-06 11:09:51 +03:00
|
|
|
}
|
|
|
|
|
2020-12-22 11:59:14 +03:00
|
|
|
KResult CoreDump::write_regions()
|
2020-11-06 11:09:51 +03:00
|
|
|
{
|
2021-02-08 17:45:40 +03:00
|
|
|
for (auto& region : m_process->space().regions()) {
|
2021-04-07 02:20:29 +03:00
|
|
|
if (region->is_kernel())
|
2020-11-06 11:09:51 +03:00
|
|
|
continue;
|
|
|
|
|
2021-04-07 02:20:29 +03:00
|
|
|
region->set_readable(true);
|
|
|
|
region->remap();
|
2020-11-06 11:09:51 +03:00
|
|
|
|
2021-04-07 02:20:29 +03:00
|
|
|
for (size_t i = 0; i < region->page_count(); i++) {
|
|
|
|
auto* page = region->physical_page(i);
|
2020-11-06 11:09:51 +03:00
|
|
|
|
|
|
|
uint8_t zero_buffer[PAGE_SIZE] = {};
|
|
|
|
Optional<UserOrKernelBuffer> src_buffer;
|
|
|
|
|
|
|
|
if (page) {
|
2021-04-07 02:20:29 +03:00
|
|
|
src_buffer = UserOrKernelBuffer::for_user_buffer(reinterpret_cast<uint8_t*>((region->vaddr().as_ptr() + (i * PAGE_SIZE))), PAGE_SIZE);
|
2020-11-06 11:09:51 +03:00
|
|
|
} else {
|
|
|
|
// If the current page is not backed by a physical page, we zero it in the coredump file.
|
|
|
|
// TODO: Do we want to include the contents of pages that have not been faulted-in in the coredump?
|
|
|
|
// (A page may not be backed by a physical page because it has never been faulted in when the process ran).
|
|
|
|
src_buffer = UserOrKernelBuffer::for_kernel_buffer(zero_buffer);
|
|
|
|
}
|
2020-12-22 11:59:14 +03:00
|
|
|
auto result = m_fd->write(src_buffer.value(), PAGE_SIZE);
|
|
|
|
if (result.is_error())
|
|
|
|
return result.error();
|
2020-11-06 11:09:51 +03:00
|
|
|
}
|
|
|
|
}
|
2020-12-22 11:59:14 +03:00
|
|
|
return KSuccess;
|
2020-11-06 11:09:51 +03:00
|
|
|
}
|
|
|
|
|
2020-12-22 11:59:14 +03:00
|
|
|
KResult CoreDump::write_notes_segment(ByteBuffer& notes_segment)
|
2020-11-06 11:09:51 +03:00
|
|
|
{
|
2020-12-22 11:59:14 +03:00
|
|
|
auto result = m_fd->write(UserOrKernelBuffer::for_kernel_buffer(notes_segment.data()), notes_segment.size());
|
|
|
|
if (result.is_error())
|
|
|
|
return result.error();
|
|
|
|
return KSuccess;
|
2020-11-06 11:09:51 +03:00
|
|
|
}
|
|
|
|
|
2020-12-30 15:19:53 +03:00
|
|
|
ByteBuffer CoreDump::create_notes_process_data() const
|
|
|
|
{
|
|
|
|
ByteBuffer process_data;
|
|
|
|
|
|
|
|
ELF::Core::ProcessInfo info {};
|
|
|
|
info.header.type = ELF::Core::NotesEntryHeader::Type::ProcessInfo;
|
|
|
|
process_data.append((void*)&info, sizeof(info));
|
|
|
|
|
2021-01-14 01:59:22 +03:00
|
|
|
JsonObject process_obj;
|
|
|
|
process_obj.set("pid", m_process->pid().value());
|
|
|
|
process_obj.set("termination_signal", m_process->termination_signal());
|
|
|
|
process_obj.set("executable_path", m_process->executable() ? m_process->executable()->absolute_path() : String::empty());
|
2021-01-15 22:21:03 +03:00
|
|
|
process_obj.set("arguments", JsonArray(m_process->arguments()));
|
|
|
|
process_obj.set("environment", JsonArray(m_process->environment()));
|
2021-01-14 01:59:22 +03:00
|
|
|
|
|
|
|
auto json_data = process_obj.to_string();
|
|
|
|
process_data.append(json_data.characters(), json_data.length() + 1);
|
2020-12-30 15:19:53 +03:00
|
|
|
|
|
|
|
return process_data;
|
|
|
|
}
|
|
|
|
|
2020-11-06 11:09:51 +03:00
|
|
|
ByteBuffer CoreDump::create_notes_threads_data() const
|
|
|
|
{
|
|
|
|
ByteBuffer threads_data;
|
|
|
|
|
2021-01-28 10:41:18 +03:00
|
|
|
for (auto& thread : m_process->threads_for_coredump({})) {
|
2020-11-06 11:09:51 +03:00
|
|
|
ByteBuffer entry_buff;
|
|
|
|
|
|
|
|
ELF::Core::ThreadInfo info {};
|
2020-11-11 23:02:39 +03:00
|
|
|
info.header.type = ELF::Core::NotesEntryHeader::Type::ThreadInfo;
|
2020-11-06 11:09:51 +03:00
|
|
|
info.tid = thread.tid().value();
|
2021-02-08 21:34:41 +03:00
|
|
|
copy_kernel_registers_into_ptrace_registers(info.regs, thread.get_register_dump_from_stack());
|
2020-11-06 11:09:51 +03:00
|
|
|
|
|
|
|
entry_buff.append((void*)&info, sizeof(info));
|
|
|
|
|
|
|
|
threads_data += entry_buff;
|
2021-01-28 10:41:18 +03:00
|
|
|
}
|
2020-11-06 11:09:51 +03:00
|
|
|
return threads_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
ByteBuffer CoreDump::create_notes_regions_data() const
|
|
|
|
{
|
|
|
|
ByteBuffer regions_data;
|
2021-04-07 02:20:29 +03:00
|
|
|
size_t region_index = 0;
|
|
|
|
for (auto& region : m_process->space().regions()) {
|
2020-11-06 11:09:51 +03:00
|
|
|
|
|
|
|
ByteBuffer memory_region_info_buffer;
|
|
|
|
ELF::Core::MemoryRegionInfo info {};
|
2020-11-11 23:02:39 +03:00
|
|
|
info.header.type = ELF::Core::NotesEntryHeader::Type::MemoryRegionInfo;
|
2020-11-06 11:09:51 +03:00
|
|
|
|
2021-04-07 02:20:29 +03:00
|
|
|
info.region_start = region->vaddr().get();
|
|
|
|
info.region_end = region->vaddr().offset(region->size()).get();
|
|
|
|
info.program_header_index = region_index++;
|
2020-11-06 11:09:51 +03:00
|
|
|
|
|
|
|
memory_region_info_buffer.append((void*)&info, sizeof(info));
|
2021-05-28 10:33:14 +03:00
|
|
|
// NOTE: The region name *is* null-terminated, so the following is ok:
|
2021-05-28 17:13:47 +03:00
|
|
|
auto name = region->name();
|
2021-05-30 20:59:03 +03:00
|
|
|
if (name.is_empty()) {
|
|
|
|
char null_terminator = '\0';
|
|
|
|
memory_region_info_buffer.append(&null_terminator, 1);
|
|
|
|
} else {
|
2021-05-28 17:13:47 +03:00
|
|
|
memory_region_info_buffer.append(name.characters_without_null_termination(), name.length() + 1);
|
2021-05-30 20:59:03 +03:00
|
|
|
}
|
2020-11-06 11:09:51 +03:00
|
|
|
|
|
|
|
regions_data += memory_region_info_buffer;
|
|
|
|
}
|
|
|
|
return regions_data;
|
|
|
|
}
|
|
|
|
|
2020-12-30 17:20:30 +03:00
|
|
|
ByteBuffer CoreDump::create_notes_metadata_data() const
|
|
|
|
{
|
|
|
|
ByteBuffer metadata_data;
|
|
|
|
|
|
|
|
ELF::Core::Metadata metadata {};
|
|
|
|
metadata.header.type = ELF::Core::NotesEntryHeader::Type::Metadata;
|
|
|
|
metadata_data.append((void*)&metadata, sizeof(metadata));
|
|
|
|
|
|
|
|
JsonObject metadata_obj;
|
|
|
|
for (auto& it : m_process->coredump_metadata())
|
|
|
|
metadata_obj.set(it.key, it.value);
|
|
|
|
auto json_data = metadata_obj.to_string();
|
|
|
|
metadata_data.append(json_data.characters(), json_data.length() + 1);
|
|
|
|
|
|
|
|
return metadata_data;
|
|
|
|
}
|
|
|
|
|
2020-11-06 11:09:51 +03:00
|
|
|
ByteBuffer CoreDump::create_notes_segment_data() const
|
|
|
|
{
|
|
|
|
ByteBuffer notes_buffer;
|
|
|
|
|
2020-12-30 15:19:53 +03:00
|
|
|
notes_buffer += create_notes_process_data();
|
2020-11-06 11:09:51 +03:00
|
|
|
notes_buffer += create_notes_threads_data();
|
|
|
|
notes_buffer += create_notes_regions_data();
|
2020-12-30 17:20:30 +03:00
|
|
|
notes_buffer += create_notes_metadata_data();
|
2020-11-06 11:09:51 +03:00
|
|
|
|
2020-11-11 23:02:39 +03:00
|
|
|
ELF::Core::NotesEntryHeader null_entry {};
|
|
|
|
null_entry.type = ELF::Core::NotesEntryHeader::Type::Null;
|
2020-11-06 11:09:51 +03:00
|
|
|
notes_buffer.append(&null_entry, sizeof(null_entry));
|
|
|
|
|
|
|
|
return notes_buffer;
|
|
|
|
}
|
|
|
|
|
2020-12-22 11:59:14 +03:00
|
|
|
KResult CoreDump::write()
|
2020-11-06 11:09:51 +03:00
|
|
|
{
|
2021-02-08 17:45:40 +03:00
|
|
|
ScopedSpinLock lock(m_process->space().get_lock());
|
2020-11-06 11:09:51 +03:00
|
|
|
ProcessPagingScope scope(m_process);
|
|
|
|
|
|
|
|
ByteBuffer notes_segment = create_notes_segment_data();
|
|
|
|
|
2020-12-22 11:59:14 +03:00
|
|
|
auto result = write_elf_header();
|
|
|
|
if (result.is_error())
|
|
|
|
return result;
|
|
|
|
result = write_program_headers(notes_segment.size());
|
|
|
|
if (result.is_error())
|
|
|
|
return result;
|
|
|
|
result = write_regions();
|
|
|
|
if (result.is_error())
|
|
|
|
return result;
|
|
|
|
result = write_notes_segment(notes_segment);
|
|
|
|
if (result.is_error())
|
|
|
|
return result;
|
|
|
|
|
2021-03-26 18:46:20 +03:00
|
|
|
return m_fd->chmod(0600); // Make coredump file read/writable
|
2020-11-06 11:09:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|