]> ruderich.org/simon Gitweb - blhc/blhc.git/commitdiff
Detect restore of -D_FORTIFY_SOURCE=2
authorSimon Ruderich <simon@ruderich.org>
Thu, 10 May 2018 15:17:44 +0000 (17:17 +0200)
committerSimon Ruderich <simon@ruderich.org>
Thu, 10 May 2018 15:17:44 +0000 (17:17 +0200)
NEWS
bin/blhc
t/logs/bad-cppflags
t/logs/good
t/tests.t

diff --git a/NEWS b/NEWS
index 206d4494b985df9230e761edc608671719eac283..1c8247f11a5eb97bac4409c72896cf237155cc53 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,6 +1,14 @@
 NEWS
 ====
 
 NEWS
 ====
 
+Version 0.XX
+------------
+
+- Detect restore of -D_FORTIFY_SOURCE=2 after it was overwritten by
+  -D_FORTIFY_SOURCE=0 or 1 or -U_FORTIFY_SOURCE; reported by Mike Hommey
+  (Debian bug #898332).
+
+
 Version 0.08
 ------------
 
 Version 0.08
 ------------
 
index 168a1699c21744707ff22708aebd49e7c44ef20d..f11ba111028c3949fd526a8f89eee47f316fba2c 100755 (executable)
--- a/bin/blhc
+++ b/bin/blhc
@@ -431,17 +431,33 @@ sub all_flags_used {
 sub cppflags_fortify_broken {
     my ($line, $missing_flags) = @_;
 
 sub cppflags_fortify_broken {
     my ($line, $missing_flags) = @_;
 
-    # This doesn't take the position into account, but is a simple solution.
-    # And if the build system tries to force -D_FORTIFY_SOURCE=0/1, something
-    # is wrong anyway.
-
-    if (any_flags_used($line, @def_cppflags_fortify_bad)) {
-        # $def_cppflags_fortify[0] must be -D_FORTIFY_SOURCE=2!
-        push @{$missing_flags}, $def_cppflags_fortify[0];
-        return 1;
+    if (not any_flags_used($line, @def_cppflags_fortify_bad)) {
+        return 0;
     }
 
     }
 
-    return 0;
+    # $def_cppflags_fortify[0] must be -D_FORTIFY_SOURCE=2!
+    my $fortify_source = $def_cppflags_fortify[0];
+
+    # Some build systems enable/disable fortify source multiple times, check
+    # the final result.
+    my $disable_pos = 0;
+    foreach my $flag (@def_cppflags_fortify_bad) {
+        while ($line =~ /$flag/g) {
+            if ($disable_pos < $+[0]) {
+                $disable_pos = $+[0];
+            }
+        }
+    }
+    my $enable_pos = 0;
+    while ($line =~ /$fortify_source/g) {
+        $enable_pos = $+[0];
+    }
+    if ($enable_pos > $disable_pos) {
+        return 0;
+    }
+
+    push @{$missing_flags}, $fortify_source;
+    return 1;
 }
 
 # Modifies $missing_flags_ref array.
 }
 
 # Modifies $missing_flags_ref array.
index a714a3220b7ef4bd614a2f5b49385930974e1ef5..43069b01551badf3535f6898ff301221ebc3ad44 100644 (file)
@@ -13,9 +13,8 @@ gcc -D_FORTIFY_SOURCE=0 -g -O2 -fstack-protector-strong -Wformat -Werror=format-
 gcc -D_FORTIFY_SOURCE=1 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -c test-b.c
 gcc -D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=1 -c test-c.c
 gcc -D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=0 -c test-d.c
 gcc -D_FORTIFY_SOURCE=1 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -c test-b.c
 gcc -D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=1 -c test-c.c
 gcc -D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=0 -c test-d.c
-gcc -D_FORTIFY_SOURCE=0 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -c test-e.c
-gcc -D_FORTIFY_SOURCE=1 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -c test-f.c
 
 gcc -U_FORTIFY_SOURCE   -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -c test-g.c
 
 gcc -U_FORTIFY_SOURCE   -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -c test-g.c
-gcc -U_FORTIFY_SOURCE   -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -c test-h.c
 gcc -D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -U_FORTIFY_SOURCE -c test-i.c
 gcc -D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -U_FORTIFY_SOURCE -c test-i.c
+gcc -D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -U_FORTIFY_SOURCE -c test-i.c
+gcc -D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -D_FORTIFY_SOURCE=1 -c test-i.c
index 80a9b7c7b5dbd65793251c602dd92f0af5f0894b..f8f3f81d5494beb24a26d739a7856877c4381c5d 100644 (file)
@@ -49,3 +49,8 @@ command --cc test
 
 gcc -MM test.c > test.d
 gcc -MM -MT test.d test.c
 
 gcc -MM test.c > test.d
 gcc -MM -MT test.d test.c
+
+gcc -U_FORTIFY_SOURCE   -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -c test-h.c
+gcc -D_FORTIFY_SOURCE=0 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -c test-e.c
+gcc -D_FORTIFY_SOURCE=1 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -c test-f.c
+gcc -D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -c test-i.c
index 052a1fda7f186c88bd05e4d08375d3bb701d82d7..5e7b7018a1364bcf26ebde31af20e9efaf39ecf5 100644 (file)
--- a/t/tests.t
+++ b/t/tests.t
@@ -503,11 +503,10 @@ CPPFLAGS missing (-D_FORTIFY_SOURCE=2): gcc -D_FORTIFY_SOURCE=0 -g -O2 -fstack-p
 CPPFLAGS missing (-D_FORTIFY_SOURCE=2): gcc -D_FORTIFY_SOURCE=1 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -c test-b.c
 CPPFLAGS missing (-D_FORTIFY_SOURCE=2): gcc -D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=1 -c test-c.c
 CPPFLAGS missing (-D_FORTIFY_SOURCE=2): gcc -D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=0 -c test-d.c
 CPPFLAGS missing (-D_FORTIFY_SOURCE=2): gcc -D_FORTIFY_SOURCE=1 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -c test-b.c
 CPPFLAGS missing (-D_FORTIFY_SOURCE=2): gcc -D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=1 -c test-c.c
 CPPFLAGS missing (-D_FORTIFY_SOURCE=2): gcc -D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=0 -c test-d.c
-CPPFLAGS missing (-D_FORTIFY_SOURCE=2): gcc -D_FORTIFY_SOURCE=0 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -c test-e.c
-CPPFLAGS missing (-D_FORTIFY_SOURCE=2): gcc -D_FORTIFY_SOURCE=1 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -c test-f.c
 CPPFLAGS missing (-D_FORTIFY_SOURCE=2): gcc -U_FORTIFY_SOURCE   -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -c test-g.c
 CPPFLAGS missing (-D_FORTIFY_SOURCE=2): gcc -U_FORTIFY_SOURCE   -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -c test-g.c
-CPPFLAGS missing (-D_FORTIFY_SOURCE=2): gcc -U_FORTIFY_SOURCE   -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -c test-h.c
 CPPFLAGS missing (-D_FORTIFY_SOURCE=2): gcc -D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -U_FORTIFY_SOURCE -c test-i.c
 CPPFLAGS missing (-D_FORTIFY_SOURCE=2): gcc -D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -U_FORTIFY_SOURCE -c test-i.c
+CPPFLAGS missing (-D_FORTIFY_SOURCE=2): gcc -D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -U_FORTIFY_SOURCE -c test-i.c
+CPPFLAGS missing (-D_FORTIFY_SOURCE=2): gcc -D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -D_FORTIFY_SOURCE=1 -c test-i.c
 ';
 
 is_blhc 'bad-cppflags', '--ignore-flag -D_FORTIFY_SOURCE=2', 0,
 ';
 
 is_blhc 'bad-cppflags', '--ignore-flag -D_FORTIFY_SOURCE=2', 0,