3 # Taint tests by Tom Phoenix <rootbeer@teleport.com>.
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?
18 # We do not want the whole taint.t to fail
19 # just because Errno possibly failing.
20 eval { require Errno; import Errno };
22 use vars qw($ipcsysv); # did we manage to load IPC::SysV?
25 if ($^O eq 'VMS' && !defined($Config{d_setenv})) {
26 $ENV{PATH} = $ENV{PATH};
27 $ENV{TERM} = $ENV{TERM} ne ''? $ENV{TERM} : 'dummy';
29 if ($Config{'extensions'} =~ /\bIPC\/SysV\b/
30 && ($Config{d_shm} || $Config{d_msg})) {
31 eval { require IPC::SysV };
34 IPC::SysV->import(qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU));
39 my $Is_VMS = $^O eq 'VMS';
40 my $Is_MSWin32 = $^O eq 'MSWin32';
41 my $Is_Dos = $^O eq 'dos';
42 my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' :
43 $Is_MSWin32 ? '.\perl' : './perl';
44 my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
48 for $x ('DCL$PATH', @MoreEnv) {
49 ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
53 \$ENV{PATH} = '' if $Config{d_setenv};
54 warn "# Note: logical name 'PATH' may have been deleted\n";
55 \@ENV{keys %old} = values %old;
61 # The empty tainted value, for tainting strings
62 my $TAINT = substr($^X, 0, 0);
63 # A tainted zero, useful for tainting numbers
64 my $TAINT0 = 0 + $TAINT;
66 # This taints each argument passed. All must be lvalues.
67 # Side effect: It also stringifies them. :-(
69 for (@_) { $_ .= $TAINT }
72 # How to identify taint when you see it
74 not eval { join("",@_), kill 0; 1 };
80 for (@_) { return 0 unless tainted $_ }
85 my($serial, $boolean, $diag) = @_;
89 print "not ok $serial\n";
90 for (split m/^/m, $diag) {
95 or substr($diag, -1) eq "\n";
99 # We need an external program to call.
100 my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : "./echo$$");
102 open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
103 print PROG 'print "@ARGV\n"', "\n";
105 my $echo = "$Invoke_Perl $ECHO";
109 # First, let's make sure that Perl is checking the dangerous
110 # environment variables. Maybe they aren't set yet, so we'll
111 # taint them ourselves.
113 $ENV{'DCL$PATH'} = '' if $Is_VMS;
116 delete @ENV{@MoreEnv};
119 test 1, eval { `$echo 1` } eq "1\n";
121 if ($Is_MSWin32 || $Is_VMS || $Is_Dos) {
122 print "# Environment tainting tests skipped\n";
123 for (2..5) { print "ok $_\n" }
126 my @vars = ('PATH', @MoreEnv);
127 while (my $v = $vars[0]) {
128 local $ENV{$v} = $TAINT;
129 last if eval { `$echo 1` };
130 last unless $@ =~ /^Insecure \$ENV{$v}/;
133 test 2, !@vars, "\$$vars[0]";
135 # tainted $TERM is unsafe only if it contains metachars
137 $ENV{TERM} = 'e=mc2';
138 test 3, eval { `$echo 1` } eq "1\n";
139 $ENV{TERM} = 'e=mc2' . $TAINT;
140 test 4, eval { `$echo 1` } eq '';
141 test 5, $@ =~ /^Insecure \$ENV{TERM}/, $@;
145 if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_Dos) {
146 print "# all directories are writeable\n";
149 $tmp = (grep { defined and -d and (stat _)[2] & 2 }
150 qw(sys$scratch /tmp /var/tmp /usr/tmp),
151 @ENV{qw(TMP TEMP)})[0]
152 or print "# can't find world-writeable directory to test PATH\n";
156 local $ENV{PATH} = $tmp;
157 test 6, eval { `$echo 1` } eq '';
158 test 7, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
161 for (6..7) { print "ok $_ # Skipped: all directories are writeable\n" }
165 $ENV{'DCL$PATH'} = $TAINT;
166 test 8, eval { `$echo 1` } eq '';
167 test 9, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
169 $ENV{'DCL$PATH'} = $tmp;
170 test 10, eval { `$echo 1` } eq '';
171 test 11, $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
174 for (10..11) { print "ok $_ # Skipped: can't find world-writeable directory to test DCL\$PATH\n" }
176 $ENV{'DCL$PATH'} = '';
179 for (8..11) { print "ok $_ # Skipped: This is not VMS\n"; }
183 # Let's see that we can taint and untaint as needed.
186 test 12, tainted $foo;
188 # That was a sanity check. If it failed, stop the insanity!
189 die "Taint checks don't seem to be enabled" unless tainted $foo;
192 test 13, not tainted $foo;
195 test 14, tainted $foo;
198 test 15, not any_tainted @list;
199 taint_these @list[1,3,5,7,9];
200 test 16, any_tainted @list;
201 test 17, all_tainted @list[1,3,5,7,9];
202 test 18, not any_tainted @list[0,2,4,6,8];
204 ($foo) = $foo =~ /(.+)/;
205 test 19, not tainted $foo;
207 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
208 test 20, not tainted $foo;
209 test 21, $foo eq 'bar';
214 ($foo) = ('bar' . $TAINT) =~ /(.+)/;
215 test 22, tainted $foo;
216 test 23, $foo eq 'bar';
218 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
219 test 24, tainted $foo;
220 test 25, $foo eq 'bar';
223 $foo = $1 if 'bar' =~ /(.+)$TAINT/;
224 test 26, tainted $foo;
225 test 27, $foo eq 'bar';
227 my $pi = 4 * atan2(1,1) + $TAINT0;
228 test 28, tainted $pi;
230 ($pi) = $pi =~ /(\d+\.\d+)/;
231 test 29, not tainted $pi;
232 test 30, sprintf("%.5f", $pi) eq '3.14159';
235 # How about command-line arguments? The problem is that we don't
236 # always get some, so we'll run another process with some.
239 open PROG, "> $arg" or die "Can't create $arg: $!";
241 eval { join('', @ARGV), kill 0 };
242 exit 0 if $@ =~ /^Insecure dependency/;
243 print "# Oops: \$@ was [$@]\n";
247 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
248 test 31, !$?, "Exited with status $?";
252 # Reading from a file should be tainted
255 test 32, open(FILE, $file), "Couldn't open '$file': $!";
258 sysread(FILE, $block, 100);
261 test 33, tainted $block;
262 test 34, tainted $line;
265 # Globs should be forbidden, except under VMS,
266 # which doesn't spawn an external program.
267 if (1 # built-in glob
269 for (35..36) { print "ok $_\n"; }
272 my @globs = eval { <*> };
273 test 35, @globs == 0 && $@ =~ /^Insecure dependency/;
275 @globs = eval { glob '*' };
276 test 36, @globs == 0 && $@ =~ /^Insecure dependency/;
279 # Output of commands should be tainted
281 my $foo = `$echo abc`;
282 test 37, tainted $foo;
285 # Certain system variables should be tainted
287 test 38, all_tainted $^X, $0;
290 # Results of matching should all be untainted
292 my $foo = "abcdefghi" . $TAINT;
293 test 39, tainted $foo;
296 test 40, not any_tainted $`, $&, $';
298 $foo =~ /(...)(...)(...)/;
299 test 41, not any_tainted $1, $2, $3, $+;
301 my @bar = $foo =~ /(...)(...)(...)/;
302 test 42, not any_tainted @bar;
304 test 43, tainted $foo; # $foo should still be tainted!
305 test 44, $foo eq "abcdefghi";
308 # Operations which affect files can't use tainted data.
310 test 45, eval { chmod 0, $TAINT } eq '', 'chmod';
311 test 46, $@ =~ /^Insecure dependency/, $@;
313 # There is no feature test in $Config{} for truncate,
314 # so we allow for the possibility that it's missing.
315 test 47, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate';
316 test 48, $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
318 test 49, eval { rename '', $TAINT } eq '', 'rename';
319 test 50, $@ =~ /^Insecure dependency/, $@;
321 test 51, eval { unlink $TAINT } eq '', 'unlink';
322 test 52, $@ =~ /^Insecure dependency/, $@;
324 test 53, eval { utime $TAINT } eq '', 'utime';
325 test 54, $@ =~ /^Insecure dependency/, $@;
327 if ($Config{d_chown}) {
328 test 55, eval { chown -1, -1, $TAINT } eq '', 'chown';
329 test 56, $@ =~ /^Insecure dependency/, $@;
332 for (55..56) { print "ok $_ # Skipped: chown() is not available\n" }
335 if ($Config{d_link}) {
336 test 57, eval { link $TAINT, '' } eq '', 'link';
337 test 58, $@ =~ /^Insecure dependency/, $@;
340 for (57..58) { print "ok $_ # Skipped: link() is not available\n" }
343 if ($Config{d_symlink}) {
344 test 59, eval { symlink $TAINT, '' } eq '', 'symlink';
345 test 60, $@ =~ /^Insecure dependency/, $@;
348 for (59..60) { print "ok $_ # Skipped: symlink() is not available\n" }
352 # Operations which affect directories can't use tainted data.
354 test 61, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir';
355 test 62, $@ =~ /^Insecure dependency/, $@;
357 test 63, eval { rmdir $TAINT } eq '', 'rmdir';
358 test 64, $@ =~ /^Insecure dependency/, $@;
360 test 65, eval { chdir $TAINT } eq '', 'chdir';
361 test 66, $@ =~ /^Insecure dependency/, $@;
363 if ($Config{d_chroot}) {
364 test 67, eval { chroot $TAINT } eq '', 'chroot';
365 test 68, $@ =~ /^Insecure dependency/, $@;
368 for (67..68) { print "ok $_ # Skipped: chroot() is not available\n" }
372 # Some operations using files can't use tainted data.
374 my $foo = "imaginary library" . $TAINT;
375 test 69, eval { require $foo } eq '', 'require';
376 test 70, $@ =~ /^Insecure dependency/, $@;
378 my $filename = "./taintB$$"; # NB: $filename isn't tainted!
379 END { unlink $filename if defined $filename }
380 $foo = $filename . $TAINT;
381 unlink $filename; # in any case
383 test 71, eval { open FOO, $foo } eq '', 'open for read';
384 test 72, $@ eq '', $@; # NB: This should be allowed
386 # Try first new style but allow also old style.
387 test 73, $!{ENOENT} ||
388 $! == 2 || # File not found
389 ($Is_Dos && $! == 22) ||
390 ($^O eq 'mint' && $! == 33);
392 test 74, eval { open FOO, "> $foo" } eq '', 'open for write';
393 test 75, $@ =~ /^Insecure dependency/, $@;
396 # Commands to the system can't use tainted data
400 if ($^O eq 'amigaos') {
401 for (76..79) { print "ok $_ # Skipped: open('|') is not available\n" }
404 test 76, eval { open FOO, "| x$foo" } eq '', 'popen to';
405 test 77, $@ =~ /^Insecure dependency/, $@;
407 test 78, eval { open FOO, "x$foo |" } eq '', 'popen from';
408 test 79, $@ =~ /^Insecure dependency/, $@;
411 test 80, eval { exec $TAINT } eq '', 'exec';
412 test 81, $@ =~ /^Insecure dependency/, $@;
414 test 82, eval { system $TAINT } eq '', 'system';
415 test 83, $@ =~ /^Insecure dependency/, $@;
420 test 84, eval { `$echo 1$foo` } eq '', 'backticks';
421 test 85, $@ =~ /^Insecure dependency/, $@;
423 if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe
424 test 86, join('', eval { glob $foo } ) ne '', 'globbing';
425 test 87, $@ eq '', $@;
428 for (86..87) { print "ok $_ # Skipped: this is not VMS\n"; }
432 # Operations which affect processes can't use tainted data.
434 test 88, eval { kill 0, $TAINT } eq '', 'kill';
435 test 89, $@ =~ /^Insecure dependency/, $@;
437 if ($Config{d_setpgrp}) {
438 test 90, eval { setpgrp 0, $TAINT } eq '', 'setpgrp';
439 test 91, $@ =~ /^Insecure dependency/, $@;
442 for (90..91) { print "ok $_ # Skipped: setpgrp() is not available\n" }
445 if ($Config{d_setprior}) {
446 test 92, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority';
447 test 93, $@ =~ /^Insecure dependency/, $@;
450 for (92..93) { print "ok $_ # Skipped: setpriority() is not available\n" }
454 # Some miscellaneous operations can't use tainted data.
456 if ($Config{d_syscall}) {
457 test 94, eval { syscall $TAINT } eq '', 'syscall';
458 test 95, $@ =~ /^Insecure dependency/, $@;
461 for (94..95) { print "ok $_ # Skipped: syscall() is not available\n" }
468 my $temp = "./taintC$$";
470 test 96, open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
472 test 97, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl';
473 test 98, $@ =~ /^Insecure dependency/, $@;
475 if ($Config{d_fcntl}) {
476 test 99, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl';
477 test 100, $@ =~ /^Insecure dependency/, $@;
480 for (99..100) { print "ok $_ # Skipped: fcntl() is not available\n" }
487 # Some tests involving references
489 my $foo = 'abc' . $TAINT;
491 test 101, not tainted $fooref;
492 test 102, tainted $$fooref;
493 test 103, tainted $foo;
496 # Some tests involving assignment
500 test 104, all_tainted $foo, $bar;
501 test 105, tainted($foo = $bar);
502 test 106, tainted($bar = $bar);
503 test 107, tainted($bar += $bar);
504 test 108, tainted($bar -= $bar);
505 test 109, tainted($bar *= $bar);
506 test 110, tainted($bar++);
507 test 111, tainted($bar /= $bar);
508 test 112, tainted($bar += 0);
509 test 113, tainted($bar -= 2);
510 test 114, tainted($bar *= -1);
511 test 115, tainted($bar /= 1);
512 test 116, tainted($bar--);
516 # Test assignment and return of lists
518 my @foo = ("A", "tainted" . $TAINT, "B");
519 test 118, not tainted $foo[0];
520 test 119, tainted $foo[1];
521 test 120, not tainted $foo[2];
523 test 121, not tainted $bar[0];
524 test 122, tainted $bar[1];
525 test 123, not tainted $bar[2];
526 my @baz = eval { "A", "tainted" . $TAINT, "B" };
527 test 124, not tainted $baz[0];
528 test 125, tainted $baz[1];
529 test 126, not tainted $baz[2];
530 my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
531 test 127, not tainted $plugh[0];
532 test 128, tainted $plugh[1];
533 test 129, not tainted $plugh[2];
534 my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
535 test 130, not tainted ((&$nautilus)[0]);
536 test 131, tainted ((&$nautilus)[1]);
537 test 132, not tainted ((&$nautilus)[2]);
538 my @xyzzy = &$nautilus;
539 test 133, not tainted $xyzzy[0];
540 test 134, tainted $xyzzy[1];
541 test 135, not tainted $xyzzy[2];
542 my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
543 test 136, not tainted ((&$red_october)[0]);
544 test 137, tainted ((&$red_october)[1]);
545 test 138, not tainted ((&$red_october)[2]);
546 my @corge = &$red_october;
547 test 139, not tainted $corge[0];
548 test 140, tainted $corge[1];
549 test 141, not tainted $corge[2];
552 # Test for system/library calls returning string data of dubious origin.
554 # No reliable %Config check for getpw*
555 if (eval { setpwent(); getpwent(); 1 }) {
557 my @getpwent = getpwent();
558 die "getpwent: $!\n" unless (@getpwent);
559 test 142,( not tainted $getpwent[0]
560 and tainted $getpwent[1]
561 and not tainted $getpwent[2]
562 and not tainted $getpwent[3]
563 and not tainted $getpwent[4]
564 and not tainted $getpwent[5]
565 and tainted $getpwent[6] # ge?cos
566 and not tainted $getpwent[7]
567 and tainted $getpwent[8]); # shell
570 for (142) { print "ok $_ # Skipped: getpwent() is not available\n" }
573 if ($Config{d_readdir}) { # pretty hard to imagine not
575 opendir(D, "op") or die "opendir: $!\n";
576 my $readdir = readdir(D);
577 test 143, tainted $readdir;
580 for (143) { print "ok $_ # Skipped: readdir() is not available\n" }
583 if ($Config{d_readlink} && $Config{d_symlink}) {
584 my $symlink = "sl$$";
586 symlink("/something/naughty", $symlink) or die "symlink: $!\n";
587 my $readlink = readlink($symlink);
588 test 144, tainted $readlink;
591 for (144) { print "ok $_ # Skipped: readlink() or symlink() is not available\n"; }
595 # test bitwise ops (regression bug)
599 test 145, not tainted $j;
602 test 146, tainted $j;
605 # test target of substitution (regression bug)
607 my $why = $TAINT."y";
609 test 147, tainted $why;
613 test 148, tainted $why;
615 $why =~ s/e/'-'.$$/ge;
616 test 149, tainted $why;
622 print "ok 150 # skipped: no IPC::SysV\n";
625 if ($Config{'extensions'} =~ /\bIPC\/SysV\b/ && $Config{d_shm}) {
630 my $id = shmget(IPC_PRIVATE, $size, S_IRWXU);
633 if (shmwrite($id, $sent, 0, 60)) {
634 if (shmread($id, $rcvd, 0, 60)) {
635 substr($rcvd, index($rcvd, "\0")) = '';
637 warn "# shmread failed: $!\n";
640 warn "# shmwrite failed: $!\n";
642 shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n";
644 warn "# shmget failed: $!\n";
647 if ($rcvd eq $sent) {
648 test 150, tainted $rcvd;
650 print "ok 150 # Skipped: SysV shared memory operation failed\n";
653 print "ok 150 # Skipped: SysV shared memory is not available\n";
660 print "ok 151 # skipped: no IPC::SysV\n";
663 if ($Config{'extensions'} =~ /\bIPC\/SysV\b/ && $Config{d_msg}) {
665 my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
667 my $sent = "message";
668 my $type_sent = 1234;
673 if (msgsnd($id, pack("l! a*", $type_sent, $sent), 0)) {
674 if (msgrcv($id, $rcvd, 60, 0, 0)) {
675 ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd);
677 warn "# msgrcv failed\n";
680 warn "# msgsnd failed\n";
682 msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n";
684 warn "# msgget failed\n";
687 if ($rcvd eq $sent && $type_sent == $type_rcvd) {
688 test 151, tainted $rcvd;
690 print "ok 151 # Skipped: SysV message queue operation failed\n";
693 print "ok 151 # Skipped: SysV message queues are not available\n";
698 # bug id 20001004.006
700 open IN, "./TEST" or warn "$0: cannot read ./TEST: $!" ;
704 print "not " unless tainted($a) && tainted($b) && !defined($b);
710 # bug id 20001004.007
712 open IN, "./TEST" or warn "$0: cannot read ./TEST: $!" ;
717 print "not " unless !tainted($c->{a}) && tainted($c->{b});
722 print "not " unless tainted($d->{a}) && !tainted($d->{b});
726 b => { c => $a, d => 42 } };
727 print "not " unless !tainted($e->{a}) &&
729 tainted($e->{b}->{c}) &&
730 !tainted($e->{b}->{d});