gz / rust-elfloader

Library to load and relocate ELF files.
https://docs.rs/elfloader
Apache License 2.0
117 stars 23 forks source link

Panic while trying to parse header #39

Open terrarier2111 opened 1 year ago

terrarier2111 commented 1 year ago

When trying to create a new ElfFile via ElfFile::new a panic occurs: Bildschirmfoto vom 2023-01-27 10-46-55

gz commented 1 year ago

are you able to share the binary you're trying to load? Or minimal source example+compiler (version)+linker (version) that produces the binary?

terrarier2111 commented 1 year ago

this is the file renamed to .txt for upload but normally it has no file extension at all: os-test.txt

landhb commented 1 year ago

I tried reproducing with the file above and it seems to work properly:

2023-04-17T17:17:01.168Z INFO  [repro] allocate base = 0x0 size = 0x219 flags =   R
2023-04-17T17:17:01.168Z INFO  [repro] allocate base = 0x1220 size = 0x22 flags = X R
2023-04-17T17:17:01.168Z INFO  [repro] allocate base = 0x2248 size = 0xa0 flags =  WR
2023-04-17T17:17:01.168Z INFO  [repro] load region into = 0x10000000 -- 0x10000219
2023-04-17T17:17:01.168Z INFO  [repro] load region into = 0x10001220 -- 0x10001242
2023-04-17T17:17:01.168Z INFO  [repro] load region into = 0x10002248 -- 0x100022e8

File loaded:

1a07577bc6bf37b0318dcf4919c35dda151c18c7 os-test.txt

Current crate versions:

elfloader v0.16.0 (rust-elfloader)
├── bitflags v1.3.2
├── log v0.4.14
│   └── cfg-if v1.0.0
└── xmas-elf v0.8.0
    └── zero v0.1.2
ghost commented 1 year ago

I did run into the same issue when using include_bytes! to include the elf. when using e.g. fs::read it seems the resulting vec buffer is aligned to 0x10 so the alignment issue in zero doesnt happen. From a quick test

include_bytes'd elf @ 0x55d1ffa31a96
fs::read'd elf @ 0x7ff5c89b1010

Im not sure if this is even a bug but the alignment requirement of the slice should probably be documented if it isn't already and i just missed it

landhb commented 1 year ago

@maddiethecafebabe Thanks for the explanation. I created a PR to provide a proper error instead of panicking https://github.com/gz/rust-elfloader/pull/41.