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