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