fd5c07b
Patch series proposed upstream:
fd5c07b
fd5c07b
  <https://sourceware.org/pipermail/libc-alpha/2021-June/127473.html>
fd5c07b
fd5c07b
Author: Florian Weimer <fweimer@redhat.com>
fd5c07b
Date:   Wed Jun 9 14:12:46 2021 +0200
fd5c07b
fd5c07b
    elf: Generalize name-based DSO recognition in ldconfig
fd5c07b
    
fd5c07b
    This introduces <dl-is_dso.h> and the _dl_is_dso function.  A
fd5c07b
    test ensures that the official names of libc.so, ld.so, and their
fd5c07b
    versioned names are recognized.
fd5c07b
22321f2
fd5c07b
diff --git a/elf/Makefile b/elf/Makefile
22321f2
index 38d08e03b8979a81..62f7e8a22544ab9d 100644
fd5c07b
--- a/elf/Makefile
fd5c07b
+++ b/elf/Makefile
22321f2
@@ -223,7 +223,8 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \
fd5c07b
 	 tst-single_threaded tst-single_threaded-pthread \
fd5c07b
 	 tst-tls-ie tst-tls-ie-dlmopen argv0test \
fd5c07b
 	 tst-glibc-hwcaps tst-glibc-hwcaps-prepend tst-glibc-hwcaps-mask \
22321f2
-	 tst-tls20 tst-tls21 tst-dlmopen-dlerror tst-dlmopen-gethostbyname
22321f2
+	 tst-tls20 tst-tls21 tst-dlmopen-dlerror tst-dlmopen-gethostbyname \
22321f2
+	 tst-dl-is_dso
fd5c07b
 #	 reldep9
fd5c07b
 tests-internal += loadtest unload unload2 circleload1 \
fd5c07b
 	 neededtest neededtest2 neededtest3 neededtest4 \
fd5c07b
diff --git a/elf/dl-is_dso.h b/elf/dl-is_dso.h
fd5c07b
new file mode 100644
fd5c07b
index 0000000000000000..94e00966a16e0df5
fd5c07b
--- /dev/null
fd5c07b
+++ b/elf/dl-is_dso.h
fd5c07b
@@ -0,0 +1,33 @@
fd5c07b
+/* Heuristic for recognizing DSO file names.
fd5c07b
+   Copyright (C) 2021 Free Software Foundation, Inc.
fd5c07b
+   This file is part of the GNU C Library.
fd5c07b
+
fd5c07b
+   The GNU C Library is free software; you can redistribute it and/or
fd5c07b
+   modify it under the terms of the GNU Lesser General Public
fd5c07b
+   License as published by the Free Software Foundation; either
fd5c07b
+   version 2.1 of the License, or (at your option) any later version.
fd5c07b
+
fd5c07b
+   The GNU C Library is distributed in the hope that it will be useful,
fd5c07b
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
fd5c07b
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
fd5c07b
+   Lesser General Public License for more details.
fd5c07b
+
fd5c07b
+   You should have received a copy of the GNU Lesser General Public
fd5c07b
+   License along with the GNU C Library; if not, see
fd5c07b
+   <https://www.gnu.org/licenses/>.  */
fd5c07b
+
fd5c07b
+#include <stdbool.h>
fd5c07b
+#include <string.h>
fd5c07b
+
fd5c07b
+/* Returns true if the file name looks like a DSO name.  */
fd5c07b
+static bool
fd5c07b
+_dl_is_dso (const char *name)
fd5c07b
+{
fd5c07b
+  /* Recognize lib*.so*, ld-*.so*, ld.so.*, ld64.so.*.  ld-*.so*
fd5c07b
+     matches both platform dynamic linker names like ld-linux.so.2,
fd5c07b
+     and versioned dynamic loader names like ld-2.12.so.  */
fd5c07b
+  return (((strncmp (name, "lib", 3) == 0 || strncmp (name, "ld-", 3) == 0)
fd5c07b
+           && strstr (name, ".so") != NULL)
fd5c07b
+          || strncmp (name, "ld.so.", 6) == 0
fd5c07b
+          || strncmp (name, "ld64.so.", 8) == 0);
fd5c07b
+}
fd5c07b
diff --git a/elf/ldconfig.c b/elf/ldconfig.c
fd5c07b
index 96bf7700b2efd37a..1037e8d0cf8d28b6 100644
fd5c07b
--- a/elf/ldconfig.c
fd5c07b
+++ b/elf/ldconfig.c
fd5c07b
@@ -43,6 +43,7 @@
fd5c07b
 #include <ldconfig.h>
fd5c07b
 #include <dl-cache.h>
fd5c07b
 #include <dl-hwcaps.h>
fd5c07b
+#include <dl-is_dso.h>
fd5c07b
 
fd5c07b
 #include <dl-procinfo.h>
fd5c07b
 
fd5c07b
@@ -842,9 +843,7 @@ search_dir (const struct dir_entry *entry)
fd5c07b
 	 subdirectory (if not already processing a glibc-hwcaps
fd5c07b
 	 subdirectory)?  The dynamic linker is also considered as
fd5c07b
 	 shared library.  */
fd5c07b
-      if (((strncmp (direntry->d_name, "lib", 3) != 0
fd5c07b
-	    && strncmp (direntry->d_name, "ld-", 3) != 0)
fd5c07b
-	   || strstr (direntry->d_name, ".so") == NULL)
fd5c07b
+      if (!_dl_is_dso (direntry->d_name)
fd5c07b
 	  && (direntry->d_type == DT_REG
fd5c07b
 	      || (entry->hwcaps == NULL
fd5c07b
 		  && !is_hwcap_platform (direntry->d_name))))
fd5c07b
diff --git a/elf/tst-dl-is_dso.c b/elf/tst-dl-is_dso.c
fd5c07b
new file mode 100644
fd5c07b
index 0000000000000000..48d2cc9fbe9edbc6
fd5c07b
--- /dev/null
fd5c07b
+++ b/elf/tst-dl-is_dso.c
fd5c07b
@@ -0,0 +1,35 @@
fd5c07b
+/* Test heuristic for recognizing DSO file names.
fd5c07b
+   Copyright (C) 2021 Free Software Foundation, Inc.
fd5c07b
+   This file is part of the GNU C Library.
fd5c07b
+
fd5c07b
+   The GNU C Library is free software; you can redistribute it and/or
fd5c07b
+   modify it under the terms of the GNU Lesser General Public
fd5c07b
+   License as published by the Free Software Foundation; either
fd5c07b
+   version 2.1 of the License, or (at your option) any later version.
fd5c07b
+
fd5c07b
+   The GNU C Library is distributed in the hope that it will be useful,
fd5c07b
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
fd5c07b
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
fd5c07b
+   Lesser General Public License for more details.
fd5c07b
+
fd5c07b
+   You should have received a copy of the GNU Lesser General Public
fd5c07b
+   License along with the GNU C Library; if not, see
fd5c07b
+   <https://www.gnu.org/licenses/>.  */
fd5c07b
+
fd5c07b
+#include <dl-is_dso.h>
fd5c07b
+#include <gnu/lib-names.h>
fd5c07b
+#include <support/check.h>
fd5c07b
+
fd5c07b
+static int
fd5c07b
+do_test (void)
fd5c07b
+{
fd5c07b
+  /* Official ABI names.  */
fd5c07b
+  TEST_VERIFY (_dl_is_dso (LIBC_SO));
fd5c07b
+  TEST_VERIFY (_dl_is_dso (LD_SO));
fd5c07b
+  /* Version-based names.  The version number does not matter.  */
fd5c07b
+  TEST_VERIFY (_dl_is_dso ("libc-2.12.so"));
fd5c07b
+  TEST_VERIFY (_dl_is_dso ("ld-2.12.so"));
fd5c07b
+  return 0;
fd5c07b
+}
fd5c07b
+
fd5c07b
+#include <support/test-driver.c>