History

leaking_addresses: add support for kernel config file

Features that rely on the ability to get kernel configuration options are ready to be implemented in script. In preparation for this we can add support for kernel config options as a separate patch to ease review. Add support for locating and parsing kernel configuration file.

Tobin C. Harding committed 8 years ago

Commit f9d2a421 parent 87e37581 file · +65 −1Browse files

Showing the changes to scripts (1 of 1 file). Show all

Unified · Split

scripts/leaking_addresses.pl+65−1
@@ -41,10 +41,10 @@ my $debug = 0; · 40 unmodified lines
4141 my $raw = 0;
4242 my $output_raw = ""; # Write raw results to file.
4343 my $input_raw = ""; # Read raw results from file instead of scanning.
44−
4544 my $suppress_dmesg = 0; # Don't show dmesg in output.
4645 my $squash_by_path = 0; # Summary report grouped by absolute path.
4746 my $squash_by_filename = 0; # Summary report grouped by filename.
47+my $kernel_config_file = ""; # Kernel configuration file.
4848
4949 # Do not parse these files (absolute path).
5050 my @skip_parse_files_abs = ('/proc/kmsg',
@@ -99,6 +99,7 @@ Options: · 48 unmodified lines
9999 --suppress-dmesg Do not show dmesg results.
100100 --squash-by-path Show one result per unique path.
101101 --squash-by-filename Show one result per unique filename.
102+ --kernel-config-file=<file> Kernel configuration file (e.g /boot/config)
102103 -d, --debug Display debugging output.
103104 -h, --help, --version Display this help and exit.
104105
@@ -118,6 +119,7 @@ GetOptions( · 13 unmodified lines
118119 'squash-by-path' => \$squash_by_path,
119120 'squash-by-filename' => \$squash_by_filename,
120121 'raw' => \$raw,
122+ 'kernel-config-file=s' => \$kernel_config_file,
121123 ) or help(1);
122124
123125 help(0) if ($help);
@@ -187,6 +189,68 @@ sub is_ppc64 · 63 unmodified lines
187189 return 0;
188190 }
189191
192+# Gets config option value from kernel config file.
193+# Returns "" on error or if config option not found.
194+sub get_kernel_config_option
195+{
196+ my ($option) = @_;
197+ my $value = "";
198+ my $tmp_file = "";
199+ my @config_files;
200+
201+ # Allow --kernel-config-file to override.
202+ if ($kernel_config_file ne "") {
203+ @config_files = ($kernel_config_file);
204+ } elsif (-R "/proc/config.gz") {
205+ my $tmp_file = "/tmp/tmpkconf";
206+
207+ if (system("gunzip < /proc/config.gz > $tmp_file")) {
208+ dprint "$0: system(gunzip < /proc/config.gz) failed\n";
209+ return "";
210+ } else {
211+ @config_files = ($tmp_file);
212+ }
213+ } else {
214+ my $file = '/boot/config-' . `uname -r`;
215+ chomp $file;
216+ @config_files = ($file, '/boot/config');
217+ }
218+
219+ foreach my $file (@config_files) {
220+ dprint("parsing config file: %s\n", $file);
221+ $value = option_from_file($option, $file);
222+ if ($value ne "") {
223+ last;
224+ }
225+ }
226+
227+ if ($tmp_file ne "") {
228+ system("rm -f $tmp_file");
229+ }
230+
231+ return $value;
232+}
233+
234+# Parses $file and returns kernel configuration option value.
235+sub option_from_file
236+{
237+ my ($option, $file) = @_;
238+ my $str = "";
239+ my $val = "";
240+
241+ open(my $fh, "<", $file) or return "";
242+ while (my $line = <$fh> ) {
243+ if ($line =~ /^$option/) {
244+ ($str, $val) = split /=/, $line;
245+ chomp $val;
246+ last;
247+ }
248+ }
249+
250+ close $fh;
251+ return $val;
252+}
253+
190254 sub is_false_positive
191255 {
192256 my ($match) = @_;