Output skipped test information in test suite:
[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 $Is_MSWin32 = $^O eq 'MSWin32';
20 my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' :
21                   $Is_MSWin32 ? '.\perl' : './perl';
22 my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
23
24 if ($Is_VMS) {
25     my (%old, $x);
26     for $x ('DCL$PATH', @MoreEnv) {
27         ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
28     }
29     eval <<EndOfCleanup;
30         END {
31             \$ENV{PATH} = '';
32             warn "# Note: logical name 'PATH' may have been deleted\n";
33             @ENV{keys %old} = values %old;
34         }
35 EndOfCleanup
36 }
37
38 # Sources of taint:
39 #   The empty tainted value, for tainting strings
40 my $TAINT = substr($^X, 0, 0);
41 #   A tainted zero, useful for tainting numbers
42 my $TAINT0 = 0 + $TAINT;
43
44 # This taints each argument passed. All must be lvalues.
45 # Side effect: It also stringifies them. :-(
46 sub taint_these (@) {
47     for (@_) { $_ .= $TAINT }
48 }
49
50 # How to identify taint when you see it
51 sub any_tainted (@) {
52     not eval { join("",@_), kill 0; 1 };
53 }
54 sub tainted ($) {
55     any_tainted @_;
56 }
57 sub all_tainted (@) {
58     for (@_) { return 0 unless tainted $_ }
59     1;
60 }
61
62 sub test ($$;$) {
63     my($serial, $boolean, $diag) = @_;
64     if ($boolean) {
65         print "ok $serial\n";
66     } else {
67         print "not ok $serial\n";
68         for (split m/^/m, $diag) {
69             print "# $_";
70         }
71         print "\n" unless
72             $diag eq ''
73             or substr($diag, -1) eq "\n";
74     }
75 }
76
77 # We need an external program to call.
78 my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : "./echo$$");
79 END { unlink $ECHO }
80 open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
81 print PROG 'print "@ARGV\n"', "\n";
82 close PROG;
83 my $echo = "$Invoke_Perl $ECHO";
84
85 print "1..140\n";
86
87 # First, let's make sure that Perl is checking the dangerous
88 # environment variables. Maybe they aren't set yet, so we'll
89 # taint them ourselves.
90 {
91     $ENV{'DCL$PATH'} = '' if $Is_VMS;
92
93     $ENV{PATH} = '';
94     delete @ENV{@MoreEnv};
95     $ENV{TERM} = 'dumb';
96
97     test 1, eval { `$echo 1` } eq "1\n";
98
99     if ($Is_MSWin32 || $Is_VMS) {
100         print "# Environment tainting tests skipped\n";
101         for (2..5) { print "ok $_\n" }
102     }
103     else {
104         my @vars = ('PATH', @MoreEnv);
105         while (my $v = $vars[0]) {
106             local $ENV{$v} = $TAINT;
107             last if eval { `$echo 1` };
108             last unless $@ =~ /^Insecure \$ENV{$v}/;
109             shift @vars;
110         }
111         test 2, !@vars, "\$$vars[0]";
112
113         # tainted $TERM is unsafe only if it contains metachars
114         local $ENV{TERM};
115         $ENV{TERM} = 'e=mc2';
116         test 3, eval { `$echo 1` } eq "1\n";
117         $ENV{TERM} = 'e=mc2' . $TAINT;
118         test 4, eval { `$echo 1` } eq '';
119         test 5, $@ =~ /^Insecure \$ENV{TERM}/, $@;
120     }
121
122     my $tmp;
123     unless ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32) {
124         $tmp = (grep { defined and -d and (stat _)[2] & 2 }
125                      qw(/tmp /var/tmp /usr/tmp /sys$scratch),
126                      @ENV{qw(TMP TEMP)})[0]
127             or print "# can't find world-writeable directory to test PATH\n";
128     }
129
130     if ($tmp) {
131         local $ENV{PATH} = $tmp;
132         test 6, eval { `$echo 1` } eq '';
133         test 7, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
134     }
135     else {
136         for (6..7) { print "ok $_ # Skipped: all directories are writeable\n" }
137     }
138
139     if ($Is_VMS) {
140         $ENV{'DCL$PATH'} = $TAINT;
141         test 8,  eval { `$echo 1` } eq '';
142         test 9, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
143         if ($tmp) {
144             $ENV{'DCL$PATH'} = $tmp;
145             test 10, eval { `$echo 1` } eq '';
146             test 11, $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
147         }
148         else {
149             for (10..11) { print "ok $_ # Skipped: can't find world-writeable directory to test DCL\$PATH\n" }
150         }
151         $ENV{'DCL$PATH'} = '';
152     }
153     else {
154         for (8..11) { print "ok $_ # Skipped: This is not VMS\n"; }
155     }
156 }
157
158 # Let's see that we can taint and untaint as needed.
159 {
160     my $foo = $TAINT;
161     test 12, tainted $foo;
162
163     # That was a sanity check. If it failed, stop the insanity!
164     die "Taint checks don't seem to be enabled" unless tainted $foo;
165
166     $foo = "foo";
167     test 13, not tainted $foo;
168
169     taint_these($foo);
170     test 14, tainted $foo;
171
172     my @list = 1..10;
173     test 15, not any_tainted @list;
174     taint_these @list[1,3,5,7,9];
175     test 16, any_tainted @list;
176     test 17, all_tainted @list[1,3,5,7,9];
177     test 18, not any_tainted @list[0,2,4,6,8];
178
179     ($foo) = $foo =~ /(.+)/;
180     test 19, not tainted $foo;
181
182     $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
183     test 20, not tainted $foo;
184     test 21, $foo eq 'bar';
185
186     my $pi = 4 * atan2(1,1) + $TAINT0;
187     test 22, tainted $pi;
188
189     ($pi) = $pi =~ /(\d+\.\d+)/;
190     test 23, not tainted $pi;
191     test 24, sprintf("%.5f", $pi) eq '3.14159';
192 }
193
194 # How about command-line arguments? The problem is that we don't
195 # always get some, so we'll run another process with some.
196 {
197     my $arg = "./arg$$";
198     open PROG, "> $arg" or die "Can't create $arg: $!";
199     print PROG q{
200         eval { join('', @ARGV), kill 0 };
201         exit 0 if $@ =~ /^Insecure dependency/;
202         print "# Oops: \$@ was [$@]\n";
203         exit 1;
204     };
205     close PROG;
206     print `$Invoke_Perl "-T" $arg and some suspect arguments`;
207     test 25, !$?, "Exited with status $?";
208     unlink $arg;
209 }
210
211 # Reading from a file should be tainted
212 {
213     my $file = './TEST';
214     test 26, open(FILE, $file), "Couldn't open '$file': $!";
215
216     my $block;
217     sysread(FILE, $block, 100);
218     my $line = <FILE>;
219     close FILE;
220     test 27, tainted $block;
221     test 28, tainted $line;
222 }
223
224 # Globs should be forbidden, except under VMS,
225 #   which doesn't spawn an external program.
226 if ($Is_VMS) {
227     for (29..30) { print "ok $_\n"; }
228 }
229 else {
230     my @globs = eval { <*> };
231     test 29, @globs == 0 && $@ =~ /^Insecure dependency/;
232
233     @globs = eval { glob '*' };
234     test 30, @globs == 0 && $@ =~ /^Insecure dependency/;
235 }
236
237 # Output of commands should be tainted
238 {
239     my $foo = `$echo abc`;
240     test 31, tainted $foo;
241 }
242
243 # Certain system variables should be tainted
244 {
245     test 32, all_tainted $^X, $0;
246 }
247
248 # Results of matching should all be untainted
249 {
250     my $foo = "abcdefghi" . $TAINT;
251     test 33, tainted $foo;
252
253     $foo =~ /def/;
254     test 34, not any_tainted $`, $&, $';
255
256     $foo =~ /(...)(...)(...)/;
257     test 35, not any_tainted $1, $2, $3, $+;
258
259     my @bar = $foo =~ /(...)(...)(...)/;
260     test 36, not any_tainted @bar;
261
262     test 37, tainted $foo;      # $foo should still be tainted!
263     test 38, $foo eq "abcdefghi";
264 }
265
266 # Operations which affect files can't use tainted data.
267 {
268     test 39, eval { chmod 0, $TAINT } eq '', 'chmod';
269     test 40, $@ =~ /^Insecure dependency/, $@;
270
271     # There is no feature test in $Config{} for truncate,
272     #   so we allow for the possibility that it's missing.
273     test 41, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate';
274     test 42, $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
275
276     test 43, eval { rename '', $TAINT } eq '', 'rename';
277     test 44, $@ =~ /^Insecure dependency/, $@;
278
279     test 45, eval { unlink $TAINT } eq '', 'unlink';
280     test 46, $@ =~ /^Insecure dependency/, $@;
281
282     test 47, eval { utime $TAINT } eq '', 'utime';
283     test 48, $@ =~ /^Insecure dependency/, $@;
284
285     if ($Config{d_chown}) {
286         test 49, eval { chown -1, -1, $TAINT } eq '', 'chown';
287         test 50, $@ =~ /^Insecure dependency/, $@;
288     }
289     else {
290         for (49..50) { print "ok $_ # Skipped: chown() is not available\n" }
291     }
292
293     if ($Config{d_link}) {
294         test 51, eval { link $TAINT, '' } eq '', 'link';
295         test 52, $@ =~ /^Insecure dependency/, $@;
296     }
297     else {
298         for (51..52) { print "ok $_ # Skipped: link() is not available\n" }
299     }
300
301     if ($Config{d_symlink}) {
302         test 53, eval { symlink $TAINT, '' } eq '', 'symlink';
303         test 54, $@ =~ /^Insecure dependency/, $@;
304     }
305     else {
306         for (53..54) { print "ok $_ # Skipped: symlink() is not available\n" }
307     }
308 }
309
310 # Operations which affect directories can't use tainted data.
311 {
312     test 55, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir';
313     test 56, $@ =~ /^Insecure dependency/, $@;
314
315     test 57, eval { rmdir $TAINT } eq '', 'rmdir';
316     test 58, $@ =~ /^Insecure dependency/, $@;
317
318     test 59, eval { chdir $TAINT } eq '', 'chdir';
319     test 60, $@ =~ /^Insecure dependency/, $@;
320
321     if ($Config{d_chroot}) {
322         test 61, eval { chroot $TAINT } eq '', 'chroot';
323         test 62, $@ =~ /^Insecure dependency/, $@;
324     }
325     else {
326         for (61..62) { print "ok $_ # Skipped: chroot() is not available\n" }
327     }
328 }
329
330 # Some operations using files can't use tainted data.
331 {
332     my $foo = "imaginary library" . $TAINT;
333     test 63, eval { require $foo } eq '', 'require';
334     test 64, $@ =~ /^Insecure dependency/, $@;
335
336     my $filename = "./taintB$$";        # NB: $filename isn't tainted!
337     END { unlink $filename if defined $filename }
338     $foo = $filename . $TAINT;
339     unlink $filename;   # in any case
340
341     test 65, eval { open FOO, $foo } eq '', 'open for read';
342     test 66, $@ eq '', $@;              # NB: This should be allowed
343     test 67, $! == 2;                   # File not found
344
345     test 68, eval { open FOO, "> $foo" } eq '', 'open for write';
346     test 69, $@ =~ /^Insecure dependency/, $@;
347 }
348
349 # Commands to the system can't use tainted data
350 {
351     my $foo = $TAINT;
352
353     if ($^O eq 'amigaos') {
354         for (70..73) { print "ok $_ # Skipped: open('|') is not available\n" }
355     }
356     else {
357         test 70, eval { open FOO, "| $foo" } eq '', 'popen to';
358         test 71, $@ =~ /^Insecure dependency/, $@;
359
360         test 72, eval { open FOO, "$foo |" } eq '', 'popen from';
361         test 73, $@ =~ /^Insecure dependency/, $@;
362     }
363
364     test 74, eval { exec $TAINT } eq '', 'exec';
365     test 75, $@ =~ /^Insecure dependency/, $@;
366
367     test 76, eval { system $TAINT } eq '', 'system';
368     test 77, $@ =~ /^Insecure dependency/, $@;
369
370     $foo = "*";
371     taint_these $foo;
372
373     test 78, eval { `$echo 1$foo` } eq '', 'backticks';
374     test 79, $@ =~ /^Insecure dependency/, $@;
375
376     if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe
377         test 80, join('', eval { glob $foo } ) ne '', 'globbing';
378         test 81, $@ eq '', $@;
379     }
380     else {
381         for (80..81) { print "ok $_ # Skipped: this is not VMS\n"; }
382     }
383 }
384
385 # Operations which affect processes can't use tainted data.
386 {
387     test 82, eval { kill 0, $TAINT } eq '', 'kill';
388     test 83, $@ =~ /^Insecure dependency/, $@;
389
390     if ($Config{d_setpgrp}) {
391         test 84, eval { setpgrp 0, $TAINT } eq '', 'setpgrp';
392         test 85, $@ =~ /^Insecure dependency/, $@;
393     }
394     else {
395         for (84..85) { print "ok $_ # Skipped: setpgrp() is not available\n" }
396     }
397
398     if ($Config{d_setprior}) {
399         test 86, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority';
400         test 87, $@ =~ /^Insecure dependency/, $@;
401     }
402     else {
403         for (86..87) { print "ok $_ # Skipped: setpriority() is not available\n" }
404     }
405 }
406
407 # Some miscellaneous operations can't use tainted data.
408 {
409     if ($Config{d_syscall}) {
410         test 88, eval { syscall $TAINT } eq '', 'syscall';
411         test 89, $@ =~ /^Insecure dependency/, $@;
412     }
413     else {
414         for (88..89) { print "ok $_ # Skipped: syscall() is not available\n" }
415     }
416
417     {
418         my $foo = "x" x 979;
419         taint_these $foo;
420         local *FOO;
421         my $temp = "./taintC$$";
422         END { unlink $temp }
423         test 90, open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
424
425         test 91, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl';
426         test 92, $@ =~ /^Insecure dependency/, $@;
427
428         if ($Config{d_fcntl}) {
429             test 93, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl';
430             test 94, $@ =~ /^Insecure dependency/, $@;
431         }
432         else {
433             for (93..94) { print "ok $_ # Skipped: fcntl() is not available\n" }
434         }
435
436         close FOO;
437     }
438 }
439
440 # Some tests involving references
441 {
442     my $foo = 'abc' . $TAINT;
443     my $fooref = \$foo;
444     test 95, not tainted $fooref;
445     test 96, tainted $$fooref;
446     test 97, tainted $foo;
447 }
448
449 # Some tests involving assignment
450 {
451     my $foo = $TAINT0;
452     my $bar = $foo;
453     test 98, all_tainted $foo, $bar;
454     test 99, tainted($foo = $bar);
455     test 100, tainted($bar = $bar);
456     test 101, tainted($bar += $bar);
457     test 102, tainted($bar -= $bar);
458     test 103, tainted($bar *= $bar);
459     test 104, tainted($bar++);
460     test 105, tainted($bar /= $bar);
461     test 106, tainted($bar += 0);
462     test 107, tainted($bar -= 2);
463     test 108, tainted($bar *= -1);
464     test 109, tainted($bar /= 1);
465     test 110, tainted($bar--);
466     test 111, $bar == 0;
467 }
468
469 # Test assignment and return of lists
470 {
471     my @foo = ("A", "tainted" . $TAINT, "B");
472     test 112, not tainted $foo[0];
473     test 113,     tainted $foo[1];
474     test 114, not tainted $foo[2];
475     my @bar = @foo;
476     test 115, not tainted $bar[0];
477     test 116,     tainted $bar[1];
478     test 117, not tainted $bar[2];
479     my @baz = eval { "A", "tainted" . $TAINT, "B" };
480     test 118, not tainted $baz[0];
481     test 119,     tainted $baz[1];
482     test 120, not tainted $baz[2];
483     my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
484     test 121, not tainted $plugh[0];
485     test 122,     tainted $plugh[1];
486     test 123, not tainted $plugh[2];
487     my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
488     test 124, not tainted ((&$nautilus)[0]);
489     test 125,     tainted ((&$nautilus)[1]);
490     test 126, not tainted ((&$nautilus)[2]);
491     my @xyzzy = &$nautilus;
492     test 127, not tainted $xyzzy[0];
493     test 128,     tainted $xyzzy[1];
494     test 129, not tainted $xyzzy[2];
495     my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
496     test 130, not tainted ((&$red_october)[0]);
497     test 131,     tainted ((&$red_october)[1]);
498     test 132, not tainted ((&$red_october)[2]);
499     my @corge = &$red_october;
500     test 133, not tainted $corge[0];
501     test 134,     tainted $corge[1];
502     test 135, not tainted $corge[2];
503 }
504
505 # Test for system/library calls returning string data of dubious origin.
506 {
507     # No reliable %Config check for getpw*
508     if (eval { setpwent(); getpwent(); 1 }) {
509         setpwent();
510         my @getpwent = getpwent();
511         die "getpwent: $!\n" unless (@getpwent);
512         test 136,(    not tainted $getpwent[0]
513                   and not tainted $getpwent[1]
514                   and not tainted $getpwent[2]
515                   and not tainted $getpwent[3]
516                   and not tainted $getpwent[4]
517                   and not tainted $getpwent[5]
518                   and     tainted $getpwent[6] # gecos
519                   and not tainted $getpwent[7]
520                   and not tainted $getpwent[8]);
521         endpwent();
522     } else {
523         print "ok 136 # Skipped: getpwent() is not available\n";
524     }
525
526     if ($Config{d_readdir}) { # pretty hard to imagine not
527         local(*D);
528         opendir(D, "op") or die "opendir: $!\n";
529         my $readdir = readdir(D);
530         test 137, tainted $readdir;
531         closedir(OP);
532     } else {
533         print "ok 137 # Skipped: readdir() is not available\n";
534     }
535
536     if ($Config{d_readlink} && $Config{d_symlink}) {
537         my $symlink = "sl$$";
538         unlink($symlink);
539         symlink("/something/naughty", $symlink) or die "symlink: $!\n";
540         my $readlink = readlink($symlink);
541         test 138, tainted $readlink;
542         unlink($symlink);
543     } else {
544         print "ok 138 # Skipped: readlink() or symlink() is not available\n";
545     }
546 }
547
548 # test bitwise ops (regression bug)
549 {
550     my $why = "y";
551     my $j = "x" | $why;
552     test 139, not tainted $j;
553     $why = $TAINT."y";
554     $j = "x" | $why;
555     test 140,     tainted $j;
556 }
557