todo: Remove obsolete items from TODO file.
[libguestfs.git] / regressions / test-noexec-stack.pl
1 #!/usr/bin/perl
2 # Copyright (C) 2009 Red Hat Inc.
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 use strict;
19 use warnings;
20
21 die("NOEXEC_CHECK not set") unless(exists($ENV{NOEXEC_CHECK}));
22
23 my @files = split(/ /, $ENV{NOEXEC_CHECK});
24
25 FILES: foreach my $file (@files) {
26     my $output;
27     my @cmd = ('readelf', '-l', $file);
28     open($output, '-|', @cmd)
29         or die("$0: failed to run: '".join(' ', @cmd)."': $!\n");
30
31     my $offset;
32     my $line = 1;
33
34     # Find the offset of the Flags field
35     while(<$output>) {
36         next unless(/^\s*Type\b/);
37
38         my @lines;
39         push(@lines, $_);
40
41         # Look for a Flg field on this line (32 bit)
42         $offset = index($_, 'Flg ');
43
44         if(-1 == $offset) {
45             # 64 bit is split over 2 lines. Look for a Flags field on the next
46             # line
47             $_ = <$output>;
48             $offset = index($_, 'Flags ');
49             $line = 2;
50             push(@lines, $_);
51         }
52
53         die("Unrecognised header: ".join("\n", @lines)) if(-1 == $offset);
54         last;
55     }
56
57     # Find the GNU_STACK entry
58     while(<$output>) {
59         next unless(/^\s*GNU_STACK\b/);
60
61         # Skip over input lines according to the header
62         for(my $i = 1; $i < $line; $i++) {
63             $_ = <$output>;
64         }
65
66         my $flags = substr($_, $offset, 3);
67
68         $flags =~ /^[ R][ W]([ E])$/ or die("Unrecognised flags: $flags");
69
70         if('E' eq $1) {
71             print "***** $file has an executable stack *****\n";
72             exit(1);
73         }
74
75         next FILES;
76     }
77
78     die("Didn't find GNU_STACK entry");
79 }