Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to execute statically linked binaries #37

Closed
Tracked by #45
imlk0 opened this issue Jul 14, 2021 · 2 comments
Closed
Tracked by #45

Failed to execute statically linked binaries #37

imlk0 opened this issue Jul 14, 2021 · 2 comments
Assignees
Labels
Milestone

Comments

@imlk0
Copy link
Collaborator

imlk0 commented Jul 14, 2021

A minimal test case:

echo 'int main(){return 0;}' | gcc -static -o rootfs/bin/test_static_linked -x c -
./target/debug/proot-rs --rootfs=./rootfs -- /bin/test_static_linked

output:

image

@imlk0 imlk0 added the bug label Jul 14, 2021
@imlk0 imlk0 added this to the v0.0.1 milestone Jul 14, 2021
@imlk0 imlk0 self-assigned this Jul 14, 2021
@imlk0
Copy link
Collaborator Author

imlk0 commented Jul 22, 2021

Document the steps to solve this problem:

  • First, in the source code of proot-rs, runing a executable without INTERP segment is not allowed currently. We need to remove some code snippet

    image

  • Now, we are able to execute a statically linked elf file. But in some case, we will got a SIGSEGV in loader.c:

    RUST_LOG=trace cargo run -- --rootfs=./rootfs -- /bin/test_execve_static 

    image

    It was caused by a mmap() syscall, when the loader are mapping a PT_LOAD segment. In this case. the memory area being mapped was 0x401000 -- (0x401000+0x81000) , which is the second PT_LOAD segment of the executing elf file:

    image

    By analysis the dumped core file, I found that the address of the current instruction (the value of rip register) is 0x401219, which is precisely within the mapping memory area.

    Then, I have a look at the program headers of our loader, and found that the PT_LOAD segment is conflict with the one in the executing elf file. That is why the issue occured.

    image

  • The original proot seems to be aware of this bug, and solve this by change the address of .text section. https://github.com/proot-me/proot/blob/a4db64532f4e8267bd37bcdc929daa4817f36ef5/src/GNUmakefile#L249

  • Therefore, to fix this, we can use the -Wl,-Ttext=0xaddress option to set the address of the .text section when we compile the loader. This involves a change to build_loader.rs.

@imlk0
Copy link
Collaborator Author

imlk0 commented Jul 27, 2021

Fixed in commit dcfd709

@imlk0 imlk0 closed this as completed Jul 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant