History

leaking_addresses: add timeout on file read

Currently script can stall if we read certain files (like /proc/kmsg). While we have a mechanism to skip these files once they are discovered it would be nice to not stall on as yet undiscovered files of this kind. Set a timer before each file is parsed, warn user if timer expires.

Tobin C. Harding committed 9 years ago

Commit dd98c251 parent 62139c11 file · +21 −1Browse files

Unified · Split

scripts/leaking_addresses.pl+21−1
@@ -29,6 +29,9 @@ my $V = '0.01'; · 28 unmodified lines
2929 # Directories to scan.
3030 my @DIRS = ('/proc', '/sys');
3131
32+# Timer for parsing each file, in seconds.
33+my $TIMEOUT = 10;
34+
3235 # Script can only grep for kernel addresses on the following architectures. If
3336 # your architecture is not listed here and has a grep'able kernel address please
3437 # consider submitting a patch.
@@ -284,6 +287,23 @@ sub skip_parse · 249 unmodified lines
284287 return skip($path, \@skip_parse_files_abs, \@skip_parse_files_any);
285288 }
286289
290+sub timed_parse_file
291+{
292+ my ($file) = @_;
293+
294+ eval {
295+ local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required.
296+ alarm $TIMEOUT;
297+ parse_file($file);
298+ alarm 0;
299+ };
300+
301+ if ($@) {
302+ die unless $@ eq "alarm\n"; # Propagate unexpected errors.
303+ printf STDERR "timed out parsing: %s\n", $file;
304+ }
305+}
306+
287307 sub parse_file
288308 {
289309 my ($file) = @_;
@@ -335,7 +355,7 @@ sub walk · 45 unmodified lines
335355 if (-d $path) {
336356 push @dirs, $path;
337357 } else {
338− parse_file($path);
358+ timed_parse_file($path);
339359 }
340360 }
341361 }