From 3c95454a36d0c8e3ba7405df4617435ddd620981 Mon Sep 17 00:00:00 2001 From: Derek Hensley Date: Sat, 16 Dec 2023 19:38:25 -0800 Subject: [PATCH 1/3] Fix wrapper_open redirects --- libc_impl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libc_impl.c b/libc_impl.c index 0a4a54a..5f2dbf2 100644 --- a/libc_impl.c +++ b/libc_impl.c @@ -1036,8 +1036,8 @@ int wrapper_open(uint8_t* mem, uint32_t pathname_addr, int flags, int mode) { char rpathname[PATH_MAX + 1]; redirect_path(rpathname, pathname, "/usr/include", usr_include_redirect); - redirect_path(rpathname, pathname, "/usr/lib", usr_lib_redirect); - redirect_path(rpathname, pathname, "/lib", usr_lib_redirect); + redirect_path(rpathname, rpathname, "/usr/lib", usr_lib_redirect); + redirect_path(rpathname, rpathname, "/lib", usr_lib_redirect); int f = flags & O_ACCMODE; if (flags & 0x100) { From 899bb1c45e95a610ecf7d2fc9f1a2eda4d3657ec Mon Sep 17 00:00:00 2001 From: Derek Hensley Date: Sun, 17 Dec 2023 08:26:24 -0800 Subject: [PATCH 2/3] Return boolean for redirected paths --- libc_impl.c | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/libc_impl.c b/libc_impl.c index 5f2dbf2..716e426 100644 --- a/libc_impl.c +++ b/libc_impl.c @@ -339,12 +339,13 @@ static void init_redirect_paths(void) { } /** - * Redirects `path` by replacing the initial segment `from` by `to`. The result is placed in `out`. - * If `path` does not have `from` as its initial segment, or there is no `to`, the original path is used. + * Redirects `path` by replacing the initial segment `from` by `to`. The result is placed in `out`, and true returned + * If `path` does not have `from` as its initial segment, or there is no `to`, false is returned. * If an error occurs, an error message will be printed, and the program exited. */ -void redirect_path(char* out, const char* path, const char* from, const char* to) { +bool redirect_path(char* out, const char* path, const char* from, const char* to) { int from_len = strlen(from); + bool redirected = false; if ((strncmp(path, from, from_len) == 0) && (to[0] != '\0')) { char redirected_path[PATH_MAX + 1] = { 0 }; @@ -354,14 +355,14 @@ void redirect_path(char* out, const char* path, const char* from, const char* to if ((n >= 0) && ((size_t)n < sizeof(redirected_path))) { strcpy(out, redirected_path); + redirected = true; } else { fprintf(stderr, "Error: Unable to redirect %s->%s for %s\n", from, to, path); exit(1); } - } else { - // memmove instead of strcpy to allow overlapping buffers - memmove(out, path, strlen(path) + 1); } + + return redirected; } typedef struct GlobalArgs { @@ -1031,13 +1032,15 @@ uint32_t wrapper_strlen(uint8_t* mem, uint32_t str_addr) { return len; } -int wrapper_open(uint8_t* mem, uint32_t pathname_addr, int flags, int mode) { - STRING(pathname) +int wrapper_open(uint8_t* mem, uint32_t path_addr, int flags, int mode) { + STRING(path) - char rpathname[PATH_MAX + 1]; - redirect_path(rpathname, pathname, "/usr/include", usr_include_redirect); - redirect_path(rpathname, rpathname, "/usr/lib", usr_lib_redirect); - redirect_path(rpathname, rpathname, "/lib", usr_lib_redirect); + char rpath[PATH_MAX + 1]; + if(!redirect_path(rpath, path, "/usr/include", usr_include_redirect) && + !redirect_path(rpath, path, "/usr/lib", usr_lib_redirect) && + !redirect_path(rpath, path, "/lib", usr_lib_redirect) ) { + memmove(rpath, path, strlen(path) + 1); + } int f = flags & O_ACCMODE; if (flags & 0x100) { @@ -1056,7 +1059,7 @@ int wrapper_open(uint8_t* mem, uint32_t pathname_addr, int flags, int mode) { f |= O_APPEND; } - int fd = open(rpathname, f, mode); + int fd = open(rpath, f, mode); MEM_U32(ERRNO_ADDR) = errno; return fd; } @@ -1471,11 +1474,13 @@ static uint32_t init_file(uint8_t* mem, int fd, int i, const char* path, const c } if (fd == -1) { - char rpathname[PATH_MAX + 1]; - redirect_path(rpathname, path, "/usr/lib/DCC", usr_lib_redirect); - redirect_path(rpathname, rpathname, "/usr/lib", usr_lib_redirect); + char rpath[PATH_MAX + 1]; + if (!redirect_path(rpath, path, "/usr/lib/DCC", usr_lib_redirect) && + !redirect_path(rpath, path, "/usr/lib", usr_lib_redirect)) { + memmove(rpath, path, strlen(path) + 1); + } - fd = open(rpathname, flags, 0666); + fd = open(rpath, flags, 0666); if (fd < 0) { MEM_U32(ERRNO_ADDR) = errno; @@ -2737,8 +2742,10 @@ int wrapper_execvp(uint8_t* mem, uint32_t file_addr, uint32_t argv_addr) { char** argv = make_argv_from_mem(mem, argc, argv_addr); char rfile[PATH_MAX + 1]; - redirect_path(rfile, file, "/usr/lib/DCC", usr_lib_redirect); - redirect_path(rfile, rfile, "/usr/lib", usr_lib_redirect); + if (!redirect_path(rfile, file, "/usr/lib/DCC", usr_lib_redirect) && + !redirect_path(rfile, file, "/usr/lib", usr_lib_redirect)) { + memmove(rfile, file, strlen(file) + 1); + } execvp(rfile, argv); From 7b2b262611055cfc04dbeea4de0ceaf9104ef3a4 Mon Sep 17 00:00:00 2001 From: Derek Hensley Date: Sun, 17 Dec 2023 08:46:56 -0800 Subject: [PATCH 3/3] comments --- libc_impl.c | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/libc_impl.c b/libc_impl.c index 716e426..a13307d 100644 --- a/libc_impl.c +++ b/libc_impl.c @@ -345,7 +345,6 @@ static void init_redirect_paths(void) { */ bool redirect_path(char* out, const char* path, const char* from, const char* to) { int from_len = strlen(from); - bool redirected = false; if ((strncmp(path, from, from_len) == 0) && (to[0] != '\0')) { char redirected_path[PATH_MAX + 1] = { 0 }; @@ -355,14 +354,15 @@ bool redirect_path(char* out, const char* path, const char* from, const char* to if ((n >= 0) && ((size_t)n < sizeof(redirected_path))) { strcpy(out, redirected_path); - redirected = true; - } else { - fprintf(stderr, "Error: Unable to redirect %s->%s for %s\n", from, to, path); - exit(1); + return true; } + + // Redirection failed + fprintf(stderr, "Error: Unable to redirect %s->%s for %s\n", from, to, path); + exit(1); } - return redirected; + return false; } typedef struct GlobalArgs { @@ -1036,10 +1036,12 @@ int wrapper_open(uint8_t* mem, uint32_t path_addr, int flags, int mode) { STRING(path) char rpath[PATH_MAX + 1]; - if(!redirect_path(rpath, path, "/usr/include", usr_include_redirect) && - !redirect_path(rpath, path, "/usr/lib", usr_lib_redirect) && - !redirect_path(rpath, path, "/lib", usr_lib_redirect) ) { - memmove(rpath, path, strlen(path) + 1); + char* pathPtr = path; + + if (redirect_path(rpath, path, "/usr/include", usr_include_redirect) || + redirect_path(rpath, path, "/usr/lib", usr_lib_redirect) || + redirect_path(rpath, path, "/lib", usr_lib_redirect) ) { + pathPtr = rpath; } int f = flags & O_ACCMODE; @@ -1059,7 +1061,7 @@ int wrapper_open(uint8_t* mem, uint32_t path_addr, int flags, int mode) { f |= O_APPEND; } - int fd = open(rpath, f, mode); + int fd = open(pathPtr, f, mode); MEM_U32(ERRNO_ADDR) = errno; return fd; } @@ -1475,12 +1477,14 @@ static uint32_t init_file(uint8_t* mem, int fd, int i, const char* path, const c if (fd == -1) { char rpath[PATH_MAX + 1]; - if (!redirect_path(rpath, path, "/usr/lib/DCC", usr_lib_redirect) && - !redirect_path(rpath, path, "/usr/lib", usr_lib_redirect)) { - memmove(rpath, path, strlen(path) + 1); + const char* pathPtr = path; + + if (redirect_path(rpath, path, "/usr/lib/DCC", usr_lib_redirect) || + redirect_path(rpath, path, "/usr/lib", usr_lib_redirect)) { + pathPtr = rpath; } - fd = open(rpath, flags, 0666); + fd = open(pathPtr, flags, 0666); if (fd < 0) { MEM_U32(ERRNO_ADDR) = errno; @@ -2742,12 +2746,14 @@ int wrapper_execvp(uint8_t* mem, uint32_t file_addr, uint32_t argv_addr) { char** argv = make_argv_from_mem(mem, argc, argv_addr); char rfile[PATH_MAX + 1]; - if (!redirect_path(rfile, file, "/usr/lib/DCC", usr_lib_redirect) && - !redirect_path(rfile, file, "/usr/lib", usr_lib_redirect)) { - memmove(rfile, file, strlen(file) + 1); + char* filePtr = file; + + if (redirect_path(rfile, file, "/usr/lib/DCC", usr_lib_redirect) || + redirect_path(rfile, file, "/usr/lib", usr_lib_redirect)) { + filePtr = rfile; } - execvp(rfile, argv); + execvp(filePtr, argv); MEM_U32(ERRNO_ADDR) = errno;