Skip to content

Commit 9f701ad

Browse files
committed
Hook readlink(3) of /self/proc/exe
1 parent 7279c61 commit 9f701ad

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CC ?= clang
22
TERMUX_BASE_DIR ?= /data/data/com.termux/files
33
CFLAGS += -Wall -Wextra -Werror -Wshadow -fvisibility=hidden -std=c17
4-
C_SOURCE := src/termux-exec.c src/exec-variants.c
4+
C_SOURCE := src/termux-exec.c src/exec-variants.c src/termux-readlink.c
55
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
66
CLANG_TIDY ?= clang-tidy
77

src/termux-readlink.c

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#define _GNU_SOURCE
2+
#include <fcntl.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
#include <sys/syscall.h>
6+
#include <unistd.h>
7+
8+
__attribute__((visibility("default"))) ssize_t readlink(const char *restrict pathname, char *restrict buf,
9+
size_t bufsiz) {
10+
if (strcmp(pathname, "/proc/self/exe") == 0) {
11+
const char *termux_self_exe = getenv("TERMUX_EXEC__PROC_SELF_EXE");
12+
if (termux_self_exe) {
13+
size_t termux_self_exe_len = strlen(termux_self_exe);
14+
size_t bytes_to_copy = (termux_self_exe_len < bufsiz) ? termux_self_exe_len : bufsiz;
15+
memcpy(buf, termux_self_exe, bytes_to_copy);
16+
return bytes_to_copy;
17+
}
18+
}
19+
20+
return syscall(SYS_readlinkat, AT_FDCWD, pathname, buf, bufsiz);
21+
}

0 commit comments

Comments
 (0)