Skip to content

Commit

Permalink
Hook readlink(3) of /self/proc/exe
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Sep 16, 2024
1 parent 7279c61 commit 9f701ad
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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

Expand Down
21 changes: 21 additions & 0 deletions src/termux-readlink.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <unistd.h>

__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);
}

0 comments on commit 9f701ad

Please sign in to comment.