3 # (c) 2017 Tobin C. Harding <me@tobin.cc>
4 # Licensed under the terms of the GNU GPL License version 2
6 # leaking_addresses.pl: Scan 64 bit kernel for potential leaking addresses.
7 # - Scans dmesg output.
8 # - Walks directory tree and parses each file (for each directory in @DIRS).
10 # Use --debug to output path before parsing, this is useful to find files that
11 # cause the script to choke.
19 use Term
::ANSIColor
qw(:constants);
20 use Getopt
::Long
qw(:config no_auto_abbrev);
27 # Directories to scan.
28 my @DIRS = ('/proc', '/sys');
30 # Timer for parsing each file, in seconds.
33 # Script can only grep for kernel addresses on the following architectures. If
34 # your architecture is not listed here and has a grep'able kernel address please
35 # consider submitting a patch.
36 my @SUPPORTED_ARCHITECTURES = ('x86_64', 'ppc64');
38 # Command line options.
42 my $output_raw = ""; # Write raw results to file.
43 my $input_raw = ""; # Read raw results from file instead of scanning.
44 my $suppress_dmesg = 0; # Don't show dmesg in output.
45 my $squash_by_path = 0; # Summary report grouped by absolute path.
46 my $squash_by_filename = 0; # Summary report grouped by filename.
47 my $kernel_config_file = ""; # Kernel configuration file.
49 # Do not parse these files (absolute path).
50 my @skip_parse_files_abs = ('/proc/kmsg',
52 '/proc/fs/ext4/sdb1/mb_groups',
54 '/sys/firmware/devicetree',
56 '/sys/kernel/debug/tracing/trace_pipe',
57 '/sys/kernel/security/apparmor/revision');
59 # Do not parse these files under any subdirectory.
60 my @skip_parse_files_any = ('0',
72 # Do not walk these directories (absolute path).
73 my @skip_walk_dirs_abs = ();
75 # Do not walk these directories under any subdirectory.
76 my @skip_walk_dirs_any = ('self',
96 -o
, --output-raw
=<file
> Save results
for future processing
.
97 -i
, --input-raw
=<file
> Read results from file instead of scanning
.
98 --raw Show raw results
(default).
99 --suppress-dmesg Do
not show dmesg results
.
100 --squash-by-path Show one result per unique path
.
101 --squash-by-filename Show one result per unique filename
.
102 --kernel-config-file
=<file
> Kernel configuration file
(e
.g
/boot/config
)
103 -d
, --debug Display debugging output
.
104 -h
, --help
, --version Display this help
and exit.
106 Scans the running
(64 bit
) kernel
for potential leaking addresses
.
113 'd|debug' => \
$debug,
116 'o|output-raw=s' => \
$output_raw,
117 'i|input-raw=s' => \
$input_raw,
118 'suppress-dmesg' => \
$suppress_dmesg,
119 'squash-by-path' => \
$squash_by_path,
120 'squash-by-filename' => \
$squash_by_filename,
122 'kernel-config-file=s' => \
$kernel_config_file,
128 format_output
($input_raw);
132 if (!$input_raw and ($squash_by_path or $squash_by_filename)) {
133 printf "\nSummary reporting only available with --input-raw=<file>\n";
134 printf "(First run scan with --output-raw=<file>.)\n";
138 if (!is_supported_architecture
()) {
139 printf "\nScript does not support your architecture, sorry.\n";
140 printf "\nCurrently we support: \n\n";
141 foreach(@SUPPORTED_ARCHITECTURES) {
145 my $archname = $Config{archname
};
146 printf "\n\$ perl -MConfig -e \'print \"\$Config{archname}\\n\"\'\n";
147 printf "%s\n", $archname;
153 open my $fh, '>', $output_raw or die "$0: $output_raw: $!\n";
164 printf(STDERR
@_) if $debug;
167 sub is_supported_architecture
169 return (is_x86_64
() or is_ppc64
());
174 my $archname = $Config{archname
};
176 if ($archname =~ m/x86_64/) {
184 my $archname = $Config{archname
};
186 if ($archname =~ m/powerpc/ and $archname =~ m/64/) {
192 # Gets config option value from kernel config file.
193 # Returns "" on error or if config option not found.
194 sub get_kernel_config_option
201 # Allow --kernel-config-file to override.
202 if ($kernel_config_file ne "") {
203 @config_files = ($kernel_config_file);
204 } elsif (-R
"/proc/config.gz") {
205 my $tmp_file = "/tmp/tmpkconf";
207 if (system("gunzip < /proc/config.gz > $tmp_file")) {
208 dprint
"$0: system(gunzip < /proc/config.gz) failed\n";
211 @config_files = ($tmp_file);
214 my $file = '/boot/config-' . `uname -r`;
216 @config_files = ($file, '/boot/config');
219 foreach my $file (@config_files) {
220 dprint
("parsing config file: %s\n", $file);
221 $value = option_from_file
($option, $file);
227 if ($tmp_file ne "") {
228 system("rm -f $tmp_file");
234 # Parses $file and returns kernel configuration option value.
237 my ($option, $file) = @_;
241 open(my $fh, "<", $file) or return "";
242 while (my $line = <$fh> ) {
243 if ($line =~ /^$option/) {
244 ($str, $val) = split /=/, $line;
254 sub is_false_positive
258 if ($match =~ '\b(0x)?(f|F){16}\b' or
259 $match =~ '\b(0x)?0{16}\b') {
263 if (is_x86_64
() and is_in_vsyscall_memory_region
($match)) {
270 sub is_in_vsyscall_memory_region
274 my $hex = hex($match);
275 my $region_min = hex("0xffffffffff600000");
276 my $region_max = hex("0xffffffffff601000");
278 return ($hex >= $region_min and $hex <= $region_max);
281 # True if argument potentially contains a kernel address.
288 if ($line =~ '^SigBlk:' or
289 $line =~ '^SigIgn:' or
290 $line =~ '^SigCgt:') {
294 if ($line =~ '\bKEY=[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b' or
295 $line =~ '\b[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b') {
299 # One of these is guaranteed to be true.
301 $address_re = '\b(0x)?ffff[[:xdigit:]]{12}\b';
302 } elsif (is_ppc64
()) {
303 $address_re = '\b(0x)?[89abcdef]00[[:xdigit:]]{13}\b';
306 while (/($address_re)/g) {
307 if (!is_false_positive
($1)) {
317 open my $cmd, '-|', 'dmesg';
319 if (may_leak_address
($_)) {
320 print 'dmesg: ' . $_;
326 # True if we should skip this path.
329 my ($path, $paths_abs, $paths_any) = @_;
331 foreach (@$paths_abs) {
332 return 1 if (/^$path$/);
335 my($filename, $dirs, $suffix) = fileparse
($path);
336 foreach (@$paths_any) {
337 return 1 if (/^$filename$/);
346 return skip
($path, \
@skip_parse_files_abs, \
@skip_parse_files_any);
354 local $SIG{ALRM
} = sub { die "alarm\n" }; # NB: \n required.
361 die unless $@ eq "alarm\n"; # Propagate unexpected errors.
362 printf STDERR
"timed out parsing: %s\n", $file;
374 if (skip_parse
($file)) {
375 dprint
"skipping file: $file\n";
378 dprint
"parsing: $file\n";
380 open my $fh, "<", $file or return;
382 if (may_leak_address
($_)) {
383 print $file . ': ' . $_;
390 # True if we should skip walking this directory.
394 return skip
($path, \
@skip_walk_dirs_abs, \
@skip_walk_dirs_any)
397 # Recursively walk directory tree.
402 while (my $pwd = shift @dirs) {
403 next if (skip_walk
($pwd));
404 next if (!opendir(DIR
, $pwd));
405 my @files = readdir(DIR
);
408 foreach my $file (@files) {
409 next if ($file eq '.' or $file eq '..');
411 my $path = "$pwd/$file";
417 timed_parse_file
($path);
427 # Default is to show raw results.
428 if ($raw or (!$squash_by_path and !$squash_by_filename)) {
429 dump_raw_output
($file);
433 my ($total, $dmesg, $paths, $files) = parse_raw_file
($file);
435 printf "\nTotal number of results from scan (incl dmesg): %d\n", $total;
437 if (!$suppress_dmesg) {
441 if ($squash_by_filename) {
442 squash_by
($files, 'filename');
445 if ($squash_by_path) {
446 squash_by
($paths, 'path');
454 open (my $fh, '<', $file) or die "$0: $file: $!\n";
456 if ($suppress_dmesg) {
457 if ("dmesg:" eq substr($_, 0, 6)) {
470 my $total = 0; # Total number of lines parsed.
471 my @dmesg; # dmesg output.
472 my %files; # Unique filenames containing leaks.
473 my %paths; # Unique paths containing leaks.
475 open (my $fh, '<', $file) or die "$0: $file: $!\n";
476 while (my $line = <$fh>) {
479 if ("dmesg:" eq substr($line, 0, 6)) {
484 cache_path
(\
%paths, $line);
485 cache_filename
(\
%files, $line);
488 return $total, \
@dmesg, \
%paths, \
%files;
495 print "\ndmesg output:\n";
498 print "<no results>\n";
503 my $index = index($_, ': ');
504 $index += 2; # skid ': '
505 print substr($_, $index);
511 my ($ref, $desc) = @_;
513 print "\nResults squashed by $desc (excl dmesg). ";
514 print "Displaying [<number of results> <$desc>], <example result>\n";
516 if (keys %$ref == 0) {
517 print "<no results>\n";
521 foreach(keys %$ref) {
522 my $lines = $ref->{$_};
523 my $length = @$lines;
524 printf "[%d %s] %s", $length, $_, @$lines[0];
530 my ($paths, $line) = @_;
532 my $index = index($line, ': ');
533 my $path = substr($line, 0, $index);
535 $index += 2; # skip ': '
536 add_to_cache
($paths, $path, substr($line, $index));
541 my ($files, $line) = @_;
543 my $index = index($line, ': ');
544 my $path = substr($line, 0, $index);
545 my $filename = basename
($path);
547 $index += 2; # skip ': '
548 add_to_cache
($files, $filename, substr($line, $index));
553 my ($cache, $key, $value) = @_;
555 if (!$cache->{$key}) {
558 push @{$cache->{$key}}, $value;