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.
106 # Scan kernel and dump raw results.
109 # Scan kernel and save results to file.
110 $0 --output-raw scan.out
112 # View summary report.
113 $0 --input-raw scan.out --squash-by-filename
115 Scans the running (64 bit) kernel for potential leaking addresses.
122 'd|debug' => \$debug,
125 'o|output-raw=s' => \$output_raw,
126 'i|input-raw=s' => \$input_raw,
127 'suppress-dmesg' => \$suppress_dmesg,
128 'squash-by-path' => \$squash_by_path,
129 'squash-by-filename' => \$squash_by_filename,
136 format_output($input_raw);
140 if (!$input_raw and ($squash_by_path or $squash_by_filename)) {
141 printf "\nSummary reporting only available with --input-raw=<file>\n";
142 printf "(First run scan with --output-raw=<file>.)\n";
146 if (!is_supported_architecture()) {
147 printf "\nScript does not support your architecture, sorry.\n";
148 printf "\nCurrently we support: \n\n";
149 foreach(@SUPPORTED_ARCHITECTURES) {
153 my $archname = $Config{archname};
154 printf "\n\$ perl -MConfig -e \'print \"\$Config{archname}\\n\"\'\n";
155 printf "%s\n", $archname;
161 open my $fh, '>', $output_raw or die "$0: $output_raw: $!\n";
172 printf(STDERR @_) if $debug;
175 sub is_supported_architecture
177 return (is_x86_64() or is_ppc64());
182 my $archname = $Config{archname};
184 if ($archname =~ m/x86_64/) {
192 my $archname = $Config{archname};
194 if ($archname =~ m/powerpc/ and $archname =~ m/64/) {
200 sub is_false_positive
204 if ($match =~ '\b(0x)?(f|F){16}\b' or
205 $match =~ '\b(0x)?0{16}\b') {
210 # vsyscall memory region, we should probably check against a range here.
211 if ($match =~ '\bf{10}600000\b' or
212 $match =~ '\bf{10}601000\b') {
220 # True if argument potentially contains a kernel address.
227 if ($line =~ '^SigBlk:' or
228 $line =~ '^SigIgn:' or
229 $line =~ '^SigCgt:') {
233 if ($line =~ '\bKEY=[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b' or
234 $line =~ '\b[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b') {
238 # One of these is guaranteed to be true.
240 $address_re = '\b(0x)?ffff[[:xdigit:]]{12}\b';
241 } elsif (is_ppc64()) {
242 $address_re = '\b(0x)?[89abcdef]00[[:xdigit:]]{13}\b';
245 while (/($address_re)/g) {
246 if (!is_false_positive($1)) {
256 open my $cmd, '-|', 'dmesg';
258 if (may_leak_address($_)) {
259 print 'dmesg: ' . $_;
265 # True if we should skip this path.
268 my ($path, $paths_abs, $paths_any) = @_;
270 foreach (@$paths_abs) {
271 return 1 if (/^$path$/);
274 my($filename, $dirs, $suffix) = fileparse($path);
275 foreach (@$paths_any) {
276 return 1 if (/^$filename$/);
285 return skip($path, \@skip_parse_files_abs, \@skip_parse_files_any);
293 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required.
300 die unless $@ eq "alarm\n"; # Propagate unexpected errors.
301 printf STDERR "timed out parsing: %s\n", $file;
313 if (skip_parse($file)) {
314 dprint "skipping file: $file\n";
317 dprint "parsing: $file\n";
319 open my $fh, "<", $file or return;
321 if (may_leak_address($_)) {
322 print $file . ': ' . $_;
329 # True if we should skip walking this directory.
333 return skip($path, \@skip_walk_dirs_abs, \@skip_walk_dirs_any)
336 # Recursively walk directory tree.
341 while (my $pwd = shift @dirs) {
342 next if (skip_walk($pwd));
343 next if (!opendir(DIR, $pwd));
344 my @files = readdir(DIR);
347 foreach my $file (@files) {
348 next if ($file eq '.' or $file eq '..');
350 my $path = "$pwd/$file";
356 timed_parse_file($path);
366 # Default is to show raw results.
367 if ($raw or (!$squash_by_path and !$squash_by_filename)) {
368 dump_raw_output($file);
372 my ($total, $dmesg, $paths, $files) = parse_raw_file($file);
374 printf "\nTotal number of results from scan (incl dmesg): %d\n", $total;
376 if (!$suppress_dmesg) {
380 if ($squash_by_filename) {
381 squash_by($files, 'filename');
384 if ($squash_by_path) {
385 squash_by($paths, 'path');
393 open (my $fh, '<', $file) or die "$0: $file: $!\n";
395 if ($suppress_dmesg) {
396 if ("dmesg:" eq substr($_, 0, 6)) {
409 my $total = 0; # Total number of lines parsed.
410 my @dmesg; # dmesg output.
411 my %files; # Unique filenames containing leaks.
412 my %paths; # Unique paths containing leaks.
414 open (my $fh, '<', $file) or die "$0: $file: $!\n";
415 while (my $line = <$fh>) {
418 if ("dmesg:" eq substr($line, 0, 6)) {
423 cache_path(\%paths, $line);
424 cache_filename(\%files, $line);
427 return $total, \@dmesg, \%paths, \%files;
434 print "\ndmesg output:\n";
437 print "<no results>\n";
442 my $index = index($_, ': ');
443 $index += 2; # skid ': '
444 print substr($_, $index);
450 my ($ref, $desc) = @_;
452 print "\nResults squashed by $desc (excl dmesg). ";
453 print "Displaying [<number of results> <$desc>], <example result>\n";
455 if (keys %$ref == 0) {
456 print "<no results>\n";
460 foreach(keys %$ref) {
461 my $lines = $ref->{$_};
462 my $length = @$lines;
463 printf "[%d %s] %s", $length, $_, @$lines[0];
469 my ($paths, $line) = @_;
471 my $index = index($line, ': ');
472 my $path = substr($line, 0, $index);
474 $index += 2; # skip ': '
475 add_to_cache($paths, $path, substr($line, $index));
480 my ($files, $line) = @_;
482 my $index = index($line, ': ');
483 my $path = substr($line, 0, $index);
484 my $filename = basename($path);
486 $index += 2; # skip ': '
487 add_to_cache($files, $filename, substr($line, $index));
492 my ($cache, $key, $value) = @_;
494 if (!$cache->{$key}) {
497 push @{$cache->{$key}}, $value;