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