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';
23 my ($olddcl) = $ENV{'DCL$PATH'} =~ /^(.*)$/;
24 my ($oldifs) = $ENV{IFS} =~ /^(.*)$/;
28 warn "# Note: logical name 'PATH' may have been deleted\n";
29 \$ENV{IFS} = \$oldifs;
30 \$ENV{'DCL\$PATH'} = \$olddcl;
36 # The empty tainted value, for tainting strings
37 my $TAINT = substr($^X, 0, 0);
38 # A tainted zero, useful for tainting numbers
39 my $TAINT0 = 0 + $TAINT;
41 # This taints each argument passed. All must be lvalues.
42 # Side effect: It also stringifies them. :-(
44 for (@_) { $_ .= $TAINT }
47 # How to identify taint when you see it
49 not eval { join("",@_), kill 0; 1 };
55 for (@_) { return 0 unless tainted $_ }
60 my($serial, $boolean, $diag) = @_;
64 print "not ok $serial\n";
65 for (split m/^/m, $diag) {
70 or substr($diag, -1) eq "\n";
74 # We need an external program to call.
75 my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : "./echo$$");
77 open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
78 print PROG 'print "@ARGV\n"', "\n";
80 my $echo = "$Invoke_Perl $ECHO";
84 # First, let's make sure that Perl is checking the dangerous
85 # environment variables. Maybe they aren't set yet, so we'll
86 # taint them ourselves.
88 $ENV{'DCL$PATH'} = '' if $Is_VMS;
91 print "# PATH/IFS tainting tests skipped\n";
92 for (1..4) { print "ok $_\n" }
97 test 1, eval { `$echo 1` } eq '';
98 test 2, $@ =~ /^Insecure \$ENV{PATH}/, $@;
102 test 3, eval { `$echo 1` } eq '';
103 test 4, $@ =~ /^Insecure \$ENV{IFS}/, $@;
106 if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32) {
107 print "# all directories are writeable\n";
110 $tmp = (grep { defined and -d and (stat _)[2] & 2 }
111 qw(/tmp /var/tmp /usr/tmp /sys$scratch),
112 @ENV{qw(TMP TEMP)})[0]
113 or print "# can't find world-writeable directory to test PATH\n";
119 test 5, eval { `$echo 1` } eq '';
120 test 6, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
123 for (5..6) { print "ok $_\n" }
128 test 7, eval { `$echo 1` } eq "1\n";
129 test 8, $@ eq '', $@;
132 $ENV{'DCL$PATH'} = $TAINT;
133 test 9, eval { `$echo 1` } eq '';
134 test 10, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
136 $ENV{'DCL$PATH'} = $tmp;
137 test 11, eval { `$echo 1` } eq '';
138 test 12, $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
141 print "# can't find world-writeable directory to test DCL\$PATH\n";
142 for (11..12) { print "ok $_\n" }
144 $ENV{'DCL$PATH'} = '';
147 print "# This is not VMS\n";
148 for (9..12) { print "ok $_\n"; }
152 # Let's see that we can taint and untaint as needed.
155 test 13, tainted $foo;
157 # That was a sanity check. If it failed, stop the insanity!
158 die "Taint checks don't seem to be enabled" unless tainted $foo;
161 test 14, not tainted $foo;
164 test 15, tainted $foo;
167 test 16, not any_tainted @list;
168 taint_these @list[1,3,5,7,9];
169 test 17, any_tainted @list;
170 test 18, all_tainted @list[1,3,5,7,9];
171 test 19, not any_tainted @list[0,2,4,6,8];
173 ($foo) = $foo =~ /(.+)/;
174 test 20, not tainted $foo;
176 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
177 test 21, not tainted $foo;
178 test 22, $foo eq 'bar';
180 my $pi = 4 * atan2(1,1) + $TAINT0;
181 test 23, tainted $pi;
183 ($pi) = $pi =~ /(\d+\.\d+)/;
184 test 24, not tainted $pi;
185 test 25, sprintf("%.5f", $pi) eq '3.14159';
188 # How about command-line arguments? The problem is that we don't
189 # always get some, so we'll run another process with some.
192 open PROG, "> $arg" or die "Can't create $arg: $!";
194 eval { join('', @ARGV), kill 0 };
195 exit 0 if $@ =~ /^Insecure dependency/;
196 print "# Oops: \$@ was [$@]\n";
200 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
201 test 26, !$?, "Exited with status $?";
205 # Reading from a file should be tainted
208 test 27, open(FILE, $file), "Couldn't open '$file': $!";
211 sysread(FILE, $block, 100);
214 test 28, tainted $block;
215 test 29, tainted $line;
218 # Globs should be tainted.
220 # Some glob implementations need to spawn system programs.
221 local $ENV{PATH} = '';
222 $ENV{PATH} = (-l '/bin' ? '' : '/bin:') . '/usr/bin' unless $Is_VMS;
225 test 30, all_tainted @globs;
228 test 31, all_tainted @globs;
231 # Output of commands should be tainted
233 my $foo = `$echo abc`;
234 test 32, tainted $foo;
237 # Certain system variables should be tainted
239 test 33, all_tainted $^X, $0;
242 # Results of matching should all be untainted
244 my $foo = "abcdefghi" . $TAINT;
245 test 34, tainted $foo;
248 test 35, not any_tainted $`, $&, $';
250 $foo =~ /(...)(...)(...)/;
251 test 36, not any_tainted $1, $2, $3, $+;
253 my @bar = $foo =~ /(...)(...)(...)/;
254 test 37, not any_tainted @bar;
256 test 38, tainted $foo; # $foo should still be tainted!
257 test 39, $foo eq "abcdefghi";
260 # Operations which affect files can't use tainted data.
262 test 40, eval { chmod 0, $TAINT } eq '', 'chmod';
263 test 41, $@ =~ /^Insecure dependency/, $@;
265 # There is no feature test in $Config{} for truncate,
266 # so we allow for the possibility that it's missing.
267 test 42, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate';
268 test 43, $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
270 test 44, eval { rename '', $TAINT } eq '', 'rename';
271 test 45, $@ =~ /^Insecure dependency/, $@;
273 test 46, eval { unlink $TAINT } eq '', 'unlink';
274 test 47, $@ =~ /^Insecure dependency/, $@;
276 test 48, eval { utime $TAINT } eq '', 'utime';
277 test 49, $@ =~ /^Insecure dependency/, $@;
279 if ($Config{d_chown}) {
280 test 50, eval { chown -1, -1, $TAINT } eq '', 'chown';
281 test 51, $@ =~ /^Insecure dependency/, $@;
284 print "# chown() is not available\n";
285 for (50..51) { print "ok $_\n" }
288 if ($Config{d_link}) {
289 test 52, eval { link $TAINT, '' } eq '', 'link';
290 test 53, $@ =~ /^Insecure dependency/, $@;
293 print "# link() is not available\n";
294 for (52..53) { print "ok $_\n" }
297 if ($Config{d_symlink}) {
298 test 54, eval { symlink $TAINT, '' } eq '', 'symlink';
299 test 55, $@ =~ /^Insecure dependency/, $@;
302 print "# symlink() is not available\n";
303 for (54..55) { print "ok $_\n" }
307 # Operations which affect directories can't use tainted data.
309 test 56, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir';
310 test 57, $@ =~ /^Insecure dependency/, $@;
312 test 58, eval { rmdir $TAINT } eq '', 'rmdir';
313 test 59, $@ =~ /^Insecure dependency/, $@;
315 test 60, eval { chdir $TAINT } eq '', 'chdir';
316 test 61, $@ =~ /^Insecure dependency/, $@;
318 if ($Config{d_chroot}) {
319 test 62, eval { chroot $TAINT } eq '', 'chroot';
320 test 63, $@ =~ /^Insecure dependency/, $@;
323 print "# chroot() is not available\n";
324 for (62..63) { print "ok $_\n" }
328 # Some operations using files can't use tainted data.
330 my $foo = "imaginary library" . $TAINT;
331 test 64, eval { require $foo } eq '', 'require';
332 test 65, $@ =~ /^Insecure dependency/, $@;
334 my $filename = "./taintB$$"; # NB: $filename isn't tainted!
335 END { unlink $filename if defined $filename }
336 $foo = $filename . $TAINT;
337 unlink $filename; # in any case
339 test 66, eval { open FOO, $foo } eq '', 'open for read';
340 test 67, $@ eq '', $@; # NB: This should be allowed
341 test 68, $! == 2; # File not found
343 test 69, eval { open FOO, "> $foo" } eq '', 'open for write';
344 test 70, $@ =~ /^Insecure dependency/, $@;
347 # Commands to the system can't use tainted data
351 if ($^O eq 'amigaos') {
352 print "# open(\"|\") is not available\n";
353 for (71..74) { print "ok $_\n" }
356 test 71, eval { open FOO, "| $foo" } eq '', 'popen to';
357 test 72, $@ =~ /^Insecure dependency/, $@;
359 test 73, eval { open FOO, "$foo |" } eq '', 'popen from';
360 test 74, $@ =~ /^Insecure dependency/, $@;
363 test 75, eval { exec $TAINT } eq '', 'exec';
364 test 76, $@ =~ /^Insecure dependency/, $@;
366 test 77, eval { system $TAINT } eq '', 'system';
367 test 78, $@ =~ /^Insecure dependency/, $@;
372 test 79, eval { `$echo 1$foo` } eq '', 'backticks';
373 test 80, $@ =~ /^Insecure dependency/, $@;
375 if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe
376 test 81, join('', eval { glob $foo } ) ne '', 'globbing';
377 test 82, $@ eq '', $@;
380 test 81, join('', eval { glob $foo } ) eq '', 'globbing';
381 test 82, $@ =~ /^Insecure dependency/, $@;
385 # Operations which affect processes can't use tainted data.
387 test 83, eval { kill 0, $TAINT } eq '', 'kill';
388 test 84, $@ =~ /^Insecure dependency/, $@;
390 if ($Config{d_setpgrp}) {
391 test 85, eval { setpgrp 0, $TAINT } eq '', 'setpgrp';
392 test 86, $@ =~ /^Insecure dependency/, $@;
395 print "# setpgrp() is not available\n";
396 for (85..86) { print "ok $_\n" }
399 if ($Config{d_setprior}) {
400 test 87, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority';
401 test 88, $@ =~ /^Insecure dependency/, $@;
404 print "# setpriority() is not available\n";
405 for (87..88) { print "ok $_\n" }
409 # Some miscellaneous operations can't use tainted data.
411 if ($Config{d_syscall}) {
412 test 89, eval { syscall $TAINT } eq '', 'syscall';
413 test 90, $@ =~ /^Insecure dependency/, $@;
416 print "# syscall() is not available\n";
417 for (89..90) { print "ok $_\n" }
424 my $temp = "./taintC$$";
426 test 91, open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
428 test 92, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl';
429 test 93, $@ =~ /^Insecure dependency/, $@;
431 if ($Config{d_fcntl}) {
432 test 94, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl';
433 test 95, $@ =~ /^Insecure dependency/, $@;
436 print "# fcntl() is not available\n";
437 for (94..95) { print "ok $_\n" }
444 # Some tests involving references
446 my $foo = 'abc' . $TAINT;
448 test 96, not tainted $fooref;
449 test 97, tainted $$fooref;
450 test 98, tainted $foo;
453 # Some tests involving assignment
457 test 99, all_tainted $foo, $bar;
458 test 100, tainted($foo = $bar);
459 test 101, tainted($bar = $bar);
460 test 102, tainted($bar += $bar);
461 test 103, tainted($bar -= $bar);
462 test 104, tainted($bar *= $bar);
463 test 105, tainted($bar++);
464 test 106, tainted($bar /= $bar);
465 test 107, tainted($bar += 0);
466 test 108, tainted($bar -= 2);
467 test 109, tainted($bar *= -1);
468 test 110, tainted($bar /= 1);
469 test 111, tainted($bar--);
473 # Test assignment and return of lists
475 my @foo = ("A", "tainted" . $TAINT, "B");
476 test 113, not tainted $foo[0];
477 test 114, tainted $foo[1];
478 test 115, not tainted $foo[2];
480 test 116, not tainted $bar[0];
481 test 117, tainted $bar[1];
482 test 118, not tainted $bar[2];
483 my @baz = eval { "A", "tainted" . $TAINT, "B" };
484 test 119, not tainted $baz[0];
485 test 120, tainted $baz[1];
486 test 121, not tainted $baz[2];
487 my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
488 test 122, not tainted $plugh[0];
489 test 123, tainted $plugh[1];
490 test 124, not tainted $plugh[2];
491 my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
492 test 125, not tainted ((&$nautilus)[0]);
493 test 126, tainted ((&$nautilus)[1]);
494 test 127, not tainted ((&$nautilus)[2]);
495 my @xyzzy = &$nautilus;
496 test 128, not tainted $xyzzy[0];
497 test 129, tainted $xyzzy[1];
498 test 130, not tainted $xyzzy[2];
499 my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
500 test 131, not tainted ((&$red_october)[0]);
501 test 132, tainted ((&$red_october)[1]);
502 test 133, not tainted ((&$red_october)[2]);
503 my @corge = &$red_october;
504 test 134, not tainted $corge[0];
505 test 135, tainted $corge[1];
506 test 136, not tainted $corge[2];