Skip to content

Commit 1383155

Browse files
easyaspi314Grimler91
authored andcommitted
Unset LD_LIBRARY_PATH and LD_PRELOAD on /system/ executables.
1 parent 0cd0e39 commit 1383155

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

termux-exec.c

+35-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@
3030
# error "unknown arch"
3131
#endif
3232

33+
#define starts_with(value, str) !strncmp(value, str, sizeof(str) - 1)
34+
3335
static const char* termux_rewrite_executable(const char* filename, char* buffer, int buffer_len)
3436
{
37+
if (starts_with(filename, TERMUX_BASE_DIR) ||
38+
starts_with(filename, "/system/"))
39+
return filename;
40+
3541
strcpy(buffer, TERMUX_PREFIX "/bin/");
3642
char* bin_match = strstr(filename, "/bin/");
3743
if (bin_match == filename || bin_match == (filename + 4)) {
@@ -66,7 +72,7 @@ static char*const * remove_ld_preload(char*const * envp)
6672
return envp;
6773
}
6874

69-
int execve(const char* filename, char* const* argv, char *const envp[])
75+
int execve(const char* filename, char* const* argv, char* const* envp)
7076
{
7177
bool android_10_debug = getenv("TERMUX_ANDROID10_DEBUG") != NULL;
7278
if (android_10_debug) {
@@ -80,6 +86,7 @@ int execve(const char* filename, char* const* argv, char *const envp[])
8086

8187
int fd = -1;
8288
const char** new_argv = NULL;
89+
const char** new_envp = NULL;
8390

8491
char filename_buffer[512];
8592
filename = termux_rewrite_executable(filename, filename_buffer, sizeof(filename_buffer));
@@ -90,6 +97,31 @@ int execve(const char* filename, char* const* argv, char *const envp[])
9097
fd = open(filename, O_RDONLY);
9198
if (fd == -1) goto final;
9299

100+
// LD_LIBRARY_PATH messes up system programs with CANNOT_LINK_EXECUTABLE errors.
101+
// If we remove.it, this problem is solved.
102+
// /system/bin/sh is fine, it only uses libc++, libc, and libdl.
103+
if (starts_with(filename, "/system/") && strcmp(filename, "/system/bin/sh") != 0) {
104+
105+
size_t envp_count = 0;
106+
while (envp[envp_count] != NULL)
107+
envp_count++;
108+
109+
new_envp = malloc((envp_count + 1) * sizeof(char*));
110+
111+
size_t pos = 0;
112+
for (size_t i = 0; i < envp_count; i++) {
113+
// Skip it if it is LD_LIBRARY_PATH or LD_PRELOAD
114+
if (!starts_with(envp[i], "LD_LIBRARY_PATH=") &&
115+
!starts_with(envp[i], "LD_PRELOAD="))
116+
new_envp[pos++] = (const char*)envp[i];
117+
}
118+
new_envp[pos] = NULL;
119+
120+
envp = (char**)new_envp;
121+
// Not.sure if needed.
122+
environ = (char**)new_envp;
123+
}
124+
93125
// execve(2): "A maximum line length of 127 characters is allowed
94126
// for the first line in a #! executable shell script."
95127
char header[128];
@@ -159,7 +191,7 @@ int execve(const char* filename, char* const* argv, char *const envp[])
159191

160192
final:
161193
if (fd != -1) close(fd);
162-
int (*real_execve)(const char*, char *const[], char *const[]) = dlsym(RTLD_NEXT, "execve");
194+
int (*real_execve)(const char*, char* const[], char* const[]) = dlsym(RTLD_NEXT, "execve");
163195

164196
bool android_10_wrapping = getenv("TERMUX_ANDROID10") != NULL;
165197
if (android_10_wrapping) {
@@ -201,5 +233,6 @@ int execve(const char* filename, char* const* argv, char *const envp[])
201233

202234
int ret = real_execve(filename, argv, envp);
203235
free(new_argv);
236+
free(new_envp);
204237
return ret;
205238
}

0 commit comments

Comments
 (0)