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