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?
12 unshift @INC, '../lib' if -d '../lib';
18 # We do not want the whole taint.t to fail
19 # just because Errno possibly failing.
20 eval { require Errno; import Errno };
23 if ($^O eq 'VMS' && !defined($Config{d_setenv})) {
24 $ENV{PATH} = $ENV{PATH};
25 $ENV{TERM} = $ENV{TERM} ne ''? $ENV{TERM} : 'dummy';
27 if ($Config{d_shm} || $Config{d_msg}) {
29 IPC::SysV->import(qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU
34 my $Is_VMS = $^O eq 'VMS';
35 my $Is_MSWin32 = $^O eq 'MSWin32';
36 my $Is_Dos = $^O eq 'dos';
37 my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' :
38 $Is_MSWin32 ? '.\perl' : './perl';
39 my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
43 for $x ('DCL$PATH', @MoreEnv) {
44 ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
48 \$ENV{PATH} = '' if $Config{d_setenv};
49 warn "# Note: logical name 'PATH' may have been deleted\n";
50 \@ENV{keys %old} = values %old;
56 # The empty tainted value, for tainting strings
57 my $TAINT = substr($^X, 0, 0);
58 # A tainted zero, useful for tainting numbers
59 my $TAINT0 = 0 + $TAINT;
61 # This taints each argument passed. All must be lvalues.
62 # Side effect: It also stringifies them. :-(
64 for (@_) { $_ .= $TAINT }
67 # How to identify taint when you see it
69 not eval { join("",@_), kill 0; 1 };
75 for (@_) { return 0 unless tainted $_ }
80 my($serial, $boolean, $diag) = @_;
84 print "not ok $serial\n";
85 for (split m/^/m, $diag) {
90 or substr($diag, -1) eq "\n";
94 # We need an external program to call.
95 my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : "./echo$$");
97 open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
98 print PROG 'print "@ARGV\n"', "\n";
100 my $echo = "$Invoke_Perl $ECHO";
104 # First, let's make sure that Perl is checking the dangerous
105 # environment variables. Maybe they aren't set yet, so we'll
106 # taint them ourselves.
108 $ENV{'DCL$PATH'} = '' if $Is_VMS;
111 delete @ENV{@MoreEnv};
114 test 1, eval { `$echo 1` } eq "1\n";
116 if ($Is_MSWin32 || $Is_VMS || $Is_Dos) {
117 print "# Environment tainting tests skipped\n";
118 for (2..5) { print "ok $_\n" }
121 my @vars = ('PATH', @MoreEnv);
122 while (my $v = $vars[0]) {
123 local $ENV{$v} = $TAINT;
124 last if eval { `$echo 1` };
125 last unless $@ =~ /^Insecure \$ENV{$v}/;
128 test 2, !@vars, "\$$vars[0]";
130 # tainted $TERM is unsafe only if it contains metachars
132 $ENV{TERM} = 'e=mc2';
133 test 3, eval { `$echo 1` } eq "1\n";
134 $ENV{TERM} = 'e=mc2' . $TAINT;
135 test 4, eval { `$echo 1` } eq '';
136 test 5, $@ =~ /^Insecure \$ENV{TERM}/, $@;
140 if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_Dos) {
141 print "# all directories are writeable\n";
144 $tmp = (grep { defined and -d and (stat _)[2] & 2 }
145 qw(/tmp /var/tmp /usr/tmp /sys$scratch),
146 @ENV{qw(TMP TEMP)})[0]
147 or print "# can't find world-writeable directory to test PATH\n";
151 local $ENV{PATH} = $tmp;
152 test 6, eval { `$echo 1` } eq '';
153 test 7, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
156 for (6..7) { print "ok $_ # Skipped: all directories are writeable\n" }
160 $ENV{'DCL$PATH'} = $TAINT;
161 test 8, eval { `$echo 1` } eq '';
162 test 9, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
164 $ENV{'DCL$PATH'} = $tmp;
165 test 10, eval { `$echo 1` } eq '';
166 test 11, $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
169 for (10..11) { print "ok $_ # Skipped: can't find world-writeable directory to test DCL\$PATH\n" }
171 $ENV{'DCL$PATH'} = '';
174 for (8..11) { print "ok $_ # Skipped: This is not VMS\n"; }
178 # Let's see that we can taint and untaint as needed.
181 test 12, tainted $foo;
183 # That was a sanity check. If it failed, stop the insanity!
184 die "Taint checks don't seem to be enabled" unless tainted $foo;
187 test 13, not tainted $foo;
190 test 14, tainted $foo;
193 test 15, not any_tainted @list;
194 taint_these @list[1,3,5,7,9];
195 test 16, any_tainted @list;
196 test 17, all_tainted @list[1,3,5,7,9];
197 test 18, not any_tainted @list[0,2,4,6,8];
199 ($foo) = $foo =~ /(.+)/;
200 test 19, not tainted $foo;
202 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
203 test 20, not tainted $foo;
204 test 21, $foo eq 'bar';
209 ($foo) = ('bar' . $TAINT) =~ /(.+)/;
210 test 22, tainted $foo;
211 test 23, $foo eq 'bar';
213 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
214 test 24, tainted $foo;
215 test 25, $foo eq 'bar';
218 $foo = $1 if 'bar' =~ /(.+)$TAINT/;
219 test 26, tainted $foo;
220 test 27, $foo eq 'bar';
222 my $pi = 4 * atan2(1,1) + $TAINT0;
223 test 28, tainted $pi;
225 ($pi) = $pi =~ /(\d+\.\d+)/;
226 test 29, not tainted $pi;
227 test 30, sprintf("%.5f", $pi) eq '3.14159';
230 # How about command-line arguments? The problem is that we don't
231 # always get some, so we'll run another process with some.
234 open PROG, "> $arg" or die "Can't create $arg: $!";
236 eval { join('', @ARGV), kill 0 };
237 exit 0 if $@ =~ /^Insecure dependency/;
238 print "# Oops: \$@ was [$@]\n";
242 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
243 test 31, !$?, "Exited with status $?";
247 # Reading from a file should be tainted
250 test 32, open(FILE, $file), "Couldn't open '$file': $!";
253 sysread(FILE, $block, 100);
256 test 33, tainted $block;
257 test 34, tainted $line;
260 # Globs should be forbidden, except under VMS,
261 # which doesn't spawn an external program.
262 if (1 # built-in glob
264 for (35..36) { print "ok $_\n"; }
267 my @globs = eval { <*> };
268 test 35, @globs == 0 && $@ =~ /^Insecure dependency/;
270 @globs = eval { glob '*' };
271 test 36, @globs == 0 && $@ =~ /^Insecure dependency/;
274 # Output of commands should be tainted
276 my $foo = `$echo abc`;
277 test 37, tainted $foo;
280 # Certain system variables should be tainted
282 test 38, all_tainted $^X, $0;
285 # Results of matching should all be untainted
287 my $foo = "abcdefghi" . $TAINT;
288 test 39, tainted $foo;
291 test 40, not any_tainted $`, $&, $';
293 $foo =~ /(...)(...)(...)/;
294 test 41, not any_tainted $1, $2, $3, $+;
296 my @bar = $foo =~ /(...)(...)(...)/;
297 test 42, not any_tainted @bar;
299 test 43, tainted $foo; # $foo should still be tainted!
300 test 44, $foo eq "abcdefghi";
303 # Operations which affect files can't use tainted data.
305 test 45, eval { chmod 0, $TAINT } eq '', 'chmod';
306 test 46, $@ =~ /^Insecure dependency/, $@;
308 # There is no feature test in $Config{} for truncate,
309 # so we allow for the possibility that it's missing.
310 test 47, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate';
311 test 48, $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
313 test 49, eval { rename '', $TAINT } eq '', 'rename';
314 test 50, $@ =~ /^Insecure dependency/, $@;
316 test 51, eval { unlink $TAINT } eq '', 'unlink';
317 test 52, $@ =~ /^Insecure dependency/, $@;
319 test 53, eval { utime $TAINT } eq '', 'utime';
320 test 54, $@ =~ /^Insecure dependency/, $@;
322 if ($Config{d_chown}) {
323 test 55, eval { chown -1, -1, $TAINT } eq '', 'chown';
324 test 56, $@ =~ /^Insecure dependency/, $@;
327 for (55..56) { print "ok $_ # Skipped: chown() is not available\n" }
330 if ($Config{d_link}) {
331 test 57, eval { link $TAINT, '' } eq '', 'link';
332 test 58, $@ =~ /^Insecure dependency/, $@;
335 for (57..58) { print "ok $_ # Skipped: link() is not available\n" }
338 if ($Config{d_symlink}) {
339 test 59, eval { symlink $TAINT, '' } eq '', 'symlink';
340 test 60, $@ =~ /^Insecure dependency/, $@;
343 for (59..60) { print "ok $_ # Skipped: symlink() is not available\n" }
347 # Operations which affect directories can't use tainted data.
349 test 61, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir';
350 test 62, $@ =~ /^Insecure dependency/, $@;
352 test 63, eval { rmdir $TAINT } eq '', 'rmdir';
353 test 64, $@ =~ /^Insecure dependency/, $@;
355 test 65, eval { chdir $TAINT } eq '', 'chdir';
356 test 66, $@ =~ /^Insecure dependency/, $@;
358 if ($Config{d_chroot}) {
359 test 67, eval { chroot $TAINT } eq '', 'chroot';
360 test 68, $@ =~ /^Insecure dependency/, $@;
363 for (67..68) { print "ok $_ # Skipped: chroot() is not available\n" }
367 # Some operations using files can't use tainted data.
369 my $foo = "imaginary library" . $TAINT;
370 test 69, eval { require $foo } eq '', 'require';
371 test 70, $@ =~ /^Insecure dependency/, $@;
373 my $filename = "./taintB$$"; # NB: $filename isn't tainted!
374 END { unlink $filename if defined $filename }
375 $foo = $filename . $TAINT;
376 unlink $filename; # in any case
378 test 71, eval { open FOO, $foo } eq '', 'open for read';
379 test 72, $@ eq '', $@; # NB: This should be allowed
381 # Try first new style but allow also old style.
382 test 73, $!{ENOENT} ||
383 $! == 2 || # File not found
384 ($Is_Dos && $! == 22) ||
385 ($^O eq 'mint' && $! == 33);
387 test 74, eval { open FOO, "> $foo" } eq '', 'open for write';
388 test 75, $@ =~ /^Insecure dependency/, $@;
391 # Commands to the system can't use tainted data
395 if ($^O eq 'amigaos') {
396 for (76..79) { print "ok $_ # Skipped: open('|') is not available\n" }
399 test 76, eval { open FOO, "| x$foo" } eq '', 'popen to';
400 test 77, $@ =~ /^Insecure dependency/, $@;
402 test 78, eval { open FOO, "x$foo |" } eq '', 'popen from';
403 test 79, $@ =~ /^Insecure dependency/, $@;
406 test 80, eval { exec $TAINT } eq '', 'exec';
407 test 81, $@ =~ /^Insecure dependency/, $@;
409 test 82, eval { system $TAINT } eq '', 'system';
410 test 83, $@ =~ /^Insecure dependency/, $@;
415 test 84, eval { `$echo 1$foo` } eq '', 'backticks';
416 test 85, $@ =~ /^Insecure dependency/, $@;
418 if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe
419 test 86, join('', eval { glob $foo } ) ne '', 'globbing';
420 test 87, $@ eq '', $@;
423 for (86..87) { print "ok $_ # Skipped: this is not VMS\n"; }
427 # Operations which affect processes can't use tainted data.
429 test 88, eval { kill 0, $TAINT } eq '', 'kill';
430 test 89, $@ =~ /^Insecure dependency/, $@;
432 if ($Config{d_setpgrp}) {
433 test 90, eval { setpgrp 0, $TAINT } eq '', 'setpgrp';
434 test 91, $@ =~ /^Insecure dependency/, $@;
437 for (90..91) { print "ok $_ # Skipped: setpgrp() is not available\n" }
440 if ($Config{d_setprior}) {
441 test 92, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority';
442 test 93, $@ =~ /^Insecure dependency/, $@;
445 for (92..93) { print "ok $_ # Skipped: setpriority() is not available\n" }
449 # Some miscellaneous operations can't use tainted data.
451 if ($Config{d_syscall}) {
452 test 94, eval { syscall $TAINT } eq '', 'syscall';
453 test 95, $@ =~ /^Insecure dependency/, $@;
456 for (94..95) { print "ok $_ # Skipped: syscall() is not available\n" }
463 my $temp = "./taintC$$";
465 test 96, open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
467 test 97, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl';
468 test 98, $@ =~ /^Insecure dependency/, $@;
470 if ($Config{d_fcntl}) {
471 test 99, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl';
472 test 100, $@ =~ /^Insecure dependency/, $@;
475 for (99..100) { print "ok $_ # Skipped: fcntl() is not available\n" }
482 # Some tests involving references
484 my $foo = 'abc' . $TAINT;
486 test 101, not tainted $fooref;
487 test 102, tainted $$fooref;
488 test 103, tainted $foo;
491 # Some tests involving assignment
495 test 104, all_tainted $foo, $bar;
496 test 105, tainted($foo = $bar);
497 test 106, tainted($bar = $bar);
498 test 107, tainted($bar += $bar);
499 test 108, tainted($bar -= $bar);
500 test 109, tainted($bar *= $bar);
501 test 110, tainted($bar++);
502 test 111, tainted($bar /= $bar);
503 test 112, tainted($bar += 0);
504 test 113, tainted($bar -= 2);
505 test 114, tainted($bar *= -1);
506 test 115, tainted($bar /= 1);
507 test 116, tainted($bar--);
511 # Test assignment and return of lists
513 my @foo = ("A", "tainted" . $TAINT, "B");
514 test 118, not tainted $foo[0];
515 test 119, tainted $foo[1];
516 test 120, not tainted $foo[2];
518 test 121, not tainted $bar[0];
519 test 122, tainted $bar[1];
520 test 123, not tainted $bar[2];
521 my @baz = eval { "A", "tainted" . $TAINT, "B" };
522 test 124, not tainted $baz[0];
523 test 125, tainted $baz[1];
524 test 126, not tainted $baz[2];
525 my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
526 test 127, not tainted $plugh[0];
527 test 128, tainted $plugh[1];
528 test 129, not tainted $plugh[2];
529 my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
530 test 130, not tainted ((&$nautilus)[0]);
531 test 131, tainted ((&$nautilus)[1]);
532 test 132, not tainted ((&$nautilus)[2]);
533 my @xyzzy = &$nautilus;
534 test 133, not tainted $xyzzy[0];
535 test 134, tainted $xyzzy[1];
536 test 135, not tainted $xyzzy[2];
537 my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
538 test 136, not tainted ((&$red_october)[0]);
539 test 137, tainted ((&$red_october)[1]);
540 test 138, not tainted ((&$red_october)[2]);
541 my @corge = &$red_october;
542 test 139, not tainted $corge[0];
543 test 140, tainted $corge[1];
544 test 141, not tainted $corge[2];
547 # Test for system/library calls returning string data of dubious origin.
549 # No reliable %Config check for getpw*
550 if (eval { setpwent(); getpwent(); 1 }) {
552 my @getpwent = getpwent();
553 die "getpwent: $!\n" unless (@getpwent);
554 test 142,( not tainted $getpwent[0]
555 and tainted $getpwent[1]
556 and not tainted $getpwent[2]
557 and not tainted $getpwent[3]
558 and not tainted $getpwent[4]
559 and not tainted $getpwent[5]
560 and tainted $getpwent[6] # ge?cos
561 and not tainted $getpwent[7]
562 and tainted $getpwent[8]); # shell
565 for (142) { print "ok $_ # Skipped: getpwent() is not available\n" }
568 if ($Config{d_readdir}) { # pretty hard to imagine not
570 opendir(D, "op") or die "opendir: $!\n";
571 my $readdir = readdir(D);
572 test 143, tainted $readdir;
575 for (143) { print "ok $_ # Skipped: readdir() is not available\n" }
578 if ($Config{d_readlink} && $Config{d_symlink}) {
579 my $symlink = "sl$$";
581 symlink("/something/naughty", $symlink) or die "symlink: $!\n";
582 my $readlink = readlink($symlink);
583 test 144, tainted $readlink;
586 for (144) { print "ok $_ # Skipped: readlink() or symlink() is not available\n"; }
590 # test bitwise ops (regression bug)
594 test 145, not tainted $j;
597 test 146, tainted $j;
600 # test target of substitution (regression bug)
602 my $why = $TAINT."y";
604 test 147, tainted $why;
608 test 148, tainted $why;
610 $why =~ s/e/'-'.$$/ge;
611 test 149, tainted $why;
616 if ($Config{d_shm}) {
621 my $id = shmget(IPC_PRIVATE, $size, S_IRWXU|S_IRWXG|S_IRWXO) ||
622 warn "# shmget failed: $!\n";
624 if (shmwrite($id, $sent, 0, 60)) {
625 if (shmread($id, $rcvd, 0, 60)) {
626 substr($rcvd, index($rcvd, "\0")) = '';
628 warn "# shmread failed: $!\n";
631 warn "# shmwrite failed: $!\n";
633 shmctl($id, IPC_RMID, 0) || warn "# shmctl failed: $!\n";
635 warn "# shmget failed: $!\n";
638 if ($rcvd eq $sent) {
639 test 150, tainted $rcvd;
641 print "ok 150 # Skipped: SysV shared memory operation failed\n";
644 print "ok 150 # Skipped: SysV shared memory is not available\n";
650 if ($Config{d_msg}) {
652 my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
654 my $sent = "message";
655 my $type_sent = 1234;
660 if (msgsnd($id, pack("l! a*", $type_sent, $sent), 0)) {
661 if (msgrcv($id, $rcvd, 60, 0, 0)) {
662 ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd);
664 warn "# msgrcv failed\n";
667 warn "# msgsnd failed\n";
669 msgctl($id, IPC_RMID, 0) || warn "# msgctl failed: $!\n";
671 warn "# msgget failed\n";
674 if ($rcvd eq $sent && $type_sent == $type_rcvd) {
675 test 151, tainted $rcvd;
677 print "ok 151 # Skipped: SysV message queue operation failed\n";
680 print "ok 151 # Skipped: SysV message queues are not available\n";