57584412182a1d6db18d7d4da53139757a579798
[p5sagit/p5-mst-13.2.git] / t / op / taint.t
1 #!./perl -T
2 #
3 # Taint tests by Tom Phoenix <rootbeer@teleport.com>.
4 #
5 # I don't claim to know all about tainting. If anyone sees
6 # tests that I've missed here, please add them. But this is 
7 # better than having no tests at all, right?
8 #
9
10 BEGIN {
11     chdir 't' if -d 't';
12     @INC = '../lib' if -d '../lib';
13 }
14
15 use strict;
16 use Config;
17
18 my $Is_VMS = $^O eq 'VMS';
19 my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' : './perl';
20 if ($Is_VMS) {
21     eval <<EndOfCleanup;
22         END {
23             \$ENV{PATH} = '';
24             warn "# Note: logical name 'PATH' may have been deleted\n";
25             \$ENV{IFS} =  "$ENV{IFS}";
26             \$ENV{'DCL\$PATH'} = "$ENV{'DCL$PATH'}";
27         }
28 EndOfCleanup
29 }
30
31 # Sources of taint:
32 #   The empty tainted value, for tainting strings
33 my $TAINT = substr($^X, 0, 0);
34 #   A tainted zero, useful for tainting numbers
35 my $TAINT0 = 0 + $TAINT;
36
37 # This taints each argument passed. All must be lvalues.
38 # Side effect: It also stringifies them. :-(
39 sub taint_these (@) {
40     for (@_) { $_ .= $TAINT }
41 }
42
43 # How to identify taint when you see it
44 sub any_tainted (@) {
45     not eval { join("",@_), kill 0; 1 };
46 }
47 sub tainted ($) {
48     any_tainted @_;
49 }
50 sub all_tainted (@) {
51     for (@_) { return 0 unless tainted $_ }
52     1;
53 }
54
55 sub test ($$;$) {
56     my($serial, $boolean, $diag) = @_;
57     if ($boolean) {
58         print "ok $serial\n";
59     } else {
60         print "not ok $serial\n";
61         for (split m/^/m, $diag) {
62             print "# $_";
63         }
64         print "\n" unless 
65             $diag eq ''
66             or substr($diag, -1) eq "\n";
67     }
68 }
69
70 # We need an external program to call.
71 my $ECHO = "./echo$$";
72 END { unlink $ECHO }
73 open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
74 print PROG 'print "@ARGV\n"', "\n";
75 close PROG;
76 my $echo = "$Invoke_Perl $ECHO";
77
78 print "1..96\n";
79
80 # First, let's make sure that Perl is checking the dangerous
81 # environment variables. Maybe they aren't set yet, so we'll
82 # taint them ourselves.
83 {
84     $ENV{'DCL$PATH'} = '' if $Is_VMS;
85
86     $ENV{PATH} = $TAINT;
87     $ENV{IFS} = '';
88     test 1, eval { `$echo 1` } eq '';
89     test 2, $@ =~ /^Insecure \$ENV{PATH}/, $@;
90
91     $ENV{PATH} = '';
92     $ENV{IFS} = $TAINT;
93     test 3, eval { `$echo 1` } eq '';
94     test 4, $@ =~ /^Insecure \$ENV{IFS}/, $@;
95
96     my ($tmp) = grep { (stat)[2] & 2 } '/tmp', '/var/tmp', '/usr/tmp';
97     if ($tmp and $^O ne 'os2') {        # All dirs are writable under OS/2
98         $ENV{PATH} = $tmp;
99         $ENV{IFS} = '';
100         test 5, eval { `$echo 1` } eq '';
101         test 6, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
102     }
103     else {
104         print "# can't find writeable directory to test PATH tainting\n";
105         for (5..6) { print "ok $_\n" }
106     }
107
108     $ENV{PATH} = '';
109     $ENV{IFS} = '';
110     test 7, eval { `$echo 1` } eq "1\n";
111     test 8, $@ eq '', $@;
112
113     if ($Is_VMS) {
114         $ENV{'DCL$PATH'} = $TAINT;
115         test 9,  eval { `$echo 1` } eq '';
116         test 10, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
117         $ENV{'DCL$PATH'} = '';
118     }
119     else {
120         print "# This is not VMS\n";
121         for (9..10) { print "ok $_\n"; }
122     }
123 }
124
125 # Let's see that we can taint and untaint as needed.
126 {
127     my $foo = $TAINT;
128     test 11, tainted $foo;
129
130     $foo = "foo";
131     test 12, not tainted $foo;
132
133     taint_these($foo);
134     test 13, tainted $foo;
135
136     my @list = 1..10;
137     test 14, not any_tainted @list;
138     taint_these @list[1,3,5,7,9];
139     test 15, any_tainted @list;
140     test 16, all_tainted @list[1,3,5,7,9];
141     test 17, not any_tainted @list[0,2,4,6,8];
142
143     ($foo) = $foo =~ /(.+)/;
144     test 18, not tainted $foo;
145
146     $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
147     test 19, not tainted $foo;
148     test 20, $foo eq 'bar';
149
150     my $pi = 4 * atan2(1,1) + $TAINT0;
151     test 21, tainted $pi;
152
153     ($pi) = $pi =~ /(\d+\.\d+)/;
154     test 22, not tainted $pi;
155     test 23, sprintf("%.5f", $pi) eq '3.14159';
156 }
157
158 # How about command-line arguments? The problem is that we don't
159 # always get some, so we'll run another process with some.
160 {
161     my $arg = "./arg$$";
162     open PROG, "> $arg" or die "Can't create $arg: $!";
163     print PROG q{
164         eval { join('', @ARGV), kill 0 };
165         exit 0 if $@ =~ /^Insecure dependency/;
166         print "# Oops: \$@ was [$@]\n";
167         exit 1;
168     };
169     close PROG;
170     print `$Invoke_Perl "-T" $arg and some suspect arguments`;
171     test 24, !$?, "Exited with status $?";
172     unlink $arg;
173 }
174
175 # Reading from a file should be tainted
176 {
177     my $file = './perl' . $Config{exe_ext};
178     test 25, open(FILE, $file), "Couldn't open '$file': $!";
179
180     my $block;
181     sysread(FILE, $block, 100);
182     my $line = <FILE>;          # Should "work"
183     close FILE;
184     test 26, tainted $block;
185     test 27, tainted $line;
186 }
187
188 # Globs should be tainted. 
189 {
190     my @globs = <*>;
191     test 28, all_tainted @globs;
192
193     @globs = glob '*';
194     test 29, all_tainted @globs;
195 }
196
197 # Output of commands should be tainted
198 {
199     my $foo = `$echo abc`;
200     test 30, tainted $foo;
201 }
202
203 # Certain system variables should be tainted
204 {
205     test 31, all_tainted $^X, $0;
206 }
207
208 # Results of matching should all be untainted
209 {
210     my $foo = "abcdefghi" . $TAINT;
211     test 32, tainted $foo;
212
213     $foo =~ /def/;
214     test 33, not any_tainted $`, $&, $';
215
216     $foo =~ /(...)(...)(...)/;
217     test 34, not any_tainted $1, $2, $3, $+;
218
219     my @bar = $foo =~ /(...)(...)(...)/;
220     test 35, not any_tainted @bar;
221
222     test 36, tainted $foo;      # $foo should still be tainted!
223     test 37, $foo eq "abcdefghi";
224 }
225
226 # Operations which affect files can't use tainted data.
227 {
228     test 38, eval { chmod 0, $TAINT } eq '', 'chmod';
229     test 39, $@ =~ /^Insecure dependency/, $@;
230
231     test 40, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate';
232     test 41, $@ =~ /^Insecure dependency/, $@;
233
234     test 42, eval { rename '', $TAINT } eq '', 'rename';
235     test 43, $@ =~ /^Insecure dependency/, $@;
236
237     test 44, eval { unlink $TAINT } eq '', 'unlink';
238     test 45, $@ =~ /^Insecure dependency/, $@;
239
240     test 46, eval { utime $TAINT } eq '', 'utime';
241     test 47, $@ =~ /^Insecure dependency/, $@;
242
243     if ($Config{d_chown}) {
244         test 48, eval { chown -1, -1, $TAINT } eq '', 'chown';
245         test 49, $@ =~ /^Insecure dependency/, $@;
246     }
247     else {
248         print "# chown() is not available\n";
249         for (48..49) { print "ok $_\n" }
250     }
251
252     if ($Config{d_link}) {
253         test 50, eval { link $TAINT, '' } eq '', 'link';
254         test 51, $@ =~ /^Insecure dependency/, $@;
255     }
256     else {
257         print "# link() is not available\n";
258         for (50..51) { print "ok $_\n" }
259     }
260
261     if ($Config{d_symlink}) {
262         test 52, eval { symlink $TAINT, '' } eq '', 'symlink';
263         test 53, $@ =~ /^Insecure dependency/, $@;
264     }
265     else {
266         print "# symlink() is not available\n";
267         for (52..53) { print "ok $_\n" }
268     }
269 }
270
271 # Operations which affect directories can't use tainted data.
272 {
273     test 54, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir';
274     test 55, $@ =~ /^Insecure dependency/, $@;
275
276     test 56, eval { rmdir $TAINT } eq '', 'rmdir';
277     test 57, $@ =~ /^Insecure dependency/, $@;
278
279     test 58, eval { chdir $TAINT } eq '', 'chdir';
280     test 59, $@ =~ /^Insecure dependency/, $@;
281
282     if ($Config{d_chroot}) {
283         test 60, eval { chroot $TAINT } eq '', 'chroot';
284         test 61, $@ =~ /^Insecure dependency/, $@;
285     }
286     else {
287         print "# chroot() is not available\n";
288         for (60..61) { print "ok $_\n" }
289     }
290 }
291
292 # Some operations using files can't use tainted data.
293 {
294     my $foo = "imaginary library" . $TAINT;
295     test 62, eval { require $foo } eq '', 'require';
296     test 63, $@ =~ /^Insecure dependency/, $@;
297
298     my $filename = "./taintB$$";        # NB: $filename isn't tainted!
299     END { unlink $filename if defined $filename }
300     $foo = $filename . $TAINT;
301     unlink $filename;   # in any case
302
303     test 64, eval { open FOO, $foo } eq '', 'open for read';
304     test 65, $@ eq '', $@;              # NB: This should be allowed
305     test 66, $! == 2;                   # File not found
306
307     test 67, eval { open FOO, "> $foo" } eq '', 'open for write';
308     test 68, $@ =~ /^Insecure dependency/, $@;
309 }
310
311 # Commands to the system can't use tainted data
312 {
313     my $foo = $TAINT;
314
315     if ($^O eq 'amigaos') {
316         print "# open(\"|\") is not available\n";
317         for (69..72) { print "ok $_\n" }
318     }
319     else {
320         test 69, eval { open FOO, "| $foo" } eq '', 'popen to';
321         test 70, $@ =~ /^Insecure dependency/, $@;
322
323         test 71, eval { open FOO, "$foo |" } eq '', 'popen from';
324         test 72, $@ =~ /^Insecure dependency/, $@;
325     }
326
327     test 73, eval { exec $TAINT } eq '', 'exec';
328     test 74, $@ =~ /^Insecure dependency/, $@;
329
330     test 75, eval { system $TAINT } eq '', 'system';
331     test 76, $@ =~ /^Insecure dependency/, $@;
332
333     $foo = "*";
334     taint_these $foo;
335
336     test 77, eval { `$echo 1$foo` } eq '', 'backticks';
337     test 78, $@ =~ /^Insecure dependency/, $@;
338
339     if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe
340         test 79, join('', eval { glob $foo } ) ne '', 'globbing';
341         test 80, $@ eq '', $@;
342     }
343     else {
344         test 79, join('', eval { glob $foo } ) eq '', 'globbing';
345         test 80, $@ =~ /^Insecure dependency/, $@;
346     }
347 }
348
349 # Operations which affect processes can't use tainted data.
350 {
351     test 81, eval { kill 0, $TAINT } eq '', 'kill';
352     test 82, $@ =~ /^Insecure dependency/, $@;
353
354     if ($Config{d_setpgrp}) {
355         test 83, eval { setpgrp 0, $TAINT } eq '', 'setpgrp';
356         test 84, $@ =~ /^Insecure dependency/, $@;
357     }
358     else {
359         print "# setpgrp() is not available\n";
360         for (83..84) { print "ok $_\n" }
361     }
362
363     if ($Config{d_setprior}) {
364         test 85, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority';
365         test 86, $@ =~ /^Insecure dependency/, $@;
366     }
367     else {
368         print "# setpriority() is not available\n";
369         for (85..86) { print "ok $_\n" }
370     }
371 }
372
373 # Some miscellaneous operations can't use tainted data.
374 {
375     if ($Config{d_syscall}) {
376         test 87, eval { syscall $TAINT } eq '', 'syscall';
377         test 88, $@ =~ /^Insecure dependency/, $@;
378     }
379     else {
380         print "# syscall() is not available\n";
381         for (87..88) { print "ok $_\n" }
382     }
383
384     {
385         my $foo = "x" x 979;
386         taint_these $foo;
387         local *FOO;
388         my $temp = "./taintC$$";
389         END { unlink $temp }
390         test 89, open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
391
392         test 90, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl';
393         test 91, $@ =~ /^Insecure dependency/, $@;
394
395         if ($Config{d_fcntl}) {
396             test 92, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl';
397             test 93, $@ =~ /^Insecure dependency/, $@;
398         }
399         else {
400             print "# fcntl() is not available\n";
401             for (92..93) { print "ok $_\n" }
402         }
403
404         close FOO;
405     }
406 }
407
408 # Some tests involving references 
409 {
410     my $foo = 'abc' . $TAINT;
411     my $fooref = \$foo;
412     test 94, not tainted $fooref;
413     test 95, tainted $$fooref;
414     test 96, tainted $foo;
415 }