leaking_addresses: add timeout on file read
[linux-block.git] / scripts / leaking_addresses.pl
CommitLineData
136fc5c4
TH
1#!/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#
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).
9#
136fc5c4
TH
10# Use --debug to output path before parsing, this is useful to find files that
11# cause the script to choke.
12#
13# You may like to set kptr_restrict=2 before running script
14# (see Documentation/sysctl/kernel.txt).
15
16use warnings;
17use strict;
18use POSIX;
19use File::Basename;
20use File::Spec;
21use Cwd 'abs_path';
22use Term::ANSIColor qw(:constants);
23use Getopt::Long qw(:config no_auto_abbrev);
62139c12 24use Config;
136fc5c4
TH
25
26my $P = $0;
27my $V = '0.01';
28
29# Directories to scan.
30my @DIRS = ('/proc', '/sys');
31
dd98c252
TH
32# Timer for parsing each file, in seconds.
33my $TIMEOUT = 10;
34
62139c12
TH
35# Script can only grep for kernel addresses on the following architectures. If
36# your architecture is not listed here and has a grep'able kernel address please
37# consider submitting a patch.
38my @SUPPORTED_ARCHITECTURES = ('x86_64', 'ppc64');
39
136fc5c4
TH
40# Command line options.
41my $help = 0;
42my $debug = 0;
d09bd8da
TH
43my $raw = 0;
44my $output_raw = ""; # Write raw results to file.
45my $input_raw = ""; # Read raw results from file instead of scanning.
46
47my $suppress_dmesg = 0; # Don't show dmesg in output.
48my $squash_by_path = 0; # Summary report grouped by absolute path.
49my $squash_by_filename = 0; # Summary report grouped by filename.
136fc5c4
TH
50
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',
1c1e3be0
TH
56 '/sys/firmware/devicetree',
57 '/proc/device-tree',
136fc5c4
TH
58 '/sys/kernel/debug/tracing/trace_pipe',
59 '/sys/kernel/security/apparmor/revision');
60
a284733e 61# Do not parse these files under any subdirectory.
136fc5c4
TH
62my @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',
1c1e3be0 82 'usbmon',
136fc5c4
TH
83 'stderr',
84 'stdin',
85 'stdout');
86
87sub help
88{
89 my ($exitcode) = @_;
90
91 print << "EOM";
d09bd8da 92
136fc5c4
TH
93Usage: $P [OPTIONS]
94Version: $V
95
96Options:
97
d09bd8da
TH
98 -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.
104 -d, --debug Display debugging output.
105 -h, --help, --version Display this help and exit.
106
107Examples:
108
109 # Scan kernel and dump raw results.
110 $0
111
112 # Scan kernel and save results to file.
113 $0 --output-raw scan.out
114
115 # View summary report.
116 $0 --input-raw scan.out --squash-by-filename
136fc5c4 117
136fc5c4
TH
118Scans the running (64 bit) kernel for potential leaking addresses.
119
120EOM
121 exit($exitcode);
122}
123
124GetOptions(
136fc5c4
TH
125 'd|debug' => \$debug,
126 'h|help' => \$help,
d09bd8da
TH
127 'version' => \$help,
128 'o|output-raw=s' => \$output_raw,
129 'i|input-raw=s' => \$input_raw,
130 'suppress-dmesg' => \$suppress_dmesg,
131 'squash-by-path' => \$squash_by_path,
132 'squash-by-filename' => \$squash_by_filename,
133 'raw' => \$raw,
136fc5c4
TH
134) or help(1);
135
136help(0) if ($help);
137
d09bd8da
TH
138if ($input_raw) {
139 format_output($input_raw);
140 exit(0);
141}
142
143if (!$input_raw and ($squash_by_path or $squash_by_filename)) {
144 printf "\nSummary reporting only available with --input-raw=<file>\n";
145 printf "(First run scan with --output-raw=<file>.)\n";
146 exit(128);
147}
148
62139c12
TH
149if (!is_supported_architecture()) {
150 printf "\nScript does not support your architecture, sorry.\n";
151 printf "\nCurrently we support: \n\n";
152 foreach(@SUPPORTED_ARCHITECTURES) {
153 printf "\t%s\n", $_;
154 }
155
156 my $archname = $Config{archname};
157 printf "\n\$ perl -MConfig -e \'print \"\$Config{archname}\\n\"\'\n";
158 printf "%s\n", $archname;
159
160 exit(129);
161}
162
d09bd8da
TH
163if ($output_raw) {
164 open my $fh, '>', $output_raw or die "$0: $output_raw: $!\n";
165 select $fh;
166}
167
136fc5c4
TH
168parse_dmesg();
169walk(@DIRS);
170
171exit 0;
172
136fc5c4
TH
173sub dprint
174{
175 printf(STDERR @_) if $debug;
176}
177
62139c12
TH
178sub is_supported_architecture
179{
180 return (is_x86_64() or is_ppc64());
181}
182
183sub is_x86_64
184{
185 my $archname = $Config{archname};
186
187 if ($archname =~ m/x86_64/) {
188 return 1;
189 }
190 return 0;
191}
192
193sub is_ppc64
194{
195 my $archname = $Config{archname};
196
197 if ($archname =~ m/powerpc/ and $archname =~ m/64/) {
198 return 1;
199 }
200 return 0;
201}
202
136fc5c4
TH
203sub is_false_positive
204{
7e5758f7
TH
205 my ($match) = @_;
206
207 if ($match =~ '\b(0x)?(f|F){16}\b' or
208 $match =~ '\b(0x)?0{16}\b') {
209 return 1;
210 }
136fc5c4 211
62139c12
TH
212 if (is_x86_64) {
213 # vsyscall memory region, we should probably check against a range here.
214 if ($match =~ '\bf{10}600000\b' or
215 $match =~ '\bf{10}601000\b') {
216 return 1;
217 }
7e5758f7 218 }
136fc5c4 219
7e5758f7 220 return 0;
136fc5c4
TH
221}
222
223# True if argument potentially contains a kernel address.
224sub may_leak_address
225{
7e5758f7 226 my ($line) = @_;
62139c12 227 my $address_re;
136fc5c4 228
7e5758f7
TH
229 # Signal masks.
230 if ($line =~ '^SigBlk:' or
231 $line =~ '^SigCgt:') {
232 return 0;
233 }
136fc5c4 234
7e5758f7
TH
235 if ($line =~ '\bKEY=[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b' or
236 $line =~ '\b[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b') {
136fc5c4 237 return 0;
7e5758f7 238 }
136fc5c4 239
62139c12
TH
240 # One of these is guaranteed to be true.
241 if (is_x86_64()) {
242 $address_re = '\b(0x)?ffff[[:xdigit:]]{12}\b';
243 } elsif (is_ppc64()) {
244 $address_re = '\b(0x)?[89abcdef]00[[:xdigit:]]{13}\b';
245 }
246
247 while (/($address_re)/g) {
7e5758f7
TH
248 if (!is_false_positive($1)) {
249 return 1;
250 }
251 }
136fc5c4 252
7e5758f7 253 return 0;
136fc5c4
TH
254}
255
256sub parse_dmesg
257{
258 open my $cmd, '-|', 'dmesg';
259 while (<$cmd>) {
260 if (may_leak_address($_)) {
261 print 'dmesg: ' . $_;
262 }
263 }
264 close $cmd;
265}
266
267# True if we should skip this path.
268sub skip
269{
270 my ($path, $paths_abs, $paths_any) = @_;
271
272 foreach (@$paths_abs) {
273 return 1 if (/^$path$/);
274 }
275
276 my($filename, $dirs, $suffix) = fileparse($path);
277 foreach (@$paths_any) {
278 return 1 if (/^$filename$/);
279 }
280
281 return 0;
282}
283
284sub skip_parse
285{
286 my ($path) = @_;
287 return skip($path, \@skip_parse_files_abs, \@skip_parse_files_any);
288}
289
dd98c252
TH
290sub timed_parse_file
291{
292 my ($file) = @_;
293
294 eval {
295 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required.
296 alarm $TIMEOUT;
297 parse_file($file);
298 alarm 0;
299 };
300
301 if ($@) {
302 die unless $@ eq "alarm\n"; # Propagate unexpected errors.
303 printf STDERR "timed out parsing: %s\n", $file;
304 }
305}
306
136fc5c4
TH
307sub parse_file
308{
309 my ($file) = @_;
310
311 if (! -R $file) {
312 return;
313 }
314
315 if (skip_parse($file)) {
316 dprint "skipping file: $file\n";
317 return;
318 }
319 dprint "parsing: $file\n";
320
321 open my $fh, "<", $file or return;
322 while ( <$fh> ) {
323 if (may_leak_address($_)) {
324 print $file . ': ' . $_;
325 }
326 }
327 close $fh;
328}
329
330
331# True if we should skip walking this directory.
332sub skip_walk
333{
334 my ($path) = @_;
335 return skip($path, \@skip_walk_dirs_abs, \@skip_walk_dirs_any)
336}
337
338# Recursively walk directory tree.
339sub walk
340{
341 my @dirs = @_;
136fc5c4
TH
342
343 while (my $pwd = shift @dirs) {
344 next if (skip_walk($pwd));
345 next if (!opendir(DIR, $pwd));
346 my @files = readdir(DIR);
347 closedir(DIR);
348
349 foreach my $file (@files) {
350 next if ($file eq '.' or $file eq '..');
351
352 my $path = "$pwd/$file";
353 next if (-l $path);
354
355 if (-d $path) {
356 push @dirs, $path;
357 } else {
dd98c252 358 timed_parse_file($path);
136fc5c4
TH
359 }
360 }
361 }
362}
d09bd8da
TH
363
364sub format_output
365{
366 my ($file) = @_;
367
368 # Default is to show raw results.
369 if ($raw or (!$squash_by_path and !$squash_by_filename)) {
370 dump_raw_output($file);
371 return;
372 }
373
374 my ($total, $dmesg, $paths, $files) = parse_raw_file($file);
375
376 printf "\nTotal number of results from scan (incl dmesg): %d\n", $total;
377
378 if (!$suppress_dmesg) {
379 print_dmesg($dmesg);
380 }
381
382 if ($squash_by_filename) {
383 squash_by($files, 'filename');
384 }
385
386 if ($squash_by_path) {
387 squash_by($paths, 'path');
388 }
389}
390
391sub dump_raw_output
392{
393 my ($file) = @_;
394
395 open (my $fh, '<', $file) or die "$0: $file: $!\n";
396 while (<$fh>) {
397 if ($suppress_dmesg) {
398 if ("dmesg:" eq substr($_, 0, 6)) {
399 next;
400 }
401 }
402 print $_;
403 }
404 close $fh;
405}
406
407sub parse_raw_file
408{
409 my ($file) = @_;
410
411 my $total = 0; # Total number of lines parsed.
412 my @dmesg; # dmesg output.
413 my %files; # Unique filenames containing leaks.
414 my %paths; # Unique paths containing leaks.
415
416 open (my $fh, '<', $file) or die "$0: $file: $!\n";
417 while (my $line = <$fh>) {
418 $total++;
419
420 if ("dmesg:" eq substr($line, 0, 6)) {
421 push @dmesg, $line;
422 next;
423 }
424
425 cache_path(\%paths, $line);
426 cache_filename(\%files, $line);
427 }
428
429 return $total, \@dmesg, \%paths, \%files;
430}
431
432sub print_dmesg
433{
434 my ($dmesg) = @_;
435
436 print "\ndmesg output:\n";
437
438 if (@$dmesg == 0) {
439 print "<no results>\n";
440 return;
441 }
442
443 foreach(@$dmesg) {
444 my $index = index($_, ': ');
445 $index += 2; # skid ': '
446 print substr($_, $index);
447 }
448}
449
450sub squash_by
451{
452 my ($ref, $desc) = @_;
453
454 print "\nResults squashed by $desc (excl dmesg). ";
455 print "Displaying [<number of results> <$desc>], <example result>\n";
456
457 if (keys %$ref == 0) {
458 print "<no results>\n";
459 return;
460 }
461
462 foreach(keys %$ref) {
463 my $lines = $ref->{$_};
464 my $length = @$lines;
465 printf "[%d %s] %s", $length, $_, @$lines[0];
466 }
467}
468
469sub cache_path
470{
471 my ($paths, $line) = @_;
472
473 my $index = index($line, ': ');
474 my $path = substr($line, 0, $index);
475
476 $index += 2; # skip ': '
477 add_to_cache($paths, $path, substr($line, $index));
478}
479
480sub cache_filename
481{
482 my ($files, $line) = @_;
483
484 my $index = index($line, ': ');
485 my $path = substr($line, 0, $index);
486 my $filename = basename($path);
487
488 $index += 2; # skip ': '
489 add_to_cache($files, $filename, substr($line, $index));
490}
491
492sub add_to_cache
493{
494 my ($cache, $key, $value) = @_;
495
496 if (!$cache->{$key}) {
497 $cache->{$key} = ();
498 }
499 push @{$cache->{$key}}, $value;
500}