diff options
author | 2021-10-01 18:03:32 -0300 | |
---|---|---|
committer | 2021-10-16 17:19:36 -0300 | |
commit | 8954cb2a1f8cf0d1e1101a2d82b3a98ce16f85c7 (patch) | |
tree | 5493f4862011efca4dca2250f0c33ae1ca2e5ff0 /src | |
parent | build: allow building with sanitizer (#4594) (diff) | |
download | firejail-8954cb2a1f8cf0d1e1101a2d82b3a98ce16f85c7.tar.gz firejail-8954cb2a1f8cf0d1e1101a2d82b3a98ce16f85c7.tar.zst firejail-8954cb2a1f8cf0d1e1101a2d82b3a98ce16f85c7.zip |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/libtrace/libtrace.c | 14 |
1 files 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 @@ | |||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 | */ | 19 | */ |
20 | #define _GNU_SOURCE | 20 | #define _GNU_SOURCE |
21 | #include <errno.h> | ||
21 | #include <stdio.h> | 22 | #include <stdio.h> |
22 | #include <stdlib.h> | 23 | #include <stdlib.h> |
23 | #include <string.h> | 24 | #include <string.h> |
24 | #include <dlfcn.h> | 25 | #include <dlfcn.h> |
25 | #include <sys/types.h> | 26 | #include <sys/types.h> |
26 | #include <limits.h> | ||
27 | #include <unistd.h> | 27 | #include <unistd.h> |
28 | #include <sys/socket.h> | 28 | #include <sys/socket.h> |
29 | #include <netinet/in.h> | 29 | #include <netinet/in.h> |
@@ -706,10 +706,14 @@ __attribute__((constructor)) | |||
706 | static void log_exec(int argc, char** argv) { | 706 | static void log_exec(int argc, char** argv) { |
707 | (void) argc; | 707 | (void) argc; |
708 | (void) argv; | 708 | (void) argv; |
709 | static char buf[PATH_MAX + 1]; | 709 | char *buf = realpath("/proc/self/exe", NULL); |
710 | int rv = readlink("/proc/self/exe", buf, PATH_MAX); | 710 | if (buf == NULL) { |
711 | if (rv != -1) { | 711 | if (errno == ENOMEM) { |
712 | buf[rv] = '\0'; // readlink does not add a '\0' at the end | 712 | tprintf(ftty, "realpath: %s\n", strerror(errno)); |
713 | exit(1); | ||
714 | } | ||
715 | } else { | ||
713 | tprintf(ftty, "%u:%s:exec %s:0\n", mypid, myname, buf); | 716 | tprintf(ftty, "%u:%s:exec %s:0\n", mypid, myname, buf); |
717 | free(buf); | ||
714 | } | 718 | } |
715 | } | 719 | } |