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'),
48 my ($message, $color) = @_;
50 # Use colors when writing to a terminal.
52 return Term::ANSIColor::colored($message, $color);
59 my ($line, @flags) = @_;
61 foreach my $flag (@flags) {
62 return 1 if $line =~ /\s$flag(\s|\\|$)/;
68 my ($line, $missing_flags_ref, @flags) = @_;
70 my @missing_flags = ();
71 foreach my $flag (@flags) {
72 if ($line !~ /\s$flag(\s|\\|$)/) {
73 push @missing_flags, $flag;
77 if (scalar @missing_flags == 0) {
81 @{$missing_flags_ref} = @missing_flags;
85 # Modifies $missing_flags_ref array.
86 sub pic_pie_conflict {
87 my ($line, $pie, $missing_flags_ref, @flags_pie) = @_;
90 return 0 if not any_flags_used($line, ('-fPIC'));
92 my %flags = map { $_ => 1 } @flags_pie;
94 # Remove all PIE flags from @missing_flags as they are not required with
98 } @{$missing_flags_ref};
99 @{$missing_flags_ref} = @result;
101 # We got a conflict when no flags are left, thus only PIE flags were
102 # missing. If other flags were missing abort because the conflict is not
104 return scalar @result == 0;
108 # CONSTANTS/VARIABLES
110 # Regex to catch (GCC) compiler warnings.
111 my $warning_regex = qr/^(.+?):([0-9]+):[0-9]+: warning: (.+?) \[(.+?)\]$/;
113 # Expected hardening flags. All flags are used as regexps.
118 '--param=ssp-buffer-size=4',
121 '-Werror=format-security',
127 '-D_FORTIFY_SOURCE=2',
136 my @ldflags_bindnow = (
139 # All (hardening) flags.
140 my @flags = (@cflags, @cflags_pie,
142 @ldflags, @ldflags_pie, @ldflags_bindnow);
143 # Renaming rules for the output so the regex parts are not visible.
145 '-Wl,(-z,)?relro' => '-Wl,-z,relro',
146 '-Wl,(-z,)?now' => '-Wl,-z,now',
152 # Additional hardening options.
156 # Parse command line arguments.
159 my $option_version = 0;
160 if (not Getopt::Long::GetOptions(
161 'help|h|?' => \$option_help,
162 'version' => \$option_version,
164 'bindnow' => \$bindnow,
165 'all' => \$option_all,
168 Pod::Usage::pod2usage(2);
172 Pod::Usage::pod2usage(1);
174 if ($option_version) {
175 print "blhc $VERSION Copyright (C) 2012 Simon Ruderich
177 This program is free software: you can redistribute it and/or modify
178 it under the terms of the GNU General Public License as published by
179 the Free Software Foundation, either version 3 of the License, or
180 (at your option) any later version.
182 This program is distributed in the hope that it will be useful,
183 but WITHOUT ANY WARRANTY; without even the implied warranty of
184 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
185 GNU General Public License for more details.
187 You should have received a copy of the GNU General Public License
188 along with this program. If not, see <http://www.gnu.org/licenses/>.
201 # Input lines, contain only the lines with compiler commands.
204 my $continuation = 0;
205 while (my $line = <>) {
206 # Ignore compiler warnings for now.
207 next if $line =~ /$warning_regex/;
209 # One line may contain multiple commands (";"). Treat each one as single
211 my @line = split /(?<!\\);/, $line;
212 foreach $line (@line) {
213 # Add newline, drop all other whitespace at the end of a line.
220 # Join lines, but leave the "\" in place so it's clear where the
221 # original line break was.
223 $input[-1] .= ' ' . $line;
226 # Ignore lines with no compiler commands.
227 next if $line !~ /\b(cc\b|gcc\b|g\+\+|c\+\+)/;
229 # Ignore false positives.
231 # `./configure` output.
232 if ($line =~ /^checking /) {
239 # Line continuation, line ends with "\".
240 if ($line =~ /\\\s*$/) {
246 if (scalar @input == 0) {
247 print "No compiler commands!\n";
252 # Check if additional hardening options were used. Used to ensure they are
253 # used for the complete build.
254 foreach my $line (@input) {
255 $pie = 1 if any_flags_used($line, @cflags_pie, @ldflags_pie);
256 $bindnow = 1 if any_flags_used($line, @ldflags_bindnow);
260 @cflags = (@cflags, @cflags_pie);
261 @ldflags = (@ldflags, @ldflags_pie);
264 @ldflags = (@ldflags, @ldflags_bindnow);
267 foreach my $line (@input) {
268 # Is this a compiler or linker command?
273 if ($line =~ /\s-l[A-Za-z0-9.-]+(\s|\\|$)/
274 or $line =~ m{\s-o ([A-Za-z0-9_/.-]+/)?[A-Za-z0-9_-]+(\.so([0-9.])*|\.la)?(\s|\\|\$)}
275 or $line =~ /^libtool: link: /
276 or $line =~ m{\s*/bin/bash .+?libtool\s+(.+?\s+)?--mode=(re)?link}) {
281 # If there are source files then it's compiling/linking in one step and we
283 if ($line =~ /\.(c|cc|cpp)\b/) {
287 # Check hardening flags.
289 if ($compiler and not all_flags_used($line, \@missing, @cflags)
290 # Libraries linked with -fPIC don't have to (and can't) be linked
291 # with -fPIE as well. It's no error if only PIE flags are missing.
292 and not pic_pie_conflict($line, $pie, \@missing, @cflags_pie)) {
293 error_flags('CFLAGS missing', \@missing, \%flag_renames, $line);
296 if ($compiler and not all_flags_used($line, \@missing, @cppflags)) {
297 error_flags('CPPFLAGS missing', \@missing, \%flag_renames, $line);
300 if ($linker and not all_flags_used($line, \@missing, @ldflags)
301 # Same here, -fPIC conflicts with -fPIE.
302 and not pic_pie_conflict($line, $pie, \@missing, @ldflags_pie)) {
303 error_flags('LDFLAGS missing', \@missing, \%flag_renames, $line);
315 blhc - build log hardening check, checks build logs for missing hardening flags
319 B<blhc> [-h -? --help]
321 B<blhc> [--pie] [--bindnow] [--all]
323 --help available options
324 --version version number and license
325 --pie force +pie check
326 --bindnow force +bindbow check
327 --all force +all (+pie, +bindnow) check
331 blhc is a small tool which checks build logs for missing hardening flags and
332 other important warnings. It's licensed under the GPL 3 or later.
338 =item B<-h -? --help>
340 Print available options.
344 Print version number and license.
348 Force check for all +pie hardening flags. By default it's auto detected.
352 Force check for all +bindnow hardening flags. By default it's auto detected.
356 Force check for all +all (+pie, +bindnow) hardening flags. By default it's
361 Auto detection only works if at least one command uses the required hardening
362 flag (e.g. -fPIE). Then it's required for all other commands as well.
366 The exit status is a "bit mask", each listed status is ORed when the error
367 condition occurs to get the result.
377 No compiler commands were found.
381 Invalid arguments/options given to blhc.
385 Missing hardening flags.
391 Simon Ruderich, E<lt>simon@ruderich.orgE<gt>
393 =head1 COPYRIGHT AND LICENSE
395 Copyright (C) 2012 by Simon Ruderich
397 This program is free software: you can redistribute it and/or modify
398 it under the terms of the GNU General Public License as published by
399 the Free Software Foundation, either version 3 of the License, or
400 (at your option) any later version.
402 This program is distributed in the hope that it will be useful,
403 but WITHOUT ANY WARRANTY; without even the implied warranty of
404 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
405 GNU General Public License for more details.
407 You should have received a copy of the GNU General Public License
408 along with this program. If not, see <http://www.gnu.org/licenses/>.