Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 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 | # |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 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 | |
| 16 | use warnings; |
| 17 | use strict; |
| 18 | use POSIX; |
| 19 | use File::Basename; |
| 20 | use File::Spec; |
| 21 | use Cwd 'abs_path'; |
| 22 | use Term::ANSIColor qw(:constants); |
| 23 | use Getopt::Long qw(:config no_auto_abbrev); |
| 24 | |
| 25 | my $P = $0; |
| 26 | my $V = '0.01'; |
| 27 | |
| 28 | # Directories to scan. |
| 29 | my @DIRS = ('/proc', '/sys'); |
| 30 | |
| 31 | # Command line options. |
| 32 | my $help = 0; |
| 33 | my $debug = 0; |
Tobin C. Harding | d09bd8d | 2017-11-09 15:07:15 +1100 | [diff] [blame^] | 34 | my $raw = 0; |
| 35 | my $output_raw = ""; # Write raw results to file. |
| 36 | my $input_raw = ""; # Read raw results from file instead of scanning. |
| 37 | |
| 38 | my $suppress_dmesg = 0; # Don't show dmesg in output. |
| 39 | my $squash_by_path = 0; # Summary report grouped by absolute path. |
| 40 | my $squash_by_filename = 0; # Summary report grouped by filename. |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 41 | |
| 42 | # Do not parse these files (absolute path). |
| 43 | my @skip_parse_files_abs = ('/proc/kmsg', |
| 44 | '/proc/kcore', |
| 45 | '/proc/fs/ext4/sdb1/mb_groups', |
| 46 | '/proc/1/fd/3', |
Tobin C. Harding | 1c1e3be | 2017-11-09 14:02:41 +1100 | [diff] [blame] | 47 | '/sys/firmware/devicetree', |
| 48 | '/proc/device-tree', |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 49 | '/sys/kernel/debug/tracing/trace_pipe', |
| 50 | '/sys/kernel/security/apparmor/revision'); |
| 51 | |
Tobin C. Harding | a284733 | 2017-11-09 13:28:43 +1100 | [diff] [blame] | 52 | # Do not parse these files under any subdirectory. |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 53 | my @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). |
| 66 | my @skip_walk_dirs_abs = (); |
| 67 | |
| 68 | # Do not walk these directories under any subdirectory. |
| 69 | my @skip_walk_dirs_any = ('self', |
| 70 | 'thread-self', |
| 71 | 'cwd', |
| 72 | 'fd', |
Tobin C. Harding | 1c1e3be | 2017-11-09 14:02:41 +1100 | [diff] [blame] | 73 | 'usbmon', |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 74 | 'stderr', |
| 75 | 'stdin', |
| 76 | 'stdout'); |
| 77 | |
| 78 | sub help |
| 79 | { |
| 80 | my ($exitcode) = @_; |
| 81 | |
| 82 | print << "EOM"; |
Tobin C. Harding | d09bd8d | 2017-11-09 15:07:15 +1100 | [diff] [blame^] | 83 | |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 84 | Usage: $P [OPTIONS] |
| 85 | Version: $V |
| 86 | |
| 87 | Options: |
| 88 | |
Tobin C. Harding | d09bd8d | 2017-11-09 15:07:15 +1100 | [diff] [blame^] | 89 | -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 | |
| 98 | Examples: |
| 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. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 108 | |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 109 | Scans the running (64 bit) kernel for potential leaking addresses. |
| 110 | |
| 111 | EOM |
| 112 | exit($exitcode); |
| 113 | } |
| 114 | |
| 115 | GetOptions( |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 116 | 'd|debug' => \$debug, |
| 117 | 'h|help' => \$help, |
Tobin C. Harding | d09bd8d | 2017-11-09 15:07:15 +1100 | [diff] [blame^] | 118 | '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. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 125 | ) or help(1); |
| 126 | |
| 127 | help(0) if ($help); |
| 128 | |
Tobin C. Harding | d09bd8d | 2017-11-09 15:07:15 +1100 | [diff] [blame^] | 129 | if ($input_raw) { |
| 130 | format_output($input_raw); |
| 131 | exit(0); |
| 132 | } |
| 133 | |
| 134 | if (!$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 | |
| 140 | if ($output_raw) { |
| 141 | open my $fh, '>', $output_raw or die "$0: $output_raw: $!\n"; |
| 142 | select $fh; |
| 143 | } |
| 144 | |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 145 | parse_dmesg(); |
| 146 | walk(@DIRS); |
| 147 | |
| 148 | exit 0; |
| 149 | |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 150 | sub dprint |
| 151 | { |
| 152 | printf(STDERR @_) if $debug; |
| 153 | } |
| 154 | |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 155 | sub is_false_positive |
| 156 | { |
Tobin C. Harding | 7e5758f | 2017-11-08 11:01:59 +1100 | [diff] [blame] | 157 | my ($match) = @_; |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 158 | |
Tobin C. Harding | 7e5758f | 2017-11-08 11:01:59 +1100 | [diff] [blame] | 159 | if ($match =~ '\b(0x)?(f|F){16}\b' or |
| 160 | $match =~ '\b(0x)?0{16}\b') { |
| 161 | return 1; |
| 162 | } |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 163 | |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 164 | |
Tobin C. Harding | 7e5758f | 2017-11-08 11:01:59 +1100 | [diff] [blame] | 165 | 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. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | # True if argument potentially contains a kernel address. |
| 174 | sub may_leak_address |
| 175 | { |
Tobin C. Harding | 7e5758f | 2017-11-08 11:01:59 +1100 | [diff] [blame] | 176 | my ($line) = @_; |
| 177 | my $address = '\b(0x)?ffff[[:xdigit:]]{12}\b'; |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 178 | |
Tobin C. Harding | 7e5758f | 2017-11-08 11:01:59 +1100 | [diff] [blame] | 179 | # Signal masks. |
| 180 | if ($line =~ '^SigBlk:' or |
| 181 | $line =~ '^SigCgt:') { |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 182 | return 0; |
Tobin C. Harding | 7e5758f | 2017-11-08 11:01:59 +1100 | [diff] [blame] | 183 | } |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 184 | |
Tobin C. Harding | 7e5758f | 2017-11-08 11:01:59 +1100 | [diff] [blame] | 185 | 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. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 189 | |
Tobin C. Harding | 7e5758f | 2017-11-08 11:01:59 +1100 | [diff] [blame] | 190 | while (/($address)/g) { |
| 191 | if (!is_false_positive($1)) { |
| 192 | return 1; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return 0; |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | sub 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. |
| 211 | sub 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 | |
| 227 | sub skip_parse |
| 228 | { |
| 229 | my ($path) = @_; |
| 230 | return skip($path, \@skip_parse_files_abs, \@skip_parse_files_any); |
| 231 | } |
| 232 | |
| 233 | sub 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. |
| 258 | sub 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. |
| 265 | sub walk |
| 266 | { |
| 267 | my @dirs = @_; |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 268 | |
| 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. Harding | d09bd8d | 2017-11-09 15:07:15 +1100 | [diff] [blame^] | 289 | |
| 290 | sub 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 | |
| 317 | sub 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 | |
| 333 | sub 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 | |
| 358 | sub 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 | |
| 376 | sub 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 | |
| 395 | sub 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 | |
| 406 | sub 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 | |
| 418 | sub add_to_cache |
| 419 | { |
| 420 | my ($cache, $key, $value) = @_; |
| 421 | |
| 422 | if (!$cache->{$key}) { |
| 423 | $cache->{$key} = (); |
| 424 | } |
| 425 | push @{$cache->{$key}}, $value; |
| 426 | } |