blob: 4610ad3c80c2121ad7dbf6f8f649f995dc9959c7 [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#
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#
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.
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);
24
25my $P = $0;
26my $V = '0.01';
27
28# Directories to scan.
29my @DIRS = ('/proc', '/sys');
30
31# Command line options.
32my $help = 0;
33my $debug = 0;
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +110034my $raw = 0;
35my $output_raw = ""; # Write raw results to file.
36my $input_raw = ""; # Read raw results from file instead of scanning.
37
38my $suppress_dmesg = 0; # Don't show dmesg in output.
39my $squash_by_path = 0; # Summary report grouped by absolute path.
40my $squash_by_filename = 0; # Summary report grouped by filename.
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110041
42# Do not parse these files (absolute path).
43my @skip_parse_files_abs = ('/proc/kmsg',
44 '/proc/kcore',
45 '/proc/fs/ext4/sdb1/mb_groups',
46 '/proc/1/fd/3',
Tobin C. Harding1c1e3be2017-11-09 14:02:41 +110047 '/sys/firmware/devicetree',
48 '/proc/device-tree',
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110049 '/sys/kernel/debug/tracing/trace_pipe',
50 '/sys/kernel/security/apparmor/revision');
51
Tobin C. Hardinga2847332017-11-09 13:28:43 +110052# Do not parse these files under any subdirectory.
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110053my @skip_parse_files_any = ('0',
54 '1',
55 '2',
56 'pagemap',
57 'events',
58 'access',
59 'registers',
60 'snapshot_raw',
61 'trace_pipe_raw',
62 'ptmx',
63 'trace_pipe');
64
65# Do not walk these directories (absolute path).
66my @skip_walk_dirs_abs = ();
67
68# Do not walk these directories under any subdirectory.
69my @skip_walk_dirs_any = ('self',
70 'thread-self',
71 'cwd',
72 'fd',
Tobin C. Harding1c1e3be2017-11-09 14:02:41 +110073 'usbmon',
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110074 'stderr',
75 'stdin',
76 'stdout');
77
78sub help
79{
80 my ($exitcode) = @_;
81
82 print << "EOM";
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +110083
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110084Usage: $P [OPTIONS]
85Version: $V
86
87Options:
88
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +110089 -o, --output-raw=<file> Save results for future processing.
90 -i, --input-raw=<file> Read results from file instead of scanning.
91 --raw Show raw results (default).
92 --suppress-dmesg Do not show dmesg results.
93 --squash-by-path Show one result per unique path.
94 --squash-by-filename Show one result per unique filename.
95 -d, --debug Display debugging output.
96 -h, --help, --version Display this help and exit.
97
98Examples:
99
100 # Scan kernel and dump raw results.
101 $0
102
103 # Scan kernel and save results to file.
104 $0 --output-raw scan.out
105
106 # View summary report.
107 $0 --input-raw scan.out --squash-by-filename
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100108
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100109Scans the running (64 bit) kernel for potential leaking addresses.
110
111EOM
112 exit($exitcode);
113}
114
115GetOptions(
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100116 'd|debug' => \$debug,
117 'h|help' => \$help,
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +1100118 'version' => \$help,
119 'o|output-raw=s' => \$output_raw,
120 'i|input-raw=s' => \$input_raw,
121 'suppress-dmesg' => \$suppress_dmesg,
122 'squash-by-path' => \$squash_by_path,
123 'squash-by-filename' => \$squash_by_filename,
124 'raw' => \$raw,
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100125) or help(1);
126
127help(0) if ($help);
128
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +1100129if ($input_raw) {
130 format_output($input_raw);
131 exit(0);
132}
133
134if (!$input_raw and ($squash_by_path or $squash_by_filename)) {
135 printf "\nSummary reporting only available with --input-raw=<file>\n";
136 printf "(First run scan with --output-raw=<file>.)\n";
137 exit(128);
138}
139
140if ($output_raw) {
141 open my $fh, '>', $output_raw or die "$0: $output_raw: $!\n";
142 select $fh;
143}
144
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100145parse_dmesg();
146walk(@DIRS);
147
148exit 0;
149
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100150sub dprint
151{
152 printf(STDERR @_) if $debug;
153}
154
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100155sub is_false_positive
156{
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100157 my ($match) = @_;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100158
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100159 if ($match =~ '\b(0x)?(f|F){16}\b' or
160 $match =~ '\b(0x)?0{16}\b') {
161 return 1;
162 }
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100163
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100164
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100165 if ($match =~ '\bf{10}600000\b' or# vsyscall memory region, we should probably check against a range here.
166 $match =~ '\bf{10}601000\b') {
167 return 1;
168 }
169
170 return 0;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100171}
172
173# True if argument potentially contains a kernel address.
174sub may_leak_address
175{
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100176 my ($line) = @_;
177 my $address = '\b(0x)?ffff[[:xdigit:]]{12}\b';
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100178
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100179 # Signal masks.
180 if ($line =~ '^SigBlk:' or
181 $line =~ '^SigCgt:') {
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100182 return 0;
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100183 }
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100184
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100185 if ($line =~ '\bKEY=[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b' or
186 $line =~ '\b[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b') {
187 return 0;
188 }
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100189
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100190 while (/($address)/g) {
191 if (!is_false_positive($1)) {
192 return 1;
193 }
194 }
195
196 return 0;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100197}
198
199sub parse_dmesg
200{
201 open my $cmd, '-|', 'dmesg';
202 while (<$cmd>) {
203 if (may_leak_address($_)) {
204 print 'dmesg: ' . $_;
205 }
206 }
207 close $cmd;
208}
209
210# True if we should skip this path.
211sub skip
212{
213 my ($path, $paths_abs, $paths_any) = @_;
214
215 foreach (@$paths_abs) {
216 return 1 if (/^$path$/);
217 }
218
219 my($filename, $dirs, $suffix) = fileparse($path);
220 foreach (@$paths_any) {
221 return 1 if (/^$filename$/);
222 }
223
224 return 0;
225}
226
227sub skip_parse
228{
229 my ($path) = @_;
230 return skip($path, \@skip_parse_files_abs, \@skip_parse_files_any);
231}
232
233sub parse_file
234{
235 my ($file) = @_;
236
237 if (! -R $file) {
238 return;
239 }
240
241 if (skip_parse($file)) {
242 dprint "skipping file: $file\n";
243 return;
244 }
245 dprint "parsing: $file\n";
246
247 open my $fh, "<", $file or return;
248 while ( <$fh> ) {
249 if (may_leak_address($_)) {
250 print $file . ': ' . $_;
251 }
252 }
253 close $fh;
254}
255
256
257# True if we should skip walking this directory.
258sub skip_walk
259{
260 my ($path) = @_;
261 return skip($path, \@skip_walk_dirs_abs, \@skip_walk_dirs_any)
262}
263
264# Recursively walk directory tree.
265sub walk
266{
267 my @dirs = @_;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100268
269 while (my $pwd = shift @dirs) {
270 next if (skip_walk($pwd));
271 next if (!opendir(DIR, $pwd));
272 my @files = readdir(DIR);
273 closedir(DIR);
274
275 foreach my $file (@files) {
276 next if ($file eq '.' or $file eq '..');
277
278 my $path = "$pwd/$file";
279 next if (-l $path);
280
281 if (-d $path) {
282 push @dirs, $path;
283 } else {
284 parse_file($path);
285 }
286 }
287 }
288}
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +1100289
290sub format_output
291{
292 my ($file) = @_;
293
294 # Default is to show raw results.
295 if ($raw or (!$squash_by_path and !$squash_by_filename)) {
296 dump_raw_output($file);
297 return;
298 }
299
300 my ($total, $dmesg, $paths, $files) = parse_raw_file($file);
301
302 printf "\nTotal number of results from scan (incl dmesg): %d\n", $total;
303
304 if (!$suppress_dmesg) {
305 print_dmesg($dmesg);
306 }
307
308 if ($squash_by_filename) {
309 squash_by($files, 'filename');
310 }
311
312 if ($squash_by_path) {
313 squash_by($paths, 'path');
314 }
315}
316
317sub dump_raw_output
318{
319 my ($file) = @_;
320
321 open (my $fh, '<', $file) or die "$0: $file: $!\n";
322 while (<$fh>) {
323 if ($suppress_dmesg) {
324 if ("dmesg:" eq substr($_, 0, 6)) {
325 next;
326 }
327 }
328 print $_;
329 }
330 close $fh;
331}
332
333sub parse_raw_file
334{
335 my ($file) = @_;
336
337 my $total = 0; # Total number of lines parsed.
338 my @dmesg; # dmesg output.
339 my %files; # Unique filenames containing leaks.
340 my %paths; # Unique paths containing leaks.
341
342 open (my $fh, '<', $file) or die "$0: $file: $!\n";
343 while (my $line = <$fh>) {
344 $total++;
345
346 if ("dmesg:" eq substr($line, 0, 6)) {
347 push @dmesg, $line;
348 next;
349 }
350
351 cache_path(\%paths, $line);
352 cache_filename(\%files, $line);
353 }
354
355 return $total, \@dmesg, \%paths, \%files;
356}
357
358sub print_dmesg
359{
360 my ($dmesg) = @_;
361
362 print "\ndmesg output:\n";
363
364 if (@$dmesg == 0) {
365 print "<no results>\n";
366 return;
367 }
368
369 foreach(@$dmesg) {
370 my $index = index($_, ': ');
371 $index += 2; # skid ': '
372 print substr($_, $index);
373 }
374}
375
376sub squash_by
377{
378 my ($ref, $desc) = @_;
379
380 print "\nResults squashed by $desc (excl dmesg). ";
381 print "Displaying [<number of results> <$desc>], <example result>\n";
382
383 if (keys %$ref == 0) {
384 print "<no results>\n";
385 return;
386 }
387
388 foreach(keys %$ref) {
389 my $lines = $ref->{$_};
390 my $length = @$lines;
391 printf "[%d %s] %s", $length, $_, @$lines[0];
392 }
393}
394
395sub cache_path
396{
397 my ($paths, $line) = @_;
398
399 my $index = index($line, ': ');
400 my $path = substr($line, 0, $index);
401
402 $index += 2; # skip ': '
403 add_to_cache($paths, $path, substr($line, $index));
404}
405
406sub cache_filename
407{
408 my ($files, $line) = @_;
409
410 my $index = index($line, ': ');
411 my $path = substr($line, 0, $index);
412 my $filename = basename($path);
413
414 $index += 2; # skip ': '
415 add_to_cache($files, $filename, substr($line, $index));
416}
417
418sub add_to_cache
419{
420 my ($cache, $key, $value) = @_;
421
422 if (!$cache->{$key}) {
423 $cache->{$key} = ();
424 }
425 push @{$cache->{$key}}, $value;
426}