/usr/bin/hardening-check is in hardening-includes 1.36ubuntu1.1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 | #!/usr/bin/perl
# Report the hardening characterists of a set of binaries.
# Copyright (C) 2009-2011 Kees Cook <kees@debian.org>
# License: GPLv2 or newer
use strict;
use warnings;
use Getopt::Long qw(:config no_ignore_case bundling);
use Pod::Usage;
use IPC::Open3;
use Symbol qw(gensym);
my $skip_pie = 0;
my $skip_stackprotector = 0;
my $skip_fortify = 0;
my $skip_relro = 0;
my $skip_bindnow = 0;
my $report_functions = 0;
my $lintian = 0;
my $verbose = 0;
my $debug = 0;
my $quiet = 0;
my $help = 0;
my $man = 0;
GetOptions(
"nopie|p+" => \$skip_pie,
"nostackprotector|s+" => \$skip_stackprotector,
"nofortify|f+" => \$skip_fortify,
"norelro|r+" => \$skip_relro,
"nobindnow|b+" => \$skip_bindnow,
"report-functions!" => \$report_functions,
"lintian!" => \$lintian,
"verbose!" => \$verbose,
"debug!" => \$debug,
"quiet|q!" => \$quiet,
"help|?" => \$help,
"man" => \$man,
) or pod2usage(2);
pod2usage(1) if $help;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;
my $overall = 0;
my $rc = 0;
my $report = "";
my @tags;
# Report a good test.
sub good($) {
my ($msg) = @_;
if ($quiet == 0) {
$report .= "\n$msg";
}
}
# Report a failed test, possibly ignoring it.
sub bad($$$$) {
my ($name, $file, $msg, $ignore) = @_;
if ($ignore) {
$msg .= " (ignored)";
}
else {
$rc = 1;
if ($lintian) {
push(@tags, "$name:$file");
}
}
$report .= "\n$msg";
}
# Safely run list-based command line and return stdout.
sub output(@) {
my (@cmd) = @_;
my ($pid, $stdout, $stderr);
$stdout = gensym;
$stderr = gensym;
$pid = open3(gensym, $stdout, $stderr, @cmd);
my $collect = "";
while ( <$stdout> ) {
$collect .= $_;
}
waitpid($pid, 0);
my $rc = $?;
if ($rc != 0) {
while ( <$stderr> ) {
print STDERR;
}
return "";
}
return $collect;
}
# Find the libc used in this executable, if any.
sub find_libc($) {
my ($file) = @_;
my $ldd = output("ldd", $file);
$ldd =~ /^\s*libc\.so\.\S+\s+\S+\s+(\S+)/m;
return $1 || "";
}
sub find_functions($$) {
my ($file, $undefined) = @_;
my (%funcs);
my $relocs = output("readelf", "-sW", $file);
for my $line (split("\n", $relocs)) {
next if ($line !~ / I?FUNC /);
next if ($undefined && $line !~ / I?FUNC .* UND /);
$line =~ s/ \([0-9]+\)$//;
$line =~ s/.* //;
$line =~ s/@.*//;
$funcs{$line} = 1;
}
return \%funcs;
}
$ENV{'LANG'} = "C";
my $name;
my %libc;
foreach my $file (@ARGV) {
$rc = 0;
$report="$file:";
# Get program headers.
my $PROG_REPORT=output("readelf", "-lW", $file);
if (length($PROG_REPORT) == 0) {
$overall = 1;
next;
}
# Get ELF headers.
my $DYN_REPORT=output("readelf", "-dW", $file);
# Get list of all symbols needing external resolution.
my $functions = find_functions($file, 1);
# Load list of protected functions from libc.
my $file_libc = find_libc($file);
if ($debug) {
print "$file: $file_libc\n";
}
if (!defined($libc{$file_libc})) {
if ($file_libc eq "") {
$libc{$file_libc}->{""} = 1;
}
else {
my $funcs = find_functions($file_libc, 0);
for my $name (keys(%{$funcs})) {
if ($name =~ /^__(\S+)_chk$/) {
if ($debug) {
print "$file_libc: $1\n";
}
$libc{$file_libc}->{$1} = 1;
}
}
}
}
# PIE
# First, verify this is an executable, not a library. This seems to be
# best seen by checking for the PHDR program header.
$name = " Position Independent Executable";
$PROG_REPORT =~ /^Elf file type is (\S+)/m;
my $elftype = $1 || "";
if ($elftype eq "DYN") {
if ($PROG_REPORT =~ /^ *\bPHDR\b/m) {
# Executable, DYN ELF type.
good("$name: yes");
}
else {
# Shared library, DYN ELF type.
good "$name: no, regular shared library (ignored)"
}
}
elsif ($elftype eq "EXEC") {
# Executable, EXEC ELF type.
bad("non-pie", $file,
"$name: no, normal executable!", $skip_pie);
}
else {
# ELF type is neither DYN nor EXEC.
bad("unknown-elf", $file,
"$name: not a known ELF type!? ($elftype)", 0);
}
# Stack-protected
$name = " Stack protected";
if (defined($functions->{'__stack_chk_fail'})) {
good("$name: yes")
}
else {
bad("no-stackprotector", $file,
"$name: no, not found!", $skip_stackprotector);
}
# Fortified Source
$name = " Fortify Source functions";
my @unprotected;
my @protected;
for my $name (keys(%{$libc{$file_libc}})) {
if (defined($functions->{$name})) {
push(@unprotected, $name);
}
if (defined($functions->{"__${name}_chk"})) {
push(@protected, $name);
}
}
if ($#protected > -1) {
if ($#unprotected == -1) {
# Certain.
good("$name: yes");
}
else {
# Vague, due to possible compile-time optimization,
# multiple linkages, etc. Assume "yes" for now.
good("$name: yes (some protected functions found)");
}
}
else {
if ($file_libc eq "") {
good("$name: unknown, not linked against libc");
}
elsif ($#unprotected == -1) {
good("$name: unknown, no protectable libc functions used");
}
else {
# Vague, since it's possible to have the compile-time
# optimizations do away with them, or be unverifiable
# at runtime. Assume "no" for now.
bad("no-fortify-functions", $file,
"$name: no, only unprotected functions found!", $skip_fortify);
}
}
if ($verbose) {
for my $name (@unprotected) {
good("\tunprotected: $name");
}
for my $name (@protected) {
good("\tprotected: $name");
}
}
# Format
# Unfortunately, I haven't thought of a way to test for this after
# compilation. What it really needs is a lintian-like check that
# reviews the build logs and looks for the warnings, or that the
# argument is changed to use -Werror,format-security to stop the build.
# RELRO
$name = " Read-only relocations";
if ($PROG_REPORT =~ /^ *\bGNU_RELRO\b/m) {
good("$name: yes");
}
else {
bad("no-relro", $file, "$name: no, not found!", $skip_relro);
}
# BIND_NOW
# This marking keeps changing:
# 0x0000000000000018 (BIND_NOW)
# 0x000000006ffffffb (FLAGS) Flags: BIND_NOW
# 0x000000006ffffffb (FLAGS_1) Flags: NOW
$name = " Immediate binding";
if ($DYN_REPORT =~ /^\s*\S+\s+\(BIND_NOW\)/m ||
$DYN_REPORT =~ /^\s*\S+\s+\(FLAGS\).*\bBIND_NOW\b/m ||
$DYN_REPORT =~ /^\s*\S+\s+\(FLAGS_1\).*\bNOW\b/m) {
good("$name: yes");
}
else {
bad("no-bindnow", $file, "$name: no not found!", $skip_bindnow);
}
if (!$lintian && (!$quiet || $rc != 0)) {
print $report,"\n";
}
if ($report_functions) {
for my $name (keys(%{$functions})) {
print $name,"\n";
}
}
if (!$lintian && $rc) {
$overall = $rc;
}
if ($lintian) {
for my $tag (@tags) {
print $tag, "\n";
}
}
}
exit($overall);
__END__
=pod
=head1 NAME
hardening-check - check binaries for security hardening features
=head1 SYNOPSIS
hardening-check [options] [ELF ...]
Examine a given set of ELF binaries and check for several security hardening
features, failing if they are not all found.
=head1 DESCRIPTION
This utility checks a given list of ELF binaries for several security
hardening features that can be compiled into an executable. These
features are:
=over 8
=item B<Position Independent Executable>
This indicates that the executable was built in such a way (PIE) that
the "text" section of the program can be relocated in memory. To take
full advantage of this feature, the executing kernel must support text
Address Space Layout Randomization (ASLR).
=item B<Stack Protected>
This indicates that there is evidence that the ELF was compiled with the
L<gcc(1)> option B<-fstack-protector> (e.g. uses B<__stack_chk_fail>). The
program will be resistant to having its stack overflowed.
When an executable was built without any character arrays being allocated
on the stack, this check will lead to false alarms (since there is no
use of B<__stack_chk_fail>), even though it was compiled with the correct
options.
=item B<Fortify Source functions>
This indicates that the executable was compiled with
B<-D_FORTIFY_SOURCE=2> and B<-O1> or higher. This causes certain unsafe
glibc functions with their safer counterparts (e.g. B<strncpy> instead
of B<strcpy>), or replaces calls that are verifiable at runtime with the
runtime-check version (e.g. B<__memcpy_chk> insteade of B<memcpy>).
When an executable was built such that the fortified versions of the glibc
functions are not useful (e.g. use is verified as safe at compile time, or
use cannot be verified at runtime), this check will lead to false alarms.
In an effort to mitigate this, the check will pass if any fortified function
is found, and will fail if only unfortified functions are found. Uncheckable
conditions also pass (e.g. no functions that could be fortified are found, or
not linked against glibc).
=item B<Read-only relocations>
This indicates that the executable was build with B<-Wl,-z,relro> to
have ELF markings (RELRO) that ask the runtime linker to mark any
regions of the relocation table as "read-only" if they were resolved
before execution begins. This reduces the possible areas of memory in
a program that can be used by an attacker that performs a successful
memory corruption exploit.
=item B<Immediate binding>
This indicates that the executable was built with B<-Wl,-z,now> to have
ELF markings (BIND_NOW) that ask the runtime linker to resolve all
relocations before starting program execution. When combined with RELRO
above, this further reduces the regions of memory available to memory
corruption attacks.
=back
=head1 OPTIONS
=over 8
=item B<--nopie>, B<-p>
No not require that the checked binaries be built as PIE.
=item B<--nostackprotector>, B<-s>
No not require that the checked binaries be built with the stack protector.
=item B<--nofortify>, B<-f>
No not require that the checked binaries be built with Fority Source.
=item B<--norelro>, B<-r>
No not require that the checked binaries be built with RELRO.
=item B<--nobindnow>, B<-b>
No not require that the checked binaries be built with BIND_NOW.
=item B<--quiet>, B<-q>
Only report failures.
=item B<--verbose>
Report verbosely on failures.
=item B<--lintian>
Switch reporting to lintian-check-parsable output.
=item B<--debug>
Report some debugging during processing.
=item B<--help>, B<-?>
Print a brief help message and exit.
=item B<--man>
Print the manual page and exit.
=back
=head1 RETURN VALUE
When all checked binaries have all checkable hardening features detected,
this program will finish with an exit code of 0. If any check fails, the
exit code with be 1. Individual checks can be disabled via command line
options.
=head1 AUTHOR
Kees Cook <kees@debian.org>
=head1 COPYRIGHT AND LICENSE
Copyright 2009-2011 Kees Cook <kees@debian.org>.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 2 or later.
=head1 SEE ALSO
L<gcc(1)>, L<hardening-wrapper(1)>
=cut
|