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 @INC = '../lib' if -d '../lib';
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/;
26 for $x ('DCL$PATH', @MoreEnv) {
27 ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
32 warn "# Note: logical name 'PATH' may have been deleted\n";
33 @ENV{keys %old} = values %old;
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;
44 # This taints each argument passed. All must be lvalues.
45 # Side effect: It also stringifies them. :-(
47 for (@_) { $_ .= $TAINT }
50 # How to identify taint when you see it
52 not eval { join("",@_), kill 0; 1 };
58 for (@_) { return 0 unless tainted $_ }
63 my($serial, $boolean, $diag) = @_;
67 print "not ok $serial\n";
68 for (split m/^/m, $diag) {
73 or substr($diag, -1) eq "\n";
77 # We need an external program to call.
78 my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : "./echo$$");
80 open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
81 print PROG 'print "@ARGV\n"', "\n";
83 my $echo = "$Invoke_Perl $ECHO";
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.
91 $ENV{'DCL$PATH'} = '' if $Is_VMS;
94 delete @ENV{@MoreEnv};
97 test 1, eval { `$echo 1` } eq "1\n";
99 if ($Is_MSWin32 || $Is_VMS) {
100 print "# Environment tainting tests skipped\n";
101 for (2..5) { print "ok $_\n" }
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}/;
111 test 2, !@vars, "\$$vars[0]";
113 # tainted $TERM is unsafe only if it contains metachars
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}/, $@;
123 unless ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32) {
124 $tmp = (grep { defined and -d and (stat _)[2] & 2 }
125 qw(/tmp /var/tmp /usr/tmp /sys$scratch),
126 @ENV{qw(TMP TEMP)})[0]
127 or print "# can't find world-writeable directory to test PATH\n";
131 local $ENV{PATH} = $tmp;
132 test 6, eval { `$echo 1` } eq '';
133 test 7, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
136 for (6..7) { print "ok $_ # Skipped: all directories are writeable\n" }
140 $ENV{'DCL$PATH'} = $TAINT;
141 test 8, eval { `$echo 1` } eq '';
142 test 9, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
144 $ENV{'DCL$PATH'} = $tmp;
145 test 10, eval { `$echo 1` } eq '';
146 test 11, $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
149 for (10..11) { print "ok $_ # Skipped: can't find world-writeable directory to test DCL\$PATH\n" }
151 $ENV{'DCL$PATH'} = '';
154 for (8..11) { print "ok $_ # Skipped: This is not VMS\n"; }
158 # Let's see that we can taint and untaint as needed.
161 test 12, tainted $foo;
163 # That was a sanity check. If it failed, stop the insanity!
164 die "Taint checks don't seem to be enabled" unless tainted $foo;
167 test 13, not tainted $foo;
170 test 14, tainted $foo;
173 test 15, not any_tainted @list;
174 taint_these @list[1,3,5,7,9];
175 test 16, any_tainted @list;
176 test 17, all_tainted @list[1,3,5,7,9];
177 test 18, not any_tainted @list[0,2,4,6,8];
179 ($foo) = $foo =~ /(.+)/;
180 test 19, not tainted $foo;
182 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
183 test 20, not tainted $foo;
184 test 21, $foo eq 'bar';
186 my $pi = 4 * atan2(1,1) + $TAINT0;
187 test 22, tainted $pi;
189 ($pi) = $pi =~ /(\d+\.\d+)/;
190 test 23, not tainted $pi;
191 test 24, sprintf("%.5f", $pi) eq '3.14159';
194 # How about command-line arguments? The problem is that we don't
195 # always get some, so we'll run another process with some.
198 open PROG, "> $arg" or die "Can't create $arg: $!";
200 eval { join('', @ARGV), kill 0 };
201 exit 0 if $@ =~ /^Insecure dependency/;
202 print "# Oops: \$@ was [$@]\n";
206 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
207 test 25, !$?, "Exited with status $?";
211 # Reading from a file should be tainted
214 test 26, open(FILE, $file), "Couldn't open '$file': $!";
217 sysread(FILE, $block, 100);
220 test 27, tainted $block;
221 test 28, tainted $line;
224 # Globs should be forbidden, except under VMS,
225 # which doesn't spawn an external program.
227 for (29..30) { print "ok $_\n"; }
230 my @globs = eval { <*> };
231 test 29, @globs == 0 && $@ =~ /^Insecure dependency/;
233 @globs = eval { glob '*' };
234 test 30, @globs == 0 && $@ =~ /^Insecure dependency/;
237 # Output of commands should be tainted
239 my $foo = `$echo abc`;
240 test 31, tainted $foo;
243 # Certain system variables should be tainted
245 test 32, all_tainted $^X, $0;
248 # Results of matching should all be untainted
250 my $foo = "abcdefghi" . $TAINT;
251 test 33, tainted $foo;
254 test 34, not any_tainted $`, $&, $';
256 $foo =~ /(...)(...)(...)/;
257 test 35, not any_tainted $1, $2, $3, $+;
259 my @bar = $foo =~ /(...)(...)(...)/;
260 test 36, not any_tainted @bar;
262 test 37, tainted $foo; # $foo should still be tainted!
263 test 38, $foo eq "abcdefghi";
266 # Operations which affect files can't use tainted data.
268 test 39, eval { chmod 0, $TAINT } eq '', 'chmod';
269 test 40, $@ =~ /^Insecure dependency/, $@;
271 # There is no feature test in $Config{} for truncate,
272 # so we allow for the possibility that it's missing.
273 test 41, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate';
274 test 42, $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
276 test 43, eval { rename '', $TAINT } eq '', 'rename';
277 test 44, $@ =~ /^Insecure dependency/, $@;
279 test 45, eval { unlink $TAINT } eq '', 'unlink';
280 test 46, $@ =~ /^Insecure dependency/, $@;
282 test 47, eval { utime $TAINT } eq '', 'utime';
283 test 48, $@ =~ /^Insecure dependency/, $@;
285 if ($Config{d_chown}) {
286 test 49, eval { chown -1, -1, $TAINT } eq '', 'chown';
287 test 50, $@ =~ /^Insecure dependency/, $@;
290 for (49..50) { print "ok $_ # Skipped: chown() is not available\n" }
293 if ($Config{d_link}) {
294 test 51, eval { link $TAINT, '' } eq '', 'link';
295 test 52, $@ =~ /^Insecure dependency/, $@;
298 for (51..52) { print "ok $_ # Skipped: link() is not available\n" }
301 if ($Config{d_symlink}) {
302 test 53, eval { symlink $TAINT, '' } eq '', 'symlink';
303 test 54, $@ =~ /^Insecure dependency/, $@;
306 for (53..54) { print "ok $_ # Skipped: symlink() is not available\n" }
310 # Operations which affect directories can't use tainted data.
312 test 55, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir';
313 test 56, $@ =~ /^Insecure dependency/, $@;
315 test 57, eval { rmdir $TAINT } eq '', 'rmdir';
316 test 58, $@ =~ /^Insecure dependency/, $@;
318 test 59, eval { chdir $TAINT } eq '', 'chdir';
319 test 60, $@ =~ /^Insecure dependency/, $@;
321 if ($Config{d_chroot}) {
322 test 61, eval { chroot $TAINT } eq '', 'chroot';
323 test 62, $@ =~ /^Insecure dependency/, $@;
326 for (61..62) { print "ok $_ # Skipped: chroot() is not available\n" }
330 # Some operations using files can't use tainted data.
332 my $foo = "imaginary library" . $TAINT;
333 test 63, eval { require $foo } eq '', 'require';
334 test 64, $@ =~ /^Insecure dependency/, $@;
336 my $filename = "./taintB$$"; # NB: $filename isn't tainted!
337 END { unlink $filename if defined $filename }
338 $foo = $filename . $TAINT;
339 unlink $filename; # in any case
341 test 65, eval { open FOO, $foo } eq '', 'open for read';
342 test 66, $@ eq '', $@; # NB: This should be allowed
343 test 67, $! == 2; # File not found
345 test 68, eval { open FOO, "> $foo" } eq '', 'open for write';
346 test 69, $@ =~ /^Insecure dependency/, $@;
349 # Commands to the system can't use tainted data
353 if ($^O eq 'amigaos') {
354 for (70..73) { print "ok $_ # Skipped: open('|') is not available\n" }
357 test 70, eval { open FOO, "| $foo" } eq '', 'popen to';
358 test 71, $@ =~ /^Insecure dependency/, $@;
360 test 72, eval { open FOO, "$foo |" } eq '', 'popen from';
361 test 73, $@ =~ /^Insecure dependency/, $@;
364 test 74, eval { exec $TAINT } eq '', 'exec';
365 test 75, $@ =~ /^Insecure dependency/, $@;
367 test 76, eval { system $TAINT } eq '', 'system';
368 test 77, $@ =~ /^Insecure dependency/, $@;
373 test 78, eval { `$echo 1$foo` } eq '', 'backticks';
374 test 79, $@ =~ /^Insecure dependency/, $@;
376 if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe
377 test 80, join('', eval { glob $foo } ) ne '', 'globbing';
378 test 81, $@ eq '', $@;
381 for (80..81) { print "ok $_ # Skipped: this is not VMS\n"; }
385 # Operations which affect processes can't use tainted data.
387 test 82, eval { kill 0, $TAINT } eq '', 'kill';
388 test 83, $@ =~ /^Insecure dependency/, $@;
390 if ($Config{d_setpgrp}) {
391 test 84, eval { setpgrp 0, $TAINT } eq '', 'setpgrp';
392 test 85, $@ =~ /^Insecure dependency/, $@;
395 for (84..85) { print "ok $_ # Skipped: setpgrp() is not available\n" }
398 if ($Config{d_setprior}) {
399 test 86, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority';
400 test 87, $@ =~ /^Insecure dependency/, $@;
403 for (86..87) { print "ok $_ # Skipped: setpriority() is not available\n" }
407 # Some miscellaneous operations can't use tainted data.
409 if ($Config{d_syscall}) {
410 test 88, eval { syscall $TAINT } eq '', 'syscall';
411 test 89, $@ =~ /^Insecure dependency/, $@;
414 for (88..89) { print "ok $_ # Skipped: syscall() is not available\n" }
421 my $temp = "./taintC$$";
423 test 90, open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
425 test 91, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl';
426 test 92, $@ =~ /^Insecure dependency/, $@;
428 if ($Config{d_fcntl}) {
429 test 93, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl';
430 test 94, $@ =~ /^Insecure dependency/, $@;
433 for (93..94) { print "ok $_ # Skipped: fcntl() is not available\n" }
440 # Some tests involving references
442 my $foo = 'abc' . $TAINT;
444 test 95, not tainted $fooref;
445 test 96, tainted $$fooref;
446 test 97, tainted $foo;
449 # Some tests involving assignment
453 test 98, all_tainted $foo, $bar;
454 test 99, tainted($foo = $bar);
455 test 100, tainted($bar = $bar);
456 test 101, tainted($bar += $bar);
457 test 102, tainted($bar -= $bar);
458 test 103, tainted($bar *= $bar);
459 test 104, tainted($bar++);
460 test 105, tainted($bar /= $bar);
461 test 106, tainted($bar += 0);
462 test 107, tainted($bar -= 2);
463 test 108, tainted($bar *= -1);
464 test 109, tainted($bar /= 1);
465 test 110, tainted($bar--);
469 # Test assignment and return of lists
471 my @foo = ("A", "tainted" . $TAINT, "B");
472 test 112, not tainted $foo[0];
473 test 113, tainted $foo[1];
474 test 114, not tainted $foo[2];
476 test 115, not tainted $bar[0];
477 test 116, tainted $bar[1];
478 test 117, not tainted $bar[2];
479 my @baz = eval { "A", "tainted" . $TAINT, "B" };
480 test 118, not tainted $baz[0];
481 test 119, tainted $baz[1];
482 test 120, not tainted $baz[2];
483 my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
484 test 121, not tainted $plugh[0];
485 test 122, tainted $plugh[1];
486 test 123, not tainted $plugh[2];
487 my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
488 test 124, not tainted ((&$nautilus)[0]);
489 test 125, tainted ((&$nautilus)[1]);
490 test 126, not tainted ((&$nautilus)[2]);
491 my @xyzzy = &$nautilus;
492 test 127, not tainted $xyzzy[0];
493 test 128, tainted $xyzzy[1];
494 test 129, not tainted $xyzzy[2];
495 my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
496 test 130, not tainted ((&$red_october)[0]);
497 test 131, tainted ((&$red_october)[1]);
498 test 132, not tainted ((&$red_october)[2]);
499 my @corge = &$red_october;
500 test 133, not tainted $corge[0];
501 test 134, tainted $corge[1];
502 test 135, not tainted $corge[2];
505 # Test for system/library calls returning string data of dubious origin.
507 # No reliable %Config check for getpw*
508 if (eval { setpwent(); getpwent(); 1 }) {
510 my @getpwent = getpwent();
511 die "getpwent: $!\n" unless (@getpwent);
512 test 136,( not tainted $getpwent[0]
513 and not tainted $getpwent[1]
514 and not tainted $getpwent[2]
515 and not tainted $getpwent[3]
516 and not tainted $getpwent[4]
517 and not tainted $getpwent[5]
518 and tainted $getpwent[6] # gecos
519 and not tainted $getpwent[7]
520 and not tainted $getpwent[8]);
523 print "ok 136 # Skipped: getpwent() is not available\n";
526 if ($Config{d_readdir}) { # pretty hard to imagine not
528 opendir(D, "op") or die "opendir: $!\n";
529 my $readdir = readdir(D);
530 test 137, tainted $readdir;
533 print "ok 137 # Skipped: readdir() is not available\n";
536 if ($Config{d_readlink} && $Config{d_symlink}) {
537 my $symlink = "sl$$";
539 symlink("/something/naughty", $symlink) or die "symlink: $!\n";
540 my $readlink = readlink($symlink);
541 test 138, tainted $readlink;
544 print "ok 138 # Skipped: readlink() or symlink() is not available\n";
548 # test bitwise ops (regression bug)
552 test 139, not tainted $j;
555 test 140, tainted $j;