3 # Build log hardening check, checks build logs for missing hardening flags.
5 # Copyright (C) 2012-2024 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.13';
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\+\+|gfortran|mpicc|mpicxx|mpifort)
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_prefix = qr/
42 [a-z0-9_]+-(?:linux-|kfreebsd-)?gnu(?:eabi|eabihf)?
44 my $cc_regex_full = qr/
45 (?:$cc_regex_full_prefix-)?
48 # Regex to check if a line contains a compiler command.
49 my $cc_regex_normal = qr/
55 # Regex to catch (GCC) compiler warnings.
56 my $warning_regex = qr/^(.+?):(\d+):\d+: warning: (.+?) \[(.+?)\]$/;
57 # Regex to catch libtool commands and not lines which show commands executed
58 # by libtool (e.g. libtool: link: ...).
59 my $libtool_regex = qr/\blibtool["']?\s.*--mode=/;
60 my $libtool_link_regex = qr/\blibtool: link: /;
62 # List of source file extensions which require preprocessing.
63 my @source_preprocess_compile_cpp = (
65 qw( cc cp cxx cpp CPP c++ C ),
69 my @source_preprocess_compile_fortran = (
71 qw( F FOR fpp FPP FTN F90 F95 F03 F08 ),
73 my @source_preprocess_compile = (
79 @source_preprocess_compile_cpp,
81 @source_preprocess_compile_fortran,
83 my @source_preprocess_no_compile = (
87 my @source_preprocess = (
88 @source_preprocess_compile,
89 @source_preprocess_no_compile,
91 # List of source file extensions which don't require preprocessing.
92 my @source_no_preprocess_compile_cpp = (
98 my @source_no_preprocess_compile_ada = (
104 my @source_no_preprocess_compile_fortran = (
106 qw( f for ftn f90 f95 f03 f08 ),
108 my @source_no_preprocess_compile = (
112 @source_no_preprocess_compile_cpp,
116 @source_no_preprocess_compile_fortran,
118 @source_no_preprocess_compile_ada,
120 my @source_no_preprocess_no_compile_ada = (
124 my @source_no_preprocess_no_compile = (
128 @source_no_preprocess_no_compile_ada,
130 my @source_no_preprocess = (
131 @source_no_preprocess_compile,
132 @source_no_preprocess_no_compile,
134 # List of header file extensions which require preprocessing.
135 my @header_preprocess = (
136 # C, C++, Objective-C, Objective-C++
139 qw( hh H hp hxx hpp HPP h++ tcc ),
143 # Normal object files.
145 # Libtool object files.
147 # Dynamic libraries. bzip2 uses .sho.
153 # Hashes for fast extensions lookup to check if a file falls in one of these
155 my %extensions_no_preprocess = map { $_ => 1 } (
156 # There's no @header_no_preprocess.
157 @source_no_preprocess,
159 my %extensions_preprocess = map { $_ => 1 } (
163 my %extensions_compile_link = map { $_ => 1 } (
165 @source_no_preprocess,
167 my %extensions_compile = map { $_ => 1 } (
168 @source_preprocess_compile,
169 @source_no_preprocess_compile,
171 my %extensions_no_compile = map { $_ => 1 } (
172 @source_preprocess_no_compile,
173 @source_no_preprocess_no_compile,
175 my %extensions_compile_cpp = map { $_ => 1 } (
176 @source_preprocess_compile_cpp,
177 @source_no_preprocess_compile_cpp,
179 my %extensions_ada = map { $_ => 1 } (
180 @source_no_preprocess_compile_ada,
181 @source_no_preprocess_no_compile_ada,
183 my %extensions_fortran = map { $_ => 1 } (
184 @source_no_preprocess_compile_fortran,
185 @source_preprocess_compile_fortran,
187 my %extensions_object = map { $_ => 1 } (
190 my %extension = map { $_ => 1 } (
191 @source_no_preprocess,
197 # Regexp to match file extensions.
198 my $file_extension_regex = qr/
200 \S+ # Filename without extension.
202 ([^\/\\.,;:\s]+)# File extension.
203 (?=\s|\\) # At end of word. Can't use \b because some files have non
204 # word characters at the end and because \b matches double
205 # extensions (like .cpp.o). Works always as all lines are
206 # terminated with "\n".
209 # Expected (hardening) flags. All flags are used as regexps (and compiled to
210 # real regexps below for better execution speed).
213 '-O(?:2|3)', # keep at index 1, search for @def_cflags_debug to change it
215 my @def_cflags_debug = (
216 # These flags indicate a debug build which disables checks for -O2.
220 my @def_cflags_format = (
221 '-Wformat(?:=2)?', # -Wformat=2 implies -Wformat, accept it too
222 '-Werror=format-security', # implies -Wformat-security
224 my @def_cflags_fortify = (
225 # fortify needs at least -O1, but -O2 is recommended anyway
227 my @def_cflags_stack = (
228 '-fstack-protector', # keep first, used by cflags_stack_broken()
229 '--param[= ]ssp-buffer-size=4',
231 my @def_cflags_stack_strong = (
232 '-fstack-protector-strong', # keep first, used by cflags_stack_broken()
234 my @def_cflags_stack_bad = (
235 # Blacklist all stack protector options for simplicity.
236 '-fno-stack-protector',
237 '-fno-stack-protector-all',
238 '-fno-stack-protector-strong',
240 my @def_cflags_stack_clash = (
241 '-fstack-clash-protection',
243 my @def_cflags_pie = (
249 # @def_cxxflags_* is the same as @def_cflags_*.
250 my @def_cppflags = ();
251 my @def_cppflags_fortify = (
252 '-D_FORTIFY_SOURCE=[23]', # must be first, see cppflags_fortify_broken()
253 # If you add another flag fix hack below (search for "Hack to fix") and
254 # $def_cppflags_fortify[0].
256 my @def_cppflags_fortify_bad = (
257 # These flags may overwrite -D_FORTIFY_SOURCE=2.
259 '-D_FORTIFY_SOURCE=0',
260 '-D_FORTIFY_SOURCE=1',
262 my @def_ldflags = ();
263 my @def_ldflags_relro = (
266 my @def_ldflags_bindnow = (
269 my @def_ldflags_pie = (
273 my @def_ldflags_pic = (
278 # References to all flags checked by the flag checker.
282 \@def_cflags_fortify,
284 \@def_cflags_stack_strong,
285 \@def_cflags_stack_bad,
286 \@def_cflags_stack_clash,
290 \@def_cppflags_fortify,
293 \@def_ldflags_bindnow,
296 # References to all used flags.
297 my @flag_refs_all = (
300 \@def_cppflags_fortify_bad,
303 # Renaming rules for the output so the regex parts are not visible. Also
304 # stores string values of flag regexps above, see compile_flag_regexp().
307 '-O(?:2|3)' => '-O2',
308 '-Wformat(?:=2)?' => '-Wformat',
309 '--param[= ]ssp-buffer-size=4' => '--param=ssp-buffer-size=4',
310 '-D_FORTIFY_SOURCE=[23]' => '-D_FORTIFY_SOURCE=2',
311 '-Wl,(?:-z,)?relro' => '-Wl,-z,relro',
312 '-Wl,(?:-z,)?now' => '-Wl,-z,now',
316 no_compiler_commands => 1 << 0,
317 # used by POD::Usage => 1 << 1,
318 non_verbose_build => 1 << 2,
319 flags_missing => 1 << 3,
320 hardening_wrapper => 1 << 4,
321 invalid_cmake => 1 << 5,
325 no_compiler_commands => 'I-no-compiler-commands',
326 non_verbose_build => 'W-compiler-flags-hidden',
327 flags_missing => 'W-dpkg-buildflags-missing',
328 hardening_wrapper => 'I-hardening-wrapper-used',
329 invalid_cmake => 'I-invalid-cmake-used',
332 # Statistics of missing flags and non-verbose build commands. Used for
336 preprocess_missing => 0,
338 compile_missing => 0,
340 compile_cpp_missing => 0,
344 commands_nonverbose => 0,
347 # Use colored (ANSI) output?
357 foreach my $delim (';', '&&', '||') {
360 push @x, Text::ParseWords::parse_line(qr/\Q$delim\E/, 1, $_);
366 # Ensure newline at the line end - necessary for
367 # correct parsing later.
374 my ($message, $missing_flags_ref, $flag_renames_ref, $line, $number) = @_;
376 # Get string value of qr//-escaped regexps and if requested rename them.
377 my @missing_flags = map {
378 $flag_renames_ref->{$_}
379 } @{$missing_flags_ref};
381 my $flags = join ' ', @missing_flags;
382 printf '%d:', $number if defined $number;
383 printf '%s (%s)%s %s',
384 error_color($message, 'red'), $flags, error_color(':', 'yellow'),
389 sub error_non_verbose_build {
390 my ($line, $number) = @_;
392 printf '%d:', $number if defined $number;
394 error_color('NONVERBOSE BUILD', 'red'),
395 error_color(':', 'yellow'),
400 sub error_invalid_cmake {
404 error_color('INVALID CMAKE', 'red'),
405 error_color(':', 'yellow'),
410 sub error_hardening_wrapper {
412 error_color('HARDENING WRAPPER', 'red'),
413 error_color(':', 'yellow'),
414 'no checks possible, aborting';
419 my ($message, $color) = @_;
422 return Term::ANSIColor::colored($message, $color);
429 my ($line, @flags) = @_;
431 foreach my $flag (@flags) {
432 return 1 if $line =~ /$flag/;
438 my ($line, $missing_flags_ref, @flags) = @_;
440 my @missing_flags = ();
441 foreach my $flag (@flags) {
442 if (not $line =~ /$flag/) {
443 push @missing_flags, $flag;
447 return 1 if scalar @missing_flags == 0;
449 @{$missing_flags_ref} = @missing_flags;
452 # Check if any of \@bad_flags occurs after $good_flag. Doesn't check if
453 # $good_flag is present.
454 sub flag_overwritten {
455 my ($line, $good_flag, $bad_flags) = @_;
457 if (not any_flags_used($line, @{$bad_flags})) {
462 foreach my $flag (@{$bad_flags}) {
463 while ($line =~ /$flag/g) {
464 if ($bad_pos < $+[0]) {
470 while ($line =~ /$good_flag/g) {
473 if ($good_pos > $bad_pos) {
479 sub cppflags_fortify_broken {
480 my ($line, $missing_flags) = @_;
482 # $def_cppflags_fortify[0] must be -D_FORTIFY_SOURCE=2!
483 my $fortify_source = $def_cppflags_fortify[0];
485 # Some build systems enable/disable fortify source multiple times, check
487 if (not flag_overwritten($line,
489 \@def_cppflags_fortify_bad)) {
492 push @{$missing_flags}, $fortify_source;
496 sub cflags_stack_broken {
497 my ($line, $missing_flags, $strong) = @_;
499 my $flag = $strong ? $def_cflags_stack_strong[0]
500 : $def_cflags_stack[0];
502 if (not flag_overwritten($line, $flag, \@def_cflags_stack_bad)) {
505 push @{$missing_flags}, $flag;
509 # Modifies $missing_flags_ref array.
510 sub pic_pie_conflict {
511 my ($line, $pie, $missing_flags_ref, @flags_pie) = @_;
513 return 0 if not $pie;
514 return 0 if not any_flags_used($line, @def_ldflags_pic);
516 my %flags = map { $_ => 1 } @flags_pie;
518 # Remove all PIE flags from @missing_flags as they are not required with
521 not exists $flags{$_}
522 } @{$missing_flags_ref};
523 @{$missing_flags_ref} = @result;
525 # We got a conflict when no flags are left, thus only PIE flags were
526 # missing. If other flags were missing abort because the conflict is not
528 return scalar @result == 0;
531 sub is_non_verbose_build {
532 my ($line, $cargo, $skip_ref, $input_ref, $line_offset, $line_count) = @_;
534 if ($line =~ /$libtool_regex/o) {
535 # libtool's --silent hides the real compiler flags.
536 if ($line =~ /\s--silent/) {
538 # If --silent is not present, skip this line as some compiler flags
539 # might be missing (e.g. -fPIE) which are handled correctly by libtool
540 # internally. libtool displays the real compiler command on the next
541 # line, so the flags are checked as usual.
548 if (not (index($line, 'checking if you want to see long compiling messages... no') == 0
549 or $line =~ /^\s*\[?(?:CC|CCLD|C\+\+|CXX|CXXLD|LD|LINK)\]?\s+(.+?)$/
550 or $line =~ /^\s*[][\/0-9 ]*[Cc]ompiling\s+(.+?)(?:\.\.\.)?$/
551 or $line =~ /^\s*[Bb]uilding (?:program|shared library)\s+(.+?)$/
552 or $line =~ /^\s*\[[\d ]+%\] Building (?:C|CXX) object (.+?)$/)) {
558 # C++ compiler setting.
559 return 0 if $line =~ /^\s*C\+\+.+?:\s+(?:yes|no)\s*$/;
560 return 0 if $line =~ /^\s*C\+\+ Library: stdc\+\+$/;
561 # "Compiling" non binary files.
562 return 0 if $line =~ /^\s*Compiling \S+\.(?:py|pyx|el)['"]?\s*(?:\.\.\.|because it changed\.)?$/;
563 return 0 if $line =~ /^\s*[Cc]ompiling catalog \S+\.po\b/;
565 return 0 if $cargo and $line =~ m{^\s*Compiling\s+\S+\s+v\S+(?:\s+\(/<<PKGBUILDDIR>>\))?$};
566 # "Compiling" with no file name.
567 if ($line =~ /^\s*[Cc]ompiling\s+(.+?)(?:\.\.\.)?$/) {
568 # $file_extension_regex may need spaces around the filename.
569 return 0 if not " $1 " =~ /$file_extension_regex/o;
574 # On the first pass we only check if this line is verbose or not.
575 return 1 if not defined $input_ref;
577 # Second pass, we have access to the next lines.
580 # CMake and other build systems print the non-verbose messages also when
581 # building verbose. If a compiler and the file name occurs in the next
582 # lines, treat it as verbose build.
584 # Get filename, we can't use the complete path as only parts of it are
585 # used in the real compiler command.
586 $file =~ m{/([^/\s]+)$};
589 for (my $i = 1; $i <= $line_count; $i++) {
590 my $next_line = $input_ref->[$line_offset + $i];
591 last unless defined $next_line;
593 if (index($next_line, $file) != -1 and $next_line =~ /$cc_regex/o) {
594 # Not a non-verbose line, but we still have to skip the
595 # current line as it doesn't contain any compiler commands.
605 # Remove @flags from $flag_refs_ref, uses $flag_renames_ref as reference.
607 my ($flag_refs_ref, $flag_renames_ref, @flags) = @_;
609 my %removes = map { $_ => 1 } @flags;
610 foreach my $flags (@{$flag_refs_ref}) {
612 # Flag found as string.
613 not exists $removes{$_}
614 # Flag found as string representation of regexp.
615 and (not defined $flag_renames_ref->{$_}
616 or not exists $removes{$flag_renames_ref->{$_}})
623 # Modifies $flag_renames_ref hash.
624 sub compile_flag_regexp {
625 my ($flag_renames_ref, @flags) = @_;
628 foreach my $flag (@flags) {
629 # Compile flag regexp for faster execution.
630 my $regex = qr/\s(['"]?)$flag\1(?:\s|\\)/;
632 # Store flag name in replacement string for correct flags in messages
633 # with qr//ed flag regexps.
634 $flag_renames_ref->{$regex}
635 = (exists $flag_renames_ref->{$flag})
636 ? $flag_renames_ref->{$flag}
639 push @result, $regex;
644 # Does any extension in @extensions exist in %{$extensions_ref}?
645 sub extension_found {
646 my ($extensions_ref, @extensions) = @_;
648 foreach my $extension (@extensions) {
649 if (exists $extensions_ref->{$extension}) {
659 # Parse command line arguments.
661 my $option_version = 0;
663 my $option_bindnow = 0;
664 my @option_ignore_arch = ();
665 my @option_ignore_flag = ();
666 my @option_ignore_arch_flag = ();
667 my @option_ignore_line = ();
668 my @option_ignore_arch_line = ();
670 my $option_arch = undef;
671 my $option_buildd = 0;
672 my $option_debian = 0;
674 my $option_line_numbers = 0;
675 if (not Getopt::Long::GetOptions(
676 'help|h|?' => \$option_help,
677 'version' => \$option_version,
679 'pie' => \$option_pie,
680 'bindnow' => \$option_bindnow,
681 'all' => \$option_all,
683 'ignore-arch=s' => \@option_ignore_arch,
684 'ignore-flag=s' => \@option_ignore_flag,
685 'ignore-arch-flag=s' => \@option_ignore_arch_flag,
686 'ignore-line=s' => \@option_ignore_line,
687 'ignore-arch-line=s' => \@option_ignore_arch_line,
689 'color' => \$option_color,
690 'arch=s' => \$option_arch,
691 'buildd' => \$option_buildd,
692 'debian' => \$option_debian,
693 'line-numbers' => \$option_line_numbers,
696 Pod::Usage::pod2usage(2);
700 Pod::Usage::pod2usage(1);
702 if ($option_version) {
704 blhc $VERSION Copyright (C) 2012-2024 Simon Ruderich
706 This program is free software: you can redistribute it and/or modify
707 it under the terms of the GNU General Public License as published by
708 the Free Software Foundation, either version 3 of the License, or
709 (at your option) any later version.
711 This program is distributed in the hope that it will be useful,
712 but WITHOUT ANY WARRANTY; without even the implied warranty of
713 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
714 GNU General Public License for more details.
716 You should have received a copy of the GNU General Public License
717 along with this program. If not, see <http://www.gnu.org/licenses/>.
723 if (scalar @ARGV == 0) {
725 Pod::Usage::pod2usage(2);
728 # Don't load Term::ANSIColor in buildd mode because Term::ANSIColor is not
729 # installed on Debian's buildds.
730 if (not $option_buildd) {
731 require Term::ANSIColor;
739 # Precompiled ignores for faster lookup.
740 my %option_ignore_arch_flag = ();
741 my %option_ignore_arch_line = ();
743 # Strip flags which should be ignored.
744 if (scalar @option_ignore_flag > 0) {
745 remove_flags(\@flag_refs, \%flag_renames, @option_ignore_flag);
747 # Same for arch specific ignore flags, but only prepare here.
748 if (scalar @option_ignore_arch_flag > 0) {
749 foreach my $ignore (@option_ignore_arch_flag) {
750 my ($ignore_arch, $ignore_flag) = split /:/, $ignore, 2;
752 if (not $ignore_arch or not $ignore_flag) {
753 printf STDERR 'Value "%s" invalid for option ignore-arch-flag '
754 . '("arch:flag" expected)' . "\n", $ignore;
756 Pod::Usage::pod2usage(2);
759 push @{$option_ignore_arch_flag{$ignore_arch}}, $ignore_flag;
763 # Precompile all flag regexps. any_flags_used(), all_flags_used() get a lot
765 foreach my $flags (@flag_refs_all) {
766 @{$flags} = compile_flag_regexp(\%flag_renames, @{$flags});
769 # Precompile ignore line regexps, also anchor at beginning and end of line.
770 # Additional entries are also extracted from the build log, see below.
771 foreach my $ignore (@option_ignore_line) {
772 $ignore = qr/^$ignore$/;
774 # Same for arch specific ignore lines.
775 if (scalar @option_ignore_arch_line > 0) {
776 foreach my $ignore (@option_ignore_arch_line) {
777 my ($ignore_arch, $ignore_line) = split /:/, $ignore, 2;
779 if (not $ignore_arch or not $ignore_line) {
780 printf STDERR 'Value "%s" invalid for option ignore-arch-line '
781 . '("arch:line" expected)' . "\n", $ignore;
783 Pod::Usage::pod2usage(2);
786 push @{$option_ignore_arch_line{$ignore_arch}}, qr/^$ignore_line$/;
794 foreach my $file (@ARGV) {
795 print "checking '$file'...\n" if scalar @ARGV > 1;
797 -f $file or die "No such file: $file";
799 open my $fh, '<', $file or die $!;
801 # Architecture of this file.
802 my $arch = $option_arch;
804 # Hardening options. Not all architectures support all hardening options.
805 my $harden_format = 1;
806 my $harden_fortify = 1;
807 my $harden_stack = 1;
808 my $harden_stack_strong = 1;
809 my $harden_stack_clash = 1;
810 my $harden_relro = 1;
811 my $harden_bindnow = $option_bindnow; # defaults to 0
812 my $harden_pie = $option_pie; # defaults to 0
814 # Number of parallel jobs to prevent false positives when detecting
815 # non-verbose builds. As not all jobs declare the number of parallel jobs
816 # use a large enough default.
819 # Don't check for PIE flags if automatically applied by the compiler. Only
820 # used in buildd and Debian mode.
821 my $disable_harden_pie = 0;
822 if ($option_debian) {
823 $disable_harden_pie = 1;
826 # Ignore additional false positives if cargo/rust is used
830 while (my $line = <$fh>) {
833 # Detect architecture automatically unless overridden. For buildd logs
834 # only, doesn't use the dpkg-buildpackage header. Necessary to ignore
835 # build logs which aren't built (wrong architecture, build error,
838 if (index($line, 'Build Architecture: ') == 0) {
839 $arch = substr $line, 20, -1; # -1 to ignore '\n' at the end
840 # For old logs (sbuild << 0.63.0-1).
841 } elsif (index($line, 'Architecture: ') == 0) {
842 $arch = substr $line, 14, -1; # -1 to ignore '\n' at the end
846 # dpkg-buildflags only provides hardening flags since 1.16.1, don't
847 # check for hardening flags in buildd mode if an older dpkg-dev is
848 # used. Default flags (-g -O2) are still checked.
850 # Packages which were built before 1.16.1 but used their own hardening
851 # flags are not checked.
853 # Strong stack protector is used since dpkg 1.17.11.
855 # Recent GCC versions automatically use PIE (only on supported
856 # architectures) and dpkg respects this properly since 1.18.15 and
857 # doesn't pass PIE flags manually.
859 and index($line, 'Toolchain package versions: ') == 0) {
860 require Dpkg::Version;
863 my $disable_strong = 1;
864 my $disable_clash = 1;
866 if ($line =~ /\bdpkg-dev_(\S+)/) {
867 if (Dpkg::Version::version_compare($1, '1.16.1') >= 0) {
870 if (Dpkg::Version::version_compare($1, '1.17.11') >= 0) {
873 if (Dpkg::Version::version_compare($1, '1.18.15') >= 0) {
874 $disable_harden_pie = 1;
876 if (Dpkg::Version::version_compare($1, '1.22.0') >= 0) {
889 if ($disable_strong) {
890 $harden_stack_strong = 0;
892 if ($disable_clash) {
893 $harden_stack_clash = 0;
897 # The following two versions of CMake in Debian obeyed CPPFLAGS, but
898 # this was later dropped because upstream rejected the patch. Thus
899 # build logs with these versions will have fortify hardening flags
900 # enabled, even though they may be not correctly set and are missing
901 # when build with later CMake versions. Thanks to Aron Xu for letting
903 if (index($line, 'Package versions: ') == 0
904 and $line =~ /\bcmake_(\S+)/
905 and ($1 eq '2.8.7-1' or $1 eq '2.8.7-2')) {
906 if (not $option_buildd) {
907 error_invalid_cmake($1);
908 $exit |= $exit_code{invalid_cmake};
910 print "$buildd_tag{invalid_cmake}|$1|\n";
914 # Debian's build daemons use "Filtered Build-Depends:" (or just
915 # "Build-Depends:" in older versions) for the build dependencies, but
916 # pbuilder uses "Depends:"; support both.
917 if (index($line, 'Filtered Build-Depends: ') == 0
918 or index($line, 'Build-Depends: ') == 0
919 or index($line, 'Depends: ') == 0) {
920 # If hardening wrapper is used (wraps calls to gcc and adds
921 # hardening flags automatically) we can't perform any checks,
923 if ($line =~ /\bhardening-wrapper\b/) {
924 if (not $option_buildd) {
925 error_hardening_wrapper();
926 $exit |= $exit_code{hardening_wrapper};
928 print "$buildd_tag{hardening_wrapper}||\n";
933 if ($line =~ /\bcargo\b/) {
938 # This flags is not always available, but if it is use it.
939 if ($line =~ /^DEB_BUILD_OPTIONS=.*\bparallel=(\d+)/) {
943 # We skip over unimportant lines at the beginning of the log to
944 # prevent false positives.
945 last if index($line, 'dpkg-buildpackage: ') == 0;
948 # Input lines, contain only the lines with compiler commands.
950 # Non-verbose lines in the input. Used to reduce calls to
951 # is_non_verbose_build() (which is quite slow) in the second loop when
952 # it's already clear if a line is non-verbose or not.
953 my @input_nonverbose = ();
955 my @input_number = ();
957 my $continuation = 0;
958 my $complete_line = undef;
960 while (my $line = <$fh>) {
963 # And stop at the end of the build log. Package details (reported by
964 # the buildd logs) are not important for us. This also prevents false
966 last if index($line, 'Build finished at ') == 0
967 and $line =~ /^Build finished at \d{8}-\d{4}$/;
969 if (not $continuation) {
973 # Detect architecture automatically unless overridden.
975 and index($line, 'dpkg-buildpackage: info: host architecture ') == 0) {
976 $arch = substr $line, 43, -1; # -1 to ignore '\n' at the end
977 # Older versions of dpkg-buildpackage
979 and index($line, 'dpkg-buildpackage: host architecture ') == 0) {
980 $arch = substr $line, 37, -1; # -1 to ignore '\n' at the end
982 # Old buildd logs use e.g. "host architecture is alpha", remove
983 # the "is", otherwise debarch_to_debtriplet() will not detect the
985 if (index($arch, 'is ') == 0) {
986 $arch = substr $arch, 3;
990 # Permit dynamic excludes from within the build log to ignore false
991 # positives. Cannot use a separate config file as we often only have
992 # the build log itself.
993 if (index($line, 'blhc: ignore-line-regexp: ') == 0) {
994 my $ignore = substr $line, 26, -1; # -1 to ignore '\n' at the end
995 push @option_ignore_line, qr/^$ignore$/;
999 next if $line =~ /^\s*#/;
1000 # Ignore compiler warnings for now.
1001 next if $line =~ /$warning_regex/o;
1003 if (not $option_buildd and index($line, "\033") != -1) { # \033 = esc
1004 # Remove all ANSI color sequences which are sometimes used in
1005 # non-verbose builds.
1006 $line = Term::ANSIColor::colorstrip($line);
1007 # Also strip '\0xf' (delete previous character), used by Elinks'
1010 # And "ESC(B" which seems to be used on armhf and hurd (not sure
1012 $line =~ s/\033\(B//g;
1015 # Check if this line indicates a non verbose build.
1017 $non_verbose |= is_non_verbose_build($line, $cargo, \$skip);
1020 # Treat each command as a single line so we don't ignore valid
1021 # commands when handling false positives. split_line() is slow, only
1022 # use it when necessary.
1023 my @line = ($line !~ /(?:;|&&|\|\|)/)
1025 : split_line($line);
1026 foreach my $line (@line) {
1027 if ($continuation) {
1030 # Join lines, but leave the "\" in place so it's clear where
1031 # the original line break was.
1032 chomp $complete_line;
1033 $complete_line .= ' ' . $line;
1035 # Line continuation, line ends with "\".
1036 if ($line =~ /\\$/) {
1038 # Start line continuation.
1039 if (not defined $complete_line) {
1040 $complete_line = $line;
1045 # Use the complete line if a line continuation occurred.
1046 if (defined $complete_line) {
1047 $line = $complete_line;
1048 $complete_line = undef;
1052 # Strip (basic) environment variables for compiler detection. This
1053 # prevents false positives when environment variables contain
1054 # compiler binaries. Nested quotes, command substitution, etc. is
1059 [a-zA-Z_]+ # environment variable name
1062 [^\s"'\$`\\]+ # non-quoted string
1064 '[^"'\$`\\]*' # single-quoted string
1066 "[^"'\$`\\]*" # double-quoted string
1071 # Ignore lines with no compiler commands.
1072 next if not $non_verbose
1073 and not $noenv =~ /$cc_regex_normal/o;
1074 # Ignore lines with no filenames with extensions. May miss some
1075 # non-verbose builds (e.g. "gcc -o test" [sic!]), but shouldn't be
1076 # a problem as the log will most likely contain other non-verbose
1077 # commands which are detected.
1078 next if not $non_verbose
1079 and not $line =~ /$file_extension_regex/o;
1081 # Ignore false positives.
1083 # `./configure` output.
1084 next if not $non_verbose
1085 and $line =~ /^(?:checking|[Cc]onfigure:) /;
1086 next if $line =~ /^\s*(?:Host\s+)?(?:C(?:\+\+)?\s+)?
1087 [Cc]ompiler[\s.]*:?\s+
1089 next if $line =~ m{^\s*(?:-\s)?(?:HOST_)?(?:CC|CXX)
1090 \s*=\s*$cc_regex_full
1091 # optional compiler options, don't allow
1092 # "everything" here to prevent false negatives
1093 \s*(?:\s-\S+)*\s*$}xo;
1094 # `echo` is never a compiler command
1095 next if $line =~ /^\s*echo\s/;
1096 # Ignore calls to `make` because they can contain environment
1097 # variables which look like compiler commands, e.g. CC=).
1098 next if $line =~ /^\s*make\s/;
1099 # `moc-qt4`/`moc-qt5` contain '-I.../linux-g++' in their command
1100 # line (or similar for other architectures) which gets recognized
1101 # as a compiler line, but `moc-qt*` is only a preprocessor for Qt
1102 # C++ files. No hardening flags are relevant during this step,
1103 # thus ignore `moc-qt*` lines. The resulting files will be
1104 # compiled in a separate step (and therefore checked).
1105 next if $line =~ m{^\S+(?:/bin/moc(?:-qt[45])?|/lib/qt6/libexec/moc)
1107 -I\S+/mkspecs/[a-z]+-g\++(?:-64)?
1109 # nvcc is not a regular C compiler
1110 next if $line =~ m{^\S+/bin/nvcc\s};
1111 # Ignore false positives when the line contains only CC=gcc but no
1112 # other gcc command.
1113 if ($line =~ /(.*)CC=$cc_regex_full(.*)/o) {
1116 next if not $before =~ /$cc_regex_normal/o
1117 and not $after =~ /$cc_regex_normal/o;
1119 # Ignore false positives caused by gcc -v. It outputs a line
1120 # looking like a normal compiler line but which is sometimes
1121 # missing hardening flags, although the normal compiler line
1123 next if $line =~ m{^\s+/usr/lib/gcc/$cc_regex_full_prefix/
1124 [0-9.]+/cc1(?:plus)?}xo;
1125 # Ignore false positive with `rm` which may remove files which
1126 # look like a compiler executable thus causing the line to be
1127 # treated as a normal compiler line.
1128 next if $line =~ m{^\s*rm\s+};
1129 next if $line =~ m{^\s*dwz\s+};
1130 # Some build systems emit "gcc > file".
1131 next if $line =~ m{$cc_regex_normal\s*>\s*\S+}o;
1132 # Hex output may contain "cc".
1133 next if $line =~ m#(?:\b[0-9a-fA-F]{2,}\b\s*){5}#;
1134 # Meson build output
1135 next if $line =~ /^C\+\+ linker for the host machine: /;
1136 # Embedded `gcc -print-*` commands
1137 next if $line =~ /`$cc_regex_normal\s*[^`]*-print-\S+`/;
1138 # cmake checking for compiler flags without setting CPPFLAGS
1139 next if $line =~ m{^\s*/usr/(bin|lib)/(ccache/)?c\+\+ (?:-std=\S+ )?-dM -E -c /usr/share/cmake-\S+/Modules/CMakeCXXCompilerABI\.cpp};
1140 # Some rustc lines look like linker commands
1141 next if $cargo and $line =~ /$rustc_regex/o;
1143 # Check if additional hardening options were used. Used to ensure
1144 # they are used for the complete build.
1145 $harden_pie = 1 if any_flags_used($line, @def_cflags_pie,
1147 $harden_bindnow = 1 if any_flags_used($line, @def_ldflags_bindnow);
1150 push @input_nonverbose, $non_verbose;
1151 push @input_number, $number if $option_line_numbers;
1155 close $fh or die $!;
1157 # Ignore arch if requested.
1158 if (scalar @option_ignore_arch > 0 and $arch) {
1159 foreach my $ignore (@option_ignore_arch) {
1160 if ($arch eq $ignore) {
1161 print "ignoring architecture '$arch'\n";
1167 if (scalar @input == 0) {
1168 if (not $option_buildd) {
1169 print "No compiler commands!\n";
1170 $exit |= $exit_code{no_compiler_commands};
1172 print "$buildd_tag{no_compiler_commands}||\n";
1177 if ($option_buildd) {
1178 $statistics{commands} += scalar @input;
1181 # Option or auto detected.
1183 # The following was partially copied from dpkg-dev 1.22.0
1184 # (/usr/share/perl5/Dpkg/Vendor/Debian.pm, set_build_features and
1185 # _add_build_flags()), copyright Raphaël Hertzog <hertzog@debian.org>,
1186 # Guillem Jover <guillem@debian.org>, Kees Cook <kees@debian.org>,
1187 # Canonical, Ltd. licensed under GPL version 2 or later. Keep it in
1192 # Recent dpkg versions use a quadruplet for arch. Support both.
1194 (undef, undef, $os, $cpu) = Dpkg::Arch::debarch_to_debtuple($arch);
1197 (undef, $os, $cpu) = Dpkg::Arch::debarch_to_debtriplet($arch);
1200 my %builtin_pie_arch = map { $_ => 1 } qw(
1232 # Disable unsupported hardening options.
1233 if ($disable_harden_pie and exists $builtin_pie_arch{$arch}) {
1236 if ($os !~ /^(?:linux|kfreebsd|hurd)$/
1237 or $cpu =~ /^(?:alpha|hppa|ia64)$/) {
1240 if ($cpu =~ /^(?:ia64|alpha|hppa|nios2)$/ or $arch eq 'arm') {
1242 $harden_stack_strong = 0;
1244 if ($arch !~ /^(?:amd64|arm64|armhf|armel)$/) {
1245 $harden_stack_clash = 0;
1247 if ($cpu =~ /^(?:ia64|hppa)$/) {
1249 $harden_bindnow = 0;
1254 my @cflags = @def_cflags;
1255 my @cxxflags = @def_cxxflags;
1256 my @cppflags = @def_cppflags;
1257 my @ldflags = @def_ldflags;
1258 # Check the specified hardening options, same order as dpkg-buildflags.
1260 @cflags = (@cflags, @def_cflags_pie);
1261 @cxxflags = (@cxxflags, @def_cflags_pie);
1262 @ldflags = (@ldflags, @def_ldflags_pie);
1264 if ($harden_stack_strong) {
1265 @cflags = (@cflags, @def_cflags_stack_strong);
1266 @cxxflags = (@cxxflags, @def_cflags_stack_strong);
1267 } elsif ($harden_stack) {
1268 @cflags = (@cflags, @def_cflags_stack);
1269 @cxxflags = (@cxxflags, @def_cflags_stack);
1271 if ($harden_stack_clash) {
1272 @cflags = (@cflags, @def_cflags_stack_clash);
1273 @cxxflags = (@cxxflags, @def_cflags_stack_clash);
1275 if ($harden_fortify) {
1276 @cflags = (@cflags, @def_cflags_fortify);
1277 @cxxflags = (@cxxflags, @def_cflags_fortify);
1278 @cppflags = (@cppflags, @def_cppflags_fortify);
1280 if ($harden_format) {
1281 @cflags = (@cflags, @def_cflags_format);
1282 @cxxflags = (@cxxflags, @def_cflags_format);
1284 if ($harden_relro) {
1285 @ldflags = (@ldflags, @def_ldflags_relro);
1287 if ($harden_bindnow) {
1288 @ldflags = (@ldflags, @def_ldflags_bindnow);
1291 # Ada doesn't support format hardening flags, see #680117 for more
1292 # information. Same for fortran.
1294 my @cflags_noformat = grep {
1296 foreach my $flag (@def_cflags_format) {
1297 $ok = 0 if $_ eq $flag;
1302 # Hack to fix cppflags_fortify_broken() if --ignore-flag
1303 # -D_FORTIFY_SOURCE=2 is used to ignore missing fortification. Only works
1304 # as long as @def_cppflags_fortify contains only one variable.
1305 if (scalar @def_cppflags_fortify == 0) {
1306 $harden_fortify = 0;
1309 # Ignore flags for this arch if requested.
1310 if ($arch and exists $option_ignore_arch_flag{$arch}) {
1311 my @local_flag_refs = (\@cflags, \@cxxflags, \@cppflags, \@ldflags);
1313 remove_flags(\@local_flag_refs,
1315 @{$option_ignore_arch_flag{$arch}});
1318 my @ignore_line = @option_ignore_line;
1319 # Ignore lines for this arch if requested.
1320 if ($arch and exists $option_ignore_arch_line{$arch}) {
1321 @ignore_line = (@ignore_line, @{$option_ignore_arch_line{$arch}});
1325 for (my $i = 0; $i < scalar @input; $i++) {
1326 my $line = $input[$i];
1328 # Ignore line if requested.
1329 foreach my $ignore (@ignore_line) {
1330 next LINE if $line =~ /$ignore/;
1334 if ($input_nonverbose[$i]
1335 and is_non_verbose_build($line, $cargo, \$skip,
1336 \@input, $i, $parallel)) {
1337 if (not $option_buildd) {
1338 error_non_verbose_build($line, $input_number[$i]);
1339 $exit |= $exit_code{non_verbose_build};
1341 $statistics{commands_nonverbose}++;
1345 # Even if it's a verbose build, we might have to skip this line (see
1346 # is_non_verbose_build()).
1349 my $orig_line = $line;
1351 # Remove everything until and including the compiler command. Makes
1352 # checks easier and faster.
1353 $line =~ s/^.*?$cc_regex//o;
1354 # "([...] test.c)" is not detected as 'test.c' - fix this by removing
1355 # the brace and similar characters at the line end.
1356 $line =~ s/['")]+$//;
1358 # Skip unnecessary tests when only preprocessing.
1359 my $flag_preprocess = 0;
1366 # Preprocess, compile, assemble.
1367 if ($line =~ /\s(-E|-S|-c)\b/) {
1369 $flag_preprocess = 1 if $1 eq '-E';
1370 $compile = 1 if $1 eq '-S' or $1 eq '-c';
1371 # Dependency generation for Makefiles. The other flags (-MF -MG -MP
1372 # -MT -MQ) are always used with -M/-MM.
1373 } elsif ($line =~ /\s(?:-M|-MM)\b/) {
1375 # Otherwise assume we are linking.
1380 # -MD/-MMD also cause dependency generation, but they don't imply -E!
1381 if ($line =~ /\s(?:-MD|-MMD)\b/) {
1383 $flag_preprocess = 0;
1386 # Dependency generation for Makefiles, no preprocessing or other flags
1388 next if $dependency;
1390 # Get all file extensions on this line.
1391 my @extensions = $line =~ /$file_extension_regex/go;
1392 # Ignore all unknown extensions to speedup the search below.
1393 @extensions = grep { exists $extension{$_} } @extensions;
1395 # These file types don't require preprocessing.
1396 if (extension_found(\%extensions_no_preprocess, @extensions)) {
1399 # These file types require preprocessing.
1400 if (extension_found(\%extensions_preprocess, @extensions)) {
1401 # Prevent false positives with "libtool: link: g++ -include test.h
1402 # .." compiler lines.
1403 if ($orig_line !~ /$libtool_link_regex/o) {
1408 if (not $flag_preprocess) {
1409 # If there are source files then it's compiling/linking in one
1410 # step and we must check both. We only check for source files
1411 # here, because header files cause too many false positives.
1412 if (extension_found(\%extensions_compile_link, @extensions)) {
1413 # Assembly files don't need CFLAGS.
1414 if (not extension_found(\%extensions_compile, @extensions)
1415 and extension_found(\%extensions_no_compile, @extensions)) {
1417 # But the rest does.
1421 # No compilable extensions found, either linking or compiling
1424 # If there are also no object files we are just compiling headers
1425 # (.h -> .h.gch). Don't check for linker flags in this case. Due
1426 # to our liberal checks for compiler lines, this also reduces the
1427 # number of false positives considerably.
1429 and not extension_found(\%extensions_object, @extensions)) {
1434 my $compile_cpp = 0;
1435 my $restore_cflags = 0;
1436 # Assume CXXFLAGS are required when a C++ file is specified in the
1439 and extension_found(\%extensions_compile_cpp, @extensions)) {
1442 # Ada needs special CFLAGS
1443 } elsif (extension_found(\%extensions_ada, @extensions)) {
1444 $restore_cflags = 1;
1445 $preprocess = 0; # Ada uses no CPPFLAGS
1446 @cflags_backup = @cflags;
1447 @cflags = @cflags_noformat;
1449 } elsif (extension_found(\%extensions_fortran, @extensions)) {
1450 $restore_cflags = 1;
1451 @cflags_backup = @cflags;
1452 @cflags = @cflags_noformat;
1455 if ($option_buildd) {
1456 $statistics{preprocess}++ if $preprocess;
1457 $statistics{compile}++ if $compile;
1458 $statistics{compile_cpp}++ if $compile_cpp;
1459 $statistics{link}++ if $link;
1462 # Check if there are flags indicating a debug build. If that's true,
1463 # skip the check for -O2. This prevents fortification, but that's fine
1464 # for a debug build.
1465 if (any_flags_used($line, @def_cflags_debug)) {
1466 remove_flags([\@cflags], \%flag_renames, $def_cflags[1]);
1467 remove_flags([\@cppflags], \%flag_renames, $def_cppflags_fortify[0]);
1470 # Check hardening flags.
1472 if ($compile and (not all_flags_used($line, \@missing, @cflags)
1473 or (($harden_stack or $harden_stack_strong)
1474 and cflags_stack_broken($line, \@missing,
1475 $harden_stack_strong)))
1476 # Libraries linked with -fPIC don't have to (and can't) be
1477 # linked with -fPIE as well. It's no error if only PIE flags
1479 and not pic_pie_conflict($line, $harden_pie, \@missing, @def_cflags_pie)
1480 # Assume dpkg-buildflags returns the correct flags.
1481 and index($line, '`dpkg-buildflags --get CFLAGS`') == -1) {
1482 if (not $option_buildd) {
1483 error_flags('CFLAGS missing', \@missing, \%flag_renames,
1484 $input[$i], $input_number[$i]);
1485 $exit |= $exit_code{flags_missing};
1487 $statistics{compile_missing}++;
1489 } elsif ($compile_cpp and not all_flags_used($line, \@missing, @cflags)
1490 # Libraries linked with -fPIC don't have to (and can't) be
1491 # linked with -fPIE as well. It's no error if only PIE flags
1493 and not pic_pie_conflict($line, $harden_pie, \@missing, @def_cflags_pie)
1494 # Assume dpkg-buildflags returns the correct flags.
1495 and index($line, '`dpkg-buildflags --get CXXFLAGS`') == -1) {
1496 if (not $option_buildd) {
1497 error_flags('CXXFLAGS missing', \@missing, \%flag_renames,
1498 $input[$i], $input_number[$i]);
1499 $exit |= $exit_code{flags_missing};
1501 $statistics{compile_cpp_missing}++;
1505 and (not all_flags_used($line, \@missing, @cppflags)
1506 # The fortify flag might be overwritten, detect that.
1508 and cppflags_fortify_broken($line, \@missing)))
1509 # Assume dpkg-buildflags returns the correct flags.
1510 and index($line, '`dpkg-buildflags --get CPPFLAGS`') == -1) {
1511 if (not $option_buildd) {
1512 error_flags('CPPFLAGS missing', \@missing, \%flag_renames,
1513 $input[$i], $input_number[$i]);
1514 $exit |= $exit_code{flags_missing};
1516 $statistics{preprocess_missing}++;
1519 if ($link and not all_flags_used($line, \@missing, @ldflags)
1520 # Same here, -fPIC conflicts with -fPIE.
1521 and not pic_pie_conflict($line, $harden_pie, \@missing, @def_ldflags_pie)
1522 # Assume dpkg-buildflags returns the correct flags.
1523 and index($line, '`dpkg-buildflags --get LDFLAGS`') == -1) {
1524 if (not $option_buildd) {
1525 error_flags('LDFLAGS missing', \@missing, \%flag_renames,
1526 $input[$i], $input_number[$i]);
1527 $exit |= $exit_code{flags_missing};
1529 $statistics{link_missing}++;
1533 # Restore normal CFLAGS.
1534 if ($restore_cflags) {
1535 @cflags = @cflags_backup;
1540 # Print statistics for buildd mode, only output in this mode.
1541 if ($option_buildd) {
1544 if ($statistics{preprocess_missing}) {
1545 push @warning, sprintf 'CPPFLAGS %d (of %d)',
1546 $statistics{preprocess_missing},
1547 $statistics{preprocess};
1549 if ($statistics{compile_missing}) {
1550 push @warning, sprintf 'CFLAGS %d (of %d)',
1551 $statistics{compile_missing},
1552 $statistics{compile};
1554 if ($statistics{compile_cpp_missing}) {
1555 push @warning, sprintf 'CXXFLAGS %d (of %d)',
1556 $statistics{compile_cpp_missing},
1557 $statistics{compile_cpp};
1559 if ($statistics{link_missing}) {
1560 push @warning, sprintf 'LDFLAGS %d (of %d)',
1561 $statistics{link_missing},
1564 if (scalar @warning) {
1565 local $" = ', '; # array join string
1566 print "$buildd_tag{flags_missing}|@warning missing|\n";
1569 if ($statistics{commands_nonverbose}) {
1570 printf "$buildd_tag{non_verbose_build}|%d (of %d) hidden|\n",
1571 $statistics{commands_nonverbose},
1572 $statistics{commands},
1584 blhc - build log hardening check, checks build logs for missing hardening flags
1588 B<blhc> [I<options>] I<< <dpkg-buildpackage build log file>.. >>
1592 blhc is a small tool which checks build logs for missing hardening flags. It's
1593 licensed under the GPL 3 or later.
1595 It's designed to check build logs generated by Debian's dpkg-buildpackage (or
1596 tools using dpkg-buildpackage like pbuilder or sbuild (which is used for the
1597 official buildd build logs)) to help maintainers detect missing hardening
1598 flags in their packages.
1600 Only gcc is detected as compiler at the moment. If other compilers support
1601 hardening flags as well, please report them.
1603 If there's no output, no flags are missing and the build log is fine.
1605 See F<README> for details about performed checks, auto-detection and
1608 =head1 FALSE POSITIVES
1610 To suppress false positives you can embed the following string in the build
1613 blhc: ignore-line-regexp: REGEXP
1615 All lines fully matching REGEXP (see B<--ignore-line> for details) will be
1616 ignored. The string can be embedded multiple times to ignore different
1619 Please use this feature sparingly so that missing flags are not overlooked. If
1620 you find false positives which affect more packages please report a bug.
1622 To generate this string simply use echo in C<debian/rules>; make sure to use @
1623 to suppress the echo command itself as it could also trigger a false positive.
1624 If the build process takes a long time edit the C<.build> file in place and
1625 tweak the ignore string until B<blhc --all --debian package.build> no longer
1626 reports any false positives.
1634 Force check for all +all (+pie, +bindnow) hardening flags. By default it's
1637 =item B<--arch> I<architecture>
1639 Set the specific architecture (e.g. amd64, armel, etc.), automatically
1640 disables hardening flags not available on this architecture. Is detected
1641 automatically if dpkg-buildpackage is used.
1645 Force check for all +bindnow hardening flags. By default it's auto detected.
1649 Special mode for buildds when automatically parsing log files. The following
1650 changes are in effect:
1656 Print tags instead of normal warnings, see L</"BUILDD TAGS"> for a list of
1661 Don't check hardening flags in old log files (if dpkg-dev << 1.16.1 is
1666 Don't require Term::ANSIColor.
1670 Return exit code 0, unless there was a error (-I, -W messages don't count as
1677 Apply Debian-specific settings. At the moment this only disables checking for
1678 PIE which is automatically applied by Debian's GCC and no longer requires a
1679 compiler command line argument.
1683 Use colored (ANSI) output for warning messages.
1685 =item B<--line-numbers>
1687 Display line numbers.
1689 =item B<--ignore-arch> I<arch>
1691 Ignore build logs from architectures matching I<arch>. I<arch> is a string.
1693 Used to prevent false positives. This option can be specified multiple times.
1695 =item B<--ignore-arch-flag> I<arch>:I<flag>
1697 Like B<--ignore-flag>, but only ignore flag on I<arch>.
1699 =item B<--ignore-arch-line> I<arch>:I<line>
1701 Like B<--ignore-line>, but only ignore line on I<arch>.
1703 =item B<--ignore-flag> I<flag>
1705 Don't print an error when the specific flag is missing in a compiler line.
1706 I<flag> is a string.
1708 Used to prevent false positives. This option can be specified multiple times.
1710 =item B<--ignore-line> I<regex>
1712 Ignore lines matching the given Perl regex. I<regex> is automatically anchored
1713 at the beginning and end of the line to prevent false negatives.
1715 B<NOTE>: Not the input lines are checked, but the lines which are displayed in
1716 warnings (which have line continuation resolved).
1718 Used to prevent false positives. This option can be specified multiple times.
1722 Force check for all +pie hardening flags. By default it's auto detected.
1724 =item B<-h -? --help>
1726 Print available options.
1730 Print version number and license.
1734 Auto detection for B<--pie> and B<--bindnow> only works if at least one
1735 command uses the required hardening flag (e.g. -fPIE). Then it's required for
1736 all other commands as well.
1740 Normal usage, parse a single log file.
1742 blhc path/to/log/file
1744 If there's no output, no flags are missing and the build log is fine.
1746 Parse multiple log files. The exit code is ORed over all files.
1748 blhc path/to/directory/with/log/files/*
1750 Don't treat missing C<-g> as error:
1752 blhc --ignore-flag -g path/to/log/file
1754 Don't treat missing C<-pie> on kfreebsd-amd64 as error:
1756 blhc --ignore-arch-flag kfreebsd-amd64:-pie path/to/log/file
1758 Ignore lines consisting exactly of C<./script gcc file> which would cause a
1761 blhc --ignore-line '\./script gcc file' path/to/log/file
1763 Ignore lines matching C<./script gcc file> somewhere in the line.
1765 blhc --ignore-line '.*\./script gcc file.*' path/to/log/file
1767 Use blhc with pbuilder.
1769 pbuilder path/to/package.dsc | tee path/log/file
1770 blhc path/to/file || echo flags missing
1772 Assume this build log was created on a Debian system and thus don't warn about
1773 missing PIE flags if the current architecture injects them automatically (this
1774 is enabled in buildd mode per default). C<--arch> is necessary if the build
1775 log contains no architecture information as written by dpkg-buildpackage.
1777 blhc --debian --all --arch=amd64 path/to/log/file
1781 The following tags are used in I<--buildd> mode. In braces the additional data
1786 =item B<I-hardening-wrapper-used>
1788 The package uses hardening-wrapper which intercepts calls to gcc and adds
1789 hardening flags. The build log doesn't contain any hardening flags and thus
1790 can't be checked by blhc.
1792 =item B<W-compiler-flags-hidden> (summary of hidden lines)
1794 Build log contains lines which hide the real compiler flags. For example:
1801 Most of the time either C<export V=1> or C<export verbose=1> in
1802 F<debian/rules> fixes builds with hidden compiler flags. Sometimes C<.SILENT>
1803 in a F<Makefile> must be removed. And as last resort the F<Makefile> must be
1804 patched to remove the C<@>s hiding the real compiler commands.
1806 =item B<W-dpkg-buildflags-missing> (summary of missing flags)
1808 CPPFLAGS, CFLAGS, CXXFLAGS, LDFLAGS missing.
1810 =item B<I-invalid-cmake-used> (version)
1812 By default CMake ignores CPPFLAGS thus missing those hardening flags. Debian
1813 patched CMake in versions 2.8.7-1 and 2.8.7-2 to respect CPPFLAGS, but this
1814 patch was rejected by upstream and later reverted in Debian. Thus those two
1815 versions show correct usage of CPPFLAGS even if the package doesn't correctly
1816 handle them (for example by passing them to CFLAGS). To prevent false
1817 negatives just blacklist those two versions.
1819 =item B<I-no-compiler-commands>
1821 No compiler commands were detected. Either the log contains none or they were
1822 not correctly detected by blhc (please report the bug in this case).
1828 The exit status is a "bit mask", each listed status is ORed when the error
1829 condition occurs to get the result.
1839 No compiler commands were found.
1843 Invalid arguments/options given to blhc.
1851 Missing hardening flags.
1855 Hardening wrapper detected, no tests performed.
1859 Invalid CMake version used. See B<I-invalid-cmake-used> under L</"BUILDD
1860 TAGS"> for a detailed explanation.
1866 Simon Ruderich, E<lt>simon@ruderich.orgE<gt>
1868 Thanks to to Bernhard R. Link E<lt>brlink@debian.orgE<gt> and Jaria Alto
1869 E<lt>jari.aalto@cante.netE<gt> for their valuable input and suggestions.
1871 =head1 LICENSE AND COPYRIGHT
1873 Copyright (C) 2012-2024 by Simon Ruderich
1875 This program is free software: you can redistribute it and/or modify
1876 it under the terms of the GNU General Public License as published by
1877 the Free Software Foundation, either version 3 of the License, or
1878 (at your option) any later version.
1880 This program is distributed in the hope that it will be useful,
1881 but WITHOUT ANY WARRANTY; without even the implied warranty of
1882 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1883 GNU General Public License for more details.
1885 You should have received a copy of the GNU General Public License
1886 along with this program. If not, see <http://www.gnu.org/licenses/>.
1890 L<hardening-check(1)>, L<dpkg-buildflags(1)>