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