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