]> ruderich.org/simon Gitweb - blhc/blhc.git/blob - bin/blhc
Fix indentation.
[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 Term::ANSIColor ();
26 use Text::ParseWords ();
27
28 our $VERSION = '0.01';
29
30
31 # CONSTANTS/VARIABLES
32
33 # Regex to catch compiler commands.
34 my $cc_regex = qr/(?:[a-z0-9_]+-(?:linux-|kfreebsd-)?gnu(?:eabi|eabihf)?-)?
35                   (?<!\.)(?:cc|gcc|g\+\+|c\+\+)
36                   (?:-[\d.]+)?/x;
37 # Regex to catch (GCC) compiler warnings.
38 my $warning_regex = qr/^(.+?):([0-9]+):[0-9]+: warning: (.+?) \[(.+?)\]$/;
39
40 # List of source file extensions which require preprocessing.
41 my @source_preprocess_compile_cpp = (
42     # C++
43     qw( cc cp cxx cpp CPP c++ C ),
44     # Objective-C++
45     qw( mm Mr),
46 );
47 my @source_preprocess_compile = (
48     # C
49     qw( c ),
50     # Objective-C
51     qw( m ),
52     # (Objective-)C++
53     @source_preprocess_compile_cpp,
54     # Fortran
55     qw( F FOR fpp FPP FTN F90 F95 F03 F08 ),
56 );
57 my @source_preprocess_no_compile = (
58     # Assembly
59     qw( s ),
60 );
61 my @source_preprocess = (
62     @source_preprocess_compile,
63     @source_preprocess_no_compile,
64 );
65 # List of source file extensions which don't require preprocessing.
66 my @source_no_preprocess_compile_cpp = (
67     # C++
68     qw( ii ),
69     # Objective-C++
70     qw( mii ),
71 );
72 my @source_no_preprocess_compile = (
73     # C
74     qw( i ),
75     # (Objective-)C++
76     @source_no_preprocess_compile_cpp,
77     # Objective-C
78     qw( mi ),
79     # Fortran
80     qw( f for ftn f90 f95 f03 f08 ),
81 );
82 my @source_no_preprocess_no_compile = (
83     # Assembly
84     qw( S sx ),
85 );
86 my @source_no_preprocess = (
87     @source_no_preprocess_compile,
88     @source_no_preprocess_no_compile,
89 );
90 # List of header file extensions which require preprocessing.
91 my @header_preprocess = (
92     # C, C++, Objective-C, Objective-C++
93     qw( h ),
94     # C++
95     qw( hh H hp hxx hpp HPP h++ tcc ),
96 );
97
98 # Hashes for fast extensions lookup to check if a file falls in one of these
99 # categories.
100 my %extensions_no_preprocess = map { $_ => 1 } (
101     @source_no_preprocess,
102 );
103 my %extensions_preprocess = map { $_ => 1 } (
104     @header_preprocess,
105     @source_preprocess,
106 );
107 my %extensions_compile_link = map { $_ => 1 } (
108     @source_preprocess,
109     @source_no_preprocess,
110 );
111 my %extensions_compile = map { $_ => 1 } (
112     @source_preprocess_compile,
113     @source_no_preprocess_compile,
114 );
115 my %extensions_no_compile = map { $_ => 1 } (
116     @source_preprocess_no_compile,
117     @source_no_preprocess_no_compile,
118 );
119 my %extensions_compile_cpp = map { $_ => 1 } (
120     @source_preprocess_compile_cpp,
121     @source_no_preprocess_compile_cpp,
122 );
123 my %extension = map { $_ => 1 } (
124     @source_no_preprocess,
125     @source_no_preprocess_compile,
126     @source_no_preprocess_compile_cpp,
127     @source_no_preprocess_no_compile,
128     @header_preprocess,
129     @source_preprocess,
130     @source_preprocess_compile,
131     @source_preprocess_compile_cpp,
132     @source_preprocess_no_compile,
133 );
134
135 # Regexp to match file extensions.
136 my $file_extension_regex = qr/
137     \s
138     \S+         # Filename without extension.
139     \.
140     ([^\\.\s]+) # File extension.
141     (?=\s|\\)   # At end of word. Can't use \b because some files have non
142                 # word characters at the end and because \b matches double
143                 # extensions (like .cpp.o). Works always as all lines are
144                 # terminated with "\n".
145     /x;
146
147 # Expected (hardening) flags. All flags are used as regexps.
148 my @def_cflags = (
149     '-g',
150     '-O(?:2|3)',
151 );
152 my @def_cflags_format = (
153     '-Wformat',
154     '-Wformat-security',
155     '-Werror=format-security',
156 );
157 my @def_cflags_fortify = (
158     # fortify needs at least -O1, but -O2 is recommended anyway
159 );
160 my @def_cflags_stack = (
161     '-fstack-protector',
162     '--param=ssp-buffer-size=4',
163 );
164 my @def_cflags_pie = (
165     '-fPIE',
166 );
167 my @def_cxxflags = (
168     '-g',
169     '-O(?:2|3)',
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 # Renaming rules for the output so the regex parts are not visible.
188 my %flag_renames = (
189     '-O(?:2|3)'       => '-O2',
190     '-Wl,(-z,)?relro' => '-Wl,-z,relro',
191     '-Wl,(-z,)?now'   => '-Wl,-z,now',
192 );
193
194 # Use colored (ANSI) output?
195 my $option_color;
196
197
198 # FUNCTIONS
199
200 sub error_flags {
201     my ($message, $missing_flags_ref, $flag_renames_ref, $line) = @_;
202
203     # Rename flags if requested.
204     my @missing_flags = map {
205         (exists $flag_renames_ref->{$_})
206             ? $flag_renames_ref->{$_}
207             : $_
208     } @{$missing_flags_ref};
209
210     my $flags = join ' ', @missing_flags;
211     printf "%s (%s)%s %s",
212            error_color($message, 'red'), $flags, error_color(':', 'yellow'),
213            $line;
214 }
215 sub error_non_verbose_build {
216     my ($line) = @_;
217
218     printf "%s%s %s",
219            error_color('NONVERBOSE BUILD', 'red'),
220            error_color(':', 'yellow'),
221            $line;
222 }
223 sub error_hardening_wrapper {
224     printf "%s%s %s\n",
225             error_color('HARDENING WRAPPER', 'red'),
226             error_color(':', 'yellow'),
227             'no checks possible, aborting';
228 }
229 sub error_color {
230     my ($message, $color) = @_;
231
232     if ($option_color) {
233         return Term::ANSIColor::colored($message, $color);
234     } else {
235         return $message;
236     }
237 }
238
239 sub any_flags_used {
240     my ($line, @flags) = @_;
241
242     foreach my $flag (@flags) {
243         return 1 if $line =~ /\s$flag(?:\s|\\)/;
244     }
245
246     return 0;
247 }
248 sub all_flags_used {
249     my ($line, $missing_flags_ref, @flags) = @_;
250
251     my @missing_flags = ();
252     foreach my $flag (@flags) {
253         if ($line !~ /\s$flag(?:\s|\\)/) {
254             push @missing_flags, $flag;
255         }
256     }
257
258     return 1 if scalar @missing_flags == 0;
259
260     @{$missing_flags_ref} = @missing_flags;
261     return 0;
262 }
263
264 # Modifies $missing_flags_ref array.
265 sub pic_pie_conflict {
266     my ($line, $pie, $missing_flags_ref, @flags_pie) = @_;
267
268     return 0 if not $pie;
269     return 0 if not any_flags_used($line, ('-fPIC', '-fpic'));
270
271     my %flags = map { $_ => 1 } @flags_pie;
272
273     # Remove all PIE flags from @missing_flags as they are not required with
274     # -fPIC.
275     my @result = grep {
276         not exists $flags{$_}
277     } @{$missing_flags_ref};
278     @{$missing_flags_ref} = @result;
279
280     # We got a conflict when no flags are left, thus only PIE flags were
281     # missing. If other flags were missing abort because the conflict is not
282     # the problem.
283     return scalar @result == 0;
284 }
285
286 sub is_non_verbose_build {
287     my ($line, $next_line, $skip_ref) = @_;
288
289     if (not ($line =~ /^checking if you want to see long compiling messages\.\.\. no/
290                 or $line =~ /^\s*\[?(?:CC|CCLD|CXX|CXXLD|LD|LINK)\]?\s+(.+?)$/
291                 or $line =~ /^\s*(?:C|c)ompiling\s+(.+?)(?:\.\.\.)?$/
292                 or $line =~ /^\s*(?:B|b)uilding (?:program|shared library)\s+(.+?)$/
293                 or $line =~ /^\s*\[[\d ]+%\] Building (?:C|CXX) object (.+?)$/)) {
294         return 0;
295     }
296
297     my $file = $1;
298
299     # On the first pass we only check if this line is verbose or not.
300     return 1 if not defined $next_line;
301
302     # Second pass, we have access to the next line.
303     ${$skip_ref} = 0;
304
305     # CMake and other build systems print the non-verbose messages also when
306     # building verbose. If a compiler and the file name occurs in the next
307     # line, treat it as verbose build.
308     if (defined $file) {
309         # Get filename, we can't use the complete path as only parts of it are
310         # used in the real compiler command.
311         $file =~ m{/([a-zA-Z0-9._-]+)$};
312         $file = $1;
313
314         if ($next_line =~ /\Q$file\E/ and $next_line =~ /$cc_regex/o) {
315             # We still have to skip the current line as it doesn't contain any
316             # compiler commands.
317             ${$skip_ref} = 1;
318             return 0;
319         }
320     }
321
322     return 1;
323 }
324
325 sub extension_found {
326     my ($extensions_ref, @extensions) = @_;
327
328     my $found = 0;
329     foreach my $extension (@extensions) {
330         if (exists $extensions_ref->{$extension}) {
331             $found = 1;
332             last;
333         }
334     }
335     return $found;
336 }
337
338
339 # MAIN
340
341 # Parse command line arguments.
342 my $option_help    = 0;
343 my $option_version = 0;
344 my $option_pie     = 0;
345 my $option_bindnow = 0;
346 my $option_all     = 0;
347 my $option_arch    = undef;
348 my $option_buildd  = 0;
349    $option_color   = 0;
350 if (not Getopt::Long::GetOptions(
351             'help|h|?' => \$option_help,
352             'version'  => \$option_version,
353             # Hardening options.
354             'pie'      => \$option_pie,
355             'bindnow'  => \$option_bindnow,
356             'all'      => \$option_all,
357             # Misc.
358             'color'    => \$option_color,
359             'arch=s'   => \$option_arch,
360             'buildd'   => \$option_buildd,
361         )) {
362     require Pod::Usage;
363     Pod::Usage::pod2usage(2);
364 }
365 if ($option_help) {
366     require Pod::Usage;
367     Pod::Usage::pod2usage(1);
368 }
369 if ($option_version) {
370     print "blhc $VERSION  Copyright (C) 2012  Simon Ruderich
371
372 This program is free software: you can redistribute it and/or modify
373 it under the terms of the GNU General Public License as published by
374 the Free Software Foundation, either version 3 of the License, or
375 (at your option) any later version.
376
377 This program is distributed in the hope that it will be useful,
378 but WITHOUT ANY WARRANTY; without even the implied warranty of
379 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
380 GNU General Public License for more details.
381
382 You should have received a copy of the GNU General Public License
383 along with this program.  If not, see <http://www.gnu.org/licenses/>.
384 ";
385     exit 0;
386 }
387
388 if ($option_all) {
389     $option_pie     = 1;
390     $option_bindnow = 1;
391 }
392
393 # Final exit code.
394 my $exit = 0;
395
396 FILE: foreach my $file (@ARGV) {
397     open my $fh, '<', $file or die "$!: $file";
398
399     # Hardening options. Not all architectures support all hardening options.
400     my $harden_format  = 1;
401     my $harden_fortify = 1;
402     my $harden_stack   = 1;
403     my $harden_relro   = 1;
404     my $harden_bindnow = $option_bindnow; # defaults to 0
405     my $harden_pie     = $option_pie;     # defaults to 0
406
407     # Input lines, contain only the lines with compiler commands.
408     my @input = ();
409
410     my $start = 0;
411     my $continuation = 0;
412     my $complete_line = undef;
413     while (my $line = <$fh>) {
414         # dpkg-buildflags only provides hardening flags since 1.16.1, don't check
415         # for hardening flags in buildd mode if an older dpkg-dev is used. Default
416         # flags (-g -O2) are still checked.
417         #
418         # Packages which were built before 1.16.1 but used their own hardening
419         # flags are not checked.
420         if ($option_buildd and not $start
421                 and $line =~ /^Toolchain package versions: /) {
422             require Dpkg::Version;
423             if ($line !~ /dpkg-dev_(\S+)/
424                     or Dpkg::Version::version_compare($1, '1.16.1') < 0) {
425                 $harden_format  = 0;
426                 $harden_fortify = 0;
427                 $harden_stack   = 0;
428                 $harden_relro   = 0;
429                 $harden_bindnow = 0;
430                 $harden_pie     = 0;
431             }
432         }
433
434         # If hardening wrapper is used (wraps calls to gcc and adds hardening
435         # flags automatically) we can't perform any checks, abort.
436         if (not $start and $line =~ /^Build-Depends: .*\bhardening-wrapper\b/) {
437             error_hardening_wrapper();
438             $exit |= 1 << 4;
439             next FILE;
440         }
441
442         # We skip over unimportant lines at the beginning of the log to prevent
443         # false positives.
444         $start = 1 if $line =~ /^dpkg-buildpackage:/;
445         next if not $start;
446         # And stop at the end of the build log. Package details (reported by the
447         # buildd logs) are not important for us. This also prevents false
448         # positives.
449         last if $line =~ /^Build finished at \d{8}-\d{4}$/;
450
451         # Detect architecture automatically unless overridden.
452         if (not $option_arch
453                 and $line =~ /^dpkg-buildpackage: host architecture (.+)$/) {
454             $option_arch = $1;
455         }
456
457         # Ignore compiler warnings for now.
458         next if $line =~ /$warning_regex/o;
459
460         if ($line =~ /\033/) { # esc
461             # Remove all ANSI color sequences which are sometimes used in
462             # non-verbose builds.
463             $line = Term::ANSIColor::colorstrip($line);
464             # Also strip '\0xf' (delete previous character), used by Elinks' build
465             # system.
466             $line =~ s/\x0f//g;
467             # And "ESC(B" which seems to be used on armhf and hurd (not sure what
468             # it does).
469             $line =~ s/\033\(B//g;
470         }
471
472         # Check if this line indicates a non verbose build.
473         my $non_verbose = is_non_verbose_build($line);
474
475         # One line may contain multiple commands (";"). Treat each one as single
476         # line. parse_line() is slow, only use it when necessary.
477         my @line = (not $line =~ /;/)
478                  ? ($line)
479                  : map {
480                        # Ensure newline at the line end - necessary for correct
481                        # parsing later.
482                        $_ =~ s/\s+$//;
483                        $_ .= "\n";
484                    } Text::ParseWords::parse_line(';', 1, $line);
485         foreach $line (@line) {
486             if ($continuation) {
487                 $continuation = 0;
488
489                 # Join lines, but leave the "\" in place so it's clear where the
490                 # original line break was.
491                 chomp $complete_line;
492                 $complete_line .= ' ' . $line;
493             }
494             # Line continuation, line ends with "\".
495             if ($line =~ /\\\s*$/) {
496                 $continuation = 1;
497                 # Start line continuation.
498                 if (not defined $complete_line) {
499                     $complete_line = $line;
500                 }
501                 next;
502             }
503
504             if (not $continuation) {
505                 # Use the complete line if a line continuation occurred.
506                 if (defined $complete_line) {
507                     $line = $complete_line;
508                     $complete_line = undef;
509                 }
510
511                 # Ignore lines with no compiler commands.
512                 next if $line !~ /\b$cc_regex(?:\s|\\)/o and not $non_verbose;
513
514                 # Ignore false positives.
515                 #
516                 # `./configure` output.
517                 next if not $non_verbose
518                         and $line =~ /^(?:checking|(?:C|c)onfigure:) /;
519                 next if $line =~ /^\s*(?:Host\s+)?(?:C\s+)?
520                                    (?:C|c)ompiler[\s.]*:?\s+
521                                    $cc_regex
522                                    (?:\s-std=[a-z0-9:+]+)?\s*$
523                                  /xo
524                         or $line =~ /^\s*(?:- )?(?:HOST_)?(?:CC|CXX)\s*=\s*$cc_regex\s*$/o
525                         or $line =~ /^\s*-- Check for working (?:C|CXX) compiler: /
526                         or $line =~ /^\s*(?:echo )?Using [A-Z_]+\s*=\s*/;
527                 # `make` output.
528                 next if $line =~ /^Making [a-z]+ in \S+/; # e.g. "[...] in c++"
529
530                 # Check if additional hardening options were used. Used to ensure
531                 # they are used for the complete build.
532                 $harden_pie     = 1 if any_flags_used($line, @def_cflags_pie, @def_ldflags_pie);
533                 $harden_bindnow = 1 if any_flags_used($line, @def_ldflags_bindnow);
534
535                 push @input, $line;
536             }
537         }
538     }
539
540     close $fh;
541
542     if (scalar @input == 0) {
543         print "No compiler commands!\n";
544         $exit |= 1;
545         next FILE;
546     }
547
548     # Option or auto detected.
549     if ($option_arch) {
550         # The following was partially copied from dpkg-dev 1.16.1.2
551         # (/usr/share/perl5/Dpkg/Vendor/Debian.pm, add_hardening_flags()),
552         # copyright Raphaël Hertzog <hertzog@debian.org>, Kees Cook
553         # <kees@debian.org>, Canonical, Ltd. licensed under GPL version 2 or
554         # later. Keep it in sync.
555
556         require Dpkg::Arch;
557         my ($abi, $os, $cpu) = Dpkg::Arch::debarch_to_debtriplet($option_arch);
558
559         # Disable unsupported hardening options.
560         if ($cpu =~ /^(ia64|alpha|mips|mipsel|hppa)$/ or $option_arch eq 'arm') {
561             $harden_stack = 0;
562         }
563         if ($cpu =~ /^(ia64|hppa|avr32)$/) {
564             $harden_relro   = 0;
565             $harden_bindnow = 0;
566         }
567     }
568
569     # Default values.
570     my @cflags   = @def_cflags;
571     my @cxxflags = @def_cxxflags;
572     my @cppflags = @def_cppflags;
573     my @ldflags  = @def_ldflags;
574     # Check the specified hardening options, same order as dpkg-buildflags.
575     if ($harden_pie) {
576         @cflags   = (@cflags,   @def_cflags_pie);
577         @cxxflags = (@cxxflags, @def_cflags_pie);
578         @ldflags  = (@ldflags,  @def_ldflags_pie);
579     }
580     if ($harden_stack) {
581         @cflags   = (@cflags,   @def_cflags_stack);
582         @cxxflags = (@cxxflags, @def_cflags_stack);
583     }
584     if ($harden_fortify) {
585         @cflags   = (@cflags,   @def_cflags_fortify);
586         @cxxflags = (@cxxflags, @def_cflags_fortify);
587         @cppflags = (@cppflags, @def_cppflags_fortify);
588     }
589     if ($harden_format) {
590         @cflags   = (@cflags,   @def_cflags_format);
591         @cxxflags = (@cxxflags, @def_cflags_format);
592     }
593     if ($harden_relro) {
594         @ldflags = (@ldflags, @def_ldflags_relro);
595     }
596     if ($harden_bindnow) {
597         @ldflags = (@ldflags, @def_ldflags_bindnow);
598     }
599
600     for (my $i = 0; $i < scalar @input; $i++) {
601         my $line = $input[$i];
602
603         my $skip = 0;
604         if (is_non_verbose_build($line, $input[$i + 1], \$skip)) {
605             error_non_verbose_build($line);
606             $exit |= 1 << 2;
607             next;
608         }
609         # Even if it's a verbose build, we might have to skip this line.
610         next if $skip;
611
612         # Remove everything until and including the compiler command. Makes checks
613         # easier and faster.
614         $line =~ s/^.*?$cc_regex//o;
615
616         # Skip unnecessary tests when only preprocessing.
617         my $flag_preprocess = 0;
618
619         my $preprocess = 0;
620         my $compile    = 0;
621         my $link       = 0;
622
623         # Preprocess, compile, assemble.
624         if ($line =~ /\s(-E|-S|-c)\b/) {
625             $preprocess      = 1;
626             $flag_preprocess = 1 if $1 eq '-E';
627             $compile         = 1 if $1 eq '-S' or $1 eq '-c';
628         # Otherwise assume we are linking.
629         } else {
630             $link = 1;
631         }
632
633         # Get all file extensions on this line.
634         my @extensions = $line =~ /$file_extension_regex/go;
635         # Ignore all unknown extensions to speedup the search below.
636         @extensions = grep { exists $extension{$_} } @extensions;
637
638         # These file types don't require preprocessing.
639         if (extension_found(\%extensions_no_preprocess, @extensions)) {
640             $preprocess = 0;
641         }
642         # These file types require preprocessing.
643         if (extension_found(\%extensions_preprocess, @extensions)) {
644             $preprocess = 1;
645         }
646
647         # If there are source files then it's compiling/linking in one step and we
648         # must check both. We only check for source files here, because header
649         # files cause too many false positives.
650         if (not $flag_preprocess
651                 and extension_found(\%extensions_compile_link, @extensions)) {
652             # Assembly files don't need CFLAGS.
653             if (not extension_found(\%extensions_compile, @extensions)
654                     and extension_found(\%extensions_no_compile, @extensions)) {
655                 $compile = 0;
656             # But the rest does.
657             } else {
658                 $compile = 1;
659             }
660         }
661
662         # Assume CXXFLAGS are required when a C++ file is specified in the
663         # compiler line.
664         my $compile_cpp = 0;
665         if ($compile
666                 and extension_found(\%extensions_compile_cpp, @extensions)) {
667             $compile     = 0;
668             $compile_cpp = 1;
669         }
670
671         # Check hardening flags.
672         my @missing;
673         if ($compile and not all_flags_used($line, \@missing, @cflags)
674                 # Libraries linked with -fPIC don't have to (and can't) be linked
675                 # with -fPIE as well. It's no error if only PIE flags are missing.
676                 and not pic_pie_conflict($line, $harden_pie, \@missing, @def_cflags_pie)
677                 # Assume dpkg-buildflags returns the correct flags.
678                 and not $line =~ /`dpkg-buildflags --get CFLAGS`/) {
679             error_flags('CFLAGS missing', \@missing, \%flag_renames, $input[$i]);
680             $exit |= 1 << 3;
681         } elsif ($compile_cpp and not all_flags_used($line, \@missing, @cflags)
682                 # Libraries linked with -fPIC don't have to (and can't) be linked
683                 # with -fPIE as well. It's no error if only PIE flags are missing.
684                 and not pic_pie_conflict($line, $harden_pie, \@missing, @def_cflags_pie)
685                 # Assume dpkg-buildflags returns the correct flags.
686                 and not $line =~ /`dpkg-buildflags --get CXXFLAGS`/) {
687             error_flags('CXXFLAGS missing', \@missing, \%flag_renames, $input[$i]);
688             $exit |= 1 << 3;
689         }
690         if ($preprocess and not all_flags_used($line, \@missing, @cppflags)
691                 # Assume dpkg-buildflags returns the correct flags.
692                 and not $line =~ /`dpkg-buildflags --get CPPFLAGS`/) {
693             error_flags('CPPFLAGS missing', \@missing, \%flag_renames, $input[$i]);
694             $exit |= 1 << 3;
695         }
696         if ($link and not all_flags_used($line, \@missing, @ldflags)
697                 # Same here, -fPIC conflicts with -fPIE.
698                 and not pic_pie_conflict($line, $harden_pie, \@missing, @def_ldflags_pie)
699                 # Assume dpkg-buildflags returns the correct flags.
700                 and not $line =~ /`dpkg-buildflags --get LDFLAGS`/) {
701             error_flags('LDFLAGS missing', \@missing, \%flag_renames, $input[$i]);
702             $exit |= 1 << 3;
703         }
704     }
705 }
706
707 exit $exit;
708
709
710 __END__
711
712 =head1 NAME
713
714 blhc - build log hardening check, checks build logs for missing hardening flags
715
716 =head1 SYNOPSIS
717
718 B<blhc> [options] <dpkg-buildpackage build log file>..
719
720     --all                   force +all (+pie, +bindnow) check
721     --arch                  set architecture (autodetected)
722     --bindnow               force +bindbow check
723     --buildd                parser mode for buildds
724     --color                 use colored output
725     --pie                   force +pie check
726     --help                  available options
727     --version               version number and license
728
729 =head1 DESCRIPTION
730
731 blhc is a small tool which checks build logs for missing hardening flags and
732 other important warnings. It's licensed under the GPL 3 or later.
733
734 =head1 OPTIONS
735
736 =over 8
737
738 =item B<--all>
739
740 Force check for all +all (+pie, +bindnow) hardening flags. By default it's
741 auto detected.
742
743 =item B<--arch>
744
745 Set the specific architecture (e.g. amd64, armel, etc.), automatically
746 disables hardening flags not available on this architecture. Is detected
747 automatically if dpkg-buildpackage is used.
748
749 =item B<--bindnow>
750
751 Force check for all +bindnow hardening flags. By default it's auto detected.
752
753 =item B<--buildd>
754
755 Special mode for buildds when automatically parsing log files. The following
756 changes are in effect:
757
758 =over 2
759
760 =item
761
762 Don't check hardening flags in old log files (if dpkg-dev << 1.16.1 is
763 detected).
764
765 =back
766
767 =item B<--color>
768
769 Use colored (ANSI) output for warning messages.
770
771 =item B<--pie>
772
773 Force check for all +pie hardening flags. By default it's auto detected.
774
775 =item B<-h -? --help>
776
777 Print available options.
778
779 =item B<--version>
780
781 Print version number and license.
782
783 =back
784
785 Auto detection for B<--pie> and B<--bindnow> only works if at least one
786 command uses the required hardening flag (e.g. -fPIE). Then it's required for
787 all other commands as well.
788
789 =head1 EXIT STATUS
790
791 The exit status is a "bit mask", each listed status is ORed when the error
792 condition occurs to get the result.
793
794 =over 4
795
796 =item B<0>
797
798 Success.
799
800 =item B<1>
801
802 No compiler commands were found.
803
804 =item B<2>
805
806 Invalid arguments/options given to blhc.
807
808 =item B<4>
809
810 Non verbose build.
811
812 =item B<8>
813
814 Missing hardening flags.
815
816 =item B<16>
817
818 Hardening wrapper detected, no tests performed.
819
820 =back
821
822 =head1 AUTHOR
823
824 Simon Ruderich, E<lt>simon@ruderich.orgE<gt>
825
826 =head1 COPYRIGHT AND LICENSE
827
828 Copyright (C) 2012 by Simon Ruderich
829
830 This program is free software: you can redistribute it and/or modify
831 it under the terms of the GNU General Public License as published by
832 the Free Software Foundation, either version 3 of the License, or
833 (at your option) any later version.
834
835 This program is distributed in the hope that it will be useful,
836 but WITHOUT ANY WARRANTY; without even the implied warranty of
837 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
838 GNU General Public License for more details.
839
840 You should have received a copy of the GNU General Public License
841 along with this program.  If not, see <http://www.gnu.org/licenses/>.
842
843 =cut