2 # SPDX-License-Identifier: GPL-2.0-only
4 # (c) 2017 Tobin C. Harding <me@tobin.cc>
6 # leaking_addresses.pl: Scan the kernel for potential leaking addresses.
7 # - Scans dmesg output.
8 # - Walks directory tree and parses each file (for each directory in @DIRS).
10 # Use --debug to output path before parsing, this is useful to find files that
11 # cause the script to choke.
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/'
26 use File
::Temp qw
/tempfile/;
28 use Term
::ANSIColor
qw(:constants);
29 use Getopt
::Long
qw(:config no_auto_abbrev);
36 # Directories to scan.
37 my @DIRS = ('/proc', '/sys');
39 # Timer for parsing each file, in seconds.
42 # Kernel addresses vary by architecture. We can only auto-detect the following
43 # architectures (using `uname -m`). (flag --32-bit overrides auto-detection.)
44 my @SUPPORTED_ARCHITECTURES = ('x86_64', 'ppc64', 'x86');
46 # Command line options.
50 my $output_raw = ""; # Write raw results to file.
51 my $input_raw = ""; # Read raw results from file instead of scanning.
52 my $suppress_dmesg = 0; # Don't show dmesg in output.
53 my $squash_by_path = 0; # Summary report grouped by absolute path.
54 my $squash_by_filename = 0; # Summary report grouped by filename.
55 my $kallsyms_file = ""; # Kernel symbols file.
56 my $kernel_config_file = ""; # Kernel configuration file.
57 my $opt_32bit = 0; # Scan 32-bit kernel.
58 my $page_offset_32bit = 0; # Page offset for 32-bit kernel.
62 # Skip these absolute paths.
67 '/sys/firmware/devicetree',
68 '/sys/kernel/tracing/trace_pipe',
69 '/sys/kernel/debug/tracing/trace_pipe',
70 '/sys/kernel/security/apparmor/revision');
72 # Skip these under any subdirectory.
95 -o
, --output
-raw
=<file
> Save results
for future processing
.
96 -i
, --input
-raw
=<file
> Read results from file instead of scanning
.
97 --raw Show raw results
(default).
98 --suppress
-dmesg Do
not show dmesg results
.
99 --squash
-by
-path Show one result per unique path
.
100 --squash
-by
-filename Show one result per unique filename
.
101 --kernel
-config
-file
=<file
> Kernel configuration file
(e
.g
/boot/config
)
102 --kallsyms
=<file
> Read kernel symbol addresses from file
(for
103 scanning binary files
).
104 --32-bit Scan
32-bit kernel
.
105 --page
-offset
-32-bit
=o Page offset
(for 32-bit kernel
0xABCD1234).
106 -d
, --debug Display debugging output
.
107 -h
, --help Display this help
and exit.
109 Scans the running kernel
for potential leaking addresses
.
116 'd|debug' => \
$debug,
118 'o|output-raw=s' => \
$output_raw,
119 'i|input-raw=s' => \
$input_raw,
120 'suppress-dmesg' => \
$suppress_dmesg,
121 'squash-by-path' => \
$squash_by_path,
122 'squash-by-filename' => \
$squash_by_filename,
124 'kallsyms=s' => \
$kallsyms_file,
125 'kernel-config-file=s' => \
$kernel_config_file,
126 '32-bit' => \
$opt_32bit,
127 'page-offset-32-bit=o' => \
$page_offset_32bit,
133 format_output
($input_raw);
137 if (!$input_raw and ($squash_by_path or $squash_by_filename)) {
138 printf "\nSummary reporting only available with --input-raw=<file>\n";
139 printf "(First run scan with --output-raw=<file>.)\n";
143 if (!(is_supported_architecture
() or $opt_32bit or $page_offset_32bit)) {
144 printf "\nScript does not support your architecture, sorry.\n";
145 printf "\nCurrently we support: \n\n";
146 foreach(@SUPPORTED_ARCHITECTURES) {
151 printf("If you are running a 32-bit architecture you may use:\n");
152 printf("\n\t--32-bit or --page-offset-32-bit=<page offset>\n\n");
154 my $archname = `uname -m`;
155 printf("Machine hardware name (`uname -m`): %s\n", $archname);
161 open my $fh, '>', $output_raw or die "$0: $output_raw: $!\n";
165 if ($kallsyms_file) {
166 open my $fh, '<', $kallsyms_file or die "$0: $kallsyms_file: $!\n";
169 my @entry = split / /, $_;
170 my $addr_text = $entry[0];
171 if ($addr_text !~ /^0/) {
172 # TODO: Why is hex() so impossibly slow?
173 my $addr = hex($addr_text);
174 my $symbol = $entry[2];
175 # Only keep kernel text addresses.
176 my $long = pack("J", $addr);
177 my $entry = [$long, $symbol];
178 push @kallsyms, $entry;
191 printf(STDERR
@_) if $debug;
194 sub is_supported_architecture
196 return (is_x86_64
() or is_ppc64
() or is_ix86_32
());
201 # Allow --32-bit or --page-offset-32-bit to override
202 if ($opt_32bit or $page_offset_32bit) {
211 state $arch = `uname -m`;
214 if ($arch =~ m/i[3456]86/) {
223 my $arch = `uname -m`;
226 if ($arch eq $desc) {
234 state $is = is_arch
('x86_64');
240 state $is = is_arch
('ppc64');
244 # Gets config option value from kernel config file.
245 # Returns "" on error or if config option not found.
246 sub get_kernel_config_option
254 # Allow --kernel-config-file to override.
255 if ($kernel_config_file ne "") {
256 @config_files = ($kernel_config_file);
257 } elsif (-R
"/proc/config.gz") {
258 ($tmp_fh, $tmp_file) = tempfile
("config.gz-XXXXXX",
261 if (system("gunzip < /proc/config.gz > $tmp_file")) {
262 dprint
("system(gunzip < /proc/config.gz) failed\n");
265 @config_files = ($tmp_file);
268 my $file = '/boot/config-' . `uname -r`;
270 @config_files = ($file, '/boot/config');
273 foreach my $file (@config_files) {
274 dprint
("parsing config file: $file\n");
275 $value = option_from_file
($option, $file);
284 # Parses $file and returns kernel configuration option value.
287 my ($option, $file) = @_;
291 open(my $fh, "<", $file) or return "";
292 while (my $line = <$fh> ) {
293 if ($line =~ /^$option/) {
294 ($str, $val) = split /=/, $line;
304 sub is_false_positive
309 return is_false_positive_32bit
($match);
312 # Ignore 64 bit false positives:
313 # 0xfffffffffffffff[0-f]
315 if ($match =~ '\b(0x)?(f|F){15}[0-9a-f]\b' or
316 $match =~ '\b(0x)?0{16}\b') {
320 if (is_x86_64
() and is_in_vsyscall_memory_region
($match)) {
327 sub is_false_positive_32bit
330 state $page_offset = get_page_offset
();
332 if ($match =~ '\b(0x)?(f|F){7}[0-9a-f]\b') {
336 if (hex($match) < $page_offset) {
343 # returns integer value
347 my $default_offset = 0xc0000000;
349 # Allow --page-offset-32bit to override.
350 if ($page_offset_32bit != 0) {
351 return $page_offset_32bit;
354 $page_offset = get_kernel_config_option
('CONFIG_PAGE_OFFSET');
356 return $default_offset;
361 sub is_in_vsyscall_memory_region
365 my $hex = hex($match);
366 my $region_min = hex("0xffffffffff600000");
367 my $region_max = hex("0xffffffffff601000");
369 return ($hex >= $region_min and $hex <= $region_max);
372 # True if argument potentially contains a kernel address.
375 my ($path, $line) = @_;
378 # Ignore Signal masks.
379 if ($line =~ '^SigBlk:' or
380 $line =~ '^SigIgn:' or
381 $line =~ '^SigCgt:') {
385 # Ignore input device reporting.
386 # /proc/bus/input/devices: B: KEY=402000000 3803078f800d001 feffffdfffefffff fffffffffffffffe
387 # /sys/devices/platform/i8042/serio0/input/input1/uevent: KEY=402000000 3803078f800d001 feffffdfffefffff fffffffffffffffe
388 # /sys/devices/platform/i8042/serio0/input/input1/capabilities/key: 402000000 3803078f800d001 feffffdfffefffff fffffffffffffffe
389 if ($line =~ '\bKEY=[[:xdigit:]]{9,14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b' or
390 ($path =~ '\bkey$' and
391 $line =~ '\b[[:xdigit:]]{9,14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b')) {
395 $address_re = get_address_re
();
396 while ($line =~ /($address_re)/g) {
397 if (!is_false_positive
($1)) {
408 return '\b(0x)?[89abcdef]00[[:xdigit:]]{13}\b';
409 } elsif (is_32bit
()) {
410 return '\b(0x)?[[:xdigit:]]{8}\b';
413 return get_x86_64_re
();
418 # We handle page table levels but only if explicitly configured using
419 # CONFIG_PGTABLE_LEVELS. If config file parsing fails or config option
420 # is not found we default to using address regular expression suitable
421 # for 4 page table levels.
422 state $ptl = get_kernel_config_option
('CONFIG_PGTABLE_LEVELS');
425 return '\b(0x)?ff[[:xdigit:]]{14}\b';
427 return '\b(0x)?ffff[[:xdigit:]]{12}\b';
432 open my $cmd, '-|', 'dmesg';
434 if (may_leak_address
("dmesg", $_)) {
435 print 'dmesg: ' . $_;
441 # True if we should skip this path.
446 foreach (@skip_abs) {
447 return 1 if (/^$path$/);
450 my($filename, $dirs, $suffix) = fileparse
($path);
451 foreach (@skip_any) {
452 return 1 if (/^$filename$/);
463 local $SIG{ALRM
} = sub { die "alarm\n" }; # NB: \n required.
470 die unless $@
eq "alarm\n"; # Propagate unexpected errors.
471 printf STDERR
"timed out parsing: %s\n", $file;
479 open my $fh, "<:raw", $file or return;
484 foreach my $entry (@kallsyms) {
485 my $addr = $entry->[0];
486 my $symbol = $entry->[1];
487 my $offset = index($bytes, $addr);
489 printf("$file: $symbol @ $offset\n");
503 if ($file =~ m
|^/sys/kernel
/btf/| or
504 $file =~ m
|^/sys/devices
/pci
| or
505 $file =~ m
|^/sys/firmware
/efi/efivars
/| or
506 $file =~ m
|^/proc/bus
/pci/|) {
509 if (scalar @kallsyms > 0) {
515 open my $fh, "<", $file or return;
518 if (may_leak_address
($file, $_)) {
519 printf("$file: $_\n");
525 # Checks if the actual path name is leaking a kernel address.
526 sub check_path_for_leaks
530 if (may_leak_address
($path, $path)) {
531 printf("Path name may contain address: $path\n");
535 # Recursively walk directory tree.
540 while (my $pwd = shift @dirs) {
541 next if (!opendir(DIR
, $pwd));
542 my @files = readdir(DIR
);
545 foreach my $file (@files) {
546 next if ($file eq '.' or $file eq '..');
548 my $path = "$pwd/$file";
551 # skip /proc/PID except /proc/1
552 next if (($path =~ /^\/proc\
/[0-9]+$/) &&
553 ($path !~ /^\/proc\
/1$/));
555 next if (skip
($path));
557 check_path_for_leaks
($path);
564 dprint
("parsing: $path\n");
565 timed_parse_file
($path);
574 # Default is to show raw results.
575 if ($raw or (!$squash_by_path and !$squash_by_filename)) {
576 dump_raw_output
($file);
580 my ($total, $dmesg, $paths, $files) = parse_raw_file
($file);
582 printf "\nTotal number of results from scan (incl dmesg): %d\n", $total;
584 if (!$suppress_dmesg) {
588 if ($squash_by_filename) {
589 squash_by
($files, 'filename');
592 if ($squash_by_path) {
593 squash_by
($paths, 'path');
601 open (my $fh, '<', $file) or die "$0: $file: $!\n";
603 if ($suppress_dmesg) {
604 if ("dmesg:" eq substr($_, 0, 6)) {
617 my $total = 0; # Total number of lines parsed.
618 my @dmesg; # dmesg output.
619 my %files; # Unique filenames containing leaks.
620 my %paths; # Unique paths containing leaks.
622 open (my $fh, '<', $file) or die "$0: $file: $!\n";
623 while (my $line = <$fh>) {
626 if ("dmesg:" eq substr($line, 0, 6)) {
631 cache_path
(\
%paths, $line);
632 cache_filename
(\
%files, $line);
635 return $total, \
@dmesg, \
%paths, \
%files;
642 print "\ndmesg output:\n";
645 print "<no results>\n";
650 my $index = index($_, ': ');
651 $index += 2; # skid ': '
652 print substr($_, $index);
658 my ($ref, $desc) = @_;
660 print "\nResults squashed by $desc (excl dmesg). ";
661 print "Displaying [<number of results> <$desc>], <example result>\n";
663 if (keys %$ref == 0) {
664 print "<no results>\n";
668 foreach(keys %$ref) {
669 my $lines = $ref->{$_};
670 my $length = @
$lines;
671 printf "[%d %s] %s", $length, $_, @
$lines[0];
677 my ($paths, $line) = @_;
679 my $index = index($line, ': ');
680 my $path = substr($line, 0, $index);
682 $index += 2; # skip ': '
683 add_to_cache
($paths, $path, substr($line, $index));
688 my ($files, $line) = @_;
690 my $index = index($line, ': ');
691 my $path = substr($line, 0, $index);
692 my $filename = basename
($path);
694 $index += 2; # skip ': '
695 add_to_cache
($files, $filename, substr($line, $index));
700 my ($cache, $key, $value) = @_;
702 if (!$cache->{$key}) {
705 push @
{$cache->{$key}}, $value;