]> ruderich.org/simon Gitweb - blhc/blhc.git/blobdiff - bin/blhc
Add --debian to handle PIE flags like buildd mode
[blhc/blhc.git] / bin / blhc
index 6afff57ff521485c9c6a4db9437bc826ea26b18d..daf5bd22c2bfa25212e87285247f5a57ea3792ad 100755 (executable)
--- a/bin/blhc
+++ b/bin/blhc
@@ -602,6 +602,7 @@ my @option_ignore_arch_line = ();
 my $option_all              = 0;
 my $option_arch             = undef;
 my $option_buildd           = 0;
+my $option_debian           = 0;
    $option_color            = 0;
 if (not Getopt::Long::GetOptions(
             'help|h|?'           => \$option_help,
@@ -620,6 +621,7 @@ if (not Getopt::Long::GetOptions(
             'color'              => \$option_color,
             'arch=s'             => \$option_arch,
             'buildd'             => \$option_buildd,
+            'debian'             => \$option_debian,
         )) {
     require Pod::Usage;
     Pod::Usage::pod2usage(2);
@@ -750,6 +752,13 @@ foreach my $file (@ARGV) {
     # use a large enough default.
     my $parallel = 10;
 
+    # Don't check for PIE flags if automatically applied by the compiler. Only
+    # used in buildd and Debian mode.
+    my $disable_harden_pie = 0;
+    if ($option_debian) {
+        $disable_harden_pie = 1;
+    }
+
     while (my $line = <$fh>) {
         # Detect architecture automatically unless overridden. For buildd logs
         # only, doesn't use the dpkg-buildpackage header. Necessary to ignore
@@ -772,6 +781,10 @@ foreach my $file (@ARGV) {
         # flags are not checked.
         #
         # Strong stack protector is used since dpkg 1.17.11.
+        #
+        # Recent GCC versions automatically use PIE (only on supported
+        # architectures) and dpkg respects this properly since 1.18.15 and
+        # doesn't pass PIE flags manually.
         if ($option_buildd
                 and index($line, 'Toolchain package versions: ') == 0) {
             require Dpkg::Version;
@@ -786,6 +799,9 @@ foreach my $file (@ARGV) {
                 if (Dpkg::Version::version_compare($1, '1.17.11') >= 0) {
                     $disable_strong = 0;
                 }
+                if (Dpkg::Version::version_compare($1, '1.18.15') >= 0) {
+                    $disable_harden_pie = 1;
+                }
             }
 
             if ($disable) {
@@ -1040,7 +1056,7 @@ foreach my $file (@ARGV) {
 
     # Option or auto detected.
     if ($arch) {
-        # The following was partially copied from dpkg-dev 1.18.15
+        # The following was partially copied from dpkg-dev 1.18.24
         # (/usr/share/perl5/Dpkg/Vendor/Debian.pm, _add_hardening_flags()),
         # copyright RaphaĆ«l Hertzog <hertzog@debian.org>, Kees Cook
         # <kees@debian.org>, Canonical, Ltd. licensed under GPL version 2 or
@@ -1056,6 +1072,11 @@ foreach my $file (@ARGV) {
             (undef, $os, $cpu) = Dpkg::Arch::debarch_to_debtriplet($arch);
         }
 
+        my %builtin_pie_arch = map { $_ => 1 } qw(
+            amd64 arm64 armel armhf i386 kfreebsd-amd64 kfreebsd-i386
+            mips mipsel mips64el ppc64el s390x sparc sparc64
+        );
+
         # Disable unsupported hardening options.
         if ($os !~ /^(?:linux|kfreebsd|knetbsd|hurd)$/
                 or $cpu =~ /^(?:hppa|avr32)$/) {
@@ -1069,6 +1090,10 @@ foreach my $file (@ARGV) {
             $harden_relro   = 0;
             $harden_bindnow = 0;
         }
+
+        if ($disable_harden_pie and exists $builtin_pie_arch{$arch}) {
+            $harden_pie = 0;
+        }
     }
 
     # Default values.
@@ -1468,6 +1493,12 @@ error).
 
 =back
 
+=item B<--debian>
+
+Apply Debian-specific settings. At the moment this only disables checking for
+PIE which is automatically applied by Debian's GCC and no longer requires a
+compiler command line argument.
+
 =item B<--color>
 
 Use colored (ANSI) output for warning messages.