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

Feature request: Support for binfmt-misc F flag #1361

Open
boltronics opened this issue Mar 15, 2024 · 1 comment
Open

Feature request: Support for binfmt-misc F flag #1361

boltronics opened this issue Mar 15, 2024 · 1 comment

Comments

@boltronics
Copy link

We now have instructions for building a "highly experimental" static binary for use in a chroot, similar to what qemu can provide.

In the case of deploying chroots using debootstrap, Debian provides a tool called qemu-debootstrap to automatically perform the multi-step process of building these for foreign architectures. I believe this would previously embed the static qemu binary into the chroot half way through the build process. The tool is currently deprecated because qemu now supports the binfmt F flag and deploys the binfmt config using this by default, so a qemu binary in the chroot is no longer required.

It would be nice if box64 could also work this way, so it could function like a drop-in replacement, and require fewer steps for these kinds of tasks.

@ElDavoo
Copy link

ElDavoo commented May 25, 2024

I've tried inserting the F flag in the /etc/binfmt.d/box64.conf file:
sudo sh -c 'truncate -s -2 /etc/binfmt.d/box64.conf && echo F >> /etc/binfmt.d/box64.conf' && sudo systemctl restart systemd-binfmt
Fancy oneliners aside, this is the new file contents:

# /etc/binfmt.d/box64.conf
:box64:M::\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x3e\x00:\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/local/bin/box64:F

After restart systemd-binfmt, box64 works, but in a limited way:

pi@host:~ $  DOCKER_DEFAULT_PLATFORM=amd64 docker run --rm -it -v /usr/lib/x86_64-linux-gnu/:/x86_64/ -e BOX64_LD_LIBRARY_PATH=/x86_64/ debian
Dynarec for ARM64, with extension: ASIMD CRC32 PageSize:4096 Running on Unknown CPU with 4 Cores
Will use Hardware counter measured at 54.0 MHz emulating 3.4 GHz
Params database has 14 entries
Box64 with Dynarec v0.2.9 5ae93f84 built on May 23 2024 16:45:40
BOX64: Didn't detect 48bits of address space, considering it's 39bits
Warning, older then 2.34 pthread_kill not found, using current one
Counted 6 Env var
BOX64 LIB PATH: /x86_64/:/lib/x86_64-linux-gnu/:/usr/lib/x86_64-linux-gnu/
BOX64 BIN PATH: ./:bin/:/usr/local/sbin/:/usr/local/bin/:/usr/sbin/:/usr/bin/:/sbin/:/bin/
Looking for /usr/bin/bash
bash detected, disabling banner
Apply RC params for bash
root@f1586a1dce3c:/# ls
bash: /usr/bin/ls: cannot execute: required file not found
root@f1586a1dce3c:/# pwd
/
root@f1586a1dce3c:/# exit
exit
pi@host:~ $ 

Can't really get what file is it talking about. Ubuntu crashes because of #1531 , while alpine just crashes:

pi@host:~ $ DOCKER_DEFAULT_PLATFORM=amd64 docker run --rm -it -v /usr/lib/x86_64-linux-gnu/:/x86_64/ -e BOX64_PREFER_EMULATED=1 -e BOX64_DYNAREC=0 -e BOX64_LOG=2 -e BOX64_LD_LIBRARY_PATH=/x86_64/ alpine
Debug level is 2
Dynarec is off
 PageSize:4096 Running on Unknown CPU with 4 Cores
Will use Hardware counter measured at 54.0 MHz emulating 3.4 GHz
Params database has 14 entries
Box64 with Dynarec v0.2.9 5ae93f84 built on May 23 2024 16:45:40
BOX64: Didn't detect 48bits of address space, considering it's 39bits
Setting up canary (for Stack protector) at FS:0x28, value:91314800
Warning, older then 2.34 pthread_kill not found, using current one
BOX64: Prefering Emulated libs
Counted 6 Env var
 Env[00]: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
 Env[01]: HOSTNAME=435f9beff160
 Env[02]: TERM=xterm
 Env[03]: LD_LIBRARY_PATH=/x86_64/
 Env[04]: HOME=/root
 Env[05]: BOX64_PATH=.:bin
BOX64 LIB PATH: /x86_64/
BOX64 BIN PATH: ./:bin/:/usr/local/sbin/:/usr/local/bin/:/usr/sbin/:/usr/bin/:/sbin/:/bin/
Looking for /bin/sh
Read 24 Section header
Read 12 Program header
Loading Sections Table String (idx = 23)
Loading SymTab Strings (idx = 0)
Loading SymTab (idx = 0)
Loading Dynamic (idx = 19)
The DT_INIT is at address 0x6000
The DT_FINI is at address 0x9f19a
The DT_INIT_ARRAY is at address 0xc2830
The DT_INIT_ARRAYSZ is 1
The DT_FINI_ARRAY is at address 0xc2838
The DT_FINI_ARRAYSZ is 1
The DT_GNU_HASH is at address 0x358
The DT_FLAGS is 0x8
RelA Table @0x3650 (0x2418/0x18)
PLT Table @0x5a68 (type=7 0x18/0x18)
The GOT Table is at address 0xc5410..0xc6000
The PLT Table is at address 0x6010..0x6030
The .text is at address 0x6030, and is 627050 big
The .eh_frame section is at address 0xc10b8..0xc10bc
Loading DynSym Strings (idx = 6)
Loading DynSym (idx = 5)
Adding "/bin/busybox" as #0 in elf collection
Elf Addr(v/p)=(nil)/(nil) Memsize=0xc6b58 (align=0x1000)
Elf Stack Memsize=1048576 (align=16)
Elf TLS Memsize=0 (align=0)
Pre-allocated 0xc6b58 byte at 0x100000000 for /bin/busybox
Delta of 0x100000000 (vaddr=(nil)) for Elf "/bin/busybox"
Mmaping 0x5b48(0x6000) bytes @0x100000000 for Elf "/bin/busybox"
Mmaping 0x9919d(0x9a000) bytes @0x100006000 for Elf "/bin/busybox"
Mmaping 0x210bc(0x22000) bytes @0x1000a0000 for Elf "/bin/busybox"
Allocating 0x6000 (0x1000c2000/0x4328) bytes @0x1000c2000, will read 0x3800 @0x1000c2830 for Elf "/bin/busybox"
Rename process to "busybox"
Calc stack size, based on 1 elf(s)
Stack is @0x7f9e051000 size=0x800000 align=0x10
Allocate a new X86_64 Emu, with EIP=(nil) and Stack=0x7f9e051000/0x800000
Setup X86_64 Emu
Grabbing R_X86_64_COPY Relocation(s) in advance for /bin/busybox
Trying to add "libc.musl-x86_64.so.1" to maplib
Trying to load "libc.musl-x86_64.so.1"
Simplified name is "libc.musl-x86_64.so.1"
Using native(wrapped) libc.musl-x86_64.so.1
Trying to add "libc.so.6" to maplib
Trying to load "libc.so.6"
Simplified name is "libc.so.6"
Using native(wrapped) libc.so.6
Trying to add "ld-linux-x86-64.so.2" to maplib
Trying to load "ld-linux-x86-64.so.2"
Simplified name is "ld-linux-x86-64.so.2"
Using native(wrapped) ld-linux-x86-64.so.2
Trying to add "libpthread.so.0" to maplib
Trying to load "libpthread.so.0"
Simplified name is "libpthread.so.0"
Using native(wrapped) libpthread.so.0
Trying to add "libdl.so.2" to maplib
Trying to load "libdl.so.2"
Simplified name is "libdl.so.2"
Using native(wrapped) libdl.so.2
Trying to add "libutil.so.1" to maplib
Trying to load "libutil.so.1"
Simplified name is "libutil.so.1"
Using native(wrapped) libutil.so.1
Trying to add "librt.so.1" to maplib
Trying to load "librt.so.1"
Simplified name is "librt.so.1"
Using native(wrapped) librt.so.1
Trying to add "libbsd.so.0" to maplib
Trying to load "libbsd.so.0"
Simplified name is "libbsd.so.0"
Using native(wrapped) libbsd.so.0
Created lib and added to maplib => success
Created lib and added to maplib => success
Created lib and added to maplib => success
Created lib and added to maplib => success
Created lib and added to maplib => success
Created lib and added to maplib => success
Trying to add "libdl.so.2" to maplib
Already present in maplib => success
Trying to add "libm.so.6" to maplib
Trying to load "libm.so.6"
Simplified name is "libm.so.6"
Using native(wrapped) libm.so.6
Trying to add "libpthread.so.0" to maplib
Already present in maplib => success
Created lib and added to maplib => success
Created lib and added to maplib => success
Created lib and added to maplib => success
Created lib and added to maplib => success
Created lib and added to maplib => success
And now export symbols / relocation for /bin/busybox...
Forcing /bin/busybox to Bind Now
Applying 385 Relocation(s) with Addend for /bin/busybox bindnow=1, deepbind=0
Error: Global Symbol ns_initparse not found, cannot apply R_X86_64_GLOB_DAT @0x1000c5548 ((nil)) in /bin/busybox
Error: Global Symbol res_mkquery not found, cannot apply R_X86_64_GLOB_DAT @0x1000c5648 ((nil)) in /bin/busybox
Error: Global Symbol ns_name_uncompress not found, cannot apply R_X86_64_GLOB_DAT @0x1000c5658 ((nil)) in /bin/busybox
Error: Global Symbol pivot_root not found, cannot apply R_X86_64_GLOB_DAT @0x1000c5740 ((nil)) in /bin/busybox
Error: Global Symbol ns_parserr not found, cannot apply R_X86_64_GLOB_DAT @0x1000c5790 ((nil)) in /bin/busybox
Error: Global Symbol ns_get32 not found, cannot apply R_X86_64_GLOB_DAT @0x1000c57c8 ((nil)) in /bin/busybox
Error: Global Symbol ether_hostton not found, cannot apply R_X86_64_GLOB_DAT @0x1000c5860 ((nil)) in /bin/busybox
Error: Global Symbol swapoff not found, cannot apply R_X86_64_GLOB_DAT @0x1000c59d0 ((nil)) in /bin/busybox
Error: Global Symbol crypt not found, cannot apply R_X86_64_GLOB_DAT @0x1000c5b08 ((nil)) in /bin/busybox
Error: Global Symbol swapon not found, cannot apply R_X86_64_GLOB_DAT @0x1000c5cd0 ((nil)) in /bin/busybox
Error: Global Symbol ether_aton_r not found, cannot apply R_X86_64_GLOB_DAT @0x1000c5d10 ((nil)) in /bin/busybox
Error: Global Symbol scanf not found, cannot apply R_X86_64_GLOB_DAT @0x1000c5d38 ((nil)) in /bin/busybox
Error: Global Symbol ns_get16 not found, cannot apply R_X86_64_GLOB_DAT @0x1000c5e18 ((nil)) in /bin/busybox
Forcing /bin/busybox to Bind Now
Applying 1 PLT Relocation(s) with Addend for /bin/busybox bindnow=1, deepbind=0
Entry Point is 0x100006ed1
Start x64emu on Main
Run X86 (0x5ede9a60), RIP=0x100006ed1, Stack=0x7f9e850e00 is32bits=0
0001|0x100006ee7: Calling __libc_start_main(0x100007502, 0x1, 0x7F9E850E08, ...) =>Calling init(0x100006000) from __libc_start_main
Run X86 (0x5ede9a60), RIP=0x100006000, Stack=0x7f9e850de8 is32bits=0
End of X86 run (0x5ede9a60), RIP=0x1008b, Stack=0x7f9e850df0, unimp=0, emu->fork=0, emu->uc_link=(nil), emu->quit=1
Transfert to main(1, 0x7f9e850e08, 0x7f9e850e18)=>0x100007502 from __libc_start_main
Run X86 (0x5ede9a60), RIP=0x100007502, Stack=0x7f9e850de8 is32bits=0
0001|0x100007095: Calling __errno_location(0x1000A336F, 0x7F9E850E08, 0x7F9E850E18, ...) => return 0x5EDE6908
0001|0x100007512: Calling setlocale(0x6, 0x1000B2F2C, 0x7F9E850E18, ...) => return 0x34D64F28
0001|0x1000076e8: Calling strrchr(0x7F9E850F50, 0x2F, 0x7F9E850E18, ...) => return 0x7F9E850F54
0001|0x100007544: Calling getuid(0x7F9E850F50, 0x2F, 0x7F9E850E18, ...) => return 0x0
0001|0x100006f53: Calling strcmp("sh", "tty") => return 0xFFFFFFFFFFFFFFFF
0001|0x100006f53: Calling strcmp("sh", "sh") => return 0x0
0001|SIGSEGV @0x348652e8 (???(0x348652e8)) (x64pc=0x4c8d3/???:"???", rsp=0x7f9e850d68, stack=0x7f9e051000:0x7f9e851000 own=(nil) fp=0x7f9e850e08), for accessing 0x4c8d3 (code=1/prot=0), db=(nil)((nil):(nil)/(nil):(nil)/???:clean, hash:0/0) handler=(nil)
RSP-0x20:0x0000000000000000 RSP-0x18:0x0000000000000000 RSP-0x10:0x0000000100006f53 RSP-0x08:0x0000007f9e850f55
RSP+0x00:0x00000001000071bd RSP+0x08:0x000000009e850f55 RSP+0x10:0x0000007f9e850f55 RSP+0x18:0x0000007f9e850e08
RAX:0x00000001000c4870 RCX:0x0000000100006000 RDX:0x0000007f9e850e08 RBX:0x00000000000000e4 
RSP:0x0000007f9e850d68 RBP:0x0000007f9e850e08 RSI:0x0000007f9e850e08 RDI:0x0000000000000001 
 R8:0x000000010009f19a  R9:0x0000000000000000 R10:0x0000000000000000 R11:0x0000000000000000 
R12:0x0000000000000000 R13:0x0000000000000000 R14:0x0000000000000000 R15:0x0000000000000000 
ES:0x002b CS:0x0033 SS:0x002b DS:0x002b FS:0x0043 GS:0x0053 

So if we understand how to fix the "things not found" problem then box64 would be able to work (normal incompatibilities apart) as a drop-in for qus and that means fast amd64 containers on aarch64 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants