From 16a6a1d08b7bf881cf5086c5e43b9071e5dadbdb Mon Sep 17 00:00:00 2001 From: Masamichi Takagi Date: Sun, 19 Apr 2020 14:38:17 +0900 Subject: [PATCH] mcexec: Fix LD_PRELOAD string manipulation (again) Fixes: 8cf70900 "mcexec: Fix LD_PRELOAD string manipulation" Change-Id: I6e0188bd60f8e3977beb22c1f9212baf37f37093 --- executer/user/mcexec.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/executer/user/mcexec.c b/executer/user/mcexec.c index 343e6fc4..834b2304 100644 --- a/executer/user/mcexec.c +++ b/executer/user/mcexec.c @@ -1847,17 +1847,22 @@ opendev() return fd; } -#define LD_PRELOAD_PREPARE(name) do { \ - int n = 0;\ - \ - n += snprintf(elembuf, PATH_MAX, "%s", nelem > 0 ? ":" : ""); \ - strncat(elembuf, libdir, PATH_MAX - n); \ - n = n + strlen(libdir) > PATH_MAX ? \ - PATH_MAX : n + strlen(libdir); \ - strncat(elembuf, "/", PATH_MAX - n); \ - n = n + 1 > PATH_MAX ? PATH_MAX : n + 1; \ - strncat(elembuf, name, PATH_MAX - n); \ - } while (0) +#define LD_PRELOAD_PREPARE(name) do { \ + int n = 0; \ + \ + if (1 + strnlen(libdir, PATH_MAX) + 1 + \ + strnlen(name, PATH_MAX) + 1 > PATH_MAX) { \ + fprintf(stderr, \ + "%s: warning: LD_PRELOAD path is too long\n", \ + __func__); \ + return; \ + } \ + if (nelem > 0) \ + n += snprintf(elembuf, PATH_MAX, ":"); \ + n += snprintf(elembuf + n, PATH_MAX - n - 1, libdir); \ + n += snprintf(elembuf + n, PATH_MAX - n - 1, "/"); \ + n += snprintf(elembuf + n, PATH_MAX - n - 1, name); \ +} while (0) #define LD_PRELOAD_APPEND do { \ if (strlen(elembuf) + 1 > remainder) { \