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

[Bug]: bad system call using semget from libandroid-sysv-semaphore #20514

Open
knyipab opened this issue Jun 12, 2024 · 4 comments
Open

[Bug]: bad system call using semget from libandroid-sysv-semaphore #20514

knyipab opened this issue Jun 12, 2024 · 4 comments
Labels
bug report Something is not working properly inactive No activity in a certain period of time

Comments

@knyipab
Copy link
Contributor

knyipab commented Jun 12, 2024

Problem description

I am building a package (jack2) that uses SYSV semaphore (libandroid-sysv-semaphore). However, it prompts bad system call in runtime. I can reproduce the issue with a simple test example below. Not sure if this is dependent on Android version, Linux kernel, or vendor.

What steps will reproduce the bug?

Here is an example taken from qt6-qtbase configure file: https://github.com/qt/qtbase/blob/79e00b0b25765b85b112b601fee70184b1efe3c0/src/corelib/configure.cmake#L258-L272.

sysv_sem.cpp

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <fcntl.h>

int main(void)
{
    key_t unix_key = ftok("test", 'Q');
    semctl(semget(unix_key, 1, 0666 | IPC_CREAT | IPC_EXCL), 0, IPC_RMID, 0);
    return 0;
}

commands to run

g++ -o sysv_sem sysv_sem.cpp -landroid-sysv-semaphore --debug
./sysv_sem

And it prints bad system call.

Output from gdb

Program received signal SIGSYS, Bad system call.
#0  0x0000007ff32be1c4 in syscall ()
   from /apex/com.android.runtime/lib64/bionic/libc.so
#1  0x0000007ff6a479b0 in semget ()
   from /data/data/com.termux/files/usr/lib/libandroid-sysv-semaphore.so
#2  0x00000055555567bc in main () at sysv_sem.cpp:9

What is the expected behavior?

No response

System information

Termux Variables:
TERMUX_API_VERSION=0.50.1
TERMUX_APK_RELEASE=GITHUB
TERMUX_APP_PACKAGE_MANAGER=apt
TERMUX_APP_PID=10415
TERMUX_IS_DEBUGGABLE_BUILD=1
TERMUX_MAIN_PACKAGE_FORMAT=debian
TERMUX_VERSION=0.118.0
TERMUX__USER_ID=0
Packages CPU architecture:
aarch64
Subscribed repositories:
# sources.list
deb https://mirrors.tuna.tsinghua.edu.cn/termux/apt/termux-main stable main
# x11-repo (sources.list.d/x11.list)
deb https://mirrors.tuna.tsinghua.edu.cn/termux/apt/termux-x11 x11 main
# tur-repo (sources.list.d/tur.list)
deb https://tur.kcubeterm.com tur-packages tur tur-on-device tur-continuous tur-multilib
Updatable packages:
clang/stable 18.1.7-1 aarch64 [upgradable from: 18.1.7]
code-oss/tur-packages 1.90.0 aarch64 [upgradable from: 1.89.1]
command-not-found/stable 2.4.0-28 aarch64 [upgradable from: 2.4.0-27]
darktable/tur-packages 4.6.1-1 aarch64 [upgradable from: 4.6.1]
debianutils/stable 5.19 aarch64 [upgradable from: 5.18]
glib-bin/stable 2.80.3 aarch64 [upgradable from: 2.80.2]
glib/stable 2.80.3 aarch64 [upgradable from: 2.80.2]
libcompiler-rt/stable 18.1.7-1 aarch64 [upgradable from: 18.1.7]
libllvm/stable 18.1.7-1 aarch64 [upgradable from: 18.1.7]
libopenmpt/stable 0.7.8 aarch64 [upgradable from: 0.7.7]
lld/stable 18.1.7-1 aarch64 [upgradable from: 18.1.7]
llvm/stable 18.1.7-1 aarch64 [upgradable from: 18.1.7]
mpv/stable 0.38.0 aarch64 [upgradable from: 0.38.0]
pango/stable 1.54.0 aarch64 [upgradable from: 1.52.2]
pipewire/stable 1.1.82-1 aarch64 [upgradable from: 1.1.82-1]
python-ensurepip-wheels/stable 3.11.9-2 all [upgradable from: 3.11.9-1]
python-fitsio/tur-packages 1.2.2 aarch64 [upgradable from: 1.2.1]
python-scipy/tur-packages 1:1.13.1-1 aarch64 [upgradable from: 1:1.13.1]
python/stable 3.11.9-2 aarch64 [upgradable from: 3.11.9-1]
termux-tools/stable 1.42.4 all [upgradable from: 1.42.3]
xfce4-session/x11 4.18.4 aarch64 [upgradable from: 4.18.3-1]
xfce4-settings/x11 4.18.5 aarch64 [upgradable from: 4.18.4]
termux-tools version:
1.42.3
Android version:
14
Kernel build information:
Linux localhost 5.15.123-android13-8-28577358-abF9460ZSS2CXD1 #1 SMP PREEMPT Mon Apr 1 02:09:15 UTC 2024 aarch64 Android
Device manufacturer:
samsung
Device model:
SM-F9460
LD Variables:
LD_LIBRARY_PATH=
LD_PRELOAD=/data/data/com.termux/files/usr/lib/libtermux-exec.so
Installed termux plugins:
com.termux.widget versionCode:13
com.termux.x11 versionCode:14
com.termux.api versionCode:51
@knyipab knyipab added bug report Something is not working properly untriaged labels Jun 12, 2024
@knyipab knyipab changed the title [Bug]: bad system call in libandroid-sysv-semaphore [Bug]: bad system call using semget from libandroid-sysv-semaphore Jun 12, 2024
@Biswa96
Copy link
Member

Biswa96 commented Jun 12, 2024

I can reproduce the issue in my Android device (Linux 4.19.312 Android 14). Here is the strace output.

SIGSYS {si_signo=SIGSYS, si_code=SYS_SECCOMP, si_call_addr=0x7397c5a9d0, si_syscall=__NR_semget, si_arch=AUDIT_ARCH_AARCH64}

@sylirre sylirre removed the untriaged label Jun 12, 2024
@sylirre
Copy link
Member

sylirre commented Jun 12, 2024

This library doesn't actually implements system call replacement stubs.

int semget(key_t key, int n, int flags) {                                       
#if defined(SYS_semget)                                                         
  return syscall(SYS_semget, key, n, flags);                                    
#else                                                                           
  return syscall(SYS_ipc, SEMGET, key, n, flags, 0, 0);                         
#endif                                                                          
}

If semget isn't allowed by seccomp on Android 14, then crash could be expected with current implementation.

@licy183
Copy link
Member

licy183 commented Jun 15, 2024

semget-related syscalls are disabled since Android 8.0. A related issue has been reported in #17780.

I could implement them using libandroid-shmem if I have more free time, but I'm currently doing an internship...

@knyipab
Copy link
Contributor Author

knyipab commented Jun 15, 2024

For jack2, I ended up using posix semaphore and submitted a PR in #20544.

For qt6-qtbase, it specifies libandroid-sysv-semaphore as deps. But I tested both QSemaphore and QSystemSemaphore and they all work fine. I doubt if sysv semaphore is indeed used as the backend for qt6.

It is still good to have a fix, perhaps for the sake of other depending packages or for termux users who compiled depending programs themselves. Appreciated and no pressure.

@stale stale bot added the inactive No activity in a certain period of time label Aug 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug report Something is not working properly inactive No activity in a certain period of time
Projects
None yet
Development

No branches or pull requests

4 participants