leaking_addresses.pl 13 KB

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