termux-packages icon indicating copy to clipboard operation
termux-packages copied to clipboard

[Bug]: chroot() function in clang and python

Open Moe-hacker opened this issue 2 years ago • 2 comments

Problem description

Cannot run any system commands in a container after chroot() function in C and Python. I have already tried to unset $LD_PRELOAD , but it doesn't work. Any way to use chroot() function to exec commands in a container or it's just a bug ?🧐 More info is in the following part.

What steps will reproduce the bug?

I'm a noob for programming , and I tried running the following C program in termux: (/data/alpine already contains an alpine rootfs)

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main(){
  system("unset LD_PRELOAD");
  chroot("/data/alpine");
  chdir("/");
  system("/bin/sh");
}

(I forgot to write return 0 but It's not important here) Nothing returnd after running the compiled program with sudo. And when I tried python (also with sudo):

from os import *
chroot("/data/alpine")
chdir("/")
system("/bin/sh")

It returned error code 32512 But the following commands can be run normally:

tsu
unset LD_PRELOAD
chroot /data/alpine /bin/sh

So how to fix the problem?Or it's a bug? Screenshots: IMG_20221128_214900 IMG_20221128_214849 IMG_20221128_214833

What is the expected behavior?

No response

System information

termux-info:

Termux Variables:
TERMUX_APP_PACKAGE_MANAGER=apt
TERMUX_APP__AM_SOCKET_SERVER_ENABLED=true
TERMUX_APP__APK_PATH=/data/app/~~QPyND5Mk48P_FqoMxi_gGw==/com.termux-5T-4pHA0Mlu2CTB1s7XxiQ==/base.apk
TERMUX_APP__APK_RELEASE=GITHUB
TERMUX_APP__FILES_DIR=/data/user/0/com.termux/files
TERMUX_APP__IS_DEBUGGABLE_BUILD=true
TERMUX_APP__IS_INSTALLED_ON_EXTERNAL_STORAGE=false
TERMUX_APP__PACKAGE_MANAGER=apt
TERMUX_APP__PACKAGE_NAME=com.termux
TERMUX_APP__PACKAGE_VARIANT=apt-android-7
TERMUX_APP__PID=31960
TERMUX_APP__SE_FILE_CONTEXT=u:object_r:app_data_file:s0:c243,c256,c512,c768
TERMUX_APP__SE_INFO=default:targetSdkVersion=28:complete
TERMUX_APP__SE_PROCESS_CONTEXT=u:r:untrusted_app_27:s0:c243,c256,c512,c768
TERMUX_APP__TARGET_SDK=28
TERMUX_APP__UID=10243
TERMUX_APP__USER_ID=0
TERMUX_APP__VERSION_CODE=118
TERMUX_APP__VERSION_NAME=0.118.0
TERMUX_MAIN_PACKAGE_FORMAT=debian
TERMUX_VERSION=0.118.0
Packages CPU architecture:
aarch64
Subscribed repositories:
# sources.list
deb https://mirrors.bfsu.edu.cn/termux/apt/termux-main stable main
# root-repo (sources.list.d/root.list)
deb https://mirrors.bfsu.edu.cn/termux/apt/termux-root root stable
Updatable packages:
containerd/root 1.6.10 aarch64 [upgradable from: 1.6.9]
docker/root 20.10.21 aarch64 [upgradable from: 20.10.16]
gawk/stable 5.2.1 aarch64 [upgradable from: 5.1.1-1]
libdevmapper/root 2.03.17 aarch64 [upgradable from: 2.03.16-1]
libmpfr/stable 4.1.1-2 aarch64 [upgradable from: 4.1.1-1]
libnewt/stable 0.52.22 aarch64 [upgradable from: 0.52.21]
ruby/stable 3.1.3 aarch64 [upgradable from: 3.1.2]
whiptail/stable 0.52.22 aarch64 [upgradable from: 0.52.21]
termux-tools version:
1.32.0
Android version:
12
Kernel build information:
Linux localhost 4.19.260-Moe-hacker-g0bb1c026ee65-dirty #3 SMP PREEMPT Sun Oct 2 10:48:46 CST 2022 aarch64 Android
Device manufacturer:
Xiaomi
Device model:
Mi 10 Ultra

Moe-hacker avatar Nov 28 '22 13:11 Moe-hacker

Are you trying to nest chroot commands? (Since it looks like # u0_a243 @ localhost is already in a chroot or proot)

Grimler91 avatar Nov 28 '22 14:11 Grimler91

This is because system(3) tries to execute /system/bin/sh, which does not exist in the chroot environment. You can instead use the exec(3) family.

xtkoba avatar Nov 28 '22 14:11 xtkoba

This is because system(3) tries to execute /system/bin/sh, which does not exist in the chroot environment. You can instead use the exec(3) family.

Thank you so much for your answer!execv() works in my program , and I'll close this issue because it's not a bug.😃

Moe-hacker avatar Nov 29 '22 03:11 Moe-hacker