Re: Regression tests fail with musl libc because libpq.so can't be loaded - Mailing list pgsql-bugs

From Tom Lane
Subject Re: Regression tests fail with musl libc because libpq.so can't be loaded
Date
Msg-id 2715727.1711410188@sss.pgh.pa.us
Whole thread Raw
In response to Re: Regression tests fail with musl libc because libpq.so can't be loaded  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: Regression tests fail with musl libc because libpq.so can't be loaded  (Thomas Munro <thomas.munro@gmail.com>)
Re: Regression tests fail with musl libc because libpq.so can't be loaded  (Peter Eisentraut <peter@eisentraut.org>)
List pgsql-bugs
I wrote:
> Thomas Munro <thomas.munro@gmail.com> writes:
>> I had originally proposed to avoid anything beginning "LD_" but Tom
>> suggested being more specific.  I doubt LD_PRELOAD can really hurt you
>> though (the linker probably only needs the value at the start by
>> definition, not at later dlopen() time (?)).

> Oh, good point.  So we could simplify the patch by only looking for
> LD_LIBRARY_PATH.

I looked at the musl source code you identified and confirmed that
only the LD_LIBRARY_PATH string is remembered in a static variable;
LD_PRELOAD is only accessed locally in that initialization function.
So we only need to do the attached.  (I failed to resist the
temptation to rewrite the comments.)

            regards, tom lane

diff --git a/src/backend/utils/misc/ps_status.c b/src/backend/utils/misc/ps_status.c
index 5d829e6e48..92cd2c7899 100644
--- a/src/backend/utils/misc/ps_status.c
+++ b/src/backend/utils/misc/ps_status.c
@@ -151,7 +151,33 @@ save_ps_display_args(int argc, char **argv)
         for (i = 0; environ[i] != NULL; i++)
         {
             if (end_of_area + 1 == environ[i])
-                end_of_area = environ[i] + strlen(environ[i]);
+            {
+                /*
+                 * The musl runtime linker keeps a static pointer to the
+                 * initial value of LD_LIBRARY_PATH, if that is defined in the
+                 * process's environment. Therefore, we must not overwrite the
+                 * value of that setting and thus cannot advance end_of_area
+                 * beyond it.  Musl does not define any identifying compiler
+                 * symbol, so we have to do this for any Linux libc we don't
+                 * know is safe.
+                 */
+#if defined(__linux__) && (!defined(__GLIBC__) && !defined(__UCLIBC__ ))
+                if (strncmp(environ[i], "LD_LIBRARY_PATH=", 16) == 0)
+                {
+                    /*
+                     * We can overwrite the name, but stop at the equals sign.
+                     * Future loop iterations will not find any more
+                     * contiguous space, but we don't break early because we
+                     * need to count the total number of environ[] entries.
+                     */
+                    end_of_area = environ[i] + 15;
+                }
+                else
+#endif
+                {
+                    end_of_area = environ[i] + strlen(environ[i]);
+                }
+            }
         }

         ps_buffer = argv[0];

pgsql-bugs by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: Regression tests fail with musl libc because libpq.so can't be loaded
Next
From: Thomas Munro
Date:
Subject: Re: Regression tests fail with musl libc because libpq.so can't be loaded