This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Test case for C<undef %File::Glob::>
[perl5.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
9d116dd7
JH
18# We do not want the whole taint.t to fail
19# just because Errno possibly failing.
20eval { require Errno; import Errno };
21
c9f931b8
JH
22use vars qw($ipcsysv); # did we manage to load IPC::SysV?
23
3eeba6fb
CB
24BEGIN {
25 if ($^O eq 'VMS' && !defined($Config{d_setenv})) {
26 $ENV{PATH} = $ENV{PATH};
27 $ENV{TERM} = $ENV{TERM} ne ''? $ENV{TERM} : 'dummy';
28 }
be3174d2
GS
29 if ($Config{'extensions'} =~ /\bIPC\/SysV\b/
30 && ($Config{d_shm} || $Config{d_msg})) {
c9f931b8
JH
31 eval { require IPC::SysV };
32 unless ($@) {
33 $ipcsysv++;
34 IPC::SysV->import(qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU));
35 }
b9d1c439 36 }
3eeba6fb
CB
37}
38
1e422769 39my $Is_VMS = $^O eq 'VMS';
68dc0745 40my $Is_MSWin32 = $^O eq 'MSWin32';
39e571d4 41my $Is_Dos = $^O eq 'dos';
68dc0745 42my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' :
43 $Is_MSWin32 ? '.\perl' : './perl';
c90c0ff4 44my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
7bac28a0 45
1e422769 46if ($Is_VMS) {
7bac28a0 47 my (%old, $x);
48 for $x ('DCL$PATH', @MoreEnv) {
49 ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
50 }
1e422769 51 eval <<EndOfCleanup;
52 END {
3eeba6fb 53 \$ENV{PATH} = '' if $Config{d_setenv};
1e422769 54 warn "# Note: logical name 'PATH' may have been deleted\n";
562a7b0c 55 \@ENV{keys %old} = values %old;
1e422769 56 }
57EndOfCleanup
58}
59
60# Sources of taint:
61# The empty tainted value, for tainting strings
62my $TAINT = substr($^X, 0, 0);
63# A tainted zero, useful for tainting numbers
64my $TAINT0 = 0 + $TAINT;
65
66# This taints each argument passed. All must be lvalues.
67# Side effect: It also stringifies them. :-(
68sub taint_these (@) {
69 for (@_) { $_ .= $TAINT }
70}
71
72# How to identify taint when you see it
73sub any_tainted (@) {
74 not eval { join("",@_), kill 0; 1 };
75}
76sub tainted ($) {
77 any_tainted @_;
78}
79sub all_tainted (@) {
80 for (@_) { return 0 unless tainted $_ }
81 1;
82}
83
84sub test ($$;$) {
85 my($serial, $boolean, $diag) = @_;
86 if ($boolean) {
87 print "ok $serial\n";
88 } else {
89 print "not ok $serial\n";
90 for (split m/^/m, $diag) {
91 print "# $_";
92 }
9607fc9c 93 print "\n" unless
1e422769 94 $diag eq ''
95 or substr($diag, -1) eq "\n";
96 }
97}
98
99# We need an external program to call.
5aabfad6 100my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : "./echo$$");
1e422769 101END { unlink $ECHO }
102open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
103print PROG 'print "@ARGV\n"', "\n";
104close PROG;
105my $echo = "$Invoke_Perl $ECHO";
106
27c9684d 107print "1..155\n";
1e422769 108
109# First, let's make sure that Perl is checking the dangerous
110# environment variables. Maybe they aren't set yet, so we'll
111# taint them ourselves.
112{
113 $ENV{'DCL$PATH'} = '' if $Is_VMS;
114
7bac28a0 115 $ENV{PATH} = '';
c90c0ff4 116 delete @ENV{@MoreEnv};
7bac28a0 117 $ENV{TERM} = 'dumb';
118
119 test 1, eval { `$echo 1` } eq "1\n";
120
39e571d4 121 if ($Is_MSWin32 || $Is_VMS || $Is_Dos) {
7bac28a0 122 print "# Environment tainting tests skipped\n";
c90c0ff4 123 for (2..5) { print "ok $_\n" }
5aabfad6 124 }
125 else {
7bac28a0 126 my @vars = ('PATH', @MoreEnv);
127 while (my $v = $vars[0]) {
128 local $ENV{$v} = $TAINT;
129 last if eval { `$echo 1` };
130 last unless $@ =~ /^Insecure \$ENV{$v}/;
131 shift @vars;
132 }
133 test 2, !@vars, "\$$vars[0]";
c90c0ff4 134
135 # tainted $TERM is unsafe only if it contains metachars
136 local $ENV{TERM};
137 $ENV{TERM} = 'e=mc2';
138 test 3, eval { `$echo 1` } eq "1\n";
139 $ENV{TERM} = 'e=mc2' . $TAINT;
140 test 4, eval { `$echo 1` } eq '';
141 test 5, $@ =~ /^Insecure \$ENV{TERM}/, $@;
5aabfad6 142 }
7bac28a0 143
9607fc9c 144 my $tmp;
48c036b1
GS
145 if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_Dos) {
146 print "# all directories are writeable\n";
147 }
148 else {
9607fc9c 149 $tmp = (grep { defined and -d and (stat _)[2] & 2 }
099f76bb 150 qw(sys$scratch /tmp /var/tmp /usr/tmp),
9607fc9c 151 @ENV{qw(TMP TEMP)})[0]
152 or print "# can't find world-writeable directory to test PATH\n";
153 }
154
385588b3 155 if ($tmp) {
7bac28a0 156 local $ENV{PATH} = $tmp;
c90c0ff4 157 test 6, eval { `$echo 1` } eq '';
158 test 7, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
1e422769 159 }
160 else {
fac76ed7 161 for (6..7) { print "ok $_ # Skipped: all directories are writeable\n" }
1e422769 162 }
163
1e422769 164 if ($Is_VMS) {
165 $ENV{'DCL$PATH'} = $TAINT;
c90c0ff4 166 test 8, eval { `$echo 1` } eq '';
167 test 9, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
9607fc9c 168 if ($tmp) {
169 $ENV{'DCL$PATH'} = $tmp;
c90c0ff4 170 test 10, eval { `$echo 1` } eq '';
171 test 11, $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
9607fc9c 172 }
173 else {
fac76ed7 174 for (10..11) { print "ok $_ # Skipped: can't find world-writeable directory to test DCL\$PATH\n" }
9607fc9c 175 }
1e422769 176 $ENV{'DCL$PATH'} = '';
177 }
178 else {
fac76ed7 179 for (8..11) { print "ok $_ # Skipped: This is not VMS\n"; }
1e422769 180 }
181}
182
183# Let's see that we can taint and untaint as needed.
184{
185 my $foo = $TAINT;
c90c0ff4 186 test 12, tainted $foo;
9607fc9c 187
188 # That was a sanity check. If it failed, stop the insanity!
189 die "Taint checks don't seem to be enabled" unless tainted $foo;
1e422769 190
191 $foo = "foo";
c90c0ff4 192 test 13, not tainted $foo;
1e422769 193
194 taint_these($foo);
c90c0ff4 195 test 14, tainted $foo;
1e422769 196
197 my @list = 1..10;
c90c0ff4 198 test 15, not any_tainted @list;
1e422769 199 taint_these @list[1,3,5,7,9];
c90c0ff4 200 test 16, any_tainted @list;
201 test 17, all_tainted @list[1,3,5,7,9];
202 test 18, not any_tainted @list[0,2,4,6,8];
1e422769 203
204 ($foo) = $foo =~ /(.+)/;
c90c0ff4 205 test 19, not tainted $foo;
1e422769 206
207 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
c90c0ff4 208 test 20, not tainted $foo;
209 test 21, $foo eq 'bar';
1e422769 210
b3eb6a9b
GS
211 {
212 use re 'taint';
213
214 ($foo) = ('bar' . $TAINT) =~ /(.+)/;
215 test 22, tainted $foo;
216 test 23, $foo eq 'bar';
217
218 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
219 test 24, tainted $foo;
220 test 25, $foo eq 'bar';
221 }
222
223 $foo = $1 if 'bar' =~ /(.+)$TAINT/;
224 test 26, tainted $foo;
225 test 27, $foo eq 'bar';
48c036b1 226
1e422769 227 my $pi = 4 * atan2(1,1) + $TAINT0;
b3eb6a9b 228 test 28, tainted $pi;
1e422769 229
230 ($pi) = $pi =~ /(\d+\.\d+)/;
b3eb6a9b
GS
231 test 29, not tainted $pi;
232 test 30, sprintf("%.5f", $pi) eq '3.14159';
1e422769 233}
234
235# How about command-line arguments? The problem is that we don't
236# always get some, so we'll run another process with some.
237{
238 my $arg = "./arg$$";
239 open PROG, "> $arg" or die "Can't create $arg: $!";
240 print PROG q{
241 eval { join('', @ARGV), kill 0 };
242 exit 0 if $@ =~ /^Insecure dependency/;
243 print "# Oops: \$@ was [$@]\n";
244 exit 1;
245 };
246 close PROG;
247 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
b3eb6a9b 248 test 31, !$?, "Exited with status $?";
1e422769 249 unlink $arg;
250}
251
252# Reading from a file should be tainted
253{
9607fc9c 254 my $file = './TEST';
b3eb6a9b 255 test 32, open(FILE, $file), "Couldn't open '$file': $!";
1e422769 256
257 my $block;
258 sysread(FILE, $block, 100);
9607fc9c 259 my $line = <FILE>;
1e422769 260 close FILE;
b3eb6a9b
GS
261 test 33, tainted $block;
262 test 34, tainted $line;
1e422769 263}
264
c90c0ff4 265# Globs should be forbidden, except under VMS,
266# which doesn't spawn an external program.
72b16652
GS
267if (1 # built-in glob
268 or $Is_VMS) {
b3eb6a9b 269 for (35..36) { print "ok $_\n"; }
c90c0ff4 270}
271else {
7bac28a0 272 my @globs = eval { <*> };
b3eb6a9b 273 test 35, @globs == 0 && $@ =~ /^Insecure dependency/;
1e422769 274
7bac28a0 275 @globs = eval { glob '*' };
b3eb6a9b 276 test 36, @globs == 0 && $@ =~ /^Insecure dependency/;
1e422769 277}
278
279# Output of commands should be tainted
280{
281 my $foo = `$echo abc`;
b3eb6a9b 282 test 37, tainted $foo;
1e422769 283}
284
285# Certain system variables should be tainted
286{
b3eb6a9b 287 test 38, all_tainted $^X, $0;
1e422769 288}
289
290# Results of matching should all be untainted
291{
292 my $foo = "abcdefghi" . $TAINT;
b3eb6a9b 293 test 39, tainted $foo;
1e422769 294
295 $foo =~ /def/;
b3eb6a9b 296 test 40, not any_tainted $`, $&, $';
1e422769 297
298 $foo =~ /(...)(...)(...)/;
b3eb6a9b 299 test 41, not any_tainted $1, $2, $3, $+;
1e422769 300
301 my @bar = $foo =~ /(...)(...)(...)/;
b3eb6a9b 302 test 42, not any_tainted @bar;
1e422769 303
b3eb6a9b
GS
304 test 43, tainted $foo; # $foo should still be tainted!
305 test 44, $foo eq "abcdefghi";
1e422769 306}
307
308# Operations which affect files can't use tainted data.
309{
b3eb6a9b
GS
310 test 45, eval { chmod 0, $TAINT } eq '', 'chmod';
311 test 46, $@ =~ /^Insecure dependency/, $@;
1e422769 312
9607fc9c 313 # There is no feature test in $Config{} for truncate,
314 # so we allow for the possibility that it's missing.
b3eb6a9b
GS
315 test 47, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate';
316 test 48, $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
1e422769 317
b3eb6a9b
GS
318 test 49, eval { rename '', $TAINT } eq '', 'rename';
319 test 50, $@ =~ /^Insecure dependency/, $@;
1e422769 320
b3eb6a9b
GS
321 test 51, eval { unlink $TAINT } eq '', 'unlink';
322 test 52, $@ =~ /^Insecure dependency/, $@;
9607fc9c 323
b3eb6a9b
GS
324 test 53, eval { utime $TAINT } eq '', 'utime';
325 test 54, $@ =~ /^Insecure dependency/, $@;
48c036b1 326
1e422769 327 if ($Config{d_chown}) {
b3eb6a9b
GS
328 test 55, eval { chown -1, -1, $TAINT } eq '', 'chown';
329 test 56, $@ =~ /^Insecure dependency/, $@;
1e422769 330 }
331 else {
b3eb6a9b 332 for (55..56) { print "ok $_ # Skipped: chown() is not available\n" }
1e422769 333 }
334
335 if ($Config{d_link}) {
b3eb6a9b
GS
336 test 57, eval { link $TAINT, '' } eq '', 'link';
337 test 58, $@ =~ /^Insecure dependency/, $@;
1e422769 338 }
339 else {
b3eb6a9b 340 for (57..58) { print "ok $_ # Skipped: link() is not available\n" }
1e422769 341 }
342
343 if ($Config{d_symlink}) {
b3eb6a9b
GS
344 test 59, eval { symlink $TAINT, '' } eq '', 'symlink';
345 test 60, $@ =~ /^Insecure dependency/, $@;
1e422769 346 }
347 else {
b3eb6a9b 348 for (59..60) { print "ok $_ # Skipped: symlink() is not available\n" }
1e422769 349 }
350}
351
352# Operations which affect directories can't use tainted data.
353{
b3eb6a9b
GS
354 test 61, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir';
355 test 62, $@ =~ /^Insecure dependency/, $@;
1e422769 356
b3eb6a9b
GS
357 test 63, eval { rmdir $TAINT } eq '', 'rmdir';
358 test 64, $@ =~ /^Insecure dependency/, $@;
9607fc9c 359
b3eb6a9b
GS
360 test 65, eval { chdir $TAINT } eq '', 'chdir';
361 test 66, $@ =~ /^Insecure dependency/, $@;
48c036b1 362
1e422769 363 if ($Config{d_chroot}) {
b3eb6a9b
GS
364 test 67, eval { chroot $TAINT } eq '', 'chroot';
365 test 68, $@ =~ /^Insecure dependency/, $@;
1e422769 366 }
367 else {
b3eb6a9b 368 for (67..68) { print "ok $_ # Skipped: chroot() is not available\n" }
1e422769 369 }
370}
371
372# Some operations using files can't use tainted data.
373{
374 my $foo = "imaginary library" . $TAINT;
b3eb6a9b
GS
375 test 69, eval { require $foo } eq '', 'require';
376 test 70, $@ =~ /^Insecure dependency/, $@;
1e422769 377
378 my $filename = "./taintB$$"; # NB: $filename isn't tainted!
379 END { unlink $filename if defined $filename }
380 $foo = $filename . $TAINT;
381 unlink $filename; # in any case
382
b3eb6a9b
GS
383 test 71, eval { open FOO, $foo } eq '', 'open for read';
384 test 72, $@ eq '', $@; # NB: This should be allowed
9d116dd7
JH
385
386 # Try first new style but allow also old style.
61ae2fbf
JH
387 test 73, $!{ENOENT} ||
388 $! == 2 || # File not found
389 ($Is_Dos && $! == 22) ||
390 ($^O eq 'mint' && $! == 33);
1e422769 391
b3eb6a9b
GS
392 test 74, eval { open FOO, "> $foo" } eq '', 'open for write';
393 test 75, $@ =~ /^Insecure dependency/, $@;
1e422769 394}
395
396# Commands to the system can't use tainted data
397{
398 my $foo = $TAINT;
399
400 if ($^O eq 'amigaos') {
b3eb6a9b 401 for (76..79) { print "ok $_ # Skipped: open('|') is not available\n" }
1e422769 402 }
403 else {
06eaf0bc 404 test 76, eval { open FOO, "| x$foo" } eq '', 'popen to';
b3eb6a9b 405 test 77, $@ =~ /^Insecure dependency/, $@;
1e422769 406
06eaf0bc 407 test 78, eval { open FOO, "x$foo |" } eq '', 'popen from';
b3eb6a9b 408 test 79, $@ =~ /^Insecure dependency/, $@;
48c036b1 409 }
1e422769 410
b3eb6a9b
GS
411 test 80, eval { exec $TAINT } eq '', 'exec';
412 test 81, $@ =~ /^Insecure dependency/, $@;
9607fc9c 413
b3eb6a9b
GS
414 test 82, eval { system $TAINT } eq '', 'system';
415 test 83, $@ =~ /^Insecure dependency/, $@;
48c036b1 416
1e422769 417 $foo = "*";
418 taint_these $foo;
419
b3eb6a9b
GS
420 test 84, eval { `$echo 1$foo` } eq '', 'backticks';
421 test 85, $@ =~ /^Insecure dependency/, $@;
1e422769 422
423 if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe
b3eb6a9b
GS
424 test 86, join('', eval { glob $foo } ) ne '', 'globbing';
425 test 87, $@ eq '', $@;
1e422769 426 }
427 else {
b3eb6a9b 428 for (86..87) { print "ok $_ # Skipped: this is not VMS\n"; }
1e422769 429 }
430}
431
432# Operations which affect processes can't use tainted data.
433{
b3eb6a9b
GS
434 test 88, eval { kill 0, $TAINT } eq '', 'kill';
435 test 89, $@ =~ /^Insecure dependency/, $@;
1e422769 436
437 if ($Config{d_setpgrp}) {
b3eb6a9b
GS
438 test 90, eval { setpgrp 0, $TAINT } eq '', 'setpgrp';
439 test 91, $@ =~ /^Insecure dependency/, $@;
1e422769 440 }
441 else {
b3eb6a9b 442 for (90..91) { print "ok $_ # Skipped: setpgrp() is not available\n" }
1e422769 443 }
444
445 if ($Config{d_setprior}) {
b3eb6a9b
GS
446 test 92, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority';
447 test 93, $@ =~ /^Insecure dependency/, $@;
1e422769 448 }
449 else {
b3eb6a9b 450 for (92..93) { print "ok $_ # Skipped: setpriority() is not available\n" }
1e422769 451 }
452}
453
454# Some miscellaneous operations can't use tainted data.
455{
456 if ($Config{d_syscall}) {
b3eb6a9b
GS
457 test 94, eval { syscall $TAINT } eq '', 'syscall';
458 test 95, $@ =~ /^Insecure dependency/, $@;
1e422769 459 }
460 else {
b3eb6a9b 461 for (94..95) { print "ok $_ # Skipped: syscall() is not available\n" }
1e422769 462 }
463
464 {
465 my $foo = "x" x 979;
466 taint_these $foo;
467 local *FOO;
468 my $temp = "./taintC$$";
469 END { unlink $temp }
b3eb6a9b 470 test 96, open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
1e422769 471
b3eb6a9b
GS
472 test 97, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl';
473 test 98, $@ =~ /^Insecure dependency/, $@;
1e422769 474
475 if ($Config{d_fcntl}) {
b3eb6a9b
GS
476 test 99, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl';
477 test 100, $@ =~ /^Insecure dependency/, $@;
1e422769 478 }
479 else {
b3eb6a9b 480 for (99..100) { print "ok $_ # Skipped: fcntl() is not available\n" }
1e422769 481 }
482
483 close FOO;
484 }
485}
486
9607fc9c 487# Some tests involving references
1e422769 488{
489 my $foo = 'abc' . $TAINT;
490 my $fooref = \$foo;
b3eb6a9b
GS
491 test 101, not tainted $fooref;
492 test 102, tainted $$fooref;
493 test 103, tainted $foo;
1e422769 494}
54310121 495
496# Some tests involving assignment
497{
498 my $foo = $TAINT0;
499 my $bar = $foo;
b3eb6a9b
GS
500 test 104, all_tainted $foo, $bar;
501 test 105, tainted($foo = $bar);
502 test 106, tainted($bar = $bar);
503 test 107, tainted($bar += $bar);
504 test 108, tainted($bar -= $bar);
505 test 109, tainted($bar *= $bar);
506 test 110, tainted($bar++);
507 test 111, tainted($bar /= $bar);
508 test 112, tainted($bar += 0);
509 test 113, tainted($bar -= 2);
510 test 114, tainted($bar *= -1);
511 test 115, tainted($bar /= 1);
512 test 116, tainted($bar--);
513 test 117, $bar == 0;
54310121 514}
a1f49e72
CS
515
516# Test assignment and return of lists
517{
518 my @foo = ("A", "tainted" . $TAINT, "B");
b3eb6a9b
GS
519 test 118, not tainted $foo[0];
520 test 119, tainted $foo[1];
521 test 120, not tainted $foo[2];
a1f49e72 522 my @bar = @foo;
b3eb6a9b
GS
523 test 121, not tainted $bar[0];
524 test 122, tainted $bar[1];
525 test 123, not tainted $bar[2];
a1f49e72 526 my @baz = eval { "A", "tainted" . $TAINT, "B" };
b3eb6a9b
GS
527 test 124, not tainted $baz[0];
528 test 125, tainted $baz[1];
529 test 126, not tainted $baz[2];
a1f49e72 530 my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
b3eb6a9b
GS
531 test 127, not tainted $plugh[0];
532 test 128, tainted $plugh[1];
533 test 129, not tainted $plugh[2];
a1f49e72 534 my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
b3eb6a9b
GS
535 test 130, not tainted ((&$nautilus)[0]);
536 test 131, tainted ((&$nautilus)[1]);
537 test 132, not tainted ((&$nautilus)[2]);
a1f49e72 538 my @xyzzy = &$nautilus;
b3eb6a9b
GS
539 test 133, not tainted $xyzzy[0];
540 test 134, tainted $xyzzy[1];
541 test 135, not tainted $xyzzy[2];
a1f49e72 542 my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
b3eb6a9b
GS
543 test 136, not tainted ((&$red_october)[0]);
544 test 137, tainted ((&$red_october)[1]);
545 test 138, not tainted ((&$red_october)[2]);
a1f49e72 546 my @corge = &$red_october;
b3eb6a9b
GS
547 test 139, not tainted $corge[0];
548 test 140, tainted $corge[1];
549 test 141, not tainted $corge[2];
a1f49e72 550}
fb73857a 551
552# Test for system/library calls returning string data of dubious origin.
553{
554 # No reliable %Config check for getpw*
555 if (eval { setpwent(); getpwent(); 1 }) {
556 setpwent();
557 my @getpwent = getpwent();
558 die "getpwent: $!\n" unless (@getpwent);
b3eb6a9b 559 test 142,( not tainted $getpwent[0]
2959b6e3 560 and tainted $getpwent[1]
fb73857a 561 and not tainted $getpwent[2]
562 and not tainted $getpwent[3]
563 and not tainted $getpwent[4]
564 and not tainted $getpwent[5]
3972a534 565 and tainted $getpwent[6] # ge?cos
fb73857a 566 and not tainted $getpwent[7]
3972a534 567 and tainted $getpwent[8]); # shell
fb73857a 568 endpwent();
569 } else {
b3eb6a9b 570 for (142) { print "ok $_ # Skipped: getpwent() is not available\n" }
fb73857a 571 }
572
573 if ($Config{d_readdir}) { # pretty hard to imagine not
574 local(*D);
575 opendir(D, "op") or die "opendir: $!\n";
576 my $readdir = readdir(D);
b3eb6a9b 577 test 143, tainted $readdir;
fb73857a 578 closedir(OP);
579 } else {
b3eb6a9b 580 for (143) { print "ok $_ # Skipped: readdir() is not available\n" }
fb73857a 581 }
582
583 if ($Config{d_readlink} && $Config{d_symlink}) {
584 my $symlink = "sl$$";
585 unlink($symlink);
586 symlink("/something/naughty", $symlink) or die "symlink: $!\n";
587 my $readlink = readlink($symlink);
b3eb6a9b 588 test 144, tainted $readlink;
fb73857a 589 unlink($symlink);
590 } else {
b3eb6a9b 591 for (144) { print "ok $_ # Skipped: readlink() or symlink() is not available\n"; }
fb73857a 592 }
593}
594
595# test bitwise ops (regression bug)
596{
597 my $why = "y";
598 my $j = "x" | $why;
b3eb6a9b 599 test 145, not tainted $j;
fb73857a 600 $why = $TAINT."y";
601 $j = "x" | $why;
b3eb6a9b 602 test 146, tainted $j;
fb73857a 603}
604
48c036b1
GS
605# test target of substitution (regression bug)
606{
607 my $why = $TAINT."y";
608 $why =~ s/y/z/;
b3eb6a9b 609 test 147, tainted $why;
48c036b1
GS
610
611 my $z = "[z]";
612 $why =~ s/$z/zee/;
b3eb6a9b 613 test 148, tainted $why;
48c036b1
GS
614
615 $why =~ s/e/'-'.$$/ge;
b3eb6a9b 616 test 149, tainted $why;
48c036b1 617}
d929ce6f
JH
618
619# test shmread
620{
c9f931b8
JH
621 unless ($ipcsysv) {
622 print "ok 150 # skipped: no IPC::SysV\n";
623 last;
624 }
be3174d2 625 if ($Config{'extensions'} =~ /\bIPC\/SysV\b/ && $Config{d_shm}) {
b9d1c439 626 no strict 'subs';
d929ce6f
JH
627 my $sent = "foobar";
628 my $rcvd;
629 my $size = 2000;
c2e66d9e
GS
630 my $id = shmget(IPC_PRIVATE, $size, S_IRWXU);
631
41d6edb2
JH
632 if (defined $id) {
633 if (shmwrite($id, $sent, 0, 60)) {
634 if (shmread($id, $rcvd, 0, 60)) {
d929ce6f
JH
635 substr($rcvd, index($rcvd, "\0")) = '';
636 } else {
637 warn "# shmread failed: $!\n";
638 }
639 } else {
640 warn "# shmwrite failed: $!\n";
641 }
c2e66d9e 642 shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n";
41d6edb2
JH
643 } else {
644 warn "# shmget failed: $!\n";
d929ce6f
JH
645 }
646
647 if ($rcvd eq $sent) {
648 test 150, tainted $rcvd;
649 } else {
650 print "ok 150 # Skipped: SysV shared memory operation failed\n";
651 }
652 } else {
41d6edb2 653 print "ok 150 # Skipped: SysV shared memory is not available\n";
d929ce6f
JH
654 }
655}
41d6edb2
JH
656
657# test msgrcv
658{
c9f931b8
JH
659 unless ($ipcsysv) {
660 print "ok 151 # skipped: no IPC::SysV\n";
661 last;
662 }
be3174d2 663 if ($Config{'extensions'} =~ /\bIPC\/SysV\b/ && $Config{d_msg}) {
b9d1c439 664 no strict 'subs';
41d6edb2
JH
665 my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
666
667 my $sent = "message";
668 my $type_sent = 1234;
669 my $rcvd;
670 my $type_rcvd;
671
672 if (defined $id) {
673 if (msgsnd($id, pack("l! a*", $type_sent, $sent), 0)) {
674 if (msgrcv($id, $rcvd, 60, 0, 0)) {
675 ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd);
676 } else {
677 warn "# msgrcv failed\n";
678 }
679 } else {
680 warn "# msgsnd failed\n";
681 }
c2e66d9e 682 msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n";
41d6edb2
JH
683 } else {
684 warn "# msgget failed\n";
685 }
686
687 if ($rcvd eq $sent && $type_sent == $type_rcvd) {
688 test 151, tainted $rcvd;
689 } else {
690 print "ok 151 # Skipped: SysV message queue operation failed\n";
691 }
692 } else {
693 print "ok 151 # Skipped: SysV message queues are not available\n";
694 }
695}
696
3887d568
AP
697{
698 # bug id 20001004.006
699
700 open IN, "./TEST" or warn "$0: cannot read ./TEST: $!" ;
701 local $/;
702 my $a = <IN>;
703 my $b = <IN>;
704 print "not " unless tainted($a) && tainted($b) && !defined($b);
705 print "ok 152\n";
27c9684d 706 close IN;
3887d568 707}
27c9684d
AP
708
709{
710 # bug id 20001004.007
711
712 open IN, "./TEST" or warn "$0: cannot read ./TEST: $!" ;
713 my $a = <IN>;
714
715 my $c = { a => 42,
716 b => $a };
717 print "not " unless !tainted($c->{a}) && tainted($c->{b});
718 print "ok 153\n";
719
720 my $d = { a => $a,
721 b => 42 };
722 print "not " unless tainted($d->{a}) && !tainted($d->{b});
723 print "ok 154\n";
724
725 my $e = { a => 42,
726 b => { c => $a, d => 42 } };
727 print "not " unless !tainted($e->{a}) &&
728 !tainted($e->{b}) &&
729 tainted($e->{b}->{c}) &&
730 !tainted($e->{b}->{d});
731 print "ok 155\n";
732
733 close IN;
734}
735