BEGIN {
chdir 't' if -d 't';
- @INC = '../lib' if -d '../lib';
+ @INC = '../lib';
}
use strict;
use Config;
+my $test = 177;
+sub ok ($;$) {
+ my($ok, $name) = @_;
+
+ # You have to do it this way or VMS will get confused.
+ print $ok ? "ok $test - $name\n" : "not ok $test - $name\n";
+
+ printf "# Failed test at line %d\n", (caller)[2] unless $ok;
+
+ $test++;
+ return $ok;
+}
+
+
+$| = 1;
+
+use vars qw($ipcsysv); # did we manage to load IPC::SysV?
+
+BEGIN {
+ if ($^O eq 'VMS' && !defined($Config{d_setenv})) {
+ $ENV{PATH} = $ENV{PATH};
+ $ENV{TERM} = $ENV{TERM} ne ''? $ENV{TERM} : 'dummy';
+ }
+ if ($Config{'extensions'} =~ /\bIPC\/SysV\b/
+ && ($Config{d_shm} || $Config{d_msg})) {
+ eval { require IPC::SysV };
+ unless ($@) {
+ $ipcsysv++;
+ IPC::SysV->import(qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU));
+ }
+ }
+}
+
my $Is_VMS = $^O eq 'VMS';
my $Is_MSWin32 = $^O eq 'MSWin32';
+my $Is_NetWare = $^O eq 'NetWare';
my $Is_Dos = $^O eq 'dos';
+my $Is_Cygwin = $^O eq 'cygwin';
my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' :
- $Is_MSWin32 ? '.\perl' : './perl';
+ ($Is_MSWin32 ? '.\perl' :
+ ($Is_NetWare ? 'perl' : './perl'));
my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
if ($Is_VMS) {
}
eval <<EndOfCleanup;
END {
- \$ENV{PATH} = '';
+ \$ENV{PATH} = '' if $Config{d_setenv};
warn "# Note: logical name 'PATH' may have been deleted\n";
- @ENV{keys %old} = values %old;
+ \@ENV{keys %old} = values %old;
}
EndOfCleanup
}
}
# We need an external program to call.
-my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : "./echo$$");
+my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : ($Is_NetWare ? "echo$$" : "./echo$$"));
END { unlink $ECHO }
open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
print PROG 'print "@ARGV\n"', "\n";
close PROG;
my $echo = "$Invoke_Perl $ECHO";
-print "1..149\n";
+print "1..203\n";
# First, let's make sure that Perl is checking the dangerous
# environment variables. Maybe they aren't set yet, so we'll
delete @ENV{@MoreEnv};
$ENV{TERM} = 'dumb';
+ if ($Is_Cygwin && ! -f 'cygwin1.dll') {
+ system("/usr/bin/cp /usr/bin/cygwin1.dll .") &&
+ die "$0: failed to cp cygwin1.dll: $!\n";
+ END { unlink "cygwin1.dll" } # yes, done for all platforms...
+ }
+
test 1, eval { `$echo 1` } eq "1\n";
- if ($Is_MSWin32 || $Is_VMS || $Is_Dos) {
+ if ($Is_MSWin32 || $Is_NetWare || $Is_VMS || $Is_Dos) {
print "# Environment tainting tests skipped\n";
for (2..5) { print "ok $_\n" }
}
}
my $tmp;
- if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_Dos) {
+ if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_NetWare || $Is_Dos) {
print "# all directories are writeable\n";
}
else {
$tmp = (grep { defined and -d and (stat _)[2] & 2 }
- qw(/tmp /var/tmp /usr/tmp /sys$scratch),
+ qw(sys$scratch /tmp /var/tmp /usr/tmp),
@ENV{qw(TMP TEMP)})[0]
or print "# can't find world-writeable directory to test PATH\n";
}
# Globs should be forbidden, except under VMS,
# which doesn't spawn an external program.
-if ($Is_VMS) {
+if (1 # built-in glob
+ or $Is_VMS) {
for (35..36) { print "ok $_\n"; }
}
else {
test 71, eval { open FOO, $foo } eq '', 'open for read';
test 72, $@ eq '', $@; # NB: This should be allowed
- test 73, $! == 2 || ($Is_Dos && $! == 22); # File not found
+
+ # Try first new style but allow also old style.
+ # We do not want the whole taint.t to fail
+ # just because Errno possibly failing.
+ test 73, eval('$!{ENOENT}') ||
+ $! == 2 || # File not found
+ ($Is_Dos && $! == 22) ||
+ ($^O eq 'mint' && $! == 33);
test 74, eval { open FOO, "> $foo" } eq '', 'open for write';
test 75, $@ =~ /^Insecure dependency/, $@;
for (76..79) { print "ok $_ # Skipped: open('|') is not available\n" }
}
else {
- test 76, eval { open FOO, "| $foo" } eq '', 'popen to';
+ test 76, eval { open FOO, "| x$foo" } eq '', 'popen to';
test 77, $@ =~ /^Insecure dependency/, $@;
- test 78, eval { open FOO, "$foo |" } eq '', 'popen from';
+ test 78, eval { open FOO, "x$foo |" } eq '', 'popen from';
test 79, $@ =~ /^Insecure dependency/, $@;
}
# Test for system/library calls returning string data of dubious origin.
{
# No reliable %Config check for getpw*
- if (eval { setpwent(); getpwent(); 1 }) {
+ if (eval { setpwent(); getpwent() }) {
setpwent();
my @getpwent = getpwent();
die "getpwent: $!\n" unless (@getpwent);
test 142,( not tainted $getpwent[0]
- and not tainted $getpwent[1]
+ and tainted $getpwent[1]
and not tainted $getpwent[2]
and not tainted $getpwent[3]
and not tainted $getpwent[4]
and not tainted $getpwent[5]
- and tainted $getpwent[6] # gecos
+ and tainted $getpwent[6] # ge?cos
and not tainted $getpwent[7]
- and not tainted $getpwent[8]);
+ and tainted $getpwent[8]); # shell
endpwent();
} else {
for (142) { print "ok $_ # Skipped: getpwent() is not available\n" }
$why =~ s/e/'-'.$$/ge;
test 149, tainted $why;
}
+
+# test shmread
+{
+ unless ($ipcsysv) {
+ print "ok 150 # skipped: no IPC::SysV\n";
+ last;
+ }
+ if ($Config{'extensions'} =~ /\bIPC\/SysV\b/ && $Config{d_shm}) {
+ no strict 'subs';
+ my $sent = "foobar";
+ my $rcvd;
+ my $size = 2000;
+ my $id = shmget(IPC_PRIVATE, $size, S_IRWXU);
+
+ if (defined $id) {
+ if (shmwrite($id, $sent, 0, 60)) {
+ if (shmread($id, $rcvd, 0, 60)) {
+ substr($rcvd, index($rcvd, "\0")) = '';
+ } else {
+ warn "# shmread failed: $!\n";
+ }
+ } else {
+ warn "# shmwrite failed: $!\n";
+ }
+ shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n";
+ } else {
+ warn "# shmget failed: $!\n";
+ }
+
+ if ($rcvd eq $sent) {
+ test 150, tainted $rcvd;
+ } else {
+ print "ok 150 # Skipped: SysV shared memory operation failed\n";
+ }
+ } else {
+ print "ok 150 # Skipped: SysV shared memory is not available\n";
+ }
+}
+
+# test msgrcv
+{
+ unless ($ipcsysv) {
+ print "ok 151 # skipped: no IPC::SysV\n";
+ last;
+ }
+ if ($Config{'extensions'} =~ /\bIPC\/SysV\b/ && $Config{d_msg}) {
+ no strict 'subs';
+ my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
+
+ my $sent = "message";
+ my $type_sent = 1234;
+ my $rcvd;
+ my $type_rcvd;
+
+ if (defined $id) {
+ if (msgsnd($id, pack("l! a*", $type_sent, $sent), 0)) {
+ if (msgrcv($id, $rcvd, 60, 0, 0)) {
+ ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd);
+ } else {
+ warn "# msgrcv failed\n";
+ }
+ } else {
+ warn "# msgsnd failed\n";
+ }
+ msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n";
+ } else {
+ warn "# msgget failed\n";
+ }
+
+ if ($rcvd eq $sent && $type_sent == $type_rcvd) {
+ test 151, tainted $rcvd;
+ } else {
+ print "ok 151 # Skipped: SysV message queue operation failed\n";
+ }
+ } else {
+ print "ok 151 # Skipped: SysV message queues are not available\n";
+ }
+}
+
+{
+ # bug id 20001004.006
+
+ open IN, "./TEST" or warn "$0: cannot read ./TEST: $!" ;
+ local $/;
+ my $a = <IN>;
+ my $b = <IN>;
+ print "not " unless tainted($a) && tainted($b) && !defined($b);
+ print "ok 152\n";
+ close IN;
+}
+
+{
+ # bug id 20001004.007
+
+ open IN, "./TEST" or warn "$0: cannot read ./TEST: $!" ;
+ my $a = <IN>;
+
+ my $c = { a => 42,
+ b => $a };
+ print "not " unless !tainted($c->{a}) && tainted($c->{b});
+ print "ok 153\n";
+
+ my $d = { a => $a,
+ b => 42 };
+ print "not " unless tainted($d->{a}) && !tainted($d->{b});
+ print "ok 154\n";
+
+ my $e = { a => 42,
+ b => { c => $a, d => 42 } };
+ print "not " unless !tainted($e->{a}) &&
+ !tainted($e->{b}) &&
+ tainted($e->{b}->{c}) &&
+ !tainted($e->{b}->{d});
+ print "ok 155\n";
+
+ close IN;
+}
+
+{
+ # bug id 20010519.003
+
+ BEGIN {
+ use vars qw($has_fcntl);
+ eval { require Fcntl; import Fcntl; };
+ unless ($@) {
+ $has_fcntl = 1;
+ }
+ }
+
+ unless ($has_fcntl) {
+ for (156..173) {
+ print "ok $_ # Skip: no Fcntl (no dynaloading?)\n";
+ }
+ } else {
+ my $evil = "foo" . $TAINT;
+
+ eval { sysopen(my $ro, $evil, &O_RDONLY) };
+ test 156, $@ !~ /^Insecure dependency/, $@;
+
+ eval { sysopen(my $wo, $evil, &O_WRONLY) };
+ test 157, $@ =~ /^Insecure dependency/, $@;
+
+ eval { sysopen(my $rw, $evil, &O_RDWR) };
+ test 158, $@ =~ /^Insecure dependency/, $@;
+
+ eval { sysopen(my $ap, $evil, &O_APPEND) };
+ test 159, $@ =~ /^Insecure dependency/, $@;
+
+ eval { sysopen(my $cr, $evil, &O_CREAT) };
+ test 160, $@ =~ /^Insecure dependency/, $@;
+
+ eval { sysopen(my $tr, $evil, &O_TRUNC) };
+ test 161, $@ =~ /^Insecure dependency/, $@;
+
+ eval { sysopen(my $ro, "foo", &O_RDONLY | $evil) };
+ test 162, $@ !~ /^Insecure dependency/, $@;
+
+ eval { sysopen(my $wo, "foo", &O_WRONLY | $evil) };
+ test 163, $@ =~ /^Insecure dependency/, $@;
+
+ eval { sysopen(my $rw, "foo", &O_RDWR | $evil) };
+ test 164, $@ =~ /^Insecure dependency/, $@;
+
+ eval { sysopen(my $ap, "foo", &O_APPEND | $evil) };
+ test 165, $@ =~ /^Insecure dependency/, $@;
+
+ eval { sysopen(my $cr, "foo", &O_CREAT | $evil) };
+ test 166, $@ =~ /^Insecure dependency/, $@;
+
+ eval { sysopen(my $tr, "foo", &O_TRUNC | $evil) };
+ test 167, $@ =~ /^Insecure dependency/, $@;
+
+ eval { sysopen(my $ro, "foo", &O_RDONLY, $evil) };
+ test 168, $@ !~ /^Insecure dependency/, $@;
+
+ eval { sysopen(my $wo, "foo", &O_WRONLY, $evil) };
+ test 169, $@ =~ /^Insecure dependency/, $@;
+
+ eval { sysopen(my $rw, "foo", &O_RDWR, $evil) };
+ test 170, $@ =~ /^Insecure dependency/, $@;
+
+ eval { sysopen(my $ap, "foo", &O_APPEND, $evil) };
+ test 171, $@ =~ /^Insecure dependency/, $@;
+
+ eval { sysopen(my $cr, "foo", &O_CREAT, $evil) };
+ test 172, $@ =~ /^Insecure dependency/, $@;
+
+ eval { sysopen(my $tr, "foo", &O_TRUNC, $evil) };
+ test 173, $@ =~ /^Insecure dependency/, $@;
+
+ unlink("foo"); # not unlink($evil), because that would fail...
+ }
+}
+
+{
+ # bug 20010526.004
+
+ use warnings;
+
+ local $SIG{__WARN__} = sub { print "not " };
+
+ sub fmi {
+ my $divnum = shift()/1;
+ sprintf("%1.1f\n", $divnum);
+ }
+
+ fmi(21 . $TAINT);
+ fmi(37);
+ fmi(248);
+
+ print "ok 174\n";
+}
+
+
+{
+ # Bug ID 20010730.010
+
+ my $i = 0;
+
+ sub Tie::TIESCALAR {
+ my $class = shift;
+ my $arg = shift;
+
+ bless \$arg => $class;
+ }
+
+ sub Tie::FETCH {
+ $i ++;
+ ${$_ [0]}
+ }
+
+
+ package main;
+
+ my $bar = "The Big Bright Green Pleasure Machine";
+ taint_these $bar;
+ tie my ($foo), Tie => $bar;
+
+ my $baz = $foo;
+
+ print $i == 1 ? "ok 175\n" : "not ok 175\n"
+
+}
+
+{
+ # Check that all environment variables are tainted.
+ my @untainted;
+ while (my ($k, $v) = each %ENV) {
+ if (!tainted($v) &&
+ # These we have untainted explicitly earlier.
+ $k !~ /^(BASH_ENV|CDPATH|ENV|IFS|PATH|TEMP|TERM|TMP)$/) {
+ push @untainted, "# '$k' = '$v'\n";
+ }
+ }
+ print @untainted == 0 ? "ok 176\n" : "not ok 176\n";
+ print "# untainted:\n", @untainted if @untainted;
+}
+
+
+ok( ${^TAINT}, '$^TAINT is on' );
+
+eval { ${^TAINT} = 0 };
+ok( ${^TAINT}, '$^TAINT is not assignable' );
+ok( $@ =~ /^Modification of a read-only value attempted/,
+ 'Assigning to ${^TAINT} fails' );
+
+{
+ # bug 20011111.105
+
+ my $re1 = qr/x$TAINT/;
+ test 180, tainted $re1;
+
+ my $re2 = qr/^$re1\z/;
+ test 181, tainted $re2;
+
+ my $re3 = "$re2";
+ test 182, tainted $re3;
+}
+
+if ($Is_MSWin32) {
+ print "ok 183 # Skipped: system {} has different semantics\n";
+}
+else
+{
+ # bug 20010221.005
+ local $ENV{PATH} .= $TAINT;
+ eval { system { "echo" } "/arg0", "arg1" };
+ test 183, $@ =~ /^Insecure \$ENV/;
+}
+if ($Is_VMS) {
+ for (184..203) {print "not ok $_ # TODO tainted %ENV warning occludes tainted arguments warning\n";}
+}
+else
+{
+ # bug 20020208.005 plus some extras
+ # single arg exec/system are tests 80-83
+ use if $] lt '5.009', warnings => FATAL => 'taint';
+ my $err = $] ge '5.009' ? qr/^Insecure dependency/
+ : qr/^Use of tainted arguments/;
+ test 184, eval { exec $TAINT, $TAINT } eq '', 'exec';
+ test 185, $@ =~ $err, $@;
+ test 186, eval { exec $TAINT $TAINT } eq '', 'exec';
+ test 187, $@ =~ $err, $@;
+ test 188, eval { exec $TAINT $TAINT, $TAINT } eq '', 'exec';
+ test 189, $@ =~ $err, $@;
+ test 190, eval { exec $TAINT 'notaint' } eq '', 'exec';
+ test 191, $@ =~ $err, $@;
+ test 192, eval { exec {'notaint'} $TAINT } eq '', 'exec';
+ test 193, $@ =~ $err, $@;
+
+ test 194, eval { system $TAINT, $TAINT } eq '', 'system';
+ test 195, $@ =~ $err, $@;
+ test 196, eval { system $TAINT $TAINT } eq '', 'exec';
+ test 197, $@ =~ $err, $@;
+ test 198, eval { system $TAINT $TAINT, $TAINT } eq '', 'exec';
+ test 199, $@ =~ $err, $@;
+ test 200, eval { system $TAINT 'notaint' } eq '', 'exec';
+ test 201, $@ =~ $err, $@;
+ test 202, eval { system {'notaint'} $TAINT } eq '', 'exec';
+ test 203, $@ =~ $err, $@;
+}