^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #!/usr/bin/env perl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) # SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) # (c) 2017 Tobin C. Harding <me@tobin.cc>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) # leaking_addresses.pl: Scan the kernel for potential leaking addresses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) # - Scans dmesg output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) # - Walks directory tree and parses each file (for each directory in @DIRS).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) # Use --debug to output path before parsing, this is useful to find files that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) # cause the script to choke.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) # When the system is idle it is likely that most files under /proc/PID will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) # identical for various processes. Scanning _all_ the PIDs under /proc is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) # unnecessary and implies that we are thoroughly scanning /proc. This is _not_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) # the case because there may be ways userspace can trigger creation of /proc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) # files that leak addresses but were not present during a scan. For these two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) # reasons we exclude all PID directories under /proc except '1/'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) use warnings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) use strict;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) use POSIX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) use File::Basename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) use File::Spec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) use Cwd 'abs_path';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) use Term::ANSIColor qw(:constants);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) use Getopt::Long qw(:config no_auto_abbrev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) use Config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) use bigint qw/hex/;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) use feature 'state';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) my $P = $0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) # Directories to scan.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) my @DIRS = ('/proc', '/sys');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) # Timer for parsing each file, in seconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) my $TIMEOUT = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) # Kernel addresses vary by architecture. We can only auto-detect the following
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) # architectures (using `uname -m`). (flag --32-bit overrides auto-detection.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) my @SUPPORTED_ARCHITECTURES = ('x86_64', 'ppc64', 'x86');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) # Command line options.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) my $help = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) my $debug = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) my $raw = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) my $output_raw = ""; # Write raw results to file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) my $input_raw = ""; # Read raw results from file instead of scanning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) my $suppress_dmesg = 0; # Don't show dmesg in output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) my $squash_by_path = 0; # Summary report grouped by absolute path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) my $squash_by_filename = 0; # Summary report grouped by filename.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) my $kernel_config_file = ""; # Kernel configuration file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) my $opt_32bit = 0; # Scan 32-bit kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) my $page_offset_32bit = 0; # Page offset for 32-bit kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) # Skip these absolute paths.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) my @skip_abs = (
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) '/proc/kmsg',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) '/proc/device-tree',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) '/proc/1/syscall',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) '/sys/firmware/devicetree',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) '/sys/kernel/debug/tracing/trace_pipe',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) '/sys/kernel/security/apparmor/revision');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) # Skip these under any subdirectory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) my @skip_any = (
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) 'pagemap',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) 'events',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) 'access',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) 'registers',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) 'snapshot_raw',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) 'trace_pipe_raw',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) 'ptmx',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) 'trace_pipe',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) 'fd',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) 'usbmon');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) sub help
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) my ($exitcode) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) print << "EOM";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) Usage: $P [OPTIONS]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) Options:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) -o, --output-raw=<file> Save results for future processing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) -i, --input-raw=<file> Read results from file instead of scanning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) --raw Show raw results (default).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) --suppress-dmesg Do not show dmesg results.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) --squash-by-path Show one result per unique path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) --squash-by-filename Show one result per unique filename.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) --kernel-config-file=<file> Kernel configuration file (e.g /boot/config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) --32-bit Scan 32-bit kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) --page-offset-32-bit=o Page offset (for 32-bit kernel 0xABCD1234).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) -d, --debug Display debugging output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) -h, --help Display this help and exit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) Scans the running kernel for potential leaking addresses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) EOM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) exit($exitcode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) GetOptions(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 'd|debug' => \$debug,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 'h|help' => \$help,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 'o|output-raw=s' => \$output_raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 'i|input-raw=s' => \$input_raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 'suppress-dmesg' => \$suppress_dmesg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 'squash-by-path' => \$squash_by_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 'squash-by-filename' => \$squash_by_filename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 'raw' => \$raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 'kernel-config-file=s' => \$kernel_config_file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) '32-bit' => \$opt_32bit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 'page-offset-32-bit=o' => \$page_offset_32bit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) ) or help(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) help(0) if ($help);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if ($input_raw) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) format_output($input_raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (!$input_raw and ($squash_by_path or $squash_by_filename)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) printf "\nSummary reporting only available with --input-raw=<file>\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) printf "(First run scan with --output-raw=<file>.)\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) exit(128);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (!(is_supported_architecture() or $opt_32bit or $page_offset_32bit)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) printf "\nScript does not support your architecture, sorry.\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) printf "\nCurrently we support: \n\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) foreach(@SUPPORTED_ARCHITECTURES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) printf "\t%s\n", $_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) printf("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) printf("If you are running a 32-bit architecture you may use:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) printf("\n\t--32-bit or --page-offset-32-bit=<page offset>\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) my $archname = `uname -m`;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) printf("Machine hardware name (`uname -m`): %s\n", $archname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) exit(129);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if ($output_raw) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) open my $fh, '>', $output_raw or die "$0: $output_raw: $!\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) select $fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) parse_dmesg();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) walk(@DIRS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) exit 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) sub dprint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) printf(STDERR @_) if $debug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) sub is_supported_architecture
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return (is_x86_64() or is_ppc64() or is_ix86_32());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) sub is_32bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) # Allow --32-bit or --page-offset-32-bit to override
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if ($opt_32bit or $page_offset_32bit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return is_ix86_32();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) sub is_ix86_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) state $arch = `uname -m`;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) chomp $arch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if ($arch =~ m/i[3456]86/) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) sub is_arch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) my ($desc) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) my $arch = `uname -m`;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) chomp $arch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if ($arch eq $desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) sub is_x86_64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) state $is = is_arch('x86_64');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return $is;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) sub is_ppc64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) state $is = is_arch('ppc64');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) return $is;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) # Gets config option value from kernel config file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) # Returns "" on error or if config option not found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) sub get_kernel_config_option
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) my ($option) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) my $value = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) my $tmp_file = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) my @config_files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) # Allow --kernel-config-file to override.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if ($kernel_config_file ne "") {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) @config_files = ($kernel_config_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) } elsif (-R "/proc/config.gz") {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) my $tmp_file = "/tmp/tmpkconf";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (system("gunzip < /proc/config.gz > $tmp_file")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) dprint("system(gunzip < /proc/config.gz) failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) return "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) @config_files = ($tmp_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) my $file = '/boot/config-' . `uname -r`;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) chomp $file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) @config_files = ($file, '/boot/config');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) foreach my $file (@config_files) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) dprint("parsing config file: $file\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) $value = option_from_file($option, $file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if ($value ne "") {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if ($tmp_file ne "") {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) system("rm -f $tmp_file");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) return $value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) # Parses $file and returns kernel configuration option value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) sub option_from_file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) my ($option, $file) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) my $str = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) my $val = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) open(my $fh, "<", $file) or return "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) while (my $line = <$fh> ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if ($line =~ /^$option/) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) ($str, $val) = split /=/, $line;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) chomp $val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) close $fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return $val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) sub is_false_positive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) my ($match) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (is_32bit()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return is_false_positive_32bit($match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) # 64 bit false positives.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if ($match =~ '\b(0x)?(f|F){16}\b' or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) $match =~ '\b(0x)?0{16}\b') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (is_x86_64() and is_in_vsyscall_memory_region($match)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) sub is_false_positive_32bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) my ($match) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) state $page_offset = get_page_offset();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if ($match =~ '\b(0x)?(f|F){8}\b') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (hex($match) < $page_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) # returns integer value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) sub get_page_offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) my $page_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) my $default_offset = 0xc0000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) # Allow --page-offset-32bit to override.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if ($page_offset_32bit != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return $page_offset_32bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) $page_offset = get_kernel_config_option('CONFIG_PAGE_OFFSET');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (!$page_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) return $default_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) return $page_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) sub is_in_vsyscall_memory_region
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) my ($match) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) my $hex = hex($match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) my $region_min = hex("0xffffffffff600000");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) my $region_max = hex("0xffffffffff601000");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return ($hex >= $region_min and $hex <= $region_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) # True if argument potentially contains a kernel address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) sub may_leak_address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) my ($line) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) my $address_re;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) # Signal masks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if ($line =~ '^SigBlk:' or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) $line =~ '^SigIgn:' or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) $line =~ '^SigCgt:') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) if ($line =~ '\bKEY=[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b' or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) $line =~ '\b[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) $address_re = get_address_re();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) while ($line =~ /($address_re)/g) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (!is_false_positive($1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) sub get_address_re
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (is_ppc64()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) return '\b(0x)?[89abcdef]00[[:xdigit:]]{13}\b';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) } elsif (is_32bit()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) return '\b(0x)?[[:xdigit:]]{8}\b';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) return get_x86_64_re();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) sub get_x86_64_re
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) # We handle page table levels but only if explicitly configured using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) # CONFIG_PGTABLE_LEVELS. If config file parsing fails or config option
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) # is not found we default to using address regular expression suitable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) # for 4 page table levels.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) state $ptl = get_kernel_config_option('CONFIG_PGTABLE_LEVELS');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if ($ptl == 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return '\b(0x)?ff[[:xdigit:]]{14}\b';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) return '\b(0x)?ffff[[:xdigit:]]{12}\b';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) sub parse_dmesg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) open my $cmd, '-|', 'dmesg';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) while (<$cmd>) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) if (may_leak_address($_)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) print 'dmesg: ' . $_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) close $cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) # True if we should skip this path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) sub skip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) my ($path) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) foreach (@skip_abs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return 1 if (/^$path$/);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) my($filename, $dirs, $suffix) = fileparse($path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) foreach (@skip_any) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) return 1 if (/^$filename$/);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) sub timed_parse_file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) my ($file) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) eval {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) alarm $TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) parse_file($file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) alarm 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if ($@) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) die unless $@ eq "alarm\n"; # Propagate unexpected errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) printf STDERR "timed out parsing: %s\n", $file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) sub parse_file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) my ($file) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (! -R $file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) if (! -T $file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) open my $fh, "<", $file or return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) while ( <$fh> ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) chomp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) if (may_leak_address($_)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) printf("$file: $_\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) close $fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) # Checks if the actual path name is leaking a kernel address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) sub check_path_for_leaks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) my ($path) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) if (may_leak_address($path)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) printf("Path name may contain address: $path\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) # Recursively walk directory tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) sub walk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) my @dirs = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) while (my $pwd = shift @dirs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) next if (!opendir(DIR, $pwd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) my @files = readdir(DIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) closedir(DIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) foreach my $file (@files) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) next if ($file eq '.' or $file eq '..');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) my $path = "$pwd/$file";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) next if (-l $path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) # skip /proc/PID except /proc/1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) next if (($path =~ /^\/proc\/[0-9]+$/) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) ($path !~ /^\/proc\/1$/));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) next if (skip($path));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) check_path_for_leaks($path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) if (-d $path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) push @dirs, $path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) dprint("parsing: $path\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) timed_parse_file($path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) sub format_output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) my ($file) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) # Default is to show raw results.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) if ($raw or (!$squash_by_path and !$squash_by_filename)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) dump_raw_output($file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) my ($total, $dmesg, $paths, $files) = parse_raw_file($file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) printf "\nTotal number of results from scan (incl dmesg): %d\n", $total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) if (!$suppress_dmesg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) print_dmesg($dmesg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) if ($squash_by_filename) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) squash_by($files, 'filename');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) if ($squash_by_path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) squash_by($paths, 'path');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) sub dump_raw_output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) my ($file) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) open (my $fh, '<', $file) or die "$0: $file: $!\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) while (<$fh>) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) if ($suppress_dmesg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if ("dmesg:" eq substr($_, 0, 6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) print $_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) close $fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) sub parse_raw_file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) my ($file) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) my $total = 0; # Total number of lines parsed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) my @dmesg; # dmesg output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) my %files; # Unique filenames containing leaks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) my %paths; # Unique paths containing leaks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) open (my $fh, '<', $file) or die "$0: $file: $!\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) while (my $line = <$fh>) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) $total++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) if ("dmesg:" eq substr($line, 0, 6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) push @dmesg, $line;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) cache_path(\%paths, $line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) cache_filename(\%files, $line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) return $total, \@dmesg, \%paths, \%files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) sub print_dmesg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) my ($dmesg) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) print "\ndmesg output:\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) if (@$dmesg == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) print "<no results>\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) foreach(@$dmesg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) my $index = index($_, ': ');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) $index += 2; # skid ': '
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) print substr($_, $index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) sub squash_by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) my ($ref, $desc) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) print "\nResults squashed by $desc (excl dmesg). ";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) print "Displaying [<number of results> <$desc>], <example result>\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) if (keys %$ref == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) print "<no results>\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) foreach(keys %$ref) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) my $lines = $ref->{$_};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) my $length = @$lines;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) printf "[%d %s] %s", $length, $_, @$lines[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) sub cache_path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) my ($paths, $line) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) my $index = index($line, ': ');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) my $path = substr($line, 0, $index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) $index += 2; # skip ': '
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) add_to_cache($paths, $path, substr($line, $index));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) sub cache_filename
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) my ($files, $line) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) my $index = index($line, ': ');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) my $path = substr($line, 0, $index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) my $filename = basename($path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) $index += 2; # skip ': '
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) add_to_cache($files, $filename, substr($line, $index));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) sub add_to_cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) my ($cache, $key, $value) = @_;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) if (!$cache->{$key}) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) $cache->{$key} = ();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) push @{$cache->{$key}}, $value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) }