This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
delete spurious blank lines added by change 23251
[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;
dc459aad 17use File::Spec::Functions;
1e422769 18
7c36658b 19my $test = 177;
1d662fb6 20sub ok ($;$) {
7c36658b
MS
21 my($ok, $name) = @_;
22
23 # You have to do it this way or VMS will get confused.
24 print $ok ? "ok $test - $name\n" : "not ok $test - $name\n";
25
26 printf "# Failed test at line %d\n", (caller)[2] unless $ok;
27
28 $test++;
29 return $ok;
30}
31
32
0ecd3ba2
MG
33$| = 1;
34
c9f931b8
JH
35use vars qw($ipcsysv); # did we manage to load IPC::SysV?
36
3eeba6fb
CB
37BEGIN {
38 if ($^O eq 'VMS' && !defined($Config{d_setenv})) {
39 $ENV{PATH} = $ENV{PATH};
40 $ENV{TERM} = $ENV{TERM} ne ''? $ENV{TERM} : 'dummy';
41 }
be3174d2
GS
42 if ($Config{'extensions'} =~ /\bIPC\/SysV\b/
43 && ($Config{d_shm} || $Config{d_msg})) {
c9f931b8
JH
44 eval { require IPC::SysV };
45 unless ($@) {
46 $ipcsysv++;
ddc3217d 47 IPC::SysV->import(qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU IPC_NOWAIT));
c9f931b8 48 }
b9d1c439 49 }
3eeba6fb
CB
50}
51
dc459aad 52my $Is_MacOS = $^O eq 'MacOS';
1e422769 53my $Is_VMS = $^O eq 'VMS';
68dc0745 54my $Is_MSWin32 = $^O eq 'MSWin32';
2986a63f 55my $Is_NetWare = $^O eq 'NetWare';
39e571d4 56my $Is_Dos = $^O eq 'dos';
343d3f38 57my $Is_Cygwin = $^O eq 'cygwin';
68dc0745 58my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' :
2986a63f 59 ($Is_MSWin32 ? '.\perl' :
dc459aad 60 $Is_MacOS ? ':perl' :
2986a63f 61 ($Is_NetWare ? 'perl' : './perl'));
c90c0ff4 62my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
7bac28a0 63
1e422769 64if ($Is_VMS) {
7bac28a0
PP
65 my (%old, $x);
66 for $x ('DCL$PATH', @MoreEnv) {
67 ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
68 }
1e422769
PP
69 eval <<EndOfCleanup;
70 END {
3eeba6fb 71 \$ENV{PATH} = '' if $Config{d_setenv};
1e422769 72 warn "# Note: logical name 'PATH' may have been deleted\n";
562a7b0c 73 \@ENV{keys %old} = values %old;
1e422769
PP
74 }
75EndOfCleanup
76}
77
78# Sources of taint:
79# The empty tainted value, for tainting strings
80my $TAINT = substr($^X, 0, 0);
81# A tainted zero, useful for tainting numbers
82my $TAINT0 = 0 + $TAINT;
83
84# This taints each argument passed. All must be lvalues.
85# Side effect: It also stringifies them. :-(
86sub taint_these (@) {
87 for (@_) { $_ .= $TAINT }
88}
89
90# How to identify taint when you see it
91sub any_tainted (@) {
92 not eval { join("",@_), kill 0; 1 };
93}
94sub tainted ($) {
95 any_tainted @_;
96}
97sub all_tainted (@) {
98 for (@_) { return 0 unless tainted $_ }
99 1;
100}
101
102sub test ($$;$) {
103 my($serial, $boolean, $diag) = @_;
104 if ($boolean) {
105 print "ok $serial\n";
106 } else {
107 print "not ok $serial\n";
108 for (split m/^/m, $diag) {
109 print "# $_";
110 }
9607fc9c 111 print "\n" unless
1e422769
PP
112 $diag eq ''
113 or substr($diag, -1) eq "\n";
114 }
115}
116
117# We need an external program to call.
dc459aad 118my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : $Is_MacOS ? ":echo$$" : ($Is_NetWare ? "echo$$" : "./echo$$"));
1e422769
PP
119END { unlink $ECHO }
120open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
121print PROG 'print "@ARGV\n"', "\n";
122close PROG;
123my $echo = "$Invoke_Perl $ECHO";
124
dc459aad
JH
125my $TEST = catfile(curdir(), 'TEST');
126
3511154c 127print "1..223\n";
1e422769
PP
128
129# First, let's make sure that Perl is checking the dangerous
130# environment variables. Maybe they aren't set yet, so we'll
131# taint them ourselves.
132{
133 $ENV{'DCL$PATH'} = '' if $Is_VMS;
134
7bac28a0 135 $ENV{PATH} = '';
c90c0ff4 136 delete @ENV{@MoreEnv};
7bac28a0
PP
137 $ENV{TERM} = 'dumb';
138
8852b6d2 139 if ($Is_Cygwin && ! -f 'cygwin1.dll') {
343d3f38
JH
140 system("/usr/bin/cp /usr/bin/cygwin1.dll .") &&
141 die "$0: failed to cp cygwin1.dll: $!\n";
cef9cd04
YST
142 eval q{
143 END { unlink "cygwin1.dll" }
144 };
145 }
146
147 if ($Is_Cygwin && ! -f 'cygcrypt-0.dll' && -f '/usr/bin/cygcrypt-0.dll') {
148 system("/usr/bin/cp /usr/bin/cygcrypt-0.dll .") &&
149 die "$0: failed to cp cygcrypt-0.dll: $!\n";
150 eval q{
151 END { unlink "cygcrypt-0.dll" }
152 };
343d3f38
JH
153 }
154
7bac28a0
PP
155 test 1, eval { `$echo 1` } eq "1\n";
156
dc459aad 157 if ($Is_MSWin32 || $Is_NetWare || $Is_VMS || $Is_Dos || $Is_MacOS) {
7bac28a0 158 print "# Environment tainting tests skipped\n";
c90c0ff4 159 for (2..5) { print "ok $_\n" }
5aabfad6
PP
160 }
161 else {
7bac28a0
PP
162 my @vars = ('PATH', @MoreEnv);
163 while (my $v = $vars[0]) {
164 local $ENV{$v} = $TAINT;
165 last if eval { `$echo 1` };
166 last unless $@ =~ /^Insecure \$ENV{$v}/;
167 shift @vars;
168 }
169 test 2, !@vars, "\$$vars[0]";
c90c0ff4
PP
170
171 # tainted $TERM is unsafe only if it contains metachars
172 local $ENV{TERM};
173 $ENV{TERM} = 'e=mc2';
174 test 3, eval { `$echo 1` } eq "1\n";
175 $ENV{TERM} = 'e=mc2' . $TAINT;
176 test 4, eval { `$echo 1` } eq '';
177 test 5, $@ =~ /^Insecure \$ENV{TERM}/, $@;
5aabfad6 178 }
7bac28a0 179
9607fc9c 180 my $tmp;
2986a63f 181 if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_NetWare || $Is_Dos) {
48c036b1
GS
182 print "# all directories are writeable\n";
183 }
184 else {
9607fc9c 185 $tmp = (grep { defined and -d and (stat _)[2] & 2 }
099f76bb 186 qw(sys$scratch /tmp /var/tmp /usr/tmp),
9607fc9c
PP
187 @ENV{qw(TMP TEMP)})[0]
188 or print "# can't find world-writeable directory to test PATH\n";
189 }
190
385588b3 191 if ($tmp) {
7bac28a0 192 local $ENV{PATH} = $tmp;
c90c0ff4
PP
193 test 6, eval { `$echo 1` } eq '';
194 test 7, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
1e422769
PP
195 }
196 else {
fac76ed7 197 for (6..7) { print "ok $_ # Skipped: all directories are writeable\n" }
1e422769
PP
198 }
199
1e422769
PP
200 if ($Is_VMS) {
201 $ENV{'DCL$PATH'} = $TAINT;
c90c0ff4
PP
202 test 8, eval { `$echo 1` } eq '';
203 test 9, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
9607fc9c
PP
204 if ($tmp) {
205 $ENV{'DCL$PATH'} = $tmp;
c90c0ff4
PP
206 test 10, eval { `$echo 1` } eq '';
207 test 11, $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
9607fc9c
PP
208 }
209 else {
fac76ed7 210 for (10..11) { print "ok $_ # Skipped: can't find world-writeable directory to test DCL\$PATH\n" }
9607fc9c 211 }
1e422769
PP
212 $ENV{'DCL$PATH'} = '';
213 }
214 else {
fac76ed7 215 for (8..11) { print "ok $_ # Skipped: This is not VMS\n"; }
1e422769
PP
216 }
217}
218
219# Let's see that we can taint and untaint as needed.
220{
221 my $foo = $TAINT;
c90c0ff4 222 test 12, tainted $foo;
9607fc9c
PP
223
224 # That was a sanity check. If it failed, stop the insanity!
225 die "Taint checks don't seem to be enabled" unless tainted $foo;
1e422769
PP
226
227 $foo = "foo";
c90c0ff4 228 test 13, not tainted $foo;
1e422769
PP
229
230 taint_these($foo);
c90c0ff4 231 test 14, tainted $foo;
1e422769
PP
232
233 my @list = 1..10;
c90c0ff4 234 test 15, not any_tainted @list;
1e422769 235 taint_these @list[1,3,5,7,9];
c90c0ff4
PP
236 test 16, any_tainted @list;
237 test 17, all_tainted @list[1,3,5,7,9];
238 test 18, not any_tainted @list[0,2,4,6,8];
1e422769
PP
239
240 ($foo) = $foo =~ /(.+)/;
c90c0ff4 241 test 19, not tainted $foo;
1e422769
PP
242
243 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
c90c0ff4
PP
244 test 20, not tainted $foo;
245 test 21, $foo eq 'bar';
1e422769 246
b3eb6a9b
GS
247 {
248 use re 'taint';
249
250 ($foo) = ('bar' . $TAINT) =~ /(.+)/;
251 test 22, tainted $foo;
252 test 23, $foo eq 'bar';
253
254 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
255 test 24, tainted $foo;
256 test 25, $foo eq 'bar';
257 }
258
259 $foo = $1 if 'bar' =~ /(.+)$TAINT/;
260 test 26, tainted $foo;
261 test 27, $foo eq 'bar';
48c036b1 262
1e422769 263 my $pi = 4 * atan2(1,1) + $TAINT0;
b3eb6a9b 264 test 28, tainted $pi;
1e422769
PP
265
266 ($pi) = $pi =~ /(\d+\.\d+)/;
b3eb6a9b
GS
267 test 29, not tainted $pi;
268 test 30, sprintf("%.5f", $pi) eq '3.14159';
1e422769
PP
269}
270
271# How about command-line arguments? The problem is that we don't
272# always get some, so we'll run another process with some.
dc459aad
JH
273SKIP: {
274 my $arg = catfile(curdir(), "arg$$");
1e422769
PP
275 open PROG, "> $arg" or die "Can't create $arg: $!";
276 print PROG q{
277 eval { join('', @ARGV), kill 0 };
278 exit 0 if $@ =~ /^Insecure dependency/;
279 print "# Oops: \$@ was [$@]\n";
280 exit 1;
281 };
282 close PROG;
283 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
b3eb6a9b 284 test 31, !$?, "Exited with status $?";
1e422769
PP
285 unlink $arg;
286}
287
288# Reading from a file should be tainted
289{
dc459aad 290 test 32, open(FILE, $TEST), "Couldn't open '$TEST': $!";
1e422769
PP
291
292 my $block;
293 sysread(FILE, $block, 100);
9607fc9c 294 my $line = <FILE>;
1e422769 295 close FILE;
b3eb6a9b
GS
296 test 33, tainted $block;
297 test 34, tainted $line;
1e422769
PP
298}
299
c90c0ff4
PP
300# Globs should be forbidden, except under VMS,
301# which doesn't spawn an external program.
72b16652
GS
302if (1 # built-in glob
303 or $Is_VMS) {
b3eb6a9b 304 for (35..36) { print "ok $_\n"; }
c90c0ff4
PP
305}
306else {
7bac28a0 307 my @globs = eval { <*> };
b3eb6a9b 308 test 35, @globs == 0 && $@ =~ /^Insecure dependency/;
1e422769 309
7bac28a0 310 @globs = eval { glob '*' };
b3eb6a9b 311 test 36, @globs == 0 && $@ =~ /^Insecure dependency/;
1e422769
PP
312}
313
314# Output of commands should be tainted
315{
316 my $foo = `$echo abc`;
b3eb6a9b 317 test 37, tainted $foo;
1e422769
PP
318}
319
320# Certain system variables should be tainted
321{
b3eb6a9b 322 test 38, all_tainted $^X, $0;
1e422769
PP
323}
324
325# Results of matching should all be untainted
326{
327 my $foo = "abcdefghi" . $TAINT;
b3eb6a9b 328 test 39, tainted $foo;
1e422769
PP
329
330 $foo =~ /def/;
b3eb6a9b 331 test 40, not any_tainted $`, $&, $';
1e422769
PP
332
333 $foo =~ /(...)(...)(...)/;
b3eb6a9b 334 test 41, not any_tainted $1, $2, $3, $+;
1e422769
PP
335
336 my @bar = $foo =~ /(...)(...)(...)/;
b3eb6a9b 337 test 42, not any_tainted @bar;
1e422769 338
b3eb6a9b
GS
339 test 43, tainted $foo; # $foo should still be tainted!
340 test 44, $foo eq "abcdefghi";
1e422769
PP
341}
342
343# Operations which affect files can't use tainted data.
344{
b3eb6a9b
GS
345 test 45, eval { chmod 0, $TAINT } eq '', 'chmod';
346 test 46, $@ =~ /^Insecure dependency/, $@;
1e422769 347
9607fc9c
PP
348 # There is no feature test in $Config{} for truncate,
349 # so we allow for the possibility that it's missing.
b3eb6a9b
GS
350 test 47, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate';
351 test 48, $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
1e422769 352
b3eb6a9b
GS
353 test 49, eval { rename '', $TAINT } eq '', 'rename';
354 test 50, $@ =~ /^Insecure dependency/, $@;
1e422769 355
b3eb6a9b
GS
356 test 51, eval { unlink $TAINT } eq '', 'unlink';
357 test 52, $@ =~ /^Insecure dependency/, $@;
9607fc9c 358
b3eb6a9b
GS
359 test 53, eval { utime $TAINT } eq '', 'utime';
360 test 54, $@ =~ /^Insecure dependency/, $@;
48c036b1 361
1e422769 362 if ($Config{d_chown}) {
b3eb6a9b
GS
363 test 55, eval { chown -1, -1, $TAINT } eq '', 'chown';
364 test 56, $@ =~ /^Insecure dependency/, $@;
1e422769
PP
365 }
366 else {
b3eb6a9b 367 for (55..56) { print "ok $_ # Skipped: chown() is not available\n" }
1e422769
PP
368 }
369
370 if ($Config{d_link}) {
b3eb6a9b
GS
371 test 57, eval { link $TAINT, '' } eq '', 'link';
372 test 58, $@ =~ /^Insecure dependency/, $@;
1e422769
PP
373 }
374 else {
b3eb6a9b 375 for (57..58) { print "ok $_ # Skipped: link() is not available\n" }
1e422769
PP
376 }
377
378 if ($Config{d_symlink}) {
b3eb6a9b
GS
379 test 59, eval { symlink $TAINT, '' } eq '', 'symlink';
380 test 60, $@ =~ /^Insecure dependency/, $@;
1e422769
PP
381 }
382 else {
b3eb6a9b 383 for (59..60) { print "ok $_ # Skipped: symlink() is not available\n" }
1e422769
PP
384 }
385}
386
387# Operations which affect directories can't use tainted data.
388{
b3eb6a9b
GS
389 test 61, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir';
390 test 62, $@ =~ /^Insecure dependency/, $@;
1e422769 391
b3eb6a9b
GS
392 test 63, eval { rmdir $TAINT } eq '', 'rmdir';
393 test 64, $@ =~ /^Insecure dependency/, $@;
9607fc9c 394
b3eb6a9b
GS
395 test 65, eval { chdir $TAINT } eq '', 'chdir';
396 test 66, $@ =~ /^Insecure dependency/, $@;
48c036b1 397
1e422769 398 if ($Config{d_chroot}) {
b3eb6a9b
GS
399 test 67, eval { chroot $TAINT } eq '', 'chroot';
400 test 68, $@ =~ /^Insecure dependency/, $@;
1e422769
PP
401 }
402 else {
b3eb6a9b 403 for (67..68) { print "ok $_ # Skipped: chroot() is not available\n" }
1e422769
PP
404 }
405}
406
407# Some operations using files can't use tainted data.
408{
409 my $foo = "imaginary library" . $TAINT;
b3eb6a9b
GS
410 test 69, eval { require $foo } eq '', 'require';
411 test 70, $@ =~ /^Insecure dependency/, $@;
1e422769
PP
412
413 my $filename = "./taintB$$"; # NB: $filename isn't tainted!
414 END { unlink $filename if defined $filename }
415 $foo = $filename . $TAINT;
416 unlink $filename; # in any case
417
b3eb6a9b
GS
418 test 71, eval { open FOO, $foo } eq '', 'open for read';
419 test 72, $@ eq '', $@; # NB: This should be allowed
9d116dd7
JH
420
421 # Try first new style but allow also old style.
327ccce1
YST
422 # We do not want the whole taint.t to fail
423 # just because Errno possibly failing.
424 test 73, eval('$!{ENOENT}') ||
61ae2fbf
JH
425 $! == 2 || # File not found
426 ($Is_Dos && $! == 22) ||
427 ($^O eq 'mint' && $! == 33);
1e422769 428
b3eb6a9b
GS
429 test 74, eval { open FOO, "> $foo" } eq '', 'open for write';
430 test 75, $@ =~ /^Insecure dependency/, $@;
1e422769
PP
431}
432
433# Commands to the system can't use tainted data
434{
435 my $foo = $TAINT;
436
437 if ($^O eq 'amigaos') {
b3eb6a9b 438 for (76..79) { print "ok $_ # Skipped: open('|') is not available\n" }
1e422769
PP
439 }
440 else {
06eaf0bc 441 test 76, eval { open FOO, "| x$foo" } eq '', 'popen to';
b3eb6a9b 442 test 77, $@ =~ /^Insecure dependency/, $@;
1e422769 443
06eaf0bc 444 test 78, eval { open FOO, "x$foo |" } eq '', 'popen from';
b3eb6a9b 445 test 79, $@ =~ /^Insecure dependency/, $@;
48c036b1 446 }
1e422769 447
b3eb6a9b
GS
448 test 80, eval { exec $TAINT } eq '', 'exec';
449 test 81, $@ =~ /^Insecure dependency/, $@;
9607fc9c 450
b3eb6a9b
GS
451 test 82, eval { system $TAINT } eq '', 'system';
452 test 83, $@ =~ /^Insecure dependency/, $@;
48c036b1 453
1e422769
PP
454 $foo = "*";
455 taint_these $foo;
456
b3eb6a9b
GS
457 test 84, eval { `$echo 1$foo` } eq '', 'backticks';
458 test 85, $@ =~ /^Insecure dependency/, $@;
1e422769
PP
459
460 if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe
b3eb6a9b
GS
461 test 86, join('', eval { glob $foo } ) ne '', 'globbing';
462 test 87, $@ eq '', $@;
1e422769
PP
463 }
464 else {
159f47d9 465 for (86..87) { print "ok $_ # Skipped: This is not VMS\n"; }
1e422769
PP
466 }
467}
468
469# Operations which affect processes can't use tainted data.
470{
b3eb6a9b
GS
471 test 88, eval { kill 0, $TAINT } eq '', 'kill';
472 test 89, $@ =~ /^Insecure dependency/, $@;
1e422769
PP
473
474 if ($Config{d_setpgrp}) {
b3eb6a9b
GS
475 test 90, eval { setpgrp 0, $TAINT } eq '', 'setpgrp';
476 test 91, $@ =~ /^Insecure dependency/, $@;
1e422769
PP
477 }
478 else {
b3eb6a9b 479 for (90..91) { print "ok $_ # Skipped: setpgrp() is not available\n" }
1e422769
PP
480 }
481
482 if ($Config{d_setprior}) {
b3eb6a9b
GS
483 test 92, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority';
484 test 93, $@ =~ /^Insecure dependency/, $@;
1e422769
PP
485 }
486 else {
b3eb6a9b 487 for (92..93) { print "ok $_ # Skipped: setpriority() is not available\n" }
1e422769
PP
488 }
489}
490
491# Some miscellaneous operations can't use tainted data.
492{
493 if ($Config{d_syscall}) {
b3eb6a9b
GS
494 test 94, eval { syscall $TAINT } eq '', 'syscall';
495 test 95, $@ =~ /^Insecure dependency/, $@;
1e422769
PP
496 }
497 else {
b3eb6a9b 498 for (94..95) { print "ok $_ # Skipped: syscall() is not available\n" }
1e422769
PP
499 }
500
501 {
502 my $foo = "x" x 979;
503 taint_these $foo;
504 local *FOO;
505 my $temp = "./taintC$$";
506 END { unlink $temp }
b3eb6a9b 507 test 96, open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
1e422769 508
b3eb6a9b
GS
509 test 97, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl';
510 test 98, $@ =~ /^Insecure dependency/, $@;
1e422769
PP
511
512 if ($Config{d_fcntl}) {
b3eb6a9b
GS
513 test 99, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl';
514 test 100, $@ =~ /^Insecure dependency/, $@;
1e422769
PP
515 }
516 else {
b3eb6a9b 517 for (99..100) { print "ok $_ # Skipped: fcntl() is not available\n" }
1e422769
PP
518 }
519
520 close FOO;
521 }
522}
523
9607fc9c 524# Some tests involving references
1e422769
PP
525{
526 my $foo = 'abc' . $TAINT;
527 my $fooref = \$foo;
b3eb6a9b
GS
528 test 101, not tainted $fooref;
529 test 102, tainted $$fooref;
530 test 103, tainted $foo;
1e422769 531}
54310121
PP
532
533# Some tests involving assignment
534{
535 my $foo = $TAINT0;
536 my $bar = $foo;
b3eb6a9b
GS
537 test 104, all_tainted $foo, $bar;
538 test 105, tainted($foo = $bar);
539 test 106, tainted($bar = $bar);
540 test 107, tainted($bar += $bar);
541 test 108, tainted($bar -= $bar);
542 test 109, tainted($bar *= $bar);
543 test 110, tainted($bar++);
544 test 111, tainted($bar /= $bar);
545 test 112, tainted($bar += 0);
546 test 113, tainted($bar -= 2);
547 test 114, tainted($bar *= -1);
548 test 115, tainted($bar /= 1);
549 test 116, tainted($bar--);
550 test 117, $bar == 0;
54310121 551}
a1f49e72
CS
552
553# Test assignment and return of lists
554{
555 my @foo = ("A", "tainted" . $TAINT, "B");
b3eb6a9b
GS
556 test 118, not tainted $foo[0];
557 test 119, tainted $foo[1];
558 test 120, not tainted $foo[2];
a1f49e72 559 my @bar = @foo;
b3eb6a9b
GS
560 test 121, not tainted $bar[0];
561 test 122, tainted $bar[1];
562 test 123, not tainted $bar[2];
a1f49e72 563 my @baz = eval { "A", "tainted" . $TAINT, "B" };
b3eb6a9b
GS
564 test 124, not tainted $baz[0];
565 test 125, tainted $baz[1];
566 test 126, not tainted $baz[2];
a1f49e72 567 my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
b3eb6a9b
GS
568 test 127, not tainted $plugh[0];
569 test 128, tainted $plugh[1];
570 test 129, not tainted $plugh[2];
a1f49e72 571 my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
b3eb6a9b
GS
572 test 130, not tainted ((&$nautilus)[0]);
573 test 131, tainted ((&$nautilus)[1]);
574 test 132, not tainted ((&$nautilus)[2]);
a1f49e72 575 my @xyzzy = &$nautilus;
b3eb6a9b
GS
576 test 133, not tainted $xyzzy[0];
577 test 134, tainted $xyzzy[1];
578 test 135, not tainted $xyzzy[2];
a1f49e72 579 my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
b3eb6a9b
GS
580 test 136, not tainted ((&$red_october)[0]);
581 test 137, tainted ((&$red_october)[1]);
582 test 138, not tainted ((&$red_october)[2]);
a1f49e72 583 my @corge = &$red_october;
b3eb6a9b
GS
584 test 139, not tainted $corge[0];
585 test 140, tainted $corge[1];
586 test 141, not tainted $corge[2];
a1f49e72 587}
fb73857a
PP
588
589# Test for system/library calls returning string data of dubious origin.
590{
591 # No reliable %Config check for getpw*
5dec6e32 592 if (eval { setpwent(); getpwent() }) {
fb73857a
PP
593 setpwent();
594 my @getpwent = getpwent();
595 die "getpwent: $!\n" unless (@getpwent);
b3eb6a9b 596 test 142,( not tainted $getpwent[0]
2959b6e3 597 and tainted $getpwent[1]
fb73857a
PP
598 and not tainted $getpwent[2]
599 and not tainted $getpwent[3]
600 and not tainted $getpwent[4]
601 and not tainted $getpwent[5]
3972a534 602 and tainted $getpwent[6] # ge?cos
fb73857a 603 and not tainted $getpwent[7]
3972a534 604 and tainted $getpwent[8]); # shell
fb73857a
PP
605 endpwent();
606 } else {
b3eb6a9b 607 for (142) { print "ok $_ # Skipped: getpwent() is not available\n" }
fb73857a
PP
608 }
609
610 if ($Config{d_readdir}) { # pretty hard to imagine not
611 local(*D);
612 opendir(D, "op") or die "opendir: $!\n";
613 my $readdir = readdir(D);
b3eb6a9b 614 test 143, tainted $readdir;
fb73857a
PP
615 closedir(OP);
616 } else {
b3eb6a9b 617 for (143) { print "ok $_ # Skipped: readdir() is not available\n" }
fb73857a
PP
618 }
619
620 if ($Config{d_readlink} && $Config{d_symlink}) {
621 my $symlink = "sl$$";
622 unlink($symlink);
dc459aad
JH
623 my $sl = "/something/naughty";
624 # it has to be a real path on Mac OS
625 $sl = MacPerl::MakePath((MacPerl::Volumes())[0]) if $Is_MacOS;
626 symlink($sl, $symlink) or die "symlink: $!\n";
fb73857a 627 my $readlink = readlink($symlink);
b3eb6a9b 628 test 144, tainted $readlink;
fb73857a
PP
629 unlink($symlink);
630 } else {
b3eb6a9b 631 for (144) { print "ok $_ # Skipped: readlink() or symlink() is not available\n"; }
fb73857a
PP
632 }
633}
634
635# test bitwise ops (regression bug)
636{
637 my $why = "y";
638 my $j = "x" | $why;
b3eb6a9b 639 test 145, not tainted $j;
fb73857a
PP
640 $why = $TAINT."y";
641 $j = "x" | $why;
b3eb6a9b 642 test 146, tainted $j;
fb73857a
PP
643}
644
48c036b1
GS
645# test target of substitution (regression bug)
646{
647 my $why = $TAINT."y";
648 $why =~ s/y/z/;
b3eb6a9b 649 test 147, tainted $why;
48c036b1
GS
650
651 my $z = "[z]";
652 $why =~ s/$z/zee/;
b3eb6a9b 653 test 148, tainted $why;
48c036b1
GS
654
655 $why =~ s/e/'-'.$$/ge;
b3eb6a9b 656 test 149, tainted $why;
48c036b1 657}
d929ce6f
JH
658
659# test shmread
660{
c9f931b8
JH
661 unless ($ipcsysv) {
662 print "ok 150 # skipped: no IPC::SysV\n";
663 last;
664 }
be3174d2 665 if ($Config{'extensions'} =~ /\bIPC\/SysV\b/ && $Config{d_shm}) {
b9d1c439 666 no strict 'subs';
d929ce6f
JH
667 my $sent = "foobar";
668 my $rcvd;
669 my $size = 2000;
c2e66d9e
GS
670 my $id = shmget(IPC_PRIVATE, $size, S_IRWXU);
671
41d6edb2
JH
672 if (defined $id) {
673 if (shmwrite($id, $sent, 0, 60)) {
674 if (shmread($id, $rcvd, 0, 60)) {
d929ce6f
JH
675 substr($rcvd, index($rcvd, "\0")) = '';
676 } else {
677 warn "# shmread failed: $!\n";
678 }
679 } else {
680 warn "# shmwrite failed: $!\n";
681 }
c2e66d9e 682 shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n";
41d6edb2
JH
683 } else {
684 warn "# shmget failed: $!\n";
d929ce6f
JH
685 }
686
687 if ($rcvd eq $sent) {
688 test 150, tainted $rcvd;
689 } else {
690 print "ok 150 # Skipped: SysV shared memory operation failed\n";
691 }
692 } else {
41d6edb2 693 print "ok 150 # Skipped: SysV shared memory is not available\n";
d929ce6f
JH
694 }
695}
41d6edb2
JH
696
697# test msgrcv
698{
c9f931b8
JH
699 unless ($ipcsysv) {
700 print "ok 151 # skipped: no IPC::SysV\n";
701 last;
702 }
be3174d2 703 if ($Config{'extensions'} =~ /\bIPC\/SysV\b/ && $Config{d_msg}) {
b9d1c439 704 no strict 'subs';
41d6edb2
JH
705 my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
706
707 my $sent = "message";
708 my $type_sent = 1234;
709 my $rcvd;
710 my $type_rcvd;
711
712 if (defined $id) {
ddc3217d
JH
713 if (msgsnd($id, pack("l! a*", $type_sent, $sent), IPC_NOWAIT)) {
714 if (msgrcv($id, $rcvd, 60, 0, IPC_NOWAIT)) {
41d6edb2
JH
715 ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd);
716 } else {
ddc3217d 717 warn "# msgrcv failed: $!\n";
41d6edb2
JH
718 }
719 } else {
ddc3217d 720 warn "# msgsnd failed: $!\n";
41d6edb2 721 }
c2e66d9e 722 msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n";
41d6edb2
JH
723 } else {
724 warn "# msgget failed\n";
725 }
726
727 if ($rcvd eq $sent && $type_sent == $type_rcvd) {
728 test 151, tainted $rcvd;
729 } else {
730 print "ok 151 # Skipped: SysV message queue operation failed\n";
731 }
732 } else {
733 print "ok 151 # Skipped: SysV message queues are not available\n";
734 }
735}
736
3887d568
AP
737{
738 # bug id 20001004.006
739
dc459aad 740 open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
3887d568
AP
741 local $/;
742 my $a = <IN>;
743 my $b = <IN>;
744 print "not " unless tainted($a) && tainted($b) && !defined($b);
745 print "ok 152\n";
27c9684d 746 close IN;
3887d568 747}
27c9684d
AP
748
749{
750 # bug id 20001004.007
751
dc459aad 752 open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
27c9684d
AP
753 my $a = <IN>;
754
755 my $c = { a => 42,
756 b => $a };
757 print "not " unless !tainted($c->{a}) && tainted($c->{b});
758 print "ok 153\n";
759
760 my $d = { a => $a,
761 b => 42 };
762 print "not " unless tainted($d->{a}) && !tainted($d->{b});
763 print "ok 154\n";
764
765 my $e = { a => 42,
766 b => { c => $a, d => 42 } };
767 print "not " unless !tainted($e->{a}) &&
768 !tainted($e->{b}) &&
769 tainted($e->{b}->{c}) &&
770 !tainted($e->{b}->{d});
771 print "ok 155\n";
772
773 close IN;
774}
775
b94c04ac
JH
776{
777 # bug id 20010519.003
778
248c32c0
JH
779 BEGIN {
780 use vars qw($has_fcntl);
781 eval { require Fcntl; import Fcntl; };
782 unless ($@) {
783 $has_fcntl = 1;
784 }
785 }
b94c04ac 786
248c32c0
JH
787 unless ($has_fcntl) {
788 for (156..173) {
789 print "ok $_ # Skip: no Fcntl (no dynaloading?)\n";
790 }
791 } else {
792 my $evil = "foo" . $TAINT;
793
794 eval { sysopen(my $ro, $evil, &O_RDONLY) };
795 test 156, $@ !~ /^Insecure dependency/, $@;
796
797 eval { sysopen(my $wo, $evil, &O_WRONLY) };
798 test 157, $@ =~ /^Insecure dependency/, $@;
799
800 eval { sysopen(my $rw, $evil, &O_RDWR) };
801 test 158, $@ =~ /^Insecure dependency/, $@;
802
803 eval { sysopen(my $ap, $evil, &O_APPEND) };
804 test 159, $@ =~ /^Insecure dependency/, $@;
805
806 eval { sysopen(my $cr, $evil, &O_CREAT) };
807 test 160, $@ =~ /^Insecure dependency/, $@;
808
809 eval { sysopen(my $tr, $evil, &O_TRUNC) };
810 test 161, $@ =~ /^Insecure dependency/, $@;
811
812 eval { sysopen(my $ro, "foo", &O_RDONLY | $evil) };
813 test 162, $@ !~ /^Insecure dependency/, $@;
814
815 eval { sysopen(my $wo, "foo", &O_WRONLY | $evil) };
816 test 163, $@ =~ /^Insecure dependency/, $@;
817
818 eval { sysopen(my $rw, "foo", &O_RDWR | $evil) };
819 test 164, $@ =~ /^Insecure dependency/, $@;
820
821 eval { sysopen(my $ap, "foo", &O_APPEND | $evil) };
822 test 165, $@ =~ /^Insecure dependency/, $@;
823
824 eval { sysopen(my $cr, "foo", &O_CREAT | $evil) };
825 test 166, $@ =~ /^Insecure dependency/, $@;
826
827 eval { sysopen(my $tr, "foo", &O_TRUNC | $evil) };
828 test 167, $@ =~ /^Insecure dependency/, $@;
829
830 eval { sysopen(my $ro, "foo", &O_RDONLY, $evil) };
831 test 168, $@ !~ /^Insecure dependency/, $@;
832
833 eval { sysopen(my $wo, "foo", &O_WRONLY, $evil) };
834 test 169, $@ =~ /^Insecure dependency/, $@;
835
836 eval { sysopen(my $rw, "foo", &O_RDWR, $evil) };
837 test 170, $@ =~ /^Insecure dependency/, $@;
838
839 eval { sysopen(my $ap, "foo", &O_APPEND, $evil) };
840 test 171, $@ =~ /^Insecure dependency/, $@;
841
842 eval { sysopen(my $cr, "foo", &O_CREAT, $evil) };
843 test 172, $@ =~ /^Insecure dependency/, $@;
844
845 eval { sysopen(my $tr, "foo", &O_TRUNC, $evil) };
846 test 173, $@ =~ /^Insecure dependency/, $@;
847
848 unlink("foo"); # not unlink($evil), because that would fail...
849 }
b94c04ac
JH
850}
851
65811bc3
JH
852{
853 # bug 20010526.004
854
855 use warnings;
856
bbd7eb8a 857 local $SIG{__WARN__} = sub { print "not " };
65811bc3
JH
858
859 sub fmi {
860 my $divnum = shift()/1;
861 sprintf("%1.1f\n", $divnum);
862 }
863
864 fmi(21 . $TAINT);
865 fmi(37);
866 fmi(248);
867
868 print "ok 174\n";
869}
870
9e1b5a4e
A
871
872{
873 # Bug ID 20010730.010
874
875 my $i = 0;
876
877 sub Tie::TIESCALAR {
878 my $class = shift;
879 my $arg = shift;
880
881 bless \$arg => $class;
882 }
883
884 sub Tie::FETCH {
885 $i ++;
886 ${$_ [0]}
887 }
888
889
890 package main;
891
892 my $bar = "The Big Bright Green Pleasure Machine";
893 taint_these $bar;
894 tie my ($foo), Tie => $bar;
895
896 my $baz = $foo;
897
898 print $i == 1 ? "ok 175\n" : "not ok 175\n"
899
900}
901
8852b6d2
JH
902{
903 # Check that all environment variables are tainted.
904 my @untainted;
905 while (my ($k, $v) = each %ENV) {
906 if (!tainted($v) &&
eb25aaf6
HS
907 # These we have explicitly untainted or set earlier.
908 $k !~ /^(BASH_ENV|CDPATH|ENV|IFS|PATH|PERL_CORE|TEMP|TERM|TMP)$/) {
8852b6d2
JH
909 push @untainted, "# '$k' = '$v'\n";
910 }
911 }
912 print @untainted == 0 ? "ok 176\n" : "not ok 176\n";
913 print "# untainted:\n", @untainted if @untainted;
914}
9e1b5a4e
A
915
916
9aa05f58 917ok( ${^TAINT} == 1, '$^TAINT is on' );
7c36658b
MS
918
919eval { ${^TAINT} = 0 };
920ok( ${^TAINT}, '$^TAINT is not assignable' );
921ok( $@ =~ /^Modification of a read-only value attempted/,
c212f17f 922 'Assigning to ${^TAINT} fails' );
7c36658b 923
e08e52cf
AMS
924{
925 # bug 20011111.105
926
927 my $re1 = qr/x$TAINT/;
928 test 180, tainted $re1;
929
930 my $re2 = qr/^$re1\z/;
931 test 181, tainted $re2;
932
933 my $re3 = "$re2";
934 test 182, tainted $re3;
935}
52a55424 936
d5b53b20
NIS
937if ($Is_MSWin32) {
938 print "ok 183 # Skipped: system {} has different semantics\n";
939}
940else
52a55424
RG
941{
942 # bug 20010221.005
943 local $ENV{PATH} .= $TAINT;
944 eval { system { "echo" } "/arg0", "arg1" };
945 test 183, $@ =~ /^Insecure \$ENV/;
946}
161720b2 947if ($Is_VMS) {
0304a67e 948 for (184..205) {print "not ok $_ # TODO tainted %ENV warning occludes tainted arguments warning\n";}
161720b2
CB
949}
950else
bbd7eb8a
RD
951{
952 # bug 20020208.005 plus some extras
953 # single arg exec/system are tests 80-83
06bd1802 954 my $err = qr/^Insecure dependency/ ;
bbd7eb8a
RD
955 test 184, eval { exec $TAINT, $TAINT } eq '', 'exec';
956 test 185, $@ =~ $err, $@;
957 test 186, eval { exec $TAINT $TAINT } eq '', 'exec';
958 test 187, $@ =~ $err, $@;
959 test 188, eval { exec $TAINT $TAINT, $TAINT } eq '', 'exec';
960 test 189, $@ =~ $err, $@;
961 test 190, eval { exec $TAINT 'notaint' } eq '', 'exec';
962 test 191, $@ =~ $err, $@;
963 test 192, eval { exec {'notaint'} $TAINT } eq '', 'exec';
964 test 193, $@ =~ $err, $@;
965
966 test 194, eval { system $TAINT, $TAINT } eq '', 'system';
967 test 195, $@ =~ $err, $@;
159f47d9 968 test 196, eval { system $TAINT $TAINT } eq '', 'system';
bbd7eb8a 969 test 197, $@ =~ $err, $@;
159f47d9 970 test 198, eval { system $TAINT $TAINT, $TAINT } eq '', 'system';
bbd7eb8a 971 test 199, $@ =~ $err, $@;
159f47d9 972 test 200, eval { system $TAINT 'notaint' } eq '', 'system';
bbd7eb8a 973 test 201, $@ =~ $err, $@;
159f47d9 974 test 202, eval { system {'notaint'} $TAINT } eq '', 'system';
bbd7eb8a 975 test 203, $@ =~ $err, $@;
159f47d9
RGS
976
977 eval { system("lskdfj does not exist","with","args"); };
978 test 204, $@ eq '';
63c6dcc1
JH
979 if ($Is_MacOS) {
980 print "ok 205 # no exec()\n";
981 } else {
982 eval { exec("lskdfj does not exist","with","args"); };
983 test 205, $@ eq '';
984 }
6c8794e1
JH
985
986 # If you add tests here update also the above skip block for VMS.
bbd7eb8a 987}
a8c7c11a
HS
988
989{
990 # [ID 20020704.001] taint propagation failure
991 use re 'taint';
992 $TAINT =~ /(.*)/;
993 test 206, tainted(my $foo = $1);
994}
7b756e0a
RGS
995
996{
c038024b
RGS
997 # [perl #24291] this used to dump core
998 our %nonmagicalenv = ( PATH => "util" );
7b756e0a
RGS
999 local *ENV = \%nonmagicalenv;
1000 eval { system("lskdfj"); };
c038024b
RGS
1001 test 207, $@ =~ /^%ENV is aliased to another variable while running with -T switch/;
1002 local *ENV = *nonmagicalenv;
7b756e0a 1003 eval { system("lskdfj"); };
c038024b 1004 test 208, $@ =~ /^%ENV is aliased to %nonmagicalenv while running with -T switch/;
7b756e0a 1005}
0b4182de
RD
1006{
1007 # [perl #24248]
1008 $TAINT =~ /(.*)/;
1009 test 209, !tainted($1);
1010 my $notaint = $1;
1011 test 210, !tainted($notaint);
1012
1013 my $l;
1014 $notaint =~ /($notaint)/;
1015 $l = $1;
1016 test 211, !tainted($1);
1017 test 212, !tainted($l);
1018 $notaint =~ /($TAINT)/;
1019 $l = $1;
1020 test 213, tainted($1);
1021 test 214, tainted($l);
1022
1023 $TAINT =~ /($notaint)/;
1024 $l = $1;
1025 test 215, !tainted($1);
1026 test 216, !tainted($l);
1027 $TAINT =~ /($TAINT)/;
1028 $l = $1;
1029 test 217, tainted($1);
1030 test 218, tainted($l);
1031
1032 my $r;
1033 ($r = $TAINT) =~ /($notaint)/;
1034 test 219, !tainted($1);
1035 ($r = $TAINT) =~ /($TAINT)/;
1036 test 220, tainted($1);
3511154c
DM
1037
1038 # [perl #24674]
1039 # accessing $^O shoudn't taint it as a side-effect;
1040 # assigning tainted data to it is now an error
1041
1042 test 221, !tainted($^O);
1043 if (!$^X) { } elsif ($^O eq 'bar') { }
1044 test 222, !tainted($^O);
1045 eval '$^O = $^X';
1046 test 223, $@ =~ /Insecure dependency in/;
0b4182de 1047}