use Getopt::Long ();
use Term::ANSIColor ();
+use Text::ParseWords ();
our $VERSION = '0.01';
# CONSTANTS/VARIABLES
# Regex to catch compiler commands.
-my $cc_regex = qr/(?:[a-z0-9_]+-(?:linux|kfreebsd)-gnu(?:eabi|eabihf)?-)?
+my $cc_regex = qr/(?:[a-z0-9_]+-(?:linux-|kfreebsd-)?gnu(?:eabi|eabihf)?-)?
(?:(?<!\.)cc|gcc|g\+\+|c\+\+)
(?:-[\d.]+)?/x;
# Regex to catch (GCC) compiler warnings.
error_color(':', 'yellow'),
$line;
}
+sub error_hardening_wrapper {
+ printf "%s%s %s\n",
+ error_color('HARDENING WRAPPER', 'red'),
+ error_color(':', 'yellow'),
+ 'no checks possible, aborting';
+}
sub error_color {
my ($message, $color) = @_;
}
}
+ # If hardening wrapper is used (wraps calls to gcc and adds hardening
+ # flags automatically) we can't perform any checks, abort.
+ if (not $start and $line =~ /^Build-Depends: .*\bhardening-wrapper\b/) {
+ error_hardening_wrapper();
+ $exit |= 1 << 4;
+ exit $exit;
+ }
+
# We skip over unimportant lines at the beginning of the log to prevent
# false positives.
$start = 1 if $line =~ /^dpkg-buildpackage:/;
next if not $start;
+ # And stop at the end of the build log. Package details (reported by the
+ # buildd logs) are not important for us. This also prevents false
+ # positives.
+ last if $line =~ /^Build finished at \d{8}-\d{4}$/;
# Detect architecture automatically unless overridden.
if (not $option_arch
my $non_verbose = is_non_verbose_build($line);
# One line may contain multiple commands (";"). Treat each one as single
- # line.
- my @line = split /(?<!\\);/, $line;
+ # line. parse_line() is slow, only use it when necessary.
+ my @line = (not $line =~ /;/)
+ ? ($line)
+ : Text::ParseWords::parse_line(';', 1, $line);
foreach $line (@line) {
# Add newline, drop all other whitespace at the end of a line.
$line =~ s/\s+$//;
next if $line =~ /^\s*Depends: .*?$cc_regex.*?$/
and $line !~ /\s-./; # option, prevent false negatives
+ # Check if additional hardening options were used. Used to ensure
+ # they are used for the complete build.
+ $harden_pie = 1 if any_flags_used($line, @cflags_pie, @ldflags_pie);
+ $harden_bindnow = 1 if any_flags_used($line, @ldflags_bindnow);
push @input, $line;
}
}
}
-# Check if additional hardening options were used. Used to ensure they are
-# used for the complete build.
-foreach my $line (@input) {
- $harden_pie = 1 if any_flags_used($line, @cflags_pie, @ldflags_pie);
- $harden_bindnow = 1 if any_flags_used($line, @ldflags_bindnow);
-}
-
# Check the specified hardening options, same order as dpkg-buildflags.
if ($harden_pie) {
@cflags = (@cflags, @cflags_pie);
if ($compile and not all_flags_used($line, \@missing, @cflags)
# Libraries linked with -fPIC don't have to (and can't) be linked
# with -fPIE as well. It's no error if only PIE flags are missing.
- and not pic_pie_conflict($line, $harden_pie, \@missing, @cflags_pie)) {
+ and not pic_pie_conflict($line, $harden_pie, \@missing, @cflags_pie)
+ # Assume dpkg-buildflags returns the correct flags.
+ and not $line =~ /`dpkg-buildflags --get (?:CFLAGS|CXXFLAGS)`/) {
error_flags('CFLAGS missing', \@missing, \%flag_renames, $line);
$exit |= 1 << 3;
}
- if ($preprocess and not all_flags_used($line, \@missing, @cppflags)) {
+ if ($preprocess and not all_flags_used($line, \@missing, @cppflags)
+ # Assume dpkg-buildflags returns the correct flags.
+ and not $line =~ /`dpkg-buildflags --get CPPFLAGS`/) {
error_flags('CPPFLAGS missing', \@missing, \%flag_renames, $line);
$exit |= 1 << 3;
}
if ($link and not all_flags_used($line, \@missing, @ldflags)
# Same here, -fPIC conflicts with -fPIE.
- and not pic_pie_conflict($line, $harden_pie, \@missing, @ldflags_pie)) {
+ and not pic_pie_conflict($line, $harden_pie, \@missing, @ldflags_pie)
+ # Assume dpkg-buildflags returns the correct flags.
+ and not $line =~ /`dpkg-buildflags --get LDFLAGS`/) {
error_flags('LDFLAGS missing', \@missing, \%flag_renames, $line);
$exit |= 1 << 3;
}
Missing hardening flags.
+=item B<16>
+
+Hardening wrapper detected, no tests performed.
+
=back
=head1 AUTHOR