Skip to content

Commit d6dd7fd

Browse files
authored
Fix wrapper_open redirects (#64)
* Fix wrapper_open redirects * Return boolean for redirected paths * comments
1 parent ea41a45 commit d6dd7fd

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

libc_impl.c

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,11 @@ static void init_redirect_paths(void) {
339339
}
340340

341341
/**
342-
* Redirects `path` by replacing the initial segment `from` by `to`. The result is placed in `out`.
343-
* If `path` does not have `from` as its initial segment, or there is no `to`, the original path is used.
342+
* Redirects `path` by replacing the initial segment `from` by `to`. The result is placed in `out`, and true returned
343+
* If `path` does not have `from` as its initial segment, or there is no `to`, false is returned.
344344
* If an error occurs, an error message will be printed, and the program exited.
345345
*/
346-
void redirect_path(char* out, const char* path, const char* from, const char* to) {
346+
bool redirect_path(char* out, const char* path, const char* from, const char* to) {
347347
int from_len = strlen(from);
348348

349349
if ((strncmp(path, from, from_len) == 0) && (to[0] != '\0')) {
@@ -354,14 +354,15 @@ void redirect_path(char* out, const char* path, const char* from, const char* to
354354

355355
if ((n >= 0) && ((size_t)n < sizeof(redirected_path))) {
356356
strcpy(out, redirected_path);
357-
} else {
358-
fprintf(stderr, "Error: Unable to redirect %s->%s for %s\n", from, to, path);
359-
exit(1);
357+
return true;
360358
}
361-
} else {
362-
// memmove instead of strcpy to allow overlapping buffers
363-
memmove(out, path, strlen(path) + 1);
359+
360+
// Redirection failed
361+
fprintf(stderr, "Error: Unable to redirect %s->%s for %s\n", from, to, path);
362+
exit(1);
364363
}
364+
365+
return false;
365366
}
366367

367368
typedef struct GlobalArgs {
@@ -1031,13 +1032,17 @@ uint32_t wrapper_strlen(uint8_t* mem, uint32_t str_addr) {
10311032
return len;
10321033
}
10331034

1034-
int wrapper_open(uint8_t* mem, uint32_t pathname_addr, int flags, int mode) {
1035-
STRING(pathname)
1035+
int wrapper_open(uint8_t* mem, uint32_t path_addr, int flags, int mode) {
1036+
STRING(path)
10361037

1037-
char rpathname[PATH_MAX + 1];
1038-
redirect_path(rpathname, pathname, "/usr/include", usr_include_redirect);
1039-
redirect_path(rpathname, pathname, "/usr/lib", usr_lib_redirect);
1040-
redirect_path(rpathname, pathname, "/lib", usr_lib_redirect);
1038+
char rpath[PATH_MAX + 1];
1039+
char* pathPtr = path;
1040+
1041+
if (redirect_path(rpath, path, "/usr/include", usr_include_redirect) ||
1042+
redirect_path(rpath, path, "/usr/lib", usr_lib_redirect) ||
1043+
redirect_path(rpath, path, "/lib", usr_lib_redirect) ) {
1044+
pathPtr = rpath;
1045+
}
10411046

10421047
int f = flags & O_ACCMODE;
10431048
if (flags & 0x100) {
@@ -1056,7 +1061,7 @@ int wrapper_open(uint8_t* mem, uint32_t pathname_addr, int flags, int mode) {
10561061
f |= O_APPEND;
10571062
}
10581063

1059-
int fd = open(rpathname, f, mode);
1064+
int fd = open(pathPtr, f, mode);
10601065
MEM_U32(ERRNO_ADDR) = errno;
10611066
return fd;
10621067
}
@@ -1471,11 +1476,15 @@ static uint32_t init_file(uint8_t* mem, int fd, int i, const char* path, const c
14711476
}
14721477

14731478
if (fd == -1) {
1474-
char rpathname[PATH_MAX + 1];
1475-
redirect_path(rpathname, path, "/usr/lib/DCC", usr_lib_redirect);
1476-
redirect_path(rpathname, rpathname, "/usr/lib", usr_lib_redirect);
1479+
char rpath[PATH_MAX + 1];
1480+
const char* pathPtr = path;
1481+
1482+
if (redirect_path(rpath, path, "/usr/lib/DCC", usr_lib_redirect) ||
1483+
redirect_path(rpath, path, "/usr/lib", usr_lib_redirect)) {
1484+
pathPtr = rpath;
1485+
}
14771486

1478-
fd = open(rpathname, flags, 0666);
1487+
fd = open(pathPtr, flags, 0666);
14791488

14801489
if (fd < 0) {
14811490
MEM_U32(ERRNO_ADDR) = errno;
@@ -2737,10 +2746,14 @@ int wrapper_execvp(uint8_t* mem, uint32_t file_addr, uint32_t argv_addr) {
27372746
char** argv = make_argv_from_mem(mem, argc, argv_addr);
27382747

27392748
char rfile[PATH_MAX + 1];
2740-
redirect_path(rfile, file, "/usr/lib/DCC", usr_lib_redirect);
2741-
redirect_path(rfile, rfile, "/usr/lib", usr_lib_redirect);
2749+
char* filePtr = file;
2750+
2751+
if (redirect_path(rfile, file, "/usr/lib/DCC", usr_lib_redirect) ||
2752+
redirect_path(rfile, file, "/usr/lib", usr_lib_redirect)) {
2753+
filePtr = rfile;
2754+
}
27422755

2743-
execvp(rfile, argv);
2756+
execvp(filePtr, argv);
27442757

27452758
MEM_U32(ERRNO_ADDR) = errno;
27462759

0 commit comments

Comments
 (0)