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