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);
26 # Directories to scan.
27 my @DIRS = ('/proc', '/sys');
29 # Timer for parsing each file, in seconds.
32 # Script can only grep for kernel addresses on the following architectures. If
33 # your architecture is not listed here and has a grep'able kernel address please
34 # consider submitting a patch.
35 my @SUPPORTED_ARCHITECTURES = ('x86_64', 'ppc64');
37 # Command line options.
41 my $output_raw = ""; # Write raw results to file.
42 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.
48 # Do not parse these files (absolute path).
49 my @skip_parse_files_abs = ('/proc/kmsg',
51 '/proc/fs/ext4/sdb1/mb_groups',
53 '/sys/firmware/devicetree',
55 '/sys/kernel/debug/tracing/trace_pipe',
56 '/sys/kernel/security/apparmor/revision');
58 # Do not parse these files under any subdirectory.
59 my @skip_parse_files_any = ('0',
71 # Do not walk these directories (absolute path).
72 my @skip_walk_dirs_abs = ();
74 # Do not walk these directories under any subdirectory.
75 my @skip_walk_dirs_any = ('self',
95 -o, --output-raw=<file> Save results for future processing.
96 -i, --input-raw=<file> Read results from file instead of scanning.
97 --raw Show raw results (default).
98 --suppress-dmesg Do not show dmesg results.
99 --squash-by-path Show one result per unique path.
100 --squash-by-filename Show one result per unique filename.
101 -d, --debug Display debugging output.
102 -h, --help, --version Display this help and exit.
104 Scans the running (64 bit) kernel for potential leaking addresses.
111 'd|debug' => \$debug,
114 'o|output-raw=s' => \$output_raw,
115 'i|input-raw=s' => \$input_raw,
116 'suppress-dmesg' => \$suppress_dmesg,
117 'squash-by-path' => \$squash_by_path,
118 'squash-by-filename' => \$squash_by_filename,
125 format_output($input_raw);
129 if (!$input_raw and ($squash_by_path or $squash_by_filename)) {
130 printf "\nSummary reporting only available with --input-raw=<file>\n";
131 printf "(First run scan with --output-raw=<file>.)\n";
135 if (!is_supported_architecture()) {
136 printf "\nScript does not support your architecture, sorry.\n";
137 printf "\nCurrently we support: \n\n";
138 foreach(@SUPPORTED_ARCHITECTURES) {
142 my $archname = $Config{archname};
143 printf "\n\$ perl -MConfig -e \'print \"\$Config{archname}\\n\"\'\n";
144 printf "%s\n", $archname;
150 open my $fh, '>', $output_raw or die "$0: $output_raw: $!\n";
161 printf(STDERR @_) if $debug;
164 sub is_supported_architecture
166 return (is_x86_64() or is_ppc64());
171 my $archname = $Config{archname};
173 if ($archname =~ m/x86_64/) {
181 my $archname = $Config{archname};
183 if ($archname =~ m/powerpc/ and $archname =~ m/64/) {
189 sub is_false_positive
193 if ($match =~ '\b(0x)?(f|F){16}\b' or
194 $match =~ '\b(0x)?0{16}\b') {
199 # vsyscall memory region, we should probably check against a range here.
200 if ($match =~ '\bf{10}600000\b' or
201 $match =~ '\bf{10}601000\b') {
209 # True if argument potentially contains a kernel address.
216 if ($line =~ '^SigBlk:' or
217 $line =~ '^SigIgn:' or
218 $line =~ '^SigCgt:') {
222 if ($line =~ '\bKEY=[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b' or
223 $line =~ '\b[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b') {
227 # One of these is guaranteed to be true.
229 $address_re = '\b(0x)?ffff[[:xdigit:]]{12}\b';
230 } elsif (is_ppc64()) {
231 $address_re = '\b(0x)?[89abcdef]00[[:xdigit:]]{13}\b';
234 while (/($address_re)/g) {
235 if (!is_false_positive($1)) {
245 open my $cmd, '-|', 'dmesg';
247 if (may_leak_address($_)) {
248 print 'dmesg: ' . $_;
254 # True if we should skip this path.
257 my ($path, $paths_abs, $paths_any) = @_;
259 foreach (@$paths_abs) {
260 return 1 if (/^$path$/);
263 my($filename, $dirs, $suffix) = fileparse($path);
264 foreach (@$paths_any) {
265 return 1 if (/^$filename$/);
274 return skip($path, \@skip_parse_files_abs, \@skip_parse_files_any);
282 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required.
289 die unless $@ eq "alarm\n"; # Propagate unexpected errors.
290 printf STDERR "timed out parsing: %s\n", $file;
302 if (skip_parse($file)) {
303 dprint "skipping file: $file\n";
306 dprint "parsing: $file\n";
308 open my $fh, "<", $file or return;
310 if (may_leak_address($_)) {
311 print $file . ': ' . $_;
318 # True if we should skip walking this directory.
322 return skip($path, \@skip_walk_dirs_abs, \@skip_walk_dirs_any)
325 # Recursively walk directory tree.
330 while (my $pwd = shift @dirs) {
331 next if (skip_walk($pwd));
332 next if (!opendir(DIR, $pwd));
333 my @files = readdir(DIR);
336 foreach my $file (@files) {
337 next if ($file eq '.' or $file eq '..');
339 my $path = "$pwd/$file";
345 timed_parse_file($path);
355 # Default is to show raw results.
356 if ($raw or (!$squash_by_path and !$squash_by_filename)) {
357 dump_raw_output($file);
361 my ($total, $dmesg, $paths, $files) = parse_raw_file($file);
363 printf "\nTotal number of results from scan (incl dmesg): %d\n", $total;
365 if (!$suppress_dmesg) {
369 if ($squash_by_filename) {
370 squash_by($files, 'filename');
373 if ($squash_by_path) {
374 squash_by($paths, 'path');
382 open (my $fh, '<', $file) or die "$0: $file: $!\n";
384 if ($suppress_dmesg) {
385 if ("dmesg:" eq substr($_, 0, 6)) {
398 my $total = 0; # Total number of lines parsed.
399 my @dmesg; # dmesg output.
400 my %files; # Unique filenames containing leaks.
401 my %paths; # Unique paths containing leaks.
403 open (my $fh, '<', $file) or die "$0: $file: $!\n";
404 while (my $line = <$fh>) {
407 if ("dmesg:" eq substr($line, 0, 6)) {
412 cache_path(\%paths, $line);
413 cache_filename(\%files, $line);
416 return $total, \@dmesg, \%paths, \%files;
423 print "\ndmesg output:\n";
426 print "<no results>\n";
431 my $index = index($_, ': ');
432 $index += 2; # skid ': '
433 print substr($_, $index);
439 my ($ref, $desc) = @_;
441 print "\nResults squashed by $desc (excl dmesg). ";
442 print "Displaying [<number of results> <$desc>], <example result>\n";
444 if (keys %$ref == 0) {
445 print "<no results>\n";
449 foreach(keys %$ref) {
450 my $lines = $ref->{$_};
451 my $length = @$lines;
452 printf "[%d %s] %s", $length, $_, @$lines[0];
458 my ($paths, $line) = @_;
460 my $index = index($line, ': ');
461 my $path = substr($line, 0, $index);
463 $index += 2; # skip ': '
464 add_to_cache($paths, $path, substr($line, $index));
469 my ($files, $line) = @_;
471 my $index = index($line, ': ');
472 my $path = substr($line, 0, $index);
473 my $filename = basename($path);
475 $index += 2; # skip ': '
476 add_to_cache($files, $filename, substr($line, $index));
481 my ($cache, $key, $value) = @_;
483 if (!$cache->{$key}) {
486 push @{$cache->{$key}}, $value;