blob: 3f8c6e230962d732497e1be9f5eafdb2153163b3 [file] [log] [blame]
Tobin C. Harding136fc5c2017-11-06 16:19:27 +11001#!/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. Harding136fc5c2017-11-06 16:19:27 +110010# 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
16use warnings;
17use strict;
18use POSIX;
19use File::Basename;
20use File::Spec;
21use Cwd 'abs_path';
22use Term::ANSIColor qw(:constants);
23use Getopt::Long qw(:config no_auto_abbrev);
24
25my $P = $0;
26my $V = '0.01';
27
28# Directories to scan.
29my @DIRS = ('/proc', '/sys');
30
31# Command line options.
32my $help = 0;
33my $debug = 0;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110034
35# Do not parse these files (absolute path).
36my @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. Hardinga2847332017-11-09 13:28:43 +110043# Do not parse these files under any subdirectory.
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110044my @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).
57my @skip_walk_dirs_abs = ();
58
59# Do not walk these directories under any subdirectory.
60my @skip_walk_dirs_any = ('self',
61 'thread-self',
62 'cwd',
63 'fd',
64 'stderr',
65 'stdin',
66 'stdout');
67
68sub help
69{
70 my ($exitcode) = @_;
71
72 print << "EOM";
73Usage: $P [OPTIONS]
74Version: $V
75
76Options:
77
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110078 -d, --debug Display debugging output.
79 -h, --help, --version Display this help and exit.
80
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110081Scans the running (64 bit) kernel for potential leaking addresses.
82
83EOM
84 exit($exitcode);
85}
86
87GetOptions(
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110088 'd|debug' => \$debug,
89 'h|help' => \$help,
90 'version' => \$help
91) or help(1);
92
93help(0) if ($help);
94
Tobin C. Harding136fc5c2017-11-06 16:19:27 +110095parse_dmesg();
96walk(@DIRS);
97
98exit 0;
99
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100100sub dprint
101{
102 printf(STDERR @_) if $debug;
103}
104
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100105sub is_false_positive
106{
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100107 my ($match) = @_;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100108
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100109 if ($match =~ '\b(0x)?(f|F){16}\b' or
110 $match =~ '\b(0x)?0{16}\b') {
111 return 1;
112 }
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100113
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100114
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100115 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. Harding136fc5c2017-11-06 16:19:27 +1100121}
122
123# True if argument potentially contains a kernel address.
124sub may_leak_address
125{
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100126 my ($line) = @_;
127 my $address = '\b(0x)?ffff[[:xdigit:]]{12}\b';
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100128
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100129 # Signal masks.
130 if ($line =~ '^SigBlk:' or
131 $line =~ '^SigCgt:') {
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100132 return 0;
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100133 }
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100134
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100135 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. Harding136fc5c2017-11-06 16:19:27 +1100139
Tobin C. Harding7e5758f2017-11-08 11:01:59 +1100140 while (/($address)/g) {
141 if (!is_false_positive($1)) {
142 return 1;
143 }
144 }
145
146 return 0;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100147}
148
149sub 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.
161sub 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
177sub skip_parse
178{
179 my ($path) = @_;
180 return skip($path, \@skip_parse_files_abs, \@skip_parse_files_any);
181}
182
183sub 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.
208sub 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.
215sub walk
216{
217 my @dirs = @_;
Tobin C. Harding136fc5c2017-11-06 16:19:27 +1100218
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}