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 $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' : './perl';
24 warn "# Note: logical name 'PATH' may have been deleted\n";
25 \$ENV{IFS} = "$ENV{IFS}";
26 \$ENV{'DCL\$PATH'} = "$ENV{'DCL$PATH'}";
32 # The empty tainted value, for tainting strings
33 my $TAINT = substr($^X, 0, 0);
34 # A tainted zero, useful for tainting numbers
35 my $TAINT0 = 0 + $TAINT;
37 # This taints each argument passed. All must be lvalues.
38 # Side effect: It also stringifies them. :-(
40 for (@_) { $_ .= $TAINT }
43 # How to identify taint when you see it
45 not eval { join("",@_), kill 0; 1 };
51 for (@_) { return 0 unless tainted $_ }
56 my($serial, $boolean, $diag) = @_;
60 print "not ok $serial\n";
61 for (split m/^/m, $diag) {
66 or substr($diag, -1) eq "\n";
70 # We need an external program to call.
71 my $ECHO = "./echo$$";
73 open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
74 print PROG 'print "@ARGV\n"', "\n";
76 my $echo = "$Invoke_Perl $ECHO";
80 # First, let's make sure that Perl is checking the dangerous
81 # environment variables. Maybe they aren't set yet, so we'll
82 # taint them ourselves.
84 $ENV{'DCL$PATH'} = '' if $Is_VMS;
88 test 1, eval { `$echo 1` } eq '';
89 test 2, $@ =~ /^Insecure \$ENV{PATH}/, $@;
93 test 3, eval { `$echo 1` } eq '';
94 test 4, $@ =~ /^Insecure \$ENV{IFS}/, $@;
96 my ($tmp) = grep { (stat)[2] & 2 } '/tmp', '/var/tmp', '/usr/tmp';
97 if ($tmp and $^O ne 'os2') { # All dirs are writable under OS/2
100 test 5, eval { `$echo 1` } eq '';
101 test 6, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
104 print "# can't find writeable directory to test PATH tainting\n";
105 for (5..6) { print "ok $_\n" }
110 test 7, eval { `$echo 1` } eq "1\n";
111 test 8, $@ eq '', $@;
114 $ENV{'DCL$PATH'} = $TAINT;
115 test 9, eval { `$echo 1` } eq '';
116 test 10, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
117 $ENV{'DCL$PATH'} = '';
120 print "# This is not VMS\n";
121 for (9..10) { print "ok $_\n"; }
125 # Let's see that we can taint and untaint as needed.
128 test 11, tainted $foo;
131 test 12, not tainted $foo;
134 test 13, tainted $foo;
137 test 14, not any_tainted @list;
138 taint_these @list[1,3,5,7,9];
139 test 15, any_tainted @list;
140 test 16, all_tainted @list[1,3,5,7,9];
141 test 17, not any_tainted @list[0,2,4,6,8];
143 ($foo) = $foo =~ /(.+)/;
144 test 18, not tainted $foo;
146 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
147 test 19, not tainted $foo;
148 test 20, $foo eq 'bar';
150 my $pi = 4 * atan2(1,1) + $TAINT0;
151 test 21, tainted $pi;
153 ($pi) = $pi =~ /(\d+\.\d+)/;
154 test 22, not tainted $pi;
155 test 23, sprintf("%.5f", $pi) eq '3.14159';
158 # How about command-line arguments? The problem is that we don't
159 # always get some, so we'll run another process with some.
162 open PROG, "> $arg" or die "Can't create $arg: $!";
164 eval { join('', @ARGV), kill 0 };
165 exit 0 if $@ =~ /^Insecure dependency/;
166 print "# Oops: \$@ was [$@]\n";
170 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
171 test 24, !$?, "Exited with status $?";
175 # Reading from a file should be tainted
177 my $file = './perl' . $Config{exe_ext};
178 test 25, open(FILE, $file), "Couldn't open '$file': $!";
181 sysread(FILE, $block, 100);
182 my $line = <FILE>; # Should "work"
184 test 26, tainted $block;
185 test 27, tainted $line;
188 # Globs should be tainted.
191 test 28, all_tainted @globs;
194 test 29, all_tainted @globs;
197 # Output of commands should be tainted
199 my $foo = `$echo abc`;
200 test 30, tainted $foo;
203 # Certain system variables should be tainted
205 test 31, all_tainted $^X, $0;
208 # Results of matching should all be untainted
210 my $foo = "abcdefghi" . $TAINT;
211 test 32, tainted $foo;
214 test 33, not any_tainted $`, $&, $';
216 $foo =~ /(...)(...)(...)/;
217 test 34, not any_tainted $1, $2, $3, $+;
219 my @bar = $foo =~ /(...)(...)(...)/;
220 test 35, not any_tainted @bar;
222 test 36, tainted $foo; # $foo should still be tainted!
223 test 37, $foo eq "abcdefghi";
226 # Operations which affect files can't use tainted data.
228 test 38, eval { chmod 0, $TAINT } eq '', 'chmod';
229 test 39, $@ =~ /^Insecure dependency/, $@;
231 test 40, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate';
232 test 41, $@ =~ /^Insecure dependency/, $@;
234 test 42, eval { rename '', $TAINT } eq '', 'rename';
235 test 43, $@ =~ /^Insecure dependency/, $@;
237 test 44, eval { unlink $TAINT } eq '', 'unlink';
238 test 45, $@ =~ /^Insecure dependency/, $@;
240 test 46, eval { utime $TAINT } eq '', 'utime';
241 test 47, $@ =~ /^Insecure dependency/, $@;
243 if ($Config{d_chown}) {
244 test 48, eval { chown -1, -1, $TAINT } eq '', 'chown';
245 test 49, $@ =~ /^Insecure dependency/, $@;
248 print "# chown() is not available\n";
249 for (48..49) { print "ok $_\n" }
252 if ($Config{d_link}) {
253 test 50, eval { link $TAINT, '' } eq '', 'link';
254 test 51, $@ =~ /^Insecure dependency/, $@;
257 print "# link() is not available\n";
258 for (50..51) { print "ok $_\n" }
261 if ($Config{d_symlink}) {
262 test 52, eval { symlink $TAINT, '' } eq '', 'symlink';
263 test 53, $@ =~ /^Insecure dependency/, $@;
266 print "# symlink() is not available\n";
267 for (52..53) { print "ok $_\n" }
271 # Operations which affect directories can't use tainted data.
273 test 54, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir';
274 test 55, $@ =~ /^Insecure dependency/, $@;
276 test 56, eval { rmdir $TAINT } eq '', 'rmdir';
277 test 57, $@ =~ /^Insecure dependency/, $@;
279 test 58, eval { chdir $TAINT } eq '', 'chdir';
280 test 59, $@ =~ /^Insecure dependency/, $@;
282 if ($Config{d_chroot}) {
283 test 60, eval { chroot $TAINT } eq '', 'chroot';
284 test 61, $@ =~ /^Insecure dependency/, $@;
287 print "# chroot() is not available\n";
288 for (60..61) { print "ok $_\n" }
292 # Some operations using files can't use tainted data.
294 my $foo = "imaginary library" . $TAINT;
295 test 62, eval { require $foo } eq '', 'require';
296 test 63, $@ =~ /^Insecure dependency/, $@;
298 my $filename = "./taintB$$"; # NB: $filename isn't tainted!
299 END { unlink $filename if defined $filename }
300 $foo = $filename . $TAINT;
301 unlink $filename; # in any case
303 test 64, eval { open FOO, $foo } eq '', 'open for read';
304 test 65, $@ eq '', $@; # NB: This should be allowed
305 test 66, $! == 2; # File not found
307 test 67, eval { open FOO, "> $foo" } eq '', 'open for write';
308 test 68, $@ =~ /^Insecure dependency/, $@;
311 # Commands to the system can't use tainted data
315 if ($^O eq 'amigaos') {
316 print "# open(\"|\") is not available\n";
317 for (69..72) { print "ok $_\n" }
320 test 69, eval { open FOO, "| $foo" } eq '', 'popen to';
321 test 70, $@ =~ /^Insecure dependency/, $@;
323 test 71, eval { open FOO, "$foo |" } eq '', 'popen from';
324 test 72, $@ =~ /^Insecure dependency/, $@;
327 test 73, eval { exec $TAINT } eq '', 'exec';
328 test 74, $@ =~ /^Insecure dependency/, $@;
330 test 75, eval { system $TAINT } eq '', 'system';
331 test 76, $@ =~ /^Insecure dependency/, $@;
336 test 77, eval { `$echo 1$foo` } eq '', 'backticks';
337 test 78, $@ =~ /^Insecure dependency/, $@;
339 if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe
340 test 79, join('', eval { glob $foo } ) ne '', 'globbing';
341 test 80, $@ eq '', $@;
344 test 79, join('', eval { glob $foo } ) eq '', 'globbing';
345 test 80, $@ =~ /^Insecure dependency/, $@;
349 # Operations which affect processes can't use tainted data.
351 test 81, eval { kill 0, $TAINT } eq '', 'kill';
352 test 82, $@ =~ /^Insecure dependency/, $@;
354 if ($Config{d_setpgrp}) {
355 test 83, eval { setpgrp 0, $TAINT } eq '', 'setpgrp';
356 test 84, $@ =~ /^Insecure dependency/, $@;
359 print "# setpgrp() is not available\n";
360 for (83..84) { print "ok $_\n" }
363 if ($Config{d_setprior}) {
364 test 85, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority';
365 test 86, $@ =~ /^Insecure dependency/, $@;
368 print "# setpriority() is not available\n";
369 for (85..86) { print "ok $_\n" }
373 # Some miscellaneous operations can't use tainted data.
375 if ($Config{d_syscall}) {
376 test 87, eval { syscall $TAINT } eq '', 'syscall';
377 test 88, $@ =~ /^Insecure dependency/, $@;
380 print "# syscall() is not available\n";
381 for (87..88) { print "ok $_\n" }
388 my $temp = "./taintC$$";
390 test 89, open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
392 test 90, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl';
393 test 91, $@ =~ /^Insecure dependency/, $@;
395 if ($Config{d_fcntl}) {
396 test 92, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl';
397 test 93, $@ =~ /^Insecure dependency/, $@;
400 print "# fcntl() is not available\n";
401 for (92..93) { print "ok $_\n" }
408 # Some tests involving references
410 my $foo = 'abc' . $TAINT;
412 test 94, not tainted $fooref;
413 test 95, tainted $$fooref;
414 test 96, tainted $foo;