]> asedeno.scripts.mit.edu Git - linux.git/blobdiff - scripts/leaking_addresses.pl
Merge tag 'iio-fixes-for-4.17a' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / scripts / leaking_addresses.pl
index 05906f6cf6b9561741ee5e815f34c22b4f3144e7..6a897788f5a7ecd3eb699ebd6aec21ee1125aecd 100755 (executable)
 # 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');
@@ -48,41 +55,27 @@ my $kernel_config_file = "";        # Kernel configuration file.
 my $opt_32bit = 0;             # Scan 32-bit kernel.
 my $page_offset_32bit = 0;     # Page offset for 32-bit kernel.
 
-# 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');
+# 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
 {
@@ -91,7 +84,6 @@ sub help
        print << "EOM";
 
 Usage: $P [OPTIONS]
-Version: $V
 
 Options:
 
@@ -190,7 +182,7 @@ sub is_32bit
 
 sub is_ix86_32
 {
-       my $arch = `uname -m`;
+       state $arch = `uname -m`;
 
        chomp $arch;
        if ($arch =~ m/i[3456]86/) {
@@ -213,12 +205,14 @@ sub is_arch
 
 sub is_x86_64
 {
-       return is_arch('x86_64');
+       state $is = is_arch('x86_64');
+       return $is;
 }
 
 sub is_ppc64
 {
-       return is_arch('ppc64');
+       state $is = is_arch('ppc64');
+       return $is;
 }
 
 # Gets config option value from kernel config file.
@@ -369,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;
                }
@@ -417,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) = @_;
@@ -462,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> ) {
@@ -477,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.
@@ -491,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);
@@ -502,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);
                }
        }
 }