From 3b857ea3fe50a380ccda268f3b281bb92ea7a874 Mon Sep 17 00:00:00 2001 From: Fredrik Fornwall Date: Mon, 16 Sep 2024 23:17:40 +0200 Subject: [PATCH] Hook readlink(3) of /self/proc/exe --- Makefile | 2 +- src/termux-readlink.c | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 src/termux-readlink.c diff --git a/Makefile b/Makefile index e1357c4..3fe778b 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC ?= clang TERMUX_BASE_DIR ?= /data/data/com.termux/files CFLAGS += -Wall -Wextra -Werror -Wshadow -fvisibility=hidden -std=c17 -C_SOURCE := src/termux-exec.c src/exec-variants.c +C_SOURCE := src/termux-exec.c src/exec-variants.c src/termux-readlink.c CLANG_FORMAT := clang-format --sort-includes --style="{ColumnLimit: 120}" $(C_SOURCE) tests/fexecve.c tests/system-uname.c tests/print-argv0.c tests/popen.c CLANG_TIDY ?= clang-tidy diff --git a/src/termux-readlink.c b/src/termux-readlink.c new file mode 100644 index 0000000..e0c8957 --- /dev/null +++ b/src/termux-readlink.c @@ -0,0 +1,21 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include + +__attribute__((visibility("default"))) ssize_t readlink(const char *restrict pathname, char *restrict buf, size_t bufsiz) { + if (strcmp(pathname, "/proc/self/exe") == 0) { + const char* termux_self_exe = getenv("TERMUX_EXEC__PROC_SELF_EXE"); + if (termux_self_exe) { + size_t termux_self_exe_len = strlen(termux_self_exe); + size_t bytes_to_copy = (termux_self_exe_len < bufsiz) ? termux_self_exe_len : bufsiz; + memcpy(buf, termux_self_exe, bytes_to_copy); + return bytes_to_copy; + } + } + + return syscall(SYS_readlinkat, AT_FDCWD, pathname, buf, bufsiz); +} +