blob: 05906f6cf6b9561741ee5e815f34c22b4f3144e7 [file] [log] [blame]
Tobin C. Harding136fc5c2017-11-06 16:19:27 +11001#!/usr/bin/env perl
2#
3# (c) 2017 Tobin C. Harding <me@tobin.cc>
4# Licensed under the terms of the GNU GPL License version 2
5#
Tobin C. Harding1410fe42018-01-29 15:00:16 +11006# leaking_addresses.pl: Scan the kernel for potential leaking addresses.
Tobin C. Harding136fc5c2017-11-06 16:19:27 +11007# - Scans dmesg output.
8# - Walks directory tree and parses each file (for each directory in @DIRS).
9#
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110010# Use --debug to output path before parsing, this is useful to find files that
11# cause the script to choke.
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110012
13use warnings;
14use strict;
15use POSIX;
16use File::Basename;
17use File::Spec;
18use Cwd 'abs_path';
19use Term::ANSIColor qw(:constants);
20use Getopt::Long qw(:config no_auto_abbrev);
Tobin C. Harding62139c12017-11-09 15:19:40 +110021use Config;
Tobin C. Harding87e37582017-12-07 12:33:21 +110022use bigint qw/hex/;
Tobin C. Harding2f042c92017-12-07 14:40:29 +110023use feature 'state';
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110024
25my $P = $0;
26my $V = '0.01';
27
28# Directories to scan.
29my @DIRS = ('/proc', '/sys');
30
Tobin C. Hardingdd98c252017-11-09 15:37:06 +110031# Timer for parsing each file, in seconds.
32my $TIMEOUT = 10;
33
Tobin C. Harding1410fe42018-01-29 15:00:16 +110034# Kernel addresses vary by architecture. We can only auto-detect the following
35# architectures (using `uname -m`). (flag --32-bit overrides auto-detection.)
36my @SUPPORTED_ARCHITECTURES = ('x86_64', 'ppc64', 'x86');
Tobin C. Harding62139c12017-11-09 15:19:40 +110037
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110038# Command line options.
39my $help = 0;
40my $debug = 0;
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +110041my $raw = 0;
42my $output_raw = ""; # Write raw results to file.
43my $input_raw = ""; # Read raw results from file instead of scanning.
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +110044my $suppress_dmesg = 0; # Don't show dmesg in output.
45my $squash_by_path = 0; # Summary report grouped by absolute path.
46my $squash_by_filename = 0; # Summary report grouped by filename.
Tobin C. Hardingf9d2a422017-12-07 13:53:41 +110047my $kernel_config_file = ""; # Kernel configuration file.
Tobin C. Harding1410fe42018-01-29 15:00:16 +110048my $opt_32bit = 0; # Scan 32-bit kernel.
49my $page_offset_32bit = 0; # Page offset for 32-bit kernel.
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110050
51# Do not parse these files (absolute path).
52my @skip_parse_files_abs = ('/proc/kmsg',
53 '/proc/kcore',
54 '/proc/fs/ext4/sdb1/mb_groups',
55 '/proc/1/fd/3',
Tobin C. Harding1c1e3be2017-11-09 14:02:41 +110056 '/sys/firmware/devicetree',
57 '/proc/device-tree',
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110058 '/sys/kernel/debug/tracing/trace_pipe',
59 '/sys/kernel/security/apparmor/revision');
60
Tobin C. Hardinga2847332017-11-09 13:28:43 +110061# Do not parse these files under any subdirectory.
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110062my @skip_parse_files_any = ('0',
63 '1',
64 '2',
65 'pagemap',
66 'events',
67 'access',
68 'registers',
69 'snapshot_raw',
70 'trace_pipe_raw',
71 'ptmx',
72 'trace_pipe');
73
74# Do not walk these directories (absolute path).
75my @skip_walk_dirs_abs = ();
76
77# Do not walk these directories under any subdirectory.
78my @skip_walk_dirs_any = ('self',
79 'thread-self',
80 'cwd',
81 'fd',
Tobin C. Harding1c1e3be2017-11-09 14:02:41 +110082 'usbmon',
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110083 'stderr',
84 'stdin',
85 'stdout');
86
87sub help
88{
89 my ($exitcode) = @_;
90
91 print << "EOM";
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +110092
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110093Usage: $P [OPTIONS]
94Version: $V
95
96Options:
97
Tobin C. Harding15d60a32017-12-07 13:57:53 +110098 -o, --output-raw=<file> Save results for future processing.
99 -i, --input-raw=<file> Read results from file instead of scanning.
100 --raw Show raw results (default).
101 --suppress-dmesg Do not show dmesg results.
102 --squash-by-path Show one result per unique path.
103 --squash-by-filename Show one result per unique filename.
Tobin C. Hardingf9d2a422017-12-07 13:53:41 +1100104 --kernel-config-file=<file> Kernel configuration file (e.g /boot/config)
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100105 --32-bit Scan 32-bit kernel.
106 --page-offset-32-bit=o Page offset (for 32-bit kernel 0xABCD1234).
Tobin C. Harding15d60a32017-12-07 13:57:53 +1100107 -d, --debug Display debugging output.
108 -h, --help, --version Display this help and exit.
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +1100109
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100110Scans the running kernel for potential leaking addresses.
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100111
112EOM
113 exit($exitcode);
114}
115
116GetOptions(
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100117 'd|debug' => \$debug,
118 'h|help' => \$help,
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +1100119 'version' => \$help,
120 'o|output-raw=s' => \$output_raw,
121 'i|input-raw=s' => \$input_raw,
122 'suppress-dmesg' => \$suppress_dmesg,
123 'squash-by-path' => \$squash_by_path,
124 'squash-by-filename' => \$squash_by_filename,
125 'raw' => \$raw,
Tobin C. Hardingf9d2a422017-12-07 13:53:41 +1100126 'kernel-config-file=s' => \$kernel_config_file,
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100127 '32-bit' => \$opt_32bit,
128 'page-offset-32-bit=o' => \$page_offset_32bit,
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100129) or help(1);
130
131help(0) if ($help);
132
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +1100133if ($input_raw) {
134 format_output($input_raw);
135 exit(0);
136}
137
138if (!$input_raw and ($squash_by_path or $squash_by_filename)) {
139 printf "\nSummary reporting only available with --input-raw=<file>\n";
140 printf "(First run scan with --output-raw=<file>.)\n";
141 exit(128);
142}
143
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100144if (!(is_supported_architecture() or $opt_32bit or $page_offset_32bit)) {
Tobin C. Harding62139c12017-11-09 15:19:40 +1100145 printf "\nScript does not support your architecture, sorry.\n";
146 printf "\nCurrently we support: \n\n";
147 foreach(@SUPPORTED_ARCHITECTURES) {
148 printf "\t%s\n", $_;
149 }
Tobin C. Harding6efb7452018-01-06 09:24:49 +1100150 printf("\n");
Tobin C. Harding62139c12017-11-09 15:19:40 +1100151
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100152 printf("If you are running a 32-bit architecture you may use:\n");
153 printf("\n\t--32-bit or --page-offset-32-bit=<page offset>\n\n");
154
Tobin C. Harding6efb7452018-01-06 09:24:49 +1100155 my $archname = `uname -m`;
156 printf("Machine hardware name (`uname -m`): %s\n", $archname);
Tobin C. Harding62139c12017-11-09 15:19:40 +1100157
158 exit(129);
159}
160
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +1100161if ($output_raw) {
162 open my $fh, '>', $output_raw or die "$0: $output_raw: $!\n";
163 select $fh;
164}
165
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100166parse_dmesg();
167walk(@DIRS);
168
169exit 0;
170
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100171sub dprint
172{
173 printf(STDERR @_) if $debug;
174}
175
Tobin C. Harding62139c12017-11-09 15:19:40 +1100176sub is_supported_architecture
177{
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100178 return (is_x86_64() or is_ppc64() or is_ix86_32());
179}
180
181sub is_32bit
182{
183 # Allow --32-bit or --page-offset-32-bit to override
184 if ($opt_32bit or $page_offset_32bit) {
185 return 1;
186 }
187
188 return is_ix86_32();
189}
190
191sub is_ix86_32
192{
193 my $arch = `uname -m`;
194
195 chomp $arch;
196 if ($arch =~ m/i[3456]86/) {
197 return 1;
198 }
199 return 0;
Tobin C. Harding62139c12017-11-09 15:19:40 +1100200}
201
Tobin C. Harding5eb0da02018-01-29 14:33:49 +1100202sub is_arch
203{
204 my ($desc) = @_;
205 my $arch = `uname -m`;
206
207 chomp $arch;
208 if ($arch eq $desc) {
209 return 1;
210 }
211 return 0;
212}
213
Tobin C. Harding62139c12017-11-09 15:19:40 +1100214sub is_x86_64
215{
Tobin C. Harding5eb0da02018-01-29 14:33:49 +1100216 return is_arch('x86_64');
Tobin C. Harding62139c12017-11-09 15:19:40 +1100217}
218
219sub is_ppc64
220{
Tobin C. Harding5eb0da02018-01-29 14:33:49 +1100221 return is_arch('ppc64');
Tobin C. Harding62139c12017-11-09 15:19:40 +1100222}
223
Tobin C. Hardingf9d2a422017-12-07 13:53:41 +1100224# Gets config option value from kernel config file.
225# Returns "" on error or if config option not found.
226sub get_kernel_config_option
227{
228 my ($option) = @_;
229 my $value = "";
230 my $tmp_file = "";
231 my @config_files;
232
233 # Allow --kernel-config-file to override.
234 if ($kernel_config_file ne "") {
235 @config_files = ($kernel_config_file);
236 } elsif (-R "/proc/config.gz") {
237 my $tmp_file = "/tmp/tmpkconf";
238
239 if (system("gunzip < /proc/config.gz > $tmp_file")) {
240 dprint "$0: system(gunzip < /proc/config.gz) failed\n";
241 return "";
242 } else {
243 @config_files = ($tmp_file);
244 }
245 } else {
246 my $file = '/boot/config-' . `uname -r`;
247 chomp $file;
248 @config_files = ($file, '/boot/config');
249 }
250
251 foreach my $file (@config_files) {
252 dprint("parsing config file: %s\n", $file);
253 $value = option_from_file($option, $file);
254 if ($value ne "") {
255 last;
256 }
257 }
258
259 if ($tmp_file ne "") {
260 system("rm -f $tmp_file");
261 }
262
263 return $value;
264}
265
266# Parses $file and returns kernel configuration option value.
267sub option_from_file
268{
269 my ($option, $file) = @_;
270 my $str = "";
271 my $val = "";
272
273 open(my $fh, "<", $file) or return "";
274 while (my $line = <$fh> ) {
275 if ($line =~ /^$option/) {
276 ($str, $val) = split /=/, $line;
277 chomp $val;
278 last;
279 }
280 }
281
282 close $fh;
283 return $val;
284}
285
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100286sub is_false_positive
287{
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100288 my ($match) = @_;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100289
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100290 if (is_32bit()) {
291 return is_false_positive_32bit($match);
292 }
293
294 # 64 bit false positives.
295
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100296 if ($match =~ '\b(0x)?(f|F){16}\b' or
297 $match =~ '\b(0x)?0{16}\b') {
298 return 1;
299 }
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100300
Tobin C. Harding87e37582017-12-07 12:33:21 +1100301 if (is_x86_64() and is_in_vsyscall_memory_region($match)) {
302 return 1;
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100303 }
304
305 return 0;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100306}
307
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100308sub is_false_positive_32bit
309{
310 my ($match) = @_;
311 state $page_offset = get_page_offset();
312
313 if ($match =~ '\b(0x)?(f|F){8}\b') {
314 return 1;
315 }
316
317 if (hex($match) < $page_offset) {
318 return 1;
319 }
320
321 return 0;
322}
323
324# returns integer value
325sub get_page_offset
326{
327 my $page_offset;
328 my $default_offset = 0xc0000000;
329
330 # Allow --page-offset-32bit to override.
331 if ($page_offset_32bit != 0) {
332 return $page_offset_32bit;
333 }
334
335 $page_offset = get_kernel_config_option('CONFIG_PAGE_OFFSET');
336 if (!$page_offset) {
337 return $default_offset;
338 }
339 return $page_offset;
340}
341
Tobin C. Harding87e37582017-12-07 12:33:21 +1100342sub is_in_vsyscall_memory_region
343{
344 my ($match) = @_;
345
346 my $hex = hex($match);
347 my $region_min = hex("0xffffffffff600000");
348 my $region_max = hex("0xffffffffff601000");
349
350 return ($hex >= $region_min and $hex <= $region_max);
351}
352
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100353# True if argument potentially contains a kernel address.
354sub may_leak_address
355{
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100356 my ($line) = @_;
Tobin C. Harding62139c12017-11-09 15:19:40 +1100357 my $address_re;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100358
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100359 # Signal masks.
360 if ($line =~ '^SigBlk:' or
Tobin C. Hardinga11949e2017-11-14 09:25:11 +1100361 $line =~ '^SigIgn:' or
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100362 $line =~ '^SigCgt:') {
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100363 return 0;
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100364 }
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100365
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100366 if ($line =~ '\bKEY=[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b' or
367 $line =~ '\b[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b') {
368 return 0;
369 }
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100370
Tobin C. Harding2f042c92017-12-07 14:40:29 +1100371 $address_re = get_address_re();
Tobin C. Harding62139c12017-11-09 15:19:40 +1100372 while (/($address_re)/g) {
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100373 if (!is_false_positive($1)) {
374 return 1;
375 }
376 }
377
378 return 0;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100379}
380
Tobin C. Harding2f042c92017-12-07 14:40:29 +1100381sub get_address_re
382{
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100383 if (is_ppc64()) {
Tobin C. Harding2f042c92017-12-07 14:40:29 +1100384 return '\b(0x)?[89abcdef]00[[:xdigit:]]{13}\b';
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100385 } elsif (is_32bit()) {
386 return '\b(0x)?[[:xdigit:]]{8}\b';
Tobin C. Harding2f042c92017-12-07 14:40:29 +1100387 }
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100388
389 return get_x86_64_re();
Tobin C. Harding2f042c92017-12-07 14:40:29 +1100390}
391
392sub get_x86_64_re
393{
394 # We handle page table levels but only if explicitly configured using
395 # CONFIG_PGTABLE_LEVELS. If config file parsing fails or config option
396 # is not found we default to using address regular expression suitable
397 # for 4 page table levels.
398 state $ptl = get_kernel_config_option('CONFIG_PGTABLE_LEVELS');
399
400 if ($ptl == 5) {
401 return '\b(0x)?ff[[:xdigit:]]{14}\b';
402 }
403 return '\b(0x)?ffff[[:xdigit:]]{12}\b';
404}
405
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100406sub parse_dmesg
407{
408 open my $cmd, '-|', 'dmesg';
409 while (<$cmd>) {
410 if (may_leak_address($_)) {
411 print 'dmesg: ' . $_;
412 }
413 }
414 close $cmd;
415}
416
417# True if we should skip this path.
418sub skip
419{
420 my ($path, $paths_abs, $paths_any) = @_;
421
422 foreach (@$paths_abs) {
423 return 1 if (/^$path$/);
424 }
425
426 my($filename, $dirs, $suffix) = fileparse($path);
427 foreach (@$paths_any) {
428 return 1 if (/^$filename$/);
429 }
430
431 return 0;
432}
433
434sub skip_parse
435{
436 my ($path) = @_;
437 return skip($path, \@skip_parse_files_abs, \@skip_parse_files_any);
438}
439
Tobin C. Hardingdd98c252017-11-09 15:37:06 +1100440sub timed_parse_file
441{
442 my ($file) = @_;
443
444 eval {
445 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required.
446 alarm $TIMEOUT;
447 parse_file($file);
448 alarm 0;
449 };
450
451 if ($@) {
452 die unless $@ eq "alarm\n"; # Propagate unexpected errors.
453 printf STDERR "timed out parsing: %s\n", $file;
454 }
455}
456
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100457sub parse_file
458{
459 my ($file) = @_;
460
461 if (! -R $file) {
462 return;
463 }
464
465 if (skip_parse($file)) {
466 dprint "skipping file: $file\n";
467 return;
468 }
469 dprint "parsing: $file\n";
470
471 open my $fh, "<", $file or return;
472 while ( <$fh> ) {
473 if (may_leak_address($_)) {
474 print $file . ': ' . $_;
475 }
476 }
477 close $fh;
478}
479
480
481# True if we should skip walking this directory.
482sub skip_walk
483{
484 my ($path) = @_;
485 return skip($path, \@skip_walk_dirs_abs, \@skip_walk_dirs_any)
486}
487
488# Recursively walk directory tree.
489sub walk
490{
491 my @dirs = @_;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100492
493 while (my $pwd = shift @dirs) {
494 next if (skip_walk($pwd));
495 next if (!opendir(DIR, $pwd));
496 my @files = readdir(DIR);
497 closedir(DIR);
498
499 foreach my $file (@files) {
500 next if ($file eq '.' or $file eq '..');
501
502 my $path = "$pwd/$file";
503 next if (-l $path);
504
505 if (-d $path) {
506 push @dirs, $path;
507 } else {
Tobin C. Hardingdd98c252017-11-09 15:37:06 +1100508 timed_parse_file($path);
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100509 }
510 }
511 }
512}
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +1100513
514sub format_output
515{
516 my ($file) = @_;
517
518 # Default is to show raw results.
519 if ($raw or (!$squash_by_path and !$squash_by_filename)) {
520 dump_raw_output($file);
521 return;
522 }
523
524 my ($total, $dmesg, $paths, $files) = parse_raw_file($file);
525
526 printf "\nTotal number of results from scan (incl dmesg): %d\n", $total;
527
528 if (!$suppress_dmesg) {
529 print_dmesg($dmesg);
530 }
531
532 if ($squash_by_filename) {
533 squash_by($files, 'filename');
534 }
535
536 if ($squash_by_path) {
537 squash_by($paths, 'path');
538 }
539}
540
541sub dump_raw_output
542{
543 my ($file) = @_;
544
545 open (my $fh, '<', $file) or die "$0: $file: $!\n";
546 while (<$fh>) {
547 if ($suppress_dmesg) {
548 if ("dmesg:" eq substr($_, 0, 6)) {
549 next;
550 }
551 }
552 print $_;
553 }
554 close $fh;
555}
556
557sub parse_raw_file
558{
559 my ($file) = @_;
560
561 my $total = 0; # Total number of lines parsed.
562 my @dmesg; # dmesg output.
563 my %files; # Unique filenames containing leaks.
564 my %paths; # Unique paths containing leaks.
565
566 open (my $fh, '<', $file) or die "$0: $file: $!\n";
567 while (my $line = <$fh>) {
568 $total++;
569
570 if ("dmesg:" eq substr($line, 0, 6)) {
571 push @dmesg, $line;
572 next;
573 }
574
575 cache_path(\%paths, $line);
576 cache_filename(\%files, $line);
577 }
578
579 return $total, \@dmesg, \%paths, \%files;
580}
581
582sub print_dmesg
583{
584 my ($dmesg) = @_;
585
586 print "\ndmesg output:\n";
587
588 if (@$dmesg == 0) {
589 print "<no results>\n";
590 return;
591 }
592
593 foreach(@$dmesg) {
594 my $index = index($_, ': ');
595 $index += 2; # skid ': '
596 print substr($_, $index);
597 }
598}
599
600sub squash_by
601{
602 my ($ref, $desc) = @_;
603
604 print "\nResults squashed by $desc (excl dmesg). ";
605 print "Displaying [<number of results> <$desc>], <example result>\n";
606
607 if (keys %$ref == 0) {
608 print "<no results>\n";
609 return;
610 }
611
612 foreach(keys %$ref) {
613 my $lines = $ref->{$_};
614 my $length = @$lines;
615 printf "[%d %s] %s", $length, $_, @$lines[0];
616 }
617}
618
619sub cache_path
620{
621 my ($paths, $line) = @_;
622
623 my $index = index($line, ': ');
624 my $path = substr($line, 0, $index);
625
626 $index += 2; # skip ': '
627 add_to_cache($paths, $path, substr($line, $index));
628}
629
630sub cache_filename
631{
632 my ($files, $line) = @_;
633
634 my $index = index($line, ': ');
635 my $path = substr($line, 0, $index);
636 my $filename = basename($path);
637
638 $index += 2; # skip ': '
639 add_to_cache($files, $filename, substr($line, $index));
640}
641
642sub add_to_cache
643{
644 my ($cache, $key, $value) = @_;
645
646 if (!$cache->{$key}) {
647 $cache->{$key} = ();
648 }
649 push @{$cache->{$key}}, $value;
650}