Suppress a few compilation warnings in pp_hot.c.
[p5sagit/p5-mst-13.2.git] / t / op / taint.t
CommitLineData
1e422769 1#!./perl -T
2#
3# Taint tests by Tom Phoenix <rootbeer@teleport.com>.
4#
5# I don't claim to know all about tainting. If anyone sees
9607fc9c 6# tests that I've missed here, please add them. But this is
1e422769 7# better than having no tests at all, right?
8#
9
10BEGIN {
11 chdir 't' if -d 't';
20822f61 12 @INC = '../lib';
1e422769 13}
14
15use strict;
16use Config;
dc459aad 17use File::Spec::Functions;
1e422769 18
7c36658b 19my $test = 177;
1d662fb6 20sub ok ($;$) {
7c36658b 21 my($ok, $name) = @_;
22
23 # You have to do it this way or VMS will get confused.
24 print $ok ? "ok $test - $name\n" : "not ok $test - $name\n";
25
26 printf "# Failed test at line %d\n", (caller)[2] unless $ok;
27
28 $test++;
29 return $ok;
30}
31
32
0ecd3ba2 33$| = 1;
34
c9f931b8 35use vars qw($ipcsysv); # did we manage to load IPC::SysV?
36
3eeba6fb 37BEGIN {
38 if ($^O eq 'VMS' && !defined($Config{d_setenv})) {
39 $ENV{PATH} = $ENV{PATH};
40 $ENV{TERM} = $ENV{TERM} ne ''? $ENV{TERM} : 'dummy';
41 }
be3174d2 42 if ($Config{'extensions'} =~ /\bIPC\/SysV\b/
43 && ($Config{d_shm} || $Config{d_msg})) {
c9f931b8 44 eval { require IPC::SysV };
45 unless ($@) {
46 $ipcsysv++;
ddc3217d 47 IPC::SysV->import(qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU IPC_NOWAIT));
c9f931b8 48 }
b9d1c439 49 }
3eeba6fb 50}
51
dc459aad 52my $Is_MacOS = $^O eq 'MacOS';
1e422769 53my $Is_VMS = $^O eq 'VMS';
68dc0745 54my $Is_MSWin32 = $^O eq 'MSWin32';
2986a63f 55my $Is_NetWare = $^O eq 'NetWare';
39e571d4 56my $Is_Dos = $^O eq 'dos';
343d3f38 57my $Is_Cygwin = $^O eq 'cygwin';
68dc0745 58my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' :
2986a63f 59 ($Is_MSWin32 ? '.\perl' :
dc459aad 60 $Is_MacOS ? ':perl' :
2986a63f 61 ($Is_NetWare ? 'perl' : './perl'));
c90c0ff4 62my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
7bac28a0 63
1e422769 64if ($Is_VMS) {
7bac28a0 65 my (%old, $x);
66 for $x ('DCL$PATH', @MoreEnv) {
67 ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
68 }
1e422769 69 eval <<EndOfCleanup;
70 END {
3eeba6fb 71 \$ENV{PATH} = '' if $Config{d_setenv};
1e422769 72 warn "# Note: logical name 'PATH' may have been deleted\n";
562a7b0c 73 \@ENV{keys %old} = values %old;
1e422769 74 }
75EndOfCleanup
76}
77
78# Sources of taint:
79# The empty tainted value, for tainting strings
80my $TAINT = substr($^X, 0, 0);
81# A tainted zero, useful for tainting numbers
82my $TAINT0 = 0 + $TAINT;
83
84# This taints each argument passed. All must be lvalues.
85# Side effect: It also stringifies them. :-(
86sub taint_these (@) {
87 for (@_) { $_ .= $TAINT }
88}
89
90# How to identify taint when you see it
91sub any_tainted (@) {
92 not eval { join("",@_), kill 0; 1 };
93}
94sub tainted ($) {
95 any_tainted @_;
96}
97sub all_tainted (@) {
98 for (@_) { return 0 unless tainted $_ }
99 1;
100}
101
102sub test ($$;$) {
103 my($serial, $boolean, $diag) = @_;
104 if ($boolean) {
105 print "ok $serial\n";
106 } else {
107 print "not ok $serial\n";
108 for (split m/^/m, $diag) {
109 print "# $_";
110 }
9607fc9c 111 print "\n" unless
1e422769 112 $diag eq ''
113 or substr($diag, -1) eq "\n";
114 }
115}
116
117# We need an external program to call.
dc459aad 118my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : $Is_MacOS ? ":echo$$" : ($Is_NetWare ? "echo$$" : "./echo$$"));
1e422769 119END { unlink $ECHO }
120open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
121print PROG 'print "@ARGV\n"', "\n";
122close PROG;
123my $echo = "$Invoke_Perl $ECHO";
124
dc459aad 125my $TEST = catfile(curdir(), 'TEST');
126
a8c7c11a 127print "1..206\n";
1e422769 128
129# First, let's make sure that Perl is checking the dangerous
130# environment variables. Maybe they aren't set yet, so we'll
131# taint them ourselves.
132{
133 $ENV{'DCL$PATH'} = '' if $Is_VMS;
134
7bac28a0 135 $ENV{PATH} = '';
c90c0ff4 136 delete @ENV{@MoreEnv};
7bac28a0 137 $ENV{TERM} = 'dumb';
138
8852b6d2 139 if ($Is_Cygwin && ! -f 'cygwin1.dll') {
343d3f38 140 system("/usr/bin/cp /usr/bin/cygwin1.dll .") &&
141 die "$0: failed to cp cygwin1.dll: $!\n";
142 END { unlink "cygwin1.dll" } # yes, done for all platforms...
143 }
144
7bac28a0 145 test 1, eval { `$echo 1` } eq "1\n";
146
dc459aad 147 if ($Is_MSWin32 || $Is_NetWare || $Is_VMS || $Is_Dos || $Is_MacOS) {
7bac28a0 148 print "# Environment tainting tests skipped\n";
c90c0ff4 149 for (2..5) { print "ok $_\n" }
5aabfad6 150 }
151 else {
7bac28a0 152 my @vars = ('PATH', @MoreEnv);
153 while (my $v = $vars[0]) {
154 local $ENV{$v} = $TAINT;
155 last if eval { `$echo 1` };
156 last unless $@ =~ /^Insecure \$ENV{$v}/;
157 shift @vars;
158 }
159 test 2, !@vars, "\$$vars[0]";
c90c0ff4 160
161 # tainted $TERM is unsafe only if it contains metachars
162 local $ENV{TERM};
163 $ENV{TERM} = 'e=mc2';
164 test 3, eval { `$echo 1` } eq "1\n";
165 $ENV{TERM} = 'e=mc2' . $TAINT;
166 test 4, eval { `$echo 1` } eq '';
167 test 5, $@ =~ /^Insecure \$ENV{TERM}/, $@;
5aabfad6 168 }
7bac28a0 169
9607fc9c 170 my $tmp;
2986a63f 171 if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_NetWare || $Is_Dos) {
48c036b1 172 print "# all directories are writeable\n";
173 }
174 else {
9607fc9c 175 $tmp = (grep { defined and -d and (stat _)[2] & 2 }
099f76bb 176 qw(sys$scratch /tmp /var/tmp /usr/tmp),
9607fc9c 177 @ENV{qw(TMP TEMP)})[0]
178 or print "# can't find world-writeable directory to test PATH\n";
179 }
180
385588b3 181 if ($tmp) {
7bac28a0 182 local $ENV{PATH} = $tmp;
c90c0ff4 183 test 6, eval { `$echo 1` } eq '';
184 test 7, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
1e422769 185 }
186 else {
fac76ed7 187 for (6..7) { print "ok $_ # Skipped: all directories are writeable\n" }
1e422769 188 }
189
1e422769 190 if ($Is_VMS) {
191 $ENV{'DCL$PATH'} = $TAINT;
c90c0ff4 192 test 8, eval { `$echo 1` } eq '';
193 test 9, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
9607fc9c 194 if ($tmp) {
195 $ENV{'DCL$PATH'} = $tmp;
c90c0ff4 196 test 10, eval { `$echo 1` } eq '';
197 test 11, $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
9607fc9c 198 }
199 else {
fac76ed7 200 for (10..11) { print "ok $_ # Skipped: can't find world-writeable directory to test DCL\$PATH\n" }
9607fc9c 201 }
1e422769 202 $ENV{'DCL$PATH'} = '';
203 }
204 else {
fac76ed7 205 for (8..11) { print "ok $_ # Skipped: This is not VMS\n"; }
1e422769 206 }
207}
208
209# Let's see that we can taint and untaint as needed.
210{
211 my $foo = $TAINT;
c90c0ff4 212 test 12, tainted $foo;
9607fc9c 213
214 # That was a sanity check. If it failed, stop the insanity!
215 die "Taint checks don't seem to be enabled" unless tainted $foo;
1e422769 216
217 $foo = "foo";
c90c0ff4 218 test 13, not tainted $foo;
1e422769 219
220 taint_these($foo);
c90c0ff4 221 test 14, tainted $foo;
1e422769 222
223 my @list = 1..10;
c90c0ff4 224 test 15, not any_tainted @list;
1e422769 225 taint_these @list[1,3,5,7,9];
c90c0ff4 226 test 16, any_tainted @list;
227 test 17, all_tainted @list[1,3,5,7,9];
228 test 18, not any_tainted @list[0,2,4,6,8];
1e422769 229
230 ($foo) = $foo =~ /(.+)/;
c90c0ff4 231 test 19, not tainted $foo;
1e422769 232
233 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
c90c0ff4 234 test 20, not tainted $foo;
235 test 21, $foo eq 'bar';
1e422769 236
b3eb6a9b 237 {
238 use re 'taint';
239
240 ($foo) = ('bar' . $TAINT) =~ /(.+)/;
241 test 22, tainted $foo;
242 test 23, $foo eq 'bar';
243
244 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
245 test 24, tainted $foo;
246 test 25, $foo eq 'bar';
247 }
248
249 $foo = $1 if 'bar' =~ /(.+)$TAINT/;
250 test 26, tainted $foo;
251 test 27, $foo eq 'bar';
48c036b1 252
1e422769 253 my $pi = 4 * atan2(1,1) + $TAINT0;
b3eb6a9b 254 test 28, tainted $pi;
1e422769 255
256 ($pi) = $pi =~ /(\d+\.\d+)/;
b3eb6a9b 257 test 29, not tainted $pi;
258 test 30, sprintf("%.5f", $pi) eq '3.14159';
1e422769 259}
260
261# How about command-line arguments? The problem is that we don't
262# always get some, so we'll run another process with some.
dc459aad 263SKIP: {
264 my $arg = catfile(curdir(), "arg$$");
1e422769 265 open PROG, "> $arg" or die "Can't create $arg: $!";
266 print PROG q{
267 eval { join('', @ARGV), kill 0 };
268 exit 0 if $@ =~ /^Insecure dependency/;
269 print "# Oops: \$@ was [$@]\n";
270 exit 1;
271 };
272 close PROG;
273 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
b3eb6a9b 274 test 31, !$?, "Exited with status $?";
1e422769 275 unlink $arg;
276}
277
278# Reading from a file should be tainted
279{
dc459aad 280 test 32, open(FILE, $TEST), "Couldn't open '$TEST': $!";
1e422769 281
282 my $block;
283 sysread(FILE, $block, 100);
9607fc9c 284 my $line = <FILE>;
1e422769 285 close FILE;
b3eb6a9b 286 test 33, tainted $block;
287 test 34, tainted $line;
1e422769 288}
289
c90c0ff4 290# Globs should be forbidden, except under VMS,
291# which doesn't spawn an external program.
72b16652 292if (1 # built-in glob
293 or $Is_VMS) {
b3eb6a9b 294 for (35..36) { print "ok $_\n"; }
c90c0ff4 295}
296else {
7bac28a0 297 my @globs = eval { <*> };
b3eb6a9b 298 test 35, @globs == 0 && $@ =~ /^Insecure dependency/;
1e422769 299
7bac28a0 300 @globs = eval { glob '*' };
b3eb6a9b 301 test 36, @globs == 0 && $@ =~ /^Insecure dependency/;
1e422769 302}
303
304# Output of commands should be tainted
305{
306 my $foo = `$echo abc`;
b3eb6a9b 307 test 37, tainted $foo;
1e422769 308}
309
310# Certain system variables should be tainted
311{
b3eb6a9b 312 test 38, all_tainted $^X, $0;
1e422769 313}
314
315# Results of matching should all be untainted
316{
317 my $foo = "abcdefghi" . $TAINT;
b3eb6a9b 318 test 39, tainted $foo;
1e422769 319
320 $foo =~ /def/;
b3eb6a9b 321 test 40, not any_tainted $`, $&, $';
1e422769 322
323 $foo =~ /(...)(...)(...)/;
b3eb6a9b 324 test 41, not any_tainted $1, $2, $3, $+;
1e422769 325
326 my @bar = $foo =~ /(...)(...)(...)/;
b3eb6a9b 327 test 42, not any_tainted @bar;
1e422769 328
b3eb6a9b 329 test 43, tainted $foo; # $foo should still be tainted!
330 test 44, $foo eq "abcdefghi";
1e422769 331}
332
333# Operations which affect files can't use tainted data.
334{
b3eb6a9b 335 test 45, eval { chmod 0, $TAINT } eq '', 'chmod';
336 test 46, $@ =~ /^Insecure dependency/, $@;
1e422769 337
9607fc9c 338 # There is no feature test in $Config{} for truncate,
339 # so we allow for the possibility that it's missing.
b3eb6a9b 340 test 47, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate';
341 test 48, $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
1e422769 342
b3eb6a9b 343 test 49, eval { rename '', $TAINT } eq '', 'rename';
344 test 50, $@ =~ /^Insecure dependency/, $@;
1e422769 345
b3eb6a9b 346 test 51, eval { unlink $TAINT } eq '', 'unlink';
347 test 52, $@ =~ /^Insecure dependency/, $@;
9607fc9c 348
b3eb6a9b 349 test 53, eval { utime $TAINT } eq '', 'utime';
350 test 54, $@ =~ /^Insecure dependency/, $@;
48c036b1 351
1e422769 352 if ($Config{d_chown}) {
b3eb6a9b 353 test 55, eval { chown -1, -1, $TAINT } eq '', 'chown';
354 test 56, $@ =~ /^Insecure dependency/, $@;
1e422769 355 }
356 else {
b3eb6a9b 357 for (55..56) { print "ok $_ # Skipped: chown() is not available\n" }
1e422769 358 }
359
360 if ($Config{d_link}) {
b3eb6a9b 361 test 57, eval { link $TAINT, '' } eq '', 'link';
362 test 58, $@ =~ /^Insecure dependency/, $@;
1e422769 363 }
364 else {
b3eb6a9b 365 for (57..58) { print "ok $_ # Skipped: link() is not available\n" }
1e422769 366 }
367
368 if ($Config{d_symlink}) {
b3eb6a9b 369 test 59, eval { symlink $TAINT, '' } eq '', 'symlink';
370 test 60, $@ =~ /^Insecure dependency/, $@;
1e422769 371 }
372 else {
b3eb6a9b 373 for (59..60) { print "ok $_ # Skipped: symlink() is not available\n" }
1e422769 374 }
375}
376
377# Operations which affect directories can't use tainted data.
378{
b3eb6a9b 379 test 61, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir';
380 test 62, $@ =~ /^Insecure dependency/, $@;
1e422769 381
b3eb6a9b 382 test 63, eval { rmdir $TAINT } eq '', 'rmdir';
383 test 64, $@ =~ /^Insecure dependency/, $@;
9607fc9c 384
b3eb6a9b 385 test 65, eval { chdir $TAINT } eq '', 'chdir';
386 test 66, $@ =~ /^Insecure dependency/, $@;
48c036b1 387
1e422769 388 if ($Config{d_chroot}) {
b3eb6a9b 389 test 67, eval { chroot $TAINT } eq '', 'chroot';
390 test 68, $@ =~ /^Insecure dependency/, $@;
1e422769 391 }
392 else {
b3eb6a9b 393 for (67..68) { print "ok $_ # Skipped: chroot() is not available\n" }
1e422769 394 }
395}
396
397# Some operations using files can't use tainted data.
398{
399 my $foo = "imaginary library" . $TAINT;
b3eb6a9b 400 test 69, eval { require $foo } eq '', 'require';
401 test 70, $@ =~ /^Insecure dependency/, $@;
1e422769 402
403 my $filename = "./taintB$$"; # NB: $filename isn't tainted!
404 END { unlink $filename if defined $filename }
405 $foo = $filename . $TAINT;
406 unlink $filename; # in any case
407
b3eb6a9b 408 test 71, eval { open FOO, $foo } eq '', 'open for read';
409 test 72, $@ eq '', $@; # NB: This should be allowed
9d116dd7 410
411 # Try first new style but allow also old style.
327ccce1 412 # We do not want the whole taint.t to fail
413 # just because Errno possibly failing.
414 test 73, eval('$!{ENOENT}') ||
61ae2fbf 415 $! == 2 || # File not found
416 ($Is_Dos && $! == 22) ||
417 ($^O eq 'mint' && $! == 33);
1e422769 418
b3eb6a9b 419 test 74, eval { open FOO, "> $foo" } eq '', 'open for write';
420 test 75, $@ =~ /^Insecure dependency/, $@;
1e422769 421}
422
423# Commands to the system can't use tainted data
424{
425 my $foo = $TAINT;
426
427 if ($^O eq 'amigaos') {
b3eb6a9b 428 for (76..79) { print "ok $_ # Skipped: open('|') is not available\n" }
1e422769 429 }
430 else {
06eaf0bc 431 test 76, eval { open FOO, "| x$foo" } eq '', 'popen to';
b3eb6a9b 432 test 77, $@ =~ /^Insecure dependency/, $@;
1e422769 433
06eaf0bc 434 test 78, eval { open FOO, "x$foo |" } eq '', 'popen from';
b3eb6a9b 435 test 79, $@ =~ /^Insecure dependency/, $@;
48c036b1 436 }
1e422769 437
b3eb6a9b 438 test 80, eval { exec $TAINT } eq '', 'exec';
439 test 81, $@ =~ /^Insecure dependency/, $@;
9607fc9c 440
b3eb6a9b 441 test 82, eval { system $TAINT } eq '', 'system';
442 test 83, $@ =~ /^Insecure dependency/, $@;
48c036b1 443
1e422769 444 $foo = "*";
445 taint_these $foo;
446
b3eb6a9b 447 test 84, eval { `$echo 1$foo` } eq '', 'backticks';
448 test 85, $@ =~ /^Insecure dependency/, $@;
1e422769 449
450 if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe
b3eb6a9b 451 test 86, join('', eval { glob $foo } ) ne '', 'globbing';
452 test 87, $@ eq '', $@;
1e422769 453 }
454 else {
159f47d9 455 for (86..87) { print "ok $_ # Skipped: This is not VMS\n"; }
1e422769 456 }
457}
458
459# Operations which affect processes can't use tainted data.
460{
b3eb6a9b 461 test 88, eval { kill 0, $TAINT } eq '', 'kill';
462 test 89, $@ =~ /^Insecure dependency/, $@;
1e422769 463
464 if ($Config{d_setpgrp}) {
b3eb6a9b 465 test 90, eval { setpgrp 0, $TAINT } eq '', 'setpgrp';
466 test 91, $@ =~ /^Insecure dependency/, $@;
1e422769 467 }
468 else {
b3eb6a9b 469 for (90..91) { print "ok $_ # Skipped: setpgrp() is not available\n" }
1e422769 470 }
471
472 if ($Config{d_setprior}) {
b3eb6a9b 473 test 92, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority';
474 test 93, $@ =~ /^Insecure dependency/, $@;
1e422769 475 }
476 else {
b3eb6a9b 477 for (92..93) { print "ok $_ # Skipped: setpriority() is not available\n" }
1e422769 478 }
479}
480
481# Some miscellaneous operations can't use tainted data.
482{
483 if ($Config{d_syscall}) {
b3eb6a9b 484 test 94, eval { syscall $TAINT } eq '', 'syscall';
485 test 95, $@ =~ /^Insecure dependency/, $@;
1e422769 486 }
487 else {
b3eb6a9b 488 for (94..95) { print "ok $_ # Skipped: syscall() is not available\n" }
1e422769 489 }
490
491 {
492 my $foo = "x" x 979;
493 taint_these $foo;
494 local *FOO;
495 my $temp = "./taintC$$";
496 END { unlink $temp }
b3eb6a9b 497 test 96, open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
1e422769 498
b3eb6a9b 499 test 97, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl';
500 test 98, $@ =~ /^Insecure dependency/, $@;
1e422769 501
502 if ($Config{d_fcntl}) {
b3eb6a9b 503 test 99, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl';
504 test 100, $@ =~ /^Insecure dependency/, $@;
1e422769 505 }
506 else {
b3eb6a9b 507 for (99..100) { print "ok $_ # Skipped: fcntl() is not available\n" }
1e422769 508 }
509
510 close FOO;
511 }
512}
513
9607fc9c 514# Some tests involving references
1e422769 515{
516 my $foo = 'abc' . $TAINT;
517 my $fooref = \$foo;
b3eb6a9b 518 test 101, not tainted $fooref;
519 test 102, tainted $$fooref;
520 test 103, tainted $foo;
1e422769 521}
54310121 522
523# Some tests involving assignment
524{
525 my $foo = $TAINT0;
526 my $bar = $foo;
b3eb6a9b 527 test 104, all_tainted $foo, $bar;
528 test 105, tainted($foo = $bar);
529 test 106, tainted($bar = $bar);
530 test 107, tainted($bar += $bar);
531 test 108, tainted($bar -= $bar);
532 test 109, tainted($bar *= $bar);
533 test 110, tainted($bar++);
534 test 111, tainted($bar /= $bar);
535 test 112, tainted($bar += 0);
536 test 113, tainted($bar -= 2);
537 test 114, tainted($bar *= -1);
538 test 115, tainted($bar /= 1);
539 test 116, tainted($bar--);
540 test 117, $bar == 0;
54310121 541}
a1f49e72 542
543# Test assignment and return of lists
544{
545 my @foo = ("A", "tainted" . $TAINT, "B");
b3eb6a9b 546 test 118, not tainted $foo[0];
547 test 119, tainted $foo[1];
548 test 120, not tainted $foo[2];
a1f49e72 549 my @bar = @foo;
b3eb6a9b 550 test 121, not tainted $bar[0];
551 test 122, tainted $bar[1];
552 test 123, not tainted $bar[2];
a1f49e72 553 my @baz = eval { "A", "tainted" . $TAINT, "B" };
b3eb6a9b 554 test 124, not tainted $baz[0];
555 test 125, tainted $baz[1];
556 test 126, not tainted $baz[2];
a1f49e72 557 my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
b3eb6a9b 558 test 127, not tainted $plugh[0];
559 test 128, tainted $plugh[1];
560 test 129, not tainted $plugh[2];
a1f49e72 561 my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
b3eb6a9b 562 test 130, not tainted ((&$nautilus)[0]);
563 test 131, tainted ((&$nautilus)[1]);
564 test 132, not tainted ((&$nautilus)[2]);
a1f49e72 565 my @xyzzy = &$nautilus;
b3eb6a9b 566 test 133, not tainted $xyzzy[0];
567 test 134, tainted $xyzzy[1];
568 test 135, not tainted $xyzzy[2];
a1f49e72 569 my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
b3eb6a9b 570 test 136, not tainted ((&$red_october)[0]);
571 test 137, tainted ((&$red_october)[1]);
572 test 138, not tainted ((&$red_october)[2]);
a1f49e72 573 my @corge = &$red_october;
b3eb6a9b 574 test 139, not tainted $corge[0];
575 test 140, tainted $corge[1];
576 test 141, not tainted $corge[2];
a1f49e72 577}
fb73857a 578
579# Test for system/library calls returning string data of dubious origin.
580{
581 # No reliable %Config check for getpw*
5dec6e32 582 if (eval { setpwent(); getpwent() }) {
fb73857a 583 setpwent();
584 my @getpwent = getpwent();
585 die "getpwent: $!\n" unless (@getpwent);
b3eb6a9b 586 test 142,( not tainted $getpwent[0]
2959b6e3 587 and tainted $getpwent[1]
fb73857a 588 and not tainted $getpwent[2]
589 and not tainted $getpwent[3]
590 and not tainted $getpwent[4]
591 and not tainted $getpwent[5]
3972a534 592 and tainted $getpwent[6] # ge?cos
fb73857a 593 and not tainted $getpwent[7]
3972a534 594 and tainted $getpwent[8]); # shell
fb73857a 595 endpwent();
596 } else {
b3eb6a9b 597 for (142) { print "ok $_ # Skipped: getpwent() is not available\n" }
fb73857a 598 }
599
600 if ($Config{d_readdir}) { # pretty hard to imagine not
601 local(*D);
602 opendir(D, "op") or die "opendir: $!\n";
603 my $readdir = readdir(D);
b3eb6a9b 604 test 143, tainted $readdir;
fb73857a 605 closedir(OP);
606 } else {
b3eb6a9b 607 for (143) { print "ok $_ # Skipped: readdir() is not available\n" }
fb73857a 608 }
609
610 if ($Config{d_readlink} && $Config{d_symlink}) {
611 my $symlink = "sl$$";
612 unlink($symlink);
dc459aad 613 my $sl = "/something/naughty";
614 # it has to be a real path on Mac OS
615 $sl = MacPerl::MakePath((MacPerl::Volumes())[0]) if $Is_MacOS;
616 symlink($sl, $symlink) or die "symlink: $!\n";
fb73857a 617 my $readlink = readlink($symlink);
b3eb6a9b 618 test 144, tainted $readlink;
fb73857a 619 unlink($symlink);
620 } else {
b3eb6a9b 621 for (144) { print "ok $_ # Skipped: readlink() or symlink() is not available\n"; }
fb73857a 622 }
623}
624
625# test bitwise ops (regression bug)
626{
627 my $why = "y";
628 my $j = "x" | $why;
b3eb6a9b 629 test 145, not tainted $j;
fb73857a 630 $why = $TAINT."y";
631 $j = "x" | $why;
b3eb6a9b 632 test 146, tainted $j;
fb73857a 633}
634
48c036b1 635# test target of substitution (regression bug)
636{
637 my $why = $TAINT."y";
638 $why =~ s/y/z/;
b3eb6a9b 639 test 147, tainted $why;
48c036b1 640
641 my $z = "[z]";
642 $why =~ s/$z/zee/;
b3eb6a9b 643 test 148, tainted $why;
48c036b1 644
645 $why =~ s/e/'-'.$$/ge;
b3eb6a9b 646 test 149, tainted $why;
48c036b1 647}
d929ce6f 648
649# test shmread
650{
c9f931b8 651 unless ($ipcsysv) {
652 print "ok 150 # skipped: no IPC::SysV\n";
653 last;
654 }
be3174d2 655 if ($Config{'extensions'} =~ /\bIPC\/SysV\b/ && $Config{d_shm}) {
b9d1c439 656 no strict 'subs';
d929ce6f 657 my $sent = "foobar";
658 my $rcvd;
659 my $size = 2000;
c2e66d9e 660 my $id = shmget(IPC_PRIVATE, $size, S_IRWXU);
661
41d6edb2 662 if (defined $id) {
663 if (shmwrite($id, $sent, 0, 60)) {
664 if (shmread($id, $rcvd, 0, 60)) {
d929ce6f 665 substr($rcvd, index($rcvd, "\0")) = '';
666 } else {
667 warn "# shmread failed: $!\n";
668 }
669 } else {
670 warn "# shmwrite failed: $!\n";
671 }
c2e66d9e 672 shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n";
41d6edb2 673 } else {
674 warn "# shmget failed: $!\n";
d929ce6f 675 }
676
677 if ($rcvd eq $sent) {
678 test 150, tainted $rcvd;
679 } else {
680 print "ok 150 # Skipped: SysV shared memory operation failed\n";
681 }
682 } else {
41d6edb2 683 print "ok 150 # Skipped: SysV shared memory is not available\n";
d929ce6f 684 }
685}
41d6edb2 686
687# test msgrcv
688{
c9f931b8 689 unless ($ipcsysv) {
690 print "ok 151 # skipped: no IPC::SysV\n";
691 last;
692 }
be3174d2 693 if ($Config{'extensions'} =~ /\bIPC\/SysV\b/ && $Config{d_msg}) {
b9d1c439 694 no strict 'subs';
41d6edb2 695 my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
696
697 my $sent = "message";
698 my $type_sent = 1234;
699 my $rcvd;
700 my $type_rcvd;
701
702 if (defined $id) {
ddc3217d 703 if (msgsnd($id, pack("l! a*", $type_sent, $sent), IPC_NOWAIT)) {
704 if (msgrcv($id, $rcvd, 60, 0, IPC_NOWAIT)) {
41d6edb2 705 ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd);
706 } else {
ddc3217d 707 warn "# msgrcv failed: $!\n";
41d6edb2 708 }
709 } else {
ddc3217d 710 warn "# msgsnd failed: $!\n";
41d6edb2 711 }
c2e66d9e 712 msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n";
41d6edb2 713 } else {
714 warn "# msgget failed\n";
715 }
716
717 if ($rcvd eq $sent && $type_sent == $type_rcvd) {
718 test 151, tainted $rcvd;
719 } else {
720 print "ok 151 # Skipped: SysV message queue operation failed\n";
721 }
722 } else {
723 print "ok 151 # Skipped: SysV message queues are not available\n";
724 }
725}
726
3887d568 727{
728 # bug id 20001004.006
729
dc459aad 730 open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
3887d568 731 local $/;
732 my $a = <IN>;
733 my $b = <IN>;
734 print "not " unless tainted($a) && tainted($b) && !defined($b);
735 print "ok 152\n";
27c9684d 736 close IN;
3887d568 737}
27c9684d 738
739{
740 # bug id 20001004.007
741
dc459aad 742 open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
27c9684d 743 my $a = <IN>;
744
745 my $c = { a => 42,
746 b => $a };
747 print "not " unless !tainted($c->{a}) && tainted($c->{b});
748 print "ok 153\n";
749
750 my $d = { a => $a,
751 b => 42 };
752 print "not " unless tainted($d->{a}) && !tainted($d->{b});
753 print "ok 154\n";
754
755 my $e = { a => 42,
756 b => { c => $a, d => 42 } };
757 print "not " unless !tainted($e->{a}) &&
758 !tainted($e->{b}) &&
759 tainted($e->{b}->{c}) &&
760 !tainted($e->{b}->{d});
761 print "ok 155\n";
762
763 close IN;
764}
765
b94c04ac 766{
767 # bug id 20010519.003
768
248c32c0 769 BEGIN {
770 use vars qw($has_fcntl);
771 eval { require Fcntl; import Fcntl; };
772 unless ($@) {
773 $has_fcntl = 1;
774 }
775 }
b94c04ac 776
248c32c0 777 unless ($has_fcntl) {
778 for (156..173) {
779 print "ok $_ # Skip: no Fcntl (no dynaloading?)\n";
780 }
781 } else {
782 my $evil = "foo" . $TAINT;
783
784 eval { sysopen(my $ro, $evil, &O_RDONLY) };
785 test 156, $@ !~ /^Insecure dependency/, $@;
786
787 eval { sysopen(my $wo, $evil, &O_WRONLY) };
788 test 157, $@ =~ /^Insecure dependency/, $@;
789
790 eval { sysopen(my $rw, $evil, &O_RDWR) };
791 test 158, $@ =~ /^Insecure dependency/, $@;
792
793 eval { sysopen(my $ap, $evil, &O_APPEND) };
794 test 159, $@ =~ /^Insecure dependency/, $@;
795
796 eval { sysopen(my $cr, $evil, &O_CREAT) };
797 test 160, $@ =~ /^Insecure dependency/, $@;
798
799 eval { sysopen(my $tr, $evil, &O_TRUNC) };
800 test 161, $@ =~ /^Insecure dependency/, $@;
801
802 eval { sysopen(my $ro, "foo", &O_RDONLY | $evil) };
803 test 162, $@ !~ /^Insecure dependency/, $@;
804
805 eval { sysopen(my $wo, "foo", &O_WRONLY | $evil) };
806 test 163, $@ =~ /^Insecure dependency/, $@;
807
808 eval { sysopen(my $rw, "foo", &O_RDWR | $evil) };
809 test 164, $@ =~ /^Insecure dependency/, $@;
810
811 eval { sysopen(my $ap, "foo", &O_APPEND | $evil) };
812 test 165, $@ =~ /^Insecure dependency/, $@;
813
814 eval { sysopen(my $cr, "foo", &O_CREAT | $evil) };
815 test 166, $@ =~ /^Insecure dependency/, $@;
816
817 eval { sysopen(my $tr, "foo", &O_TRUNC | $evil) };
818 test 167, $@ =~ /^Insecure dependency/, $@;
819
820 eval { sysopen(my $ro, "foo", &O_RDONLY, $evil) };
821 test 168, $@ !~ /^Insecure dependency/, $@;
822
823 eval { sysopen(my $wo, "foo", &O_WRONLY, $evil) };
824 test 169, $@ =~ /^Insecure dependency/, $@;
825
826 eval { sysopen(my $rw, "foo", &O_RDWR, $evil) };
827 test 170, $@ =~ /^Insecure dependency/, $@;
828
829 eval { sysopen(my $ap, "foo", &O_APPEND, $evil) };
830 test 171, $@ =~ /^Insecure dependency/, $@;
831
832 eval { sysopen(my $cr, "foo", &O_CREAT, $evil) };
833 test 172, $@ =~ /^Insecure dependency/, $@;
834
835 eval { sysopen(my $tr, "foo", &O_TRUNC, $evil) };
836 test 173, $@ =~ /^Insecure dependency/, $@;
837
838 unlink("foo"); # not unlink($evil), because that would fail...
839 }
b94c04ac 840}
841
65811bc3 842{
843 # bug 20010526.004
844
845 use warnings;
846
bbd7eb8a 847 local $SIG{__WARN__} = sub { print "not " };
65811bc3 848
849 sub fmi {
850 my $divnum = shift()/1;
851 sprintf("%1.1f\n", $divnum);
852 }
853
854 fmi(21 . $TAINT);
855 fmi(37);
856 fmi(248);
857
858 print "ok 174\n";
859}
860
9e1b5a4e 861
862{
863 # Bug ID 20010730.010
864
865 my $i = 0;
866
867 sub Tie::TIESCALAR {
868 my $class = shift;
869 my $arg = shift;
870
871 bless \$arg => $class;
872 }
873
874 sub Tie::FETCH {
875 $i ++;
876 ${$_ [0]}
877 }
878
879
880 package main;
881
882 my $bar = "The Big Bright Green Pleasure Machine";
883 taint_these $bar;
884 tie my ($foo), Tie => $bar;
885
886 my $baz = $foo;
887
888 print $i == 1 ? "ok 175\n" : "not ok 175\n"
889
890}
891
8852b6d2 892{
893 # Check that all environment variables are tainted.
894 my @untainted;
895 while (my ($k, $v) = each %ENV) {
896 if (!tainted($v) &&
eb25aaf6 897 # These we have explicitly untainted or set earlier.
898 $k !~ /^(BASH_ENV|CDPATH|ENV|IFS|PATH|PERL_CORE|TEMP|TERM|TMP)$/) {
8852b6d2 899 push @untainted, "# '$k' = '$v'\n";
900 }
901 }
902 print @untainted == 0 ? "ok 176\n" : "not ok 176\n";
903 print "# untainted:\n", @untainted if @untainted;
904}
9e1b5a4e 905
906
9aa05f58 907ok( ${^TAINT} == 1, '$^TAINT is on' );
7c36658b 908
909eval { ${^TAINT} = 0 };
910ok( ${^TAINT}, '$^TAINT is not assignable' );
911ok( $@ =~ /^Modification of a read-only value attempted/,
c212f17f 912 'Assigning to ${^TAINT} fails' );
7c36658b 913
e08e52cf 914{
915 # bug 20011111.105
916
917 my $re1 = qr/x$TAINT/;
918 test 180, tainted $re1;
919
920 my $re2 = qr/^$re1\z/;
921 test 181, tainted $re2;
922
923 my $re3 = "$re2";
924 test 182, tainted $re3;
925}
52a55424 926
d5b53b20 927if ($Is_MSWin32) {
928 print "ok 183 # Skipped: system {} has different semantics\n";
929}
930else
52a55424 931{
932 # bug 20010221.005
933 local $ENV{PATH} .= $TAINT;
934 eval { system { "echo" } "/arg0", "arg1" };
935 test 183, $@ =~ /^Insecure \$ENV/;
936}
161720b2 937if ($Is_VMS) {
0304a67e 938 for (184..205) {print "not ok $_ # TODO tainted %ENV warning occludes tainted arguments warning\n";}
161720b2 939}
940else
bbd7eb8a 941{
942 # bug 20020208.005 plus some extras
943 # single arg exec/system are tests 80-83
06bd1802 944 my $err = qr/^Insecure dependency/ ;
bbd7eb8a 945 test 184, eval { exec $TAINT, $TAINT } eq '', 'exec';
946 test 185, $@ =~ $err, $@;
947 test 186, eval { exec $TAINT $TAINT } eq '', 'exec';
948 test 187, $@ =~ $err, $@;
949 test 188, eval { exec $TAINT $TAINT, $TAINT } eq '', 'exec';
950 test 189, $@ =~ $err, $@;
951 test 190, eval { exec $TAINT 'notaint' } eq '', 'exec';
952 test 191, $@ =~ $err, $@;
953 test 192, eval { exec {'notaint'} $TAINT } eq '', 'exec';
954 test 193, $@ =~ $err, $@;
955
956 test 194, eval { system $TAINT, $TAINT } eq '', 'system';
957 test 195, $@ =~ $err, $@;
159f47d9 958 test 196, eval { system $TAINT $TAINT } eq '', 'system';
bbd7eb8a 959 test 197, $@ =~ $err, $@;
159f47d9 960 test 198, eval { system $TAINT $TAINT, $TAINT } eq '', 'system';
bbd7eb8a 961 test 199, $@ =~ $err, $@;
159f47d9 962 test 200, eval { system $TAINT 'notaint' } eq '', 'system';
bbd7eb8a 963 test 201, $@ =~ $err, $@;
159f47d9 964 test 202, eval { system {'notaint'} $TAINT } eq '', 'system';
bbd7eb8a 965 test 203, $@ =~ $err, $@;
159f47d9 966
967 eval { system("lskdfj does not exist","with","args"); };
968 test 204, $@ eq '';
63c6dcc1 969 if ($Is_MacOS) {
970 print "ok 205 # no exec()\n";
971 } else {
972 eval { exec("lskdfj does not exist","with","args"); };
973 test 205, $@ eq '';
974 }
6c8794e1 975
976 # If you add tests here update also the above skip block for VMS.
bbd7eb8a 977}
a8c7c11a 978
979{
980 # [ID 20020704.001] taint propagation failure
981 use re 'taint';
982 $TAINT =~ /(.*)/;
983 test 206, tainted(my $foo = $1);
984}