1a31fa7409b7a311f4bcf52ceed8e2177ad4e3c3
[libguestfs.git] / regressions / test-noexec-stack.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 die("NOEXEC_CHECK not set") unless(exists($ENV{NOEXEC_CHECK}));
7
8 my @files = split(/ /, $ENV{NOEXEC_CHECK});
9
10 FILES: foreach my $file (@files) {
11     my $output;
12     my @cmd = ('readelf', '-l', $file);
13     open($output, '-|', @cmd)
14         or die("$0: failed to run: '".join(' ', @cmd)."': $!\n");
15
16     my $offset;
17     my $line = 1;
18
19     # Find the offset of the Flags field
20     while(<$output>) {
21         next unless(/^\s*Type\b/);
22
23         my @lines;
24         push(@lines, $_);
25
26         # Look for a Flg field on this line (32 bit)
27         $offset = index($_, 'Flg ');
28
29         if(-1 == $offset) {
30             # 64 bit is split over 2 lines. Look for a Flags field on the next
31             # line
32             $_ = <$output>;
33             $offset = index($_, 'Flags ');
34             $line = 2;
35             push(@lines, $_);
36         }
37
38         die("Unrecognised header: ".join("\n", @lines)) if(-1 == $offset);
39         last;
40     }
41
42     # Find the GNU_STACK entry
43     while(<$output>) {
44         next unless(/^\s*GNU_STACK\b/);
45
46         # Skip over input lines according to the header
47         for(my $i = 1; $i < $line; $i++) {
48             $_ = <$output>;
49         }
50
51         my $flags = substr($_, $offset, 3);
52
53         $flags =~ /^[ R][ W]([ E])$/ or die("Unrecognised flags: $flags");
54
55         if('E' eq $1) {
56             print "***** $file has an executable stack *****\n";
57             exit(1);
58         }
59
60         next FILES;
61     }
62
63     die("Didn't find GNU_STACK entry");
64 }