Merge branch 'dmi-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jdelvar...
[sfrench/cifs-2.6.git] / scripts / leaking_addresses.pl
index 56894daf6368f423b17445f48d08896b750ce7e8..6a897788f5a7ecd3eb699ebd6aec21ee1125aecd 100755 (executable)
@@ -3,13 +3,21 @@
 # (c) 2017 Tobin C. Harding <me@tobin.cc>
 # Licensed under the terms of the GNU GPL License version 2
 #
-# leaking_addresses.pl: Scan 64 bit kernel for potential leaking addresses.
+# leaking_addresses.pl: Scan the kernel for potential leaking addresses.
 #  - Scans dmesg output.
 #  - Walks directory tree and parses each file (for each directory in @DIRS).
 #
 # Use --debug to output path before parsing, this is useful to find files that
 # cause the script to choke.
 
+#
+# When the system is idle it is likely that most files under /proc/PID will be
+# identical for various processes.  Scanning _all_ the PIDs under /proc is
+# unnecessary and implies that we are thoroughly scanning /proc.  This is _not_
+# the case because there may be ways userspace can trigger creation of /proc
+# files that leak addresses but were not present during a scan.  For these two
+# reasons we exclude all PID directories under /proc except '1/'
+
 use warnings;
 use strict;
 use POSIX;
@@ -23,7 +31,6 @@ use bigint qw/hex/;
 use feature 'state';
 
 my $P = $0;
-my $V = '0.01';
 
 # Directories to scan.
 my @DIRS = ('/proc', '/sys');
@@ -31,10 +38,9 @@ my @DIRS = ('/proc', '/sys');
 # Timer for parsing each file, in seconds.
 my $TIMEOUT = 10;
 
-# Script can only grep for kernel addresses on the following architectures. If
-# your architecture is not listed here and has a grep'able kernel address please
-# consider submitting a patch.
-my @SUPPORTED_ARCHITECTURES = ('x86_64', 'ppc64');
+# Kernel addresses vary by architecture.  We can only auto-detect the following
+# architectures (using `uname -m`).  (flag --32-bit overrides auto-detection.)
+my @SUPPORTED_ARCHITECTURES = ('x86_64', 'ppc64', 'x86');
 
 # Command line options.
 my $help = 0;
@@ -46,42 +52,30 @@ my $suppress_dmesg = 0;             # Don't show dmesg in output.
 my $squash_by_path = 0;                # Summary report grouped by absolute path.
 my $squash_by_filename = 0;    # Summary report grouped by filename.
 my $kernel_config_file = "";   # Kernel configuration file.
-
-# Do not parse these files (absolute path).
-my @skip_parse_files_abs = ('/proc/kmsg',
-                           '/proc/kcore',
-                           '/proc/fs/ext4/sdb1/mb_groups',
-                           '/proc/1/fd/3',
-                           '/sys/firmware/devicetree',
-                           '/proc/device-tree',
-                           '/sys/kernel/debug/tracing/trace_pipe',
-                           '/sys/kernel/security/apparmor/revision');
-
-# Do not parse these files under any subdirectory.
-my @skip_parse_files_any = ('0',
-                           '1',
-                           '2',
-                           'pagemap',
-                           'events',
-                           'access',
-                           'registers',
-                           'snapshot_raw',
-                           'trace_pipe_raw',
-                           'ptmx',
-                           'trace_pipe');
-
-# Do not walk these directories (absolute path).
-my @skip_walk_dirs_abs = ();
-
-# Do not walk these directories under any subdirectory.
-my @skip_walk_dirs_any = ('self',
-                         'thread-self',
-                         'cwd',
-                         'fd',
-                         'usbmon',
-                         'stderr',
-                         'stdin',
-                         'stdout');
+my $opt_32bit = 0;             # Scan 32-bit kernel.
+my $page_offset_32bit = 0;     # Page offset for 32-bit kernel.
+
+# Skip these absolute paths.
+my @skip_abs = (
+       '/proc/kmsg',
+       '/proc/device-tree',
+       '/proc/1/syscall',
+       '/sys/firmware/devicetree',
+       '/sys/kernel/debug/tracing/trace_pipe',
+       '/sys/kernel/security/apparmor/revision');
+
+# Skip these under any subdirectory.
+my @skip_any = (
+       'pagemap',
+       'events',
+       'access',
+       'registers',
+       'snapshot_raw',
+       'trace_pipe_raw',
+       'ptmx',
+       'trace_pipe',
+       'fd',
+       'usbmon');
 
 sub help
 {
@@ -90,7 +84,6 @@ sub help
        print << "EOM";
 
 Usage: $P [OPTIONS]
-Version: $V
 
 Options:
 
@@ -101,10 +94,12 @@ Options:
              --squash-by-path          Show one result per unique path.
              --squash-by-filename      Show one result per unique filename.
        --kernel-config-file=<file>     Kernel configuration file (e.g /boot/config)
+       --32-bit                        Scan 32-bit kernel.
+       --page-offset-32-bit=o          Page offset (for 32-bit kernel 0xABCD1234).
        -d, --debug                     Display debugging output.
        -h, --help, --version           Display this help and exit.
 
-Scans the running (64 bit) kernel for potential leaking addresses.
+Scans the running kernel for potential leaking addresses.
 
 EOM
        exit($exitcode);
@@ -121,6 +116,8 @@ GetOptions(
        'squash-by-filename'    => \$squash_by_filename,
        'raw'                   => \$raw,
        'kernel-config-file=s'  => \$kernel_config_file,
+       '32-bit'                => \$opt_32bit,
+       'page-offset-32-bit=o'  => \$page_offset_32bit,
 ) or help(1);
 
 help(0) if ($help);
@@ -136,7 +133,7 @@ if (!$input_raw and ($squash_by_path or $squash_by_filename)) {
        exit(128);
 }
 
-if (!is_supported_architecture()) {
+if (!(is_supported_architecture() or $opt_32bit or $page_offset_32bit)) {
        printf "\nScript does not support your architecture, sorry.\n";
        printf "\nCurrently we support: \n\n";
        foreach(@SUPPORTED_ARCHITECTURES) {
@@ -144,6 +141,9 @@ if (!is_supported_architecture()) {
        }
        printf("\n");
 
+       printf("If you are running a 32-bit architecture you may use:\n");
+       printf("\n\t--32-bit or --page-offset-32-bit=<page offset>\n\n");
+
        my $archname = `uname -m`;
        printf("Machine hardware name (`uname -m`): %s\n", $archname);
 
@@ -167,27 +167,52 @@ sub dprint
 
 sub is_supported_architecture
 {
-       return (is_x86_64() or is_ppc64());
+       return (is_x86_64() or is_ppc64() or is_ix86_32());
 }
 
-sub is_x86_64
+sub is_32bit
 {
-       my $archname = `uname -m`;
-
-       if ($archname =~ m/x86_64/) {
+       # Allow --32-bit or --page-offset-32-bit to override
+       if ($opt_32bit or $page_offset_32bit) {
                return 1;
        }
-       return 0;
+
+       return is_ix86_32();
 }
 
-sub is_ppc64
+sub is_ix86_32
 {
-       my $archname = `uname -m`;
+       state $arch = `uname -m`;
 
-       if ($archname =~ m/ppc64/) {
-               return 1;
-       }
-       return 0;
+       chomp $arch;
+       if ($arch =~ m/i[3456]86/) {
+               return 1;
+       }
+       return 0;
+}
+
+sub is_arch
+{
+       my ($desc) = @_;
+       my $arch = `uname -m`;
+
+       chomp $arch;
+       if ($arch eq $desc) {
+               return 1;
+       }
+       return 0;
+}
+
+sub is_x86_64
+{
+       state $is = is_arch('x86_64');
+       return $is;
+}
+
+sub is_ppc64
+{
+       state $is = is_arch('ppc64');
+       return $is;
 }
 
 # Gets config option value from kernel config file.
@@ -256,6 +281,12 @@ sub is_false_positive
 {
        my ($match) = @_;
 
+       if (is_32bit()) {
+               return is_false_positive_32bit($match);
+       }
+
+       # 64 bit false positives.
+
        if ($match =~ '\b(0x)?(f|F){16}\b' or
            $match =~ '\b(0x)?0{16}\b') {
                return 1;
@@ -268,6 +299,40 @@ sub is_false_positive
        return 0;
 }
 
+sub is_false_positive_32bit
+{
+       my ($match) = @_;
+       state $page_offset = get_page_offset();
+
+       if ($match =~ '\b(0x)?(f|F){8}\b') {
+               return 1;
+       }
+
+       if (hex($match) < $page_offset) {
+               return 1;
+       }
+
+       return 0;
+}
+
+# returns integer value
+sub get_page_offset
+{
+       my $page_offset;
+       my $default_offset = 0xc0000000;
+
+       # Allow --page-offset-32bit to override.
+       if ($page_offset_32bit != 0) {
+               return $page_offset_32bit;
+       }
+
+       $page_offset = get_kernel_config_option('CONFIG_PAGE_OFFSET');
+       if (!$page_offset) {
+              return $default_offset;
+       }
+       return $page_offset;
+}
+
 sub is_in_vsyscall_memory_region
 {
        my ($match) = @_;
@@ -298,7 +363,7 @@ sub may_leak_address
        }
 
        $address_re = get_address_re();
-       while (/($address_re)/g) {
+       while ($line =~ /($address_re)/g) {
                if (!is_false_positive($1)) {
                        return 1;
                }
@@ -309,11 +374,13 @@ sub may_leak_address
 
 sub get_address_re
 {
-       if (is_x86_64()) {
-               return get_x86_64_re();
-       } elsif (is_ppc64()) {
+       if (is_ppc64()) {
                return '\b(0x)?[89abcdef]00[[:xdigit:]]{13}\b';
+       } elsif (is_32bit()) {
+               return '\b(0x)?[[:xdigit:]]{8}\b';
        }
+
+       return get_x86_64_re();
 }
 
 sub get_x86_64_re
@@ -344,26 +411,20 @@ sub parse_dmesg
 # True if we should skip this path.
 sub skip
 {
-       my ($path, $paths_abs, $paths_any) = @_;
+       my ($path) = @_;
 
-       foreach (@$paths_abs) {
+       foreach (@skip_abs) {
                return 1 if (/^$path$/);
        }
 
        my($filename, $dirs, $suffix) = fileparse($path);
-       foreach (@$paths_any) {
+       foreach (@skip_any) {
                return 1 if (/^$filename$/);
        }
 
        return 0;
 }
 
-sub skip_parse
-{
-       my ($path) = @_;
-       return skip($path, \@skip_parse_files_abs, \@skip_parse_files_any);
-}
-
 sub timed_parse_file
 {
        my ($file) = @_;
@@ -389,11 +450,9 @@ sub parse_file
                return;
        }
 
-       if (skip_parse($file)) {
-               dprint "skipping file: $file\n";
+       if (! -T $file) {
                return;
        }
-       dprint "parsing: $file\n";
 
        open my $fh, "<", $file or return;
        while ( <$fh> ) {
@@ -404,12 +463,14 @@ sub parse_file
        close $fh;
 }
 
-
-# True if we should skip walking this directory.
-sub skip_walk
+# Checks if the actual path name is leaking a kernel address.
+sub check_path_for_leaks
 {
        my ($path) = @_;
-       return skip($path, \@skip_walk_dirs_abs, \@skip_walk_dirs_any)
+
+       if (may_leak_address($path)) {
+               printf("Path name may contain address: $path\n");
+       }
 }
 
 # Recursively walk directory tree.
@@ -418,7 +479,6 @@ sub walk
        my @dirs = @_;
 
        while (my $pwd = shift @dirs) {
-               next if (skip_walk($pwd));
                next if (!opendir(DIR, $pwd));
                my @files = readdir(DIR);
                closedir(DIR);
@@ -429,11 +489,21 @@ sub walk
                        my $path = "$pwd/$file";
                        next if (-l $path);
 
+                       # skip /proc/PID except /proc/1
+                       next if (($path =~ /^\/proc\/[0-9]+$/) &&
+                                ($path !~ /^\/proc\/1$/));
+
+                       next if (skip($path));
+
+                       check_path_for_leaks($path);
+
                        if (-d $path) {
                                push @dirs, $path;
-                       } else {
-                               timed_parse_file($path);
+                               next;
                        }
+
+                       dprint "parsing: $path\n";
+                       timed_parse_file($path);
                }
        }
 }