This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[inseparable changes from match from perl-5.003_91 to perl-5.003_92]
[perl5.git] / t / op / taint.t
CommitLineData
1e422769 1#!./perl -T
2#
3# Taint tests by Tom Phoenix <rootbeer@teleport.com>.
4#
5# I don't claim to know all about tainting. If anyone sees
9607fc9c 6# tests that I've missed here, please add them. But this is
1e422769 7# better than having no tests at all, right?
8#
9
10BEGIN {
11 chdir 't' if -d 't';
12 @INC = '../lib' if -d '../lib';
13}
14
15use strict;
16use Config;
17
18my $Is_VMS = $^O eq 'VMS';
19my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' : './perl';
20if ($Is_VMS) {
21 eval <<EndOfCleanup;
22 END {
23 \$ENV{PATH} = '';
24 warn "# Note: logical name 'PATH' may have been deleted\n";
25 \$ENV{IFS} = "$ENV{IFS}";
26 \$ENV{'DCL\$PATH'} = "$ENV{'DCL$PATH'}";
27 }
28EndOfCleanup
29}
30
31# Sources of taint:
32# The empty tainted value, for tainting strings
33my $TAINT = substr($^X, 0, 0);
34# A tainted zero, useful for tainting numbers
35my $TAINT0 = 0 + $TAINT;
36
37# This taints each argument passed. All must be lvalues.
38# Side effect: It also stringifies them. :-(
39sub taint_these (@) {
40 for (@_) { $_ .= $TAINT }
41}
42
43# How to identify taint when you see it
44sub any_tainted (@) {
45 not eval { join("",@_), kill 0; 1 };
46}
47sub tainted ($) {
48 any_tainted @_;
49}
50sub all_tainted (@) {
51 for (@_) { return 0 unless tainted $_ }
52 1;
53}
54
55sub test ($$;$) {
56 my($serial, $boolean, $diag) = @_;
57 if ($boolean) {
58 print "ok $serial\n";
59 } else {
60 print "not ok $serial\n";
61 for (split m/^/m, $diag) {
62 print "# $_";
63 }
9607fc9c 64 print "\n" unless
1e422769 65 $diag eq ''
66 or substr($diag, -1) eq "\n";
67 }
68}
69
70# We need an external program to call.
71my $ECHO = "./echo$$";
72END { unlink $ECHO }
73open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
74print PROG 'print "@ARGV\n"', "\n";
75close PROG;
76my $echo = "$Invoke_Perl $ECHO";
77
9607fc9c 78print "1..98\n";
1e422769 79
80# First, let's make sure that Perl is checking the dangerous
81# environment variables. Maybe they aren't set yet, so we'll
82# taint them ourselves.
83{
84 $ENV{'DCL$PATH'} = '' if $Is_VMS;
85
86 $ENV{PATH} = $TAINT;
9607fc9c 87 $ENV{IFS} = " \t\n";
1e422769 88 test 1, eval { `$echo 1` } eq '';
89 test 2, $@ =~ /^Insecure \$ENV{PATH}/, $@;
90
91 $ENV{PATH} = '';
92 $ENV{IFS} = $TAINT;
93 test 3, eval { `$echo 1` } eq '';
94 test 4, $@ =~ /^Insecure \$ENV{IFS}/, $@;
95
9607fc9c 96 my $tmp;
97 if ($^O eq 'os2' || $^O eq 'amigaos') {
98 print "# all directories are writeable\n";
99 }
100 else {
101 $tmp = (grep { defined and -d and (stat _)[2] & 2 }
102 qw(/tmp /var/tmp /usr/tmp /sys$scratch),
103 @ENV{qw(TMP TEMP)})[0]
104 or print "# can't find world-writeable directory to test PATH\n";
105 }
106
385588b3 107 if ($tmp) {
1e422769 108 $ENV{PATH} = $tmp;
9607fc9c 109 $ENV{IFS} = " \t\n";
1e422769 110 test 5, eval { `$echo 1` } eq '';
111 test 6, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
112 }
113 else {
1e422769 114 for (5..6) { print "ok $_\n" }
115 }
116
117 $ENV{PATH} = '';
9607fc9c 118 $ENV{IFS} = " \t\n";
1e422769 119 test 7, eval { `$echo 1` } eq "1\n";
120 test 8, $@ eq '', $@;
121
122 if ($Is_VMS) {
123 $ENV{'DCL$PATH'} = $TAINT;
124 test 9, eval { `$echo 1` } eq '';
125 test 10, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
9607fc9c 126 if ($tmp) {
127 $ENV{'DCL$PATH'} = $tmp;
128 test 11, eval { `$echo 1` } eq '';
129 test 12, $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
130 }
131 else {
132 print "# can't find world-writeable directory to test DCL\$PATH\n";
133 for (11..12) { print "ok $_\n" }
134 }
1e422769 135 $ENV{'DCL$PATH'} = '';
136 }
137 else {
138 print "# This is not VMS\n";
9607fc9c 139 for (9..12) { print "ok $_\n"; }
1e422769 140 }
141}
142
143# Let's see that we can taint and untaint as needed.
144{
145 my $foo = $TAINT;
9607fc9c 146 test 13, tainted $foo;
147
148 # That was a sanity check. If it failed, stop the insanity!
149 die "Taint checks don't seem to be enabled" unless tainted $foo;
1e422769 150
151 $foo = "foo";
9607fc9c 152 test 14, not tainted $foo;
1e422769 153
154 taint_these($foo);
9607fc9c 155 test 15, tainted $foo;
1e422769 156
157 my @list = 1..10;
9607fc9c 158 test 16, not any_tainted @list;
1e422769 159 taint_these @list[1,3,5,7,9];
9607fc9c 160 test 17, any_tainted @list;
161 test 18, all_tainted @list[1,3,5,7,9];
162 test 19, not any_tainted @list[0,2,4,6,8];
1e422769 163
164 ($foo) = $foo =~ /(.+)/;
9607fc9c 165 test 20, not tainted $foo;
1e422769 166
167 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
9607fc9c 168 test 21, not tainted $foo;
169 test 22, $foo eq 'bar';
1e422769 170
171 my $pi = 4 * atan2(1,1) + $TAINT0;
9607fc9c 172 test 23, tainted $pi;
1e422769 173
174 ($pi) = $pi =~ /(\d+\.\d+)/;
9607fc9c 175 test 24, not tainted $pi;
176 test 25, sprintf("%.5f", $pi) eq '3.14159';
1e422769 177}
178
179# How about command-line arguments? The problem is that we don't
180# always get some, so we'll run another process with some.
181{
182 my $arg = "./arg$$";
183 open PROG, "> $arg" or die "Can't create $arg: $!";
184 print PROG q{
185 eval { join('', @ARGV), kill 0 };
186 exit 0 if $@ =~ /^Insecure dependency/;
187 print "# Oops: \$@ was [$@]\n";
188 exit 1;
189 };
190 close PROG;
191 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
9607fc9c 192 test 26, !$?, "Exited with status $?";
1e422769 193 unlink $arg;
194}
195
196# Reading from a file should be tainted
197{
9607fc9c 198 my $file = './TEST';
199 test 27, open(FILE, $file), "Couldn't open '$file': $!";
1e422769 200
201 my $block;
202 sysread(FILE, $block, 100);
9607fc9c 203 my $line = <FILE>;
1e422769 204 close FILE;
9607fc9c 205 test 28, tainted $block;
206 test 29, tainted $line;
1e422769 207}
208
9607fc9c 209# Globs should be tainted.
1e422769 210{
9607fc9c 211 # Some glob implementations need to spawn system programs.
212 local $ENV{PATH} = '';
213 $ENV{PATH} = (-l '/bin' ? '' : '/bin:') . '/usr/bin' unless $Is_VMS;
214
1e422769 215 my @globs = <*>;
9607fc9c 216 test 30, all_tainted @globs;
1e422769 217
218 @globs = glob '*';
9607fc9c 219 test 31, all_tainted @globs;
1e422769 220}
221
222# Output of commands should be tainted
223{
224 my $foo = `$echo abc`;
9607fc9c 225 test 32, tainted $foo;
1e422769 226}
227
228# Certain system variables should be tainted
229{
9607fc9c 230 test 33, all_tainted $^X, $0;
1e422769 231}
232
233# Results of matching should all be untainted
234{
235 my $foo = "abcdefghi" . $TAINT;
9607fc9c 236 test 34, tainted $foo;
1e422769 237
238 $foo =~ /def/;
9607fc9c 239 test 35, not any_tainted $`, $&, $';
1e422769 240
241 $foo =~ /(...)(...)(...)/;
9607fc9c 242 test 36, not any_tainted $1, $2, $3, $+;
1e422769 243
244 my @bar = $foo =~ /(...)(...)(...)/;
9607fc9c 245 test 37, not any_tainted @bar;
1e422769 246
9607fc9c 247 test 38, tainted $foo; # $foo should still be tainted!
248 test 39, $foo eq "abcdefghi";
1e422769 249}
250
251# Operations which affect files can't use tainted data.
252{
9607fc9c 253 test 40, eval { chmod 0, $TAINT } eq '', 'chmod';
1e422769 254 test 41, $@ =~ /^Insecure dependency/, $@;
255
9607fc9c 256 # There is no feature test in $Config{} for truncate,
257 # so we allow for the possibility that it's missing.
258 test 42, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate';
259 test 43, $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
1e422769 260
9607fc9c 261 test 44, eval { rename '', $TAINT } eq '', 'rename';
1e422769 262 test 45, $@ =~ /^Insecure dependency/, $@;
263
9607fc9c 264 test 46, eval { unlink $TAINT } eq '', 'unlink';
1e422769 265 test 47, $@ =~ /^Insecure dependency/, $@;
266
9607fc9c 267 test 48, eval { utime $TAINT } eq '', 'utime';
268 test 49, $@ =~ /^Insecure dependency/, $@;
269
1e422769 270 if ($Config{d_chown}) {
9607fc9c 271 test 50, eval { chown -1, -1, $TAINT } eq '', 'chown';
272 test 51, $@ =~ /^Insecure dependency/, $@;
1e422769 273 }
274 else {
275 print "# chown() is not available\n";
9607fc9c 276 for (50..51) { print "ok $_\n" }
1e422769 277 }
278
279 if ($Config{d_link}) {
9607fc9c 280 test 52, eval { link $TAINT, '' } eq '', 'link';
281 test 53, $@ =~ /^Insecure dependency/, $@;
1e422769 282 }
283 else {
284 print "# link() is not available\n";
9607fc9c 285 for (52..53) { print "ok $_\n" }
1e422769 286 }
287
288 if ($Config{d_symlink}) {
9607fc9c 289 test 54, eval { symlink $TAINT, '' } eq '', 'symlink';
290 test 55, $@ =~ /^Insecure dependency/, $@;
1e422769 291 }
292 else {
293 print "# symlink() is not available\n";
9607fc9c 294 for (54..55) { print "ok $_\n" }
1e422769 295 }
296}
297
298# Operations which affect directories can't use tainted data.
299{
9607fc9c 300 test 56, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir';
1e422769 301 test 57, $@ =~ /^Insecure dependency/, $@;
302
9607fc9c 303 test 58, eval { rmdir $TAINT } eq '', 'rmdir';
1e422769 304 test 59, $@ =~ /^Insecure dependency/, $@;
305
9607fc9c 306 test 60, eval { chdir $TAINT } eq '', 'chdir';
307 test 61, $@ =~ /^Insecure dependency/, $@;
308
1e422769 309 if ($Config{d_chroot}) {
9607fc9c 310 test 62, eval { chroot $TAINT } eq '', 'chroot';
311 test 63, $@ =~ /^Insecure dependency/, $@;
1e422769 312 }
313 else {
314 print "# chroot() is not available\n";
9607fc9c 315 for (62..63) { print "ok $_\n" }
1e422769 316 }
317}
318
319# Some operations using files can't use tainted data.
320{
321 my $foo = "imaginary library" . $TAINT;
9607fc9c 322 test 64, eval { require $foo } eq '', 'require';
323 test 65, $@ =~ /^Insecure dependency/, $@;
1e422769 324
325 my $filename = "./taintB$$"; # NB: $filename isn't tainted!
326 END { unlink $filename if defined $filename }
327 $foo = $filename . $TAINT;
328 unlink $filename; # in any case
329
9607fc9c 330 test 66, eval { open FOO, $foo } eq '', 'open for read';
331 test 67, $@ eq '', $@; # NB: This should be allowed
332 test 68, $! == 2; # File not found
1e422769 333
9607fc9c 334 test 69, eval { open FOO, "> $foo" } eq '', 'open for write';
335 test 70, $@ =~ /^Insecure dependency/, $@;
1e422769 336}
337
338# Commands to the system can't use tainted data
339{
340 my $foo = $TAINT;
341
342 if ($^O eq 'amigaos') {
343 print "# open(\"|\") is not available\n";
9607fc9c 344 for (71..74) { print "ok $_\n" }
1e422769 345 }
346 else {
9607fc9c 347 test 71, eval { open FOO, "| $foo" } eq '', 'popen to';
1e422769 348 test 72, $@ =~ /^Insecure dependency/, $@;
1e422769 349
9607fc9c 350 test 73, eval { open FOO, "$foo |" } eq '', 'popen from';
351 test 74, $@ =~ /^Insecure dependency/, $@;
352 }
1e422769 353
9607fc9c 354 test 75, eval { exec $TAINT } eq '', 'exec';
1e422769 355 test 76, $@ =~ /^Insecure dependency/, $@;
356
9607fc9c 357 test 77, eval { system $TAINT } eq '', 'system';
358 test 78, $@ =~ /^Insecure dependency/, $@;
359
1e422769 360 $foo = "*";
361 taint_these $foo;
362
9607fc9c 363 test 79, eval { `$echo 1$foo` } eq '', 'backticks';
364 test 80, $@ =~ /^Insecure dependency/, $@;
1e422769 365
366 if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe
9607fc9c 367 test 81, join('', eval { glob $foo } ) ne '', 'globbing';
368 test 82, $@ eq '', $@;
1e422769 369 }
370 else {
9607fc9c 371 test 81, join('', eval { glob $foo } ) eq '', 'globbing';
372 test 82, $@ =~ /^Insecure dependency/, $@;
1e422769 373 }
374}
375
376# Operations which affect processes can't use tainted data.
377{
9607fc9c 378 test 83, eval { kill 0, $TAINT } eq '', 'kill';
379 test 84, $@ =~ /^Insecure dependency/, $@;
1e422769 380
381 if ($Config{d_setpgrp}) {
9607fc9c 382 test 85, eval { setpgrp 0, $TAINT } eq '', 'setpgrp';
383 test 86, $@ =~ /^Insecure dependency/, $@;
1e422769 384 }
385 else {
386 print "# setpgrp() is not available\n";
9607fc9c 387 for (85..86) { print "ok $_\n" }
1e422769 388 }
389
390 if ($Config{d_setprior}) {
9607fc9c 391 test 87, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority';
392 test 88, $@ =~ /^Insecure dependency/, $@;
1e422769 393 }
394 else {
395 print "# setpriority() is not available\n";
9607fc9c 396 for (87..88) { print "ok $_\n" }
1e422769 397 }
398}
399
400# Some miscellaneous operations can't use tainted data.
401{
402 if ($Config{d_syscall}) {
9607fc9c 403 test 89, eval { syscall $TAINT } eq '', 'syscall';
404 test 90, $@ =~ /^Insecure dependency/, $@;
1e422769 405 }
406 else {
407 print "# syscall() is not available\n";
9607fc9c 408 for (89..90) { print "ok $_\n" }
1e422769 409 }
410
411 {
412 my $foo = "x" x 979;
413 taint_these $foo;
414 local *FOO;
415 my $temp = "./taintC$$";
416 END { unlink $temp }
9607fc9c 417 test 91, open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
1e422769 418
9607fc9c 419 test 92, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl';
420 test 93, $@ =~ /^Insecure dependency/, $@;
1e422769 421
422 if ($Config{d_fcntl}) {
9607fc9c 423 test 94, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl';
424 test 95, $@ =~ /^Insecure dependency/, $@;
1e422769 425 }
426 else {
427 print "# fcntl() is not available\n";
9607fc9c 428 for (94..95) { print "ok $_\n" }
1e422769 429 }
430
431 close FOO;
432 }
433}
434
9607fc9c 435# Some tests involving references
1e422769 436{
437 my $foo = 'abc' . $TAINT;
438 my $fooref = \$foo;
9607fc9c 439 test 96, not tainted $fooref;
440 test 97, tainted $$fooref;
441 test 98, tainted $foo;
1e422769 442}