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?
13 require './loc_tools.pl';
24 use vars qw($ipcsysv); # did we manage to load IPC::SysV?
26 my ($old_env_path, $old_env_dcl_path, $old_env_term);
28 $old_env_path = $ENV{'PATH'};
29 $old_env_dcl_path = $ENV{'DCL$PATH'};
30 $old_env_term = $ENV{'TERM'};
31 if ($^O eq 'VMS' && !defined($Config{d_setenv})) {
32 $ENV{PATH} = $ENV{PATH};
33 $ENV{TERM} = $ENV{TERM} ne ''? $ENV{TERM} : 'dummy';
35 if ($Config{'extensions'} =~ /\bIPC\/SysV\b/
36 && ($Config{d_shm} || $Config{d_msg})) {
37 eval { require IPC::SysV };
40 IPC::SysV->import(qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU IPC_NOWAIT));
45 my $Is_VMS = $^O eq 'VMS';
46 my $Is_MSWin32 = $^O eq 'MSWin32';
47 my $Is_NetWare = $^O eq 'NetWare';
48 my $Is_Dos = $^O eq 'dos';
49 my $Is_Cygwin = $^O eq 'cygwin';
50 my $Is_OpenBSD = $^O eq 'openbsd';
51 my $Is_MirBSD = $^O eq 'mirbsd';
52 my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.exe' :
53 $Is_MSWin32 ? '.\perl' :
54 $Is_NetWare ? 'perl' :
56 my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
60 for $x ('DCL$PATH', @MoreEnv) {
61 ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
63 # VMS note: PATH and TERM are automatically created by the C
64 # library in VMS on reference to the their keys in %ENV.
65 # There is currently no way to determine if they did not exist
66 # before this test was run.
69 \$ENV{PATH} = \$old_env_path;
70 warn "# Note: logical name 'PATH' may have been created\n";
71 \$ENV{'TERM'} = \$old_env_term;
72 warn "# Note: logical name 'TERM' may have been created\n";
73 \@ENV{keys %old} = values %old;
74 if (defined \$old_env_dcl_path) {
75 \$ENV{'DCL\$PATH'} = \$old_env_dcl_path;
77 delete \$ENV{'DCL\$PATH'};
84 # The empty tainted value, for tainting strings
85 my $TAINT = substr($^X, 0, 0);
86 # A tainted zero, useful for tainting numbers
93 # This taints each argument passed. All must be lvalues.
94 # Side effect: It also stringifies them. :-(
96 for (@_) { $_ .= $TAINT }
99 # How to identify taint when you see it
101 local $@; # Don't pollute caller's value.
102 not eval { join("",@_), kill 0; 1 };
107 local $::Level = $::Level + 1;
108 ok(tainted($thing), @_);
113 local $::Level = $::Level + 1;
114 ok(!tainted($thing), @_);
118 my ($code, $what, $desc) = @_;
120 local $::Level = $::Level + 1;
121 is(eval { $code->(); }, undef, $desc);
122 like($@, qr/^Insecure dependency in $what while running with -T switch/);
125 # We need an external program to call.
126 my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : ($Is_NetWare ? "echo$$" : "./echo$$"));
128 open my $fh, '>', $ECHO or die "Can't create $ECHO: $!";
129 print $fh 'print "@ARGV\n"', "\n";
131 my $echo = "$Invoke_Perl $ECHO";
135 # First, let's make sure that Perl is checking the dangerous
136 # environment variables. Maybe they aren't set yet, so we'll
137 # taint them ourselves.
139 $ENV{'DCL$PATH'} = '' if $Is_VMS;
141 $ENV{PATH} = ($Is_Cygwin) ? '/usr/bin' : '';
142 delete @ENV{@MoreEnv};
145 is(eval { `$echo 1` }, "1\n");
148 skip "Environment tainting tests skipped", 4
149 if $Is_MSWin32 || $Is_NetWare || $Is_VMS || $Is_Dos;
151 my @vars = ('PATH', @MoreEnv);
152 while (my $v = $vars[0]) {
153 local $ENV{$v} = $TAINT;
154 last if eval { `$echo 1` };
155 last unless $@ =~ /^Insecure \$ENV\{$v}/;
160 # tainted $TERM is unsafe only if it contains metachars
162 $ENV{TERM} = 'e=mc2';
163 is(eval { `$echo 1` }, "1\n");
164 $ENV{TERM} = 'e=mc2' . $TAINT;
165 is(eval { `$echo 1` }, undef);
166 like($@, qr/^Insecure \$ENV\{TERM}/);
170 if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_NetWare || $Is_Dos) {
171 print "# all directories are writeable\n";
174 $tmp = (grep { defined and -d and (stat _)[2] & 2 }
175 qw(sys$scratch /tmp /var/tmp /usr/tmp),
176 @ENV{qw(TMP TEMP)})[0]
177 or print "# can't find world-writeable directory to test PATH\n";
181 skip "all directories are writeable", 2 unless $tmp;
183 local $ENV{PATH} = $tmp;
184 is(eval { `$echo 1` }, undef);
185 # Message can be different depending on whether echo
186 # is a builtin or not
187 like($@, qr/^Insecure (?:directory in )?\$ENV\{PATH}/);
191 skip "This is not VMS", 4 unless $Is_VMS;
193 $ENV{'DCL$PATH'} = $TAINT;
194 is(eval { `$echo 1` }, undef);
195 like($@, qr/^Insecure \$ENV\{DCL\$PATH}/);
197 skip q[can't find world-writeable directory to test DCL$PATH], 2
200 $ENV{'DCL$PATH'} = $tmp;
201 is(eval { `$echo 1` }, undef);
202 like($@, qr/^Insecure directory in \$ENV\{DCL\$PATH}/);
204 $ENV{'DCL$PATH'} = '';
208 # Let's see that we can taint and untaint as needed.
213 # That was a sanity check. If it failed, stop the insanity!
214 die "Taint checks don't seem to be enabled" unless tainted $foo;
223 isnt_tainted($_) foreach @list;
224 taint_these @list[1,3,5,7,9];
225 is_tainted($_) foreach @list[1,3,5,7,9];
226 isnt_tainted($_) foreach @list[0,2,4,6,8];
228 ($foo) = $foo =~ /(.+)/;
231 my ($desc, $s, $res, $res2, $one);
233 $desc = "match with string tainted";
235 $s = 'abcd' . $TAINT;
238 is_tainted($s, "$desc: s tainted");
239 isnt_tainted($res, "$desc: res not tainted");
240 isnt_tainted($one, "$desc: \$1 not tainted");
241 is($res, 1, "$desc: res value");
242 is($one, 'abcd', "$desc: \$1 value");
244 $desc = "match /g with string tainted";
246 $s = 'abcd' . $TAINT;
249 is_tainted($s, "$desc: s tainted");
250 isnt_tainted($res, "$desc: res not tainted");
251 isnt_tainted($one, "$desc: \$1 not tainted");
252 is($res, 1, "$desc: res value");
253 is($one, 'a', "$desc: \$1 value");
255 $desc = "match with string tainted, list cxt";
257 $s = 'abcd' . $TAINT;
258 ($res) = $s =~ /(.+)/;
260 is_tainted($s, "$desc: s tainted");
261 isnt_tainted($res, "$desc: res not tainted");
262 isnt_tainted($one, "$desc: \$1 not tainted");
263 is($res, 'abcd', "$desc: res value");
264 is($one, 'abcd', "$desc: \$1 value");
266 $desc = "match /g with string tainted, list cxt";
268 $s = 'abcd' . $TAINT;
269 ($res, $res2) = $s =~ /(.)/g;
271 is_tainted($s, "$desc: s tainted");
272 isnt_tainted($res, "$desc: res not tainted");
273 isnt_tainted($res2,"$desc: res2 not tainted");
274 isnt_tainted($one, "$desc: \$1 not tainted");
275 is($res, 'a', "$desc: res value");
276 is($res2,'b', "$desc: res2 value");
277 is($one, 'd', "$desc: \$1 value");
279 $desc = "match with pattern tainted";
282 $res = $s =~ /$TAINT(.+)/;
284 isnt_tainted($s, "$desc: s not tainted");
285 isnt_tainted($res, "$desc: res not tainted");
286 is_tainted($one, "$desc: \$1 tainted");
287 is($res, 1, "$desc: res value");
288 is($one, 'abcd', "$desc: \$1 value");
290 $desc = "match /g with pattern tainted";
293 $res = $s =~ /$TAINT(.)/g;
295 isnt_tainted($s, "$desc: s not tainted");
296 isnt_tainted($res, "$desc: res not tainted");
297 is_tainted($one, "$desc: \$1 tainted");
298 is($res, 1, "$desc: res value");
299 is($one, 'a', "$desc: \$1 value");
302 skip 'Locales not available', 10 unless locales_enabled('LC_CTYPE');
304 $desc = "match with pattern tainted via locale";
309 $res = $s =~ /(\w+)/; $one = $1;
311 isnt_tainted($s, "$desc: s not tainted");
312 isnt_tainted($res, "$desc: res not tainted");
313 is_tainted($one, "$desc: \$1 tainted");
314 is($res, 1, "$desc: res value");
315 is($one, 'abcd', "$desc: \$1 value");
317 $desc = "match /g with pattern tainted via locale";
322 $res = $s =~ /(\w)/g; $one = $1;
324 isnt_tainted($s, "$desc: s not tainted");
325 isnt_tainted($res, "$desc: res not tainted");
326 is_tainted($one, "$desc: \$1 tainted");
327 is($res, 1, "$desc: res value");
328 is($one, 'a', "$desc: \$1 value");
331 $desc = "match with pattern tainted, list cxt";
334 ($res) = $s =~ /$TAINT(.+)/;
336 isnt_tainted($s, "$desc: s not tainted");
337 is_tainted($res, "$desc: res tainted");
338 is_tainted($one, "$desc: \$1 tainted");
339 is($res, 'abcd', "$desc: res value");
340 is($one, 'abcd', "$desc: \$1 value");
342 $desc = "match /g with pattern tainted, list cxt";
345 ($res, $res2) = $s =~ /$TAINT(.)/g;
347 isnt_tainted($s, "$desc: s not tainted");
348 is_tainted($res, "$desc: res tainted");
349 is_tainted($one, "$desc: \$1 tainted");
350 is($res, 'a', "$desc: res value");
351 is($res2,'b', "$desc: res2 value");
352 is($one, 'd', "$desc: \$1 value");
355 skip 'Locales not available', 12 unless locales_enabled('LC_CTYPE');
357 $desc = "match with pattern tainted via locale, list cxt";
362 ($res) = $s =~ /(\w+)/; $one = $1;
364 isnt_tainted($s, "$desc: s not tainted");
365 is_tainted($res, "$desc: res tainted");
366 is_tainted($one, "$desc: \$1 tainted");
367 is($res, 'abcd', "$desc: res value");
368 is($one, 'abcd', "$desc: \$1 value");
370 $desc = "match /g with pattern tainted via locale, list cxt";
375 ($res, $res2) = $s =~ /(\w)/g; $one = $1;
377 isnt_tainted($s, "$desc: s not tainted");
378 is_tainted($res, "$desc: res tainted");
379 is_tainted($res2, "$desc: res2 tainted");
380 is_tainted($one, "$desc: \$1 tainted");
381 is($res, 'a', "$desc: res value");
382 is($res2,'b', "$desc: res2 value");
383 is($one, 'd', "$desc: \$1 value");
386 $desc = "substitution with string tainted";
388 $s = 'abcd' . $TAINT;
389 $res = $s =~ s/(.+)/xyz/;
391 is_tainted($s, "$desc: s tainted");
392 isnt_tainted($res, "$desc: res not tainted");
393 isnt_tainted($one, "$desc: \$1 not tainted");
394 is($s, 'xyz', "$desc: s value");
395 is($res, 1, "$desc: res value");
396 is($one, 'abcd', "$desc: \$1 value");
398 $desc = "substitution /g with string tainted";
400 $s = 'abcd' . $TAINT;
401 $res = $s =~ s/(.)/x/g;
403 is_tainted($s, "$desc: s tainted");
404 is_tainted($res, "$desc: res tainted");
405 isnt_tainted($one, "$desc: \$1 not tainted");
406 is($s, 'xxxx', "$desc: s value");
407 is($res, 4, "$desc: res value");
408 is($one, 'd', "$desc: \$1 value");
410 $desc = "substitution /r with string tainted";
412 $s = 'abcd' . $TAINT;
413 $res = $s =~ s/(.+)/xyz/r;
415 is_tainted($s, "$desc: s tainted");
416 is_tainted($res, "$desc: res tainted");
417 isnt_tainted($one, "$desc: \$1 not tainted");
418 is($s, 'abcd', "$desc: s value");
419 is($res, 'xyz', "$desc: res value");
420 is($one, 'abcd', "$desc: \$1 value");
422 $desc = "substitution /e with string tainted";
424 $s = 'abcd' . $TAINT;
426 $res = $s =~ s{(.+)}{
427 $one = $one . "x"; # make sure code not tainted
428 isnt_tainted($one, "$desc: code not tainted within /e");
430 isnt_tainted($one, "$desc: \$1 not tainted within /e");
434 is_tainted($s, "$desc: s tainted");
435 isnt_tainted($res, "$desc: res not tainted");
436 isnt_tainted($one, "$desc: \$1 not tainted");
437 is($s, 'xyz', "$desc: s value");
438 is($res, 1, "$desc: res value");
439 is($one, 'abcd', "$desc: \$1 value");
441 $desc = "substitution with pattern tainted";
444 $res = $s =~ s/$TAINT(.+)/xyz/;
446 is_tainted($s, "$desc: s tainted");
447 isnt_tainted($res, "$desc: res not tainted");
448 is_tainted($one, "$desc: \$1 tainted");
449 is($s, 'xyz', "$desc: s value");
450 is($res, 1, "$desc: res value");
451 is($one, 'abcd', "$desc: \$1 value");
453 $desc = "substitution /g with pattern tainted";
456 $res = $s =~ s/$TAINT(.)/x/g;
458 is_tainted($s, "$desc: s tainted");
459 is_tainted($res, "$desc: res tainted");
460 is_tainted($one, "$desc: \$1 tainted");
461 is($s, 'xxxx', "$desc: s value");
462 is($res, 4, "$desc: res value");
463 is($one, 'd', "$desc: \$1 value");
465 $desc = "substitution /ge with pattern tainted";
471 $res = $s =~ s{(.)$TAINT}{
472 $j = $i; # make sure code not tainted
474 isnt_tainted($j, "$desc: code not tainted within /e");
477 isnt_tainted($s, "$desc: s not tainted loop 1");
480 is_tainted($s, "$desc: s tainted loop $i");
482 is_tainted($one, "$desc: \$1 tainted loop $i");
487 is_tainted($s, "$desc: s tainted");
488 is_tainted($res, "$desc: res tainted");
489 is_tainted($one, "$desc: \$1 tainted");
490 is($s, '123', "$desc: s value");
491 is($res, 3, "$desc: res value");
492 is($one, 'c', "$desc: \$1 value");
494 $desc = "substitution /r with pattern tainted";
497 $res = $s =~ s/$TAINT(.+)/xyz/r;
499 isnt_tainted($s, "$desc: s not tainted");
500 is_tainted($res, "$desc: res tainted");
501 is_tainted($one, "$desc: \$1 tainted");
502 is($s, 'abcd', "$desc: s value");
503 is($res, 'xyz', "$desc: res value");
504 is($one, 'abcd', "$desc: \$1 value");
507 skip 'Locales not available', 18 unless locales_enabled('LC_CTYPE');
509 $desc = "substitution with pattern tainted via locale";
514 $res = $s =~ s/(\w+)/xyz/; $one = $1;
516 is_tainted($s, "$desc: s tainted");
517 isnt_tainted($res, "$desc: res not tainted");
518 is_tainted($one, "$desc: \$1 tainted");
519 is($s, 'xyz', "$desc: s value");
520 is($res, 1, "$desc: res value");
521 is($one, 'abcd', "$desc: \$1 value");
523 $desc = "substitution /g with pattern tainted via locale";
528 $res = $s =~ s/(\w)/x/g; $one = $1;
530 is_tainted($s, "$desc: s tainted");
531 is_tainted($res, "$desc: res tainted");
532 is_tainted($one, "$desc: \$1 tainted");
533 is($s, 'xxxx', "$desc: s value");
534 is($res, 4, "$desc: res value");
535 is($one, 'd', "$desc: \$1 value");
537 $desc = "substitution /r with pattern tainted via locale";
542 $res = $s =~ s/(\w+)/xyz/r; $one = $1;
544 isnt_tainted($s, "$desc: s not tainted");
545 is_tainted($res, "$desc: res tainted");
546 is_tainted($one, "$desc: \$1 tainted");
547 is($s, 'abcd', "$desc: s value");
548 is($res, 'xyz', "$desc: res value");
549 is($one, 'abcd', "$desc: \$1 value");
552 $desc = "substitution with replacement tainted";
555 $res = $s =~ s/(.+)/xyz$TAINT/;
557 is_tainted($s, "$desc: s tainted");
558 isnt_tainted($res, "$desc: res not tainted");
559 isnt_tainted($one, "$desc: \$1 not tainted");
560 is($s, 'xyz', "$desc: s value");
561 is($res, 1, "$desc: res value");
562 is($one, 'abcd', "$desc: \$1 value");
564 $desc = "substitution /g with replacement tainted";
567 $res = $s =~ s/(.)/x$TAINT/g;
569 is_tainted($s, "$desc: s tainted");
570 isnt_tainted($res, "$desc: res not tainted");
571 isnt_tainted($one, "$desc: \$1 not tainted");
572 is($s, 'xxxx', "$desc: s value");
573 is($res, 4, "$desc: res value");
574 is($one, 'd', "$desc: \$1 value");
576 $desc = "substitution /ge with replacement tainted";
583 $j = $i; # make sure code not tainted
585 isnt_tainted($j, "$desc: code not tainted within /e");
588 isnt_tainted($s, "$desc: s not tainted loop 1");
591 is_tainted($s, "$desc: s tainted loop $i");
593 isnt_tainted($one, "$desc: \$1 not tainted within /e");
598 is_tainted($s, "$desc: s tainted");
599 isnt_tainted($res, "$desc: res tainted");
600 isnt_tainted($one, "$desc: \$1 not tainted");
601 is($s, '123', "$desc: s value");
602 is($res, 3, "$desc: res value");
603 is($one, 'c', "$desc: \$1 value");
605 $desc = "substitution /r with replacement tainted";
608 $res = $s =~ s/(.+)/xyz$TAINT/r;
610 isnt_tainted($s, "$desc: s not tainted");
611 is_tainted($res, "$desc: res tainted");
612 isnt_tainted($one, "$desc: \$1 not tainted");
613 is($s, 'abcd', "$desc: s value");
614 is($res, 'xyz', "$desc: res value");
615 is($one, 'abcd', "$desc: \$1 value");
618 # now do them all again with "use re 'taint"
622 $desc = "use re 'taint': match with string tainted";
624 $s = 'abcd' . $TAINT;
627 is_tainted($s, "$desc: s tainted");
628 isnt_tainted($res, "$desc: res not tainted");
629 is_tainted($one, "$desc: \$1 tainted");
630 is($res, 1, "$desc: res value");
631 is($one, 'abcd', "$desc: \$1 value");
633 $desc = "use re 'taint': match /g with string tainted";
635 $s = 'abcd' . $TAINT;
638 is_tainted($s, "$desc: s tainted");
639 isnt_tainted($res, "$desc: res not tainted");
640 is_tainted($one, "$desc: \$1 tainted");
641 is($res, 1, "$desc: res value");
642 is($one, 'a', "$desc: \$1 value");
644 $desc = "use re 'taint': match with string tainted, list cxt";
646 $s = 'abcd' . $TAINT;
647 ($res) = $s =~ /(.+)/;
649 is_tainted($s, "$desc: s tainted");
650 is_tainted($res, "$desc: res tainted");
651 is_tainted($one, "$desc: \$1 tainted");
652 is($res, 'abcd', "$desc: res value");
653 is($one, 'abcd', "$desc: \$1 value");
655 $desc = "use re 'taint': match /g with string tainted, list cxt";
657 $s = 'abcd' . $TAINT;
658 ($res, $res2) = $s =~ /(.)/g;
660 is_tainted($s, "$desc: s tainted");
661 is_tainted($res, "$desc: res tainted");
662 is_tainted($res2, "$desc: res2 tainted");
663 is_tainted($one, "$desc: \$1 not tainted");
664 is($res, 'a', "$desc: res value");
665 is($res2,'b', "$desc: res2 value");
666 is($one, 'd', "$desc: \$1 value");
668 $desc = "use re 'taint': match with pattern tainted";
671 $res = $s =~ /$TAINT(.+)/;
673 isnt_tainted($s, "$desc: s not tainted");
674 isnt_tainted($res, "$desc: res not tainted");
675 is_tainted($one, "$desc: \$1 tainted");
676 is($res, 1, "$desc: res value");
677 is($one, 'abcd', "$desc: \$1 value");
679 $desc = "use re 'taint': match /g with pattern tainted";
682 $res = $s =~ /$TAINT(.)/g;
684 isnt_tainted($s, "$desc: s not tainted");
685 isnt_tainted($res, "$desc: res not tainted");
686 is_tainted($one, "$desc: \$1 tainted");
687 is($res, 1, "$desc: res value");
688 is($one, 'a', "$desc: \$1 value");
691 skip 'Locales not available', 10 unless locales_enabled('LC_CTYPE');
693 $desc = "use re 'taint': match with pattern tainted via locale";
698 $res = $s =~ /(\w+)/; $one = $1;
700 isnt_tainted($s, "$desc: s not tainted");
701 isnt_tainted($res, "$desc: res not tainted");
702 is_tainted($one, "$desc: \$1 tainted");
703 is($res, 1, "$desc: res value");
704 is($one, 'abcd', "$desc: \$1 value");
706 $desc = "use re 'taint': match /g with pattern tainted via locale";
711 $res = $s =~ /(\w)/g; $one = $1;
713 isnt_tainted($s, "$desc: s not tainted");
714 isnt_tainted($res, "$desc: res not tainted");
715 is_tainted($one, "$desc: \$1 tainted");
716 is($res, 1, "$desc: res value");
717 is($one, 'a', "$desc: \$1 value");
720 $desc = "use re 'taint': match with pattern tainted, list cxt";
723 ($res) = $s =~ /$TAINT(.+)/;
725 isnt_tainted($s, "$desc: s not tainted");
726 is_tainted($res, "$desc: res tainted");
727 is_tainted($one, "$desc: \$1 tainted");
728 is($res, 'abcd', "$desc: res value");
729 is($one, 'abcd', "$desc: \$1 value");
731 $desc = "use re 'taint': match /g with pattern tainted, list cxt";
734 ($res, $res2) = $s =~ /$TAINT(.)/g;
736 isnt_tainted($s, "$desc: s not tainted");
737 is_tainted($res, "$desc: res tainted");
738 is_tainted($one, "$desc: \$1 tainted");
739 is($res, 'a', "$desc: res value");
740 is($res2,'b', "$desc: res2 value");
741 is($one, 'd', "$desc: \$1 value");
744 skip 'Locales not available', 12 unless locales_enabled('LC_CTYPE');
746 $desc = "use re 'taint': match with pattern tainted via locale, list cxt";
751 ($res) = $s =~ /(\w+)/; $one = $1;
753 isnt_tainted($s, "$desc: s not tainted");
754 is_tainted($res, "$desc: res tainted");
755 is_tainted($one, "$desc: \$1 tainted");
756 is($res, 'abcd', "$desc: res value");
757 is($one, 'abcd', "$desc: \$1 value");
759 $desc = "use re 'taint': match /g with pattern tainted via locale, list cxt";
764 ($res, $res2) = $s =~ /(\w)/g; $one = $1;
766 isnt_tainted($s, "$desc: s not tainted");
767 is_tainted($res, "$desc: res tainted");
768 is_tainted($res2, "$desc: res2 tainted");
769 is_tainted($one, "$desc: \$1 tainted");
770 is($res, 'a', "$desc: res value");
771 is($res2,'b', "$desc: res2 value");
772 is($one, 'd', "$desc: \$1 value");
775 $desc = "use re 'taint': substitution with string tainted";
777 $s = 'abcd' . $TAINT;
778 $res = $s =~ s/(.+)/xyz/;
780 is_tainted($s, "$desc: s tainted");
781 isnt_tainted($res, "$desc: res not tainted");
782 is_tainted($one, "$desc: \$1 tainted");
783 is($s, 'xyz', "$desc: s value");
784 is($res, 1, "$desc: res value");
785 is($one, 'abcd', "$desc: \$1 value");
787 $desc = "use re 'taint': substitution /g with string tainted";
789 $s = 'abcd' . $TAINT;
790 $res = $s =~ s/(.)/x/g;
792 is_tainted($s, "$desc: s tainted");
793 is_tainted($res, "$desc: res tainted");
794 is_tainted($one, "$desc: \$1 tainted");
795 is($s, 'xxxx', "$desc: s value");
796 is($res, 4, "$desc: res value");
797 is($one, 'd', "$desc: \$1 value");
799 $desc = "use re 'taint': substitution /r with string tainted";
801 $s = 'abcd' . $TAINT;
802 $res = $s =~ s/(.+)/xyz/r;
804 is_tainted($s, "$desc: s tainted");
805 is_tainted($res, "$desc: res tainted");
806 is_tainted($one, "$desc: \$1 tainted");
807 is($s, 'abcd', "$desc: s value");
808 is($res, 'xyz', "$desc: res value");
809 is($one, 'abcd', "$desc: \$1 value");
811 $desc = "use re 'taint': substitution /e with string tainted";
813 $s = 'abcd' . $TAINT;
815 $res = $s =~ s{(.+)}{
816 $one = $one . "x"; # make sure code not tainted
817 isnt_tainted($one, "$desc: code not tainted within /e");
819 is_tainted($one, "$desc: $1 tainted within /e");
823 is_tainted($s, "$desc: s tainted");
824 isnt_tainted($res, "$desc: res not tainted");
825 is_tainted($one, "$desc: \$1 tainted");
826 is($s, 'xyz', "$desc: s value");
827 is($res, 1, "$desc: res value");
828 is($one, 'abcd', "$desc: \$1 value");
830 $desc = "use re 'taint': substitution with pattern tainted";
833 $res = $s =~ s/$TAINT(.+)/xyz/;
835 is_tainted($s, "$desc: s tainted");
836 isnt_tainted($res, "$desc: res not tainted");
837 is_tainted($one, "$desc: \$1 tainted");
838 is($s, 'xyz', "$desc: s value");
839 is($res, 1, "$desc: res value");
840 is($one, 'abcd', "$desc: \$1 value");
842 $desc = "use re 'taint': substitution /g with pattern tainted";
845 $res = $s =~ s/$TAINT(.)/x/g;
847 is_tainted($s, "$desc: s tainted");
848 is_tainted($res, "$desc: res tainted");
849 is_tainted($one, "$desc: \$1 tainted");
850 is($s, 'xxxx', "$desc: s value");
851 is($res, 4, "$desc: res value");
852 is($one, 'd', "$desc: \$1 value");
854 $desc = "use re 'taint': substitution /ge with pattern tainted";
860 $res = $s =~ s{(.)$TAINT}{
861 $j = $i; # make sure code not tainted
863 isnt_tainted($j, "$desc: code not tainted within /e");
866 isnt_tainted($s, "$desc: s not tainted loop 1");
869 is_tainted($s, "$desc: s tainted loop $i");
871 is_tainted($one, "$desc: \$1 tainted loop $i");
876 is_tainted($s, "$desc: s tainted");
877 is_tainted($res, "$desc: res tainted");
878 is_tainted($one, "$desc: \$1 tainted");
879 is($s, '123', "$desc: s value");
880 is($res, 3, "$desc: res value");
881 is($one, 'c', "$desc: \$1 value");
884 $desc = "use re 'taint': substitution /r with pattern tainted";
887 $res = $s =~ s/$TAINT(.+)/xyz/r;
889 isnt_tainted($s, "$desc: s not tainted");
890 is_tainted($res, "$desc: res tainted");
891 is_tainted($one, "$desc: \$1 tainted");
892 is($s, 'abcd', "$desc: s value");
893 is($res, 'xyz', "$desc: res value");
894 is($one, 'abcd', "$desc: \$1 value");
897 skip 'Locales not available', 18 unless locales_enabled('LC_CTYPE');
899 $desc = "use re 'taint': substitution with pattern tainted via locale";
904 $res = $s =~ s/(\w+)/xyz/; $one = $1;
906 is_tainted($s, "$desc: s tainted");
907 isnt_tainted($res, "$desc: res not tainted");
908 is_tainted($one, "$desc: \$1 tainted");
909 is($s, 'xyz', "$desc: s value");
910 is($res, 1, "$desc: res value");
911 is($one, 'abcd', "$desc: \$1 value");
913 $desc = "use re 'taint': substitution /g with pattern tainted via locale";
918 $res = $s =~ s/(\w)/x/g; $one = $1;
920 is_tainted($s, "$desc: s tainted");
921 is_tainted($res, "$desc: res tainted");
922 is_tainted($one, "$desc: \$1 tainted");
923 is($s, 'xxxx', "$desc: s value");
924 is($res, 4, "$desc: res value");
925 is($one, 'd', "$desc: \$1 value");
927 $desc = "use re 'taint': substitution /r with pattern tainted via locale";
932 $res = $s =~ s/(\w+)/xyz/r; $one = $1;
934 isnt_tainted($s, "$desc: s not tainted");
935 is_tainted($res, "$desc: res tainted");
936 is_tainted($one, "$desc: \$1 tainted");
937 is($s, 'abcd', "$desc: s value");
938 is($res, 'xyz', "$desc: res value");
939 is($one, 'abcd', "$desc: \$1 value");
942 $desc = "use re 'taint': substitution with replacement tainted";
945 $res = $s =~ s/(.+)/xyz$TAINT/;
947 is_tainted($s, "$desc: s tainted");
948 isnt_tainted($res, "$desc: res not tainted");
949 isnt_tainted($one, "$desc: \$1 not tainted");
950 is($s, 'xyz', "$desc: s value");
951 is($res, 1, "$desc: res value");
952 is($one, 'abcd', "$desc: \$1 value");
954 $desc = "use re 'taint': substitution /g with replacement tainted";
957 $res = $s =~ s/(.)/x$TAINT/g;
959 is_tainted($s, "$desc: s tainted");
960 isnt_tainted($res, "$desc: res not tainted");
961 isnt_tainted($one, "$desc: \$1 not tainted");
962 is($s, 'xxxx', "$desc: s value");
963 is($res, 4, "$desc: res value");
964 is($one, 'd', "$desc: \$1 value");
966 $desc = "use re 'taint': substitution /ge with replacement tainted";
973 $j = $i; # make sure code not tainted
975 isnt_tainted($j, "$desc: code not tainted within /e");
978 isnt_tainted($s, "$desc: s not tainted loop 1");
981 is_tainted($s, "$desc: s tainted loop $i");
983 isnt_tainted($one, "$desc: \$1 not tainted");
988 is_tainted($s, "$desc: s tainted");
989 isnt_tainted($res, "$desc: res tainted");
990 isnt_tainted($one, "$desc: \$1 not tainted");
991 is($s, '123', "$desc: s value");
992 is($res, 3, "$desc: res value");
993 is($one, 'c', "$desc: \$1 value");
995 $desc = "use re 'taint': substitution /r with replacement tainted";
998 $res = $s =~ s/(.+)/xyz$TAINT/r;
1000 isnt_tainted($s, "$desc: s not tainted");
1001 is_tainted($res, "$desc: res tainted");
1002 isnt_tainted($one, "$desc: \$1 not tainted");
1003 is($s, 'abcd', "$desc: s value");
1004 is($res, 'xyz', "$desc: res value");
1005 is($one, 'abcd', "$desc: \$1 value");
1007 # [perl #121854] match taintedness became sticky
1008 # when one match has a taintess result, subseqent matches
1009 # using the same pattern shouldn't necessarily be tainted
1012 my $f = sub { $_[0] =~ /(.*)/ or die; $1 };
1013 $res = $f->($TAINT);
1014 is_tainted($res, "121854: res tainted");
1016 isnt_tainted($res, "121854: res not tainted");
1020 $foo = $1 if 'bar' =~ /(.+)$TAINT/;
1024 my $pi = 4 * atan2(1,1) + $TAINT0;
1027 ($pi) = $pi =~ /(\d+\.\d+)/;
1029 is(sprintf("%.5f", $pi), '3.14159');
1032 # How about command-line arguments? The problem is that we don't
1033 # always get some, so we'll run another process with some.
1035 my $arg = tempfile();
1036 open $fh, '>', $arg or die "Can't create $arg: $!";
1038 eval { join('', @ARGV), kill 0 };
1039 exit 0 if $@ =~ /^Insecure dependency/;
1040 print "# Oops: \$@ was [$@]\n";
1043 close $fh or die "Can't close $arg: $!";
1044 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
1045 is($?, 0, "Exited with status $?");
1049 # Reading from a file should be tainted
1051 ok(open my $fh, '<', $TEST) or diag("Couldn't open '$TEST': $!");
1054 sysread($fh, $block, 100);
1061 # Output of commands should be tainted
1063 my $foo = `$echo abc`;
1067 # Certain system variables should be tainted
1073 # Results of matching should all be untainted
1075 my $foo = "abcdefghi" . $TAINT;
1083 $foo =~ /(...)(...)(...)/;
1089 my @bar = $foo =~ /(...)(...)(...)/;
1090 isnt_tainted($_) foreach @bar;
1092 is_tainted($foo); # $foo should still be tainted!
1093 is($foo, "abcdefghi");
1096 # Operations which affect files can't use tainted data.
1098 violates_taint(sub { chmod 0, $TAINT }, 'chmod');
1101 skip "truncate() is not available", 2 unless $Config{d_truncate};
1103 violates_taint(sub { truncate 'NoSuChFiLe', $TAINT0 }, 'truncate');
1106 violates_taint(sub { rename '', $TAINT }, 'rename');
1107 violates_taint(sub { unlink $TAINT }, 'unlink');
1108 violates_taint(sub { utime $TAINT }, 'utime');
1111 skip "chown() is not available", 2 unless $Config{d_chown};
1113 violates_taint(sub { chown -1, -1, $TAINT }, 'chown');
1117 skip "link() is not available", 2 unless $Config{d_link};
1119 violates_taint(sub { link $TAINT, '' }, 'link');
1123 skip "symlink() is not available", 2 unless $Config{d_symlink};
1125 violates_taint(sub { symlink $TAINT, '' }, 'symlink');
1129 # Operations which affect directories can't use tainted data.
1131 violates_taint(sub { mkdir "foo".$TAINT, 0755 . $TAINT0 }, 'mkdir');
1132 violates_taint(sub { rmdir $TAINT }, 'rmdir');
1133 violates_taint(sub { chdir "foo".$TAINT }, 'chdir');
1136 skip "chroot() is not available", 2 unless $Config{d_chroot};
1138 violates_taint(sub { chroot $TAINT }, 'chroot');
1142 # Some operations using files can't use tainted data.
1144 my $foo = "imaginary library" . $TAINT;
1145 violates_taint(sub { require $foo }, 'require');
1147 my $filename = tempfile(); # NB: $filename isn't tainted!
1148 $foo = $filename . $TAINT;
1149 unlink $filename; # in any case
1151 is(eval { open FOO, $foo }, undef, 'open for read');
1152 is($@, ''); # NB: This should be allowed
1153 is(eval { open my $fh, , '<', $foo }, undef, 'open for read');
1154 is($@, ''); # NB: This should be allowed
1156 # Try first new style but allow also old style.
1157 # We do not want the whole taint.t to fail
1158 # just because Errno possibly failing.
1159 ok(eval('$!{ENOENT}') ||
1160 $! == 2 || # File not found
1161 ($Is_Dos && $! == 22));
1163 violates_taint(sub { open FOO, "> $foo" }, 'open', 'open for write');
1164 violates_taint(sub { open my $fh, '>', $foo }, 'open', 'open for write');
1167 # Commands to the system can't use tainted data
1172 skip "open('|') is not available", 8 if $^O eq 'amigaos';
1174 violates_taint(sub { open FOO, "| x$foo" }, 'piped open', 'popen to');
1175 violates_taint(sub { open FOO, "x$foo |" }, 'piped open', 'popen from');
1176 violates_taint(sub { open my $fh, '|-', "x$foo" }, 'piped open', 'popen to');
1177 violates_taint(sub { open my $fh, '-|', "x$foo" }, 'piped open', 'popen from');
1180 violates_taint(sub { exec $TAINT }, 'exec');
1181 violates_taint(sub { system $TAINT }, 'system');
1186 violates_taint(sub { `$echo 1$foo` }, '``', 'backticks');
1189 # wildcard expansion doesn't invoke shell on VMS, so is safe
1190 skip "This is not VMS", 2 unless $Is_VMS;
1192 isnt(join('', eval { glob $foo } ), '', 'globbing');
1197 # Operations which affect processes can't use tainted data.
1199 violates_taint(sub { kill 0, $TAINT }, 'kill');
1202 skip "setpgrp() is not available", 2 unless $Config{d_setpgrp};
1204 violates_taint(sub { setpgrp 0, $TAINT0 }, 'setpgrp');
1208 skip "setpriority() is not available", 2 unless $Config{d_setprior};
1210 violates_taint(sub { setpriority 0, $TAINT0, $TAINT0 }, 'setpriority');
1214 # Some miscellaneous operations can't use tainted data.
1217 skip "syscall() is not available", 2 unless $Config{d_syscall};
1219 violates_taint(sub { syscall $TAINT }, 'syscall');
1223 my $foo = "x" x 979;
1226 my $temp = tempfile();
1227 ok(open FOO, "> $temp") or diag("Couldn't open $temp for write: $!");
1228 violates_taint(sub { ioctl FOO, $TAINT0, $foo }, 'ioctl');
1230 my $temp2 = tempfile();
1231 ok(open my $fh, '>', $temp2) or diag("Couldn't open $temp2 for write: $!");
1232 violates_taint(sub { ioctl $fh, $TAINT0, $foo }, 'ioctl');
1235 skip "fcntl() is not available", 4 unless $Config{d_fcntl};
1237 violates_taint(sub { fcntl FOO, $TAINT0, $foo }, 'fcntl');
1238 violates_taint(sub { fcntl $fh, $TAINT0, $foo }, 'fcntl');
1245 # Some tests involving references
1247 my $foo = 'abc' . $TAINT;
1249 isnt_tainted($fooref);
1250 is_tainted($$fooref);
1254 # Some tests involving assignment
1260 is_tainted($foo = $bar);
1261 is_tainted($bar = $bar);
1262 is_tainted($bar += $bar);
1263 is_tainted($bar -= $bar);
1264 is_tainted($bar *= $bar);
1266 is_tainted($bar /= $bar);
1267 is_tainted($bar += 0);
1268 is_tainted($bar -= 2);
1269 is_tainted($bar *= -1);
1270 is_tainted($bar /= 1);
1275 # Test assignment and return of lists
1277 my @foo = ("A", "tainted" . $TAINT, "B");
1278 isnt_tainted($foo[0]);
1279 is_tainted( $foo[1]);
1280 isnt_tainted($foo[2]);
1282 isnt_tainted($bar[0]);
1283 is_tainted( $bar[1]);
1284 isnt_tainted($bar[2]);
1285 my @baz = eval { "A", "tainted" . $TAINT, "B" };
1286 isnt_tainted($baz[0]);
1287 is_tainted( $baz[1]);
1288 isnt_tainted($baz[2]);
1289 my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
1290 isnt_tainted($plugh[0]);
1291 is_tainted( $plugh[1]);
1292 isnt_tainted($plugh[2]);
1293 my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
1294 isnt_tainted(((&$nautilus)[0]));
1295 is_tainted( ((&$nautilus)[1]));
1296 isnt_tainted(((&$nautilus)[2]));
1297 my @xyzzy = &$nautilus;
1298 isnt_tainted($xyzzy[0]);
1299 is_tainted( $xyzzy[1]);
1300 isnt_tainted($xyzzy[2]);
1301 my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
1302 isnt_tainted(((&$red_october)[0]));
1303 is_tainted( ((&$red_october)[1]));
1304 isnt_tainted(((&$red_october)[2]));
1305 my @corge = &$red_october;
1306 isnt_tainted($corge[0]);
1307 is_tainted( $corge[1]);
1308 isnt_tainted($corge[2]);
1311 # Test for system/library calls returning string data of dubious origin.
1313 # No reliable %Config check for getpw*
1315 skip "getpwent() is not available", 9 unless
1316 eval { setpwent(); getpwent() };
1319 my @getpwent = getpwent();
1320 die "getpwent: $!\n" unless (@getpwent);
1321 isnt_tainted($getpwent[0]);
1322 is_tainted($getpwent[1]);
1323 isnt_tainted($getpwent[2]);
1324 isnt_tainted($getpwent[3]);
1325 isnt_tainted($getpwent[4]);
1326 isnt_tainted($getpwent[5]);
1327 is_tainted($getpwent[6], 'ge?cos');
1328 isnt_tainted($getpwent[7]);
1329 is_tainted($getpwent[8], 'shell');
1334 # pretty hard to imagine not
1335 skip "readdir() is not available", 1 unless $Config{d_readdir};
1337 opendir my $dh, "op" or die "opendir: $!\n";
1338 my $readdir = readdir $dh;
1339 is_tainted($readdir);
1344 skip "readlink() or symlink() is not available" unless
1345 $Config{d_readlink} && $Config{d_symlink};
1347 my $symlink = "sl$$";
1349 my $sl = "/something/naughty";
1350 # it has to be a real path on Mac OS
1351 symlink($sl, $symlink) or die "symlink: $!\n";
1352 my $readlink = readlink($symlink);
1353 is_tainted($readlink);
1358 # test bitwise ops (regression bug)
1368 # test target of substitution (regression bug)
1370 my $why = $TAINT."y";
1378 $why =~ s/e/'-'.$$/ge;
1384 skip "no IPC::SysV", 2 unless $ipcsysv;
1388 skip "shm*() not available", 1 unless $Config{d_shm};
1391 my $sent = "foobar";
1396 local $SIG{SYS} = sub { die "SIGSYS caught\n" };
1397 $id = shmget(IPC_PRIVATE, $size, S_IRWXU);
1399 } or do { chomp(my $msg = $@); skip "shmget: $msg", 1; };
1402 if (shmwrite($id, $sent, 0, 60)) {
1403 if (shmread($id, $rcvd, 0, 60)) {
1404 substr($rcvd, index($rcvd, "\0")) = '';
1406 warn "# shmread failed: $!\n";
1409 warn "# shmwrite failed: $!\n";
1411 shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n";
1413 warn "# shmget failed: $!\n";
1416 skip "SysV shared memory operation failed", 1 unless
1419 is_tainted($rcvd, "shmread");
1425 skip "msg*() not available", 1 unless $Config{d_msg};
1430 local $SIG{SYS} = sub { die "SIGSYS caught\n" };
1431 $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
1433 } or do { chomp(my $msg = $@); skip "msgget: $msg", 1; };
1435 my $sent = "message";
1436 my $type_sent = 1234;
1441 if (msgsnd($id, pack("l! a*", $type_sent, $sent), IPC_NOWAIT)) {
1442 if (msgrcv($id, $rcvd, 60, 0, IPC_NOWAIT)) {
1443 ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd);
1445 warn "# msgrcv failed: $!\n";
1448 warn "# msgsnd failed: $!\n";
1450 msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n";
1452 warn "# msgget failed\n";
1456 skip "SysV message queue operation failed", 1
1457 unless $rcvd eq $sent && $type_sent == $type_rcvd;
1459 is_tainted($rcvd, "msgrcv");
1465 # bug id 20001004.006
1467 open my $fh, '<', $TEST or warn "$0: cannot read $TEST: $!" ;
1478 # bug id 20001004.007
1480 open my $fh, '<', $TEST or warn "$0: cannot read $TEST: $!" ;
1486 isnt_tainted($c->{a});
1487 is_tainted($c->{b});
1492 is_tainted($d->{a});
1493 isnt_tainted($d->{b});
1497 b => { c => $a, d => 42 } };
1498 isnt_tainted($e->{a});
1499 isnt_tainted($e->{b});
1500 is_tainted($e->{b}->{c});
1501 isnt_tainted($e->{b}->{d});
1505 # bug id 20010519.003
1508 use vars qw($has_fcntl);
1509 eval { require Fcntl; import Fcntl; };
1516 skip "no Fcntl", 36 unless $has_fcntl;
1518 my $foo = tempfile();
1519 my $evil = $foo . $TAINT;
1521 is(eval { sysopen(my $ro, $evil, &O_RDONLY) }, undef);
1524 violates_taint(sub { sysopen(my $wo, $evil, &O_WRONLY) }, 'sysopen');
1525 violates_taint(sub { sysopen(my $rw, $evil, &O_RDWR) }, 'sysopen');
1526 violates_taint(sub { sysopen(my $ap, $evil, &O_APPEND) }, 'sysopen');
1527 violates_taint(sub { sysopen(my $cr, $evil, &O_CREAT) }, 'sysopen');
1528 violates_taint(sub { sysopen(my $tr, $evil, &O_TRUNC) }, 'sysopen');
1530 is(eval { sysopen(my $ro, $foo, &O_RDONLY | $TAINT0) }, undef);
1533 violates_taint(sub { sysopen(my $wo, $foo, &O_WRONLY | $TAINT0) }, 'sysopen');
1534 violates_taint(sub { sysopen(my $rw, $foo, &O_RDWR | $TAINT0) }, 'sysopen');
1535 violates_taint(sub { sysopen(my $ap, $foo, &O_APPEND | $TAINT0) }, 'sysopen');
1536 violates_taint(sub { sysopen(my $cr, $foo, &O_CREAT | $TAINT0) }, 'sysopen');
1537 violates_taint(sub { sysopen(my $tr, $foo, &O_TRUNC | $TAINT0) }, 'sysopen');
1538 is(eval { sysopen(my $ro, $foo, &O_RDONLY, $TAINT0) }, undef);
1541 violates_taint(sub { sysopen(my $wo, $foo, &O_WRONLY, $TAINT0) }, 'sysopen');
1542 violates_taint(sub { sysopen(my $rw, $foo, &O_RDWR, $TAINT0) }, 'sysopen');
1543 violates_taint(sub { sysopen(my $ap, $foo, &O_APPEND, $TAINT0) }, 'sysopen');
1544 violates_taint(sub { sysopen(my $cr, $foo, &O_CREAT, $TAINT0) }, 'sysopen');
1545 violates_taint(sub { sysopen(my $tr, $foo, &O_TRUNC, $TAINT0) }, 'sysopen');
1554 my $saw_warning = 0;
1555 local $SIG{__WARN__} = sub { ++$saw_warning };
1558 my $divnum = shift()/1;
1559 sprintf("%1.1f\n", $divnum);
1566 is($saw_warning, 0);
1571 # Bug ID 20010730.010
1575 sub Tie::TIESCALAR {
1579 bless \$arg => $class;
1590 my $bar = "The Big Bright Green Pleasure Machine";
1592 tie my ($foo), Tie => $bar;
1600 # Check that all environment variables are tainted.
1602 while (my ($k, $v) = each %ENV) {
1604 # These we have explicitly untainted or set earlier.
1605 $k !~ /^(BASH_ENV|CDPATH|ENV|IFS|PATH|PERL_CORE|TEMP|TERM|TMP)$/) {
1606 push @untainted, "# '$k' = '$v'\n";
1609 is("@untainted", "");
1613 is(${^TAINT}, 1, '$^TAINT is on');
1615 eval { ${^TAINT} = 0 };
1616 is(${^TAINT}, 1, '$^TAINT is not assignable');
1617 like($@, qr/^Modification of a read-only value attempted/,
1618 'Assigning to ${^TAINT} fails');
1623 my $re1 = qr/x$TAINT/;
1626 my $re2 = qr/^$re1\z/;
1634 skip "system {} has different semantics on Win32", 1 if $Is_MSWin32;
1637 local $ENV{PATH} .= $TAINT;
1638 eval { system { "echo" } "/arg0", "arg1" };
1639 like($@, qr/^Insecure \$ENV/);
1643 todo_skip 'tainted %ENV warning occludes tainted arguments warning', 22
1646 # bug 20020208.005 plus some single arg exec/system extras
1647 violates_taint(sub { exec $TAINT, $TAINT }, 'exec');
1648 violates_taint(sub { exec $TAINT $TAINT }, 'exec');
1649 violates_taint(sub { exec $TAINT $TAINT, $TAINT }, 'exec');
1650 violates_taint(sub { exec $TAINT 'notaint' }, 'exec');
1651 violates_taint(sub { exec {'notaint'} $TAINT }, 'exec');
1653 violates_taint(sub { system $TAINT, $TAINT }, 'system');
1654 violates_taint(sub { system $TAINT $TAINT }, 'system');
1655 violates_taint(sub { system $TAINT $TAINT, $TAINT }, 'system');
1656 violates_taint(sub { system $TAINT 'notaint' }, 'system');
1657 violates_taint(sub { system {'notaint'} $TAINT }, 'system');
1661 system("lskdfj does not exist","with","args");
1667 exec("lskdfj does not exist","with","args");
1671 # If you add tests here update also the above skip block for VMS.
1675 # [ID 20020704.001] taint propagation failure
1678 is_tainted(my $foo = $1);
1682 # [perl #24291] this used to dump core
1683 our %nonmagicalenv = ( PATH => "util" );
1684 local *ENV = \%nonmagicalenv;
1685 eval { system("lskdfj"); };
1686 like($@, qr/^%ENV is aliased to another variable while running with -T switch/);
1687 local *ENV = *nonmagicalenv;
1688 eval { system("lskdfj"); };
1689 like($@, qr/^%ENV is aliased to %nonmagicalenv while running with -T switch/);
1696 isnt_tainted($notaint);
1699 $notaint =~ /($notaint)/;
1703 $notaint =~ /($TAINT)/;
1708 $TAINT =~ /($notaint)/;
1712 $TAINT =~ /($TAINT)/;
1718 ($r = $TAINT) =~ /($notaint)/;
1720 ($r = $TAINT) =~ /($TAINT)/;
1724 use re 'eval'; # this shouldn't make any difference
1725 ($r = $TAINT) =~ /($notaint)/;
1727 ($r = $TAINT) =~ /($TAINT)/;
1732 # accessing $^O shoudn't taint it as a side-effect;
1733 # assigning tainted data to it is now an error
1736 if (!$^X) { } elsif ($^O eq 'bar') { }
1738 local $^O; # We're going to clobber something test infrastructure depends on.
1740 like($@, qr/Insecure dependency in/);
1743 EFFECTIVELY_CONSTANTS: {
1744 my $tainted_number = 12 + $TAINT0;
1745 is_tainted( $tainted_number );
1747 # Even though it's always 0, it's still tainted
1748 my $tainted_product = $tainted_number * 0;
1749 is_tainted( $tainted_product );
1750 is($tainted_product, 0);
1753 TERNARY_CONDITIONALS: {
1754 my $tainted_true = $TAINT . "blah blah blah";
1755 my $tainted_false = $TAINT0;
1756 is_tainted( $tainted_true );
1757 is_tainted( $tainted_false );
1759 my $result = $tainted_true ? "True" : "False";
1760 is($result, "True");
1761 isnt_tainted( $result );
1763 $result = $tainted_false ? "True" : "False";
1764 is($result, "False");
1765 isnt_tainted( $result );
1767 my $untainted_whatever = "The Fabulous Johnny Cash";
1768 my $tainted_whatever = "Soft Cell" . $TAINT;
1770 $result = $tainted_true ? $tainted_whatever : $untainted_whatever;
1771 is($result, "Soft Cell");
1772 is_tainted( $result );
1774 $result = $tainted_false ? $tainted_whatever : $untainted_whatever;
1775 is($result, "The Fabulous Johnny Cash");
1776 isnt_tainted( $result );
1780 # rt.perl.org 5900 $1 remains tainted if...
1781 # 1) The regular expression contains a scalar variable AND
1782 # 2) The regular expression appears in an elsif clause
1784 my $foo = "abcdefghi" . $TAINT;
1786 my $valid_chars = 'a-z';
1789 elsif ( $foo =~ /([$valid_chars]+)/o ) {
1796 elsif ( my @bar = $foo =~ /([$valid_chars]+)/o ) {
1797 isnt_tainted($bar[0]);
1802 # at scope exit, a restored localised value should have its old
1803 # taint status, not the taint status of the current statement
1812 my $c = do { local $x99; $^X };
1819 my $c = do { local $x99; '' };
1823 # an mg_get of a tainted value during localization shouldn't taint the
1827 eval { local $0, eval '1' };
1831 # [perl #8262] //g loops infinitely on tainted data
1837 cmp_ok pos($a[0]), '>', 0, "infinite m//g on arrays (aelemfast)";
1842 cmp_ok pos($a[$i]), '>', 0, "infinite m//g on arrays (aelem)";
1847 cmp_ok pos($h{a}), '>', 0, "infinite m//g on hashes (helem)";
1853 eval 'use Scalar::Util "dualvar"; $got_dualvar++';
1854 skip "No Scalar::Util::dualvar" unless $got_dualvar;
1855 my $a = Scalar::Util::dualvar(3, $^X);
1857 is ($b, 8, "Arithmetic on tainted dualvars works");
1860 # opening '|-' should not trigger $ENV{PATH} check
1864 skip "fork() is not available", 3 unless $Config{'d_fork'};
1865 skip "opening |- is not stable on threaded Open/MirBSD with taint", 3
1866 if $Config{useithreads} and $Is_OpenBSD || $Is_MirBSD;
1868 $ENV{'PATH'} = $TAINT;
1869 local $SIG{'PIPE'} = 'IGNORE';
1871 my $pid = open my $pipe, '|-';
1872 if (!defined $pid) {
1873 die "open failed: $!";
1876 kill 'KILL', $$; # child suicide
1880 unlike($@, qr/Insecure \$ENV/, 'fork triggers %ENV check');
1881 is($@, '', 'pipe/fork/open/close failed');
1883 open my $pipe, "|$Invoke_Perl -e 1";
1886 like($@, qr/Insecure \$ENV/, 'popen neglects %ENV check');
1891 package AUTOLOAD_TAINT;
1894 return if $AUTOLOAD =~ /DESTROY/;
1895 if ($AUTOLOAD =~ /untainted/) {
1896 main::isnt_tainted($AUTOLOAD, '$AUTOLOAD can be untainted');
1897 my $copy = $AUTOLOAD;
1898 main::isnt_tainted($copy, '$AUTOLOAD can be untainted');
1900 main::is_tainted($AUTOLOAD, '$AUTOLOAD can be tainted');
1901 my $copy = $AUTOLOAD;
1902 main::is_tainted($copy, '$AUTOLOAD can be tainted');
1907 my $o = bless [], 'AUTOLOAD_TAINT';
1914 # tests for tainted format in s?printf
1915 my $fmt = $TAINT . "# %s\n";
1916 violates_taint(sub { printf($fmt, "foo") }, 'printf',
1917 q/printf doesn't like tainted formats/);
1918 violates_taint(sub { printf($TAINT . "# %s\n", "foo") }, 'printf',
1919 q/printf doesn't like tainted format expressions/);
1920 eval { printf("# %s\n", $TAINT . "foo") };
1921 is($@, '', q/printf accepts other tainted args/);
1922 violates_taint(sub { sprintf($fmt, "foo") }, 'sprintf',
1923 q/sprintf doesn't like tainted formats/);
1924 violates_taint(sub { sprintf($TAINT . "# %s\n", "foo") }, 'sprintf',
1925 q/sprintf doesn't like tainted format expressions/);
1926 eval { sprintf("# %s\n", $TAINT . "foo") };
1927 is($@, '', q/sprintf accepts other tainted args/);
1936 is ($val, '7000000000', 'Assignment to untainted variable');
1939 is ($val, '7000000000', 'Assignment to tainted variable');
1944 my $tainted = '1' . $TAINT;
1945 eval '$val = eval $tainted;';
1946 is ($val, 0, "eval doesn't like tainted strings");
1947 like ($@, qr/^Insecure dependency in eval/);
1949 # Rather nice code to get a tainted undef by from Rick Delaney
1950 open my $fh, "test.pl" or die $!;
1951 seek $fh, 0, 2 or die $!;
1954 eval 'eval $tainted';
1955 like ($@, qr/^Insecure dependency in eval/);
1958 foreach my $ord (78, 163, 256) {
1960 my $line = 'A1' . $TAINT . chr $ord;
1964 isnt_tainted($1, "\\S match with chr $ord");
1969 skip 'No crypt function, skipping crypt tests', 4 if(!$Config{d_crypt});
1972 # On platforms implementing FIPS mode, using a weak algorithm
1973 # (including the default triple-DES algorithm) causes crypt(3) to
1974 # return a null pointer, which Perl converts into undef. We assume
1975 # for now that all such platforms support glibc-style selection of
1976 # a different hashing algorithm.
1977 # glibc supports MD5, but OpenBSD only supports Blowfish.
1978 my $alg = ''; # Use default algorithm
1979 if ( !defined(crypt("ab", $alg."cd")) ) {
1980 $alg = '$5$'; # Try SHA-256
1982 if ( !defined(crypt("ab", $alg."cd")) ) {
1983 $alg = '$2b$12$FPWWO2RJ3CK4FINTw0Hi'; # Try Blowfish
1985 if ( !defined(crypt("ab", $alg."cd")) ) {
1986 $alg = ''; # Nothing worked. Back to default
1988 my $x = crypt($_[0], $alg . $_[1]);
1991 sub co { my $x = ~$_[0]; $x }
1993 $a = cr('hello', 'foo' . $TAINT);
1994 $b = cr('hello', 'foo');
1995 is_tainted($a, "tainted crypt");
1996 isnt_tainted($b, "untainted crypt");
1997 $a = co('foo' . $TAINT);
1999 is_tainted($a, "tainted complement");
2000 isnt_tainted($b, "untainted complement");
2005 my @data = qw(bonk zam zlonk qunckkk);
2006 # Clearly some sort of usenet bang-path
2007 my $string = $TAINT . join "!", @data;
2009 is_tainted($string, "tainted data");
2011 my @got = split /!|,/, $string;
2013 # each @got would be useful here, but I want the test for earlier perls
2014 for my $i (0 .. $#data) {
2015 is_tainted($got[$i], "tainted result $i");
2016 is($got[$i], $data[$i], "correct content $i");
2019 is_tainted($string, "still tainted data");
2021 my @got = split /[!,]/, $string;
2023 # each @got would be useful here, but I want the test for earlier perls
2024 for my $i (0 .. $#data) {
2025 is_tainted($got[$i], "tainted result $i");
2026 is($got[$i], $data[$i], "correct content $i");
2029 is_tainted($string, "still tainted data");
2031 my @got = split /!/, $string;
2033 # each @got would be useful here, but I want the test for earlier perls
2034 for my $i (0 .. $#data) {
2035 is_tainted($got[$i], "tainted result $i");
2036 is($got[$i], $data[$i], "correct content $i");
2040 # Bug RT #52552 - broken by change at git commit id f337b08
2042 my $x = $TAINT. q{print "Hello world\n"};
2043 my $y = pack "a*", $x;
2044 is_tainted($y, "pack a* preserves tainting");
2046 my $z = pack "A*", q{print "Hello world\n"}.$TAINT;
2047 is_tainted($z, "pack A* preserves tainting");
2049 my $zz = pack "a*a*", q{print "Hello world\n"}, $TAINT;
2050 is_tainted($zz, "pack a*a* preserves tainting");
2053 # Bug RT #61976 tainted $! would show numeric rather than string value
2056 my $tainted_path = substr($^X,0,0) . "/no/such/file";
2058 # $! is used in a tainted expression, so gets tainted
2059 open my $fh, $tainted_path or $err= "$!";
2060 unlike($err, qr/^\d+$/, 'tainted $!');
2064 # #6758: tainted values become untainted in tied hashes
2065 # (also applies to other value magic such as pos)
2070 sub TIEHASH { bless {} }
2071 sub TIEARRAY { bless {} }
2076 main::is_tainted($_[1], "tied arg1 tainted");
2077 main::is_tainted($_[2], "tied arg2 tainted");
2083 my ($k,$v) = qw(1111 val);
2085 tie my @array, 'P6758';
2086 tie my %hash , 'P6758';
2089 ok $i == 2, "tied STORE called correct number of times";
2092 # Bug RT #45167 the return value of sprintf sometimes wasn't tainted
2093 # when the args were tainted. This only occurred on the first use of
2094 # sprintf; after that, its TARG has taint magic attached, so setmagic
2095 # at the end works. That's why there are multiple sprintf's below, rather
2096 # than just one wrapped in an inner loop. Also, any plaintext between
2097 # format entries would correctly cause tainting to get set. so test with
2098 # "%s%s" rather than eg "%s %s".
2101 for my $var1 ($TAINT, "123") {
2102 for my $var2 ($TAINT0, "456") {
2103 is( tainted(sprintf '%s', $var1, $var2), tainted($var1),
2104 "sprintf '%s', '$var1', '$var2'" );
2105 is( tainted(sprintf ' %s', $var1, $var2), tainted($var1),
2106 "sprintf ' %s', '$var1', '$var2'" );
2107 is( tainted(sprintf '%s%s', $var1, $var2),
2108 tainted($var1) || tainted($var2),
2109 "sprintf '%s%s', '$var1', '$var2'" );
2115 # Bug RT #67962: old tainted $1 gets treated as tainted
2116 # in next untainted # match
2120 "abc".$TAINT =~ /(.*)/; # make $1 tainted
2121 is_tainted($1, '$1 should be tainted');
2123 my $untainted = "abcdef";
2124 isnt_tainted($untainted, '$untainted should be untainted');
2125 $untainted =~ s/(abc)/$1/;
2126 isnt_tainted($untainted, '$untainted should still be untainted');
2127 $untainted =~ s/(abc)/x$1/;
2128 isnt_tainted($untainted, '$untainted should yet still be untainted');
2132 # On Windows we can't spawn a fresh Perl interpreter unless at
2133 # least the Windows system directory (usually C:\Windows\System32)
2134 # is still on the PATH. There is however no way to determine the
2135 # actual path on the current system without loading the Win32
2136 # module, so we just restore the original $ENV{PATH} here.
2137 local $ENV{PATH} = $ENV{PATH};
2138 $ENV{PATH} = $old_env_path if $Is_MSWin32;
2140 fresh_perl_is(<<'end', "ok", { switches => [ '-T' ] },
2141 $TAINT = substr($^X, 0, 0);
2142 formline('@'.('<'x("2000".$TAINT)).' | @*', 'hallo', 'welt');
2145 "formline survives a tainted dynamic picture");
2149 isnt_tainted($^A, "format accumulator not tainted yet");
2150 formline('@ | @*', 'hallo' . $TAINT, 'welt');
2151 is_tainted($^A, "tainted formline argument makes a tainted accumulator");
2153 isnt_tainted($^A, "accumulator can be explicitly untainted");
2154 formline('@' .('<'*5) . ' | @*', 'hallo', 'welt');
2155 isnt_tainted($^A, "accumulator still untainted");
2157 is_tainted($^A, "accumulator can be explicitly tainted");
2158 formline('@' .('<'*5) . ' | @*', 'hallo', 'welt');
2159 is_tainted($^A, "accumulator still tainted");
2161 isnt_tainted($^A, "accumulator untainted again");
2162 formline('@' .('<'*5) . ' | @*', 'hallo', 'welt');
2163 isnt_tainted($^A, "accumulator still untainted");
2164 formline('@' .('<'*(5+$TAINT0)) . ' | @*', 'hallo', 'welt');
2165 is_tainted($^A, "the accumulator should be tainted already");
2166 is_tainted($^A, "tainted formline picture makes a tainted accumulator");
2170 "Constant(1)" =~ / ^ ([a-z_]\w*) (?: [(] (.*) [)] )? $ /xi;
2173 isnt_tainted($a, "regex optimization of single char /[]/i doesn't taint");
2174 isnt_tainted($b, "regex optimization of single char /[]/i doesn't taint");
2178 # RT 81230: tainted value during FETCH created extra ref to tied obj
2186 my $x = $^X; # tainted
2189 sub FETCH { my $x = $_[0]; $$x . "" }
2194 local $SIG{__WARN__} = sub { $w .= "@_" };
2196 untie %h if $h{"k"};
2198 ::is($w, "", "RT 81230");
2202 # Compiling a subroutine inside a tainted expression does not make the
2203 # constant folded values tainted.
2204 my $x = sub { "x" . "y" };
2205 my $y = $ENV{PATH} . $x->(); # Compile $x inside a tainted expression
2207 isnt_tainted($z, "Constants folded value not tainted");
2211 # now that regexes are first class SVs, make sure that they themselves
2212 # as well as references to them are tainted
2214 my $rr = qr/(.)$TAINT/;
2215 my $r = $$rr; # bare REGEX
2217 ok($s =~ s/$r/x/, "match bare regex");
2218 is_tainted($s, "match bare regex taint");
2219 is($s, 'xbc', "match bare regex taint value");
2223 # [perl #82616] security Issues with user-defined \p{} properties
2224 # A using a tainted user-defined property should croak
2226 sub IsA { sprintf "%02x", ord("A") }
2229 ok("A" =~ /\p{$prop}/, "user-defined property: non-tainted case");
2230 $prop = "IsA$TAINT";
2231 eval { "A" =~ /\p{$prop}/};
2232 like($@, qr/Insecure user-defined property \\p\{main::IsA}/,
2233 "user-defined property: tainted case");
2237 # [perl #87336] lc/uc(first) failing to taint the returned string
2238 my $source = "foo$TAINT";
2239 my $dest = lc $source;
2240 is_tainted $dest, "lc(tainted) taints its return value";
2241 $dest = lcfirst $source;
2242 is_tainted $dest, "lcfirst(tainted) taints its return value";
2244 is_tainted $dest, "uc(tainted) taints its return value";
2245 $dest = ucfirst $source;
2246 is_tainted $dest, "ucfirst(tainted) taints its return value";
2250 # Taintedness of values returned from given()
2251 use feature 'switch';
2252 no warnings 'experimental::smartmatch';
2254 my @descriptions = ('when', 'given end', 'default');
2257 my $letter = "$_$TAINT";
2259 my $desc = "tainted value returned from " . shift(@descriptions);
2263 when ('x') { $letter }
2264 when ('y') { goto leavegiven }
2269 is $res, $letter, "$desc is correct";
2270 is_tainted $res, "$desc stays tainted";
2275 # tainted constants and index()
2276 # RT 64804; http://bugs.debian.org/291450
2278 ok(tainted $old_env_path, "initial taintedness");
2279 BEGIN { no strict 'refs'; my $v = $old_env_path; *{"::C"} = sub () { $v }; }
2280 ok(tainted C, "constant is tainted properly");
2281 ok(!tainted "", "tainting not broken yet");
2283 ok(!tainted "", "tainting still works after index() of the constant");
2286 # Tainted values with smartmatch
2287 # [perl #93590] S_do_smartmatch stealing its own string buffers
2289 no warnings 'experimental::smartmatch';
2290 ok "M$TAINT" ~~ ['m', 'M'], '$tainted ~~ ["whatever", "match"]';
2291 ok !("M$TAINT" ~~ ['m', undef]), '$tainted ~~ ["whatever", undef]';
2294 # Tainted values and ref()
2296 my $x = bless \"M$TAINT", ref(bless[], "main");
2298 pass("no death when TARG of ref is tainted");
2300 # $$ should not be tainted by being read in a tainted expression.
2302 isnt_tainted $$, "PID not tainted initially";
2303 my $x = $ENV{PATH}.$$;
2304 isnt_tainted $$, "PID not tainted when read in tainted expression";
2308 skip 'Locales not available', 4 unless locales_enabled('LC_CTYPE');
2312 my ($latin1, $utf8) = ("\xDF") x 2;
2313 utf8::downgrade($latin1);
2314 utf8::upgrade($utf8);
2316 is_tainted fc($latin1), "under locale, lc(latin1) taints the result";
2317 is_tainted fc($utf8), "under locale, lc(utf8) taints the result";
2319 is_tainted "\F$latin1", "under locale, \\Flatin1 taints the result";
2320 is_tainted "\F$utf8", "under locale, \\Futf8 taints the result";
2325 eval { die "Test\n".substr($ENV{PATH}, 0, 0); };
2328 like($@, qr/^Test\n\t\.\.\.propagated at /, "error should be propagated");
2331 # tainted run-time (?{}) should die
2334 my $code = '(?{})' . $TAINT;
2336 eval { "a" =~ /$code/ };
2337 like($@, qr/Eval-group in insecure regular expression/, "tainted (?{})");
2340 # reset() and tainted undef (?!)
2342 $_ = "$TAINT".reset "x";
2343 is eval { eval $::x.1 }, 1, 'reset does not taint undef';
2347 # See the comment above the first formline test.
2348 local $ENV{PATH} = $ENV{PATH};
2349 $ENV{PATH} = $old_env_path if $Is_MSWin32;
2351 switches => [ '-T' ],
2352 prog => 'use constant K=>$^X; 0 if K; BEGIN{} use strict; '
2353 .'print 122669, qq-\n-',
2356 'tainted constant as logop condition should not prevent "use"';
2359 # optimised SETi etc need to handle tainting
2362 my ($i1, $i2, $i3) = (1, 1, 1);
2363 my ($n1, $n2, $n3) = (1.1, 1.1, 1.1);
2364 my $tn = $TAINT0 + 1.1;
2367 is_tainted $i1, "+ SETi";
2369 is_tainted $i2, "- SETi";
2371 is_tainted $i3, "* SETi";
2374 is_tainted $n1, "+ SETn";
2376 is_tainted $n2, "- SETn";
2378 is_tainted $n3, "* SETn";
2381 # check that localizing something with get magic (e.g. taint) doesn't
2382 # upgrade pIOK to IOK
2385 local our $x = 1.1 + $TAINT0; # $x should be NOK
2386 my $ix = int($x); # now NOK, pIOK
2391 isnt($x, 1); # it should be 1.1, not 1
2395 # This may bomb out with the alarm signal so keep it last
2397 skip "No alarm()" unless $Config{d_alarm};
2398 # Test from RT #41831]
2399 # [PATCH] Bug & fix: hang when using study + taint mode (perl 5.6.1, 5.8.x)
2401 my $DATA = <<'END' . $TAINT;
2411 ## don't set $SIG{ALRM}, since we'd never get to a user-level handler as
2412 ## perl is stuck in a regexp infinite loop!
2416 if ($DATA =~ /^line2.*line4/m) {
2417 fail("Should not be a match")
2419 pass("Match on tainted multiline data should fail promptly");
2425 # Keep the previous test last