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