blob: 2ad6e7fb669840b05c5af2c5dac10e2e8baf62b4 [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
Tobin C. Hardingb401f562018-02-19 11:03:37 +110051# Skip these absolute paths.
52my @skip_abs = (
53 '/proc/kmsg',
54 '/proc/device-tree',
55 '/sys/firmware/devicetree',
56 '/sys/kernel/debug/tracing/trace_pipe',
57 '/sys/kernel/security/apparmor/revision');
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110058
Tobin C. Hardingb401f562018-02-19 11:03:37 +110059# Skip these under any subdirectory.
60my @skip_any = (
61 'pagemap',
62 'events',
63 'access',
64 'registers',
65 'snapshot_raw',
66 'trace_pipe_raw',
67 'ptmx',
68 'trace_pipe',
69 'fd',
70 'usbmon');
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110071
72sub help
73{
74 my ($exitcode) = @_;
75
76 print << "EOM";
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +110077
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110078Usage: $P [OPTIONS]
79Version: $V
80
81Options:
82
Tobin C. Harding15d60a32017-12-07 13:57:53 +110083 -o, --output-raw=<file> Save results for future processing.
84 -i, --input-raw=<file> Read results from file instead of scanning.
85 --raw Show raw results (default).
86 --suppress-dmesg Do not show dmesg results.
87 --squash-by-path Show one result per unique path.
88 --squash-by-filename Show one result per unique filename.
Tobin C. Hardingf9d2a422017-12-07 13:53:41 +110089 --kernel-config-file=<file> Kernel configuration file (e.g /boot/config)
Tobin C. Harding1410fe42018-01-29 15:00:16 +110090 --32-bit Scan 32-bit kernel.
91 --page-offset-32-bit=o Page offset (for 32-bit kernel 0xABCD1234).
Tobin C. Harding15d60a32017-12-07 13:57:53 +110092 -d, --debug Display debugging output.
93 -h, --help, --version Display this help and exit.
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +110094
Tobin C. Harding1410fe42018-01-29 15:00:16 +110095Scans the running kernel for potential leaking addresses.
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110096
97EOM
98 exit($exitcode);
99}
100
101GetOptions(
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100102 'd|debug' => \$debug,
103 'h|help' => \$help,
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +1100104 'version' => \$help,
105 'o|output-raw=s' => \$output_raw,
106 'i|input-raw=s' => \$input_raw,
107 'suppress-dmesg' => \$suppress_dmesg,
108 'squash-by-path' => \$squash_by_path,
109 'squash-by-filename' => \$squash_by_filename,
110 'raw' => \$raw,
Tobin C. Hardingf9d2a422017-12-07 13:53:41 +1100111 'kernel-config-file=s' => \$kernel_config_file,
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100112 '32-bit' => \$opt_32bit,
113 'page-offset-32-bit=o' => \$page_offset_32bit,
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100114) or help(1);
115
116help(0) if ($help);
117
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +1100118if ($input_raw) {
119 format_output($input_raw);
120 exit(0);
121}
122
123if (!$input_raw and ($squash_by_path or $squash_by_filename)) {
124 printf "\nSummary reporting only available with --input-raw=<file>\n";
125 printf "(First run scan with --output-raw=<file>.)\n";
126 exit(128);
127}
128
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100129if (!(is_supported_architecture() or $opt_32bit or $page_offset_32bit)) {
Tobin C. Harding62139c12017-11-09 15:19:40 +1100130 printf "\nScript does not support your architecture, sorry.\n";
131 printf "\nCurrently we support: \n\n";
132 foreach(@SUPPORTED_ARCHITECTURES) {
133 printf "\t%s\n", $_;
134 }
Tobin C. Harding6efb7452018-01-06 09:24:49 +1100135 printf("\n");
Tobin C. Harding62139c12017-11-09 15:19:40 +1100136
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100137 printf("If you are running a 32-bit architecture you may use:\n");
138 printf("\n\t--32-bit or --page-offset-32-bit=<page offset>\n\n");
139
Tobin C. Harding6efb7452018-01-06 09:24:49 +1100140 my $archname = `uname -m`;
141 printf("Machine hardware name (`uname -m`): %s\n", $archname);
Tobin C. Harding62139c12017-11-09 15:19:40 +1100142
143 exit(129);
144}
145
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +1100146if ($output_raw) {
147 open my $fh, '>', $output_raw or die "$0: $output_raw: $!\n";
148 select $fh;
149}
150
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100151parse_dmesg();
152walk(@DIRS);
153
154exit 0;
155
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100156sub dprint
157{
158 printf(STDERR @_) if $debug;
159}
160
Tobin C. Harding62139c12017-11-09 15:19:40 +1100161sub is_supported_architecture
162{
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100163 return (is_x86_64() or is_ppc64() or is_ix86_32());
164}
165
166sub is_32bit
167{
168 # Allow --32-bit or --page-offset-32-bit to override
169 if ($opt_32bit or $page_offset_32bit) {
170 return 1;
171 }
172
173 return is_ix86_32();
174}
175
176sub is_ix86_32
177{
178 my $arch = `uname -m`;
179
180 chomp $arch;
181 if ($arch =~ m/i[3456]86/) {
182 return 1;
183 }
184 return 0;
Tobin C. Harding62139c12017-11-09 15:19:40 +1100185}
186
Tobin C. Harding5eb0da02018-01-29 14:33:49 +1100187sub is_arch
188{
189 my ($desc) = @_;
190 my $arch = `uname -m`;
191
192 chomp $arch;
193 if ($arch eq $desc) {
194 return 1;
195 }
196 return 0;
197}
198
Tobin C. Harding62139c12017-11-09 15:19:40 +1100199sub is_x86_64
200{
Tobin C. Harding5eb0da02018-01-29 14:33:49 +1100201 return is_arch('x86_64');
Tobin C. Harding62139c12017-11-09 15:19:40 +1100202}
203
204sub is_ppc64
205{
Tobin C. Harding5eb0da02018-01-29 14:33:49 +1100206 return is_arch('ppc64');
Tobin C. Harding62139c12017-11-09 15:19:40 +1100207}
208
Tobin C. Hardingf9d2a422017-12-07 13:53:41 +1100209# Gets config option value from kernel config file.
210# Returns "" on error or if config option not found.
211sub get_kernel_config_option
212{
213 my ($option) = @_;
214 my $value = "";
215 my $tmp_file = "";
216 my @config_files;
217
218 # Allow --kernel-config-file to override.
219 if ($kernel_config_file ne "") {
220 @config_files = ($kernel_config_file);
221 } elsif (-R "/proc/config.gz") {
222 my $tmp_file = "/tmp/tmpkconf";
223
224 if (system("gunzip < /proc/config.gz > $tmp_file")) {
225 dprint "$0: system(gunzip < /proc/config.gz) failed\n";
226 return "";
227 } else {
228 @config_files = ($tmp_file);
229 }
230 } else {
231 my $file = '/boot/config-' . `uname -r`;
232 chomp $file;
233 @config_files = ($file, '/boot/config');
234 }
235
236 foreach my $file (@config_files) {
237 dprint("parsing config file: %s\n", $file);
238 $value = option_from_file($option, $file);
239 if ($value ne "") {
240 last;
241 }
242 }
243
244 if ($tmp_file ne "") {
245 system("rm -f $tmp_file");
246 }
247
248 return $value;
249}
250
251# Parses $file and returns kernel configuration option value.
252sub option_from_file
253{
254 my ($option, $file) = @_;
255 my $str = "";
256 my $val = "";
257
258 open(my $fh, "<", $file) or return "";
259 while (my $line = <$fh> ) {
260 if ($line =~ /^$option/) {
261 ($str, $val) = split /=/, $line;
262 chomp $val;
263 last;
264 }
265 }
266
267 close $fh;
268 return $val;
269}
270
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100271sub is_false_positive
272{
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100273 my ($match) = @_;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100274
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100275 if (is_32bit()) {
276 return is_false_positive_32bit($match);
277 }
278
279 # 64 bit false positives.
280
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100281 if ($match =~ '\b(0x)?(f|F){16}\b' or
282 $match =~ '\b(0x)?0{16}\b') {
283 return 1;
284 }
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100285
Tobin C. Harding87e37582017-12-07 12:33:21 +1100286 if (is_x86_64() and is_in_vsyscall_memory_region($match)) {
287 return 1;
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100288 }
289
290 return 0;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100291}
292
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100293sub is_false_positive_32bit
294{
295 my ($match) = @_;
296 state $page_offset = get_page_offset();
297
298 if ($match =~ '\b(0x)?(f|F){8}\b') {
299 return 1;
300 }
301
302 if (hex($match) < $page_offset) {
303 return 1;
304 }
305
306 return 0;
307}
308
309# returns integer value
310sub get_page_offset
311{
312 my $page_offset;
313 my $default_offset = 0xc0000000;
314
315 # Allow --page-offset-32bit to override.
316 if ($page_offset_32bit != 0) {
317 return $page_offset_32bit;
318 }
319
320 $page_offset = get_kernel_config_option('CONFIG_PAGE_OFFSET');
321 if (!$page_offset) {
322 return $default_offset;
323 }
324 return $page_offset;
325}
326
Tobin C. Harding87e37582017-12-07 12:33:21 +1100327sub is_in_vsyscall_memory_region
328{
329 my ($match) = @_;
330
331 my $hex = hex($match);
332 my $region_min = hex("0xffffffffff600000");
333 my $region_max = hex("0xffffffffff601000");
334
335 return ($hex >= $region_min and $hex <= $region_max);
336}
337
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100338# True if argument potentially contains a kernel address.
339sub may_leak_address
340{
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100341 my ($line) = @_;
Tobin C. Harding62139c12017-11-09 15:19:40 +1100342 my $address_re;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100343
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100344 # Signal masks.
345 if ($line =~ '^SigBlk:' or
Tobin C. Hardinga11949e2017-11-14 09:25:11 +1100346 $line =~ '^SigIgn:' or
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100347 $line =~ '^SigCgt:') {
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100348 return 0;
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100349 }
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100350
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100351 if ($line =~ '\bKEY=[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b' or
352 $line =~ '\b[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b') {
353 return 0;
354 }
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100355
Tobin C. Harding2f042c92017-12-07 14:40:29 +1100356 $address_re = get_address_re();
Tobin C. Harding62139c12017-11-09 15:19:40 +1100357 while (/($address_re)/g) {
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100358 if (!is_false_positive($1)) {
359 return 1;
360 }
361 }
362
363 return 0;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100364}
365
Tobin C. Harding2f042c92017-12-07 14:40:29 +1100366sub get_address_re
367{
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100368 if (is_ppc64()) {
Tobin C. Harding2f042c92017-12-07 14:40:29 +1100369 return '\b(0x)?[89abcdef]00[[:xdigit:]]{13}\b';
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100370 } elsif (is_32bit()) {
371 return '\b(0x)?[[:xdigit:]]{8}\b';
Tobin C. Harding2f042c92017-12-07 14:40:29 +1100372 }
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100373
374 return get_x86_64_re();
Tobin C. Harding2f042c92017-12-07 14:40:29 +1100375}
376
377sub get_x86_64_re
378{
379 # We handle page table levels but only if explicitly configured using
380 # CONFIG_PGTABLE_LEVELS. If config file parsing fails or config option
381 # is not found we default to using address regular expression suitable
382 # for 4 page table levels.
383 state $ptl = get_kernel_config_option('CONFIG_PGTABLE_LEVELS');
384
385 if ($ptl == 5) {
386 return '\b(0x)?ff[[:xdigit:]]{14}\b';
387 }
388 return '\b(0x)?ffff[[:xdigit:]]{12}\b';
389}
390
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100391sub parse_dmesg
392{
393 open my $cmd, '-|', 'dmesg';
394 while (<$cmd>) {
395 if (may_leak_address($_)) {
396 print 'dmesg: ' . $_;
397 }
398 }
399 close $cmd;
400}
401
402# True if we should skip this path.
403sub skip
404{
Tobin C. Hardingb401f562018-02-19 11:03:37 +1100405 my ($path) = @_;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100406
Tobin C. Hardingb401f562018-02-19 11:03:37 +1100407 foreach (@skip_abs) {
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100408 return 1 if (/^$path$/);
409 }
410
411 my($filename, $dirs, $suffix) = fileparse($path);
Tobin C. Hardingb401f562018-02-19 11:03:37 +1100412 foreach (@skip_any) {
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100413 return 1 if (/^$filename$/);
414 }
415
416 return 0;
417}
418
Tobin C. Hardingdd98c252017-11-09 15:37:06 +1100419sub timed_parse_file
420{
421 my ($file) = @_;
422
423 eval {
424 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required.
425 alarm $TIMEOUT;
426 parse_file($file);
427 alarm 0;
428 };
429
430 if ($@) {
431 die unless $@ eq "alarm\n"; # Propagate unexpected errors.
432 printf STDERR "timed out parsing: %s\n", $file;
433 }
434}
435
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100436sub parse_file
437{
438 my ($file) = @_;
439
440 if (! -R $file) {
441 return;
442 }
443
Tobin C. Hardinge2858ca2018-02-19 10:22:15 +1100444 if (! -T $file) {
445 return;
446 }
447
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100448 open my $fh, "<", $file or return;
449 while ( <$fh> ) {
450 if (may_leak_address($_)) {
451 print $file . ': ' . $_;
452 }
453 }
454 close $fh;
455}
456
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100457# Recursively walk directory tree.
458sub walk
459{
460 my @dirs = @_;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100461
462 while (my $pwd = shift @dirs) {
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100463 next if (!opendir(DIR, $pwd));
464 my @files = readdir(DIR);
465 closedir(DIR);
466
467 foreach my $file (@files) {
468 next if ($file eq '.' or $file eq '..');
469
470 my $path = "$pwd/$file";
471 next if (-l $path);
472
Tobin C. Hardingb401f562018-02-19 11:03:37 +1100473 next if (skip($path));
474
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100475 if (-d $path) {
476 push @dirs, $path;
Tobin C. Hardingb401f562018-02-19 11:03:37 +1100477 next;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100478 }
Tobin C. Hardingb401f562018-02-19 11:03:37 +1100479
480 dprint "parsing: $path\n";
481 timed_parse_file($path);
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100482 }
483 }
484}
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +1100485
486sub format_output
487{
488 my ($file) = @_;
489
490 # Default is to show raw results.
491 if ($raw or (!$squash_by_path and !$squash_by_filename)) {
492 dump_raw_output($file);
493 return;
494 }
495
496 my ($total, $dmesg, $paths, $files) = parse_raw_file($file);
497
498 printf "\nTotal number of results from scan (incl dmesg): %d\n", $total;
499
500 if (!$suppress_dmesg) {
501 print_dmesg($dmesg);
502 }
503
504 if ($squash_by_filename) {
505 squash_by($files, 'filename');
506 }
507
508 if ($squash_by_path) {
509 squash_by($paths, 'path');
510 }
511}
512
513sub dump_raw_output
514{
515 my ($file) = @_;
516
517 open (my $fh, '<', $file) or die "$0: $file: $!\n";
518 while (<$fh>) {
519 if ($suppress_dmesg) {
520 if ("dmesg:" eq substr($_, 0, 6)) {
521 next;
522 }
523 }
524 print $_;
525 }
526 close $fh;
527}
528
529sub parse_raw_file
530{
531 my ($file) = @_;
532
533 my $total = 0; # Total number of lines parsed.
534 my @dmesg; # dmesg output.
535 my %files; # Unique filenames containing leaks.
536 my %paths; # Unique paths containing leaks.
537
538 open (my $fh, '<', $file) or die "$0: $file: $!\n";
539 while (my $line = <$fh>) {
540 $total++;
541
542 if ("dmesg:" eq substr($line, 0, 6)) {
543 push @dmesg, $line;
544 next;
545 }
546
547 cache_path(\%paths, $line);
548 cache_filename(\%files, $line);
549 }
550
551 return $total, \@dmesg, \%paths, \%files;
552}
553
554sub print_dmesg
555{
556 my ($dmesg) = @_;
557
558 print "\ndmesg output:\n";
559
560 if (@$dmesg == 0) {
561 print "<no results>\n";
562 return;
563 }
564
565 foreach(@$dmesg) {
566 my $index = index($_, ': ');
567 $index += 2; # skid ': '
568 print substr($_, $index);
569 }
570}
571
572sub squash_by
573{
574 my ($ref, $desc) = @_;
575
576 print "\nResults squashed by $desc (excl dmesg). ";
577 print "Displaying [<number of results> <$desc>], <example result>\n";
578
579 if (keys %$ref == 0) {
580 print "<no results>\n";
581 return;
582 }
583
584 foreach(keys %$ref) {
585 my $lines = $ref->{$_};
586 my $length = @$lines;
587 printf "[%d %s] %s", $length, $_, @$lines[0];
588 }
589}
590
591sub cache_path
592{
593 my ($paths, $line) = @_;
594
595 my $index = index($line, ': ');
596 my $path = substr($line, 0, $index);
597
598 $index += 2; # skip ': '
599 add_to_cache($paths, $path, substr($line, $index));
600}
601
602sub cache_filename
603{
604 my ($files, $line) = @_;
605
606 my $index = index($line, ': ');
607 my $path = substr($line, 0, $index);
608 my $filename = basename($path);
609
610 $index += 2; # skip ': '
611 add_to_cache($files, $filename, substr($line, $index));
612}
613
614sub add_to_cache
615{
616 my ($cache, $key, $value) = @_;
617
618 if (!$cache->{$key}) {
619 $cache->{$key} = ();
620 }
621 push @{$cache->{$key}}, $value;
622}