-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
838 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Checks: '-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,-clang-analyzer-security.insecureAPI.strcpy,-clang-analyzer-valist.Uninitialized' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
branches: | ||
- '*' | ||
pull_request: | ||
|
||
jobs: | ||
check: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: Homebrew/actions/setup-homebrew@master | ||
- run: brew install clang-format | ||
- run: make | ||
- run: make check | ||
- run: make unit-test | ||
|
||
actionlint: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Download actionlint | ||
id: get_actionlint | ||
run: bash <(curl https://raw.githubusercontent.com/rhysd/actionlint/main/scripts/download-actionlint.bash) | ||
- name: Check workflow files | ||
run: ${{ steps.get_actionlint.outputs.executable }} -color |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
*.so | ||
*.o | ||
*-actual | ||
*.swo | ||
*.swp | ||
test-binary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,35 @@ | ||
TERMUX_PREFIX := /data/data/com.termux/files/usr | ||
TERMUX_BASE_DIR := /data/data/com.termux/files | ||
CFLAGS += -Wall -Wextra -Werror -Oz | ||
CFLAGS += -Wall -Wextra -Werror -Wshadow -O2 | ||
C_SOURCE := termux-exec.c exec-variants.c | ||
CLANG_FORMAT := clang-format --sort-includes --style="{ColumnLimit: 120}" $(C_SOURCE) | ||
CLANG_TIDY := clang-tidy | ||
|
||
libtermux-exec.so: termux-exec.c | ||
$(CC) $(CFLAGS) $(LDFLAGS) termux-exec.c -DTERMUX_PREFIX=\"$(TERMUX_PREFIX)\" -DTERMUX_BASE_DIR=\"$(TERMUX_BASE_DIR)\" -shared -fPIC -o libtermux-exec.so | ||
libtermux-exec.so: $(C_SOURCE) | ||
$(CC) $(CFLAGS) $(LDFLAGS) $(C_SOURCE) -DTERMUX_PREFIX=\"$(TERMUX_PREFIX)\" -DTERMUX_BASE_DIR=\"$(TERMUX_BASE_DIR)\" -shared -fPIC -o libtermux-exec.so | ||
|
||
clean: | ||
rm -f libtermux-exec.so tests/*-actual test-binary | ||
|
||
install: libtermux-exec.so | ||
install libtermux-exec.so $(DESTDIR)$(PREFIX)/lib/libtermux-exec.so | ||
|
||
uninstall: | ||
rm -f $(DESTDIR)$(PREFIX)/lib/libtermux-exec.so | ||
|
||
test: libtermux-exec.so | ||
on-device-tests: libtermux-exec.so | ||
@LD_PRELOAD=${CURDIR}/libtermux-exec.so ./run-tests.sh | ||
|
||
clean: | ||
rm -f libtermux-exec.so tests/*-actual | ||
format: | ||
$(CLANG_FORMAT) -i $(C_SOURCE) | ||
|
||
check: | ||
$(CLANG_FORMAT) --dry-run $(C_SOURCE) | ||
$(CLANG_TIDY) -warnings-as-errors='*' $(C_SOURCE) -- | ||
|
||
test-binary: $(C_SOURCE) | ||
$(CC) $(CFLAGS) $(LDFLAGS) $(C_SOURCE) -g -fsanitize=address -fno-omit-frame-pointer -DUNIT_TEST=1 -o test-binary | ||
|
||
unit-test: test-binary | ||
./test-binary | ||
|
||
.PHONY: clean install test uninstall | ||
.PHONY: clean install uninstall test format check-format test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
#define _GNU_SOURCE | ||
#include <errno.h> | ||
#include <paths.h> | ||
#include <stdarg.h> | ||
#include <stdbool.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
|
||
enum { ExecL, ExecLE, ExecLP }; | ||
|
||
static int __exec_as_script(const char *buf, char *const *argv, char *const *envp) { | ||
size_t arg_count = 1; | ||
while (argv[arg_count] != NULL) | ||
++arg_count; | ||
|
||
const char *script_argv[arg_count + 2]; | ||
script_argv[0] = "sh"; | ||
script_argv[1] = buf; | ||
memcpy(script_argv + 2, argv + 1, arg_count * sizeof(char *)); | ||
return execve("/data/data/com.termux/files/usr/bin/sh", (char **const)script_argv, envp); | ||
} | ||
|
||
int execv(const char *name, char *const *argv) { return execve(name, argv, environ); } | ||
|
||
int execvp(const char *name, char *const *argv) { return execvpe(name, argv, environ); } | ||
|
||
int execvpe(const char *name, char *const *argv, char *const *envp) { | ||
// if (name == NULL || *name == '\0') { errno = ENOENT; return -1; } | ||
|
||
// If it's an absolute or relative path name, it's easy. | ||
if (strchr(name, '/') && execve(name, argv, envp) == -1) { | ||
if (errno == ENOEXEC) | ||
return __exec_as_script(name, argv, envp); | ||
return -1; | ||
} | ||
|
||
// Get the path we're searching. | ||
const char *path = getenv("PATH"); | ||
if (path == NULL) | ||
path = _PATH_DEFPATH; | ||
|
||
// Make a writable copy. | ||
size_t len = strlen(path) + 1; | ||
char writable_path[len]; | ||
memcpy(writable_path, path, len); | ||
|
||
bool saw_EACCES = false; | ||
|
||
// Try each element of $PATH in turn... | ||
char *strsep_buf = writable_path; | ||
const char *dir; | ||
while ((dir = strsep(&strsep_buf, ":"))) { | ||
// It's a shell path: double, leading and trailing colons | ||
// mean the current directory. | ||
if (*dir == '\0') | ||
dir = "."; | ||
|
||
size_t dir_len = strlen(dir); | ||
size_t name_len = strlen(name); | ||
|
||
char buf[dir_len + 1 + name_len + 1]; | ||
mempcpy(mempcpy(mempcpy(buf, dir, dir_len), "/", 1), name, name_len + 1); | ||
|
||
execve(buf, argv, envp); | ||
switch (errno) { | ||
case EISDIR: | ||
case ELOOP: | ||
case ENAMETOOLONG: | ||
case ENOENT: | ||
case ENOTDIR: | ||
break; | ||
case ENOEXEC: | ||
return __exec_as_script(buf, argv, envp); | ||
return -1; | ||
case EACCES: | ||
saw_EACCES = true; | ||
break; | ||
default: | ||
return -1; | ||
} | ||
} | ||
if (saw_EACCES) | ||
errno = EACCES; | ||
return -1; | ||
} | ||
|
||
static int __execl(int variant, const char *name, const char *argv0, va_list ap) { | ||
// Count the arguments. | ||
va_list count_ap; | ||
va_copy(count_ap, ap); | ||
size_t n = 1; | ||
while (va_arg(count_ap, char *) != NULL) { | ||
++n; | ||
} | ||
va_end(count_ap); | ||
|
||
// Construct the new argv. | ||
char *argv[n + 1]; | ||
argv[0] = (char *)argv0; | ||
n = 1; | ||
while ((argv[n] = va_arg(ap, char *)) != NULL) { | ||
++n; | ||
} | ||
|
||
// Collect the argp too. | ||
char **argp = (variant == ExecLE) ? va_arg(ap, char **) : environ; | ||
|
||
va_end(ap); | ||
|
||
return (variant == ExecLP) ? execvp(name, argv) : execve(name, argv, argp); | ||
} | ||
|
||
int execl(const char *name, const char *arg, ...) { | ||
va_list ap; | ||
va_start(ap, arg); | ||
int result = __execl(ExecL, name, arg, ap); | ||
va_end(ap); | ||
return result; | ||
} | ||
|
||
int execle(const char *name, const char *arg, ...) { | ||
va_list ap; | ||
va_start(ap, arg); | ||
int result = __execl(ExecLE, name, arg, ap); | ||
va_end(ap); | ||
return result; | ||
} | ||
|
||
int execlp(const char *name, const char *arg, ...) { | ||
va_list ap; | ||
va_start(ap, arg); | ||
int result = __execl(ExecLP, name, arg, ap); | ||
va_end(ap); | ||
return result; | ||
} | ||
|
||
int fexecve(int fd, char *const *argv, char *const *envp) { | ||
char buf[40]; | ||
snprintf(buf, sizeof(buf), "/proc/self/fd/%d", fd); | ||
execve(buf, argv, envp); | ||
if (errno == ENOENT) | ||
errno = EBADF; | ||
return -1; | ||
} |
Oops, something went wrong.