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.
45 my $suppress_dmesg = 0; # Don't show dmesg in output.
46 my $squash_by_path = 0; # Summary report grouped by absolute path.
47 my $squash_by_filename = 0; # Summary report grouped by filename.
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 -d, --debug Display debugging output.
103 -h, --help, --version Display this help and exit.
105 Scans the running (64 bit) kernel for potential leaking addresses.
112 'd|debug' => \$debug,
115 'o|output-raw=s' => \$output_raw,
116 'i|input-raw=s' => \$input_raw,
117 'suppress-dmesg' => \$suppress_dmesg,
118 'squash-by-path' => \$squash_by_path,
119 'squash-by-filename' => \$squash_by_filename,
126 format_output($input_raw);
130 if (!$input_raw and ($squash_by_path or $squash_by_filename)) {
131 printf "\nSummary reporting only available with --input-raw=<file>\n";
132 printf "(First run scan with --output-raw=<file>.)\n";
136 if (!is_supported_architecture()) {
137 printf "\nScript does not support your architecture, sorry.\n";
138 printf "\nCurrently we support: \n\n";
139 foreach(@SUPPORTED_ARCHITECTURES) {
143 my $archname = $Config{archname};
144 printf "\n\$ perl -MConfig -e \'print \"\$Config{archname}\\n\"\'\n";
145 printf "%s\n", $archname;
151 open my $fh, '>', $output_raw or die "$0: $output_raw: $!\n";
162 printf(STDERR @_) if $debug;
165 sub is_supported_architecture
167 return (is_x86_64() or is_ppc64());
172 my $archname = $Config{archname};
174 if ($archname =~ m/x86_64/) {
182 my $archname = $Config{archname};
184 if ($archname =~ m/powerpc/ and $archname =~ m/64/) {
190 sub is_false_positive
194 if ($match =~ '\b(0x)?(f|F){16}\b' or
195 $match =~ '\b(0x)?0{16}\b') {
199 if (is_x86_64() and is_in_vsyscall_memory_region($match)) {
206 sub is_in_vsyscall_memory_region
210 my $hex = hex($match);
211 my $region_min = hex("0xffffffffff600000");
212 my $region_max = hex("0xffffffffff601000");
214 return ($hex >= $region_min and $hex <= $region_max);
217 # True if argument potentially contains a kernel address.
224 if ($line =~ '^SigBlk:' or
225 $line =~ '^SigIgn:' or
226 $line =~ '^SigCgt:') {
230 if ($line =~ '\bKEY=[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b' or
231 $line =~ '\b[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b') {
235 # One of these is guaranteed to be true.
237 $address_re = '\b(0x)?ffff[[:xdigit:]]{12}\b';
238 } elsif (is_ppc64()) {
239 $address_re = '\b(0x)?[89abcdef]00[[:xdigit:]]{13}\b';
242 while (/($address_re)/g) {
243 if (!is_false_positive($1)) {
253 open my $cmd, '-|', 'dmesg';
255 if (may_leak_address($_)) {
256 print 'dmesg: ' . $_;
262 # True if we should skip this path.
265 my ($path, $paths_abs, $paths_any) = @_;
267 foreach (@$paths_abs) {
268 return 1 if (/^$path$/);
271 my($filename, $dirs, $suffix) = fileparse($path);
272 foreach (@$paths_any) {
273 return 1 if (/^$filename$/);
282 return skip($path, \@skip_parse_files_abs, \@skip_parse_files_any);
290 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required.
297 die unless $@ eq "alarm\n"; # Propagate unexpected errors.
298 printf STDERR "timed out parsing: %s\n", $file;
310 if (skip_parse($file)) {
311 dprint "skipping file: $file\n";
314 dprint "parsing: $file\n";
316 open my $fh, "<", $file or return;
318 if (may_leak_address($_)) {
319 print $file . ': ' . $_;
326 # True if we should skip walking this directory.
330 return skip($path, \@skip_walk_dirs_abs, \@skip_walk_dirs_any)
333 # Recursively walk directory tree.
338 while (my $pwd = shift @dirs) {
339 next if (skip_walk($pwd));
340 next if (!opendir(DIR, $pwd));
341 my @files = readdir(DIR);
344 foreach my $file (@files) {
345 next if ($file eq '.' or $file eq '..');
347 my $path = "$pwd/$file";
353 timed_parse_file($path);
363 # Default is to show raw results.
364 if ($raw or (!$squash_by_path and !$squash_by_filename)) {
365 dump_raw_output($file);
369 my ($total, $dmesg, $paths, $files) = parse_raw_file($file);
371 printf "\nTotal number of results from scan (incl dmesg): %d\n", $total;
373 if (!$suppress_dmesg) {
377 if ($squash_by_filename) {
378 squash_by($files, 'filename');
381 if ($squash_by_path) {
382 squash_by($paths, 'path');
390 open (my $fh, '<', $file) or die "$0: $file: $!\n";
392 if ($suppress_dmesg) {
393 if ("dmesg:" eq substr($_, 0, 6)) {
406 my $total = 0; # Total number of lines parsed.
407 my @dmesg; # dmesg output.
408 my %files; # Unique filenames containing leaks.
409 my %paths; # Unique paths containing leaks.
411 open (my $fh, '<', $file) or die "$0: $file: $!\n";
412 while (my $line = <$fh>) {
415 if ("dmesg:" eq substr($line, 0, 6)) {
420 cache_path(\%paths, $line);
421 cache_filename(\%files, $line);
424 return $total, \@dmesg, \%paths, \%files;
431 print "\ndmesg output:\n";
434 print "<no results>\n";
439 my $index = index($_, ': ');
440 $index += 2; # skid ': '
441 print substr($_, $index);
447 my ($ref, $desc) = @_;
449 print "\nResults squashed by $desc (excl dmesg). ";
450 print "Displaying [<number of results> <$desc>], <example result>\n";
452 if (keys %$ref == 0) {
453 print "<no results>\n";
457 foreach(keys %$ref) {
458 my $lines = $ref->{$_};
459 my $length = @$lines;
460 printf "[%d %s] %s", $length, $_, @$lines[0];
466 my ($paths, $line) = @_;
468 my $index = index($line, ': ');
469 my $path = substr($line, 0, $index);
471 $index += 2; # skip ': '
472 add_to_cache($paths, $path, substr($line, $index));
477 my ($files, $line) = @_;
479 my $index = index($line, ': ');
480 my $path = substr($line, 0, $index);
481 my $filename = basename($path);
483 $index += 2; # skip ': '
484 add_to_cache($files, $filename, substr($line, $index));
489 my ($cache, $key, $value) = @_;
491 if (!$cache->{$key}) {
494 push @{$cache->{$key}}, $value;