blob: db6f39df879f0015028d4906753b479de340ce6c [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
Tobin C. Harding472c9e12018-02-27 15:02:57 +110013#
14# When the system is idle it is likely that most files under /proc/PID will be
15# identical for various processes. Scanning _all_ the PIDs under /proc is
16# unnecessary and implies that we are thoroughly scanning /proc. This is _not_
17# the case because there may be ways userspace can trigger creation of /proc
18# files that leak addresses but were not present during a scan. For these two
19# reasons we exclude all PID directories under /proc except '1/'
20
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110021use warnings;
22use strict;
23use POSIX;
24use File::Basename;
25use File::Spec;
26use Cwd 'abs_path';
27use Term::ANSIColor qw(:constants);
28use Getopt::Long qw(:config no_auto_abbrev);
Tobin C. Harding62139c12017-11-09 15:19:40 +110029use Config;
Tobin C. Harding87e37582017-12-07 12:33:21 +110030use bigint qw/hex/;
Tobin C. Harding2f042c92017-12-07 14:40:29 +110031use feature 'state';
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110032
33my $P = $0;
34my $V = '0.01';
35
36# Directories to scan.
37my @DIRS = ('/proc', '/sys');
38
Tobin C. Hardingdd98c252017-11-09 15:37:06 +110039# Timer for parsing each file, in seconds.
40my $TIMEOUT = 10;
41
Tobin C. Harding1410fe42018-01-29 15:00:16 +110042# Kernel addresses vary by architecture. We can only auto-detect the following
43# architectures (using `uname -m`). (flag --32-bit overrides auto-detection.)
44my @SUPPORTED_ARCHITECTURES = ('x86_64', 'ppc64', 'x86');
Tobin C. Harding62139c12017-11-09 15:19:40 +110045
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110046# Command line options.
47my $help = 0;
48my $debug = 0;
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +110049my $raw = 0;
50my $output_raw = ""; # Write raw results to file.
51my $input_raw = ""; # Read raw results from file instead of scanning.
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +110052my $suppress_dmesg = 0; # Don't show dmesg in output.
53my $squash_by_path = 0; # Summary report grouped by absolute path.
54my $squash_by_filename = 0; # Summary report grouped by filename.
Tobin C. Hardingf9d2a422017-12-07 13:53:41 +110055my $kernel_config_file = ""; # Kernel configuration file.
Tobin C. Harding1410fe42018-01-29 15:00:16 +110056my $opt_32bit = 0; # Scan 32-bit kernel.
57my $page_offset_32bit = 0; # Page offset for 32-bit kernel.
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110058
Tobin C. Hardingb401f562018-02-19 11:03:37 +110059# Skip these absolute paths.
60my @skip_abs = (
61 '/proc/kmsg',
62 '/proc/device-tree',
Tobin C. Harding2ad74292018-02-27 14:14:24 +110063 '/proc/1/syscall',
Tobin C. Hardingb401f562018-02-19 11:03:37 +110064 '/sys/firmware/devicetree',
65 '/sys/kernel/debug/tracing/trace_pipe',
66 '/sys/kernel/security/apparmor/revision');
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110067
Tobin C. Hardingb401f562018-02-19 11:03:37 +110068# Skip these under any subdirectory.
69my @skip_any = (
70 'pagemap',
71 'events',
72 'access',
73 'registers',
74 'snapshot_raw',
75 'trace_pipe_raw',
76 'ptmx',
77 'trace_pipe',
78 'fd',
79 'usbmon');
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110080
81sub help
82{
83 my ($exitcode) = @_;
84
85 print << "EOM";
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +110086
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110087Usage: $P [OPTIONS]
88Version: $V
89
90Options:
91
Tobin C. Harding15d60a32017-12-07 13:57:53 +110092 -o, --output-raw=<file> Save results for future processing.
93 -i, --input-raw=<file> Read results from file instead of scanning.
94 --raw Show raw results (default).
95 --suppress-dmesg Do not show dmesg results.
96 --squash-by-path Show one result per unique path.
97 --squash-by-filename Show one result per unique filename.
Tobin C. Hardingf9d2a422017-12-07 13:53:41 +110098 --kernel-config-file=<file> Kernel configuration file (e.g /boot/config)
Tobin C. Harding1410fe42018-01-29 15:00:16 +110099 --32-bit Scan 32-bit kernel.
100 --page-offset-32-bit=o Page offset (for 32-bit kernel 0xABCD1234).
Tobin C. Harding15d60a32017-12-07 13:57:53 +1100101 -d, --debug Display debugging output.
102 -h, --help, --version Display this help and exit.
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +1100103
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100104Scans the running kernel for potential leaking addresses.
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100105
106EOM
107 exit($exitcode);
108}
109
110GetOptions(
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100111 'd|debug' => \$debug,
112 'h|help' => \$help,
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +1100113 'version' => \$help,
114 'o|output-raw=s' => \$output_raw,
115 'i|input-raw=s' => \$input_raw,
116 'suppress-dmesg' => \$suppress_dmesg,
117 'squash-by-path' => \$squash_by_path,
118 'squash-by-filename' => \$squash_by_filename,
119 'raw' => \$raw,
Tobin C. Hardingf9d2a422017-12-07 13:53:41 +1100120 'kernel-config-file=s' => \$kernel_config_file,
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100121 '32-bit' => \$opt_32bit,
122 'page-offset-32-bit=o' => \$page_offset_32bit,
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100123) or help(1);
124
125help(0) if ($help);
126
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +1100127if ($input_raw) {
128 format_output($input_raw);
129 exit(0);
130}
131
132if (!$input_raw and ($squash_by_path or $squash_by_filename)) {
133 printf "\nSummary reporting only available with --input-raw=<file>\n";
134 printf "(First run scan with --output-raw=<file>.)\n";
135 exit(128);
136}
137
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100138if (!(is_supported_architecture() or $opt_32bit or $page_offset_32bit)) {
Tobin C. Harding62139c12017-11-09 15:19:40 +1100139 printf "\nScript does not support your architecture, sorry.\n";
140 printf "\nCurrently we support: \n\n";
141 foreach(@SUPPORTED_ARCHITECTURES) {
142 printf "\t%s\n", $_;
143 }
Tobin C. Harding6efb7452018-01-06 09:24:49 +1100144 printf("\n");
Tobin C. Harding62139c12017-11-09 15:19:40 +1100145
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100146 printf("If you are running a 32-bit architecture you may use:\n");
147 printf("\n\t--32-bit or --page-offset-32-bit=<page offset>\n\n");
148
Tobin C. Harding6efb7452018-01-06 09:24:49 +1100149 my $archname = `uname -m`;
150 printf("Machine hardware name (`uname -m`): %s\n", $archname);
Tobin C. Harding62139c12017-11-09 15:19:40 +1100151
152 exit(129);
153}
154
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +1100155if ($output_raw) {
156 open my $fh, '>', $output_raw or die "$0: $output_raw: $!\n";
157 select $fh;
158}
159
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100160parse_dmesg();
161walk(@DIRS);
162
163exit 0;
164
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100165sub dprint
166{
167 printf(STDERR @_) if $debug;
168}
169
Tobin C. Harding62139c12017-11-09 15:19:40 +1100170sub is_supported_architecture
171{
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100172 return (is_x86_64() or is_ppc64() or is_ix86_32());
173}
174
175sub is_32bit
176{
177 # Allow --32-bit or --page-offset-32-bit to override
178 if ($opt_32bit or $page_offset_32bit) {
179 return 1;
180 }
181
182 return is_ix86_32();
183}
184
185sub is_ix86_32
186{
Tobin C. Harding5e4bac32018-02-19 13:23:44 +1100187 state $arch = `uname -m`;
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100188
189 chomp $arch;
190 if ($arch =~ m/i[3456]86/) {
191 return 1;
192 }
193 return 0;
Tobin C. Harding62139c12017-11-09 15:19:40 +1100194}
195
Tobin C. Harding5eb0da02018-01-29 14:33:49 +1100196sub is_arch
197{
198 my ($desc) = @_;
199 my $arch = `uname -m`;
200
201 chomp $arch;
202 if ($arch eq $desc) {
203 return 1;
204 }
205 return 0;
206}
207
Tobin C. Harding62139c12017-11-09 15:19:40 +1100208sub is_x86_64
209{
Tobin C. Harding5e4bac32018-02-19 13:23:44 +1100210 state $is = is_arch('x86_64');
211 return $is;
Tobin C. Harding62139c12017-11-09 15:19:40 +1100212}
213
214sub is_ppc64
215{
Tobin C. Harding5e4bac32018-02-19 13:23:44 +1100216 state $is = is_arch('ppc64');
217 return $is;
Tobin C. Harding62139c12017-11-09 15:19:40 +1100218}
219
Tobin C. Hardingf9d2a422017-12-07 13:53:41 +1100220# Gets config option value from kernel config file.
221# Returns "" on error or if config option not found.
222sub get_kernel_config_option
223{
224 my ($option) = @_;
225 my $value = "";
226 my $tmp_file = "";
227 my @config_files;
228
229 # Allow --kernel-config-file to override.
230 if ($kernel_config_file ne "") {
231 @config_files = ($kernel_config_file);
232 } elsif (-R "/proc/config.gz") {
233 my $tmp_file = "/tmp/tmpkconf";
234
235 if (system("gunzip < /proc/config.gz > $tmp_file")) {
236 dprint "$0: system(gunzip < /proc/config.gz) failed\n";
237 return "";
238 } else {
239 @config_files = ($tmp_file);
240 }
241 } else {
242 my $file = '/boot/config-' . `uname -r`;
243 chomp $file;
244 @config_files = ($file, '/boot/config');
245 }
246
247 foreach my $file (@config_files) {
248 dprint("parsing config file: %s\n", $file);
249 $value = option_from_file($option, $file);
250 if ($value ne "") {
251 last;
252 }
253 }
254
255 if ($tmp_file ne "") {
256 system("rm -f $tmp_file");
257 }
258
259 return $value;
260}
261
262# Parses $file and returns kernel configuration option value.
263sub option_from_file
264{
265 my ($option, $file) = @_;
266 my $str = "";
267 my $val = "";
268
269 open(my $fh, "<", $file) or return "";
270 while (my $line = <$fh> ) {
271 if ($line =~ /^$option/) {
272 ($str, $val) = split /=/, $line;
273 chomp $val;
274 last;
275 }
276 }
277
278 close $fh;
279 return $val;
280}
281
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100282sub is_false_positive
283{
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100284 my ($match) = @_;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100285
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100286 if (is_32bit()) {
287 return is_false_positive_32bit($match);
288 }
289
290 # 64 bit false positives.
291
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100292 if ($match =~ '\b(0x)?(f|F){16}\b' or
293 $match =~ '\b(0x)?0{16}\b') {
294 return 1;
295 }
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100296
Tobin C. Harding87e37582017-12-07 12:33:21 +1100297 if (is_x86_64() and is_in_vsyscall_memory_region($match)) {
298 return 1;
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100299 }
300
301 return 0;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100302}
303
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100304sub is_false_positive_32bit
305{
306 my ($match) = @_;
307 state $page_offset = get_page_offset();
308
309 if ($match =~ '\b(0x)?(f|F){8}\b') {
310 return 1;
311 }
312
313 if (hex($match) < $page_offset) {
314 return 1;
315 }
316
317 return 0;
318}
319
320# returns integer value
321sub get_page_offset
322{
323 my $page_offset;
324 my $default_offset = 0xc0000000;
325
326 # Allow --page-offset-32bit to override.
327 if ($page_offset_32bit != 0) {
328 return $page_offset_32bit;
329 }
330
331 $page_offset = get_kernel_config_option('CONFIG_PAGE_OFFSET');
332 if (!$page_offset) {
333 return $default_offset;
334 }
335 return $page_offset;
336}
337
Tobin C. Harding87e37582017-12-07 12:33:21 +1100338sub is_in_vsyscall_memory_region
339{
340 my ($match) = @_;
341
342 my $hex = hex($match);
343 my $region_min = hex("0xffffffffff600000");
344 my $region_max = hex("0xffffffffff601000");
345
346 return ($hex >= $region_min and $hex <= $region_max);
347}
348
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100349# True if argument potentially contains a kernel address.
350sub may_leak_address
351{
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100352 my ($line) = @_;
Tobin C. Harding62139c12017-11-09 15:19:40 +1100353 my $address_re;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100354
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100355 # Signal masks.
356 if ($line =~ '^SigBlk:' or
Tobin C. Hardinga11949e2017-11-14 09:25:11 +1100357 $line =~ '^SigIgn:' or
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100358 $line =~ '^SigCgt:') {
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100359 return 0;
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100360 }
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100361
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100362 if ($line =~ '\bKEY=[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b' or
363 $line =~ '\b[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b') {
364 return 0;
365 }
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100366
Tobin C. Harding2f042c92017-12-07 14:40:29 +1100367 $address_re = get_address_re();
Tobin C. Harding62139c12017-11-09 15:19:40 +1100368 while (/($address_re)/g) {
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100369 if (!is_false_positive($1)) {
370 return 1;
371 }
372 }
373
374 return 0;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100375}
376
Tobin C. Harding2f042c92017-12-07 14:40:29 +1100377sub get_address_re
378{
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100379 if (is_ppc64()) {
Tobin C. Harding2f042c92017-12-07 14:40:29 +1100380 return '\b(0x)?[89abcdef]00[[:xdigit:]]{13}\b';
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100381 } elsif (is_32bit()) {
382 return '\b(0x)?[[:xdigit:]]{8}\b';
Tobin C. Harding2f042c92017-12-07 14:40:29 +1100383 }
Tobin C. Harding1410fe42018-01-29 15:00:16 +1100384
385 return get_x86_64_re();
Tobin C. Harding2f042c92017-12-07 14:40:29 +1100386}
387
388sub get_x86_64_re
389{
390 # We handle page table levels but only if explicitly configured using
391 # CONFIG_PGTABLE_LEVELS. If config file parsing fails or config option
392 # is not found we default to using address regular expression suitable
393 # for 4 page table levels.
394 state $ptl = get_kernel_config_option('CONFIG_PGTABLE_LEVELS');
395
396 if ($ptl == 5) {
397 return '\b(0x)?ff[[:xdigit:]]{14}\b';
398 }
399 return '\b(0x)?ffff[[:xdigit:]]{12}\b';
400}
401
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100402sub parse_dmesg
403{
404 open my $cmd, '-|', 'dmesg';
405 while (<$cmd>) {
406 if (may_leak_address($_)) {
407 print 'dmesg: ' . $_;
408 }
409 }
410 close $cmd;
411}
412
413# True if we should skip this path.
414sub skip
415{
Tobin C. Hardingb401f562018-02-19 11:03:37 +1100416 my ($path) = @_;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100417
Tobin C. Hardingb401f562018-02-19 11:03:37 +1100418 foreach (@skip_abs) {
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100419 return 1 if (/^$path$/);
420 }
421
422 my($filename, $dirs, $suffix) = fileparse($path);
Tobin C. Hardingb401f562018-02-19 11:03:37 +1100423 foreach (@skip_any) {
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100424 return 1 if (/^$filename$/);
425 }
426
427 return 0;
428}
429
Tobin C. Hardingdd98c252017-11-09 15:37:06 +1100430sub timed_parse_file
431{
432 my ($file) = @_;
433
434 eval {
435 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required.
436 alarm $TIMEOUT;
437 parse_file($file);
438 alarm 0;
439 };
440
441 if ($@) {
442 die unless $@ eq "alarm\n"; # Propagate unexpected errors.
443 printf STDERR "timed out parsing: %s\n", $file;
444 }
445}
446
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100447sub parse_file
448{
449 my ($file) = @_;
450
451 if (! -R $file) {
452 return;
453 }
454
Tobin C. Hardinge2858ca2018-02-19 10:22:15 +1100455 if (! -T $file) {
456 return;
457 }
458
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100459 open my $fh, "<", $file or return;
460 while ( <$fh> ) {
461 if (may_leak_address($_)) {
462 print $file . ': ' . $_;
463 }
464 }
465 close $fh;
466}
467
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100468# Recursively walk directory tree.
469sub walk
470{
471 my @dirs = @_;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100472
473 while (my $pwd = shift @dirs) {
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100474 next if (!opendir(DIR, $pwd));
475 my @files = readdir(DIR);
476 closedir(DIR);
477
478 foreach my $file (@files) {
479 next if ($file eq '.' or $file eq '..');
480
481 my $path = "$pwd/$file";
482 next if (-l $path);
483
Tobin C. Harding472c9e12018-02-27 15:02:57 +1100484 # skip /proc/PID except /proc/1
485 next if (($path =~ /^\/proc\/[0-9]+$/) &&
486 ($path !~ /^\/proc\/1$/));
487
Tobin C. Hardingb401f562018-02-19 11:03:37 +1100488 next if (skip($path));
489
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100490 if (-d $path) {
491 push @dirs, $path;
Tobin C. Hardingb401f562018-02-19 11:03:37 +1100492 next;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100493 }
Tobin C. Hardingb401f562018-02-19 11:03:37 +1100494
495 dprint "parsing: $path\n";
496 timed_parse_file($path);
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100497 }
498 }
499}
Tobin C. Hardingd09bd8d2017-11-09 15:07:15 +1100500
501sub format_output
502{
503 my ($file) = @_;
504
505 # Default is to show raw results.
506 if ($raw or (!$squash_by_path and !$squash_by_filename)) {
507 dump_raw_output($file);
508 return;
509 }
510
511 my ($total, $dmesg, $paths, $files) = parse_raw_file($file);
512
513 printf "\nTotal number of results from scan (incl dmesg): %d\n", $total;
514
515 if (!$suppress_dmesg) {
516 print_dmesg($dmesg);
517 }
518
519 if ($squash_by_filename) {
520 squash_by($files, 'filename');
521 }
522
523 if ($squash_by_path) {
524 squash_by($paths, 'path');
525 }
526}
527
528sub dump_raw_output
529{
530 my ($file) = @_;
531
532 open (my $fh, '<', $file) or die "$0: $file: $!\n";
533 while (<$fh>) {
534 if ($suppress_dmesg) {
535 if ("dmesg:" eq substr($_, 0, 6)) {
536 next;
537 }
538 }
539 print $_;
540 }
541 close $fh;
542}
543
544sub parse_raw_file
545{
546 my ($file) = @_;
547
548 my $total = 0; # Total number of lines parsed.
549 my @dmesg; # dmesg output.
550 my %files; # Unique filenames containing leaks.
551 my %paths; # Unique paths containing leaks.
552
553 open (my $fh, '<', $file) or die "$0: $file: $!\n";
554 while (my $line = <$fh>) {
555 $total++;
556
557 if ("dmesg:" eq substr($line, 0, 6)) {
558 push @dmesg, $line;
559 next;
560 }
561
562 cache_path(\%paths, $line);
563 cache_filename(\%files, $line);
564 }
565
566 return $total, \@dmesg, \%paths, \%files;
567}
568
569sub print_dmesg
570{
571 my ($dmesg) = @_;
572
573 print "\ndmesg output:\n";
574
575 if (@$dmesg == 0) {
576 print "<no results>\n";
577 return;
578 }
579
580 foreach(@$dmesg) {
581 my $index = index($_, ': ');
582 $index += 2; # skid ': '
583 print substr($_, $index);
584 }
585}
586
587sub squash_by
588{
589 my ($ref, $desc) = @_;
590
591 print "\nResults squashed by $desc (excl dmesg). ";
592 print "Displaying [<number of results> <$desc>], <example result>\n";
593
594 if (keys %$ref == 0) {
595 print "<no results>\n";
596 return;
597 }
598
599 foreach(keys %$ref) {
600 my $lines = $ref->{$_};
601 my $length = @$lines;
602 printf "[%d %s] %s", $length, $_, @$lines[0];
603 }
604}
605
606sub cache_path
607{
608 my ($paths, $line) = @_;
609
610 my $index = index($line, ': ');
611 my $path = substr($line, 0, $index);
612
613 $index += 2; # skip ': '
614 add_to_cache($paths, $path, substr($line, $index));
615}
616
617sub cache_filename
618{
619 my ($files, $line) = @_;
620
621 my $index = index($line, ': ');
622 my $path = substr($line, 0, $index);
623 my $filename = basename($path);
624
625 $index += 2; # skip ': '
626 add_to_cache($files, $filename, substr($line, $index));
627}
628
629sub add_to_cache
630{
631 my ($cache, $key, $value) = @_;
632
633 if (!$cache->{$key}) {
634 $cache->{$key} = ();
635 }
636 push @{$cache->{$key}}, $value;
637}