Commit Graph

31 Commits

Author SHA1 Message Date
Andreas Kling
34b81f17c2 LibELF: Avoid unnecessarily recomputing loop boundaries over and over 2020-02-22 11:25:15 +01:00
Andreas Kling
4198061534 LibELF: Use the ELF_STRTAB string constant instead of hard-coding 2020-02-21 16:16:23 +01:00
Andreas Kling
26fb3f7269 LibELF: Short-circuit symbolication when there are no symbols 2020-02-19 22:18:41 +01:00
Andreas Kling
a356e48150 Kernel: Move all code into the Kernel namespace 2020-02-16 01:27:42 +01:00
Andreas Kling
3bbf4610d2 AK: Add a forward declaration header
You can now #include <AK/Forward.h> to get most of the AK types as
forward declarations.

Header dependency explosion is one of the main contributors to compile
times at the moment, so this is a step towards smaller include graphs.
2020-02-14 23:31:18 +01:00
Andreas Kling
6cbd72f54f AK: Remove bitrotted Traits::dump() mechanism
This was only used by HashTable::dump() which I used when doing the
first HashTable implementation. Removing this allows us to also remove
most includes of <AK/kstdio.h>.
2020-02-10 11:55:34 +01:00
Liav A
7c4dd0c8cf LibELF: Use VirtualAddress class from LibBareMetal 2020-02-09 19:38:17 +01:00
Andreas Kling
f8b00aa290 LibGfx: Unpublish Gfx::Size from the global namespace 2020-02-06 13:32:14 +01:00
Andrew Kaster
117820f610 Meta: Claim copyright on files added by me
Demos/DynamicLink, LibC/cxxabi.cpp, and LibELF/ELFDynamic*.[cpp/h]
2020-02-02 02:05:38 +01:00
Andreas Kling
94ca55cefd Meta: Add license header to source files
As suggested by Joshua, this commit adds the 2-clause BSD license as a
comment block to the top of every source file.

For the first pass, I've just added myself for simplicity. I encourage
everyone to add themselves as copyright holders of any file they've
added or modified in some significant way. If I've added myself in
error somewhere, feel free to replace it with the appropriate copyright
holder instead.

Going forward, all new source files should include a license header.
2020-01-18 09:45:54 +01:00
Andreas Kling
c6e552ac8f Kernel+LibELF: Don't blindly trust ELF symbol offsets in symbolication
It was possible to craft a custom ELF executable that when symbolicated
would cause the kernel to read from user-controlled addresses anywhere
in memory. You could then fetch this memory via /proc/PID/stack

We fix this by making ELFImage hand out StringView rather than raw
const char* for symbol names. In case a symbol offset is outside the
ELF image, you get a null StringView. :^)

Test: Kernel/elf-symbolication-kernel-read-exploit.cpp
2020-01-16 22:11:31 +01:00
Andrew Kaster
046d6a6bbb LibELF: Add methods to validate the ELF and program headers
These will make sure there's no funny business or funny offsets in the
main ELF header or each Program Header. More can still be done (like
validating section headers), but this is a good start
2020-01-13 13:03:30 +01:00
Andreas Kling
197e73ee31 Kernel+LibELF: Enable SMAP protection during non-syscall exec()
When loading a new executable, we now map the ELF image in kernel-only
memory and parse it there. Then we use copy_to_user() when initializing
writable regions with data from the executable.

Note that the exec() syscall still disables SMAP protection and will
require additional work. This patch only affects kernel-originated
process spawns.
2020-01-10 10:57:06 +01:00
Andrew Kaster
c21f384d17 LibELF: Remove DynamicSection from ELFImage
Since ELFDynamicObject needs the actual virtual address of the .dynamic
section in the loaded image, and not the file offset like we assumed
before, due to MAP_PRIVATE secretly giving us a MAP_SHARED, we can
remove all of the Dynamic* code from ELFImage.

ELFDynamicLoader only needs ELFImage to get the Program headers at this
point. More consolidation opportunities seem likely in the future.
2020-01-09 09:29:36 +01:00
Andrew Kaster
2e349337d3 LibELF: Map .text segment with MAP_ANONYMOUS for shared objects
We need to workaround the fact that MAP_PRIVATE when passed a file
descriptor doesn't work the way we expect. We can't change the
permissions on our mmap to PROT_WRITE if the original executable doesn't
have PROT_WRITE.

Because of this, we need to construct our ELFDynamicObject using the
actual virtual address of the .dynamic section, instead of using the
offset into the ELFImage that was actually getting modified by accident
...somehow. Not clear what was going on.
2020-01-09 09:29:36 +01:00
Andreas Kling
78a63930cc Kernel+LibELF: Validate PT_LOAD and PT_TLS offsets before memcpy()'ing
Before this, you could make the kernel copy memory from anywhere by
setting up an ELF executable with a program header specifying file
offsets outside the file.

Since ELFImage didn't even know how large it was, we had no clue that
we were copying things from outside the ELF.

Fix this by adding a size field to ELFImage and validating program
header ranges before memcpy()'ing to them.

The ELF code is definitely going to need more validation and checking.
2020-01-06 21:04:57 +01:00
Andreas Kling
7ae7a60caa LibELF: Fix stack overflow in ELFImage::relocations()
Thanks to braindead for finding the bug! :^)
2020-01-05 10:37:54 +01:00
Andrew Kaster
767f4c7421 LibELF+LibC: Split ELFDynamicObject into a Loader + Object
Separate some responsibilities:

ELFDynamicLoader is responsible for loading elf binaries from disk and
performing relocations, calling init functions, and eventually calling
finalizer functions.

ELFDynamicObject is a helper class to parse the .dynamic section of an
elf binary, or the table of Elf32_Dyn entries at the _DYNAMIC symbol.
ELFDynamicObject now owns the helper classes for Relocations, Symbols,
Sections and the like that ELFDynamicLoader will use to perform
relocations and symbol lookup.

Because these new helpers are constructed from offsets into the .dynamic
section within the loaded .data section of the binary, we don't need the
ELFImage for nearly as much of the loading processes as we did before.
Therefore we can remove most of the extra DynamicXXX classes and just
keep the one that lets us find the location of _DYNAMIC in the new ELF.

And finally, since we changed the name of the class that dlopen/dlsym
care about, we need to compile/link and use the new ELFDynamicLoader
class in LibC.
2020-01-04 10:39:04 +01:00
Andrew Kaster
2c4f837428 LibELF: Simplify R_386_32 relocations to ignore symbol bind value
For dynamic loading, the symbol bind of a symbol actually doesn't
matter. We could do what old glibc did and try to find a strong
symbol for any weak definitions, but the ELF spec doesn't require
it and they changed that a few years ago anyway. So, moot point. :)
2020-01-02 12:28:29 +01:00
Andrew Kaster
331f37d1a8 LibELF: Re-organize ELFDynamicObject::load and add PLT trampoline
ELFDynamicObject::load looks a lot better with all the steps
re-organized into helpers.

Add plt_trampoline.S to handle PLT fixups for lazy loading.
Add the needed trampoline-trampolines in ELFDynamicObject to get to
the proper relocations and to return the symbol back to the assembly
method to call into from the PLT once we return back to user code.
2020-01-01 23:54:06 +01:00
Andrew Kaster
f23dc4ea69 LibELF: Call DT_INIT method now that startfiles are correct for DSOs
We weren't calling the method here before because it was ill-formed.
No start files meant that we got the front half of the init section but
not the back half (no 'ret' in _init!). Now that we have the proper
crtbeginS and crtendS files from libgcc to help us out, we can assume
that DSOs will have the proper _init method defined.
2020-01-01 23:05:17 +01:00
Andrew Kaster
a18b37880e LibELF: Add ELFDynamicObject to dynamically load libaries
This patch also adds some missing relocation defines to exec_elf.h,
and a few helper classes/methods to ELFImage so that we can use it
for our dynamically loaded libs and not just main program images from
the kernel :)
2020-01-01 17:48:41 +01:00
Andrew Kaster
21161342ef LibELF: Replace kprintf's in ELFImage.cpp with dbgprintf
This lets us use the class in userspace
2020-01-01 17:48:41 +01:00
Andreas Kling
9e55bcb7da Kernel: Make kernel memory regions be non-executable by default
From now on, you'll have to request executable memory specifically
if you want some.
2019-12-25 22:41:34 +01:00
joshua stein
ac25438d54 Build: clean up build system, use one shared Makefile
Allow everything to be built from the top level directory with just
'make', cleaned with 'make clean', and installed with 'make
install'.  Also support these in any particular subdirectory.

Specifying 'make VERBOSE=1' will print each ld/g++/etc. command as
it runs.

Kernel and early host tools (IPCCompiler, etc.) are built as
object.host.o so that they don't conflict with other things built
with the cross-compiler.
2019-12-20 20:20:54 +01:00
Andreas Kling
0f393148da Kernel: Separate out the symbol offsets in profile output
Instead of saying "main +39" and "main +57" etc, we now have a separate
field in /proc/profile for the offset-into-the-symbol.
2019-12-12 21:59:47 +01:00
Andreas Kling
f75a6b9daa Kernel: Demangle kernel C++ symbols correctly again
I broke this while implementing module linking. Also move the actual
demangling work to AK, in AK::demangle(const char*)
2019-11-29 14:59:15 +01:00
Andreas Kling
1f34e16ec6 LibELF: Add ELFImage::Symbol::bind() 2019-11-28 21:29:57 +01:00
Andreas Kling
c10a5ac4ad LibELF: Restore the relocation code from git history
This is going to be very useful for implementing kernel modules.
We'll also need it for dynamic linking later on.
2019-11-28 20:53:02 +01:00
Andreas Kling
0adbacf59e Kernel: Demangle userspace ELF symbols in backtraces
Turns out we can use abi::__cxa_demangle() for this, and all we need to
provide is sprintf(), realloc() and free(), so this patch exposes them.

We now have fully demangled C++ backtraces :^)
2019-11-27 14:06:24 +01:00
Andreas Kling
49635e62fa LibELF: Move AK/ELF/ into Libraries/LibELF/
Let's arrange things like this instead. It didn't feel right for all of
the ELF handling code to live in AK.
2019-11-06 13:42:38 +01:00