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

Too big a chunk of refactoring and portability fix. #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
libtermux-exec.so: termux-exec.c
$(CC) $(CFLAGS) -Wall -Wextra -Oz termux-exec.c -shared -fPIC -o libtermux-exec.so
UNAME_S := $(shell uname -s)

ifeq ($(UNAME_S),Darwin)
CFLAGS += -Oz
else
CFLAGS += -Os
endif

libtermux-exec.so: termux-exec.c termux_rewrite_executable.c
$(CC) $(CFLAGS) $^ -shared -fPIC -o $@

termux-rewrite-exe: termux-rewrite-exe.c termux_rewrite_executable.c
$(CC) $(CFLAGS) $^ -o $@

install: libtermux-exec.so
install libtermux-exec.so $(PREFIX)/lib/libtermux-exec.so
Expand All @@ -11,6 +22,6 @@ test: libtermux-exec.so
@LD_PRELOAD=${CURDIR}/libtermux-exec.so ./run-tests.sh

clean:
rm -f libtermux-exec.so tests/*-actual
rm -f libtermux-exec.so termux-rewrite-exe tests/*-actual

.PHONY: clean install test uninstall
27 changes: 13 additions & 14 deletions termux-exec.c
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
#define _GNU_SOURCE
// for RTLD_NEXT
// ref: http://comments.gmane.org/gmane.linux.man/5208

#include "termux_rewrite_executable.h"

#include <dlfcn.h>

#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static const char* termux_rewrite_executable(const char* filename, char* buffer, int buffer_len)
{
strcpy(buffer, "/data/data/com.termux/files/usr/bin/");
char* bin_match = strstr(filename, "/bin/");
if (bin_match == filename || bin_match == (filename + 4)) {
// We have either found "/bin/" at the start of the string or at
// "/xxx/bin/". Take the path after that.
strncpy(buffer + 36, bin_match + 5, buffer_len - 37);
filename = buffer;
}
return filename;
}
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int execve(const char* filename, char* const* argv, char *const envp[])
{
int fd = -1;
const char** new_argv = NULL;

char filename_buffer[512];
filename = termux_rewrite_executable(filename, filename_buffer, sizeof(filename_buffer));
filename = termux_rewrite_executable(filename_buffer, sizeof(filename_buffer), filename);

// Error out if the file is not executable:
if (access(filename, X_OK) != 0) goto final;
Expand Down Expand Up @@ -69,7 +67,8 @@ int execve(const char* filename, char* const* argv, char *const envp[])
}

char interp_buf[512];
const char* new_interpreter = termux_rewrite_executable(interpreter, interp_buf, sizeof(interp_buf));
const char* new_interpreter =
termux_rewrite_executable(interp_buf, sizeof(interp_buf), interpreter);
if (new_interpreter == interpreter) goto final;

int orig_argv_count = 0;
Expand Down
49 changes: 49 additions & 0 deletions termux-rewrite-exe.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
command line test driver

## command ##
$ ./termux-rewrite-exe /bin/sh /usr/bin/env
## output ##
/data/data/com.termux/files/usr/bin/sh
/data/data/com.termux/files/usr/bin/env
*/

#include "termux_rewrite_executable.h"

#include <stdio.h>
#include <stddef.h>
#include <string.h>

int main(int argc, char** argv) {
int verbose = 0;
size_t idx = 1;
for ( ; idx < argc; ++idx ) {
if (strcmp("-v", argv[idx]) == 0) {
verbose = 1;
continue;
}
if (strcmp("--", argv[idx]) == 0) {
++idx;
break;
}
if (strncmp("-", argv[idx], 1) == 0) {
fprintf(stderr, "unrecognized option '%s'", argv[idx]);
return 1;
}

break;
}
const char* fmt[]={
"%.0s%s\n",
"'%s' -> '%s'\n"
};

char rewritten[512];
for( ; idx < argc; ++idx ) {
printf(
fmt[verbose],
argv[idx],
termux_rewrite_executable(rewritten, sizeof(rewritten), argv[idx]));
}
return 0;
}
20 changes: 20 additions & 0 deletions termux_rewrite_executable.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "termux_rewrite_executable.h"

#include <string.h>
#include <stdio.h>

const char* termux_rewrite_executable(
char* rewritten, size_t rewritten_size, const char* original)
{
const char* bin_match = strstr(original, "/bin/");
// We have either found "/bin/" at the start of the string or at
// "/xxx/bin/". Take the path after that.
if (bin_match != original && bin_match != (original + 4)) {
snprintf(rewritten, rewritten_size, "%s", original);
return rewritten;
}

const char termux_usr_bin[]="/data/data/com.termux/files/usr/bin";
snprintf(rewritten, rewritten_size, "%s/%s", termux_usr_bin, bin_match + 5);
return rewritten;
}
11 changes: 11 additions & 0 deletions termux_rewrite_executable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef TERMUX_REWRITE_EXECUTABLE__INCLUDED
#define TERMUX_REWRITE_EXECUTABLE__INCLUDED

#include <stddef.h>

const char* termux_rewrite_executable(
char* rewritten,
size_t rewritten_size,
const char* original);

#endif // TERMUX_REWRITE_EXECUTABLE__INCLUDED