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 Term::ANSIColor ();
27 our $VERSION = '0.01';
32 # Regex to catch compiler commands.
33 my $cc_regex = qr/(?:x86_64-linux-gnu-)?(?:(?<!\.)cc|gcc|g\+\+|c\+\+)(?:-[\d.]+)?/;
34 # Regex to catch (GCC) compiler warnings.
35 my $warning_regex = qr/^(.+?):([0-9]+):[0-9]+: warning: (.+?) \[(.+?)\]$/;
37 # Expected (hardening) flags. All flags are used as regexps.
45 '-Werror=format-security',
47 my @cflags_fortify = (
48 # fortify needs at least -O1, but -O2 is recommended anyway
52 '--param=ssp-buffer-size=4',
58 my @cppflags_fortify = (
59 '-D_FORTIFY_SOURCE=2',
65 my @ldflags_bindnow = (
72 # Renaming rules for the output so the regex parts are not visible.
75 '-Wl,(-z,)?relro' => '-Wl,-z,relro',
76 '-Wl,(-z,)?now' => '-Wl,-z,now',
83 my ($message, $missing_flags_ref, $flag_renames_ref, $line) = @_;
85 # Rename flags if requested.
86 my @missing_flags = map {
87 (exists $flag_renames_ref->{$_})
88 ? $flag_renames_ref->{$_}
90 } @{$missing_flags_ref};
92 my $flags = join ' ', @missing_flags;
93 printf "%s (%s)%s %s",
94 error_color($message, 'red'), $flags, error_color(':', 'yellow'),
97 sub error_non_verbose_build {
101 error_color('NONVERBOSE BUILD', 'red'),
102 error_color(':', 'yellow'),
106 my ($message, $color) = @_;
108 # Use colors when writing to a terminal.
110 return Term::ANSIColor::colored($message, $color);
117 my ($line, @flags) = @_;
119 foreach my $flag (@flags) {
120 return 1 if $line =~ /\s$flag(?:\s|\\|$)/;
126 my ($line, $missing_flags_ref, @flags) = @_;
128 my @missing_flags = ();
129 foreach my $flag (@flags) {
130 if ($line !~ /\s$flag(?:\s|\\|$)/) {
131 push @missing_flags, $flag;
135 if (scalar @missing_flags == 0) {
139 @{$missing_flags_ref} = @missing_flags;
143 # Modifies $missing_flags_ref array.
144 sub pic_pie_conflict {
145 my ($line, $pie, $missing_flags_ref, @flags_pie) = @_;
147 return 0 if not $pie;
148 return 0 if not any_flags_used($line, ('-fPIC', '-fpic'));
150 my %flags = map { $_ => 1 } @flags_pie;
152 # Remove all PIE flags from @missing_flags as they are not required with
155 not exists $flags{$_}
156 } @{$missing_flags_ref};
157 @{$missing_flags_ref} = @result;
159 # We got a conflict when no flags are left, thus only PIE flags were
160 # missing. If other flags were missing abort because the conflict is not
162 return scalar @result == 0;
165 sub is_non_verbose_build {
166 my ($line, $next_line, $skip_ref) = @_;
168 if (not ($line =~ /^checking if you want to see long compiling messages\.\.\. no/
169 or $line =~ /^\s*\[?(?:CC|CCLD|LD)\]?\s+(.+?)$/
170 or $line =~ /^\s*(?:C|c)ompiling\s+(.+?)(?:\.\.\.)?$/
171 or $line =~ /^\s*(?:B|b)uilding (?:program|shared library)\s+(.+?)$/
172 or $line =~ /^\s*\[[\d ]+%\] Building (?:C|CXX) object (.+?)$/)) {
178 # On the first pass we only check if this line is verbose or not.
179 return 1 if not defined $next_line;
181 # Second pass, we have access to the next line.
184 # CMake and other build systems print the non-verbose messages also when
185 # building verbose. If a compiler and the file name occurs in the next
186 # line, treat it as verbose build.
188 # Get filename, we can't use the complete path as only parts of it are
189 # used in the real compiler command.
190 $file =~ m{/([a-zA-Z0-9._-]+)$};
193 if ($next_line =~ /\Q$file\E/ and $next_line =~ /$cc_regex/) {
194 # We still have to skip the current line as it doesn't contain any
207 # Hardening options. Not all architectures support all hardening options.
208 my $harden_format = 1;
209 my $harden_fortify = 1;
210 my $harden_stack = 1;
211 my $harden_relro = 1;
212 my $harden_bindnow = 0;
215 # Parse command line arguments.
217 my $option_version = 0;
219 my $option_arch = undef;
220 if (not Getopt::Long::GetOptions(
221 'help|h|?' => \$option_help,
222 'version' => \$option_version,
224 'pie' => \$harden_pie,
225 'bindnow' => \$harden_bindnow,
226 'all' => \$option_all,
228 'arch' => \$option_arch,
231 Pod::Usage::pod2usage(2);
235 Pod::Usage::pod2usage(1);
237 if ($option_version) {
238 print "blhc $VERSION Copyright (C) 2012 Simon Ruderich
240 This program is free software: you can redistribute it and/or modify
241 it under the terms of the GNU General Public License as published by
242 the Free Software Foundation, either version 3 of the License, or
243 (at your option) any later version.
245 This program is distributed in the hope that it will be useful,
246 but WITHOUT ANY WARRANTY; without even the implied warranty of
247 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
248 GNU General Public License for more details.
250 You should have received a copy of the GNU General Public License
251 along with this program. If not, see <http://www.gnu.org/licenses/>.
264 # Input lines, contain only the lines with compiler commands.
268 my $continuation = 0;
269 my $complete_line = undef;
270 while (my $line = <>) {
271 # We skip over unimportant lines at the beginning to prevent false
273 $start = 1 if $line =~ /^dpkg-buildpackage:/;
276 # Detect architecture automatically unless overridden.
278 and $line =~ /^dpkg-buildpackage: host architecture (.+)$/) {
282 # Ignore compiler warnings for now.
283 next if $line =~ /$warning_regex/;
285 # Remove all ANSI color sequences which are sometimes used in non-verbose
287 $line = Term::ANSIColor::colorstrip($line);
288 # Also strip '\0xf' (delete previous character), used by Elink's build
291 # And "ESC(B" which seems to be used on armhf and hurd (not sure what it
293 $line =~ s/\033\(B//g;
295 # Check if this line indicates a non verbose build.
296 my $non_verbose = is_non_verbose_build($line);
298 # One line may contain multiple commands (";"). Treat each one as single
300 my @line = split /(?<!\\);/, $line;
301 foreach $line (@line) {
302 # Add newline, drop all other whitespace at the end of a line.
309 # Join lines, but leave the "\" in place so it's clear where the
310 # original line break was.
311 chomp $complete_line;
312 $complete_line .= ' ' . $line;
314 # Line continuation, line ends with "\".
315 if ($line =~ /\\\s*$/) {
317 # Start line continuation.
318 if (not defined $complete_line) {
319 $complete_line = $line;
324 if (not $continuation) {
325 # Use the complete line if a line continuation occurred.
326 if (defined $complete_line) {
327 $line = $complete_line;
328 $complete_line = undef;
331 # Ignore lines with no compiler commands.
332 next if $line !~ /\b$cc_regex(?:\s|\\)/ and not $non_verbose;
334 # Ignore false positives.
336 # `./configure` output.
337 next if not $non_verbose and $line =~ /^checking /;
338 next if $line =~ /^\s*(?:C\s+)?
339 (?:C|c)ompiler[\s.]*:\s+
341 (?:\s-std=[a-z0-9:+]+)?\s*$
343 or $line =~ /^\s*(?:- )?(?:CC|CXX)\s*=\s*$cc_regex\s*$/
344 or $line =~ /^\s*-- Check for working (?:C|CXX) compiler: /
345 or $line =~ /^\s*(?:echo )?Using [A-Z_]+\s*=\s*/;
352 if (scalar @input == 0) {
353 print "No compiler commands!\n";
358 # Option or auto detected.
360 # The following was partially copied from dpkg-dev 1.16.1.2
361 # (/usr/share/perl5/Dpkg/Vendor/Debian.pm, add_hardening_flags()),
362 # copyright Raphaƫl Hertzog <hertzog@debian.org>, Kees Cook
363 # <kees@debian.org>, Canonical, Ltd. licensed under GPL version 2 or
364 # later. Keep it in sync.
367 my ($abi, $os, $cpu) = Dpkg::Arch::debarch_to_debtriplet($option_arch);
369 # Disable unsupported hardening options.
370 if ($cpu =~ /^(ia64|alpha|mips|mipsel|hppa)$/ or $option_arch eq 'arm') {
373 if ($cpu =~ /^(ia64|hppa|avr32)$/) {
379 # Check if additional hardening options were used. Used to ensure they are
380 # used for the complete build.
381 foreach my $line (@input) {
382 $harden_pie = 1 if any_flags_used($line, @cflags_pie, @ldflags_pie);
383 $harden_bindnow = 1 if any_flags_used($line, @ldflags_bindnow);
386 # Check the specified hardening options, same order as dpkg-buildflags.
388 @cflags = (@cflags, @cflags_pie);
389 @ldflags = (@ldflags, @ldflags_pie);
392 @cflags = (@cflags, @cflags_stack);
394 if ($harden_fortify) {
395 @cflags = (@cflags, @cflags_fortify);
396 @cppflags = (@cppflags, @cppflags_fortify);
398 if ($harden_format) {
399 @cflags = (@cflags, @cflags_format);
402 @ldflags = (@ldflags, @ldflags_relro);
404 if ($harden_bindnow) {
405 @ldflags = (@ldflags, @ldflags_bindnow);
408 for (my $i = 0; $i < scalar @input; $i++) {
409 my $line = $input[$i];
412 if (is_non_verbose_build($line, $input[$i + 1], \$skip)) {
413 error_non_verbose_build($line);
417 # Even if it's a verbose build, we might have to skip this line.
421 # Is this a compiler or linker command?
426 if ($line =~ m{\s-o # -o
427 [\s\\]*\s+ # possible line continuation
428 (?:[/.A-Za-z0-9~_-]+/)? # path to file
429 [A-Za-z0-9~_-]+ # binary name (no dots!)
430 (?:[0-9.]*\.so[0-9.]*[a-z]? # library (including version)
432 |\.cgi)? # CGI binary
433 (?:\s|\\|$) # end of file name
435 or $line =~ /^libtool: link: /
436 or $line =~ m{\s*/bin/bash .+?libtool\s+(.+?\s+)?--mode=(re)?link}) {
441 # If there are source files then it's compiling/linking in one step and we
443 if ($line =~ /\.(?:c|cc|cpp)\b/) {
447 # Check hardening flags.
449 if ($compiler and not all_flags_used($line, \@missing, @cflags)
450 # Libraries linked with -fPIC don't have to (and can't) be linked
451 # with -fPIE as well. It's no error if only PIE flags are missing.
452 and not pic_pie_conflict($line, $harden_pie, \@missing, @cflags_pie)) {
453 error_flags('CFLAGS missing', \@missing, \%flag_renames, $line);
456 if ($compiler and not all_flags_used($line, \@missing, @cppflags)) {
457 error_flags('CPPFLAGS missing', \@missing, \%flag_renames, $line);
460 if ($linker and not all_flags_used($line, \@missing, @ldflags)
461 # Same here, -fPIC conflicts with -fPIE.
462 and not pic_pie_conflict($line, $harden_pie, \@missing, @ldflags_pie)) {
463 error_flags('LDFLAGS missing', \@missing, \%flag_renames, $line);
475 blhc - build log hardening check, checks build logs for missing hardening flags
479 B<blhc> [-h -? --help]
481 B<blhc> [--pie] [--bindnow] [--all]
483 --help available options
484 --version version number and license
485 --pie force +pie check
486 --bindnow force +bindbow check
487 --all force +all (+pie, +bindnow) check
488 --arch set architecture (autodetected)
492 blhc is a small tool which checks build logs for missing hardening flags and
493 other important warnings. It's licensed under the GPL 3 or later.
499 =item B<-h -? --help>
501 Print available options.
505 Print version number and license.
509 Force check for all +pie hardening flags. By default it's auto detected.
513 Force check for all +bindnow hardening flags. By default it's auto detected.
517 Force check for all +all (+pie, +bindnow) hardening flags. By default it's
522 Set the specific architecture (e.g. amd64, armel, etc.), automatically
523 disables hardening flags not available on this architecture. Is detected
524 automatically if dpkg-buildpackage is used.
528 Auto detection only works if at least one command uses the required hardening
529 flag (e.g. -fPIE). Then it's required for all other commands as well.
533 The exit status is a "bit mask", each listed status is ORed when the error
534 condition occurs to get the result.
544 No compiler commands were found.
548 Invalid arguments/options given to blhc.
556 Missing hardening flags.
562 Simon Ruderich, E<lt>simon@ruderich.orgE<gt>
564 =head1 COPYRIGHT AND LICENSE
566 Copyright (C) 2012 by Simon Ruderich
568 This program is free software: you can redistribute it and/or modify
569 it under the terms of the GNU General Public License as published by
570 the Free Software Foundation, either version 3 of the License, or
571 (at your option) any later version.
573 This program is distributed in the hope that it will be useful,
574 but WITHOUT ANY WARRANTY; without even the implied warranty of
575 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
576 GNU General Public License for more details.
578 You should have received a copy of the GNU General Public License
579 along with this program. If not, see <http://www.gnu.org/licenses/>.