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 ENV CDPATH TERM/;
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;
99 test 1, eval { `$echo 1` } eq "1\n";
102 print "# Environment tainting tests skipped\n";
103 for (2) { print "ok $_\n" }
106 my @vars = ('PATH', @MoreEnv);
107 while (my $v = $vars[0]) {
108 local $ENV{$v} = $TAINT;
109 last if eval { `$echo 1` };
110 last unless $@ =~ /^Insecure \$ENV{$v}/;
113 test 2, !@vars, "\$$vars[0]";
117 if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32) {
118 print "# all directories are writeable\n";
121 $tmp = (grep { defined and -d and (stat _)[2] & 2 }
122 qw(/tmp /var/tmp /usr/tmp /sys$scratch),
123 @ENV{qw(TMP TEMP)})[0]
124 or print "# can't find world-writeable directory to test PATH\n";
128 local $ENV{PATH} = $tmp;
129 test 3, eval { `$echo 1` } eq '';
130 test 4, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
133 for (3..4) { print "ok $_\n" }
137 $ENV{'DCL$PATH'} = $TAINT;
138 test 5, eval { `$echo 1` } eq '';
139 test 6, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
141 $ENV{'DCL$PATH'} = $tmp;
142 test 7, eval { `$echo 1` } eq '';
143 test 8, $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
146 print "# can't find world-writeable directory to test DCL\$PATH\n";
147 for (7..8) { print "ok $_\n" }
149 $ENV{'DCL$PATH'} = '';
152 print "# This is not VMS\n";
153 for (5..8) { print "ok $_\n"; }
157 # Let's see that we can taint and untaint as needed.
160 test 9, tainted $foo;
162 # That was a sanity check. If it failed, stop the insanity!
163 die "Taint checks don't seem to be enabled" unless tainted $foo;
166 test 10, not tainted $foo;
169 test 11, tainted $foo;
172 test 12, not any_tainted @list;
173 taint_these @list[1,3,5,7,9];
174 test 13, any_tainted @list;
175 test 14, all_tainted @list[1,3,5,7,9];
176 test 15, not any_tainted @list[0,2,4,6,8];
178 ($foo) = $foo =~ /(.+)/;
179 test 16, not tainted $foo;
181 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
182 test 17, not tainted $foo;
183 test 18, $foo eq 'bar';
185 my $pi = 4 * atan2(1,1) + $TAINT0;
186 test 19, tainted $pi;
188 ($pi) = $pi =~ /(\d+\.\d+)/;
189 test 20, not tainted $pi;
190 test 21, sprintf("%.5f", $pi) eq '3.14159';
193 # How about command-line arguments? The problem is that we don't
194 # always get some, so we'll run another process with some.
197 open PROG, "> $arg" or die "Can't create $arg: $!";
199 eval { join('', @ARGV), kill 0 };
200 exit 0 if $@ =~ /^Insecure dependency/;
201 print "# Oops: \$@ was [$@]\n";
205 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
206 test 22, !$?, "Exited with status $?";
210 # Reading from a file should be tainted
213 test 23, open(FILE, $file), "Couldn't open '$file': $!";
216 sysread(FILE, $block, 100);
219 test 24, tainted $block;
220 test 25, tainted $line;
223 # Globs should be forbidden.
225 # Some glob implementations need to spawn system programs.
226 local $ENV{PATH} = '';
227 $ENV{PATH} = (-l '/bin' ? '' : '/bin:') . '/usr/bin' unless $Is_VMS;
229 my @globs = eval { <*> };
230 test 26, @globs == 0 && $@ =~ /^Insecure dependency/;
232 @globs = eval { glob '*' };
233 test 27, @globs == 0 && $@ =~ /^Insecure dependency/;
236 # Output of commands should be tainted
238 my $foo = `$echo abc`;
239 test 28, tainted $foo;
242 # Certain system variables should be tainted
244 test 29, all_tainted $^X, $0;
247 # Results of matching should all be untainted
249 my $foo = "abcdefghi" . $TAINT;
250 test 30, tainted $foo;
253 test 31, not any_tainted $`, $&, $';
255 $foo =~ /(...)(...)(...)/;
256 test 32, not any_tainted $1, $2, $3, $+;
258 my @bar = $foo =~ /(...)(...)(...)/;
259 test 33, not any_tainted @bar;
261 test 34, tainted $foo; # $foo should still be tainted!
262 test 35, $foo eq "abcdefghi";
265 # Operations which affect files can't use tainted data.
267 test 36, eval { chmod 0, $TAINT } eq '', 'chmod';
268 test 37, $@ =~ /^Insecure dependency/, $@;
270 # There is no feature test in $Config{} for truncate,
271 # so we allow for the possibility that it's missing.
272 test 38, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate';
273 test 39, $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
275 test 40, eval { rename '', $TAINT } eq '', 'rename';
276 test 41, $@ =~ /^Insecure dependency/, $@;
278 test 42, eval { unlink $TAINT } eq '', 'unlink';
279 test 43, $@ =~ /^Insecure dependency/, $@;
281 test 44, eval { utime $TAINT } eq '', 'utime';
282 test 45, $@ =~ /^Insecure dependency/, $@;
284 if ($Config{d_chown}) {
285 test 46, eval { chown -1, -1, $TAINT } eq '', 'chown';
286 test 47, $@ =~ /^Insecure dependency/, $@;
289 print "# chown() is not available\n";
290 for (46..47) { print "ok $_\n" }
293 if ($Config{d_link}) {
294 test 48, eval { link $TAINT, '' } eq '', 'link';
295 test 49, $@ =~ /^Insecure dependency/, $@;
298 print "# link() is not available\n";
299 for (48..49) { print "ok $_\n" }
302 if ($Config{d_symlink}) {
303 test 50, eval { symlink $TAINT, '' } eq '', 'symlink';
304 test 51, $@ =~ /^Insecure dependency/, $@;
307 print "# symlink() is not available\n";
308 for (50..51) { print "ok $_\n" }
312 # Operations which affect directories can't use tainted data.
314 test 52, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir';
315 test 53, $@ =~ /^Insecure dependency/, $@;
317 test 54, eval { rmdir $TAINT } eq '', 'rmdir';
318 test 55, $@ =~ /^Insecure dependency/, $@;
320 test 56, eval { chdir $TAINT } eq '', 'chdir';
321 test 57, $@ =~ /^Insecure dependency/, $@;
323 if ($Config{d_chroot}) {
324 test 58, eval { chroot $TAINT } eq '', 'chroot';
325 test 59, $@ =~ /^Insecure dependency/, $@;
328 print "# chroot() is not available\n";
329 for (58..59) { print "ok $_\n" }
333 # Some operations using files can't use tainted data.
335 my $foo = "imaginary library" . $TAINT;
336 test 60, eval { require $foo } eq '', 'require';
337 test 61, $@ =~ /^Insecure dependency/, $@;
339 my $filename = "./taintB$$"; # NB: $filename isn't tainted!
340 END { unlink $filename if defined $filename }
341 $foo = $filename . $TAINT;
342 unlink $filename; # in any case
344 test 62, eval { open FOO, $foo } eq '', 'open for read';
345 test 63, $@ eq '', $@; # NB: This should be allowed
346 test 64, $! == 2; # File not found
348 test 65, eval { open FOO, "> $foo" } eq '', 'open for write';
349 test 66, $@ =~ /^Insecure dependency/, $@;
352 # Commands to the system can't use tainted data
356 if ($^O eq 'amigaos') {
357 print "# open(\"|\") is not available\n";
358 for (67..70) { print "ok $_\n" }
361 test 67, eval { open FOO, "| $foo" } eq '', 'popen to';
362 test 68, $@ =~ /^Insecure dependency/, $@;
364 test 69, eval { open FOO, "$foo |" } eq '', 'popen from';
365 test 70, $@ =~ /^Insecure dependency/, $@;
368 test 71, eval { exec $TAINT } eq '', 'exec';
369 test 72, $@ =~ /^Insecure dependency/, $@;
371 test 73, eval { system $TAINT } eq '', 'system';
372 test 74, $@ =~ /^Insecure dependency/, $@;
377 test 75, eval { `$echo 1$foo` } eq '', 'backticks';
378 test 76, $@ =~ /^Insecure dependency/, $@;
380 if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe
381 test 77, join('', eval { glob $foo } ) ne '', 'globbing';
382 test 78, $@ eq '', $@;
385 test 77, join('', eval { glob $foo } ) eq '', 'globbing';
386 test 78, $@ =~ /^Insecure dependency/, $@;
390 # Operations which affect processes can't use tainted data.
392 test 79, eval { kill 0, $TAINT } eq '', 'kill';
393 test 80, $@ =~ /^Insecure dependency/, $@;
395 if ($Config{d_setpgrp}) {
396 test 81, eval { setpgrp 0, $TAINT } eq '', 'setpgrp';
397 test 82, $@ =~ /^Insecure dependency/, $@;
400 print "# setpgrp() is not available\n";
401 for (81..82) { print "ok $_\n" }
404 if ($Config{d_setprior}) {
405 test 83, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority';
406 test 84, $@ =~ /^Insecure dependency/, $@;
409 print "# setpriority() is not available\n";
410 for (83..84) { print "ok $_\n" }
414 # Some miscellaneous operations can't use tainted data.
416 if ($Config{d_syscall}) {
417 test 85, eval { syscall $TAINT } eq '', 'syscall';
418 test 86, $@ =~ /^Insecure dependency/, $@;
421 print "# syscall() is not available\n";
422 for (85..86) { print "ok $_\n" }
429 my $temp = "./taintC$$";
431 test 87, open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
433 test 88, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl';
434 test 89, $@ =~ /^Insecure dependency/, $@;
436 if ($Config{d_fcntl}) {
437 test 90, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl';
438 test 91, $@ =~ /^Insecure dependency/, $@;
441 print "# fcntl() is not available\n";
442 for (90..91) { print "ok $_\n" }
449 # Some tests involving references
451 my $foo = 'abc' . $TAINT;
453 test 92, not tainted $fooref;
454 test 93, tainted $$fooref;
455 test 94, tainted $foo;
458 # Some tests involving assignment
462 test 95, all_tainted $foo, $bar;
463 test 96, tainted($foo = $bar);
464 test 97, tainted($bar = $bar);
465 test 98, tainted($bar += $bar);
466 test 99, tainted($bar -= $bar);
467 test 100, tainted($bar *= $bar);
468 test 101, tainted($bar++);
469 test 102, tainted($bar /= $bar);
470 test 103, tainted($bar += 0);
471 test 104, tainted($bar -= 2);
472 test 105, tainted($bar *= -1);
473 test 106, tainted($bar /= 1);
474 test 107, tainted($bar--);
478 # Test assignment and return of lists
480 my @foo = ("A", "tainted" . $TAINT, "B");
481 test 109, not tainted $foo[0];
482 test 110, tainted $foo[1];
483 test 111, not tainted $foo[2];
485 test 112, not tainted $bar[0];
486 test 113, tainted $bar[1];
487 test 114, not tainted $bar[2];
488 my @baz = eval { "A", "tainted" . $TAINT, "B" };
489 test 115, not tainted $baz[0];
490 test 116, tainted $baz[1];
491 test 117, not tainted $baz[2];
492 my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
493 test 118, not tainted $plugh[0];
494 test 119, tainted $plugh[1];
495 test 120, not tainted $plugh[2];
496 my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
497 test 121, not tainted ((&$nautilus)[0]);
498 test 122, tainted ((&$nautilus)[1]);
499 test 123, not tainted ((&$nautilus)[2]);
500 my @xyzzy = &$nautilus;
501 test 124, not tainted $xyzzy[0];
502 test 125, tainted $xyzzy[1];
503 test 126, not tainted $xyzzy[2];
504 my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
505 test 127, not tainted ((&$red_october)[0]);
506 test 128, tainted ((&$red_october)[1]);
507 test 129, not tainted ((&$red_october)[2]);
508 my @corge = &$red_october;
509 test 130, not tainted $corge[0];
510 test 131, tainted $corge[1];
511 test 132, not tainted $corge[2];