From 9b77630c8bad4c8bfc32a873deda2d90bc3b9f86 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Fri, 24 Aug 2018 11:31:57 +0900 Subject: [PATCH] mcexec: readlink and use full path for reexec This fixes comm on linux side, showing mcexec instead of 'exe' Change-Id: I9345d7a23dccb36b3a1e17fd3e7491eaeca54e5b --- executer/user/mcexec.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/executer/user/mcexec.c b/executer/user/mcexec.c index d02b5cdc..685a24d3 100644 --- a/executer/user/mcexec.c +++ b/executer/user/mcexec.c @@ -1325,6 +1325,7 @@ static int reduce_stack(struct rlimit *orig_rlim, char *argv[]) { int n; char newval[40]; + char path[1024]; int error; struct rlimit new_rlim; @@ -1356,7 +1357,17 @@ static int reduce_stack(struct rlimit *orig_rlim, char *argv[]) return 1; } - execv("/proc/self/exe", argv); + error = readlink("/proc/self/exe", path, sizeof(path)); + if (error < 0) { + __eprintf("Could not readlink /proc/self/exe? %m\n"); + return 1; + } else if (error >= sizeof(path)) { + strcpy(path, "/proc/self/exe"); + } else { + path[error] = '\0'; + } + + execv(path, argv); __eprintf("failed to execv(myself)\n"); return 1; @@ -2053,7 +2064,15 @@ int main(int argc, char **argv) error = setenv("MCEXEC_ADDR_NO_RANDOMIZE", "1", 1); CHKANDJUMP(error == -1, 1, "setenv failed\n"); - error = execv("/proc/self/exe", argv); + error = readlink("/proc/self/exe", path, sizeof(path)); + CHKANDJUMP(error == -1, 1, "readlink failed: %m\n"); + if (error >= sizeof(path)) { + strcpy(path, "/proc/self/exe"); + } else { + path[error] = '\0'; + } + + error = execv(path, argv); CHKANDJUMPF(error == -1, 1, "execv failed, error=%d,strerror=%s\n", error, strerror(errno)); } if (getenv("MCEXEC_ADDR_NO_RANDOMIZE")) {