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