Skip to contentSkip to conversation

rtld: get machine from undef_map for protected symbols

PATCH In review Kyle Russell 2 participants 3 messages Sep 28, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rtld: get machine from undef_map for protected symbols

Avoids rtld segfault when _dl_lookup_symbol_x is called with NULL for skip_map on a protected symbol relocation.

Global protected symbols may not actually require a copy relocaton, in which case skip_map is undefined, so use the undef_map to determine the symbol arch.

---
 src/rtld/dl-lookupX.h   |  6 +++---
 testsuite/Makefile.am   |  2 +-
 testsuite/reloc12.c     | 19 +++++++++++++++++++
 testsuite/reloc12.h     | 11 +++++++++++
 testsuite/reloc12.sh    | 20 ++++++++++++++++++++
 testsuite/reloc12lib1.c | 11 +++++++++++
 testsuite/reloc12lib2.c | 16 ++++++++++++++++
 7 files changed, 81 insertions(+), 4 deletions(-)
 create mode 100644 testsuite/reloc12.c
 create mode 100644 testsuite/reloc12.h
 create mode 100755 testsuite/reloc12.sh
 create mode 100644 testsuite/reloc12lib1.c
 create mode 100644 testsuite/reloc12lib2.c
src/rtld/dl-lookupX.h +3 -3
index 425bb4b..250c509 100644
@@ -679,10 +679,10 @@ _dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
 	    if (do_lookup_x (undef_name, new_hash, &old_hash, *ref,
 			     &protected_value, *scope, i, version, flags,
 			     skip_map,
-			     (ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA(skip_map->machine)
+			     (ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA(undef_map->machine)
 			      && ELFW(ST_TYPE) ((*ref)->st_info) == STT_OBJECT
-			      && type_class == ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA(skip_map->machine))
-			     ? ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA(skip_map->machine)
+			      && type_class == ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA(undef_map->machine))
+			     ? ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA(undef_map->machine)
 			     : ELF_RTYPE_CLASS_PLT, NULL) != 0)
 	      break;
 
testsuite/Makefile.am +1 -1
index 030f65b..21de6a9 100644
@@ -5,7 +5,7 @@ AM_CFLAGS = -Wall
 
 TESTS = movelibs.sh \
 	reloc1.sh reloc2.sh reloc3.sh reloc4.sh reloc5.sh reloc6.sh \
-	reloc7.sh reloc8.sh reloc9.sh reloc10.sh reloc11.sh \
+	reloc7.sh reloc8.sh reloc9.sh reloc10.sh reloc11.sh reloc12.sh \
 	shuffle1.sh shuffle2.sh shuffle3.sh shuffle4.sh shuffle5.sh \
 	shuffle6.sh shuffle7.sh shuffle8.sh shuffle9.sh undo1.sh \
 	layout1.sh layout2.sh unprel1.sh \
testsuite/reloc12.c new file +19 -0
new file mode 100644
index 0000000..cfa8888
@@ -0,0 +1,19 @@
+#include "reloc12.h"
+#include <stdlib.h>
+
+int main()
+{
+  A* ptr = find('b');
+  if(b(ptr) != 0)
+    abort();
+
+  ptr = find('a');
+  if(b(ptr) != 1)
+    abort();
+
+  ptr = find('r');
+  if(b(ptr) != 2)
+    abort();
+
+  exit(0);
+}
testsuite/reloc12.h new file +11 -0
new file mode 100644
index 0000000..8e09405
@@ -0,0 +1,11 @@
+typedef struct
+  {
+    char a;
+    int b;
+  } A;
+
+extern A foo[] __attribute ((visibility ("protected")));
+
+A* find(char a);
+char a(const A*);
+int b(const A*);
testsuite/reloc12.sh new file +20 -0
new file mode 100755
index 0000000..a8a43c7
@@ -0,0 +1,20 @@
+#!/bin/bash
+. `dirname $0`/functions.sh
+rm -f reloc12 reloc12lib*.so reloc12.log
+rm -f prelink.cache
+$RUN_HOST $CC -shared -O2 -fpic -o reloc12lib1.so $srcdir/reloc12lib1.c
+$RUN_HOST $CC -shared -O2 -fpic -o reloc12lib2.so $srcdir/reloc12lib2.c
+BINS="reloc12"
+LIBS="reloc12lib1.so reloc12lib2.so"
+$RUN_HOST $CCLINK -o reloc12 $srcdir/reloc12.c -Wl,--rpath-link,. ${LIBS}
+savelibs
+echo $PRELINK ${PRELINK_OPTS--vm} ./reloc12 > reloc12.log
+$RUN_HOST $PRELINK ${PRELINK_OPTS--vm} ./reloc12 >> reloc12.log 2>&1 || exit 1
+grep -q ^`echo $PRELINK | sed 's/ .*$/: /'` reloc12.log && exit 2
+if [ "x$CROSS" = "x" ]; then
+ $RUN LD_LIBRARY_PATH=. ./reloc12 || exit 3
+fi
+$RUN_HOST $READELF -a ./reloc12 >> reloc12.log 2>&1 || exit 4
+# So that it is not prelinked again
+chmod -x ./reloc12
+comparelibs >> reloc12.log 2>&1 || exit 5
testsuite/reloc12lib1.c new file +11 -0
new file mode 100644
index 0000000..db7e64f
@@ -0,0 +1,11 @@
+#include "reloc12.h"
+
+char a(const A *d)
+{
+  return d ? d->a : 0;
+}
+
+int b(const A *d)
+{
+  return d ? d->b : -1;
+}
testsuite/reloc12lib2.c new file +16 -0
new file mode 100644
index 0000000..ffa60a0
@@ -0,0 +1,16 @@
+#include "reloc12.h"
+
+A foo[] = {
+  { 'b', 0 },
+  { 'a', 1 },
+  { 'r', 2 }
+};
+
+A* find(char a)
+{
+  for(A* ptr = foo; ptr < foo + sizeof(foo)/sizeof(foo[0]); ptr++)
+    if(ptr->a == a)
+      return ptr;
+
+  return 0;
+}
Signature · 1 line
2.17.1
#1 lore raw
Quoted 6 lines · Kyle Russell

Avoids rtld segfault when _dl_lookup_symbol_x is called with NULL for skip_map on a protected symbol relocation.

Global protected symbols may not actually require a copy relocaton, in which case skip_map is undefined, so use the undef_map to determine the symbol arch.

Thank you. Any objections with me adding a 'Signed-off-by: ' line with your name?

I'm going to start enforcing submissions need a signed-off-by line per the terms of the "Developer's Certificate of Origin",

https://www.kernel.org/doc/html/latest/process/submitting-patches.html#sign-your-work-the-developer-s-certificate-of-origin

(While it is not yet a requirement, I'd like to start doing it now.)

Thanks!
--Mark

Quoted 154 lines
---
 src/rtld/dl-lookupX.h   |  6 +++---
 testsuite/Makefile.am   |  2 +-
 testsuite/reloc12.c     | 19 +++++++++++++++++++
 testsuite/reloc12.h     | 11 +++++++++++
 testsuite/reloc12.sh    | 20 ++++++++++++++++++++
 testsuite/reloc12lib1.c | 11 +++++++++++
 testsuite/reloc12lib2.c | 16 ++++++++++++++++
 7 files changed, 81 insertions(+), 4 deletions(-)
 create mode 100644 testsuite/reloc12.c
 create mode 100644 testsuite/reloc12.h
 create mode 100755 testsuite/reloc12.sh
 create mode 100644 testsuite/reloc12lib1.c
 create mode 100644 testsuite/reloc12lib2.c
src/rtld/dl-lookupX.h +3 -3
index 425bb4b..250c509 100644
@@ -679,10 +679,10 @@ _dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
 	    if (do_lookup_x (undef_name, new_hash, &old_hash, *ref,
 			     &protected_value, *scope, i, version, flags,
 			     skip_map,
-			     (ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA(skip_map->machine)
+			     (ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA(undef_map->machine)
 			      && ELFW(ST_TYPE) ((*ref)->st_info) == STT_OBJECT
-			      && type_class == ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA(skip_map->machine))
-			     ? ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA(skip_map->machine)
+			      && type_class == ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA(undef_map->machine))
+			     ? ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA(undef_map->machine)
 			     : ELF_RTYPE_CLASS_PLT, NULL) != 0)
 	      break;
 
testsuite/Makefile.am +1 -1
index 030f65b..21de6a9 100644
@@ -5,7 +5,7 @@ AM_CFLAGS = -Wall
 
 TESTS = movelibs.sh \
 	reloc1.sh reloc2.sh reloc3.sh reloc4.sh reloc5.sh reloc6.sh \
-	reloc7.sh reloc8.sh reloc9.sh reloc10.sh reloc11.sh \
+	reloc7.sh reloc8.sh reloc9.sh reloc10.sh reloc11.sh reloc12.sh \
 	shuffle1.sh shuffle2.sh shuffle3.sh shuffle4.sh shuffle5.sh \
 	shuffle6.sh shuffle7.sh shuffle8.sh shuffle9.sh undo1.sh \
 	layout1.sh layout2.sh unprel1.sh \
testsuite/reloc12.c new file +19 -0
new file mode 100644
index 0000000..cfa8888
@@ -0,0 +1,19 @@
+#include "reloc12.h"
+#include <stdlib.h>
+
+int main()
+{
+  A* ptr = find('b');
+  if(b(ptr) != 0)
+    abort();
+
+  ptr = find('a');
+  if(b(ptr) != 1)
+    abort();
+
+  ptr = find('r');
+  if(b(ptr) != 2)
+    abort();
+
+  exit(0);
+}
testsuite/reloc12.h new file +11 -0
new file mode 100644
index 0000000..8e09405
@@ -0,0 +1,11 @@
+typedef struct
+  {
+    char a;
+    int b;
+  } A;
+
+extern A foo[] __attribute ((visibility ("protected")));
+
+A* find(char a);
+char a(const A*);
+int b(const A*);
testsuite/reloc12.sh new file +20 -0
new file mode 100755
index 0000000..a8a43c7
@@ -0,0 +1,20 @@
+#!/bin/bash
+. `dirname $0`/functions.sh
+rm -f reloc12 reloc12lib*.so reloc12.log
+rm -f prelink.cache
+$RUN_HOST $CC -shared -O2 -fpic -o reloc12lib1.so $srcdir/reloc12lib1.c
+$RUN_HOST $CC -shared -O2 -fpic -o reloc12lib2.so $srcdir/reloc12lib2.c
+BINS="reloc12"
+LIBS="reloc12lib1.so reloc12lib2.so"
+$RUN_HOST $CCLINK -o reloc12 $srcdir/reloc12.c -Wl,--rpath-link,. ${LIBS}
+savelibs
+echo $PRELINK ${PRELINK_OPTS--vm} ./reloc12 > reloc12.log
+$RUN_HOST $PRELINK ${PRELINK_OPTS--vm} ./reloc12 >> reloc12.log 2>&1 || exit 1
+grep -q ^`echo $PRELINK | sed 's/ .*$/: /'` reloc12.log && exit 2
+if [ "x$CROSS" = "x" ]; then
+ $RUN LD_LIBRARY_PATH=. ./reloc12 || exit 3
+fi
+$RUN_HOST $READELF -a ./reloc12 >> reloc12.log 2>&1 || exit 4
+# So that it is not prelinked again
+chmod -x ./reloc12
+comparelibs >> reloc12.log 2>&1 || exit 5
testsuite/reloc12lib1.c new file +11 -0
new file mode 100644
index 0000000..db7e64f
@@ -0,0 +1,11 @@
+#include "reloc12.h"
+
+char a(const A *d)
+{
+  return d ? d->a : 0;
+}
+
+int b(const A *d)
+{
+  return d ? d->b : -1;
+}
testsuite/reloc12lib2.c new file +16 -0
new file mode 100644
index 0000000..ffa60a0
@@ -0,0 +1,16 @@
+#include "reloc12.h"
+
+A foo[] = {
+  { 'b', 0 },
+  { 'a', 1 },
+  { 'r', 2 }
+};
+
+A* find(char a)
+{
+  for(A* ptr = foo; ptr < foo + sizeof(foo)/sizeof(foo[0]); ptr++)
+    if(ptr->a == a)
+      return ptr;
+
+  return 0;
+}
#2 lore raw

No objections, please go ahead. Sorry I forgot! I realize I didn't do that on the other patch I just sent either.

Quoted 189 lines · Mark Hatle
Quoted 6 lines · Kyle Russell

Avoids rtld segfault when _dl_lookup_symbol_x is called with NULL for skip_map on a protected symbol relocation.

Global protected symbols may not actually require a copy relocaton, in which case skip_map is undefined, so use the undef_map to determine the symbol arch.

Thank you. Any objections with me adding a 'Signed-off-by: ' line with your name?

I'm going to start enforcing submissions need a signed-off-by line per the terms
of the "Developer's Certificate of Origin",

https://www.kernel.org/doc/html/latest/process/submitting-patches.html#sign-your-work-the-developer-s-certificate-of-origin

(While it is not yet a requirement, I'd like to start doing it now.)

Thanks!
--Mark

src/rtld/dl-lookupX.h _dl_lookup_symbol_x (const char *undef_name,

Quoted 20 lines
---
 src/rtld/dl-lookupX.h   |  6 +++---
 testsuite/Makefile.am   |  2 +-
 testsuite/reloc12.c     | 19 +++++++++++++++++++
 testsuite/reloc12.h     | 11 +++++++++++
 testsuite/reloc12.sh    | 20 ++++++++++++++++++++
 testsuite/reloc12lib1.c | 11 +++++++++++
 testsuite/reloc12lib2.c | 16 ++++++++++++++++
 7 files changed, 81 insertions(+), 4 deletions(-)
 create mode 100644 testsuite/reloc12.c
 create mode 100644 testsuite/reloc12.h
 create mode 100755 testsuite/reloc12.sh
 create mode 100644 testsuite/reloc12lib1.c
 create mode 100644 testsuite/reloc12lib2.c
src/rtld/dl-lookupX.h +0 -0
index 425bb4b..250c509 100644
@@ -679,10 +679,10 @@ _dl_lookup_symbol_x (const char *undef_name,

struct link_map *undef_map,

src/rtld/dl-lookupX.h _dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,

Quoted 4 lines
src/rtld/dl-lookupX.h +0 -1
          if (do_lookup_x (undef_name, new_hash, &old_hash, *ref,
                           &protected_value, *scope, i, version, flags,
                           skip_map,
-

(ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA(skip_map->machine)

src/rtld/dl-lookupX.h _dl_lookup_symbol_x (const char *undef_name, · line 682

Quoted 1 line
src/rtld/dl-lookupX.h +1 -0
+

(ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA(undef_map->machine)

src/rtld/dl-lookupX.h _dl_lookup_symbol_x (const char *undef_name, · line 683

Quoted 1 line
src/rtld/dl-lookupX.h +0 -0
                            && ELFW(ST_TYPE) ((*ref)->st_info) ==

STT_OBJECT

src/rtld/dl-lookupX.h _dl_lookup_symbol_x (const char *undef_name, · line 684

Quoted 1 line
src/rtld/dl-lookupX.h +0 -1
-                           && type_class ==

ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA(skip_map->machine))

src/rtld/dl-lookupX.h _dl_lookup_symbol_x (const char *undef_name, · line 685

Quoted 1 line
src/rtld/dl-lookupX.h +0 -1
-                          ?

ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA(skip_map->machine)

src/rtld/dl-lookupX.h _dl_lookup_symbol_x (const char *undef_name, · line 684

Quoted 1 line
src/rtld/dl-lookupX.h +1 -0
+                           && type_class ==

ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA(undef_map->machine))

src/rtld/dl-lookupX.h _dl_lookup_symbol_x (const char *undef_name, · line 685

Quoted 1 line
src/rtld/dl-lookupX.h +1 -0
+                          ?

ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA(undef_map->machine)

Quoted 64 lines
src/rtld/dl-lookupX.h +0 -0
                           : ELF_RTYPE_CLASS_PLT, NULL) != 0)
            break;
 
testsuite/Makefile.am +1 -1
index 030f65b..21de6a9 100644
@@ -5,7 +5,7 @@ AM_CFLAGS = -Wall
 
 TESTS = movelibs.sh \
      reloc1.sh reloc2.sh reloc3.sh reloc4.sh reloc5.sh reloc6.sh \
-     reloc7.sh reloc8.sh reloc9.sh reloc10.sh reloc11.sh \
+     reloc7.sh reloc8.sh reloc9.sh reloc10.sh reloc11.sh reloc12.sh \
      shuffle1.sh shuffle2.sh shuffle3.sh shuffle4.sh shuffle5.sh \
      shuffle6.sh shuffle7.sh shuffle8.sh shuffle9.sh undo1.sh \
      layout1.sh layout2.sh unprel1.sh \
testsuite/reloc12.c new file +19 -0
new file mode 100644
index 0000000..cfa8888
@@ -0,0 +1,19 @@
+#include "reloc12.h"
+#include <stdlib.h>
+
+int main()
+{
+  A* ptr = find('b');
+  if(b(ptr) != 0)
+    abort();
+
+  ptr = find('a');
+  if(b(ptr) != 1)
+    abort();
+
+  ptr = find('r');
+  if(b(ptr) != 2)
+    abort();
+
+  exit(0);
+}
testsuite/reloc12.h new file +11 -0
new file mode 100644
index 0000000..8e09405
@@ -0,0 +1,11 @@
+typedef struct
+  {
+    char a;
+    int b;
+  } A;
+
+extern A foo[] __attribute ((visibility ("protected")));
+
+A* find(char a);
+char a(const A*);
+int b(const A*);
testsuite/reloc12.sh new file +0 -0
new file mode 100755
index 0000000..a8a43c7

testsuite/reloc12.sh line 9

Quoted 9 lines
testsuite/reloc12.sh new file +9 -0
@@ -0,0 +1,20 @@
+#!/bin/bash
+. `dirname $0`/functions.sh
+rm -f reloc12 reloc12lib*.so reloc12.log
+rm -f prelink.cache
+$RUN_HOST $CC -shared -O2 -fpic -o reloc12lib1.so $srcdir/reloc12lib1.c
+$RUN_HOST $CC -shared -O2 -fpic -o reloc12lib2.so $srcdir/reloc12lib2.c
+BINS="reloc12"
+LIBS="reloc12lib1.so reloc12lib2.so"
+$RUN_HOST $CCLINK -o reloc12 $srcdir/reloc12.c -Wl,--rpath-link,.

${LIBS}

testsuite/reloc12.sh

Quoted 3 lines
testsuite/reloc12.sh new file +3 -0
+savelibs
+echo $PRELINK ${PRELINK_OPTS--vm} ./reloc12 > reloc12.log
+$RUN_HOST $PRELINK ${PRELINK_OPTS--vm} ./reloc12 >> reloc12.log 2>&1 ||

exit 1

Quoted 48 lines
testsuite/reloc12.sh new file +8 -0
+grep -q ^`echo $PRELINK | sed 's/ .*$/: /'` reloc12.log && exit 2
+if [ "x$CROSS" = "x" ]; then
+ $RUN LD_LIBRARY_PATH=. ./reloc12 || exit 3
+fi
+$RUN_HOST $READELF -a ./reloc12 >> reloc12.log 2>&1 || exit 4
+# So that it is not prelinked again
+chmod -x ./reloc12
+comparelibs >> reloc12.log 2>&1 || exit 5
testsuite/reloc12lib1.c new file +11 -0
new file mode 100644
index 0000000..db7e64f
@@ -0,0 +1,11 @@
+#include "reloc12.h"
+
+char a(const A *d)
+{
+  return d ? d->a : 0;
+}
+
+int b(const A *d)
+{
+  return d ? d->b : -1;
+}
testsuite/reloc12lib2.c new file +16 -0
new file mode 100644
index 0000000..ffa60a0
@@ -0,0 +1,16 @@
+#include "reloc12.h"
+
+A foo[] = {
+  { 'b', 0 },
+  { 'a', 1 },
+  { 'r', 2 }
+};
+
+A* find(char a)
+{
+  for(A* ptr = foo; ptr < foo + sizeof(foo)/sizeof(foo[0]); ptr++)
+    if(ptr->a == a)
+      return ptr;
+
+  return 0;
+}