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?
14 require './loc_tools.pl';
24 my $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 non-empty string
87 my $TAINTXYZ = "xyz".$TAINT;
88 # A tainted zero, useful for tainting numbers
95 # This taints each argument passed. All must be lvalues.
96 # Side effect: It also stringifies them. :-(
98 for (@_) { $_ .= $TAINT }
101 # How to identify taint when you see it
103 local $@; # Don't pollute caller's value.
104 not eval { join("",@_), kill 0; 1 };
109 local $::Level = $::Level + 1;
110 ok(tainted($thing), @_);
115 local $::Level = $::Level + 1;
116 ok(!tainted($thing), @_);
120 my ($code, $what, $desc) = @_;
122 local $::Level = $::Level + 1;
123 is(eval { $code->(); }, undef, $desc);
124 like($@, qr/^Insecure dependency in $what while running with -T switch/);
127 # We need an external program to call.
128 my $ECHO = ($Is_MSWin32 ? ".\\tmpecho$$" : ($Is_NetWare ? "tmpecho$$" : "./tmpecho$$"));
130 open my $fh, '>', $ECHO or die "Can't create $ECHO: $!";
131 print $fh 'print "@ARGV\n"', "\n";
133 my $echo = "$Invoke_Perl $ECHO";
137 # First, let's make sure that Perl is checking the dangerous
138 # environment variables. Maybe they aren't set yet, so we'll
139 # taint them ourselves.
141 $ENV{'DCL$PATH'} = '' if $Is_VMS;
143 $ENV{PATH} = ($Is_Cygwin) ? '/usr/bin' : '';
144 delete @ENV{@MoreEnv};
147 is(eval { `$echo 1` }, "1\n");
150 skip "Environment tainting tests skipped", 4
151 if $Is_MSWin32 || $Is_NetWare || $Is_VMS || $Is_Dos;
153 my @vars = ('PATH', @MoreEnv);
154 while (my $v = $vars[0]) {
155 local $ENV{$v} = $TAINT;
156 last if eval { `$echo 1` };
157 last unless $@ =~ /^Insecure \$ENV\{$v\}/;
162 # tainted $TERM is unsafe only if it contains metachars
164 $ENV{TERM} = 'e=mc2';
165 is(eval { `$echo 1` }, "1\n");
166 $ENV{TERM} = 'e=mc2' . $TAINT;
167 is(eval { `$echo 1` }, undef);
168 like($@, qr/^Insecure \$ENV\{TERM\}/);
172 if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_NetWare || $Is_Dos) {
173 print "# all directories are writeable\n";
176 $tmp = (grep { defined and -d and (stat _)[2] & 2 }
177 qw(sys$scratch /tmp /var/tmp /usr/tmp),
178 @ENV{qw(TMP TEMP)})[0]
179 or print "# can't find world-writeable directory to test PATH\n";
183 skip "all directories are writeable", 2 unless $tmp;
185 local $ENV{PATH} = $tmp;
186 is(eval { `$echo 1` }, undef);
187 # Message can be different depending on whether echo
188 # is a builtin or not
189 like($@, qr/^Insecure (?:directory in )?\$ENV\{PATH\}/);
192 # Relative paths in $ENV{PATH} are always implicitly tainted.
194 skip "Do these work on VMS?", 4 if $Is_VMS;
195 skip "Not applicable to DOSish systems", 4 if! $tmp;
197 local $ENV{PATH} = '.';
198 is(eval { `$echo 1` }, undef);
199 like($@, qr/^Insecure (?:directory in )?\$ENV\{PATH\}/);
201 # Backslash should not fool perl into thinking that this is one
203 local $ENV{PATH} = '/\:.';
204 is(eval { `$echo 1` }, undef);
205 like($@, qr/^Insecure (?:directory in )?\$ENV\{PATH\}/);
209 skip "This is not VMS", 4 unless $Is_VMS;
211 $ENV{'DCL$PATH'} = $TAINT;
212 is(eval { `$echo 1` }, undef);
213 like($@, qr/^Insecure \$ENV\{DCL\$PATH\}/);
215 skip q[can't find world-writeable directory to test DCL$PATH], 2
218 $ENV{'DCL$PATH'} = $tmp;
219 is(eval { `$echo 1` }, undef);
220 like($@, qr/^Insecure directory in \$ENV\{DCL\$PATH\}/);
222 $ENV{'DCL$PATH'} = '';
226 # Let's see that we can taint and untaint as needed.
231 # That was a sanity check. If it failed, stop the insanity!
232 die "Taint checks don't seem to be enabled" unless tainted $foo;
241 isnt_tainted($_) foreach @list;
242 taint_these @list[1,3,5,7,9];
243 is_tainted($_) foreach @list[1,3,5,7,9];
244 isnt_tainted($_) foreach @list[0,2,4,6,8];
246 ($foo) = $foo =~ /(.+)/;
249 my ($desc, $s, $res, $res2, $one);
251 $desc = "match with string tainted";
253 $s = 'abcd' . $TAINT;
256 is_tainted($s, "$desc: s tainted");
257 isnt_tainted($res, "$desc: res not tainted");
258 isnt_tainted($one, "$desc: \$1 not tainted");
259 is($res, 1, "$desc: res value");
260 is($one, 'abcd', "$desc: \$1 value");
262 $desc = "match /g with string tainted";
264 $s = 'abcd' . $TAINT;
267 is_tainted($s, "$desc: s tainted");
268 isnt_tainted($res, "$desc: res not tainted");
269 isnt_tainted($one, "$desc: \$1 not tainted");
270 is($res, 1, "$desc: res value");
271 is($one, 'a', "$desc: \$1 value");
273 $desc = "match with string tainted, list cxt";
275 $s = 'abcd' . $TAINT;
276 ($res) = $s =~ /(.+)/;
278 is_tainted($s, "$desc: s tainted");
279 isnt_tainted($res, "$desc: res not tainted");
280 isnt_tainted($one, "$desc: \$1 not tainted");
281 is($res, 'abcd', "$desc: res value");
282 is($one, 'abcd', "$desc: \$1 value");
284 $desc = "match /g with string tainted, list cxt";
286 $s = 'abcd' . $TAINT;
287 ($res, $res2) = $s =~ /(.)/g;
289 is_tainted($s, "$desc: s tainted");
290 isnt_tainted($res, "$desc: res not tainted");
291 isnt_tainted($res2,"$desc: res2 not tainted");
292 isnt_tainted($one, "$desc: \$1 not tainted");
293 is($res, 'a', "$desc: res value");
294 is($res2,'b', "$desc: res2 value");
295 is($one, 'd', "$desc: \$1 value");
297 $desc = "match with pattern tainted";
300 $res = $s =~ /$TAINT(.+)/;
302 isnt_tainted($s, "$desc: s not tainted");
303 isnt_tainted($res, "$desc: res not tainted");
304 is_tainted($one, "$desc: \$1 tainted");
305 is($res, 1, "$desc: res value");
306 is($one, 'abcd', "$desc: \$1 value");
308 $desc = "match /g with pattern tainted";
311 $res = $s =~ /$TAINT(.)/g;
313 isnt_tainted($s, "$desc: s not tainted");
314 isnt_tainted($res, "$desc: res not tainted");
315 is_tainted($one, "$desc: \$1 tainted");
316 is($res, 1, "$desc: res value");
317 is($one, 'a', "$desc: \$1 value");
320 skip 'Locales not available', 10 unless locales_enabled('LC_CTYPE');
322 $desc = "match with pattern tainted via locale";
327 $res = $s =~ /(\w+)/; $one = $1;
329 isnt_tainted($s, "$desc: s not tainted");
330 isnt_tainted($res, "$desc: res not tainted");
331 is_tainted($one, "$desc: \$1 tainted");
332 is($res, 1, "$desc: res value");
333 is($one, 'abcd', "$desc: \$1 value");
335 $desc = "match /g with pattern tainted via locale";
340 $res = $s =~ /(\w)/g; $one = $1;
342 isnt_tainted($s, "$desc: s not tainted");
343 isnt_tainted($res, "$desc: res not tainted");
344 is_tainted($one, "$desc: \$1 tainted");
345 is($res, 1, "$desc: res value");
346 is($one, 'a', "$desc: \$1 value");
349 $desc = "match with pattern tainted, list cxt";
352 ($res) = $s =~ /$TAINT(.+)/;
354 isnt_tainted($s, "$desc: s not tainted");
355 is_tainted($res, "$desc: res tainted");
356 is_tainted($one, "$desc: \$1 tainted");
357 is($res, 'abcd', "$desc: res value");
358 is($one, 'abcd', "$desc: \$1 value");
360 $desc = "match /g with pattern tainted, list cxt";
363 ($res, $res2) = $s =~ /$TAINT(.)/g;
365 isnt_tainted($s, "$desc: s not tainted");
366 is_tainted($res, "$desc: res tainted");
367 is_tainted($one, "$desc: \$1 tainted");
368 is($res, 'a', "$desc: res value");
369 is($res2,'b', "$desc: res2 value");
370 is($one, 'd', "$desc: \$1 value");
373 skip 'Locales not available', 12 unless locales_enabled('LC_CTYPE');
375 $desc = "match with pattern tainted via locale, list cxt";
380 ($res) = $s =~ /(\w+)/; $one = $1;
382 isnt_tainted($s, "$desc: s not tainted");
383 is_tainted($res, "$desc: res tainted");
384 is_tainted($one, "$desc: \$1 tainted");
385 is($res, 'abcd', "$desc: res value");
386 is($one, 'abcd', "$desc: \$1 value");
388 $desc = "match /g with pattern tainted via locale, list cxt";
393 ($res, $res2) = $s =~ /(\w)/g; $one = $1;
395 isnt_tainted($s, "$desc: s not tainted");
396 is_tainted($res, "$desc: res tainted");
397 is_tainted($res2, "$desc: res2 tainted");
398 is_tainted($one, "$desc: \$1 tainted");
399 is($res, 'a', "$desc: res value");
400 is($res2,'b', "$desc: res2 value");
401 is($one, 'd', "$desc: \$1 value");
404 $desc = "substitution with string tainted";
406 $s = 'abcd' . $TAINT;
407 $res = $s =~ s/(.+)/xyz/;
409 is_tainted($s, "$desc: s tainted");
410 isnt_tainted($res, "$desc: res not tainted");
411 isnt_tainted($one, "$desc: \$1 not tainted");
412 is($s, 'xyz', "$desc: s value");
413 is($res, 1, "$desc: res value");
414 is($one, 'abcd', "$desc: \$1 value");
416 $desc = "substitution /g with string tainted";
418 $s = 'abcd' . $TAINT;
419 $res = $s =~ s/(.)/x/g;
421 is_tainted($s, "$desc: s tainted");
422 is_tainted($res, "$desc: res tainted");
423 isnt_tainted($one, "$desc: \$1 not tainted");
424 is($s, 'xxxx', "$desc: s value");
425 is($res, 4, "$desc: res value");
426 is($one, 'd', "$desc: \$1 value");
428 $desc = "substitution /r with string tainted";
430 $s = 'abcd' . $TAINT;
431 $res = $s =~ s/(.+)/xyz/r;
433 is_tainted($s, "$desc: s tainted");
434 is_tainted($res, "$desc: res tainted");
435 isnt_tainted($one, "$desc: \$1 not tainted");
436 is($s, 'abcd', "$desc: s value");
437 is($res, 'xyz', "$desc: res value");
438 is($one, 'abcd', "$desc: \$1 value");
440 $desc = "substitution /e with string tainted";
442 $s = 'abcd' . $TAINT;
444 $res = $s =~ s{(.+)}{
445 $one = $one . "x"; # make sure code not tainted
446 isnt_tainted($one, "$desc: code not tainted within /e");
448 isnt_tainted($one, "$desc: \$1 not tainted within /e");
452 is_tainted($s, "$desc: s tainted");
453 isnt_tainted($res, "$desc: res not tainted");
454 isnt_tainted($one, "$desc: \$1 not tainted");
455 is($s, 'xyz', "$desc: s value");
456 is($res, 1, "$desc: res value");
457 is($one, 'abcd', "$desc: \$1 value");
459 $desc = "substitution with pattern tainted";
462 $res = $s =~ s/$TAINT(.+)/xyz/;
464 is_tainted($s, "$desc: s tainted");
465 isnt_tainted($res, "$desc: res not tainted");
466 is_tainted($one, "$desc: \$1 tainted");
467 is($s, 'xyz', "$desc: s value");
468 is($res, 1, "$desc: res value");
469 is($one, 'abcd', "$desc: \$1 value");
471 $desc = "substitution /g with pattern tainted";
474 $res = $s =~ s/$TAINT(.)/x/g;
476 is_tainted($s, "$desc: s tainted");
477 is_tainted($res, "$desc: res tainted");
478 is_tainted($one, "$desc: \$1 tainted");
479 is($s, 'xxxx', "$desc: s value");
480 is($res, 4, "$desc: res value");
481 is($one, 'd', "$desc: \$1 value");
483 $desc = "substitution /ge with pattern tainted";
489 $res = $s =~ s{(.)$TAINT}{
490 $j = $i; # make sure code not tainted
492 isnt_tainted($j, "$desc: code not tainted within /e");
495 isnt_tainted($s, "$desc: s not tainted loop 1");
498 is_tainted($s, "$desc: s tainted loop $i");
500 is_tainted($one, "$desc: \$1 tainted loop $i");
505 is_tainted($s, "$desc: s tainted");
506 is_tainted($res, "$desc: res tainted");
507 is_tainted($one, "$desc: \$1 tainted");
508 is($s, '123', "$desc: s value");
509 is($res, 3, "$desc: res value");
510 is($one, 'c', "$desc: \$1 value");
512 $desc = "substitution /r with pattern tainted";
515 $res = $s =~ s/$TAINT(.+)/xyz/r;
517 isnt_tainted($s, "$desc: s not tainted");
518 is_tainted($res, "$desc: res tainted");
519 is_tainted($one, "$desc: \$1 tainted");
520 is($s, 'abcd', "$desc: s value");
521 is($res, 'xyz', "$desc: res value");
522 is($one, 'abcd', "$desc: \$1 value");
525 skip 'Locales not available', 18 unless locales_enabled('LC_CTYPE');
527 $desc = "substitution with pattern tainted via locale";
532 $res = $s =~ s/(\w+)/xyz/; $one = $1;
534 is_tainted($s, "$desc: s tainted");
535 isnt_tainted($res, "$desc: res not tainted");
536 is_tainted($one, "$desc: \$1 tainted");
537 is($s, 'xyz', "$desc: s value");
538 is($res, 1, "$desc: res value");
539 is($one, 'abcd', "$desc: \$1 value");
541 $desc = "substitution /g with pattern tainted via locale";
546 $res = $s =~ s/(\w)/x/g; $one = $1;
548 is_tainted($s, "$desc: s tainted");
549 is_tainted($res, "$desc: res tainted");
550 is_tainted($one, "$desc: \$1 tainted");
551 is($s, 'xxxx', "$desc: s value");
552 is($res, 4, "$desc: res value");
553 is($one, 'd', "$desc: \$1 value");
555 $desc = "substitution /r with pattern tainted via locale";
560 $res = $s =~ s/(\w+)/xyz/r; $one = $1;
562 isnt_tainted($s, "$desc: s not tainted");
563 is_tainted($res, "$desc: res tainted");
564 is_tainted($one, "$desc: \$1 tainted");
565 is($s, 'abcd', "$desc: s value");
566 is($res, 'xyz', "$desc: res value");
567 is($one, 'abcd', "$desc: \$1 value");
570 $desc = "substitution with partial replacement tainted";
573 $res = $s =~ s/(.+)/xyz$TAINT/;
575 is_tainted($s, "$desc: s tainted");
576 isnt_tainted($res, "$desc: res not tainted");
577 isnt_tainted($one, "$desc: \$1 not tainted");
578 is($s, 'xyz', "$desc: s value");
579 is($res, 1, "$desc: res value");
580 is($one, 'abcd', "$desc: \$1 value");
582 $desc = "substitution /g with partial replacement tainted";
585 $res = $s =~ s/(.)/x$TAINT/g;
587 is_tainted($s, "$desc: s tainted");
588 isnt_tainted($res, "$desc: res not tainted");
589 isnt_tainted($one, "$desc: \$1 not tainted");
590 is($s, 'xxxx', "$desc: s value");
591 is($res, 4, "$desc: res value");
592 is($one, 'd', "$desc: \$1 value");
594 $desc = "substitution /ge with partial replacement tainted";
601 $j = $i; # make sure code not tainted
603 isnt_tainted($j, "$desc: code not tainted within /e");
606 isnt_tainted($s, "$desc: s not tainted loop 1");
609 is_tainted($s, "$desc: s tainted loop $i");
611 isnt_tainted($one, "$desc: \$1 not tainted within /e");
616 is_tainted($s, "$desc: s tainted");
617 isnt_tainted($res, "$desc: res tainted");
618 isnt_tainted($one, "$desc: \$1 not tainted");
619 is($s, '123', "$desc: s value");
620 is($res, 3, "$desc: res value");
621 is($one, 'c', "$desc: \$1 value");
623 $desc = "substitution /r with partial replacement tainted";
626 $res = $s =~ s/(.+)/xyz$TAINT/r;
628 isnt_tainted($s, "$desc: s not tainted");
629 is_tainted($res, "$desc: res tainted");
630 isnt_tainted($one, "$desc: \$1 not tainted");
631 is($s, 'abcd', "$desc: s value");
632 is($res, 'xyz', "$desc: res value");
633 is($one, 'abcd', "$desc: \$1 value");
635 $desc = "substitution with whole replacement tainted";
638 $res = $s =~ s/(.+)/$TAINTXYZ/;
640 is_tainted($s, "$desc: s tainted");
641 isnt_tainted($res, "$desc: res not tainted");
642 isnt_tainted($one, "$desc: \$1 not tainted");
643 is($s, 'xyz', "$desc: s value");
644 is($res, 1, "$desc: res value");
645 is($one, 'abcd', "$desc: \$1 value");
647 $desc = "substitution /g with whole replacement tainted";
650 $res = $s =~ s/(.)/$TAINTXYZ/g;
652 is_tainted($s, "$desc: s tainted");
653 isnt_tainted($res, "$desc: res not tainted");
654 isnt_tainted($one, "$desc: \$1 not tainted");
655 is($s, 'xyz' x 4, "$desc: s value");
656 is($res, 4, "$desc: res value");
657 is($one, 'd', "$desc: \$1 value");
659 $desc = "substitution /ge with whole replacement tainted";
666 $j = $i; # make sure code not tainted
668 isnt_tainted($j, "$desc: code not tainted within /e");
671 isnt_tainted($s, "$desc: s not tainted loop 1");
674 is_tainted($s, "$desc: s tainted loop $i");
676 isnt_tainted($one, "$desc: \$1 not tainted within /e");
681 is_tainted($s, "$desc: s tainted");
682 isnt_tainted($res, "$desc: res tainted");
683 isnt_tainted($one, "$desc: \$1 not tainted");
684 is($s, 'xyz' x 3, "$desc: s value");
685 is($res, 3, "$desc: res value");
686 is($one, 'c', "$desc: \$1 value");
688 $desc = "substitution /r with whole replacement tainted";
691 $res = $s =~ s/(.+)/$TAINTXYZ/r;
693 isnt_tainted($s, "$desc: s not tainted");
694 is_tainted($res, "$desc: res tainted");
695 isnt_tainted($one, "$desc: \$1 not tainted");
696 is($s, 'abcd', "$desc: s value");
697 is($res, 'xyz', "$desc: res value");
698 is($one, 'abcd', "$desc: \$1 value");
701 # now do them all again with "use re 'taint"
705 $desc = "use re 'taint': match with string tainted";
707 $s = 'abcd' . $TAINT;
710 is_tainted($s, "$desc: s tainted");
711 isnt_tainted($res, "$desc: res not tainted");
712 is_tainted($one, "$desc: \$1 tainted");
713 is($res, 1, "$desc: res value");
714 is($one, 'abcd', "$desc: \$1 value");
716 $desc = "use re 'taint': match /g with string tainted";
718 $s = 'abcd' . $TAINT;
721 is_tainted($s, "$desc: s tainted");
722 isnt_tainted($res, "$desc: res not tainted");
723 is_tainted($one, "$desc: \$1 tainted");
724 is($res, 1, "$desc: res value");
725 is($one, 'a', "$desc: \$1 value");
727 $desc = "use re 'taint': match with string tainted, list cxt";
729 $s = 'abcd' . $TAINT;
730 ($res) = $s =~ /(.+)/;
732 is_tainted($s, "$desc: s tainted");
733 is_tainted($res, "$desc: res tainted");
734 is_tainted($one, "$desc: \$1 tainted");
735 is($res, 'abcd', "$desc: res value");
736 is($one, 'abcd', "$desc: \$1 value");
738 $desc = "use re 'taint': match /g with string tainted, list cxt";
740 $s = 'abcd' . $TAINT;
741 ($res, $res2) = $s =~ /(.)/g;
743 is_tainted($s, "$desc: s tainted");
744 is_tainted($res, "$desc: res tainted");
745 is_tainted($res2, "$desc: res2 tainted");
746 is_tainted($one, "$desc: \$1 not tainted");
747 is($res, 'a', "$desc: res value");
748 is($res2,'b', "$desc: res2 value");
749 is($one, 'd', "$desc: \$1 value");
751 $desc = "use re 'taint': match with pattern tainted";
754 $res = $s =~ /$TAINT(.+)/;
756 isnt_tainted($s, "$desc: s not tainted");
757 isnt_tainted($res, "$desc: res not tainted");
758 is_tainted($one, "$desc: \$1 tainted");
759 is($res, 1, "$desc: res value");
760 is($one, 'abcd', "$desc: \$1 value");
762 $desc = "use re 'taint': match /g with pattern tainted";
765 $res = $s =~ /$TAINT(.)/g;
767 isnt_tainted($s, "$desc: s not tainted");
768 isnt_tainted($res, "$desc: res not tainted");
769 is_tainted($one, "$desc: \$1 tainted");
770 is($res, 1, "$desc: res value");
771 is($one, 'a', "$desc: \$1 value");
774 skip 'Locales not available', 10 unless locales_enabled('LC_CTYPE');
776 $desc = "use re 'taint': match with pattern tainted via locale";
781 $res = $s =~ /(\w+)/; $one = $1;
783 isnt_tainted($s, "$desc: s not tainted");
784 isnt_tainted($res, "$desc: res not tainted");
785 is_tainted($one, "$desc: \$1 tainted");
786 is($res, 1, "$desc: res value");
787 is($one, 'abcd', "$desc: \$1 value");
789 $desc = "use re 'taint': match /g with pattern tainted via locale";
794 $res = $s =~ /(\w)/g; $one = $1;
796 isnt_tainted($s, "$desc: s not tainted");
797 isnt_tainted($res, "$desc: res not tainted");
798 is_tainted($one, "$desc: \$1 tainted");
799 is($res, 1, "$desc: res value");
800 is($one, 'a', "$desc: \$1 value");
803 $desc = "use re 'taint': match with pattern tainted, list cxt";
806 ($res) = $s =~ /$TAINT(.+)/;
808 isnt_tainted($s, "$desc: s not tainted");
809 is_tainted($res, "$desc: res tainted");
810 is_tainted($one, "$desc: \$1 tainted");
811 is($res, 'abcd', "$desc: res value");
812 is($one, 'abcd', "$desc: \$1 value");
814 $desc = "use re 'taint': match /g with pattern tainted, list cxt";
817 ($res, $res2) = $s =~ /$TAINT(.)/g;
819 isnt_tainted($s, "$desc: s not tainted");
820 is_tainted($res, "$desc: res tainted");
821 is_tainted($one, "$desc: \$1 tainted");
822 is($res, 'a', "$desc: res value");
823 is($res2,'b', "$desc: res2 value");
824 is($one, 'd', "$desc: \$1 value");
827 skip 'Locales not available', 12 unless locales_enabled('LC_CTYPE');
829 $desc = "use re 'taint': match with pattern tainted via locale, list cxt";
834 ($res) = $s =~ /(\w+)/; $one = $1;
836 isnt_tainted($s, "$desc: s not tainted");
837 is_tainted($res, "$desc: res tainted");
838 is_tainted($one, "$desc: \$1 tainted");
839 is($res, 'abcd', "$desc: res value");
840 is($one, 'abcd', "$desc: \$1 value");
842 $desc = "use re 'taint': match /g with pattern tainted via locale, list cxt";
847 ($res, $res2) = $s =~ /(\w)/g; $one = $1;
849 isnt_tainted($s, "$desc: s not tainted");
850 is_tainted($res, "$desc: res tainted");
851 is_tainted($res2, "$desc: res2 tainted");
852 is_tainted($one, "$desc: \$1 tainted");
853 is($res, 'a', "$desc: res value");
854 is($res2,'b', "$desc: res2 value");
855 is($one, 'd', "$desc: \$1 value");
858 $desc = "use re 'taint': substitution with string tainted";
860 $s = 'abcd' . $TAINT;
861 $res = $s =~ s/(.+)/xyz/;
863 is_tainted($s, "$desc: s tainted");
864 isnt_tainted($res, "$desc: res not tainted");
865 is_tainted($one, "$desc: \$1 tainted");
866 is($s, 'xyz', "$desc: s value");
867 is($res, 1, "$desc: res value");
868 is($one, 'abcd', "$desc: \$1 value");
870 $desc = "use re 'taint': substitution /g with string tainted";
872 $s = 'abcd' . $TAINT;
873 $res = $s =~ s/(.)/x/g;
875 is_tainted($s, "$desc: s tainted");
876 is_tainted($res, "$desc: res tainted");
877 is_tainted($one, "$desc: \$1 tainted");
878 is($s, 'xxxx', "$desc: s value");
879 is($res, 4, "$desc: res value");
880 is($one, 'd', "$desc: \$1 value");
882 $desc = "use re 'taint': substitution /r with string tainted";
884 $s = 'abcd' . $TAINT;
885 $res = $s =~ s/(.+)/xyz/r;
887 is_tainted($s, "$desc: s tainted");
888 is_tainted($res, "$desc: res tainted");
889 is_tainted($one, "$desc: \$1 tainted");
890 is($s, 'abcd', "$desc: s value");
891 is($res, 'xyz', "$desc: res value");
892 is($one, 'abcd', "$desc: \$1 value");
894 $desc = "use re 'taint': substitution /e with string tainted";
896 $s = 'abcd' . $TAINT;
898 $res = $s =~ s{(.+)}{
899 $one = $one . "x"; # make sure code not tainted
900 isnt_tainted($one, "$desc: code not tainted within /e");
902 is_tainted($one, "$desc: $1 tainted within /e");
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 with pattern tainted";
916 $res = $s =~ s/$TAINT(.+)/xyz/;
918 is_tainted($s, "$desc: s tainted");
919 isnt_tainted($res, "$desc: res not tainted");
920 is_tainted($one, "$desc: \$1 tainted");
921 is($s, 'xyz', "$desc: s value");
922 is($res, 1, "$desc: res value");
923 is($one, 'abcd', "$desc: \$1 value");
925 $desc = "use re 'taint': substitution /g with pattern tainted";
928 $res = $s =~ s/$TAINT(.)/x/g;
930 is_tainted($s, "$desc: s tainted");
931 is_tainted($res, "$desc: res tainted");
932 is_tainted($one, "$desc: \$1 tainted");
933 is($s, 'xxxx', "$desc: s value");
934 is($res, 4, "$desc: res value");
935 is($one, 'd', "$desc: \$1 value");
937 $desc = "use re 'taint': substitution /ge with pattern tainted";
943 $res = $s =~ s{(.)$TAINT}{
944 $j = $i; # make sure code not tainted
946 isnt_tainted($j, "$desc: code not tainted within /e");
949 isnt_tainted($s, "$desc: s not tainted loop 1");
952 is_tainted($s, "$desc: s tainted loop $i");
954 is_tainted($one, "$desc: \$1 tainted loop $i");
959 is_tainted($s, "$desc: s tainted");
960 is_tainted($res, "$desc: res tainted");
961 is_tainted($one, "$desc: \$1 tainted");
962 is($s, '123', "$desc: s value");
963 is($res, 3, "$desc: res value");
964 is($one, 'c', "$desc: \$1 value");
967 $desc = "use re 'taint': substitution /r with pattern tainted";
970 $res = $s =~ s/$TAINT(.+)/xyz/r;
972 isnt_tainted($s, "$desc: s not tainted");
973 is_tainted($res, "$desc: res tainted");
974 is_tainted($one, "$desc: \$1 tainted");
975 is($s, 'abcd', "$desc: s value");
976 is($res, 'xyz', "$desc: res value");
977 is($one, 'abcd', "$desc: \$1 value");
980 skip 'Locales not available', 18 unless locales_enabled('LC_CTYPE');
982 $desc = "use re 'taint': substitution with pattern tainted via locale";
987 $res = $s =~ s/(\w+)/xyz/; $one = $1;
989 is_tainted($s, "$desc: s tainted");
990 isnt_tainted($res, "$desc: res not tainted");
991 is_tainted($one, "$desc: \$1 tainted");
992 is($s, 'xyz', "$desc: s value");
993 is($res, 1, "$desc: res value");
994 is($one, 'abcd', "$desc: \$1 value");
996 $desc = "use re 'taint': substitution /g with pattern tainted via locale";
1001 $res = $s =~ s/(\w)/x/g; $one = $1;
1003 is_tainted($s, "$desc: s tainted");
1004 is_tainted($res, "$desc: res tainted");
1005 is_tainted($one, "$desc: \$1 tainted");
1006 is($s, 'xxxx', "$desc: s value");
1007 is($res, 4, "$desc: res value");
1008 is($one, 'd', "$desc: \$1 value");
1010 $desc = "use re 'taint': substitution /r with pattern tainted via locale";
1015 $res = $s =~ s/(\w+)/xyz/r; $one = $1;
1017 isnt_tainted($s, "$desc: s not tainted");
1018 is_tainted($res, "$desc: res tainted");
1019 is_tainted($one, "$desc: \$1 tainted");
1020 is($s, 'abcd', "$desc: s value");
1021 is($res, 'xyz', "$desc: res value");
1022 is($one, 'abcd', "$desc: \$1 value");
1025 $desc = "use re 'taint': substitution with partial replacement tainted";
1028 $res = $s =~ s/(.+)/xyz$TAINT/;
1030 is_tainted($s, "$desc: s tainted");
1031 isnt_tainted($res, "$desc: res not tainted");
1032 isnt_tainted($one, "$desc: \$1 not tainted");
1033 is($s, 'xyz', "$desc: s value");
1034 is($res, 1, "$desc: res value");
1035 is($one, 'abcd', "$desc: \$1 value");
1037 $desc = "use re 'taint': substitution /g with partial replacement tainted";
1040 $res = $s =~ s/(.)/x$TAINT/g;
1042 is_tainted($s, "$desc: s tainted");
1043 isnt_tainted($res, "$desc: res not tainted");
1044 isnt_tainted($one, "$desc: \$1 not tainted");
1045 is($s, 'xxxx', "$desc: s value");
1046 is($res, 4, "$desc: res value");
1047 is($one, 'd', "$desc: \$1 value");
1049 $desc = "use re 'taint': substitution /ge with partial replacement tainted";
1055 $res = $s =~ s{(.)}{
1056 $j = $i; # make sure code not tainted
1058 isnt_tainted($j, "$desc: code not tainted within /e");
1061 isnt_tainted($s, "$desc: s not tainted loop 1");
1064 is_tainted($s, "$desc: s tainted loop $i");
1066 isnt_tainted($one, "$desc: \$1 not tainted");
1071 is_tainted($s, "$desc: s tainted");
1072 isnt_tainted($res, "$desc: res tainted");
1073 isnt_tainted($one, "$desc: \$1 not tainted");
1074 is($s, '123', "$desc: s value");
1075 is($res, 3, "$desc: res value");
1076 is($one, 'c', "$desc: \$1 value");
1078 $desc = "use re 'taint': substitution /r with partial replacement tainted";
1081 $res = $s =~ s/(.+)/xyz$TAINT/r;
1083 isnt_tainted($s, "$desc: s not tainted");
1084 is_tainted($res, "$desc: res tainted");
1085 isnt_tainted($one, "$desc: \$1 not tainted");
1086 is($s, 'abcd', "$desc: s value");
1087 is($res, 'xyz', "$desc: res value");
1088 is($one, 'abcd', "$desc: \$1 value");
1090 $desc = "use re 'taint': substitution with whole replacement tainted";
1093 $res = $s =~ s/(.+)/$TAINTXYZ/;
1095 is_tainted($s, "$desc: s tainted");
1096 isnt_tainted($res, "$desc: res not tainted");
1097 isnt_tainted($one, "$desc: \$1 not tainted");
1098 is($s, 'xyz', "$desc: s value");
1099 is($res, 1, "$desc: res value");
1100 is($one, 'abcd', "$desc: \$1 value");
1102 $desc = "use re 'taint': substitution /g with whole replacement tainted";
1105 $res = $s =~ s/(.)/$TAINTXYZ/g;
1107 is_tainted($s, "$desc: s tainted");
1108 isnt_tainted($res, "$desc: res not tainted");
1109 isnt_tainted($one, "$desc: \$1 not tainted");
1110 is($s, 'xyz' x 4, "$desc: s value");
1111 is($res, 4, "$desc: res value");
1112 is($one, 'd', "$desc: \$1 value");
1114 $desc = "use re 'taint': substitution /ge with whole replacement tainted";
1120 $res = $s =~ s{(.)}{
1121 $j = $i; # make sure code not tainted
1123 isnt_tainted($j, "$desc: code not tainted within /e");
1126 isnt_tainted($s, "$desc: s not tainted loop 1");
1129 is_tainted($s, "$desc: s tainted loop $i");
1131 isnt_tainted($one, "$desc: \$1 not tainted");
1136 is_tainted($s, "$desc: s tainted");
1137 isnt_tainted($res, "$desc: res tainted");
1138 isnt_tainted($one, "$desc: \$1 not tainted");
1139 is($s, 'xyz' x 3, "$desc: s value");
1140 is($res, 3, "$desc: res value");
1141 is($one, 'c', "$desc: \$1 value");
1143 $desc = "use re 'taint': substitution /r with whole replacement tainted";
1146 $res = $s =~ s/(.+)/$TAINTXYZ/r;
1148 isnt_tainted($s, "$desc: s not tainted");
1149 is_tainted($res, "$desc: res tainted");
1150 isnt_tainted($one, "$desc: \$1 not tainted");
1151 is($s, 'abcd', "$desc: s value");
1152 is($res, 'xyz', "$desc: res value");
1153 is($one, 'abcd', "$desc: \$1 value");
1155 # [perl #121854] match taintedness became sticky
1156 # when one match has a taintess result, subseqent matches
1157 # using the same pattern shouldn't necessarily be tainted
1160 my $f = sub { $_[0] =~ /(.*)/ or die; $1 };
1161 $res = $f->($TAINT);
1162 is_tainted($res, "121854: res tainted");
1164 isnt_tainted($res, "121854: res not tainted");
1168 $foo = $1 if 'bar' =~ /(.+)$TAINT/;
1172 my $pi = 4 * atan2(1,1) + $TAINT0;
1175 ($pi) = $pi =~ /(\d+\.\d+)/;
1177 is(sprintf("%.5f", $pi), '3.14159');
1180 # How about command-line arguments? The problem is that we don't
1181 # always get some, so we'll run another process with some.
1183 my $arg = tempfile();
1184 open $fh, '>', $arg or die "Can't create $arg: $!";
1186 eval { join('', @ARGV), kill 0 };
1187 exit 0 if $@ =~ /^Insecure dependency/;
1188 print "# Oops: \$@ was [$@]\n";
1191 close $fh or die "Can't close $arg: $!";
1192 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
1193 is($?, 0, "Exited with status $?");
1197 # Reading from a file should be tainted
1199 ok(open my $fh, '<', $TEST) or diag("Couldn't open '$TEST': $!");
1202 sysread($fh, $block, 100);
1209 # Output of commands should be tainted
1211 my $foo = `$echo abc`;
1215 # Certain system variables should be tainted
1221 # Results of matching should all be untainted
1223 my $foo = "abcdefghi" . $TAINT;
1231 $foo =~ /(...)(...)(...)/;
1237 my @bar = $foo =~ /(...)(...)(...)/;
1238 isnt_tainted($_) foreach @bar;
1240 is_tainted($foo); # $foo should still be tainted!
1241 is($foo, "abcdefghi");
1244 # Operations which affect files can't use tainted data.
1246 violates_taint(sub { chmod 0, $TAINT }, 'chmod');
1249 skip "truncate() is not available", 2 unless $Config{d_truncate};
1251 violates_taint(sub { truncate 'NoSuChFiLe', $TAINT0 }, 'truncate');
1254 violates_taint(sub { rename '', $TAINT }, 'rename');
1255 violates_taint(sub { unlink $TAINT }, 'unlink');
1256 violates_taint(sub { utime $TAINT }, 'utime');
1259 skip "chown() is not available", 2 unless $Config{d_chown};
1261 violates_taint(sub { chown -1, -1, $TAINT }, 'chown');
1265 skip "link() is not available", 2 unless $Config{d_link};
1267 violates_taint(sub { link $TAINT, '' }, 'link');
1271 skip "symlink() is not available", 2 unless $Config{d_symlink};
1273 violates_taint(sub { symlink $TAINT, '' }, 'symlink');
1277 # Operations which affect directories can't use tainted data.
1279 violates_taint(sub { mkdir "foo".$TAINT, 0755 . $TAINT0 }, 'mkdir');
1280 violates_taint(sub { rmdir $TAINT }, 'rmdir');
1281 violates_taint(sub { chdir "foo".$TAINT }, 'chdir');
1284 skip "chroot() is not available", 2 unless $Config{d_chroot};
1286 violates_taint(sub { chroot $TAINT }, 'chroot');
1290 # Some operations using files can't use tainted data.
1292 my $foo = "imaginary library" . $TAINT;
1293 violates_taint(sub { require $foo }, 'require');
1294 violates_taint(sub { do $foo }, 'do');
1296 my $filename = tempfile(); # NB: $filename isn't tainted!
1297 $foo = $filename . $TAINT;
1298 unlink $filename; # in any case
1300 is(eval { open FOO, $foo }, undef, 'open for read');
1301 is($@, ''); # NB: This should be allowed
1302 is(eval { open my $fh, , '<', $foo }, undef, 'open for read');
1303 is($@, ''); # NB: This should be allowed
1305 # Try first new style but allow also old style.
1306 # We do not want the whole taint.t to fail
1307 # just because Errno possibly failing.
1308 ok(eval('$!{ENOENT}') ||
1309 $! == 2 || # File not found
1310 ($Is_Dos && $! == 22));
1312 violates_taint(sub { open FOO, "> $foo" }, 'open', 'open for write');
1313 violates_taint(sub { open my $fh, '>', $foo }, 'open', 'open for write');
1316 # Commands to the system can't use tainted data
1321 skip "open('|') is not available", 8 if $^O eq 'amigaos';
1323 violates_taint(sub { open FOO, "| x$foo" }, 'piped open', 'popen to');
1324 violates_taint(sub { open FOO, "x$foo |" }, 'piped open', 'popen from');
1325 violates_taint(sub { open my $fh, '|-', "x$foo" }, 'piped open', 'popen to');
1326 violates_taint(sub { open my $fh, '-|', "x$foo" }, 'piped open', 'popen from');
1329 violates_taint(sub { exec $TAINT }, 'exec');
1330 violates_taint(sub { system $TAINT }, 'system');
1335 violates_taint(sub { `$echo 1$foo` }, '``', 'backticks');
1338 # wildcard expansion doesn't invoke shell on VMS, so is safe
1339 skip "This is not VMS", 2 unless $Is_VMS;
1341 isnt(join('', eval { glob $foo } ), '', 'globbing');
1346 # Operations which affect processes can't use tainted data.
1348 violates_taint(sub { kill 0, $TAINT }, 'kill');
1351 skip "setpgrp() is not available", 2 unless $Config{d_setpgrp};
1353 violates_taint(sub { setpgrp 0, $TAINT0 }, 'setpgrp');
1357 skip "setpriority() is not available", 2 unless $Config{d_setprior};
1359 violates_taint(sub { setpriority 0, $TAINT0, $TAINT0 }, 'setpriority');
1363 # Some miscellaneous operations can't use tainted data.
1366 skip "syscall() is not available", 2 unless $Config{d_syscall};
1368 violates_taint(sub { syscall $TAINT }, 'syscall');
1372 my $foo = "x" x 979;
1375 my $temp = tempfile();
1376 ok(open FOO, "> $temp") or diag("Couldn't open $temp for write: $!");
1377 violates_taint(sub { ioctl FOO, $TAINT0, $foo }, 'ioctl');
1379 my $temp2 = tempfile();
1380 ok(open my $fh, '>', $temp2) or diag("Couldn't open $temp2 for write: $!");
1381 violates_taint(sub { ioctl $fh, $TAINT0, $foo }, 'ioctl');
1384 skip "fcntl() is not available", 4 unless $Config{d_fcntl};
1386 violates_taint(sub { fcntl FOO, $TAINT0, $foo }, 'fcntl');
1387 violates_taint(sub { fcntl $fh, $TAINT0, $foo }, 'fcntl');
1394 # Some tests involving references
1396 my $foo = 'abc' . $TAINT;
1398 isnt_tainted($fooref);
1399 is_tainted($$fooref);
1403 # Some tests involving assignment
1409 is_tainted($foo = $bar);
1410 is_tainted($bar = $bar);
1411 is_tainted($bar += $bar);
1412 is_tainted($bar -= $bar);
1413 is_tainted($bar *= $bar);
1415 is_tainted($bar /= $bar);
1416 is_tainted($bar += 0);
1417 is_tainted($bar -= 2);
1418 is_tainted($bar *= -1);
1419 is_tainted($bar /= 1);
1424 # Test assignment and return of lists
1426 my @foo = ("A", "tainted" . $TAINT, "B");
1427 isnt_tainted($foo[0]);
1428 is_tainted( $foo[1]);
1429 isnt_tainted($foo[2]);
1431 isnt_tainted($bar[0]);
1432 is_tainted( $bar[1]);
1433 isnt_tainted($bar[2]);
1434 my @baz = eval { "A", "tainted" . $TAINT, "B" };
1435 isnt_tainted($baz[0]);
1436 is_tainted( $baz[1]);
1437 isnt_tainted($baz[2]);
1438 my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
1439 isnt_tainted($plugh[0]);
1440 is_tainted( $plugh[1]);
1441 isnt_tainted($plugh[2]);
1442 my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
1443 isnt_tainted(((&$nautilus)[0]));
1444 is_tainted( ((&$nautilus)[1]));
1445 isnt_tainted(((&$nautilus)[2]));
1446 my @xyzzy = &$nautilus;
1447 isnt_tainted($xyzzy[0]);
1448 is_tainted( $xyzzy[1]);
1449 isnt_tainted($xyzzy[2]);
1450 my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
1451 isnt_tainted(((&$red_october)[0]));
1452 is_tainted( ((&$red_october)[1]));
1453 isnt_tainted(((&$red_october)[2]));
1454 my @corge = &$red_october;
1455 isnt_tainted($corge[0]);
1456 is_tainted( $corge[1]);
1457 isnt_tainted($corge[2]);
1460 # Test for system/library calls returning string data of dubious origin.
1462 # No reliable %Config check for getpw*
1464 skip "getpwent() is not available", 9 unless
1465 eval { setpwent(); getpwent() };
1468 my @getpwent = getpwent();
1469 die "getpwent: $!\n" unless (@getpwent);
1470 isnt_tainted($getpwent[0]);
1471 is_tainted($getpwent[1]);
1472 isnt_tainted($getpwent[2]);
1473 isnt_tainted($getpwent[3]);
1474 isnt_tainted($getpwent[4]);
1475 isnt_tainted($getpwent[5]);
1476 is_tainted($getpwent[6], 'ge?cos');
1477 isnt_tainted($getpwent[7]);
1478 is_tainted($getpwent[8], 'shell');
1483 # pretty hard to imagine not
1484 skip "readdir() is not available", 1 unless $Config{d_readdir};
1486 opendir my $dh, "op" or die "opendir: $!\n";
1487 my $readdir = readdir $dh;
1488 is_tainted($readdir);
1493 skip "readlink() or symlink() is not available" unless
1494 $Config{d_readlink} && $Config{d_symlink};
1496 my $symlink = "sl$$";
1498 my $sl = "/something/naughty";
1499 # it has to be a real path on Mac OS
1500 symlink($sl, $symlink) or die "symlink: $!\n";
1501 my $readlink = readlink($symlink);
1502 is_tainted($readlink);
1507 # test bitwise ops (regression bug)
1517 # test target of substitution (regression bug)
1519 my $why = $TAINT."y";
1527 $why =~ s/e/'-'.$$/ge;
1533 skip "no IPC::SysV", 2 unless $ipcsysv;
1537 skip "shm*() not available", 1 unless $Config{d_shm};
1540 my $sent = "foobar";
1545 local $SIG{SYS} = sub { die "SIGSYS caught\n" };
1546 $id = shmget(IPC_PRIVATE, $size, S_IRWXU);
1548 } or do { chomp(my $msg = $@); skip "shmget: $msg", 1; };
1551 if (shmwrite($id, $sent, 0, 60)) {
1552 if (shmread($id, $rcvd, 0, 60)) {
1553 substr($rcvd, index($rcvd, "\0")) = '';
1555 warn "# shmread failed: $!\n";
1558 warn "# shmwrite failed: $!\n";
1560 shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n";
1562 warn "# shmget failed: $!\n";
1565 skip "SysV shared memory operation failed", 1 unless
1568 is_tainted($rcvd, "shmread");
1574 skip "msg*() not available", 1 unless $Config{d_msg};
1579 local $SIG{SYS} = sub { die "SIGSYS caught\n" };
1580 $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
1582 } or do { chomp(my $msg = $@); skip "msgget: $msg", 1; };
1584 my $sent = "message";
1585 my $type_sent = 1234;
1590 if (msgsnd($id, pack("l! a*", $type_sent, $sent), IPC_NOWAIT)) {
1591 if (msgrcv($id, $rcvd, 60, 0, IPC_NOWAIT)) {
1592 ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd);
1594 warn "# msgrcv failed: $!\n";
1597 warn "# msgsnd failed: $!\n";
1599 msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n";
1601 warn "# msgget failed\n";
1605 skip "SysV message queue operation failed", 1
1606 unless $rcvd eq $sent && $type_sent == $type_rcvd;
1608 is_tainted($rcvd, "msgrcv");
1614 # bug id 20001004.006 (#4380)
1616 open my $fh, '<', $TEST or warn "$0: cannot read $TEST: $!" ;
1627 # bug id 20001004.007 (#4381)
1629 open my $fh, '<', $TEST or warn "$0: cannot read $TEST: $!" ;
1635 isnt_tainted($c->{a});
1636 is_tainted($c->{b});
1641 is_tainted($d->{a});
1642 isnt_tainted($d->{b});
1646 b => { c => $a, d => 42 } };
1647 isnt_tainted($e->{a});
1648 isnt_tainted($e->{b});
1649 is_tainted($e->{b}->{c});
1650 isnt_tainted($e->{b}->{d});
1654 # bug id 20010519.003 (#7015)
1658 eval { require Fcntl; import Fcntl; };
1665 skip "no Fcntl", 36 unless $has_fcntl;
1667 my $foo = tempfile();
1668 my $evil = $foo . $TAINT;
1670 is(eval { sysopen(my $ro, $evil, &O_RDONLY) }, undef);
1673 violates_taint(sub { sysopen(my $wo, $evil, &O_WRONLY) }, 'sysopen');
1674 violates_taint(sub { sysopen(my $rw, $evil, &O_RDWR) }, 'sysopen');
1675 violates_taint(sub { sysopen(my $ap, $evil, &O_APPEND) }, 'sysopen');
1676 violates_taint(sub { sysopen(my $cr, $evil, &O_CREAT) }, 'sysopen');
1677 violates_taint(sub { sysopen(my $tr, $evil, &O_TRUNC) }, 'sysopen');
1679 is(eval { sysopen(my $ro, $foo, &O_RDONLY | $TAINT0) }, undef);
1682 violates_taint(sub { sysopen(my $wo, $foo, &O_WRONLY | $TAINT0) }, 'sysopen');
1683 violates_taint(sub { sysopen(my $rw, $foo, &O_RDWR | $TAINT0) }, 'sysopen');
1684 violates_taint(sub { sysopen(my $ap, $foo, &O_APPEND | $TAINT0) }, 'sysopen');
1685 violates_taint(sub { sysopen(my $cr, $foo, &O_CREAT | $TAINT0) }, 'sysopen');
1686 violates_taint(sub { sysopen(my $tr, $foo, &O_TRUNC | $TAINT0) }, 'sysopen');
1687 is(eval { sysopen(my $ro, $foo, &O_RDONLY, $TAINT0) }, undef);
1690 violates_taint(sub { sysopen(my $wo, $foo, &O_WRONLY, $TAINT0) }, 'sysopen');
1691 violates_taint(sub { sysopen(my $rw, $foo, &O_RDWR, $TAINT0) }, 'sysopen');
1692 violates_taint(sub { sysopen(my $ap, $foo, &O_APPEND, $TAINT0) }, 'sysopen');
1693 violates_taint(sub { sysopen(my $cr, $foo, &O_CREAT, $TAINT0) }, 'sysopen');
1694 violates_taint(sub { sysopen(my $tr, $foo, &O_TRUNC, $TAINT0) }, 'sysopen');
1699 # bug 20010526.004 (#7041)
1703 my $saw_warning = 0;
1704 local $SIG{__WARN__} = sub { ++$saw_warning };
1707 my $divnum = shift()/1;
1708 sprintf("%1.1f\n", $divnum);
1715 is($saw_warning, 0);
1720 # Bug ID 20010730.010 (#7387)
1724 sub Tie::TIESCALAR {
1728 bless \$arg => $class;
1739 my $bar = "The Big Bright Green Pleasure Machine";
1741 tie my ($foo), Tie => $bar;
1749 # Check that all environment variables are tainted.
1751 while (my ($k, $v) = each %ENV) {
1753 # These we have explicitly untainted or set earlier.
1754 $k !~ /^(BASH_ENV|CDPATH|ENV|IFS|PATH|PERL_CORE|TEMP|TERM|TMP)$/) {
1755 push @untainted, "# '$k' = '$v'\n";
1758 is("@untainted", "");
1762 is(${^TAINT}, 1, '$^TAINT is on');
1764 eval { ${^TAINT} = 0 };
1765 is(${^TAINT}, 1, '$^TAINT is not assignable');
1766 like($@, qr/^Modification of a read-only value attempted/,
1767 'Assigning to ${^TAINT} fails');
1770 # bug 20011111.105 (#7897)
1772 my $re1 = qr/x$TAINT/;
1775 my $re2 = qr/^$re1\z/;
1783 skip "system {} has different semantics on Win32", 1 if $Is_MSWin32;
1785 # bug 20010221.005 (#5882)
1786 local $ENV{PATH} .= $TAINT;
1787 eval { system { "echo" } "/arg0", "arg1" };
1788 like($@, qr/^Insecure \$ENV/);
1792 todo_skip 'tainted %ENV warning occludes tainted arguments warning', 22
1795 # bug 20020208.005 (#8465) plus some single arg exec/system extras
1796 violates_taint(sub { exec $TAINT, $TAINT }, 'exec');
1797 violates_taint(sub { exec $TAINT $TAINT }, 'exec');
1798 violates_taint(sub { exec $TAINT $TAINT, $TAINT }, 'exec');
1799 violates_taint(sub { exec $TAINT 'notaint' }, 'exec');
1800 violates_taint(sub { exec {'notaint'} $TAINT }, 'exec');
1802 violates_taint(sub { system $TAINT, $TAINT }, 'system');
1803 violates_taint(sub { system $TAINT $TAINT }, 'system');
1804 violates_taint(sub { system $TAINT $TAINT, $TAINT }, 'system');
1805 violates_taint(sub { system $TAINT 'notaint' }, 'system');
1806 violates_taint(sub { system {'notaint'} $TAINT }, 'system');
1810 system("lskdfj does not exist","with","args");
1816 exec("lskdfj does not exist","with","args");
1820 # If you add tests here update also the above skip block for VMS.
1824 # [ID 20020704.001 (#10026)] taint propagation failure
1827 is_tainted(my $foo = $1);
1831 # [perl #24291] this used to dump core
1832 our %nonmagicalenv = ( PATH => "util" );
1833 local *ENV = \%nonmagicalenv;
1834 eval { system("lskdfj"); };
1835 like($@, qr/^%ENV is aliased to another variable while running with -T switch/);
1836 local *ENV = *nonmagicalenv;
1837 eval { system("lskdfj"); };
1838 like($@, qr/^%ENV is aliased to %nonmagicalenv while running with -T switch/);
1845 isnt_tainted($notaint);
1848 $notaint =~ /($notaint)/;
1852 $notaint =~ /($TAINT)/;
1857 $TAINT =~ /($notaint)/;
1861 $TAINT =~ /($TAINT)/;
1867 ($r = $TAINT) =~ /($notaint)/;
1869 ($r = $TAINT) =~ /($TAINT)/;
1873 use re 'eval'; # this shouldn't make any difference
1874 ($r = $TAINT) =~ /($notaint)/;
1876 ($r = $TAINT) =~ /($TAINT)/;
1881 # accessing $^O shoudn't taint it as a side-effect;
1882 # assigning tainted data to it is now an error
1885 if (!$^X) { } elsif ($^O eq 'bar') { }
1887 local $^O; # We're going to clobber something test infrastructure depends on.
1889 like($@, qr/Insecure dependency in/);
1892 EFFECTIVELY_CONSTANTS: {
1893 my $tainted_number = 12 + $TAINT0;
1894 is_tainted( $tainted_number );
1896 # Even though it's always 0, it's still tainted
1897 my $tainted_product = $tainted_number * 0;
1898 is_tainted( $tainted_product );
1899 is($tainted_product, 0);
1902 TERNARY_CONDITIONALS: {
1903 my $tainted_true = $TAINT . "blah blah blah";
1904 my $tainted_false = $TAINT0;
1905 is_tainted( $tainted_true );
1906 is_tainted( $tainted_false );
1908 my $result = $tainted_true ? "True" : "False";
1909 is($result, "True");
1910 isnt_tainted( $result );
1912 $result = $tainted_false ? "True" : "False";
1913 is($result, "False");
1914 isnt_tainted( $result );
1916 my $untainted_whatever = "The Fabulous Johnny Cash";
1917 my $tainted_whatever = "Soft Cell" . $TAINT;
1919 $result = $tainted_true ? $tainted_whatever : $untainted_whatever;
1920 is($result, "Soft Cell");
1921 is_tainted( $result );
1923 $result = $tainted_false ? $tainted_whatever : $untainted_whatever;
1924 is($result, "The Fabulous Johnny Cash");
1925 isnt_tainted( $result );
1929 # rt.perl.org 5900 $1 remains tainted if...
1930 # 1) The regular expression contains a scalar variable AND
1931 # 2) The regular expression appears in an elsif clause
1933 my $foo = "abcdefghi" . $TAINT;
1935 my $valid_chars = 'a-z';
1938 elsif ( $foo =~ /([$valid_chars]+)/o ) {
1945 elsif ( my @bar = $foo =~ /([$valid_chars]+)/o ) {
1946 isnt_tainted($bar[0]);
1951 # at scope exit, a restored localised value should have its old
1952 # taint status, not the taint status of the current statement
1961 my $c = do { local $x99; $^X };
1968 my $c = do { local $x99; '' };
1972 # an mg_get of a tainted value during localization shouldn't taint the
1976 eval { local $0, eval '1' };
1980 # [perl #8262] //g loops infinitely on tainted data
1986 cmp_ok pos($a[0]), '>', 0, "infinite m//g on arrays (aelemfast)";
1991 cmp_ok pos($a[$i]), '>', 0, "infinite m//g on arrays (aelem)";
1996 cmp_ok pos($h{a}), '>', 0, "infinite m//g on hashes (helem)";
2002 eval 'use Scalar::Util "dualvar"; $got_dualvar++';
2003 skip "No Scalar::Util::dualvar" unless $got_dualvar;
2004 my $a = Scalar::Util::dualvar(3, $^X);
2006 is ($b, 8, "Arithmetic on tainted dualvars works");
2009 # opening '|-' should not trigger $ENV{PATH} check
2013 skip "fork() is not available", 3 unless $Config{'d_fork'};
2014 skip "opening |- is not stable on threaded Open/MirBSD with taint", 3
2015 if $Config{useithreads} and $Is_OpenBSD || $Is_MirBSD;
2017 $ENV{'PATH'} = $TAINT;
2018 local $SIG{'PIPE'} = 'IGNORE';
2020 my $pid = open my $pipe, '|-';
2021 if (!defined $pid) {
2022 die "open failed: $!";
2025 kill 'KILL', $$; # child suicide
2029 unlike($@, qr/Insecure \$ENV/, 'fork triggers %ENV check');
2030 is($@, '', 'pipe/fork/open/close failed');
2032 open my $pipe, "|$Invoke_Perl -e 1";
2035 like($@, qr/Insecure \$ENV/, 'popen neglects %ENV check');
2040 package AUTOLOAD_TAINT;
2043 return if $AUTOLOAD =~ /DESTROY/;
2044 if ($AUTOLOAD =~ /untainted/) {
2045 main::isnt_tainted($AUTOLOAD, '$AUTOLOAD can be untainted');
2046 my $copy = $AUTOLOAD;
2047 main::isnt_tainted($copy, '$AUTOLOAD can be untainted');
2049 main::is_tainted($AUTOLOAD, '$AUTOLOAD can be tainted');
2050 my $copy = $AUTOLOAD;
2051 main::is_tainted($copy, '$AUTOLOAD can be tainted');
2056 my $o = bless [], 'AUTOLOAD_TAINT';
2063 # tests for tainted format in s?printf
2064 my $fmt = $TAINT . "# %s\n";
2065 violates_taint(sub { printf($fmt, "foo") }, 'printf',
2066 q/printf doesn't like tainted formats/);
2067 violates_taint(sub { printf($TAINT . "# %s\n", "foo") }, 'printf',
2068 q/printf doesn't like tainted format expressions/);
2069 eval { printf("# %s\n", $TAINT . "foo") };
2070 is($@, '', q/printf accepts other tainted args/);
2071 violates_taint(sub { sprintf($fmt, "foo") }, 'sprintf',
2072 q/sprintf doesn't like tainted formats/);
2073 violates_taint(sub { sprintf($TAINT . "# %s\n", "foo") }, 'sprintf',
2074 q/sprintf doesn't like tainted format expressions/);
2075 eval { sprintf("# %s\n", $TAINT . "foo") };
2076 is($@, '', q/sprintf accepts other tainted args/);
2085 is ($val, '7000000000', 'Assignment to untainted variable');
2088 is ($val, '7000000000', 'Assignment to tainted variable');
2093 my $tainted = '1' . $TAINT;
2094 eval '$val = eval $tainted;';
2095 is ($val, 0, "eval doesn't like tainted strings");
2096 like ($@, qr/^Insecure dependency in eval/);
2098 # Rather nice code to get a tainted undef by from Rick Delaney
2099 open my $fh, "test.pl" or die $!;
2100 seek $fh, 0, 2 or die $!;
2103 eval 'eval $tainted';
2104 like ($@, qr/^Insecure dependency in eval/);
2107 foreach my $ord (78, 163, 256) {
2109 my $line = 'A1' . $TAINT . chr $ord;
2113 isnt_tainted($1, "\\S match with chr $ord");
2118 skip 'No crypt function, skipping crypt tests', 4 if(!$Config{d_crypt});
2121 # On platforms implementing FIPS mode, using a weak algorithm
2122 # (including the default triple-DES algorithm) causes crypt(3) to
2123 # return a null pointer, which Perl converts into undef. We assume
2124 # for now that all such platforms support glibc-style selection of
2125 # a different hashing algorithm.
2126 # glibc supports MD5, but OpenBSD only supports Blowfish.
2127 my $alg = ''; # Use default algorithm
2128 if ( !defined(crypt("ab", $alg."cd")) ) {
2129 $alg = '$5$'; # Try SHA-256
2131 if ( !defined(crypt("ab", $alg."cd")) ) {
2132 $alg = '$2b$12$FPWWO2RJ3CK4FINTw0Hi'; # Try Blowfish
2134 if ( !defined(crypt("ab", $alg."cd")) ) {
2135 $alg = ''; # Nothing worked. Back to default
2137 my $x = crypt($_[0], $alg . $_[1]);
2140 sub co { my $x = ~$_[0]; $x }
2142 $a = cr('hello', 'foo' . $TAINT);
2143 $b = cr('hello', 'foo');
2144 is_tainted($a, "tainted crypt");
2145 isnt_tainted($b, "untainted crypt");
2146 $a = co('foo' . $TAINT);
2148 is_tainted($a, "tainted complement");
2149 isnt_tainted($b, "untainted complement");
2154 my @data = qw(bonk zam zlonk qunckkk);
2155 # Clearly some sort of usenet bang-path
2156 my $string = $TAINT . join "!", @data;
2158 is_tainted($string, "tainted data");
2160 my @got = split /!|,/, $string;
2162 # each @got would be useful here, but I want the test for earlier perls
2163 for my $i (0 .. $#data) {
2164 is_tainted($got[$i], "tainted result $i");
2165 is($got[$i], $data[$i], "correct content $i");
2168 is_tainted($string, "still tainted data");
2170 my @got = split /[!,]/, $string;
2172 # each @got would be useful here, but I want the test for earlier perls
2173 for my $i (0 .. $#data) {
2174 is_tainted($got[$i], "tainted result $i");
2175 is($got[$i], $data[$i], "correct content $i");
2178 is_tainted($string, "still tainted data");
2180 my @got = split /!/, $string;
2182 # each @got would be useful here, but I want the test for earlier perls
2183 for my $i (0 .. $#data) {
2184 is_tainted($got[$i], "tainted result $i");
2185 is($got[$i], $data[$i], "correct content $i");
2189 # Bug RT #52552 - broken by change at git commit id f337b08
2191 my $x = $TAINT. q{print "Hello world\n"};
2192 my $y = pack "a*", $x;
2193 is_tainted($y, "pack a* preserves tainting");
2195 my $z = pack "A*", q{print "Hello world\n"}.$TAINT;
2196 is_tainted($z, "pack A* preserves tainting");
2198 my $zz = pack "a*a*", q{print "Hello world\n"}, $TAINT;
2199 is_tainted($zz, "pack a*a* preserves tainting");
2202 # Bug RT #61976 tainted $! would show numeric rather than string value
2205 my $tainted_path = substr($^X,0,0) . "/no/such/file";
2207 # $! is used in a tainted expression, so gets tainted
2208 open my $fh, $tainted_path or $err= "$!";
2209 unlike($err, qr/^\d+$/, 'tainted $!');
2213 # #6758: tainted values become untainted in tied hashes
2214 # (also applies to other value magic such as pos)
2219 sub TIEHASH { bless {} }
2220 sub TIEARRAY { bless {} }
2225 main::is_tainted($_[1], "tied arg1 tainted");
2226 main::is_tainted($_[2], "tied arg2 tainted");
2232 my ($k,$v) = qw(1111 val);
2234 tie my @array, 'P6758';
2235 tie my %hash , 'P6758';
2238 ok $i == 2, "tied STORE called correct number of times";
2241 # Bug RT #45167 the return value of sprintf sometimes wasn't tainted
2242 # when the args were tainted. This only occurred on the first use of
2243 # sprintf; after that, its TARG has taint magic attached, so setmagic
2244 # at the end works. That's why there are multiple sprintf's below, rather
2245 # than just one wrapped in an inner loop. Also, any plaintext between
2246 # format entries would correctly cause tainting to get set. so test with
2247 # "%s%s" rather than eg "%s %s".
2250 for my $var1 ($TAINT, "123") {
2251 for my $var2 ($TAINT0, "456") {
2252 is( tainted(sprintf '%s', $var1, $var2), tainted($var1),
2253 "sprintf '%s', '$var1', '$var2'" );
2254 is( tainted(sprintf ' %s', $var1, $var2), tainted($var1),
2255 "sprintf ' %s', '$var1', '$var2'" );
2256 is( tainted(sprintf '%s%s', $var1, $var2),
2257 tainted($var1) || tainted($var2),
2258 "sprintf '%s%s', '$var1', '$var2'" );
2264 # Bug RT #67962: old tainted $1 gets treated as tainted
2265 # in next untainted # match
2269 "abc".$TAINT =~ /(.*)/; # make $1 tainted
2270 is_tainted($1, '$1 should be tainted');
2272 my $untainted = "abcdef";
2273 isnt_tainted($untainted, '$untainted should be untainted');
2274 $untainted =~ s/(abc)/$1/;
2275 isnt_tainted($untainted, '$untainted should still be untainted');
2276 $untainted =~ s/(abc)/x$1/;
2277 isnt_tainted($untainted, '$untainted should yet still be untainted');
2281 # On Windows we can't spawn a fresh Perl interpreter unless at
2282 # least the Windows system directory (usually C:\Windows\System32)
2283 # is still on the PATH. There is however no way to determine the
2284 # actual path on the current system without loading the Win32
2285 # module, so we just restore the original $ENV{PATH} here.
2286 local $ENV{PATH} = $ENV{PATH};
2287 $ENV{PATH} = $old_env_path if $Is_MSWin32;
2289 fresh_perl_is(<<'end', "ok", { switches => [ '-T' ] },
2290 $TAINT = substr($^X, 0, 0);
2291 formline('@'.('<'x("2000".$TAINT)).' | @*', 'hallo', 'welt');
2294 "formline survives a tainted dynamic picture");
2298 isnt_tainted($^A, "format accumulator not tainted yet");
2299 formline('@ | @*', 'hallo' . $TAINT, 'welt');
2300 is_tainted($^A, "tainted formline argument makes a tainted accumulator");
2302 isnt_tainted($^A, "accumulator can be explicitly untainted");
2303 formline('@' .('<'*5) . ' | @*', 'hallo', 'welt');
2304 isnt_tainted($^A, "accumulator still untainted");
2306 is_tainted($^A, "accumulator can be explicitly tainted");
2307 formline('@' .('<'*5) . ' | @*', 'hallo', 'welt');
2308 is_tainted($^A, "accumulator still tainted");
2310 isnt_tainted($^A, "accumulator untainted again");
2311 formline('@' .('<'*5) . ' | @*', 'hallo', 'welt');
2312 isnt_tainted($^A, "accumulator still untainted");
2313 formline('@' .('<'*(5+$TAINT0)) . ' | @*', 'hallo', 'welt');
2314 is_tainted($^A, "the accumulator should be tainted already");
2315 is_tainted($^A, "tainted formline picture makes a tainted accumulator");
2319 "Constant(1)" =~ / ^ ([a-z_]\w*) (?: [(] (.*) [)] )? $ /xi;
2322 isnt_tainted($a, "regex optimization of single char /[]/i doesn't taint");
2323 isnt_tainted($b, "regex optimization of single char /[]/i doesn't taint");
2327 # RT 81230: tainted value during FETCH created extra ref to tied obj
2335 my $x = $^X; # tainted
2338 sub FETCH { my $x = $_[0]; $$x . "" }
2343 local $SIG{__WARN__} = sub { $w .= "@_" };
2345 untie %h if $h{"k"};
2347 ::is($w, "", "RT 81230");
2351 # Compiling a subroutine inside a tainted expression does not make the
2352 # constant folded values tainted.
2353 my $x = sub { "x" . "y" };
2354 my $y = $ENV{PATH} . $x->(); # Compile $x inside a tainted expression
2356 isnt_tainted($z, "Constants folded value not tainted");
2360 # now that regexes are first class SVs, make sure that they themselves
2361 # as well as references to them are tainted
2363 my $rr = qr/(.)$TAINT/;
2364 my $r = $$rr; # bare REGEX
2366 ok($s =~ s/$r/x/, "match bare regex");
2367 is_tainted($s, "match bare regex taint");
2368 is($s, 'xbc', "match bare regex taint value");
2372 # [perl #82616] security Issues with user-defined \p{} properties
2373 # A using a tainted user-defined property should croak
2375 sub IsA { sprintf "%02x", ord("A") }
2378 ok("A" =~ /\p{$prop}/, "user-defined property: non-tainted case");
2379 $prop = "IsA$TAINT";
2380 eval { "A" =~ /\p{$prop}/};
2381 like($@, qr/Insecure user-defined property \\p\{main::IsA\}/,
2382 "user-defined property: tainted case");
2386 # [perl #87336] lc/uc(first) failing to taint the returned string
2387 my $source = "foo$TAINT";
2388 my $dest = lc $source;
2389 is_tainted $dest, "lc(tainted) taints its return value";
2390 $dest = lcfirst $source;
2391 is_tainted $dest, "lcfirst(tainted) taints its return value";
2393 is_tainted $dest, "uc(tainted) taints its return value";
2394 $dest = ucfirst $source;
2395 is_tainted $dest, "ucfirst(tainted) taints its return value";
2399 # Taintedness of values returned from given()
2400 use feature 'switch';
2401 no warnings 'experimental::smartmatch';
2403 my @descriptions = ('when', 'given end', 'default');
2406 my $letter = "$_$TAINT";
2408 my $desc = "tainted value returned from " . shift(@descriptions);
2412 when ('x') { $letter }
2413 when ('y') { goto leavegiven }
2418 is $res, $letter, "$desc is correct";
2419 is_tainted $res, "$desc stays tainted";
2424 # tainted constants and index()
2425 # RT 64804; http://bugs.debian.org/291450
2427 ok(tainted $old_env_path, "initial taintedness");
2428 BEGIN { no strict 'refs'; my $v = $old_env_path; *{"::C"} = sub () { $v }; }
2429 ok(tainted C, "constant is tainted properly");
2430 ok(!tainted "", "tainting not broken yet");
2432 ok(!tainted "", "tainting still works after index() of the constant");
2435 # Tainted values with smartmatch
2436 # [perl #93590] S_do_smartmatch stealing its own string buffers
2438 no warnings 'experimental::smartmatch';
2439 ok "M$TAINT" ~~ ['m', 'M'], '$tainted ~~ ["whatever", "match"]';
2440 ok !("M$TAINT" ~~ ['m', undef]), '$tainted ~~ ["whatever", undef]';
2443 # Tainted values and ref()
2445 my $x = bless \"M$TAINT", ref(bless[], "main");
2447 pass("no death when TARG of ref is tainted");
2449 # $$ should not be tainted by being read in a tainted expression.
2451 isnt_tainted $$, "PID not tainted initially";
2452 my $x = $ENV{PATH}.$$;
2453 isnt_tainted $$, "PID not tainted when read in tainted expression";
2457 skip 'Locales not available', 4 unless locales_enabled('LC_CTYPE');
2461 my ($latin1, $utf8) = ("\xDF") x 2;
2462 utf8::downgrade($latin1);
2463 utf8::upgrade($utf8);
2465 is_tainted fc($latin1), "under locale, lc(latin1) taints the result";
2466 is_tainted fc($utf8), "under locale, lc(utf8) taints the result";
2468 is_tainted "\F$latin1", "under locale, \\Flatin1 taints the result";
2469 is_tainted "\F$utf8", "under locale, \\Futf8 taints the result";
2474 eval { die "Test\n".substr($ENV{PATH}, 0, 0); };
2477 like($@, qr/^Test\n\t\.\.\.propagated at /, "error should be propagated");
2480 # tainted run-time (?{}) should die
2483 my $code = '(?{})' . $TAINT;
2485 eval { "a" =~ /$code/ };
2486 like($@, qr/Eval-group in insecure regular expression/, "tainted (?{})");
2489 # reset() and tainted undef (?!)
2491 $_ = "$TAINT".reset "x";
2492 is eval { eval $::x.1 }, 1, 'reset does not taint undef';
2496 # See the comment above the first formline test.
2497 local $ENV{PATH} = $ENV{PATH};
2498 $ENV{PATH} = $old_env_path if $Is_MSWin32;
2500 switches => [ '-T' ],
2501 prog => 'use constant K=>$^X; 0 if K; BEGIN{} use strict; '
2502 .'print 122669, qq-\n-',
2505 'tainted constant as logop condition should not prevent "use"';
2508 # optimised SETi etc need to handle tainting
2511 my ($i1, $i2, $i3) = (1, 1, 1);
2512 my ($n1, $n2, $n3) = (1.1, 1.1, 1.1);
2513 my $tn = $TAINT0 + 1.1;
2516 is_tainted $i1, "+ SETi";
2518 is_tainted $i2, "- SETi";
2520 is_tainted $i3, "* SETi";
2523 is_tainted $n1, "+ SETn";
2525 is_tainted $n2, "- SETn";
2527 is_tainted $n3, "* SETn";
2530 # check that localizing something with get magic (e.g. taint) doesn't
2531 # upgrade pIOK to IOK
2534 local our $x = 1.1 + $TAINT0; # $x should be NOK
2535 my $ix = int($x); # now NOK, pIOK
2540 isnt($x, 1); # it should be 1.1, not 1
2544 # every item in a list assignment is independent, even if the lvalue
2545 # has taint magic already
2547 my ($a, $b, $c, $d);
2550 ($a, $b, $c) = ($TAINT, 0, 0);
2551 is_tainted $a, "list assign tainted a";
2552 isnt_tainted $b, "list assign tainted b";
2553 isnt_tainted $c, "list assign tainted c";
2557 ($a, $b, $c) = ($TAINT, 0, 0);
2558 is_tainted $a, "list assign detainted a";
2559 isnt_tainted $b, "list assign detainted b";
2560 isnt_tainted $c, "list assign detainted c";
2564 ($a, $b, $c) = ($TAINT);
2565 is_tainted $a, "list assign empty rhs a";
2566 isnt_tainted $b, "list assign empty rhs b";
2567 isnt_tainted $c, "list assign empty rhs c";
2571 ($a = ($TAINT. "x")), (($b, $c) = (0));
2572 is_tainted $a, "list assign already tainted expression a";
2573 isnt_tainted $b, "list assign already tainted expression b";
2574 isnt_tainted $c, "list assign already tainted expression c";
2578 (($a) = ($TAINT. "x")), ($b = $b . "x");
2579 is_tainted $a, "list assign post tainted expression a";
2580 isnt_tainted $b, "list assign post tainted expression b";
2583 # Module::Runtime was temporarily broken between 5.27.0 and 5.27.1 because
2584 # ref() would fail an assertion in a tainted statement. (No ok() neces-
2585 # sary since it aborts when it fails.)
2586 () = defined $^X && ref \$^X;
2588 # taint passing through overloading
2590 sub new { bless({ t => $_[1] }, $_[0]) }
2591 use overload '""' => sub { $_[0]->{t} ? "hi".$TAINT : "hello" };
2593 my $ovclean = OvTaint->new(0);
2594 my $ovtaint = OvTaint->new(1);
2595 isnt_tainted("$ovclean", "overload preserves cleanliness");
2596 is_tainted("$ovtaint", "overload preserves taint");
2598 # substitutions with overloaded replacement
2600 my ($desc, $s, $res, $one);
2602 $desc = "substitution with partial replacement overloaded and clean";
2604 $res = $s =~ s/(.+)/xyz$ovclean/;
2606 isnt_tainted($s, "$desc: s not tainted");
2607 isnt_tainted($res, "$desc: res not tainted");
2608 isnt_tainted($one, "$desc: \$1 not tainted");
2609 is($s, 'xyzhello', "$desc: s value");
2610 is($res, 1, "$desc: res value");
2611 is($one, 'abcd', "$desc: \$1 value");
2613 $desc = "substitution with partial replacement overloaded and tainted";
2615 $res = $s =~ s/(.+)/xyz$ovtaint/;
2617 is_tainted($s, "$desc: s tainted");
2618 isnt_tainted($res, "$desc: res not tainted");
2619 isnt_tainted($one, "$desc: \$1 not tainted");
2620 is($s, 'xyzhi', "$desc: s value");
2621 is($res, 1, "$desc: res value");
2622 is($one, 'abcd', "$desc: \$1 value");
2624 $desc = "substitution with whole replacement overloaded and clean";
2626 $res = $s =~ s/(.+)/$ovclean/;
2628 isnt_tainted($s, "$desc: s not tainted");
2629 isnt_tainted($res, "$desc: res not tainted");
2630 isnt_tainted($one, "$desc: \$1 not tainted");
2631 is($s, 'hello', "$desc: s value");
2632 is($res, 1, "$desc: res value");
2633 is($one, 'abcd', "$desc: \$1 value");
2635 $desc = "substitution with whole replacement overloaded and tainted";
2637 $res = $s =~ s/(.+)/$ovtaint/;
2639 is_tainted($s, "$desc: s tainted");
2640 isnt_tainted($res, "$desc: res not tainted");
2641 isnt_tainted($one, "$desc: \$1 not tainted");
2642 is($s, 'hi', "$desc: s value");
2643 is($res, 1, "$desc: res value");
2644 is($one, 'abcd', "$desc: \$1 value");
2646 $desc = "substitution /e with partial replacement overloaded and clean";
2648 $res = $s =~ s/(.+)/"xyz".$ovclean/e;
2650 isnt_tainted($s, "$desc: s not tainted");
2651 isnt_tainted($res, "$desc: res not tainted");
2652 isnt_tainted($one, "$desc: \$1 not tainted");
2653 is($s, 'xyzhello', "$desc: s value");
2654 is($res, 1, "$desc: res value");
2655 is($one, 'abcd', "$desc: \$1 value");
2657 $desc = "substitution /e with partial replacement overloaded and tainted";
2659 $res = $s =~ s/(.+)/"xyz".$ovtaint/e;
2661 is_tainted($s, "$desc: s tainted");
2662 isnt_tainted($res, "$desc: res not tainted");
2663 isnt_tainted($one, "$desc: \$1 not tainted");
2664 is($s, 'xyzhi', "$desc: s value");
2665 is($res, 1, "$desc: res value");
2666 is($one, 'abcd', "$desc: \$1 value");
2668 $desc = "substitution /e with whole replacement overloaded and clean";
2670 $res = $s =~ s/(.+)/$ovclean/e;
2672 isnt_tainted($s, "$desc: s not tainted");
2673 isnt_tainted($res, "$desc: res not tainted");
2674 isnt_tainted($one, "$desc: \$1 not tainted");
2675 is($s, 'hello', "$desc: s value");
2676 is($res, 1, "$desc: res value");
2677 is($one, 'abcd', "$desc: \$1 value");
2679 $desc = "substitution /e with whole replacement overloaded and tainted";
2681 $res = $s =~ s/(.+)/$ovtaint/e;
2683 is_tainted($s, "$desc: s tainted");
2684 isnt_tainted($res, "$desc: res not tainted");
2685 isnt_tainted($one, "$desc: \$1 not tainted");
2686 is($s, 'hi', "$desc: s value");
2687 is($res, 1, "$desc: res value");
2688 is($one, 'abcd', "$desc: \$1 value");
2690 $desc = "substitution /e with extra code and partial replacement overloaded and clean";
2692 $res = $s =~ s/(.+)/(my $z++), "xyz".$ovclean/e;
2694 isnt_tainted($s, "$desc: s not tainted");
2695 isnt_tainted($res, "$desc: res not tainted");
2696 isnt_tainted($one, "$desc: \$1 not tainted");
2697 is($s, 'xyzhello', "$desc: s value");
2698 is($res, 1, "$desc: res value");
2699 is($one, 'abcd', "$desc: \$1 value");
2701 $desc = "substitution /e with extra code and partial replacement overloaded and tainted";
2703 $res = $s =~ s/(.+)/(my $z++), "xyz".$ovtaint/e;
2705 is_tainted($s, "$desc: s tainted");
2706 isnt_tainted($res, "$desc: res not tainted");
2707 isnt_tainted($one, "$desc: \$1 not tainted");
2708 is($s, 'xyzhi', "$desc: s value");
2709 is($res, 1, "$desc: res value");
2710 is($one, 'abcd', "$desc: \$1 value");
2712 $desc = "substitution /e with extra code and whole replacement overloaded and clean";
2714 $res = $s =~ s/(.+)/(my $z++), $ovclean/e;
2716 isnt_tainted($s, "$desc: s not tainted");
2717 isnt_tainted($res, "$desc: res not tainted");
2718 isnt_tainted($one, "$desc: \$1 not tainted");
2719 is($s, 'hello', "$desc: s value");
2720 is($res, 1, "$desc: res value");
2721 is($one, 'abcd', "$desc: \$1 value");
2723 $desc = "substitution /e with extra code and whole replacement overloaded and tainted";
2725 $res = $s =~ s/(.+)/(my $z++), $ovtaint/e;
2727 is_tainted($s, "$desc: s tainted");
2728 isnt_tainted($res, "$desc: res not tainted");
2729 isnt_tainted($one, "$desc: \$1 not tainted");
2730 is($s, 'hi', "$desc: s value");
2731 is($res, 1, "$desc: res value");
2732 is($one, 'abcd', "$desc: \$1 value");
2734 $desc = "substitution /r with partial replacement overloaded and clean";
2736 $res = $s =~ s/(.+)/xyz$ovclean/r;
2738 isnt_tainted($s, "$desc: s not tainted");
2739 isnt_tainted($res, "$desc: res not tainted");
2740 isnt_tainted($one, "$desc: \$1 not tainted");
2741 is($s, 'abcd', "$desc: s value");
2742 is($res, 'xyzhello', "$desc: res value");
2743 is($one, 'abcd', "$desc: \$1 value");
2745 $desc = "substitution /r with partial replacement overloaded and tainted";
2747 $res = $s =~ s/(.+)/xyz$ovtaint/r;
2749 isnt_tainted($s, "$desc: s not tainted");
2750 is_tainted($res, "$desc: res tainted");
2751 isnt_tainted($one, "$desc: \$1 not tainted");
2752 is($s, 'abcd', "$desc: s value");
2753 is($res, 'xyzhi', "$desc: res value");
2754 is($one, 'abcd', "$desc: \$1 value");
2756 $desc = "substitution /r with whole replacement overloaded and clean";
2758 $res = $s =~ s/(.+)/$ovclean/r;
2760 isnt_tainted($s, "$desc: s not tainted");
2761 isnt_tainted($res, "$desc: res not tainted");
2762 isnt_tainted($one, "$desc: \$1 not tainted");
2763 is($s, 'abcd', "$desc: s value");
2764 is($res, 'hello', "$desc: res value");
2765 is($one, 'abcd', "$desc: \$1 value");
2767 $desc = "substitution /r with whole replacement overloaded and tainted";
2769 $res = $s =~ s/(.+)/$ovtaint/r;
2771 isnt_tainted($s, "$desc: s not tainted");
2772 is_tainted($res, "$desc: res tainted");
2773 isnt_tainted($one, "$desc: \$1 not tainted");
2774 is($s, 'abcd', "$desc: s value");
2775 is($res, 'hi', "$desc: res value");
2776 is($one, 'abcd', "$desc: \$1 value");
2778 $desc = "substitution /g with partial replacement overloaded and clean";
2780 $res = $s =~ s/(.)/x$ovclean/g;
2782 isnt_tainted($s, "$desc: s not tainted");
2783 isnt_tainted($res, "$desc: res not tainted");
2784 isnt_tainted($one, "$desc: \$1 not tainted");
2785 is($s, 'xhello' x 4, "$desc: s value");
2786 is($res, 4, "$desc: res value");
2787 is($one, 'd', "$desc: \$1 value");
2789 $desc = "substitution /g with partial replacement overloaded and tainted";
2791 $res = $s =~ s/(.)/x$ovtaint/g;
2793 is_tainted($s, "$desc: s tainted");
2794 isnt_tainted($res, "$desc: res not tainted");
2795 isnt_tainted($one, "$desc: \$1 not tainted");
2796 is($s, 'xhi' x 4, "$desc: s value");
2797 is($res, 4, "$desc: res value");
2798 is($one, 'd', "$desc: \$1 value");
2800 $desc = "substitution /g with whole replacement overloaded and clean";
2802 $res = $s =~ s/(.)/$ovclean/g;
2804 isnt_tainted($s, "$desc: s not tainted");
2805 isnt_tainted($res, "$desc: res not tainted");
2806 isnt_tainted($one, "$desc: \$1 not tainted");
2807 is($s, 'hello' x 4, "$desc: s value");
2808 is($res, 4, "$desc: res value");
2809 is($one, 'd', "$desc: \$1 value");
2811 $desc = "substitution /g with whole replacement overloaded and tainted";
2813 $res = $s =~ s/(.)/$ovtaint/g;
2815 is_tainted($s, "$desc: s tainted");
2816 isnt_tainted($res, "$desc: res not tainted");
2817 isnt_tainted($one, "$desc: \$1 not tainted");
2818 is($s, 'hi' x 4, "$desc: s value");
2819 is($res, 4, "$desc: res value");
2820 is($one, 'd', "$desc: \$1 value");
2822 $desc = "substitution /ge with partial replacement overloaded and clean";
2824 $res = $s =~ s/(.)/"x".$ovclean/ge;
2826 isnt_tainted($s, "$desc: s not tainted");
2827 isnt_tainted($res, "$desc: res not tainted");
2828 isnt_tainted($one, "$desc: \$1 not tainted");
2829 is($s, 'xhello' x 4, "$desc: s value");
2830 is($res, 4, "$desc: res value");
2831 is($one, 'd', "$desc: \$1 value");
2833 $desc = "substitution /ge with partial replacement overloaded and tainted";
2835 $res = $s =~ s/(.)/"x".$ovtaint/ge;
2837 is_tainted($s, "$desc: s tainted");
2838 isnt_tainted($res, "$desc: res not tainted");
2839 isnt_tainted($one, "$desc: \$1 not tainted");
2840 is($s, 'xhi' x 4, "$desc: s value");
2841 is($res, 4, "$desc: res value");
2842 is($one, 'd', "$desc: \$1 value");
2844 $desc = "substitution /ge with whole replacement overloaded and clean";
2846 $res = $s =~ s/(.)/$ovclean/ge;
2848 isnt_tainted($s, "$desc: s not tainted");
2849 isnt_tainted($res, "$desc: res not tainted");
2850 isnt_tainted($one, "$desc: \$1 not tainted");
2851 is($s, 'hello' x 4, "$desc: s value");
2852 is($res, 4, "$desc: res value");
2853 is($one, 'd', "$desc: \$1 value");
2855 $desc = "substitution /ge with whole replacement overloaded and tainted";
2857 $res = $s =~ s/(.)/$ovtaint/ge;
2859 is_tainted($s, "$desc: s tainted");
2860 isnt_tainted($res, "$desc: res not tainted");
2861 isnt_tainted($one, "$desc: \$1 not tainted");
2862 is($s, 'hi' x 4, "$desc: s value");
2863 is($res, 4, "$desc: res value");
2864 is($one, 'd', "$desc: \$1 value");
2868 # It was trying to taint a boolean return from s/// (e.g. PL_sv_yes)
2869 # and was thus crashing with 'Modification of a read-only value'.
2872 my $s = "abcd" . $TAINT;
2873 ok(!!($s =~ s/a/x/g), "RT #132385");
2876 # This may bomb out with the alarm signal so keep it last
2878 skip "No alarm()" unless $Config{d_alarm};
2879 # Test from RT #41831]
2880 # [PATCH] Bug & fix: hang when using study + taint mode (perl 5.6.1, 5.8.x)
2882 my $DATA = <<'END' . $TAINT;
2892 ## don't set $SIG{ALRM}, since we'd never get to a user-level handler as
2893 ## perl is stuck in a regexp infinite loop!
2897 if ($DATA =~ /^line2.*line4/m) {
2898 fail("Should not be a match")
2900 pass("Match on tainted multiline data should fail promptly");
2906 # Keep the previous test last