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>
|
2021-06-29 23:07:36 +03:00
|
|
|
* Copyright (c) 2021, Andreas Kling <klingi@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-06-29 23:07:36 +03:00
|
|
|
#include <AK/JsonObjectSerializer.h>
|
2022-11-26 15:42:35 +03:00
|
|
|
#include <AK/Singleton.h>
|
2021-08-22 15:51:04 +03:00
|
|
|
#include <Kernel/Coredump.h>
|
2020-11-06 11:09:51 +03:00
|
|
|
#include <Kernel/FileSystem/Custody.h>
|
2021-09-07 14:39:11 +03:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2020-11-06 11:09:51 +03:00
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
2021-07-06 12:21:52 +03:00
|
|
|
#include <Kernel/KLexicalPath.h>
|
2021-08-22 02:37:17 +03:00
|
|
|
#include <Kernel/Locking/Spinlock.h>
|
2021-09-06 18:22:36 +03:00
|
|
|
#include <Kernel/Memory/ScopedAddressSpaceSwitcher.h>
|
2020-11-06 11:09:51 +03:00
|
|
|
#include <Kernel/Process.h>
|
2021-04-16 22:53:43 +03:00
|
|
|
#include <LibC/elf.h>
|
2021-08-22 15:51:04 +03:00
|
|
|
#include <LibELF/Core.h>
|
2020-11-06 11:09:51 +03:00
|
|
|
|
2021-09-30 18:52:02 +03:00
|
|
|
#define INCLUDE_USERSPACE_HEAP_MEMORY_IN_COREDUMPS 0
|
|
|
|
|
2022-11-09 13:39:58 +03:00
|
|
|
static Singleton<SpinlockProtected<OwnPtr<KString>, LockRank::None>> s_coredump_directory_path;
|
2022-11-26 15:42:35 +03:00
|
|
|
|
2020-11-06 11:09:51 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2022-11-09 13:39:58 +03:00
|
|
|
SpinlockProtected<OwnPtr<KString>, LockRank::None>& Coredump::directory_path()
|
2022-11-26 15:42:35 +03:00
|
|
|
{
|
|
|
|
return s_coredump_directory_path;
|
|
|
|
}
|
|
|
|
|
2022-08-23 21:51:42 +03:00
|
|
|
bool Coredump::FlatRegionData::looks_like_userspace_heap_region() const
|
2021-09-30 18:52:02 +03:00
|
|
|
{
|
2022-08-23 21:51:42 +03:00
|
|
|
return name().starts_with("LibJS:"sv) || name().starts_with("malloc:"sv);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Coredump::FlatRegionData::is_consistent_with_region(Memory::Region const& region) const
|
|
|
|
{
|
|
|
|
if (m_access != region.access())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (m_page_count != region.page_count() || m_size != region.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (m_vaddr != region.vaddr())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2021-09-30 18:52:02 +03:00
|
|
|
}
|
|
|
|
|
2022-08-19 21:53:40 +03:00
|
|
|
ErrorOr<NonnullOwnPtr<Coredump>> Coredump::try_create(NonnullLockRefPtr<Process> process, StringView output_path)
|
2020-11-06 11:09:51 +03:00
|
|
|
{
|
2020-12-27 02:51:48 +03:00
|
|
|
if (!process->is_dumpable()) {
|
2021-08-22 15:51:04 +03:00
|
|
|
dbgln("Refusing to generate coredump for non-dumpable process {}", process->pid().value());
|
2021-09-06 18:33:37 +03:00
|
|
|
return EPERM;
|
2020-12-25 20:27:42 +03:00
|
|
|
}
|
|
|
|
|
2022-08-23 21:51:42 +03:00
|
|
|
Vector<FlatRegionData> regions;
|
|
|
|
size_t number_of_regions = process->address_space().with([](auto& space) {
|
|
|
|
return space->region_tree().regions().size();
|
|
|
|
});
|
|
|
|
TRY(regions.try_ensure_capacity(number_of_regions));
|
|
|
|
TRY(process->address_space().with([&](auto& space) -> ErrorOr<void> {
|
|
|
|
for (auto& region : space->region_tree().regions())
|
|
|
|
TRY(regions.try_empend(region, TRY(KString::try_create(region.name()))));
|
|
|
|
return {};
|
|
|
|
}));
|
|
|
|
|
2021-09-06 18:33:37 +03:00
|
|
|
auto description = TRY(try_create_target_file(process, output_path));
|
2022-08-23 21:51:42 +03:00
|
|
|
return adopt_nonnull_own_or_enomem(new (nothrow) Coredump(move(process), move(description), move(regions)));
|
2020-11-06 11:09:51 +03:00
|
|
|
}
|
|
|
|
|
2022-08-23 21:51:42 +03:00
|
|
|
Coredump::Coredump(NonnullLockRefPtr<Process> process, NonnullLockRefPtr<OpenFileDescription> description, Vector<FlatRegionData> regions)
|
2020-12-27 02:51:48 +03:00
|
|
|
: m_process(move(process))
|
2021-09-06 18:33:37 +03:00
|
|
|
, m_description(move(description))
|
2022-08-23 21:51:42 +03:00
|
|
|
, m_regions(move(regions))
|
2020-11-06 11:09:51 +03:00
|
|
|
{
|
2021-09-30 18:52:02 +03:00
|
|
|
m_num_program_headers = 0;
|
2022-08-23 21:51:42 +03:00
|
|
|
for (auto& region : m_regions) {
|
2021-09-30 18:52:02 +03:00
|
|
|
#if !INCLUDE_USERSPACE_HEAP_MEMORY_IN_COREDUMPS
|
2022-08-23 21:51:42 +03:00
|
|
|
if (region.looks_like_userspace_heap_region())
|
|
|
|
continue;
|
2021-09-30 18:52:02 +03:00
|
|
|
#endif
|
2021-12-19 03:07:16 +03:00
|
|
|
|
2022-08-23 21:51:42 +03:00
|
|
|
if (region.access() == Memory::Region::Access::None)
|
|
|
|
continue;
|
|
|
|
++m_num_program_headers;
|
|
|
|
}
|
2021-09-30 18:52:02 +03:00
|
|
|
++m_num_program_headers; // +1 for NOTE segment
|
2020-11-06 11:09:51 +03:00
|
|
|
}
|
|
|
|
|
2022-08-19 21:53:40 +03:00
|
|
|
ErrorOr<NonnullLockRefPtr<OpenFileDescription>> Coredump::try_create_target_file(Process const& process, StringView output_path)
|
2020-11-06 11:09:51 +03:00
|
|
|
{
|
2021-07-06 12:21:52 +03:00
|
|
|
auto output_directory = KLexicalPath::dirname(output_path);
|
2022-08-21 17:02:24 +03:00
|
|
|
auto dump_directory = TRY(VirtualFileSystem::the().open_directory(Process::current().credentials(), output_directory, VirtualFileSystem::the().root_custody()));
|
2021-09-06 18:33:37 +03:00
|
|
|
auto dump_directory_metadata = dump_directory->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-08-22 15:51:04 +03:00
|
|
|
dbgln("Refusing to put coredump in sketchy directory '{}'", output_directory);
|
2021-09-06 18:33:37 +03:00
|
|
|
return EINVAL;
|
2021-01-10 13:27:02 +03:00
|
|
|
}
|
2022-08-21 17:02:24 +03:00
|
|
|
|
|
|
|
auto process_credentials = process.credentials();
|
2021-09-06 18:33:37 +03:00
|
|
|
return TRY(VirtualFileSystem::the().open(
|
2022-08-21 17:02:24 +03:00
|
|
|
Process::current().credentials(),
|
2021-07-06 12:21:52 +03:00
|
|
|
KLexicalPath::basename(output_path),
|
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-09-06 18:33:37 +03:00
|
|
|
*dump_directory,
|
2022-08-21 17:02:24 +03:00
|
|
|
UidAndGid { process_credentials->uid(), process_credentials->gid() }));
|
2020-11-06 11:09:51 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> 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';
|
2022-10-04 03:05:54 +03:00
|
|
|
#if ARCH(X86_64) || ARCH(AARCH64)
|
2021-06-29 18:48:01 +03:00
|
|
|
elf_file_header.e_ident[EI_CLASS] = ELFCLASS64;
|
2022-07-22 21:48:24 +03:00
|
|
|
#else
|
|
|
|
# error Unknown architecture
|
2021-06-29 18:48:01 +03:00
|
|
|
#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;
|
2022-10-04 03:05:54 +03:00
|
|
|
#if ARCH(X86_64)
|
2021-06-29 18:48:01 +03:00
|
|
|
elf_file_header.e_machine = EM_X86_64;
|
2022-07-22 21:48:24 +03:00
|
|
|
#elif ARCH(AARCH64)
|
|
|
|
elf_file_header.e_machine = EM_AARCH64;
|
|
|
|
#else
|
|
|
|
# error Unknown architecture
|
2021-06-29 18:48:01 +03:00
|
|
|
#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-09-06 18:33:37 +03:00
|
|
|
TRY(m_description->write(UserOrKernelBuffer::for_kernel_buffer(reinterpret_cast<uint8_t*>(&elf_file_header)), sizeof(ElfW(Ehdr))));
|
2021-09-06 18:27:56 +03:00
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
return {};
|
2020-11-06 11:09:51 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> 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));
|
2022-08-23 21:51:42 +03:00
|
|
|
for (auto& region : m_regions) {
|
2021-09-30 18:52:02 +03:00
|
|
|
#if !INCLUDE_USERSPACE_HEAP_MEMORY_IN_COREDUMPS
|
2022-08-23 21:51:42 +03:00
|
|
|
if (region.looks_like_userspace_heap_region())
|
|
|
|
continue;
|
2021-09-30 18:52:02 +03:00
|
|
|
#endif
|
|
|
|
|
2022-08-23 21:51:42 +03:00
|
|
|
if (region.access() == Memory::Region::Access::None)
|
|
|
|
continue;
|
2021-12-19 03:07:16 +03:00
|
|
|
|
2022-08-23 21:51:42 +03:00
|
|
|
ElfW(Phdr) phdr {};
|
2020-11-06 11:09:51 +03:00
|
|
|
|
2022-08-23 21:51:42 +03:00
|
|
|
phdr.p_type = PT_LOAD;
|
|
|
|
phdr.p_offset = offset;
|
|
|
|
phdr.p_vaddr = region.vaddr().get();
|
|
|
|
phdr.p_paddr = 0;
|
2020-11-06 11:09:51 +03:00
|
|
|
|
2022-08-23 21:51:42 +03:00
|
|
|
phdr.p_filesz = region.page_count() * PAGE_SIZE;
|
|
|
|
phdr.p_memsz = region.page_count() * PAGE_SIZE;
|
|
|
|
phdr.p_align = 0;
|
2020-11-06 11:09:51 +03:00
|
|
|
|
2022-08-23 21:51:42 +03:00
|
|
|
phdr.p_flags = region.is_readable() ? PF_R : 0;
|
|
|
|
if (region.is_writable())
|
|
|
|
phdr.p_flags |= PF_W;
|
|
|
|
if (region.is_executable())
|
|
|
|
phdr.p_flags |= PF_X;
|
2020-11-06 11:09:51 +03:00
|
|
|
|
2022-08-23 21:51:42 +03:00
|
|
|
offset += phdr.p_filesz;
|
2020-11-06 11:09:51 +03:00
|
|
|
|
2022-08-23 21:51:42 +03:00
|
|
|
[[maybe_unused]] auto rc = m_description->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-09-06 18:33:37 +03:00
|
|
|
TRY(m_description->write(UserOrKernelBuffer::for_kernel_buffer(reinterpret_cast<uint8_t*>(¬es_pheader)), sizeof(ElfW(Phdr))));
|
2021-09-06 18:27:56 +03:00
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
return {};
|
2020-11-06 11:09:51 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> Coredump::write_regions()
|
2020-11-06 11:09:51 +03:00
|
|
|
{
|
2021-11-21 14:28:01 +03:00
|
|
|
u8 zero_buffer[PAGE_SIZE] = {};
|
|
|
|
|
2022-08-23 21:51:42 +03:00
|
|
|
for (auto& region : m_regions) {
|
|
|
|
VERIFY(!region.is_kernel());
|
2021-09-30 18:52:02 +03:00
|
|
|
|
|
|
|
#if !INCLUDE_USERSPACE_HEAP_MEMORY_IN_COREDUMPS
|
2022-08-23 21:51:42 +03:00
|
|
|
if (region.looks_like_userspace_heap_region())
|
|
|
|
continue;
|
2021-09-30 18:52:02 +03:00
|
|
|
#endif
|
2020-11-06 11:09:51 +03:00
|
|
|
|
2022-08-23 21:51:42 +03:00
|
|
|
if (region.access() == Memory::Region::Access::None)
|
|
|
|
continue;
|
|
|
|
|
2022-08-23 22:21:20 +03:00
|
|
|
auto buffer = TRY(KBuffer::try_create_with_size("Coredump Region Copy Buffer"sv, region.page_count() * PAGE_SIZE));
|
|
|
|
|
2022-08-23 21:51:42 +03:00
|
|
|
TRY(m_process->address_space().with([&](auto& space) -> ErrorOr<void> {
|
|
|
|
auto* real_region = space->region_tree().regions().find(region.vaddr().get());
|
|
|
|
|
2023-02-04 15:35:57 +03:00
|
|
|
if (!real_region) {
|
|
|
|
dmesgln("Coredump::write_regions: Failed to find matching region in the process");
|
|
|
|
return Error::from_errno(EFAULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!region.is_consistent_with_region(*real_region)) {
|
|
|
|
dmesgln("Coredump::write_regions: Found region does not match stored metadata");
|
|
|
|
return Error::from_errno(EINVAL);
|
|
|
|
}
|
2022-08-23 21:30:12 +03:00
|
|
|
|
|
|
|
// If we crashed in the middle of mapping in Regions, they do not have a page directory yet, and will crash on a remap() call
|
2022-08-23 21:51:42 +03:00
|
|
|
if (!real_region->is_mapped())
|
|
|
|
return {};
|
2022-08-23 21:30:12 +03:00
|
|
|
|
2022-08-23 21:51:42 +03:00
|
|
|
real_region->set_readable(true);
|
|
|
|
real_region->remap();
|
2022-08-23 21:30:12 +03:00
|
|
|
|
|
|
|
for (size_t i = 0; i < region.page_count(); i++) {
|
2022-08-23 21:51:42 +03:00
|
|
|
auto page = real_region->physical_page(i);
|
2022-08-23 21:30:12 +03:00
|
|
|
auto src_buffer = [&]() -> ErrorOr<UserOrKernelBuffer> {
|
|
|
|
if (page)
|
|
|
|
return UserOrKernelBuffer::for_user_buffer(reinterpret_cast<uint8_t*>((region.vaddr().as_ptr() + (i * PAGE_SIZE))), PAGE_SIZE);
|
|
|
|
// If the current page is not backed by a physical page, we zero it in the coredump file.
|
|
|
|
return UserOrKernelBuffer::for_kernel_buffer(zero_buffer);
|
|
|
|
}();
|
2022-08-23 22:21:20 +03:00
|
|
|
TRY(src_buffer.value().read(buffer->bytes().slice(i * PAGE_SIZE, PAGE_SIZE)));
|
2022-08-23 13:28:04 +03:00
|
|
|
}
|
2022-08-23 21:51:42 +03:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}));
|
2022-08-23 22:21:20 +03:00
|
|
|
|
|
|
|
TRY(m_description->write(UserOrKernelBuffer::for_kernel_buffer(buffer->data()), buffer->size()));
|
2022-08-23 21:51:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
2020-11-06 11:09:51 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> Coredump::write_notes_segment(ReadonlyBytes notes_segment)
|
2020-11-06 11:09:51 +03:00
|
|
|
{
|
2021-09-06 19:42:15 +03:00
|
|
|
TRY(m_description->write(UserOrKernelBuffer::for_kernel_buffer(const_cast<u8*>(notes_segment.data())), notes_segment.size()));
|
2021-11-08 02:51:39 +03:00
|
|
|
return {};
|
2020-11-06 11:09:51 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> Coredump::create_notes_process_data(auto& builder) const
|
2020-12-30 15:19:53 +03:00
|
|
|
{
|
|
|
|
ELF::Core::ProcessInfo info {};
|
|
|
|
info.header.type = ELF::Core::NotesEntryHeader::Type::ProcessInfo;
|
2021-09-06 19:42:15 +03:00
|
|
|
TRY(builder.append_bytes(ReadonlyBytes { (void*)&info, sizeof(info) }));
|
2020-12-30 15:19:53 +03:00
|
|
|
|
2021-06-29 23:07:36 +03:00
|
|
|
{
|
2022-02-24 21:08:48 +03:00
|
|
|
auto process_obj = TRY(JsonObjectSerializer<>::try_create(builder));
|
|
|
|
TRY(process_obj.add("pid"sv, m_process->pid().value()));
|
|
|
|
TRY(process_obj.add("termination_signal"sv, m_process->termination_signal()));
|
|
|
|
TRY(process_obj.add("executable_path"sv, m_process->executable() ? TRY(m_process->executable()->try_serialize_absolute_path())->view() : ""sv));
|
2021-06-29 23:27:11 +03:00
|
|
|
|
|
|
|
{
|
2022-02-24 21:08:48 +03:00
|
|
|
auto arguments_array = TRY(process_obj.add_array("arguments"sv));
|
2021-12-29 03:01:27 +03:00
|
|
|
for (auto const& argument : m_process->arguments())
|
2022-02-24 21:08:48 +03:00
|
|
|
TRY(arguments_array.add(argument.view()));
|
|
|
|
TRY(arguments_array.finish());
|
2021-06-29 23:27:11 +03:00
|
|
|
}
|
2021-06-29 23:07:36 +03:00
|
|
|
|
2021-06-29 23:27:11 +03:00
|
|
|
{
|
2022-02-24 21:08:48 +03:00
|
|
|
auto environment_array = TRY(process_obj.add_array("environment"sv));
|
2021-12-29 03:01:27 +03:00
|
|
|
for (auto const& variable : m_process->environment())
|
2022-02-24 21:08:48 +03:00
|
|
|
TRY(environment_array.add(variable.view()));
|
|
|
|
TRY(environment_array.finish());
|
2021-06-29 23:27:11 +03:00
|
|
|
}
|
2022-02-24 21:08:48 +03:00
|
|
|
|
|
|
|
TRY(process_obj.finish());
|
2021-06-29 23:07:36 +03:00
|
|
|
}
|
|
|
|
|
2021-09-06 19:42:15 +03:00
|
|
|
TRY(builder.append('\0'));
|
2021-11-08 02:51:39 +03:00
|
|
|
return {};
|
2020-12-30 15:19:53 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> Coredump::create_notes_threads_data(auto& builder) const
|
2020-11-06 11:09:51 +03:00
|
|
|
{
|
2021-12-29 03:01:27 +03:00
|
|
|
for (auto const& thread : m_process->threads_for_coredump({})) {
|
2020-11-06 11:09:51 +03:00
|
|
|
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-08-10 02:19:23 +03:00
|
|
|
|
|
|
|
if (thread.current_trap())
|
|
|
|
copy_kernel_registers_into_ptrace_registers(info.regs, thread.get_register_dump_from_stack());
|
2020-11-06 11:09:51 +03:00
|
|
|
|
2021-09-06 19:42:15 +03:00
|
|
|
TRY(builder.append_bytes(ReadonlyBytes { &info, sizeof(info) }));
|
2021-01-28 10:41:18 +03:00
|
|
|
}
|
2021-11-08 02:51:39 +03:00
|
|
|
return {};
|
2020-11-06 11:09:51 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> Coredump::create_notes_regions_data(auto& builder) const
|
2020-11-06 11:09:51 +03:00
|
|
|
{
|
2021-04-07 02:20:29 +03:00
|
|
|
size_t region_index = 0;
|
2022-08-23 21:51:42 +03:00
|
|
|
for (auto const& region : m_regions) {
|
2021-09-30 18:52:02 +03:00
|
|
|
#if !INCLUDE_USERSPACE_HEAP_MEMORY_IN_COREDUMPS
|
2022-08-23 21:51:42 +03:00
|
|
|
if (region.looks_like_userspace_heap_region())
|
|
|
|
continue;
|
2021-09-30 18:52:02 +03:00
|
|
|
#endif
|
|
|
|
|
2022-08-23 21:51:42 +03:00
|
|
|
if (region.access() == Memory::Region::Access::None)
|
|
|
|
continue;
|
2021-12-19 03:07:16 +03:00
|
|
|
|
2022-08-23 21:51:42 +03:00
|
|
|
ELF::Core::MemoryRegionInfo info {};
|
|
|
|
info.header.type = ELF::Core::NotesEntryHeader::Type::MemoryRegionInfo;
|
2020-11-06 11:09:51 +03:00
|
|
|
|
2022-08-23 21:51:42 +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
|
|
|
|
2022-08-23 21:51:42 +03:00
|
|
|
TRY(builder.append_bytes(ReadonlyBytes { (void*)&info, sizeof(info) }));
|
2021-09-06 01:58:46 +03:00
|
|
|
|
2022-08-23 21:51:42 +03:00
|
|
|
// NOTE: The region name *is* null-terminated, so the following is ok:
|
|
|
|
auto name = region.name();
|
|
|
|
if (name.is_empty())
|
|
|
|
TRY(builder.append('\0'));
|
|
|
|
else
|
|
|
|
TRY(builder.append(name.characters_without_null_termination(), name.length() + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
2020-11-06 11:09:51 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> Coredump::create_notes_metadata_data(auto& builder) const
|
2020-12-30 17:20:30 +03:00
|
|
|
{
|
|
|
|
ELF::Core::Metadata metadata {};
|
|
|
|
metadata.header.type = ELF::Core::NotesEntryHeader::Type::Metadata;
|
2021-09-06 19:42:15 +03:00
|
|
|
TRY(builder.append_bytes(ReadonlyBytes { (void*)&metadata, sizeof(metadata) }));
|
2020-12-30 17:20:30 +03:00
|
|
|
|
2021-06-29 23:27:11 +03:00
|
|
|
{
|
2022-02-24 21:08:48 +03:00
|
|
|
auto metadata_obj = TRY(JsonObjectSerializer<>::try_create(builder));
|
|
|
|
TRY(m_process->for_each_coredump_property([&](auto& key, auto& value) -> ErrorOr<void> {
|
|
|
|
TRY(metadata_obj.add(key.view(), value.view()));
|
|
|
|
return {};
|
|
|
|
}));
|
|
|
|
TRY(metadata_obj.finish());
|
2021-06-29 23:27:11 +03:00
|
|
|
}
|
2021-09-06 19:42:15 +03:00
|
|
|
TRY(builder.append('\0'));
|
2021-11-08 02:51:39 +03:00
|
|
|
return {};
|
2020-12-30 17:20:30 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> Coredump::create_notes_segment_data(auto& builder) const
|
2020-11-06 11:09:51 +03:00
|
|
|
{
|
2021-09-06 19:42:15 +03:00
|
|
|
TRY(create_notes_process_data(builder));
|
|
|
|
TRY(create_notes_threads_data(builder));
|
|
|
|
TRY(create_notes_regions_data(builder));
|
|
|
|
TRY(create_notes_metadata_data(builder));
|
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;
|
2021-09-06 19:42:15 +03:00
|
|
|
TRY(builder.append(ReadonlyBytes { &null_entry, sizeof(null_entry) }));
|
2020-11-06 11:09:51 +03:00
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
return {};
|
2020-11-06 11:09:51 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> Coredump::write()
|
2020-11-06 11:09:51 +03:00
|
|
|
{
|
2021-09-06 18:22:36 +03:00
|
|
|
ScopedAddressSpaceSwitcher switcher(m_process);
|
2020-11-06 11:09:51 +03:00
|
|
|
|
2021-09-07 16:54:23 +03:00
|
|
|
auto builder = TRY(KBufferBuilder::try_create());
|
2021-09-06 19:42:15 +03:00
|
|
|
TRY(create_notes_segment_data(builder));
|
2021-09-06 18:27:56 +03:00
|
|
|
TRY(write_elf_header());
|
2021-09-06 19:42:15 +03:00
|
|
|
TRY(write_program_headers(builder.bytes().size()));
|
2021-09-06 18:27:56 +03:00
|
|
|
TRY(write_regions());
|
2021-09-06 19:42:15 +03:00
|
|
|
TRY(write_notes_segment(builder.bytes()));
|
2020-12-22 11:59:14 +03:00
|
|
|
|
2022-08-21 17:15:29 +03:00
|
|
|
return m_description->chmod(Process::current().credentials(), 0600); // Make coredump file read/writable
|
2020-11-06 11:09:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|