termux / proot

An chroot-like implementation using ptrace.
https://wiki.termux.com/wiki/PRoot
Other
774 stars 160 forks source link

Proot error #292

Closed bishalzkaboom closed 9 months ago

bishalzkaboom commented 9 months ago

proot warning: can't chdir("/data/data/com.termux/files/home/./.") in the guest rootfs: No such file or directory proot info: default working directory is now "/" proot error: execve("/bin/sh"): No such file or directory proot info: possible causes:

sylirre commented 9 months ago

Provide info on how you set up the proot/chroot environment. From what is seen in the error message it looks like either your distribution messed up or your proot command is wrong.

bishalzkaboom commented 9 months ago

I am getting this error from proot binary file when i do ./proot -r alpine it gives me this error, i am a android developer and i wanna implement proot in my app so i was just testing some things in termux by installing a proot binary and executing it executes normally when i install proot in termux through pkg manager but when i do pkg uninstall proot and run same binary it gives me thus error i have setuped the libs also and i have unset the lib-termux-exec also can you tell me how can i make that work in my app and can you tell me what thigs us needed to setup proot and what is done in termux while installing proot

bishalzkaboom commented 9 months ago

The proot binary can be found here : https://github.com/pelya/debian-noroot/tree/master/proot-prebuilt/arm64-v8a

bishalzkaboom commented 9 months ago

Complete log:

~ $ ./proot -r alpine proot warning: can't chdir("/data/data/com.termux/files/home/./.") in the guest rootfs: No such file or directory proot info: default working directory is now "/" proot error: execve("/bin/sh"): No such file or directory proot info: possible causes:

sylirre commented 9 months ago

proot -r alpine

Either change working directory in proot or bind it.

proot -w / -r alpine

proot -b $HOME -r alpine

Make sure you have extracted alpine rootfs correctly and unset LD_PRELOAD.

This however isn't enough to get a properly working environment.


Missing configured resolv.conf doesn't let to install packages.

Screenshot_20240107-120633

Also it is highly recommended to bind at least /dev and /proc. Without dev shells will not be properly attached to terminal when using su/sudo. The missing proc will prevent programs from gathering system information as well as will disable process management: ps, kill, and related utilities will not work.

However even if we did all of this, something still not working properly. Android normally disallows hard links and we see permission denials.

Screenshot_20240107-120751

Fixing this:

Screenshot_20240107-121853

Now that would be your minimal setup.

All commands (assuming LD_PRELOAD is not set):

curl -LO https://dl-cdn.alpinelinux.org/alpine/v3.19/releases/aarch64/alpine-minirootfs-3.19.0-aarch64.tar.gz
mkdir alpine
tar -C alpine -xf alpine-minirootfs-3.19.0-aarch64.tar.gz
echo "nameserver 1.1.1.1" > ./alpine/etc/resolv.conf
proot -w / -b /dev -b /proc --link2symlink -0 -r alpine
bishalzkaboom commented 9 months ago

Same Error:

~/tmp $ curl -LO https://dl-cdn.alpinelinux.org/alpine/v3.19/releases/aarch64/alpine-minirootfs-3.19.0-aarch64.tar.gz % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 - 0 0 0 0 0 0 0 0 - 0 3143k 0 9693 0 0 6190 0 6 3143k 6 198k 0 0 80400 0 26 3143k 26 844k 0 0 223k 0 35 3143k 35 1127k 0 0 239k 0 50 3143k 50 1602k 0 0 242k 0 55 3143k 55 1730k 0 0 223k 0 59 3143k 59 1858k 0 0 218k 0 63 3143k 63 2002k 0 0 209k 0 67 3143k 67 2106k 0 0 198k 0 69 3143k 69 2176k 0 0 188k 0 71 3143k 71 2251k 0 0 179k 0 73 3143k 73 2319k 0 0 171k 0 75 3143k 75 2371k 0 0 163k 0 77 3143k 77 2427k 0 0 155k 0 78 3143k 78 2464k 0 0 148k 0 79 3143k 79 2507k 0 0 142k 0 80 3143k 80 2536k 0 0 136k 0 81 3143k 81 2566k 0 0 131k 0 83 3143k 83 2610k 0 0 127k 0 84 3143k 84 2651k 0 0 121k 0 85 3143k 85 2682k 0 0 118k 0 86 3143k 86 2711k 0 0 115k 0 87 3143k 87 2746k 0 0 111k 0 88 3143k 88 2782k 0 0 108k 0 90 3143k 90 2834k 0 0 106k 0 91 3143k 91 2885k 0 0 104k 0 94 3143k 94 2983k 0 0 104k 0 96 3143k 96 3044k 0 0 102k 0 98 3143k 98 3095k 0 0 100k 0 99 3143k 99 3124k 0 0 98k 0 100 3143k 100 3143k 0 0 98k 0 0:00:31 0:00:31 --:--:-- 62210 ~/tmp $ mkdir alpine ~/tmp $ tar -C alpine -xf alpine-minirootfs-3.19.0-aarch64.tar.gz ~/tmp $ rm alpine-minirootfs-3.19.0-aarch64.tar.gz ~/tmp $ echo "nameserver 1.1.1.1" > ./alpine/etc/resolv.conf ~/tmp $ proot -w / -b /dev -b /proc --link2symlink -0 -r alpine The program proot is not installed. Install it by executing: pkg install proot ~/tmp $ ./proot -w / -b /dev -b /proc --link2symlink -0 -r alpine proot error: execve("/bin/sh"): No such file or directory proot info: possible causes:

sylirre commented 9 months ago

Use unset LD_PRELOAD before running proot.

bishalzkaboom commented 9 months ago

Welcome to Termux!

Docs: https://termux.dev/docs Donate: https://termux.dev/donate Community: https://termux.dev/community

Working with packages:

Subscribing to additional repositories:

For fixing any repository issues, try 'termux-change-repo' command.

Report issues at https://termux.dev/issues ~ $ ls alpine storage tmp ~ $ cd tmp ~/tmp $ ls alpine proot ~/tmp $ unset LD_PRELOAD ~/tmp $ ./proot -w / -b /dev -b /proc --link2symlink -0 -r alpine proot error: execve("/bin/sh"): No such file or directory proot info: possible causes:

bishalzkaboom commented 9 months ago

I am using a proot binary i gave given its link too it works when i do pkg install proot and if i delete the termux's proot in the $PREFIX and the libs and add my own but it don't work if i do pkg uninstall proot i think there is some code running in pkg install proot or there are some libs that are installed but not showed in the log can you tell me what is done?

sylirre commented 8 months ago

@bishalzkaboom Pelya's proot shouldn't be used. Termux has a reason to keep own copy of the proot sources - lots of fixed added on top as well as it has more features.

Don't report issues about custom software as we're not going to spend time investigate impossible bugs. That should be obvious.

There no additional code running when you install a package. Instead you getting a binary compiled from a patched source code.

You may see history of changes here: https://github.com/termux/proot/commits/master/

bishalzkaboom commented 8 months ago

Wait for one second forget about pelya's proot, run fresh termux and install proot and rootfs and run proot -r alpine for eg it will work fine but if you copy the proot bin from the prefix and the libs from prefix and do pkg uninstall proot and add them back it will not work now

sylirre commented 8 months ago

Termux packages aren't supposed to be used this way. We don't provide standalone portable binaries.

If you look inside the deb package or proot, you will see that it has two components: user executable (proot) and loader. There also another non-obvious thing: the proot is dependent on libtalloc shared library.

Screenshot_20240108-152035

So in order to make usable in your custom app it has to be recompiled.

bishalzkaboom commented 8 months ago

I wanna implement the proot in my android app what did i need to consider like i don't wan't whole terminal i will run the code with process builder and i just wanna establish a ssh connection to the linux runned by the proot like the app called userland how can i do so and what did i need to do?

bishalzkaboom commented 8 months ago

Thanks dude for your information i figured it out when i uninstall the proot the proot loaders is been deleted from the libexec folder and installed after i do install proot via pkg and i just wanna know how to re-compile the proot for custom package name or for custom libexec path and can you tell me what the share folder does cause the proot can run without it

sylirre commented 8 months ago

Here is how you can build it in Termux:

yes | pkg upgrade
pkg in build-essential libtalloc-static git binutils-is-llvm ndk-multilib ndk-multilib-native-static termux-elf-cleaner
git clone https://github.com/termux/proot
cd proot/src
make LDFLAGS="-static -ltalloc"
termux-elf-cleaner ./proot

Copy resulting proot binary. Yes, here proot won't require any external files.

bishalzkaboom commented 8 months ago

It will create a proot bin and it don't require other files like libtalloc and loders?

sylirre commented 8 months ago

If you will use commands above, you should get a fully standalone binary.

bishalzkaboom commented 8 months ago

Thanks a lot💗

bishalzkaboom commented 8 months ago

Hey i know the the issue is fixed and it is a bit offtopic but can you tell me one thing i have made a terminal emulator with modifications in neotty app with the termux JNI in it by building it with my package i have the command to be /bin/sh and run the terminal by replacing it from the Qemu but when i try to access /sdcard it tells permission denied and i have a binqry file i have kept it in my data directory and i have done chmod +x * but also it is telling permission denied how can this issues be fix and the binary i am using is proot that i have compiled, but the issue persists with all the binary's

sylirre commented 8 months ago

You can execute files only on internal storage (/data/data/your.package.name/) and only if your app target SDK version is 28 or lower.

JNI lets you to load native code as part of application but it is not a workaround for execute permission issue. You still won't be able to run executables from data directory on internal storage if SDK >29.

bishalzkaboom commented 8 months ago

No no i have the executables in /data/data//files/bin Then also chmod not working,

And the other issue is that i cannot even get the files of /sdcard or /storage/emulated/0/ in short

bishalzkaboom commented 8 months ago

Log : :/data/data/geq.kaboom.terminal/files $ ls bin :/data/data/geq.kaboom.terminal/files $ cd bin :/data/data/geq.kaboom.terminal/files/bin $ ls proot :/data/data/geq.kaboom.terminal/files/bin $ ./proot /bin/sh: ./proot: Permission denied 1|:/data/data/geq.kaboom.terminal/files/bin $ chmod +x proot :/data/data/geq.kaboom.terminal/files/bin $ ls proot :/data/data/geq.kaboom.terminal/files/bin $ ./proot /bin/sh: ./proot: Permission denied 1|:/data/data/geq.kaboom.terminal/files/bin $ 1|:/data/data/geq.kaboom.terminal/files/bin $ 1|:/data/data/geq.kaboom.terminal/files/bin $ 1|:/data/data/geq.kaboom.terminal/files/bin $ 1|:/data/data/geq.kaboom.terminal/files/bin $ ls /sdcard ls: /sdcard: Permission denied 1|:/data/data/geq.kaboom.terminal/files/bin $ ls /storage/emulated/0/ ls: /storage/emulated/0/: Permission denied 1|:/data/data/geq.kaboom.terminal/files/bin $

sylirre commented 8 months ago

Execute permission does not work if your app has target SDK level 29 or higher. So if your app has such SDK level configuration, the chmod won't have any effect.

Termux is built with target SDK level 28 which enables behavior of old Android and allows execution.

As for storage, you need to grant necessary permissions to your app first.

bishalzkaboom commented 8 months ago

There is not any other solution to use chmod? Cause i wanna make a terminal which uses proot to give me a debian terminal,

And for the storage part i have granted the permission of write and read external storage and gave the permission in the app too but also the command is not working

bishalzkaboom commented 8 months ago

Same error in max sdk 28 in android manifest

a/data/geq.kaboom.terminal/files $ ls bin :/data/data/geq.kaboom.terminal/files $ ls /sdcard ls: /sdcard: Permission denied 1|:/data/data/geq.kaboom.terminal/files $ ls bin :/data/data/geq.kaboom.terminal/files $ cd bin :/data/data/geq.kaboom.terminal/files/bin $ ls proot :/data/data/geq.kaboom.terminal/files/bin $ ./proot /bin/sh: ./proot: Permission denied 1|:/data/data/geq.kaboom.terminal/files/bin $ chmod +x proot :/data/data/geq.kaboom.terminal/files/bin $ ./proot /bin/sh: ./proot: Permission denied /proot < /bin/sh: ./proot: Permission denied 1|:/data/data/geq.kaboom.terminal/files/bin $ ./proot /bin/sh: ./proot: Permission denied 1|:/data/data/geq.kaboom.terminal/files/bin $ chmod +x * && ./proot /bin/sh: ./proot: Permission denied 1|:/data/data/geq.kaboom.terminal/files/bin $

twaik commented 8 months ago

@bishalzkaboom Actually it is possible to make proot work without bypassing W^X restriction. You can build proot as shared library compiled with Android NDK adding the following C++ code after main() code.

int main(int, char**, char**);
JNIEXPORT extern "C" void
__libc_init(void* r, void (*o)(void), int (*m)(int, char**, char**) __unused, void *s) {
    ((decltype(&__libc_init)) dlsym(RTLD_NEXT, "__libc_init"))(r, o, main, s);
}

I am not sure if proot is written in C or C++.

After this you will be able to start proot with the following commandline.

LD_PRELOAD=<path_to_apk>!/lib/<abi>/libproot.so /system/bin/sh

That is some kind of main() function hooking under bionic sauce.

There are a few ways getting path to your apk.

  1. Android APIs. PackageManager::getApplicationInfo("your.package.name", 0) should give you an answer where apk is contained.
  2. Tricky. You can exploit an ability of DexClassLoader to print path to apk when it is being converted to String. Log.i("PATH", MainActivity.class.getClassLoader().toString().split("zip file \"|\"")[1]);.
  3. JNI. You can get path of shared library using dladdr
    Dl_info info;
    dladdr((void*) JNI_OnLoad, &info);
    __android_log_print(ANDROID_LOG_DEBUG, "PATH",  "%s", info.dli_fname);

You can use dirname to extract directory and sprintf to concatenate it with proot library name.

twaik commented 8 months ago

can i get your any social media account

No.

i wanna get more information about it

I showed you the direction. For further researching you can use Google.

bishalzkaboom commented 8 months ago

@twaik while compiling the c we need proot src? Or where it needs to be ?

twaik commented 8 months ago

If you need to patch it to add __libc_init hook you must build it from sources. It does not really matter where sources are contained. I prefer using git submodules and patch sources before building. You can download sources as a tarball before building, use git clone or distribute sources with your program, whatever.

sylirre commented 8 months ago

Same error in max sdk 28

I didn't said anything about max sdk version and SDK nowadays is changed in different files.

You need to change target SDK version. Not max, not min, not SDK version to compile with.

https://github.com/termux/termux-app/blob/8e3a8980a849046adbd4156741e1d84047ee1df6/app/build.gradle#L43

Finally as said there are other solutions like provided by above or just renaming your binary from proot to proot.so and placing it into native library directory of APK file.

Anyway, hints are given and rest is on your own. We are not going to provide full example how to make custom terminal app.