From 8954cb2a1f8cf0d1e1101a2d82b3a98ce16f85c7 Mon Sep 17 00:00:00 2001 From: "Kelvin M. Klann" Date: Fri, 1 Oct 2021 18:03:32 -0300 Subject: libtrace.c: use realpath instead of readlink to avoid PATH_MAX PATH_MAX is not guaranteed to be defined and it may be defined to -1. Avoid depending on it by getting the result directly from realpath. See commit 579f856c5 ("firejail.h: add missing linux/limits.h include") / PR #4583 for details. Note: This replaces the static char array currently used with a dynamic one returned from realpath. Misc: This is a continuation of #4583. --- src/libtrace/libtrace.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/libtrace/libtrace.c b/src/libtrace/libtrace.c index d88512b0a..319902ff7 100644 --- a/src/libtrace/libtrace.c +++ b/src/libtrace/libtrace.c @@ -18,12 +18,12 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #define _GNU_SOURCE +#include #include #include #include #include #include -#include #include #include #include @@ -706,10 +706,14 @@ __attribute__((constructor)) static void log_exec(int argc, char** argv) { (void) argc; (void) argv; - static char buf[PATH_MAX + 1]; - int rv = readlink("/proc/self/exe", buf, PATH_MAX); - if (rv != -1) { - buf[rv] = '\0'; // readlink does not add a '\0' at the end + char *buf = realpath("/proc/self/exe", NULL); + if (buf == NULL) { + if (errno == ENOMEM) { + tprintf(ftty, "realpath: %s\n", strerror(errno)); + exit(1); + } + } else { tprintf(ftty, "%u:%s:exec %s:0\n", mypid, myname, buf); + free(buf); } } -- cgit v1.2.3-54-g00ecf