]> ruderich.org/simon Gitweb - blhc/blhc.git/blob - bin/blhc
Clarify that no output means no flags are missing.
[blhc/blhc.git] / bin / blhc
1 #!/usr/bin/perl
2
3 # Build log hardening check, checks build logs for missing hardening flags.
4
5 # Copyright (C) 2012  Simon Ruderich
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
21 use strict;
22 use warnings;
23
24 use Getopt::Long ();
25 use Text::ParseWords ();
26
27 our $VERSION = '0.02';
28
29
30 # CONSTANTS/VARIABLES
31
32 # Regex to catch compiler commands.
33 my $cc_regex = qr/
34     (?<!\s-)               # ignore options, e.g. "-c++" [sic!] (used by swig)
35     (?<!\.)                # ignore file names, e.g. "test.gcc"
36     (?:cc|gcc|g\+\+|c\+\+)
37     (?:-[\d.]+)?           # version suffix, e.g. "gcc-4.6"
38     /x;
39 # Full regex which matches the complete compiler name. Used in a few places to
40 # prevent false negatives.
41 my $cc_regex_full = qr/
42     (?:[a-z0-9_]+-(?:linux-|kfreebsd-)?gnu(?:eabi|eabihf)?-)?
43     $cc_regex
44     /x;
45 # Regex to catch (GCC) compiler warnings.
46 my $warning_regex = qr/^(.+?):(\d+):\d+: warning: (.+?) \[(.+?)\]$/;
47
48 # List of source file extensions which require preprocessing.
49 my @source_preprocess_compile_cpp = (
50     # C++
51     qw( cc cp cxx cpp CPP c++ C ),
52     # Objective-C++
53     qw( mm M ),
54 );
55 my @source_preprocess_compile = (
56     # C
57     qw( c ),
58     # Objective-C
59     qw( m ),
60     # (Objective-)C++
61     @source_preprocess_compile_cpp,
62     # Fortran
63     qw( F FOR fpp FPP FTN F90 F95 F03 F08 ),
64 );
65 my @source_preprocess_no_compile = (
66     # Assembly
67     qw( S sx ),
68 );
69 my @source_preprocess = (
70     @source_preprocess_compile,
71     @source_preprocess_no_compile,
72 );
73 # List of source file extensions which don't require preprocessing.
74 my @source_no_preprocess_compile_cpp = (
75     # C++
76     qw( ii ),
77     # Objective-C++
78     qw( mii ),
79 );
80 my @source_no_preprocess_compile = (
81     # C
82     qw( i ),
83     # (Objective-)C++
84     @source_no_preprocess_compile_cpp,
85     # Objective-C
86     qw( mi ),
87     # Fortran
88     qw( f for ftn f90 f95 f03 f08 ),
89 );
90 my @source_no_preprocess_no_compile = (
91     # Assembly
92     qw( s ),
93 );
94 my @source_no_preprocess = (
95     @source_no_preprocess_compile,
96     @source_no_preprocess_no_compile,
97 );
98 # List of header file extensions which require preprocessing.
99 my @header_preprocess = (
100     # C, C++, Objective-C, Objective-C++
101     qw( h ),
102     # C++
103     qw( hh H hp hxx hpp HPP h++ tcc ),
104 );
105
106 # Hashes for fast extensions lookup to check if a file falls in one of these
107 # categories.
108 my %extensions_no_preprocess = map { $_ => 1 } (
109     @source_no_preprocess,
110 );
111 my %extensions_preprocess = map { $_ => 1 } (
112     @header_preprocess,
113     @source_preprocess,
114 );
115 my %extensions_compile_link = map { $_ => 1 } (
116     @source_preprocess,
117     @source_no_preprocess,
118 );
119 my %extensions_compile = map { $_ => 1 } (
120     @source_preprocess_compile,
121     @source_no_preprocess_compile,
122 );
123 my %extensions_no_compile = map { $_ => 1 } (
124     @source_preprocess_no_compile,
125     @source_no_preprocess_no_compile,
126 );
127 my %extensions_compile_cpp = map { $_ => 1 } (
128     @source_preprocess_compile_cpp,
129     @source_no_preprocess_compile_cpp,
130 );
131 my %extension = map { $_ => 1 } (
132     @source_no_preprocess,
133     @header_preprocess,
134     @source_preprocess,
135 );
136
137 # Regexp to match file extensions.
138 my $file_extension_regex = qr/
139     \s
140     \S+             # Filename without extension.
141     \.
142     ([^\/\\.,;:\s]+)# File extension.
143     (?=\s|\\)       # At end of word. Can't use \b because some files have non
144                     # word characters at the end and because \b matches double
145                     # extensions (like .cpp.o). Works always as all lines are
146                     # terminated with "\n".
147     /x;
148
149 # Expected (hardening) flags. All flags are used as regexps.
150 my @def_cflags = (
151     '-g',
152     '-O(?:2|3)',
153 );
154 my @def_cflags_format = (
155     '-Wformat',
156     '-Werror=format-security', # implies -Wformat-security
157 );
158 my @def_cflags_fortify = (
159     # fortify needs at least -O1, but -O2 is recommended anyway
160 );
161 my @def_cflags_stack = (
162     '-fstack-protector',
163     '--param=ssp-buffer-size=4',
164 );
165 my @def_cflags_pie = (
166     '-fPIE',
167 );
168 my @def_cxxflags = (
169     @def_cflags,
170 );
171 # @def_cxxflags_* is the same as @def_cflags_*.
172 my @def_cppflags = ();
173 my @def_cppflags_fortify = (
174     '-D_FORTIFY_SOURCE=2',
175 );
176 my @def_ldflags = ();
177 my @def_ldflags_relro = (
178     '-Wl,(?:-z,)?relro',
179 );
180 my @def_ldflags_bindnow = (
181     '-Wl,(?:-z,)?now',
182 );
183 my @def_ldflags_pie = (
184     '-fPIE',
185     '-pie',
186 );
187 my @def_ldflags_pic = (
188     '-fPIC',
189     '-fpic',
190     '-shared',
191 );
192 # References to all flags checked by the parser.
193 my @flag_refs = (
194     \@def_cflags,
195     \@def_cflags_format,
196     \@def_cflags_fortify,
197     \@def_cflags_stack,
198     \@def_cflags_pie,
199     \@def_cxxflags,
200     \@def_cppflags,
201     \@def_cppflags_fortify,
202     \@def_ldflags,
203     \@def_ldflags_relro,
204     \@def_ldflags_bindnow,
205     \@def_ldflags_pie,
206 );
207 # References to all used flags.
208 my @flag_refs_all = (
209     @flag_refs,
210     \@def_ldflags_pic,
211 );
212 # Renaming rules for the output so the regex parts are not visible. Also
213 # stores string values of flag regexps above, see compile_flag_regexp().
214 my %flag_renames = (
215     '-O(?:2|3)'         => '-O2',
216     '-Wl,(?:-z,)?relro' => '-Wl,-z,relro',
217     '-Wl,(?:-z,)?now'   => '-Wl,-z,now',
218 );
219
220 my %exit_code = (
221     no_compiler_commands => 1 << 0,
222     # used by POD::Usage => 1 << 1,
223     non_verbose_build    => 1 << 2,
224     flags_missing        => 1 << 3,
225     hardening_wrapper    => 1 << 4,
226     invalid_cmake        => 1 << 5,
227 );
228
229 my %buildd_tag = (
230     no_compiler_commands => 'I-no-compiler-commands',
231     non_verbose_build    => 'W-compiler-flags-hidden',
232     flags_missing        => 'W-dpkg-buildflags-missing',
233     hardening_wrapper    => 'I-hardening-wrapper-used',
234     invalid_cmake        => 'I-invalid-cmake-used',
235 );
236
237 # Statistics of missing flags and non-verbose build commands. Used for
238 # $option_buildd.
239 my %statistics = (
240     preprocess          => 0,
241     preprocess_missing  => 0,
242     compile             => 0,
243     compile_missing     => 0,
244     compile_cpp         => 0,
245     compile_cpp_missing => 0,
246     link                => 0,
247     link_missing        => 0,
248     commands            => 0,
249     commands_nonverbose => 0,
250 );
251
252 # Use colored (ANSI) output?
253 my $option_color;
254
255
256 # FUNCTIONS
257
258 sub error_flags {
259     my ($message, $missing_flags_ref, $flag_renames_ref, $line) = @_;
260
261     # Get string value of qr//-escaped regexps and if requested rename them.
262     my @missing_flags = map {
263             $flag_renames_ref->{$_}
264         } @{$missing_flags_ref};
265
266     my $flags = join ' ', @missing_flags;
267     printf '%s (%s)%s %s',
268            error_color($message, 'red'), $flags, error_color(':', 'yellow'),
269            $line;
270 }
271 sub error_non_verbose_build {
272     my ($line) = @_;
273
274     printf '%s%s %s',
275            error_color('NONVERBOSE BUILD', 'red'),
276            error_color(':', 'yellow'),
277            $line;
278 }
279 sub error_invalid_cmake {
280     my ($version) = @_;
281
282     printf "%s%s %s\n",
283             error_color('INVALID CMAKE', 'red'),
284             error_color(':', 'yellow'),
285             $version;
286 }
287 sub error_hardening_wrapper {
288     printf "%s%s %s\n",
289             error_color('HARDENING WRAPPER', 'red'),
290             error_color(':', 'yellow'),
291             'no checks possible, aborting';
292 }
293 sub error_color {
294     my ($message, $color) = @_;
295
296     if ($option_color) {
297         return Term::ANSIColor::colored($message, $color);
298     } else {
299         return $message;
300     }
301 }
302
303 sub any_flags_used {
304     my ($line, @flags) = @_;
305
306     foreach my $flag (@flags) {
307         return 1 if $line =~ /$flag/;
308     }
309
310     return 0;
311 }
312 sub all_flags_used {
313     my ($line, $missing_flags_ref, @flags) = @_;
314
315     my @missing_flags = ();
316     foreach my $flag (@flags) {
317         if (not $line =~ /$flag/) {
318             push @missing_flags, $flag;
319         }
320     }
321
322     return 1 if scalar @missing_flags == 0;
323
324     @{$missing_flags_ref} = @missing_flags;
325     return 0;
326 }
327
328 # Modifies $missing_flags_ref array.
329 sub pic_pie_conflict {
330     my ($line, $pie, $missing_flags_ref, @flags_pie) = @_;
331
332     return 0 if not $pie;
333     return 0 if not any_flags_used($line, @def_ldflags_pic);
334
335     my %flags = map { $_ => 1 } @flags_pie;
336
337     # Remove all PIE flags from @missing_flags as they are not required with
338     # -fPIC.
339     my @result = grep {
340         not exists $flags{$_}
341     } @{$missing_flags_ref};
342     @{$missing_flags_ref} = @result;
343
344     # We got a conflict when no flags are left, thus only PIE flags were
345     # missing. If other flags were missing abort because the conflict is not
346     # the problem.
347     return scalar @result == 0;
348 }
349
350 sub is_non_verbose_build {
351     my ($line, $next_line, $skip_ref) = @_;
352
353     if (not (index($line, 'checking if you want to see long compiling messages... no') == 0
354                 or $line =~ /^\s*\[?(?:CC|CCLD|C\+\+|CXX|CXXLD|LD|LINK)\]?\s+(.+?)$/
355                 or $line =~ /^\s*[Cc]ompiling\s+(.+?)(?:\.\.\.)?$/
356                 or $line =~ /^\s*[Bb]uilding (?:program|shared library)\s+(.+?)$/
357                 or $line =~ /^\s*\[[\d ]+%\] Building (?:C|CXX) object (.+?)$/)) {
358         return 0;
359     }
360
361     # False positives.
362     #
363     # C++ compiler setting.
364     return 0 if $line =~ /^\s*C\+\+.+?:\s+(?:yes|no)\s*$/;
365     # "Compiling" with no file name.
366     if ($line =~ /^\s*[Cc]ompiling\s+(.+?)(?:\.\.\.)?$/) {
367         # $file_extension_regex may need spaces around the filename.
368         return 0 if not " $1 " =~ /$file_extension_regex/o;
369     }
370
371     my $file = $1;
372
373     # On the first pass we only check if this line is verbose or not.
374     return 1 if not defined $next_line;
375
376     # Second pass, we have access to the next line.
377     ${$skip_ref} = 0;
378
379     # CMake and other build systems print the non-verbose messages also when
380     # building verbose. If a compiler and the file name occurs in the next
381     # line, treat it as verbose build.
382     if (defined $file) {
383         # Get filename, we can't use the complete path as only parts of it are
384         # used in the real compiler command.
385         $file =~ m{/([^/\s]+)$};
386         $file = $1;
387
388         if (index($next_line, $file) != -1 and $next_line =~ /$cc_regex/o) {
389             # We still have to skip the current line as it doesn't contain any
390             # compiler commands.
391             ${$skip_ref} = 1;
392             return 0;
393         }
394     }
395
396     return 1;
397 }
398
399 sub remove_flags {
400     my ($flag_refs_ref, $flag_renames_ref, @flags) = @_;
401
402     my %removes = map { $_ => 1 } @flags;
403     foreach my $flags (@{$flag_refs_ref}) {
404         @{$flags} = grep {
405             # Flag found as string.
406             not exists $removes{$_}
407             # Flag found as string representation of regexp.
408                 and (not defined $flag_renames_ref->{$_}
409                         or not exists $removes{$flag_renames_ref->{$_}})
410         } @{$flags};
411     }
412 }
413
414 sub compile_flag_regexp {
415     my ($flag_renames_ref, @flags) = @_;
416
417     my @result = ();
418     foreach my $flag (@flags) {
419         # Store flag name in replacement string for correct flags in messages
420         # with qr//ed flag regexps.
421         $flag_renames_ref->{qr/\s$flag(?:\s|\\)/}
422             = (exists $flag_renames_ref->{$flag})
423                 ? $flag_renames_ref->{$flag}
424                 : $flag;
425
426         # Compile flag regexp for faster execution.
427         push @result, qr/\s$flag(?:\s|\\)/;
428     }
429     return @result;
430 }
431
432 sub extension_found {
433     my ($extensions_ref, @extensions) = @_;
434
435     my $found = 0;
436     foreach my $extension (@extensions) {
437         if (exists $extensions_ref->{$extension}) {
438             $found = 1;
439             last;
440         }
441     }
442     return $found;
443 }
444
445
446 # MAIN
447
448 # Parse command line arguments.
449 my $option_help             = 0;
450 my $option_version          = 0;
451 my $option_pie              = 0;
452 my $option_bindnow          = 0;
453 my @option_ignore_arch      = ();
454 my @option_ignore_flag      = ();
455 my @option_ignore_arch_flag = ();
456 my @option_ignore_line      = ();
457 my @option_ignore_arch_line = ();
458 my $option_all              = 0;
459 my $option_arch             = undef;
460 my $option_buildd           = 0;
461    $option_color            = 0;
462 if (not Getopt::Long::GetOptions(
463             'help|h|?'           => \$option_help,
464             'version'            => \$option_version,
465             # Hardening options.
466             'pie'                => \$option_pie,
467             'bindnow'            => \$option_bindnow,
468             'all'                => \$option_all,
469             # Ignore.
470             'ignore-arch=s'      => \@option_ignore_arch,
471             'ignore-flag=s'      => \@option_ignore_flag,
472             'ignore-arch-flag=s' => \@option_ignore_arch_flag,
473             'ignore-line=s'      => \@option_ignore_line,
474             'ignore-arch-line=s' => \@option_ignore_arch_line,
475             # Misc.
476             'color'              => \$option_color,
477             'arch=s'             => \$option_arch,
478             'buildd'             => \$option_buildd,
479         )) {
480     require Pod::Usage;
481     Pod::Usage::pod2usage(2);
482 }
483 if ($option_help) {
484     require Pod::Usage;
485     Pod::Usage::pod2usage(1);
486 }
487 if ($option_version) {
488     print "blhc $VERSION  Copyright (C) 2012  Simon Ruderich
489
490 This program is free software: you can redistribute it and/or modify
491 it under the terms of the GNU General Public License as published by
492 the Free Software Foundation, either version 3 of the License, or
493 (at your option) any later version.
494
495 This program is distributed in the hope that it will be useful,
496 but WITHOUT ANY WARRANTY; without even the implied warranty of
497 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
498 GNU General Public License for more details.
499
500 You should have received a copy of the GNU General Public License
501 along with this program.  If not, see <http://www.gnu.org/licenses/>.
502 ";
503     exit 0;
504 }
505
506 # Arguments missing.
507 if (scalar @ARGV == 0) {
508     require Pod::Usage;
509     Pod::Usage::pod2usage(2);
510 }
511
512 # Don't load Term::ANSIColor in buildd mode because Term::ANSIColor is not
513 # installed on Debian's buildds.
514 if (not $option_buildd) {
515     require Term::ANSIColor;
516 }
517
518 if ($option_all) {
519     $option_pie     = 1;
520     $option_bindnow = 1;
521 }
522
523 # Precompiled ignores for faster lookup.
524 my %option_ignore_arch_flag = ();
525 my %option_ignore_arch_line = ();
526
527 # Strip flags which should be ignored.
528 if (scalar @option_ignore_flag > 0) {
529     remove_flags(\@flag_refs, \%flag_renames, @option_ignore_flag);
530 }
531 # Same for arch specific ignore flags, but only prepare here.
532 if (scalar @option_ignore_arch_flag > 0) {
533     foreach my $ignore (@option_ignore_arch_flag) {
534         my ($ignore_arch, $ignore_flag) = split ':', $ignore, 2;
535
536         if (not $ignore_arch or not $ignore_flag) {
537             printf STDERR 'Value "%s" invalid for option ignore-arch-flag '
538                         . '("arch:flag" expected)' . "\n", $ignore;
539             require Pod::Usage;
540             Pod::Usage::pod2usage(2);
541         }
542
543         push @{$option_ignore_arch_flag{$ignore_arch}}, $ignore_flag;
544     }
545 }
546
547 # Precompile all flag regexps. any_flags_used(), all_flags_used() get a lot
548 # faster with this.
549 foreach my $flags (@flag_refs_all) {
550     @{$flags} = compile_flag_regexp(\%flag_renames, @{$flags});
551 }
552
553 # Precompile ignore line regexps, also anchor at beginning and end of line.
554 foreach my $ignore (@option_ignore_line) {
555     $ignore = qr/^$ignore$/;
556 }
557 # Same for arch specific ignore lines.
558 if (scalar @option_ignore_arch_line > 0) {
559     foreach my $ignore (@option_ignore_arch_line) {
560         my ($ignore_arch, $ignore_line) = split ':', $ignore, 2;
561
562         if (not $ignore_arch or not $ignore_line) {
563             printf STDERR 'Value "%s" invalid for option ignore-arch-line '
564                         . '("arch:line" expected)' . "\n", $ignore;
565             require Pod::Usage;
566             Pod::Usage::pod2usage(2);
567         }
568
569         push @{$option_ignore_arch_line{$ignore_arch}}, qr/^$ignore_line$/;
570     }
571 }
572
573 # Final exit code.
574 my $exit = 0;
575
576 FILE:
577 foreach my $file (@ARGV) {
578     print "checking '$file'...\n" if scalar @ARGV > 1;
579
580     open my $fh, '<', $file or die $!;
581
582     # Architecture of this file.
583     my $arch = $option_arch;
584
585     # Hardening options. Not all architectures support all hardening options.
586     my $harden_format  = 1;
587     my $harden_fortify = 1;
588     my $harden_stack   = 1;
589     my $harden_relro   = 1;
590     my $harden_bindnow = $option_bindnow; # defaults to 0
591     my $harden_pie     = $option_pie;     # defaults to 0
592
593     while (my $line = <$fh>) {
594         # Detect architecture automatically unless overridden. For buildd logs
595         # only, doesn't use the dpkg-buildpackage header. Necessary to ignore
596         # build logs which aren't built (wrong architecture, build error,
597         # etc.).
598         if (not $arch
599                 and $line =~ /^Architecture: (.+)$/) {
600             $arch = $1;
601         }
602
603         # dpkg-buildflags only provides hardening flags since 1.16.1, don't
604         # check for hardening flags in buildd mode if an older dpkg-dev is
605         # used. Default flags (-g -O2) are still checked.
606         #
607         # Packages which were built before 1.16.1 but used their own hardening
608         # flags are not checked.
609         if ($option_buildd
610                 and index($line, 'Toolchain package versions: ') == 0) {
611             require Dpkg::Version;
612             if (not $line =~ /\bdpkg-dev_(\S+)/
613                     or Dpkg::Version::version_compare($1, '1.16.1') < 0) {
614                 $harden_format  = 0;
615                 $harden_fortify = 0;
616                 $harden_stack   = 0;
617                 $harden_relro   = 0;
618                 $harden_bindnow = 0;
619                 $harden_pie     = 0;
620             }
621         }
622
623         # The following two versions of CMake in Debian obeyed CPPFLAGS, but
624         # this was later dropped because upstream rejected the patch. Thus
625         # build logs with these versions will have fortify hardening flags
626         # enabled, even though they may be not correctly set and are missing
627         # when build with later CMake versions. Thanks to Aron Xu for letting
628         # me know.
629         if (index($line, 'Package versions: ') == 0
630                 and $line =~ /\bcmake_(\S+)/
631                 and ($1 eq '2.8.7-1' or $1 eq '2.8.7-2')) {
632             if (not $option_buildd) {
633                 error_invalid_cmake($1);
634             } else {
635                 print "$buildd_tag{invalid_cmake} $1\n";
636             }
637             $exit |= $exit_code{invalid_cmake};
638         }
639
640         # If hardening wrapper is used (wraps calls to gcc and adds hardening
641         # flags automatically) we can't perform any checks, abort.
642         if (index($line, 'Build-Depends: ') == 0
643                 and $line =~ /\bhardening-wrapper\b/) {
644             if (not $option_buildd) {
645                 error_hardening_wrapper();
646             } else {
647                 print "$buildd_tag{hardening_wrapper}\n";
648             }
649             $exit |= $exit_code{hardening_wrapper};
650             next FILE;
651         }
652
653         # We skip over unimportant lines at the beginning of the log to
654         # prevent false positives.
655         last if index($line, 'dpkg-buildpackage: ') == 0;
656     }
657
658     # Input lines, contain only the lines with compiler commands.
659     my @input = ();
660
661     my $continuation = 0;
662     my $complete_line = undef;
663     while (my $line = <$fh>) {
664         # And stop at the end of the build log. Package details (reported by
665         # the buildd logs) are not important for us. This also prevents false
666         # positives.
667         last if $line =~ /^Build finished at \d{8}-\d{4}$/;
668
669         # Detect architecture automatically unless overridden.
670         if (not $arch
671                 and $line =~ /^dpkg-buildpackage: host architecture (.+)$/) {
672             $arch = $1;
673         }
674
675         # Ignore compiler warnings for now.
676         next if $line =~ /$warning_regex/o;
677
678         if (not $option_buildd and index($line, "\033") != -1) { # esc
679             # Remove all ANSI color sequences which are sometimes used in
680             # non-verbose builds.
681             $line = Term::ANSIColor::colorstrip($line);
682             # Also strip '\0xf' (delete previous character), used by Elinks'
683             # build system.
684             $line =~ s/\x0f//g;
685             # And "ESC(B" which seems to be used on armhf and hurd (not sure
686             # what it does).
687             $line =~ s/\033\(B//g;
688         }
689
690         # Check if this line indicates a non verbose build.
691         my $non_verbose = is_non_verbose_build($line);
692
693         # One line may contain multiple commands (";"). Treat each one as
694         # single line. parse_line() is slow, only use it when necessary.
695         my @line = (index($line, ';') == -1)
696                  ? ($line)
697                  : map {
698                        # Ensure newline at the line end - necessary for
699                        # correct parsing later.
700                        $_ =~ s/\s+$//;
701                        $_ .= "\n";
702                    } Text::ParseWords::parse_line(';', 1, $line);
703         foreach my $line (@line) {
704             if ($continuation) {
705                 $continuation = 0;
706
707                 # Join lines, but leave the "\" in place so it's clear where
708                 # the original line break was.
709                 chomp $complete_line;
710                 $complete_line .= ' ' . $line;
711             }
712             # Line continuation, line ends with "\".
713             if ($line =~ /\\$/) {
714                 $continuation = 1;
715                 # Start line continuation.
716                 if (not defined $complete_line) {
717                     $complete_line = $line;
718                 }
719                 next;
720             }
721
722             # Use the complete line if a line continuation occurred.
723             if (defined $complete_line) {
724                 $line = $complete_line;
725                 $complete_line = undef;
726             }
727
728             # Ignore lines with no compiler commands.
729             next if not $non_verbose
730                     and not $line =~ /\b$cc_regex(?:\s|\\)/o;
731             # Ignore lines with no filenames with extensions. May miss some
732             # non-verbose builds (e.g. "gcc -o test" [sic!]), but shouldn't be
733             # a problem as the log will most likely contain other non-verbose
734             # commands which are detected.
735             next if not $non_verbose
736                     and not $line =~ /$file_extension_regex/o;
737
738             # Ignore false positives.
739             #
740             # `./configure` output.
741             next if not $non_verbose
742                     and $line =~ /^(?:checking|[Cc]onfigure:) /;
743             next if $line =~ /^\s*(?:Host\s+)?(?:C(?:\+\+)?\s+)?
744                                 [Cc]ompiler[\s.]*:?\s+
745                                 /xo;
746             next if $line =~ /^\s*(?:- )?(?:HOST_)?(?:CC|CXX)\s*=\s*$cc_regex_full\s*$/o;
747             # `moc-qt4`, contains '-I/usr/share/qt4/mkspecs/linux-g++' (or
748             # similar for other architectures) which gets recognized as a
749             # compiler line. Ignore it.
750             next if $line =~ m{^/usr/bin/moc-qt4
751                                \s.+\s
752                                -I/usr/share/qt4/mkspecs/[a-z]+-g\++(?:-64)?
753                                \s}x;
754
755             # Check if additional hardening options were used. Used to ensure
756             # they are used for the complete build.
757             $harden_pie     = 1 if any_flags_used($line, @def_cflags_pie,
758                                                          @def_ldflags_pie);
759             $harden_bindnow = 1 if any_flags_used($line, @def_ldflags_bindnow);
760
761             push @input, $line;
762         }
763     }
764
765     close $fh or die $!;
766
767     # Ignore arch if requested.
768     if (scalar @option_ignore_arch > 0 and $arch) {
769         foreach my $ignore (@option_ignore_arch) {
770             if ($arch eq $ignore) {
771                 print "ignoring architecture '$arch'\n";
772                 next FILE;
773             }
774         }
775     }
776
777     if (scalar @input == 0) {
778         if (not $option_buildd) {
779             print "No compiler commands!\n";
780         } else {
781             print "$buildd_tag{no_compiler_commands}\n";
782         }
783         $exit |= $exit_code{no_compiler_commands};
784         next FILE;
785     }
786
787     if ($option_buildd) {
788         $statistics{commands} += scalar @input;
789     }
790
791     # Option or auto detected.
792     if ($arch) {
793         # The following was partially copied from dpkg-dev 1.16.1.2
794         # (/usr/share/perl5/Dpkg/Vendor/Debian.pm, add_hardening_flags()),
795         # copyright Raphaël Hertzog <hertzog@debian.org>, Kees Cook
796         # <kees@debian.org>, Canonical, Ltd. licensed under GPL version 2 or
797         # later. Keep it in sync.
798
799         require Dpkg::Arch;
800         my ($abi, $os, $cpu) = Dpkg::Arch::debarch_to_debtriplet($arch);
801
802         # Disable unsupported hardening options.
803         if ($cpu =~ /^(?:ia64|alpha|mips|mipsel|hppa)$/ or $arch eq 'arm') {
804             $harden_stack = 0;
805         }
806         if ($cpu =~ /^(?:ia64|hppa|avr32)$/) {
807             $harden_relro   = 0;
808             $harden_bindnow = 0;
809         }
810     }
811
812     # Default values.
813     my @cflags   = @def_cflags;
814     my @cxxflags = @def_cxxflags;
815     my @cppflags = @def_cppflags;
816     my @ldflags  = @def_ldflags;
817     # Check the specified hardening options, same order as dpkg-buildflags.
818     if ($harden_pie) {
819         @cflags   = (@cflags,   @def_cflags_pie);
820         @cxxflags = (@cxxflags, @def_cflags_pie);
821         @ldflags  = (@ldflags,  @def_ldflags_pie);
822     }
823     if ($harden_stack) {
824         @cflags   = (@cflags,   @def_cflags_stack);
825         @cxxflags = (@cxxflags, @def_cflags_stack);
826     }
827     if ($harden_fortify) {
828         @cflags   = (@cflags,   @def_cflags_fortify);
829         @cxxflags = (@cxxflags, @def_cflags_fortify);
830         @cppflags = (@cppflags, @def_cppflags_fortify);
831     }
832     if ($harden_format) {
833         @cflags   = (@cflags,   @def_cflags_format);
834         @cxxflags = (@cxxflags, @def_cflags_format);
835     }
836     if ($harden_relro) {
837         @ldflags = (@ldflags, @def_ldflags_relro);
838     }
839     if ($harden_bindnow) {
840         @ldflags = (@ldflags, @def_ldflags_bindnow);
841     }
842
843     # Ignore flags for this arch if requested.
844     if ($arch and exists $option_ignore_arch_flag{$arch}) {
845         my @flag_refs = (\@cflags, \@cxxflags, \@cppflags, \@ldflags);
846
847         remove_flags(\@flag_refs,
848                      \%flag_renames,
849                      @{$option_ignore_arch_flag{$arch}});
850     }
851
852     my @ignore_line = @option_ignore_line;
853     # Ignore lines for this arch if requested.
854     if ($arch and exists $option_ignore_arch_line{$arch}) {
855         @ignore_line = (@ignore_line, @{$option_ignore_arch_line{$arch}});
856     }
857
858 LINE:
859     for (my $i = 0; $i < scalar @input; $i++) {
860         my $line = $input[$i];
861
862         # Ignore line if requested.
863         foreach my $ignore (@ignore_line) {
864             next LINE if $line =~ /$ignore/;
865         }
866
867         my $skip = 0;
868         if (is_non_verbose_build($line, $input[$i + 1], \$skip)) {
869             if (not $option_buildd) {
870                 error_non_verbose_build($line);
871             } else {
872                 $statistics{commands_nonverbose}++;
873             }
874             $exit |= $exit_code{non_verbose_build};
875             next;
876         }
877         # Even if it's a verbose build, we might have to skip this line.
878         next if $skip;
879
880         # Remove everything until and including the compiler command. Makes
881         # checks easier and faster.
882         $line =~ s/^.*?$cc_regex//o;
883         # "([...] test.c)" is not detected as 'test.c' - fix this by removing
884         # the brace and similar characters.
885         $line =~ s/['")]+$//;
886
887         # Skip unnecessary tests when only preprocessing.
888         my $flag_preprocess = 0;
889
890         my $dependency = 0;
891         my $preprocess = 0;
892         my $compile    = 0;
893         my $link       = 0;
894
895         # Preprocess, compile, assemble.
896         if ($line =~ /\s(-E|-S|-c)\b/) {
897             $preprocess      = 1;
898             $flag_preprocess = 1 if $1 eq '-E';
899             $compile         = 1 if $1 eq '-S' or $1 eq '-c';
900         # Dependency generation for Makefiles. The other flags (-MF -MG -MP
901         # -MT -MQ) are always used with -M/-MM.
902         } elsif ($line =~ /\s(?:-M|-MM)\b/) {
903             $dependency = 1;
904         # Otherwise assume we are linking.
905         } else {
906             $link = 1;
907         }
908
909         # -MD/-MMD also cause dependency generation, but they don't imply -E!
910         if ($line =~ /\s(?:-MD|-MMD)\b/) {
911             $dependency      = 0;
912             $flag_preprocess = 0;
913         }
914
915         # Dependency generation for Makefiles, no preprocessing or other flags
916         # needed.
917         next if $dependency;
918
919         # Get all file extensions on this line.
920         my @extensions = $line =~ /$file_extension_regex/go;
921         # Ignore all unknown extensions to speedup the search below.
922         @extensions = grep { exists $extension{$_} } @extensions;
923
924         # These file types don't require preprocessing.
925         if (extension_found(\%extensions_no_preprocess, @extensions)) {
926             $preprocess = 0;
927         }
928         # These file types require preprocessing.
929         if (extension_found(\%extensions_preprocess, @extensions)) {
930             $preprocess = 1;
931         }
932
933         # If there are source files then it's compiling/linking in one step
934         # and we must check both. We only check for source files here, because
935         # header files cause too many false positives.
936         if (not $flag_preprocess
937                 and extension_found(\%extensions_compile_link, @extensions)) {
938             # Assembly files don't need CFLAGS.
939             if (not extension_found(\%extensions_compile, @extensions)
940                     and extension_found(\%extensions_no_compile, @extensions)) {
941                 $compile = 0;
942             # But the rest does.
943             } else {
944                 $compile = 1;
945             }
946         }
947
948         # Assume CXXFLAGS are required when a C++ file is specified in the
949         # compiler line.
950         my $compile_cpp = 0;
951         if ($compile
952                 and extension_found(\%extensions_compile_cpp, @extensions)) {
953             $compile     = 0;
954             $compile_cpp = 1;
955         }
956
957         if ($option_buildd) {
958             $statistics{preprocess}++  if $preprocess;
959             $statistics{compile}++     if $compile;
960             $statistics{compile_cpp}++ if $compile_cpp;
961             $statistics{link}++        if $link;
962         }
963
964         # Check hardening flags.
965         my @missing;
966         if ($compile and not all_flags_used($line, \@missing, @cflags)
967                 # Libraries linked with -fPIC don't have to (and can't) be
968                 # linked with -fPIE as well. It's no error if only PIE flags
969                 # are missing.
970                 and not pic_pie_conflict($line, $harden_pie, \@missing, @def_cflags_pie)
971                 # Assume dpkg-buildflags returns the correct flags.
972                 and index($line, '`dpkg-buildflags --get CFLAGS`') == -1) {
973             if (not $option_buildd) {
974                 error_flags('CFLAGS missing', \@missing, \%flag_renames, $input[$i]);
975             } else {
976                 $statistics{compile_missing}++;
977             }
978             $exit |= $exit_code{flags_missing};
979         } elsif ($compile_cpp and not all_flags_used($line, \@missing, @cflags)
980                 # Libraries linked with -fPIC don't have to (and can't) be
981                 # linked with -fPIE as well. It's no error if only PIE flags
982                 # are missing.
983                 and not pic_pie_conflict($line, $harden_pie, \@missing, @def_cflags_pie)
984                 # Assume dpkg-buildflags returns the correct flags.
985                 and index($line, '`dpkg-buildflags --get CXXFLAGS`') == -1) {
986             if (not $option_buildd) {
987                 error_flags('CXXFLAGS missing', \@missing, \%flag_renames, $input[$i]);
988             } else {
989                 $statistics{compile_cpp_missing}++;
990             }
991             $exit |= $exit_code{flags_missing};
992         }
993         if ($preprocess and not all_flags_used($line, \@missing, @cppflags)
994                 # Assume dpkg-buildflags returns the correct flags.
995                 and index($line, '`dpkg-buildflags --get CPPFLAGS`') == -1) {
996             if (not $option_buildd) {
997                 error_flags('CPPFLAGS missing', \@missing, \%flag_renames, $input[$i]);
998             } else {
999                 $statistics{preprocess_missing}++;
1000             }
1001             $exit |= $exit_code{flags_missing};
1002         }
1003         if ($link and not all_flags_used($line, \@missing, @ldflags)
1004                 # Same here, -fPIC conflicts with -fPIE.
1005                 and not pic_pie_conflict($line, $harden_pie, \@missing, @def_ldflags_pie)
1006                 # Assume dpkg-buildflags returns the correct flags.
1007                 and index($line, '`dpkg-buildflags --get LDFLAGS`') == -1) {
1008             if (not $option_buildd) {
1009                 error_flags('LDFLAGS missing', \@missing, \%flag_renames, $input[$i]);
1010             } else {
1011                 $statistics{link_missing}++;
1012             }
1013             $exit |= $exit_code{flags_missing};
1014         }
1015     }
1016 }
1017
1018 # Print statistics for buildd mode, only output in this mode.
1019 if ($option_buildd) {
1020     my @warning;
1021
1022     if ($statistics{preprocess_missing}) {
1023         push @warning, sprintf 'CPPFLAGS %d (of %d)',
1024                                $statistics{preprocess_missing},
1025                                $statistics{preprocess};
1026     }
1027     if ($statistics{compile_missing}) {
1028         push @warning, sprintf 'CFLAGS %d (of %d)',
1029                                $statistics{compile_missing},
1030                                $statistics{compile};
1031     }
1032     if ($statistics{compile_cpp_missing}) {
1033         push @warning, sprintf 'CXXFLAGS %d (of %d)',
1034                                $statistics{compile_cpp_missing},
1035                                $statistics{compile_cpp};
1036     }
1037     if ($statistics{link_missing}) {
1038         push @warning, sprintf 'LDFLAGS %d (of %d)',
1039                                $statistics{link_missing},
1040                                $statistics{link};
1041     }
1042     if (scalar @warning) {
1043         local $" = ', '; # array join string
1044         print "$buildd_tag{flags_missing} @warning missing\n";
1045     }
1046
1047     if ($statistics{commands_nonverbose}) {
1048         printf "$buildd_tag{non_verbose_build} %d (of %d) hidden\n",
1049                $statistics{commands_nonverbose},
1050                $statistics{commands},
1051     }
1052 }
1053
1054
1055 exit $exit;
1056
1057
1058 __END__
1059
1060 =head1 NAME
1061
1062 blhc - build log hardening check, checks build logs for missing hardening flags
1063
1064 =head1 SYNOPSIS
1065
1066 B<blhc> [I<options>] I<< <dpkg-buildpackage build log file>.. >>
1067
1068 =head1 DESCRIPTION
1069
1070 blhc is a small tool which checks build logs for missing hardening flags. It's
1071 licensed under the GPL 3 or later.
1072
1073 It's designed to check build logs generated by Debian's dpkg-buildpackage (or
1074 tools using dpkg-buildpackage like pbuilder or the official buildd build logs)
1075 to help maintainers detect missing hardening flags in their packages.
1076
1077 If there's no output, no flags are missing and the build log is fine.
1078
1079 =head1 OPTIONS
1080
1081 =over 8
1082
1083 =item B<--all>
1084
1085 Force check for all +all (+pie, +bindnow) hardening flags. By default it's
1086 auto detected.
1087
1088 =item B<--arch> I<architecture>
1089
1090 Set the specific architecture (e.g. amd64, armel, etc.), automatically
1091 disables hardening flags not available on this architecture. Is detected
1092 automatically if dpkg-buildpackage is used.
1093
1094 =item B<--bindnow>
1095
1096 Force check for all +bindnow hardening flags. By default it's auto detected.
1097
1098 =item B<--buildd>
1099
1100 Special mode for buildds when automatically parsing log files. The following
1101 changes are in effect:
1102
1103 =over 2
1104
1105 =item *
1106
1107 Print tags instead of normal warnings, see L</"BUILDD TAGS"> for a list of
1108 possible tags.
1109
1110 =item *
1111
1112 Don't check hardening flags in old log files (if dpkg-dev << 1.16.1 is
1113 detected).
1114
1115 =item *
1116
1117 Don't require Term::ANSIColor.
1118
1119 =back
1120
1121 =item B<--color>
1122
1123 Use colored (ANSI) output for warning messages.
1124
1125 =item B<--ignore-arch> I<arch>
1126
1127 Ignore build logs from architectures matching I<arch>. I<arch> is a string.
1128
1129 Used to prevent false positives. This option can be specified multiple times.
1130
1131 =item B<--ignore-arch-flag> I<arch>:I<flag>
1132
1133 Like B<--ignore-flag>, but only ignore flag on I<arch>.
1134
1135 =item B<--ignore-arch-line> I<arch>:I<line>
1136
1137 Like B<--ignore-line>, but only ignore line on I<arch>.
1138
1139 =item B<--ignore-flag> I<flag>
1140
1141 Don't print an error when the specific flag is missing in a compiler line.
1142 I<flag> is a string.
1143
1144 Used to prevent false positives. This option can be specified multiple times.
1145
1146 =item B<--ignore-line> I<regex>
1147
1148 Ignore lines matching the given Perl regex. I<regex> is automatically anchored
1149 at the beginning and end of the line to prevent false negatives.
1150
1151 B<NOTE>: Not the input lines are checked, but the lines which are displayed in
1152 warnings (which have line continuation resolved).
1153
1154 Used to prevent false positives. This option can be specified multiple times.
1155
1156 =item B<--pie>
1157
1158 Force check for all +pie hardening flags. By default it's auto detected.
1159
1160 =item B<-h -? --help>
1161
1162 Print available options.
1163
1164 =item B<--version>
1165
1166 Print version number and license.
1167
1168 =back
1169
1170 Auto detection for B<--pie> and B<--bindnow> only works if at least one
1171 command uses the required hardening flag (e.g. -fPIE). Then it's required for
1172 all other commands as well.
1173
1174 =head1 EXAMPLES
1175
1176 Normal usage, parse a single log file.
1177
1178     blhc path/to/log/file
1179
1180 If there's no output, no flags are missing and the build log is fine.
1181
1182 Parse multiple log files. The exit code is ORed over all files.
1183
1184     blhc path/to/directory/with/log/files/*
1185
1186 Don't treat missing C<-g> as error:
1187
1188     blhc --ignore-flag -g path/to/log/file
1189
1190 Don't treat missing C<-pie> on kfreebsd-amd64 as error:
1191
1192     blhc --ignore-arch-flag kfreebsd-amd64:-pie path/to/log/file
1193
1194 Ignore lines consisting exactly of C<./script gcc file> which would cause a
1195 false positive.
1196
1197     blhc --ignore-line '\./script gcc file' path/to/log/file
1198
1199 Ignore lines matching C<./script gcc file> somewhere in the line.
1200
1201     blhc --ignore-line '.*\./script gcc file.*' path/to/log/file
1202
1203 Use blhc with pbuilder.
1204
1205     pbuilder path/to/package.dsc | tee path/log/file
1206     blhc path/to/file || echo flags missing
1207
1208 =head1 BUILDD TAGS
1209
1210 The following tags are used in I<--buildd> mode. In braces the additional data
1211 which is displayed.
1212
1213 =over 2
1214
1215 =item B<I-hardening-wrapper-used>
1216
1217 The package uses hardening-wrapper which intercepts calls to gcc and adds
1218 hardening flags. The build log doesn't contain any hardening flags and thus
1219 can't be checked by blhc.
1220
1221 =item B<W-compiler-flags-hidden> (summary of hidden lines)
1222
1223 Build log contains lines which hide the real compiler flags. For example:
1224
1225     CC test-a.c
1226     CC test-b.c
1227     CC test-c.c
1228     LD test
1229
1230 Most of the time either C<export V=1> or C<export verbose=1> in
1231 F<debian/rules> fixes builds with hidden compiler flags. Sometimes C<.SILENT>
1232 in a F<Makefile> must be removed. And as last resort the F<Makefile> must be
1233 patched to remove the C<@>s hiding the real compiler commands.
1234
1235 =item B<W-dpkg-buildflags-missing> (summary of missing flags)
1236
1237 CPPFLAGS, CFLAGS, CXXFLAGS, LDFLAGS missing.
1238
1239 =item B<I-invalid-cmake-used> (version)
1240
1241 By default CMake ignores CPPFLAGS thus missing those hardening flags. Debian
1242 patched CMake in versions 2.8.7-1 and 2.8.7-2 to respect CPPFLAGS, but this
1243 patch was rejected by upstream and later reverted in Debian. Thus those two
1244 versions show correct usage of CPPFLAGS even if the package doesn't correctly
1245 handle them (for example by passing them to CFLAGS). To prevent false
1246 negatives just blacklist those two versions.
1247
1248 =item B<I-no-compiler-commands>
1249
1250 No compiler commands were detected. Either the log contains none or they were
1251 not correctly detected by blhc (please report the bug in this case).
1252
1253 =back
1254
1255 =head1 EXIT STATUS
1256
1257 The exit status is a "bit mask", each listed status is ORed when the error
1258 condition occurs to get the result.
1259
1260 =over 4
1261
1262 =item B<0>
1263
1264 Success.
1265
1266 =item B<1>
1267
1268 No compiler commands were found.
1269
1270 =item B<2>
1271
1272 Invalid arguments/options given to blhc.
1273
1274 =item B<4>
1275
1276 Non verbose build.
1277
1278 =item B<8>
1279
1280 Missing hardening flags.
1281
1282 =item B<16>
1283
1284 Hardening wrapper detected, no tests performed.
1285
1286 =item B<32>
1287
1288 Invalid CMake version used. See B<I-invalid-cmake-used> under L</"BUILDD
1289 TAGS"> for a detailed explanation.
1290
1291 =back
1292
1293 =head1 AUTHOR
1294
1295 Simon Ruderich, E<lt>simon@ruderich.orgE<gt>
1296
1297 Thanks to to Bernhard R. Link E<lt>brlink@debian.orgE<gt> and Jaria Alto
1298 E<lt>jari.aalto@cante.netE<gt> for their valuable input and suggestions.
1299
1300 =head1 COPYRIGHT AND LICENSE
1301
1302 Copyright (C) 2012 by Simon Ruderich
1303
1304 This program is free software: you can redistribute it and/or modify
1305 it under the terms of the GNU General Public License as published by
1306 the Free Software Foundation, either version 3 of the License, or
1307 (at your option) any later version.
1308
1309 This program is distributed in the hope that it will be useful,
1310 but WITHOUT ANY WARRANTY; without even the implied warranty of
1311 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1312 GNU General Public License for more details.
1313
1314 You should have received a copy of the GNU General Public License
1315 along with this program.  If not, see <http://www.gnu.org/licenses/>.
1316
1317 =head1 SEE ALSO
1318
1319 L<hardening-check(1)>, L<dpkg-buildflags(1)>
1320
1321 =cut