Skip to content

Commit

Permalink
libtrace.c: use realpath instead of readlink to avoid PATH_MAX
Browse files Browse the repository at this point in the history
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 579f856 ("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.
  • Loading branch information
kmk3 committed Oct 16, 2021
1 parent 2164412 commit 8954cb2
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/libtrace/libtrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <sys/types.h>
#include <limits.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 8954cb2

Please sign in to comment.