3 # Build log hardening check, checks build logs for missing hardening flags.
5 # Copyright (C) 2012 Simon Ruderich
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.
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.
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/>.
25 use Text::ParseWords ();
27 our $VERSION = '0.01';
32 # Regex to catch compiler commands.
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"
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)?-)?
45 # Regex to catch (GCC) compiler warnings.
46 my $warning_regex = qr/^(.+?):([0-9]+):[0-9]+: warning: (.+?) \[(.+?)\]$/;
48 # List of source file extensions which require preprocessing.
49 my @source_preprocess_compile_cpp = (
51 qw( cc cp cxx cpp CPP c++ C ),
55 my @source_preprocess_compile = (
61 @source_preprocess_compile_cpp,
63 qw( F FOR fpp FPP FTN F90 F95 F03 F08 ),
65 my @source_preprocess_no_compile = (
69 my @source_preprocess = (
70 @source_preprocess_compile,
71 @source_preprocess_no_compile,
73 # List of source file extensions which don't require preprocessing.
74 my @source_no_preprocess_compile_cpp = (
80 my @source_no_preprocess_compile = (
84 @source_no_preprocess_compile_cpp,
88 qw( f for ftn f90 f95 f03 f08 ),
90 my @source_no_preprocess_no_compile = (
94 my @source_no_preprocess = (
95 @source_no_preprocess_compile,
96 @source_no_preprocess_no_compile,
98 # List of header file extensions which require preprocessing.
99 my @header_preprocess = (
100 # C, C++, Objective-C, Objective-C++
103 qw( hh H hp hxx hpp HPP h++ tcc ),
106 # Hashes for fast extensions lookup to check if a file falls in one of these
108 my %extensions_no_preprocess = map { $_ => 1 } (
109 @source_no_preprocess,
111 my %extensions_preprocess = map { $_ => 1 } (
115 my %extensions_compile_link = map { $_ => 1 } (
117 @source_no_preprocess,
119 my %extensions_compile = map { $_ => 1 } (
120 @source_preprocess_compile,
121 @source_no_preprocess_compile,
123 my %extensions_no_compile = map { $_ => 1 } (
124 @source_preprocess_no_compile,
125 @source_no_preprocess_no_compile,
127 my %extensions_compile_cpp = map { $_ => 1 } (
128 @source_preprocess_compile_cpp,
129 @source_no_preprocess_compile_cpp,
131 my %extension = map { $_ => 1 } (
132 @source_no_preprocess,
137 # Regexp to match file extensions.
138 my $file_extension_regex = qr/
140 \S+ # Filename without extension.
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".
149 # Expected (hardening) flags. All flags are used as regexps.
154 my @def_cflags_format = (
157 '-Werror=format-security',
159 my @def_cflags_fortify = (
160 # fortify needs at least -O1, but -O2 is recommended anyway
162 my @def_cflags_stack = (
164 '--param=ssp-buffer-size=4',
166 my @def_cflags_pie = (
172 # @def_cxxflags_* is the same as @def_cflags_*.
173 my @def_cppflags = ();
174 my @def_cppflags_fortify = (
175 '-D_FORTIFY_SOURCE=2',
177 my @def_ldflags = ();
178 my @def_ldflags_relro = (
181 my @def_ldflags_bindnow = (
184 my @def_ldflags_pie = (
188 my @def_ldflags_pic = (
193 # References to all flags checked by the parser.
197 \@def_cflags_fortify,
202 \@def_cppflags_fortify,
205 \@def_ldflags_bindnow,
207 # References to all used flags.
208 my @flag_refs_all = (
213 # Renaming rules for the output so the regex parts are not visible. Also
214 # stores string values of flag regexps above, see compile_flag_regexp().
216 '-O(?:2|3)' => '-O2',
217 '-Wl,(?:-z,)?relro' => '-Wl,-z,relro',
218 '-Wl,(?:-z,)?now' => '-Wl,-z,now',
222 no_compiler_commands => 1 << 0,
223 # used by POD::Usage => 1 << 1,
224 non_verbose_build => 1 << 2,
225 flags_missing => 1 << 3,
226 hardening_wrapper => 1 << 4,
227 invalid_cmake => 1 << 5,
230 # Statistics of missing flags and non-verbose build commands. Used for
234 preprocess_missing => 0,
236 compile_missing => 0,
238 compile_cpp_missing => 0,
242 commands_nonverbose => 0,
245 # Use colored (ANSI) output?
252 my ($message, $missing_flags_ref, $flag_renames_ref, $line) = @_;
254 # Get string value of qr//-escaped regexps and if requested rename them.
255 my @missing_flags = map {
256 $flag_renames_ref->{$_}
257 } @{$missing_flags_ref};
259 my $flags = join ' ', @missing_flags;
260 printf "%s (%s)%s %s",
261 error_color($message, 'red'), $flags, error_color(':', 'yellow'),
264 sub error_non_verbose_build {
268 error_color('NONVERBOSE BUILD', 'red'),
269 error_color(':', 'yellow'),
272 sub error_invalid_cmake {
276 error_color('INVALID CMAKE', 'red'),
277 error_color(':', 'yellow'),
280 sub error_hardening_wrapper {
282 error_color('HARDENING WRAPPER', 'red'),
283 error_color(':', 'yellow'),
284 'no checks possible, aborting';
287 my ($message, $color) = @_;
290 return Term::ANSIColor::colored($message, $color);
297 my ($line, @flags) = @_;
299 foreach my $flag (@flags) {
300 return 1 if $line =~ /$flag/;
306 my ($line, $missing_flags_ref, @flags) = @_;
308 my @missing_flags = ();
309 foreach my $flag (@flags) {
310 if (not $line =~ /$flag/) {
311 push @missing_flags, $flag;
315 return 1 if scalar @missing_flags == 0;
317 @{$missing_flags_ref} = @missing_flags;
321 # Modifies $missing_flags_ref array.
322 sub pic_pie_conflict {
323 my ($line, $pie, $missing_flags_ref, @flags_pie) = @_;
325 return 0 if not $pie;
326 return 0 if not any_flags_used($line, @def_ldflags_pic);
328 my %flags = map { $_ => 1 } @flags_pie;
330 # Remove all PIE flags from @missing_flags as they are not required with
333 not exists $flags{$_}
334 } @{$missing_flags_ref};
335 @{$missing_flags_ref} = @result;
337 # We got a conflict when no flags are left, thus only PIE flags were
338 # missing. If other flags were missing abort because the conflict is not
340 return scalar @result == 0;
343 sub is_non_verbose_build {
344 my ($line, $next_line, $skip_ref) = @_;
346 if (not (index($line, 'checking if you want to see long compiling messages... no') == 0
347 or $line =~ /^\s*\[?(?:CC|CCLD|C\+\+|CXX|CXXLD|LD|LINK)\]?\s+(.+?)$/
348 or $line =~ /^\s*(?:C|c)ompiling\s+(.+?)(?:\.\.\.)?$/
349 or $line =~ /^\s*(?:B|b)uilding (?:program|shared library)\s+(.+?)$/
350 or $line =~ /^\s*\[[\d ]+%\] Building (?:C|CXX) object (.+?)$/)) {
356 # C++ compiler setting.
357 return 0 if $line =~ /^\s*C\+\+.+?:\s+(?:yes|no)\s*$/;
358 # "Compiling" with no file name.
359 if ($line =~ /^\s*(?:C|c)ompiling\s+(.+?)(?:\.\.\.)?$/) {
360 # $file_extension_regex may need spaces around the filename.
361 return 0 if not " $1 " =~ /$file_extension_regex/o;
366 # On the first pass we only check if this line is verbose or not.
367 return 1 if not defined $next_line;
369 # Second pass, we have access to the next line.
372 # CMake and other build systems print the non-verbose messages also when
373 # building verbose. If a compiler and the file name occurs in the next
374 # line, treat it as verbose build.
376 # Get filename, we can't use the complete path as only parts of it are
377 # used in the real compiler command.
378 $file =~ m{/([^/\s]+)$};
381 if (index($next_line, $file) != -1 and $next_line =~ /$cc_regex/o) {
382 # We still have to skip the current line as it doesn't contain any
392 sub compile_flag_regexp {
393 my ($flag_renames_ref, @flags) = @_;
396 foreach my $flag (@flags) {
397 # Store flag name in replacement string for correct flags in messages
398 # with qr//ed flag regexps.
399 $flag_renames_ref->{qr/\s$flag(?:\s|\\)/}
400 = (exists $flag_renames_ref->{$flag})
401 ? $flag_renames_ref->{$flag}
404 # Compile flag regexp for faster execution.
405 push @result, qr/\s$flag(?:\s|\\)/;
410 sub extension_found {
411 my ($extensions_ref, @extensions) = @_;
414 foreach my $extension (@extensions) {
415 if (exists $extensions_ref->{$extension}) {
426 # Parse command line arguments.
428 my $option_version = 0;
430 my $option_bindnow = 0;
431 my @option_ignore_flag = ();
432 my @option_ignore_line = ();
434 my $option_arch = undef;
435 my $option_buildd = 0;
437 if (not Getopt::Long::GetOptions(
438 'help|h|?' => \$option_help,
439 'version' => \$option_version,
441 'pie' => \$option_pie,
442 'bindnow' => \$option_bindnow,
443 'all' => \$option_all,
445 'ignore-flag=s' => \@option_ignore_flag,
446 'ignore-line=s' => \@option_ignore_line,
448 'color' => \$option_color,
449 'arch=s' => \$option_arch,
450 'buildd' => \$option_buildd,
452 or scalar @ARGV == 0) {
454 Pod::Usage::pod2usage(2);
458 Pod::Usage::pod2usage(1);
460 if ($option_version) {
461 print "blhc $VERSION Copyright (C) 2012 Simon Ruderich
463 This program is free software: you can redistribute it and/or modify
464 it under the terms of the GNU General Public License as published by
465 the Free Software Foundation, either version 3 of the License, or
466 (at your option) any later version.
468 This program is distributed in the hope that it will be useful,
469 but WITHOUT ANY WARRANTY; without even the implied warranty of
470 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
471 GNU General Public License for more details.
473 You should have received a copy of the GNU General Public License
474 along with this program. If not, see <http://www.gnu.org/licenses/>.
479 # Don't load Term::ANSIColor in buildd mode because Term::ANSIColor is not
480 # installed on Debian's buildds.
481 if (not $option_buildd) {
482 require Term::ANSIColor;
490 # Strip flags which should be ignored.
491 if (scalar @option_ignore_flag > 0) {
492 my %ignores = map { $_ => 1 } @option_ignore_flag;
493 foreach my $flags (@flag_refs) {
495 # Flag found as string.
496 not exists $ignores{$_}
497 # Flag found as string representation of regexp.
498 and (not defined $flag_renames{$_}
499 or not exists $ignores{$flag_renames{$_}})
504 # Precompile all flag regexps. any_flags_used(), all_flags_used() get a lot
506 foreach my $flags (@flag_refs_all) {
507 @{$flags} = compile_flag_regexp(\%flag_renames, @{$flags});
510 # Precompile ignore line regexps, also anchor at beginning and end of line.
511 foreach my $ignore (@option_ignore_line) {
512 $ignore = qr/^$ignore$/;
519 foreach my $file (@ARGV) {
520 print "checking '$file'...\n" if scalar @ARGV > 1;
522 open my $fh, '<', $file or die "$!: $file";
524 # Architecture of this file.
525 my $arch = $option_arch;
527 # Hardening options. Not all architectures support all hardening options.
528 my $harden_format = 1;
529 my $harden_fortify = 1;
530 my $harden_stack = 1;
531 my $harden_relro = 1;
532 my $harden_bindnow = $option_bindnow; # defaults to 0
533 my $harden_pie = $option_pie; # defaults to 0
535 while (my $line = <$fh>) {
536 # dpkg-buildflags only provides hardening flags since 1.16.1, don't
537 # check for hardening flags in buildd mode if an older dpkg-dev is
538 # used. Default flags (-g -O2) are still checked.
540 # Packages which were built before 1.16.1 but used their own hardening
541 # flags are not checked.
543 and index($line, 'Toolchain package versions: ') == 0) {
544 require Dpkg::Version;
545 if (not $line =~ /\bdpkg-dev_(\S+)/
546 or Dpkg::Version::version_compare($1, '1.16.1') < 0) {
556 # The following two versions of CMake in Debian obeyed CPPFLAGS, but
557 # this was later dropped because upstream rejected the patch. Thus
558 # build logs with these versions will have fortify hardening flags
559 # enabled, even though they may be not correctly set and are missing
560 # when build with later CMake versions. Thanks to Aron Xu for letting
562 if (index($line, 'Package versions: ') == 0
563 and $line =~ /\bcmake_(\S+)/
564 and ($1 eq '2.8.7-1' or $1 eq '2.8.7-2')) {
565 if (not $option_buildd) {
566 error_invalid_cmake($1);
568 print "W-invalid-cmake-used $1\n";
570 $exit |= $exit_code{invalid_cmake};
573 # If hardening wrapper is used (wraps calls to gcc and adds hardening
574 # flags automatically) we can't perform any checks, abort.
575 if (index($line, 'Build-Depends: ') == 0
576 and $line =~ /\bhardening-wrapper\b/) {
577 if (not $option_buildd) {
578 error_hardening_wrapper();
580 print "I-hardening-wrapper-used\n";
582 $exit |= $exit_code{hardening_wrapper};
586 # We skip over unimportant lines at the beginning of the log to
587 # prevent false positives.
588 last if index($line, 'dpkg-buildpackage: ') == 0;
591 # Input lines, contain only the lines with compiler commands.
594 my $continuation = 0;
595 my $complete_line = undef;
596 while (my $line = <$fh>) {
597 # And stop at the end of the build log. Package details (reported by
598 # the buildd logs) are not important for us. This also prevents false
600 last if $line =~ /^Build finished at \d{8}-\d{4}$/;
602 # Detect architecture automatically unless overridden.
604 and $line =~ /^dpkg-buildpackage: host architecture (.+)$/) {
608 # Ignore compiler warnings for now.
609 next if $line =~ /$warning_regex/o;
611 if (not $option_buildd and index($line, "\033") != -1) { # esc
612 # Remove all ANSI color sequences which are sometimes used in
613 # non-verbose builds.
614 $line = Term::ANSIColor::colorstrip($line);
615 # Also strip '\0xf' (delete previous character), used by Elinks'
618 # And "ESC(B" which seems to be used on armhf and hurd (not sure
620 $line =~ s/\033\(B//g;
623 # Check if this line indicates a non verbose build.
624 my $non_verbose = is_non_verbose_build($line);
626 # One line may contain multiple commands (";"). Treat each one as
627 # single line. parse_line() is slow, only use it when necessary.
628 my @line = (index($line, ';') == -1)
631 # Ensure newline at the line end - necessary for
632 # correct parsing later.
635 } Text::ParseWords::parse_line(';', 1, $line);
636 foreach $line (@line) {
640 # Join lines, but leave the "\" in place so it's clear where
641 # the original line break was.
642 chomp $complete_line;
643 $complete_line .= ' ' . $line;
645 # Line continuation, line ends with "\".
646 if ($line =~ /\\$/) {
648 # Start line continuation.
649 if (not defined $complete_line) {
650 $complete_line = $line;
655 # Use the complete line if a line continuation occurred.
656 if (defined $complete_line) {
657 $line = $complete_line;
658 $complete_line = undef;
661 # Ignore lines with no compiler commands.
662 next if not $non_verbose
663 and not $line =~ /\b$cc_regex(?:\s|\\)/o;
664 # Ignore lines with no filenames with extensions. May miss some
665 # non-verbose builds (e.g. "gcc -o test" [sic!]), but shouldn't be
666 # a problem as the log will most likely contain other non-verbose
667 # commands which are detected.
668 next if not $non_verbose
669 and not $line =~ /$file_extension_regex/o;
671 # Ignore false positives.
673 # `./configure` output.
674 next if not $non_verbose
675 and $line =~ /^(?:checking|(?:C|c)onfigure:) /;
676 next if $line =~ /^\s*(?:Host\s+)?(?:C(?:\+\+)?\s+)?
677 (?:C|c)ompiler[\s.]*:?\s+
679 next if $line =~ /^\s*(?:- )?(?:HOST_)?(?:CC|CXX)\s*=\s*$cc_regex_full\s*$/o;
681 # Check if additional hardening options were used. Used to ensure
682 # they are used for the complete build.
683 $harden_pie = 1 if any_flags_used($line, @def_cflags_pie, @def_ldflags_pie);
684 $harden_bindnow = 1 if any_flags_used($line, @def_ldflags_bindnow);
692 if (scalar @input == 0) {
693 if (not $option_buildd) {
694 print "No compiler commands!\n";
696 print "W-no-compiler-commands\n";
698 $exit |= $exit_code{no_compiler_commands};
702 if ($option_buildd) {
703 $statistics{commands} += scalar @input;
706 # Option or auto detected.
708 # The following was partially copied from dpkg-dev 1.16.1.2
709 # (/usr/share/perl5/Dpkg/Vendor/Debian.pm, add_hardening_flags()),
710 # copyright Raphaël Hertzog <hertzog@debian.org>, Kees Cook
711 # <kees@debian.org>, Canonical, Ltd. licensed under GPL version 2 or
712 # later. Keep it in sync.
715 my ($abi, $os, $cpu) = Dpkg::Arch::debarch_to_debtriplet($arch);
717 # Disable unsupported hardening options.
718 if ($cpu =~ /^(?:ia64|alpha|mips|mipsel|hppa)$/ or $arch eq 'arm') {
721 if ($cpu =~ /^(?:ia64|hppa|avr32)$/) {
728 my @cflags = @def_cflags;
729 my @cxxflags = @def_cxxflags;
730 my @cppflags = @def_cppflags;
731 my @ldflags = @def_ldflags;
732 # Check the specified hardening options, same order as dpkg-buildflags.
734 @cflags = (@cflags, @def_cflags_pie);
735 @cxxflags = (@cxxflags, @def_cflags_pie);
736 @ldflags = (@ldflags, @def_ldflags_pie);
739 @cflags = (@cflags, @def_cflags_stack);
740 @cxxflags = (@cxxflags, @def_cflags_stack);
742 if ($harden_fortify) {
743 @cflags = (@cflags, @def_cflags_fortify);
744 @cxxflags = (@cxxflags, @def_cflags_fortify);
745 @cppflags = (@cppflags, @def_cppflags_fortify);
747 if ($harden_format) {
748 @cflags = (@cflags, @def_cflags_format);
749 @cxxflags = (@cxxflags, @def_cflags_format);
752 @ldflags = (@ldflags, @def_ldflags_relro);
754 if ($harden_bindnow) {
755 @ldflags = (@ldflags, @def_ldflags_bindnow);
759 for (my $i = 0; $i < scalar @input; $i++) {
760 my $line = $input[$i];
762 # Ignore line if requested.
763 foreach my $ignore (@option_ignore_line) {
764 next LINE if $line =~ /$ignore/;
768 if (is_non_verbose_build($line, $input[$i + 1], \$skip)) {
769 if (not $option_buildd) {
770 error_non_verbose_build($line);
772 $statistics{commands_nonverbose}++;
774 $exit |= $exit_code{non_verbose_build};
777 # Even if it's a verbose build, we might have to skip this line.
780 # Remove everything until and including the compiler command. Makes
781 # checks easier and faster.
782 $line =~ s/^.*?$cc_regex//o;
783 # "([...] test.c)" is not detected as 'test.c' - fix this by removing
784 # the brace and similar characters.
785 $line =~ s/['")]+$//;
787 # Skip unnecessary tests when only preprocessing.
788 my $flag_preprocess = 0;
795 # Preprocess, compile, assemble.
796 if ($line =~ /\s(-E|-S|-c)\b/) {
798 $flag_preprocess = 1 if $1 eq '-E';
799 $compile = 1 if $1 eq '-S' or $1 eq '-c';
800 # Dependency generation for Makefiles. The other flags (-MF -MG -MP
801 # -MT -MQ) are always used with -M/-MM.
802 } elsif ($line =~ /\s(?:-M|-MM)\b/) {
804 # Otherwise assume we are linking.
809 # -MD/-MMD also cause dependency generation, but they don't imply -E!
810 if ($line =~ /\s(?:-MD|-MMD)\b/) {
812 $flag_preprocess = 0;
815 # Dependency generation for Makefiles, no preprocessing or other flags
819 # Get all file extensions on this line.
820 my @extensions = $line =~ /$file_extension_regex/go;
821 # Ignore all unknown extensions to speedup the search below.
822 @extensions = grep { exists $extension{$_} } @extensions;
824 # These file types don't require preprocessing.
825 if (extension_found(\%extensions_no_preprocess, @extensions)) {
828 # These file types require preprocessing.
829 if (extension_found(\%extensions_preprocess, @extensions)) {
833 # If there are source files then it's compiling/linking in one step
834 # and we must check both. We only check for source files here, because
835 # header files cause too many false positives.
836 if (not $flag_preprocess
837 and extension_found(\%extensions_compile_link, @extensions)) {
838 # Assembly files don't need CFLAGS.
839 if (not extension_found(\%extensions_compile, @extensions)
840 and extension_found(\%extensions_no_compile, @extensions)) {
848 # Assume CXXFLAGS are required when a C++ file is specified in the
852 and extension_found(\%extensions_compile_cpp, @extensions)) {
857 if ($option_buildd) {
858 $statistics{preprocess}++ if $preprocess;
859 $statistics{compile}++ if $compile;
860 $statistics{compile_cpp}++ if $compile_cpp;
861 $statistics{link}++ if $link;
864 # Check hardening flags.
866 if ($compile and not all_flags_used($line, \@missing, @cflags)
867 # Libraries linked with -fPIC don't have to (and can't) be
868 # linked with -fPIE as well. It's no error if only PIE flags
870 and not pic_pie_conflict($line, $harden_pie, \@missing, @def_cflags_pie)
871 # Assume dpkg-buildflags returns the correct flags.
872 and index($line, '`dpkg-buildflags --get CFLAGS`') == -1) {
873 if (not $option_buildd) {
874 error_flags('CFLAGS missing', \@missing, \%flag_renames, $input[$i]);
876 $statistics{compile_missing}++;
878 $exit |= $exit_code{flags_missing};
879 } elsif ($compile_cpp and not all_flags_used($line, \@missing, @cflags)
880 # Libraries linked with -fPIC don't have to (and can't) be
881 # linked with -fPIE as well. It's no error if only PIE flags
883 and not pic_pie_conflict($line, $harden_pie, \@missing, @def_cflags_pie)
884 # Assume dpkg-buildflags returns the correct flags.
885 and index($line, '`dpkg-buildflags --get CXXFLAGS`') == -1) {
886 if (not $option_buildd) {
887 error_flags('CXXFLAGS missing', \@missing, \%flag_renames, $input[$i]);
889 $statistics{compile_cpp_missing}++;
891 $exit |= $exit_code{flags_missing};
893 if ($preprocess and not all_flags_used($line, \@missing, @cppflags)
894 # Assume dpkg-buildflags returns the correct flags.
895 and index($line, '`dpkg-buildflags --get CPPFLAGS`') == -1) {
896 if (not $option_buildd) {
897 error_flags('CPPFLAGS missing', \@missing, \%flag_renames, $input[$i]);
899 $statistics{preprocess_missing}++;
901 $exit |= $exit_code{flags_missing};
903 if ($link and not all_flags_used($line, \@missing, @ldflags)
904 # Same here, -fPIC conflicts with -fPIE.
905 and not pic_pie_conflict($line, $harden_pie, \@missing, @def_ldflags_pie)
906 # Assume dpkg-buildflags returns the correct flags.
907 and index($line, '`dpkg-buildflags --get LDFLAGS`') == -1) {
908 if (not $option_buildd) {
909 error_flags('LDFLAGS missing', \@missing, \%flag_renames, $input[$i]);
911 $statistics{link_missing}++;
913 $exit |= $exit_code{flags_missing};
918 # Print statistics for buildd mode, only output in this mode.
919 if ($option_buildd) {
922 if ($statistics{preprocess_missing}) {
923 push @warning, sprintf "CPPFLAGS %d (of %d)",
924 $statistics{preprocess_missing},
925 $statistics{preprocess};
927 if ($statistics{compile_missing}) {
928 push @warning, sprintf "CFLAGS %d (of %d)",
929 $statistics{compile_missing},
930 $statistics{compile};
932 if ($statistics{compile_cpp_missing}) {
933 push @warning, sprintf "CXXFLAGS %d (of %d)",
934 $statistics{compile_cpp_missing},
935 $statistics{compile_cpp};
937 if ($statistics{link_missing}) {
938 push @warning, sprintf "LDFLAGS %d (of %d)",
939 $statistics{link_missing},
942 if (scalar @warning) {
943 local $" = ', '; # array join string
944 print "W-dpkg-buildflags-missing @warning missing\n";
947 if ($statistics{commands_nonverbose}) {
948 printf "W-compiler-flags-hidden %d (of %d) hidden\n",
949 $statistics{commands_nonverbose},
950 $statistics{commands},
962 blhc - build log hardening check, checks build logs for missing hardening flags
966 B<blhc> [I<options>] I<E<lt>dpkg-buildpackage build log fileE<gt>..>
970 blhc is a small tool which checks build logs for missing hardening flags. It's
971 licensed under the GPL 3 or later.
973 It's designed to check build logs generated by Debian's dpkg-buildpackage (or
974 tools using dpkg-buildpackage like pbuilder or the official buildd build logs)
975 to help maintainers detect missing hardening flags in their packages.
983 Force check for all +all (+pie, +bindnow) hardening flags. By default it's
986 =item B<--arch> I<architecture>
988 Set the specific architecture (e.g. amd64, armel, etc.), automatically
989 disables hardening flags not available on this architecture. Is detected
990 automatically if dpkg-buildpackage is used.
994 Force check for all +bindnow hardening flags. By default it's auto detected.
998 Special mode for buildds when automatically parsing log files. The following
999 changes are in effect:
1005 Print tags instead of normal warnings, see L</"BUILDD TAGS"> for a list of
1010 Don't check hardening flags in old log files (if dpkg-dev << 1.16.1 is
1015 Don't require Term::ANSIColor.
1021 Use colored (ANSI) output for warning messages.
1023 =item B<--ignore-flag> I<flag>
1025 Don't print an error when the specific flag is missing in a compiler line.
1026 I<flag> is a string.
1028 Used to prevent false positives. This option can be specified multiple times.
1030 =item B<--ignore-line> I<regex>
1032 Ignore lines matching the given Perl regex. I<regex> is automatically anchored
1033 at the beginning and end of the line to prevent false negatives.
1035 B<NOTE>: Not the input lines are checked, but the lines which are displayed in
1036 warnings (which have line continuation resolved).
1038 Used to prevent false positives. This option can be specified multiple times.
1042 Force check for all +pie hardening flags. By default it's auto detected.
1044 =item B<-h -? --help>
1046 Print available options.
1050 Print version number and license.
1054 Auto detection for B<--pie> and B<--bindnow> only works if at least one
1055 command uses the required hardening flag (e.g. -fPIE). Then it's required for
1056 all other commands as well.
1060 Normal usage, parse a single log file.
1062 blhc path/to/log/file
1064 Parse multiple log files. The exit code is ORed over all files.
1066 blhc path/to/directory/with/log/files/*
1068 Don't treat missing C<-g> as error:
1070 blhc --ignore-flag -g path/to/log/file
1072 Ignore lines consisting exactly of C<./script gcc file> which would cause a
1075 blhc --ignore-line '\./script gcc file' path/to/log/file
1077 Ignore lines matching C<./script gcc file> somewhere in the line.
1079 blhc --ignore-line '.*\./script gcc file.*' path/to/log/file
1081 Use blhc with pbuilder.
1083 pbuilder path/to/package.dsc | tee path/log/file
1084 blhc path/to/file || echo flags missing
1088 The following tags are used in I<--buildd> mode. In braces the additional data
1095 B<I-hardening-wrapper-used>
1097 The package uses hardening-wrapper which intercepts calls to gcc and adds
1098 hardening flags. The build log doesn't contain any hardening flags and thus
1099 can't be checked by blhc.
1103 B<W-compiler-flags-hidden> (summary of hidden lines)
1105 Build log contains lines which hide the real compiler flags. For example:
1112 Most of the time either C<export V=1> or C<export verbose=1> in
1113 F<debian/rules> fixes builds with hidden compiler flags. Sometimes C<.SILENT>
1114 in a F<Makefile> must be removed. And as last resort the F<Makefile> must be
1115 patched to remove the C<@>s hiding the real compiler commands.
1119 B<W-dpkg-buildflags-missing> (summary of missing flags)
1121 CPPFLAGS, CFLAGS, CXXFLAGS, LDFLAGS missing.
1125 B<W-invalid-cmake-used> (version)
1129 B<W-no-compiler-commands>
1131 No compiler commands were detected. Either the log contains none or they were
1132 not correctly detected by blhc (please report the bug in this case).
1138 The exit status is a "bit mask", each listed status is ORed when the error
1139 condition occurs to get the result.
1149 No compiler commands were found.
1153 Invalid arguments/options given to blhc.
1161 Missing hardening flags.
1165 Hardening wrapper detected, no tests performed.
1171 Simon Ruderich, E<lt>simon@ruderich.orgE<gt>
1173 Thanks to to Bernhard R. Link E<lt>brlink@debian.orgE<gt> and Jaria Alto
1174 E<lt>jari.aalto@cante.netE<gt> for their valuable input and suggestions.
1176 =head1 COPYRIGHT AND LICENSE
1178 Copyright (C) 2012 by Simon Ruderich
1180 This program is free software: you can redistribute it and/or modify
1181 it under the terms of the GNU General Public License as published by
1182 the Free Software Foundation, either version 3 of the License, or
1183 (at your option) any later version.
1185 This program is distributed in the hope that it will be useful,
1186 but WITHOUT ANY WARRANTY; without even the implied warranty of
1187 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1188 GNU General Public License for more details.
1190 You should have received a copy of the GNU General Public License
1191 along with this program. If not, see <http://www.gnu.org/licenses/>.
1195 L<hardening-check(1)>, L<dpkg-buildflags(1)>