Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | # |
| 3 | # (c) 2017 Tobin C. Harding <[email protected]> |
| 4 | # Licensed under the terms of the GNU GPL License version 2 |
| 5 | # |
| 6 | # leaking_addresses.pl: Scan 64 bit kernel for potential leaking addresses. |
| 7 | # - Scans dmesg output. |
| 8 | # - Walks directory tree and parses each file (for each directory in @DIRS). |
| 9 | # |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 10 | # Use --debug to output path before parsing, this is useful to find files that |
| 11 | # cause the script to choke. |
| 12 | # |
| 13 | # You may like to set kptr_restrict=2 before running script |
| 14 | # (see Documentation/sysctl/kernel.txt). |
| 15 | |
| 16 | use warnings; |
| 17 | use strict; |
| 18 | use POSIX; |
| 19 | use File::Basename; |
| 20 | use File::Spec; |
| 21 | use Cwd 'abs_path'; |
| 22 | use Term::ANSIColor qw(:constants); |
| 23 | use Getopt::Long qw(:config no_auto_abbrev); |
| 24 | |
| 25 | my $P = $0; |
| 26 | my $V = '0.01'; |
| 27 | |
| 28 | # Directories to scan. |
| 29 | my @DIRS = ('/proc', '/sys'); |
| 30 | |
| 31 | # Command line options. |
| 32 | my $help = 0; |
| 33 | my $debug = 0; |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 34 | |
| 35 | # Do not parse these files (absolute path). |
| 36 | my @skip_parse_files_abs = ('/proc/kmsg', |
| 37 | '/proc/kcore', |
| 38 | '/proc/fs/ext4/sdb1/mb_groups', |
| 39 | '/proc/1/fd/3', |
| 40 | '/sys/kernel/debug/tracing/trace_pipe', |
| 41 | '/sys/kernel/security/apparmor/revision'); |
| 42 | |
Tobin C. Harding | a284733 | 2017-11-09 13:28:43 +1100 | [diff] [blame^] | 43 | # Do not parse these files under any subdirectory. |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 44 | my @skip_parse_files_any = ('0', |
| 45 | '1', |
| 46 | '2', |
| 47 | 'pagemap', |
| 48 | 'events', |
| 49 | 'access', |
| 50 | 'registers', |
| 51 | 'snapshot_raw', |
| 52 | 'trace_pipe_raw', |
| 53 | 'ptmx', |
| 54 | 'trace_pipe'); |
| 55 | |
| 56 | # Do not walk these directories (absolute path). |
| 57 | my @skip_walk_dirs_abs = (); |
| 58 | |
| 59 | # Do not walk these directories under any subdirectory. |
| 60 | my @skip_walk_dirs_any = ('self', |
| 61 | 'thread-self', |
| 62 | 'cwd', |
| 63 | 'fd', |
| 64 | 'stderr', |
| 65 | 'stdin', |
| 66 | 'stdout'); |
| 67 | |
| 68 | sub help |
| 69 | { |
| 70 | my ($exitcode) = @_; |
| 71 | |
| 72 | print << "EOM"; |
| 73 | Usage: $P [OPTIONS] |
| 74 | Version: $V |
| 75 | |
| 76 | Options: |
| 77 | |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 78 | -d, --debug Display debugging output. |
| 79 | -h, --help, --version Display this help and exit. |
| 80 | |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 81 | Scans the running (64 bit) kernel for potential leaking addresses. |
| 82 | |
| 83 | EOM |
| 84 | exit($exitcode); |
| 85 | } |
| 86 | |
| 87 | GetOptions( |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 88 | 'd|debug' => \$debug, |
| 89 | 'h|help' => \$help, |
| 90 | 'version' => \$help |
| 91 | ) or help(1); |
| 92 | |
| 93 | help(0) if ($help); |
| 94 | |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 95 | parse_dmesg(); |
| 96 | walk(@DIRS); |
| 97 | |
| 98 | exit 0; |
| 99 | |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 100 | sub dprint |
| 101 | { |
| 102 | printf(STDERR @_) if $debug; |
| 103 | } |
| 104 | |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 105 | sub is_false_positive |
| 106 | { |
Tobin C. Harding | 7e5758f | 2017-11-08 11:01:59 +1100 | [diff] [blame] | 107 | my ($match) = @_; |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 108 | |
Tobin C. Harding | 7e5758f | 2017-11-08 11:01:59 +1100 | [diff] [blame] | 109 | if ($match =~ '\b(0x)?(f|F){16}\b' or |
| 110 | $match =~ '\b(0x)?0{16}\b') { |
| 111 | return 1; |
| 112 | } |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 113 | |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 114 | |
Tobin C. Harding | 7e5758f | 2017-11-08 11:01:59 +1100 | [diff] [blame] | 115 | if ($match =~ '\bf{10}600000\b' or# vsyscall memory region, we should probably check against a range here. |
| 116 | $match =~ '\bf{10}601000\b') { |
| 117 | return 1; |
| 118 | } |
| 119 | |
| 120 | return 0; |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | # True if argument potentially contains a kernel address. |
| 124 | sub may_leak_address |
| 125 | { |
Tobin C. Harding | 7e5758f | 2017-11-08 11:01:59 +1100 | [diff] [blame] | 126 | my ($line) = @_; |
| 127 | my $address = '\b(0x)?ffff[[:xdigit:]]{12}\b'; |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 128 | |
Tobin C. Harding | 7e5758f | 2017-11-08 11:01:59 +1100 | [diff] [blame] | 129 | # Signal masks. |
| 130 | if ($line =~ '^SigBlk:' or |
| 131 | $line =~ '^SigCgt:') { |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 132 | return 0; |
Tobin C. Harding | 7e5758f | 2017-11-08 11:01:59 +1100 | [diff] [blame] | 133 | } |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 134 | |
Tobin C. Harding | 7e5758f | 2017-11-08 11:01:59 +1100 | [diff] [blame] | 135 | if ($line =~ '\bKEY=[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b' or |
| 136 | $line =~ '\b[[:xdigit:]]{14} [[:xdigit:]]{16} [[:xdigit:]]{16}\b') { |
| 137 | return 0; |
| 138 | } |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 139 | |
Tobin C. Harding | 7e5758f | 2017-11-08 11:01:59 +1100 | [diff] [blame] | 140 | while (/($address)/g) { |
| 141 | if (!is_false_positive($1)) { |
| 142 | return 1; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return 0; |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | sub parse_dmesg |
| 150 | { |
| 151 | open my $cmd, '-|', 'dmesg'; |
| 152 | while (<$cmd>) { |
| 153 | if (may_leak_address($_)) { |
| 154 | print 'dmesg: ' . $_; |
| 155 | } |
| 156 | } |
| 157 | close $cmd; |
| 158 | } |
| 159 | |
| 160 | # True if we should skip this path. |
| 161 | sub skip |
| 162 | { |
| 163 | my ($path, $paths_abs, $paths_any) = @_; |
| 164 | |
| 165 | foreach (@$paths_abs) { |
| 166 | return 1 if (/^$path$/); |
| 167 | } |
| 168 | |
| 169 | my($filename, $dirs, $suffix) = fileparse($path); |
| 170 | foreach (@$paths_any) { |
| 171 | return 1 if (/^$filename$/); |
| 172 | } |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | sub skip_parse |
| 178 | { |
| 179 | my ($path) = @_; |
| 180 | return skip($path, \@skip_parse_files_abs, \@skip_parse_files_any); |
| 181 | } |
| 182 | |
| 183 | sub parse_file |
| 184 | { |
| 185 | my ($file) = @_; |
| 186 | |
| 187 | if (! -R $file) { |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | if (skip_parse($file)) { |
| 192 | dprint "skipping file: $file\n"; |
| 193 | return; |
| 194 | } |
| 195 | dprint "parsing: $file\n"; |
| 196 | |
| 197 | open my $fh, "<", $file or return; |
| 198 | while ( <$fh> ) { |
| 199 | if (may_leak_address($_)) { |
| 200 | print $file . ': ' . $_; |
| 201 | } |
| 202 | } |
| 203 | close $fh; |
| 204 | } |
| 205 | |
| 206 | |
| 207 | # True if we should skip walking this directory. |
| 208 | sub skip_walk |
| 209 | { |
| 210 | my ($path) = @_; |
| 211 | return skip($path, \@skip_walk_dirs_abs, \@skip_walk_dirs_any) |
| 212 | } |
| 213 | |
| 214 | # Recursively walk directory tree. |
| 215 | sub walk |
| 216 | { |
| 217 | my @dirs = @_; |
Tobin C. Harding | 136fc5c | 2017-11-06 16:19:27 +1100 | [diff] [blame] | 218 | |
| 219 | while (my $pwd = shift @dirs) { |
| 220 | next if (skip_walk($pwd)); |
| 221 | next if (!opendir(DIR, $pwd)); |
| 222 | my @files = readdir(DIR); |
| 223 | closedir(DIR); |
| 224 | |
| 225 | foreach my $file (@files) { |
| 226 | next if ($file eq '.' or $file eq '..'); |
| 227 | |
| 228 | my $path = "$pwd/$file"; |
| 229 | next if (-l $path); |
| 230 | |
| 231 | if (-d $path) { |
| 232 | push @dirs, $path; |
| 233 | } else { |
| 234 | parse_file($path); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |