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