a2cf7d
commit 0499a353a6e196f468e7ec554cb13c82011f0e36
a2cf7d
Author: Florian Weimer <fweimer@redhat.com>
a2cf7d
Date:   Mon Mar 2 14:24:27 2020 +0100
a2cf7d
a2cf7d
    elf: Add elf/check-wx-segment, a test for the presence of WX segments
a2cf7d
    
a2cf7d
    Writable, executable segments defeat security hardening.  The
a2cf7d
    existing check for DT_TEXTREL does not catch this.
a2cf7d
    
a2cf7d
    hppa and SPARC currently keep the PLT in an RWX load segment.
a2cf7d
a2cf7d
# Conflicts:
a2cf7d
#	sysdeps/sparc/Makefile
a2cf7d
a2cf7d
diff --git a/elf/Makefile b/elf/Makefile
a2cf7d
index f1a16fe8ca594c57..a52d9b1f6a4364a7 100644
a2cf7d
--- a/elf/Makefile
a2cf7d
+++ b/elf/Makefile
a2cf7d
@@ -378,6 +378,7 @@ tests-special += $(objpfx)tst-pathopt.out $(objpfx)tst-rtld-load-self.out \
a2cf7d
 		 $(objpfx)tst-rtld-preload.out
a2cf7d
 endif
a2cf7d
 tests-special += $(objpfx)check-textrel.out $(objpfx)check-execstack.out \
a2cf7d
+		 $(objpfx)check-wx-segment.out \
a2cf7d
 		 $(objpfx)check-localplt.out $(objpfx)check-initfini.out
a2cf7d
 endif
a2cf7d
 
a2cf7d
@@ -1148,6 +1149,12 @@ $(objpfx)check-execstack.out: $(..)scripts/check-execstack.awk \
a2cf7d
 	$(evaluate-test)
a2cf7d
 generated += check-execstack.out
a2cf7d
 
a2cf7d
+$(objpfx)check-wx-segment.out: $(..)scripts/check-wx-segment.py \
a2cf7d
+			      $(all-built-dso:=.phdr)
a2cf7d
+	$(PYTHON) $^ --xfail="$(check-wx-segment-xfail)" > $@; \
a2cf7d
+	$(evaluate-test)
a2cf7d
+generated += check-wx-segment.out
a2cf7d
+
a2cf7d
 $(objpfx)tst-dlmodcount: $(libdl)
a2cf7d
 $(objpfx)tst-dlmodcount.out: $(test-modules)
a2cf7d
 
a2cf7d
diff --git a/scripts/check-wx-segment.py b/scripts/check-wx-segment.py
a2cf7d
new file mode 100644
a2cf7d
index 0000000000000000..e1fa79387ce22c4b
a2cf7d
--- /dev/null
a2cf7d
+++ b/scripts/check-wx-segment.py
a2cf7d
@@ -0,0 +1,85 @@
a2cf7d
+#!/usr/bin/python3
a2cf7d
+# Check ELF program headers for WX segments.
a2cf7d
+# Copyright (C) 2020 Free Software Foundation, Inc.
a2cf7d
+# This file is part of the GNU C Library.
a2cf7d
+#
a2cf7d
+# The GNU C Library is free software; you can redistribute it and/or
a2cf7d
+# modify it under the terms of the GNU Lesser General Public
a2cf7d
+# License as published by the Free Software Foundation; either
a2cf7d
+# version 2.1 of the License, or (at your option) any later version.
a2cf7d
+#
a2cf7d
+# The GNU C Library is distributed in the hope that it will be useful,
a2cf7d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
a2cf7d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
a2cf7d
+# Lesser General Public License for more details.
a2cf7d
+#
a2cf7d
+# You should have received a copy of the GNU Lesser General Public
a2cf7d
+# License along with the GNU C Library; if not, see
a2cf7d
+# <https://www.gnu.org/licenses/>.
a2cf7d
+
a2cf7d
+"""Check that the program headers do not contain write-exec segments."""
a2cf7d
+
a2cf7d
+import argparse
a2cf7d
+import os.path
a2cf7d
+import re
a2cf7d
+import sys
a2cf7d
+
a2cf7d
+# Regular expression to extract the RWE flags field.  The
a2cf7d
+# address/offset columns have varying width.
a2cf7d
+RE_LOAD = re.compile(
a2cf7d
+    r'^  LOAD +(?:0x[0-9a-fA-F]+ +){5}([R ][W ][ E]) +0x[0-9a-fA-F]+\n\Z')
a2cf7d
+
a2cf7d
+def process_file(path, inp, xfail):
a2cf7d
+    """Analyze one input file."""
a2cf7d
+
a2cf7d
+    errors = 0
a2cf7d
+    for line in inp:
a2cf7d
+        error = None
a2cf7d
+        if line.startswith('  LOAD '):
a2cf7d
+            match = RE_LOAD.match(line)
a2cf7d
+            if match is None:
a2cf7d
+                error = 'Invalid LOAD line'
a2cf7d
+            else:
a2cf7d
+                flags, = match.groups()
a2cf7d
+                if 'W' in flags and 'E' in flags:
a2cf7d
+                    if xfail:
a2cf7d
+                        print('{}: warning: WX segment (as expected)'.format(
a2cf7d
+                            path))
a2cf7d
+                    else:
a2cf7d
+                        error = 'WX segment'
a2cf7d
+
a2cf7d
+        if error is not None:
a2cf7d
+            print('{}: error: {}: {!r}'.format(path, error, line.strip()))
a2cf7d
+            errors += 1
a2cf7d
+
a2cf7d
+    if xfail and errors == 0:
a2cf7d
+        print('{}: warning: missing expected WX segment'.format(path))
a2cf7d
+    return errors
a2cf7d
+
a2cf7d
+
a2cf7d
+def main():
a2cf7d
+    """The main entry point."""
a2cf7d
+    parser = argparse.ArgumentParser(description=__doc__)
a2cf7d
+    parser.add_argument('--xfail',
a2cf7d
+                        help='Mark input files as XFAILed ("*" for all)',
a2cf7d
+                        type=str, default='')
a2cf7d
+    parser.add_argument('phdrs',
a2cf7d
+                        help='Files containing readelf -Wl output',
a2cf7d
+                        nargs='*')
a2cf7d
+    opts = parser.parse_args(sys.argv)
a2cf7d
+
a2cf7d
+    xfails = set(opts.xfail.split(' '))
a2cf7d
+    xfails_all = opts.xfail.strip() == '*'
a2cf7d
+
a2cf7d
+    errors = 0
a2cf7d
+    for path in opts.phdrs:
a2cf7d
+        xfail = ((os.path.basename(path) + '.phdrs') in xfails
a2cf7d
+                 or xfails_all)
a2cf7d
+        with open(path) as inp:
a2cf7d
+            errors += process_file(path, inp, xfail)
a2cf7d
+    if errors > 0:
a2cf7d
+        sys.exit(1)
a2cf7d
+
a2cf7d
+
a2cf7d
+if __name__ == '__main__':
a2cf7d
+    main()
a2cf7d
diff --git a/sysdeps/sparc/Makefile b/sysdeps/sparc/Makefile
a2cf7d
index 3f0c0964002560f0..a1004e819c9b0c38 100644
a2cf7d
--- a/sysdeps/sparc/Makefile
a2cf7d
+++ b/sysdeps/sparc/Makefile
a2cf7d
@@ -16,5 +16,14 @@ CPPFLAGS-crti.S += -fPIC
a2cf7d
 CPPFLAGS-crtn.S += -fPIC
a2cf7d
 endif
a2cf7d
 
a2cf7d
+ifeq ($(subdir),elf)
a2cf7d
+
a2cf7d
+# Lazy binding on SPARC rewrites the PLT sequence.  See the Solaris
a2cf7d
+# Linker and Libraries Guide, section SPARC: Procedure Linkage Table.
a2cf7d
+# <https://docs.oracle.com/cd/E19455-01/816-0559/chapter6-1236/index.html>
a2cf7d
+test-xfail-check-wx-segment = *
a2cf7d
+
a2cf7d
+endif # $(subdir) == elf
a2cf7d
+
a2cf7d
 # The assembler on SPARC needs the -fPIC flag even when it's assembler code.
a2cf7d
 ASFLAGS-.os += -fPIC
a2cf7d
diff --git a/sysdeps/unix/sysv/linux/hppa/Makefile b/sysdeps/unix/sysv/linux/hppa/Makefile
a2cf7d
index e1637f54f508c007..c89ec8318208205d 100644
a2cf7d
--- a/sysdeps/unix/sysv/linux/hppa/Makefile
a2cf7d
+++ b/sysdeps/unix/sysv/linux/hppa/Makefile
a2cf7d
@@ -3,9 +3,14 @@ ifeq ($(subdir),stdlib)
a2cf7d
 gen-as-const-headers += ucontext_i.sym
a2cf7d
 endif
a2cf7d
 
a2cf7d
+ifeq ($(subdir),elf)
a2cf7d
 # Supporting non-executable stacks on HPPA requires changes to both
a2cf7d
 # the Linux kernel and glibc. The kernel currently needs an executable
a2cf7d
 # stack for syscall restarts and signal returns.
a2cf7d
-ifeq ($(subdir),elf)
a2cf7d
 test-xfail-check-execstack = yes
a2cf7d
-endif
a2cf7d
+
a2cf7d
+# On hppa, the PLT is executable because it contains an executable
a2cf7d
+# trampoline used during lazy binding.
a2cf7d
+test-xfail-check-wx-segment = *
a2cf7d
+
a2cf7d
+endif # $(subdir) == elf