blob: 6e5bc57caeaaf09373a479f2eaf3032e19ec61e2 [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{
Tobin C. Harding5e4bac32018-02-19 13:23:44 +1100178 state $arch = `uname -m`;
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100179
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. Harding5e4bac32018-02-19 13:23:44 +1100201 state $is = is_arch('x86_64');
202 return $is;
Tobin C. Harding62139c12017-11-09 15:19:40 +1100203}
204
205sub is_ppc64
206{
Tobin C. Harding5e4bac32018-02-19 13:23:44 +1100207 state $is = is_arch('ppc64');
208 return $is;
Tobin C. Harding62139c12017-11-09 15:19:40 +1100209}
210
Tobin C. Hardingf9d2a422017-12-07 13:53:41 +1100211# Gets config option value from kernel config file.
212# Returns "" on error or if config option not found.
213sub get_kernel_config_option
214{
215 my ($option) = @_;
216 my $value = "";
217 my $tmp_file = "";
218 my @config_files;
219
220 # Allow --kernel-config-file to override.
221 if ($kernel_config_file ne "") {
222 @config_files = ($kernel_config_file);
223 } elsif (-R "/proc/config.gz") {
224 my $tmp_file = "/tmp/tmpkconf";
225
226 if (system("gunzip < /proc/config.gz > $tmp_file")) {
227 dprint "$0: system(gunzip < /proc/config.gz) failed\n";
228 return "";
229 } else {
230 @config_files = ($tmp_file);
231 }
232 } else {
233 my $file = '/boot/config-' . `uname -r`;
234 chomp $file;
235 @config_files = ($file, '/boot/config');
236 }
237
238 foreach my $file (@config_files) {
239 dprint("parsing config file: %s\n", $file);
240 $value = option_from_file($option, $file);
241 if ($value ne "") {
242 last;
243 }
244 }
245
246 if ($tmp_file ne "") {
247 system("rm -f $tmp_file");
248 }
249
250 return $value;
251}
252
253# Parses $file and returns kernel configuration option value.
254sub option_from_file
255{
256 my ($option, $file) = @_;
257 my $str = "";
258 my $val = "";
259
260 open(my $fh, "<", $file) or return "";
261 while (my $line = <$fh> ) {
262 if ($line =~ /^$option/) {
263 ($str, $val) = split /=/, $line;
264 chomp $val;
265 last;
266 }
267 }
268
269 close $fh;
270 return $val;
271}
272
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100273sub is_false_positive
274{
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100275 my ($match) = @_;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100276
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100277 if (is_32bit()) {
278 return is_false_positive_32bit($match);
279 }
280
281 # 64 bit false positives.
282
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100283 if ($match =~ '\b(0x)?(f|F){16}\b' or
284 $match =~ '\b(0x)?0{16}\b') {
285 return 1;
286 }
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100287
Tobin C. Harding87e37582017-12-07 12:33:21 +1100288 if (is_x86_64() and is_in_vsyscall_memory_region($match)) {
289 return 1;
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100290 }
291
292 return 0;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100293}
294
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100295sub is_false_positive_32bit
296{
297 my ($match) = @_;
298 state $page_offset = get_page_offset();
299
300 if ($match =~ '\b(0x)?(f|F){8}\b') {
301 return 1;
302 }
303
304 if (hex($match) < $page_offset) {
305 return 1;
306 }
307
308 return 0;
309}
310
311# returns integer value
312sub get_page_offset
313{
314 my $page_offset;
315 my $default_offset = 0xc0000000;
316
317 # Allow --page-offset-32bit to override.
318 if ($page_offset_32bit != 0) {
319 return $page_offset_32bit;
320 }
321
322 $page_offset = get_kernel_config_option('CONFIG_PAGE_OFFSET');
323 if (!$page_offset) {
324 return $default_offset;
325 }
326 return $page_offset;
327}
328
Tobin C. Harding87e37582017-12-07 12:33:21 +1100329sub is_in_vsyscall_memory_region
330{
331 my ($match) = @_;
332
333 my $hex = hex($match);
334 my $region_min = hex("0xffffffffff600000");
335 my $region_max = hex("0xffffffffff601000");
336
337 return ($hex >= $region_min and $hex <= $region_max);
338}
339
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100340# True if argument potentially contains a kernel address.
341sub may_leak_address
342{
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100343 my ($line) = @_;
Tobin C. Harding62139c12017-11-09 15:19:40 +1100344 my $address_re;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100345
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100346 # Signal masks.
347 if ($line =~ '^SigBlk:' or
Tobin C. Hardinga11949e2017-11-14 09:25:11 +1100348 $line =~ '^SigIgn:' or
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100349 $line =~ '^SigCgt:') {
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100350 return 0;
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100351 }
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100352
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100353 if ($line =~ '\bKEY=[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b' or
354 $line =~ '\b[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b') {
355 return 0;
356 }
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100357
Tobin C. Harding2f042c92017-12-07 14:40:29 +1100358 $address_re = get_address_re();
Tobin C. Harding62139c12017-11-09 15:19:40 +1100359 while (/($address_re)/g) {
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100360 if (!is_false_positive($1)) {
361 return 1;
362 }
363 }
364
365 return 0;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100366}
367
Tobin C. Harding2f042c92017-12-07 14:40:29 +1100368sub get_address_re
369{
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100370 if (is_ppc64()) {
Tobin C. Harding2f042c92017-12-07 14:40:29 +1100371 return '\b(0x)?[89abcdef]00[[:xdigit:]]{13}\b';
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100372 } elsif (is_32bit()) {
373 return '\b(0x)?[[:xdigit:]]{8}\b';
Tobin C. Harding2f042c92017-12-07 14:40:29 +1100374 }
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100375
376 return get_x86_64_re();
Tobin C. Harding2f042c92017-12-07 14:40:29 +1100377}
378
379sub get_x86_64_re
380{
381 # We handle page table levels but only if explicitly configured using
382 # CONFIG_PGTABLE_LEVELS. If config file parsing fails or config option
383 # is not found we default to using address regular expression suitable
384 # for 4 page table levels.
385 state $ptl = get_kernel_config_option('CONFIG_PGTABLE_LEVELS');
386
387 if ($ptl == 5) {
388 return '\b(0x)?ff[[:xdigit:]]{14}\b';
389 }
390 return '\b(0x)?ffff[[:xdigit:]]{12}\b';
391}
392
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100393sub parse_dmesg
394{
395 open my $cmd, '-|', 'dmesg';
396 while (<$cmd>) {
397 if (may_leak_address($_)) {
398 print 'dmesg: ' . $_;
399 }
400 }
401 close $cmd;
402}
403
404# True if we should skip this path.
405sub skip
406{
Tobin C. Hardingb401f562018-02-19 11:03:37 +1100407 my ($path) = @_;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100408
Tobin C. Hardingb401f562018-02-19 11:03:37 +1100409 foreach (@skip_abs) {
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100410 return 1 if (/^$path$/);
411 }
412
413 my($filename, $dirs, $suffix) = fileparse($path);
Tobin C. Hardingb401f562018-02-19 11:03:37 +1100414 foreach (@skip_any) {
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100415 return 1 if (/^$filename$/);
416 }
417
418 return 0;
419}
420
Tobin C. Hardingdd98c252017-11-09 15:37:06 +1100421sub timed_parse_file
422{
423 my ($file) = @_;
424
425 eval {
426 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required.
427 alarm $TIMEOUT;
428 parse_file($file);
429 alarm 0;
430 };
431
432 if ($@) {
433 die unless $@ eq "alarm\n"; # Propagate unexpected errors.
434 printf STDERR "timed out parsing: %s\n", $file;
435 }
436}
437
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100438sub parse_file
439{
440 my ($file) = @_;
441
442 if (! -R $file) {
443 return;
444 }
445
Tobin C. Hardinge2858ca2018-02-19 10:22:15 +1100446 if (! -T $file) {
447 return;
448 }
449
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100450 open my $fh, "<", $file or return;
451 while ( <$fh> ) {
452 if (may_leak_address($_)) {
453 print $file . ': ' . $_;
454 }
455 }
456 close $fh;
457}
458
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100459# Recursively walk directory tree.
460sub walk
461{
462 my @dirs = @_;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100463
464 while (my $pwd = shift @dirs) {
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100465 next if (!opendir(DIR, $pwd));
466 my @files = readdir(DIR);
467 closedir(DIR);
468
469 foreach my $file (@files) {
470 next if ($file eq '.' or $file eq '..');
471
472 my $path = "$pwd/$file";
473 next if (-l $path);
474
Tobin C. Hardingb401f562018-02-19 11:03:37 +1100475 next if (skip($path));
476
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100477 if (-d $path) {
478 push @dirs, $path;
Tobin C. Hardingb401f562018-02-19 11:03:37 +1100479 next;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100480 }
Tobin C. Hardingb401f562018-02-19 11:03:37 +1100481
482 dprint "parsing: $path\n";
483 timed_parse_file($path);
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100484 }
485 }
486}
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +1100487
488sub format_output
489{
490 my ($file) = @_;
491
492 # Default is to show raw results.
493 if ($raw or (!$squash_by_path and !$squash_by_filename)) {
494 dump_raw_output($file);
495 return;
496 }
497
498 my ($total, $dmesg, $paths, $files) = parse_raw_file($file);
499
500 printf "\nTotal number of results from scan (incl dmesg): %d\n", $total;
501
502 if (!$suppress_dmesg) {
503 print_dmesg($dmesg);
504 }
505
506 if ($squash_by_filename) {
507 squash_by($files, 'filename');
508 }
509
510 if ($squash_by_path) {
511 squash_by($paths, 'path');
512 }
513}
514
515sub dump_raw_output
516{
517 my ($file) = @_;
518
519 open (my $fh, '<', $file) or die "$0: $file: $!\n";
520 while (<$fh>) {
521 if ($suppress_dmesg) {
522 if ("dmesg:" eq substr($_, 0, 6)) {
523 next;
524 }
525 }
526 print $_;
527 }
528 close $fh;
529}
530
531sub parse_raw_file
532{
533 my ($file) = @_;
534
535 my $total = 0; # Total number of lines parsed.
536 my @dmesg; # dmesg output.
537 my %files; # Unique filenames containing leaks.
538 my %paths; # Unique paths containing leaks.
539
540 open (my $fh, '<', $file) or die "$0: $file: $!\n";
541 while (my $line = <$fh>) {
542 $total++;
543
544 if ("dmesg:" eq substr($line, 0, 6)) {
545 push @dmesg, $line;
546 next;
547 }
548
549 cache_path(\%paths, $line);
550 cache_filename(\%files, $line);
551 }
552
553 return $total, \@dmesg, \%paths, \%files;
554}
555
556sub print_dmesg
557{
558 my ($dmesg) = @_;
559
560 print "\ndmesg output:\n";
561
562 if (@$dmesg == 0) {
563 print "<no results>\n";
564 return;
565 }
566
567 foreach(@$dmesg) {
568 my $index = index($_, ': ');
569 $index += 2; # skid ': '
570 print substr($_, $index);
571 }
572}
573
574sub squash_by
575{
576 my ($ref, $desc) = @_;
577
578 print "\nResults squashed by $desc (excl dmesg). ";
579 print "Displaying [<number of results> <$desc>], <example result>\n";
580
581 if (keys %$ref == 0) {
582 print "<no results>\n";
583 return;
584 }
585
586 foreach(keys %$ref) {
587 my $lines = $ref->{$_};
588 my $length = @$lines;
589 printf "[%d %s] %s", $length, $_, @$lines[0];
590 }
591}
592
593sub cache_path
594{
595 my ($paths, $line) = @_;
596
597 my $index = index($line, ': ');
598 my $path = substr($line, 0, $index);
599
600 $index += 2; # skip ': '
601 add_to_cache($paths, $path, substr($line, $index));
602}
603
604sub cache_filename
605{
606 my ($files, $line) = @_;
607
608 my $index = index($line, ': ');
609 my $path = substr($line, 0, $index);
610 my $filename = basename($path);
611
612 $index += 2; # skip ': '
613 add_to_cache($files, $filename, substr($line, $index));
614}
615
616sub add_to_cache
617{
618 my ($cache, $key, $value) = @_;
619
620 if (!$cache->{$key}) {
621 $cache->{$key} = ();
622 }
623 push @{$cache->{$key}}, $value;
624}