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';
21 my($olddcl) = $ENV{'DCL$PATH'} =~ /^(.*)$/;
22 my($oldifs) = $ENV{IFS} =~ /^(.*)$/;
26 warn "# Note: logical name 'PATH' may have been deleted\n";
27 \$ENV{IFS} = "$oldifs";
28 \$ENV{'DCL\$PATH'} = "$olddcl";
34 # The empty tainted value, for tainting strings
35 my $TAINT = substr($^X, 0, 0);
36 # A tainted zero, useful for tainting numbers
37 my $TAINT0 = 0 + $TAINT;
39 # This taints each argument passed. All must be lvalues.
40 # Side effect: It also stringifies them. :-(
42 for (@_) { $_ .= $TAINT }
45 # How to identify taint when you see it
47 not eval { join("",@_), kill 0; 1 };
53 for (@_) { return 0 unless tainted $_ }
58 my($serial, $boolean, $diag) = @_;
62 print "not ok $serial\n";
63 for (split m/^/m, $diag) {
68 or substr($diag, -1) eq "\n";
72 # We need an external program to call.
73 my $ECHO = "./echo$$";
75 open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
76 print PROG 'print "@ARGV\n"', "\n";
78 my $echo = "$Invoke_Perl $ECHO";
82 # First, let's make sure that Perl is checking the dangerous
83 # environment variables. Maybe they aren't set yet, so we'll
84 # taint them ourselves.
86 $ENV{'DCL$PATH'} = '' if $Is_VMS;
90 test 1, eval { `$echo 1` } eq '';
91 test 2, $@ =~ /^Insecure \$ENV{PATH}/, $@;
95 test 3, eval { `$echo 1` } eq '';
96 test 4, $@ =~ /^Insecure \$ENV{IFS}/, $@;
99 if ($^O eq 'os2' || $^O eq 'amigaos') {
100 print "# all directories are writeable\n";
103 $tmp = (grep { defined and -d and (stat _)[2] & 2 }
104 qw(/tmp /var/tmp /usr/tmp /sys$scratch),
105 @ENV{qw(TMP TEMP)})[0]
106 or print "# can't find world-writeable directory to test PATH\n";
112 test 5, eval { `$echo 1` } eq '';
113 test 6, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
116 for (5..6) { print "ok $_\n" }
121 test 7, eval { `$echo 1` } eq "1\n";
122 test 8, $@ eq '', $@;
125 $ENV{'DCL$PATH'} = $TAINT;
126 test 9, eval { `$echo 1` } eq '';
127 test 10, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
129 $ENV{'DCL$PATH'} = $tmp;
130 test 11, eval { `$echo 1` } eq '';
131 test 12, $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
134 print "# can't find world-writeable directory to test DCL\$PATH\n";
135 for (11..12) { print "ok $_\n" }
137 $ENV{'DCL$PATH'} = '';
140 print "# This is not VMS\n";
141 for (9..12) { print "ok $_\n"; }
145 # Let's see that we can taint and untaint as needed.
148 test 13, tainted $foo;
150 # That was a sanity check. If it failed, stop the insanity!
151 die "Taint checks don't seem to be enabled" unless tainted $foo;
154 test 14, not tainted $foo;
157 test 15, tainted $foo;
160 test 16, not any_tainted @list;
161 taint_these @list[1,3,5,7,9];
162 test 17, any_tainted @list;
163 test 18, all_tainted @list[1,3,5,7,9];
164 test 19, not any_tainted @list[0,2,4,6,8];
166 ($foo) = $foo =~ /(.+)/;
167 test 20, not tainted $foo;
169 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
170 test 21, not tainted $foo;
171 test 22, $foo eq 'bar';
173 my $pi = 4 * atan2(1,1) + $TAINT0;
174 test 23, tainted $pi;
176 ($pi) = $pi =~ /(\d+\.\d+)/;
177 test 24, not tainted $pi;
178 test 25, sprintf("%.5f", $pi) eq '3.14159';
181 # How about command-line arguments? The problem is that we don't
182 # always get some, so we'll run another process with some.
185 open PROG, "> $arg" or die "Can't create $arg: $!";
187 eval { join('', @ARGV), kill 0 };
188 exit 0 if $@ =~ /^Insecure dependency/;
189 print "# Oops: \$@ was [$@]\n";
193 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
194 test 26, !$?, "Exited with status $?";
198 # Reading from a file should be tainted
201 test 27, open(FILE, $file), "Couldn't open '$file': $!";
204 sysread(FILE, $block, 100);
207 test 28, tainted $block;
208 test 29, tainted $line;
211 # Globs should be tainted.
213 # Some glob implementations need to spawn system programs.
214 local $ENV{PATH} = '';
215 $ENV{PATH} = (-l '/bin' ? '' : '/bin:') . '/usr/bin' unless $Is_VMS;
218 test 30, all_tainted @globs;
221 test 31, all_tainted @globs;
224 # Output of commands should be tainted
226 my $foo = `$echo abc`;
227 test 32, tainted $foo;
230 # Certain system variables should be tainted
232 test 33, all_tainted $^X, $0;
235 # Results of matching should all be untainted
237 my $foo = "abcdefghi" . $TAINT;
238 test 34, tainted $foo;
241 test 35, not any_tainted $`, $&, $';
243 $foo =~ /(...)(...)(...)/;
244 test 36, not any_tainted $1, $2, $3, $+;
246 my @bar = $foo =~ /(...)(...)(...)/;
247 test 37, not any_tainted @bar;
249 test 38, tainted $foo; # $foo should still be tainted!
250 test 39, $foo eq "abcdefghi";
253 # Operations which affect files can't use tainted data.
255 test 40, eval { chmod 0, $TAINT } eq '', 'chmod';
256 test 41, $@ =~ /^Insecure dependency/, $@;
258 # There is no feature test in $Config{} for truncate,
259 # so we allow for the possibility that it's missing.
260 test 42, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate';
261 test 43, $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
263 test 44, eval { rename '', $TAINT } eq '', 'rename';
264 test 45, $@ =~ /^Insecure dependency/, $@;
266 test 46, eval { unlink $TAINT } eq '', 'unlink';
267 test 47, $@ =~ /^Insecure dependency/, $@;
269 test 48, eval { utime $TAINT } eq '', 'utime';
270 test 49, $@ =~ /^Insecure dependency/, $@;
272 if ($Config{d_chown}) {
273 test 50, eval { chown -1, -1, $TAINT } eq '', 'chown';
274 test 51, $@ =~ /^Insecure dependency/, $@;
277 print "# chown() is not available\n";
278 for (50..51) { print "ok $_\n" }
281 if ($Config{d_link}) {
282 test 52, eval { link $TAINT, '' } eq '', 'link';
283 test 53, $@ =~ /^Insecure dependency/, $@;
286 print "# link() is not available\n";
287 for (52..53) { print "ok $_\n" }
290 if ($Config{d_symlink}) {
291 test 54, eval { symlink $TAINT, '' } eq '', 'symlink';
292 test 55, $@ =~ /^Insecure dependency/, $@;
295 print "# symlink() is not available\n";
296 for (54..55) { print "ok $_\n" }
300 # Operations which affect directories can't use tainted data.
302 test 56, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir';
303 test 57, $@ =~ /^Insecure dependency/, $@;
305 test 58, eval { rmdir $TAINT } eq '', 'rmdir';
306 test 59, $@ =~ /^Insecure dependency/, $@;
308 test 60, eval { chdir $TAINT } eq '', 'chdir';
309 test 61, $@ =~ /^Insecure dependency/, $@;
311 if ($Config{d_chroot}) {
312 test 62, eval { chroot $TAINT } eq '', 'chroot';
313 test 63, $@ =~ /^Insecure dependency/, $@;
316 print "# chroot() is not available\n";
317 for (62..63) { print "ok $_\n" }
321 # Some operations using files can't use tainted data.
323 my $foo = "imaginary library" . $TAINT;
324 test 64, eval { require $foo } eq '', 'require';
325 test 65, $@ =~ /^Insecure dependency/, $@;
327 my $filename = "./taintB$$"; # NB: $filename isn't tainted!
328 END { unlink $filename if defined $filename }
329 $foo = $filename . $TAINT;
330 unlink $filename; # in any case
332 test 66, eval { open FOO, $foo } eq '', 'open for read';
333 test 67, $@ eq '', $@; # NB: This should be allowed
334 test 68, $! == 2; # File not found
336 test 69, eval { open FOO, "> $foo" } eq '', 'open for write';
337 test 70, $@ =~ /^Insecure dependency/, $@;
340 # Commands to the system can't use tainted data
344 if ($^O eq 'amigaos') {
345 print "# open(\"|\") is not available\n";
346 for (71..74) { print "ok $_\n" }
349 test 71, eval { open FOO, "| $foo" } eq '', 'popen to';
350 test 72, $@ =~ /^Insecure dependency/, $@;
352 test 73, eval { open FOO, "$foo |" } eq '', 'popen from';
353 test 74, $@ =~ /^Insecure dependency/, $@;
356 test 75, eval { exec $TAINT } eq '', 'exec';
357 test 76, $@ =~ /^Insecure dependency/, $@;
359 test 77, eval { system $TAINT } eq '', 'system';
360 test 78, $@ =~ /^Insecure dependency/, $@;
365 test 79, eval { `$echo 1$foo` } eq '', 'backticks';
366 test 80, $@ =~ /^Insecure dependency/, $@;
368 if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe
369 test 81, join('', eval { glob $foo } ) ne '', 'globbing';
370 test 82, $@ eq '', $@;
373 test 81, join('', eval { glob $foo } ) eq '', 'globbing';
374 test 82, $@ =~ /^Insecure dependency/, $@;
378 # Operations which affect processes can't use tainted data.
380 test 83, eval { kill 0, $TAINT } eq '', 'kill';
381 test 84, $@ =~ /^Insecure dependency/, $@;
383 if ($Config{d_setpgrp}) {
384 test 85, eval { setpgrp 0, $TAINT } eq '', 'setpgrp';
385 test 86, $@ =~ /^Insecure dependency/, $@;
388 print "# setpgrp() is not available\n";
389 for (85..86) { print "ok $_\n" }
392 if ($Config{d_setprior}) {
393 test 87, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority';
394 test 88, $@ =~ /^Insecure dependency/, $@;
397 print "# setpriority() is not available\n";
398 for (87..88) { print "ok $_\n" }
402 # Some miscellaneous operations can't use tainted data.
404 if ($Config{d_syscall}) {
405 test 89, eval { syscall $TAINT } eq '', 'syscall';
406 test 90, $@ =~ /^Insecure dependency/, $@;
409 print "# syscall() is not available\n";
410 for (89..90) { print "ok $_\n" }
417 my $temp = "./taintC$$";
419 test 91, open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
421 test 92, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl';
422 test 93, $@ =~ /^Insecure dependency/, $@;
424 if ($Config{d_fcntl}) {
425 test 94, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl';
426 test 95, $@ =~ /^Insecure dependency/, $@;
429 print "# fcntl() is not available\n";
430 for (94..95) { print "ok $_\n" }
437 # Some tests involving references
439 my $foo = 'abc' . $TAINT;
441 test 96, not tainted $fooref;
442 test 97, tainted $$fooref;
443 test 98, tainted $foo;