Skip to content

Commit

Permalink
mptcpize: do not override existing LD_PRELOAD value (#315)
Browse files Browse the repository at this point in the history
If any.

Before, previously set LD_PRELOAD were overridden. According to
'man ld.so', there can be more than one library to load before other
objects as this env var accepts a list separated by spaces or colons:

  A list of additional, user-specified, ELF shared objects to be loaded
  before all others.  This feature can be used to selectively override
  functions in other shared objects.

  The items of the list can be separated by spaces or colons, and there
  is no support for escaping either separator.

So let's do that: if a previous LD_PRELOAD is detected, it will no
longer be dropped, but used with the MPTCP Wrap library added at the
end, e.g.

  LD_PRELOAD=dummy.so:libmptcpwrap.so.0.0.1

Signed-off-by: Matthieu Baerts (NGI0) <[email protected]>
  • Loading branch information
matttbe authored Jan 27, 2025
1 parent 07d4ec0 commit d16f0c9
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/mptcpize.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#define SYSTEMD_SERVICE_TAG "[Service]"
#define SYSTEMCTL_SHOW "systemctl show -p FragmentPath "
#define PRELOAD_VAR "LD_PRELOAD="
#define MPTCPWRAP_ENV "LD_PRELOAD="PKGLIBDIR"/libmptcpwrap.so.0.0."LIBREVISION
#define MPTCPWRAP_ENV PKGLIBDIR"/libmptcpwrap.so.0.0."LIBREVISION

/* Program documentation. */
static char args_doc[] = "CMD";
Expand Down Expand Up @@ -62,8 +62,9 @@ static void help(void)

static int run(int argc, char *av[])
{
int i, nr = 0, debug = 0;
char **envp, **argv;
int i, nr = 0, ld, debug = 0;
char **envp, **argv, *env;
size_t len;

if (argc > 0 && strcmp(av[0], "-d") == 0) {
debug = 1;
Expand All @@ -87,17 +88,27 @@ static int run(int argc, char *av[])
// ... filtering out any 'LD_PRELOAD' ...
nr = 0;
i = 0;
ld = -1;
while (environ[nr]) {
if (strncmp(environ[nr], PRELOAD_VAR,
strlen(PRELOAD_VAR)) != 0) {
strlen(PRELOAD_VAR)) == 0) {
ld = nr;
} else {
envp[i] = environ[nr];
i++;
}
nr++;
}

// ... appending the mptcpwrap preload...
envp[i++] = MPTCPWRAP_ENV;
if (ld >= 0) {
len = strlen(environ[ld]) + strlen(MPTCPWRAP_ENV) + 2;
env = alloca(len);
snprintf(env, len, "%s:%s", environ[ld], MPTCPWRAP_ENV);
} else {
env = PRELOAD_VAR MPTCPWRAP_ENV;
}
envp[i++] = env;

// ... and enable dbg if needed
if (debug)
Expand Down Expand Up @@ -204,7 +215,7 @@ static int unit_update(int argc, char *argv[], int enable)

if (append_env &&
(is_env || strncmp(line, SYSTEMD_SERVICE_TAG, strlen(SYSTEMD_SERVICE_TAG)) == 0)) {
if (dprintf(dst, "%s%s\n", SYSTEMD_ENV_VAR, MPTCPWRAP_ENV) < 0)
if (dprintf(dst, "%s%s\n", SYSTEMD_ENV_VAR, PRELOAD_VAR MPTCPWRAP_ENV) < 0)
error(1, errno, "can't write to env string into %s", dst_path);
append_env = 0;
}
Expand Down

0 comments on commit d16f0c9

Please sign in to comment.