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