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';
33 my ($message, $missing_flags_ref, $flag_renames_ref, $line) = @_;
35 # Rename flags if requested.
36 my @missing_flags = map {
37 (exists $flag_renames_ref->{$_})
38 ? $flag_renames_ref->{$_}
40 } @{$missing_flags_ref};
42 my $flags = join ' ', @missing_flags;
43 printf "%s (%s)%s %s",
44 error_color($message, 'red'), $flags, error_color(':', 'yellow'),
47 sub error_nonverbose_build {
51 error_color('NONVERBOSE BUILD', 'red'),
52 error_color(':', 'yellow'),
56 my ($message, $color) = @_;
58 # Use colors when writing to a terminal.
60 return Term::ANSIColor::colored($message, $color);
67 my ($line, @flags) = @_;
69 foreach my $flag (@flags) {
70 return 1 if $line =~ /\s$flag(\s|\\|$)/;
76 my ($line, $missing_flags_ref, @flags) = @_;
78 my @missing_flags = ();
79 foreach my $flag (@flags) {
80 if ($line !~ /\s$flag(\s|\\|$)/) {
81 push @missing_flags, $flag;
85 if (scalar @missing_flags == 0) {
89 @{$missing_flags_ref} = @missing_flags;
93 # Modifies $missing_flags_ref array.
94 sub pic_pie_conflict {
95 my ($line, $pie, $missing_flags_ref, @flags_pie) = @_;
98 return 0 if not any_flags_used($line, ('-fPIC', '-fpic'));
100 my %flags = map { $_ => 1 } @flags_pie;
102 # Remove all PIE flags from @missing_flags as they are not required with
105 not exists $flags{$_}
106 } @{$missing_flags_ref};
107 @{$missing_flags_ref} = @result;
109 # We got a conflict when no flags are left, thus only PIE flags were
110 # missing. If other flags were missing abort because the conflict is not
112 return scalar @result == 0;
116 # CONSTANTS/VARIABLES
118 # Regex to catch (GCC) compiler warnings.
119 my $warning_regex = qr/^(.+?):([0-9]+):[0-9]+: warning: (.+?) \[(.+?)\]$/;
121 # Expected hardening flags. All flags are used as regexps.
126 '--param=ssp-buffer-size=4',
129 '-Werror=format-security',
135 '-D_FORTIFY_SOURCE=2',
144 my @ldflags_bindnow = (
147 # All (hardening) flags.
148 my @flags = (@cflags, @cflags_pie,
150 @ldflags, @ldflags_pie, @ldflags_bindnow);
151 # Renaming rules for the output so the regex parts are not visible.
153 '-Wl,(-z,)?relro' => '-Wl,-z,relro',
154 '-Wl,(-z,)?now' => '-Wl,-z,now',
160 # Additional hardening options.
164 # Parse command line arguments.
167 my $option_version = 0;
168 if (not Getopt::Long::GetOptions(
169 'help|h|?' => \$option_help,
170 'version' => \$option_version,
172 'bindnow' => \$bindnow,
173 'all' => \$option_all,
176 Pod::Usage::pod2usage(2);
180 Pod::Usage::pod2usage(1);
182 if ($option_version) {
183 print "blhc $VERSION Copyright (C) 2012 Simon Ruderich
185 This program is free software: you can redistribute it and/or modify
186 it under the terms of the GNU General Public License as published by
187 the Free Software Foundation, either version 3 of the License, or
188 (at your option) any later version.
190 This program is distributed in the hope that it will be useful,
191 but WITHOUT ANY WARRANTY; without even the implied warranty of
192 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
193 GNU General Public License for more details.
195 You should have received a copy of the GNU General Public License
196 along with this program. If not, see <http://www.gnu.org/licenses/>.
209 # Input lines, contain only the lines with compiler commands.
212 my $continuation = 0;
213 while (my $line = <>) {
214 # Ignore compiler warnings for now.
215 next if $line =~ /$warning_regex/;
217 # Try to detect non verbose build logs.
218 if ($line =~ /^checking if you want to see long compiling messages\.\.\. no/
219 or $line =~ /^\s*(CC|CCLD)\s+/
220 or $line =~ /^\s*(C|c)ompiling\s+/
221 or $line =~ /^\s*\[[\d ]+%\] Building /) {
222 error_nonverbose_build($line);
227 # One line may contain multiple commands (";"). Treat each one as single
229 my @line = split /(?<!\\);/, $line;
230 foreach $line (@line) {
231 # Add newline, drop all other whitespace at the end of a line.
238 # Join lines, but leave the "\" in place so it's clear where the
239 # original line break was.
241 $input[-1] .= ' ' . $line;
244 # Ignore lines with no compiler commands.
245 next if $line !~ /\b(cc|gcc|g\+\+|c\+\+)(\s|\\)/;
247 # Ignore false positives.
249 # `./configure` output.
250 if ($line =~ /^checking /) {
257 # Line continuation, line ends with "\".
258 if ($line =~ /\\\s*$/) {
264 if (scalar @input == 0) {
265 print "No compiler commands!\n";
270 # Check if additional hardening options were used. Used to ensure they are
271 # used for the complete build.
272 foreach my $line (@input) {
273 $pie = 1 if any_flags_used($line, @cflags_pie, @ldflags_pie);
274 $bindnow = 1 if any_flags_used($line, @ldflags_bindnow);
278 @cflags = (@cflags, @cflags_pie);
279 @ldflags = (@ldflags, @ldflags_pie);
282 @ldflags = (@ldflags, @ldflags_bindnow);
285 foreach my $line (@input) {
286 # Ignore false positives.
288 # ./configure summary.
289 my $cc_regex = qr/(cc|(x86_64-linux-gnu-)?gcc|g\+\+|c\+\+)/;
290 next if $line =~ /^\s*(C|c)ompiler[\s.]*:\s+$cc_regex(\s-std=[a-z0-9:+]+)?\s*$/
291 or $line =~ /^\s*- (CC|CXX)\s*=\s*$cc_regex\s*$/
292 or $line =~ /^\s*-- Check for working (C|CXX) compiler: /;
294 # Is this a compiler or linker command?
299 if ($line =~ m{\s-o # -o
300 [\s\\]*\s+ # possible line continuation
301 ([A-Za-z0-9_/.-]+/)? # path to file
302 [A-Za-z0-9_-]+ # binary name (no dots!)
303 ([0-9.]*\.so[0-9.]*[a-z]? # library (including version)
305 (\s|\\|\$) # end of file name
307 or $line =~ /^libtool: link: /
308 or $line =~ m{\s*/bin/bash .+?libtool\s+(.+?\s+)?--mode=(re)?link}) {
313 # If there are source files then it's compiling/linking in one step and we
315 if ($line =~ /\.(c|cc|cpp)\b/) {
319 # Check hardening flags.
321 if ($compiler and not all_flags_used($line, \@missing, @cflags)
322 # Libraries linked with -fPIC don't have to (and can't) be linked
323 # with -fPIE as well. It's no error if only PIE flags are missing.
324 and not pic_pie_conflict($line, $pie, \@missing, @cflags_pie)) {
325 error_flags('CFLAGS missing', \@missing, \%flag_renames, $line);
328 if ($compiler and not all_flags_used($line, \@missing, @cppflags)) {
329 error_flags('CPPFLAGS missing', \@missing, \%flag_renames, $line);
332 if ($linker and not all_flags_used($line, \@missing, @ldflags)
333 # Same here, -fPIC conflicts with -fPIE.
334 and not pic_pie_conflict($line, $pie, \@missing, @ldflags_pie)) {
335 error_flags('LDFLAGS missing', \@missing, \%flag_renames, $line);
347 blhc - build log hardening check, checks build logs for missing hardening flags
351 B<blhc> [-h -? --help]
353 B<blhc> [--pie] [--bindnow] [--all]
355 --help available options
356 --version version number and license
357 --pie force +pie check
358 --bindnow force +bindbow check
359 --all force +all (+pie, +bindnow) check
363 blhc is a small tool which checks build logs for missing hardening flags and
364 other important warnings. It's licensed under the GPL 3 or later.
370 =item B<-h -? --help>
372 Print available options.
376 Print version number and license.
380 Force check for all +pie hardening flags. By default it's auto detected.
384 Force check for all +bindnow hardening flags. By default it's auto detected.
388 Force check for all +all (+pie, +bindnow) hardening flags. By default it's
393 Auto detection only works if at least one command uses the required hardening
394 flag (e.g. -fPIE). Then it's required for all other commands as well.
398 The exit status is a "bit mask", each listed status is ORed when the error
399 condition occurs to get the result.
409 No compiler commands were found.
413 Invalid arguments/options given to blhc.
421 Missing hardening flags.
427 Simon Ruderich, E<lt>simon@ruderich.orgE<gt>
429 =head1 COPYRIGHT AND LICENSE
431 Copyright (C) 2012 by Simon Ruderich
433 This program is free software: you can redistribute it and/or modify
434 it under the terms of the GNU General Public License as published by
435 the Free Software Foundation, either version 3 of the License, or
436 (at your option) any later version.
438 This program is distributed in the hope that it will be useful,
439 but WITHOUT ANY WARRANTY; without even the implied warranty of
440 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
441 GNU General Public License for more details.
443 You should have received a copy of the GNU General Public License
444 along with this program. If not, see <http://www.gnu.org/licenses/>.