Commit Graph

174 Commits

Author SHA1 Message Date
Andreas Kling
68576bcf1b LibELF: Call mmap() before constructing the DynamicLoader object
Refactor DynamicLoader construction with a try_create() helper so that
we can call mmap() before making a loader. This way the loader doesn't
need to have an "mmap failed" state.

This patch also takes care of determining the ELF file size in
try_create() instead of expecting callers to provide it.
2021-01-31 11:06:00 +01:00
Andreas Kling
5bf9999652 LibELF: Add a bunch of overflow checks in ELF validation 2021-01-30 13:54:24 +01:00
Ben Wiederhake
ec91f8ad1d LibELF: Avoid quadratic memory usage weakness
Section names are referred to by offset and length. We do not check
(and probably should not check) whether these names overlap in any way.
This opened the door to many sections (in this example: about 2700)
forcing ELF::Image::m_sections to contain endless copies of the same
huge string (in this case: 882K).

Fix this by loading only the first PAGE_SIZE bytes of each name.
Since section names are only relevant for relocations and debug
information and most section names are hard-coded (and far below 4096
bytes) anyway, this should be no restriction at all for 'normal'
executables.

Found by OSS-Fuzz:
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=29187
2021-01-30 09:25:02 +01:00
Jorropo
8f8bbd1bcd
DynamicLoader: load_program_headers use variables to store regions (#5173)
Previously regions were stored in a vector and then a pointer to
regions in this vector were taken and stored. The problem is the vector
were still appended after pointers were taken, if enough regions were
present the vector would grow so large that it needed a resize, this
cause his memory to moved and now the previous pointers are now
pointing to old memory we just freed.

Fixes #5160
2021-01-30 09:21:54 +01:00
Andreas Kling
d988bd86e4 LibELF: Map text segments read-only at first
To support upcoming W^X changes in the kernel, the dynamic loader needs
to be careful about the order in which permissions are added to shared
library text segments.

We now start by mapping text segments read-only (no-write, no-exec).
If relocations are needed, we make them writable, and then finally,
for all text segments, we finish by making them read+exec.
2021-01-29 14:52:22 +01:00
Andreas Kling
3438b77aa4 LibELF: Tidy up DynamicLoader::load_program_headers() a bit
Remove a confusing temporary, rename some things and add assertions.
2021-01-29 13:45:41 +01:00
Jorropo
c33d50872e
LibELF: perror on failed mmap in load_program_headers (#5159) 2021-01-29 08:50:43 +01:00
Andreas Kling
322c161ee4 LibELF: Implement ASLR for shared libraries :^)
Use mmap() with the new MAP_RANDOMIZED flag to load shared libraries at
random addresses in each process.

To avoid address space collisions, we start by doing a large chunk mmap
that covers enough VM for both text and data, then we unmap and remap
the data segment separately, once we know everything will fit.

This is pretty cool! :^)
2021-01-28 16:23:38 +01:00
Andreas Kling
67bc5e0bbd DynamicLoader: Pass MAP_FIXED to mmap() where applicable
Otherwise the kernel is free to allocate a different address.
2021-01-27 21:14:42 +01:00
Andreas Kling
adcc1c1eff LibELF: Cache the DynamicObject in DynamicLoader
This avoids reparsing the same dynamic library file multiple times.
2021-01-25 18:57:06 +01:00
Andreas Kling
41d8734288 LibELF: Use Optional<SymbolLookupResult> as a return type
Instead of storing a "found" state inside the result object.
2021-01-25 18:57:06 +01:00
Andreas Kling
a5de46684b LibELF: Convert String::format() to String::formatted() 2021-01-25 18:57:06 +01:00
Andreas Kling
c5e52be5d4 LibELF: Convert dbgprintf() in DynamicObject class to dbgln() 2021-01-25 18:57:06 +01:00
Andreas Kling
98e4e2aae3 LibELF: Convert dbgprintf() in DynamicLinker class to dbgln() 2021-01-25 18:57:06 +01:00
Andreas Kling
e9fd81b964 LibELF: Convert dbgprintf() in DynamicLoader class to dbgln() 2021-01-25 18:57:06 +01:00
asynts
eea72b9b5c Everywhere: Hook up remaining debug macros to Debug.h. 2021-01-25 09:47:36 +01:00
asynts
acdcf59a33 Everywhere: Remove unnecessary debug comments.
It would be tempting to uncomment these statements, but that won't work
with the new changes.

This was done with the following commands:

    find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec awk -i inplace '$0 !~ /\/\/#define/ { if (!toggle) { print; } else { toggle = !toggle } } ; $0 ~/\/\/#define/ { toggle = 1 }' {} \;

    find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec awk -i inplace '$0 !~ /\/\/ #define/ { if (!toggle) { print; } else { toggle = !toggle } } ; $0 ~/\/\/ #define/ { toggle = 1 }' {} \;
2021-01-25 09:47:36 +01:00
Ben Wiederhake
67cda61b7f Libraries: Add missing headers
A C++ source file containing just
    #include <LibFoo/Bar.h>
should always compile cleanly.

This patch adds missing header inclusions that could have caused weird error
messages if they were used in a different context. Also, this confused QtCreator.
2021-01-22 21:49:54 +01:00
Ben Wiederhake
58f25092c7 Meta: Fix silly copyright line 2021-01-22 11:28:07 +01:00
Linus Groh
678919e9c1 Kernel: Set "pledge_violation" coredump metadata in REQUIRE_PROMISE()
Similar to LibC storing an assertion message before aborting, process
death by pledge violation now sets a "pledge_violation" key with the
respective pledge name as value in its coredump metadata, which the
CrashReporter will then show.
2021-01-20 21:01:15 +01:00
Linus Groh
6715ca3e16 LibELF: Remove unused m_global_symbol_lookup_func from DynamicObject
This was refactored in 3e815ad, leaving this unused member behind.
2021-01-18 19:17:52 +01:00
Brendan Coles
66b0012bfd LibELF: validate_program_headers: Validate PT_INTERP header p_filesz > 1 2021-01-16 22:39:26 +01:00
Linus Groh
568cde5e23 Kernel+LibELF+LibCoreDump+CrashReporter: Use JSON for ProcessInfo
This is in preparation of adding (much) more process information to
coredumps. As we can only have one null-terminated char[] of arbitrary
length in each struct it's now a single JSON blob, which is a great fit:
easily extensible in the future and allows for key/value pairs and even
nested objects, which will be used e.g. for the process environment, for
example.
2021-01-15 23:26:47 +01:00
Andreas Kling
13d7c09125 Libraries: Move to Userland/Libraries/ 2021-01-12 12:17:46 +01:00