This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
applied patch, tweak for threads awareness
[perl5.git] / t / op / taint.t
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
6 # tests that I've missed here, please add them. But this is
7 # better than having no tests at all, right?
8 #
9
10 BEGIN {
11     chdir 't' if -d 't';
12     @INC = '../lib' if -d '../lib';
13 }
14
15 use strict;
16 use Config;
17
18 my $Is_VMS = $^O eq 'VMS';
19 my $Is_MSWin32 = $^O eq 'MSWin32';
20 my $Is_Dos = $^O eq 'dos';
21 my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' :
22                   $Is_MSWin32 ? '.\perl' : './perl';
23 my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
24
25 if ($Is_VMS) {
26     my (%old, $x);
27     for $x ('DCL$PATH', @MoreEnv) {
28         ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
29     }
30     eval <<EndOfCleanup;
31         END {
32             \$ENV{PATH} = '';
33             warn "# Note: logical name 'PATH' may have been deleted\n";
34             @ENV{keys %old} = values %old;
35         }
36 EndOfCleanup
37 }
38
39 # Sources of taint:
40 #   The empty tainted value, for tainting strings
41 my $TAINT = substr($^X, 0, 0);
42 #   A tainted zero, useful for tainting numbers
43 my $TAINT0 = 0 + $TAINT;
44
45 # This taints each argument passed. All must be lvalues.
46 # Side effect: It also stringifies them. :-(
47 sub taint_these (@) {
48     for (@_) { $_ .= $TAINT }
49 }
50
51 # How to identify taint when you see it
52 sub any_tainted (@) {
53     not eval { join("",@_), kill 0; 1 };
54 }
55 sub tainted ($) {
56     any_tainted @_;
57 }
58 sub all_tainted (@) {
59     for (@_) { return 0 unless tainted $_ }
60     1;
61 }
62
63 sub test ($$;$) {
64     my($serial, $boolean, $diag) = @_;
65     if ($boolean) {
66         print "ok $serial\n";
67     } else {
68         print "not ok $serial\n";
69         for (split m/^/m, $diag) {
70             print "# $_";
71         }
72         print "\n" unless
73             $diag eq ''
74             or substr($diag, -1) eq "\n";
75     }
76 }
77
78 # We need an external program to call.
79 my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : "./echo$$");
80 END { unlink $ECHO }
81 open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
82 print PROG 'print "@ARGV\n"', "\n";
83 close PROG;
84 my $echo = "$Invoke_Perl $ECHO";
85
86 print "1..145\n";
87
88 # First, let's make sure that Perl is checking the dangerous
89 # environment variables. Maybe they aren't set yet, so we'll
90 # taint them ourselves.
91 {
92     $ENV{'DCL$PATH'} = '' if $Is_VMS;
93
94     $ENV{PATH} = '';
95     delete @ENV{@MoreEnv};
96     $ENV{TERM} = 'dumb';
97
98     test 1, eval { `$echo 1` } eq "1\n";
99
100     if ($Is_MSWin32 || $Is_VMS || $Is_Dos) {
101         print "# Environment tainting tests skipped\n";
102         for (2..5) { print "ok $_\n" }
103     }
104     else {
105         my @vars = ('PATH', @MoreEnv);
106         while (my $v = $vars[0]) {
107             local $ENV{$v} = $TAINT;
108             last if eval { `$echo 1` };
109             last unless $@ =~ /^Insecure \$ENV{$v}/;
110             shift @vars;
111         }
112         test 2, !@vars, "\$$vars[0]";
113
114         # tainted $TERM is unsafe only if it contains metachars
115         local $ENV{TERM};
116         $ENV{TERM} = 'e=mc2';
117         test 3, eval { `$echo 1` } eq "1\n";
118         $ENV{TERM} = 'e=mc2' . $TAINT;
119         test 4, eval { `$echo 1` } eq '';
120         test 5, $@ =~ /^Insecure \$ENV{TERM}/, $@;
121     }
122
123     my $tmp;
124     if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_Dos) {
125         print "# all directories are writeable\n";
126     }
127     else {
128         $tmp = (grep { defined and -d and (stat _)[2] & 2 }
129                      qw(/tmp /var/tmp /usr/tmp /sys$scratch),
130                      @ENV{qw(TMP TEMP)})[0]
131             or print "# can't find world-writeable directory to test PATH\n";
132     }
133
134     if ($tmp) {
135         local $ENV{PATH} = $tmp;
136         test 6, eval { `$echo 1` } eq '';
137         test 7, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
138     }
139     else {
140         for (6..7) { print "ok $_ # Skipped: all directories are writeable\n" }
141     }
142
143     if ($Is_VMS) {
144         $ENV{'DCL$PATH'} = $TAINT;
145         test 8,  eval { `$echo 1` } eq '';
146         test 9, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
147         if ($tmp) {
148             $ENV{'DCL$PATH'} = $tmp;
149             test 10, eval { `$echo 1` } eq '';
150             test 11, $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
151         }
152         else {
153             for (10..11) { print "ok $_ # Skipped: can't find world-writeable directory to test DCL\$PATH\n" }
154         }
155         $ENV{'DCL$PATH'} = '';
156     }
157     else {
158         for (8..11) { print "ok $_ # Skipped: This is not VMS\n"; }
159     }
160 }
161
162 # Let's see that we can taint and untaint as needed.
163 {
164     my $foo = $TAINT;
165     test 12, tainted $foo;
166
167     # That was a sanity check. If it failed, stop the insanity!
168     die "Taint checks don't seem to be enabled" unless tainted $foo;
169
170     $foo = "foo";
171     test 13, not tainted $foo;
172
173     taint_these($foo);
174     test 14, tainted $foo;
175
176     my @list = 1..10;
177     test 15, not any_tainted @list;
178     taint_these @list[1,3,5,7,9];
179     test 16, any_tainted @list;
180     test 17, all_tainted @list[1,3,5,7,9];
181     test 18, not any_tainted @list[0,2,4,6,8];
182
183     ($foo) = $foo =~ /(.+)/;
184     test 19, not tainted $foo;
185
186     $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
187     test 20, not tainted $foo;
188     test 21, $foo eq 'bar';
189
190     $foo = $1 if ('bar' . $TAINT) =~ /(.+)/t;
191     test 22, tainted $foo;
192     test 23, $foo eq 'bar';
193
194     my $pi = 4 * atan2(1,1) + $TAINT0;
195     test 24, tainted $pi;
196
197     ($pi) = $pi =~ /(\d+\.\d+)/;
198     test 25, not tainted $pi;
199     test 26, sprintf("%.5f", $pi) eq '3.14159';
200 }
201
202 # How about command-line arguments? The problem is that we don't
203 # always get some, so we'll run another process with some.
204 {
205     my $arg = "./arg$$";
206     open PROG, "> $arg" or die "Can't create $arg: $!";
207     print PROG q{
208         eval { join('', @ARGV), kill 0 };
209         exit 0 if $@ =~ /^Insecure dependency/;
210         print "# Oops: \$@ was [$@]\n";
211         exit 1;
212     };
213     close PROG;
214     print `$Invoke_Perl "-T" $arg and some suspect arguments`;
215     test 27, !$?, "Exited with status $?";
216     unlink $arg;
217 }
218
219 # Reading from a file should be tainted
220 {
221     my $file = './TEST';
222     test 28, open(FILE, $file), "Couldn't open '$file': $!";
223
224     my $block;
225     sysread(FILE, $block, 100);
226     my $line = <FILE>;
227     close FILE;
228     test 29, tainted $block;
229     test 30, tainted $line;
230 }
231
232 # Globs should be forbidden, except under VMS,
233 #   which doesn't spawn an external program.
234 if ($Is_VMS) {
235     for (31..32) { print "ok $_\n"; }
236 }
237 else {
238     my @globs = eval { <*> };
239     test 31, @globs == 0 && $@ =~ /^Insecure dependency/;
240
241     @globs = eval { glob '*' };
242     test 32, @globs == 0 && $@ =~ /^Insecure dependency/;
243 }
244
245 # Output of commands should be tainted
246 {
247     my $foo = `$echo abc`;
248     test 33, tainted $foo;
249 }
250
251 # Certain system variables should be tainted
252 {
253     test 34, all_tainted $^X, $0;
254 }
255
256 # Results of matching should all be untainted
257 {
258     my $foo = "abcdefghi" . $TAINT;
259     test 35, tainted $foo;
260
261     $foo =~ /def/;
262     test 36, not any_tainted $`, $&, $';
263
264     $foo =~ /(...)(...)(...)/;
265     test 37, not any_tainted $1, $2, $3, $+;
266
267     my @bar = $foo =~ /(...)(...)(...)/;
268     test 38, not any_tainted @bar;
269
270     test 39, tainted $foo;      # $foo should still be tainted!
271     test 40, $foo eq "abcdefghi";
272 }
273
274 # Operations which affect files can't use tainted data.
275 {
276     test 41, eval { chmod 0, $TAINT } eq '', 'chmod';
277     test 42, $@ =~ /^Insecure dependency/, $@;
278
279     # There is no feature test in $Config{} for truncate,
280     #   so we allow for the possibility that it's missing.
281     test 43, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate';
282     test 44, $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
283
284     test 45, eval { rename '', $TAINT } eq '', 'rename';
285     test 46, $@ =~ /^Insecure dependency/, $@;
286
287     test 47, eval { unlink $TAINT } eq '', 'unlink';
288     test 48, $@ =~ /^Insecure dependency/, $@;
289
290     test 49, eval { utime $TAINT } eq '', 'utime';
291     test 50, $@ =~ /^Insecure dependency/, $@;
292
293     if ($Config{d_chown}) {
294         test 51, eval { chown -1, -1, $TAINT } eq '', 'chown';
295         test 52, $@ =~ /^Insecure dependency/, $@;
296     }
297     else {
298         for (51..52) { print "ok $_ # Skipped: chown() is not available\n" }
299     }
300
301     if ($Config{d_link}) {
302         test 53, eval { link $TAINT, '' } eq '', 'link';
303         test 54, $@ =~ /^Insecure dependency/, $@;
304     }
305     else {
306         for (53..54) { print "ok $_ # Skipped: link() is not available\n" }
307     }
308
309     if ($Config{d_symlink}) {
310         test 55, eval { symlink $TAINT, '' } eq '', 'symlink';
311         test 56, $@ =~ /^Insecure dependency/, $@;
312     }
313     else {
314         for (55..56) { print "ok $_ # Skipped: symlink() is not available\n" }
315     }
316 }
317
318 # Operations which affect directories can't use tainted data.
319 {
320     test 57, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir';
321     test 58, $@ =~ /^Insecure dependency/, $@;
322
323     test 59, eval { rmdir $TAINT } eq '', 'rmdir';
324     test 60, $@ =~ /^Insecure dependency/, $@;
325
326     test 61, eval { chdir $TAINT } eq '', 'chdir';
327     test 62, $@ =~ /^Insecure dependency/, $@;
328
329     if ($Config{d_chroot}) {
330         test 63, eval { chroot $TAINT } eq '', 'chroot';
331         test 64, $@ =~ /^Insecure dependency/, $@;
332     }
333     else {
334         for (63..64) { print "ok $_ # Skipped: chroot() is not available\n" }
335     }
336 }
337
338 # Some operations using files can't use tainted data.
339 {
340     my $foo = "imaginary library" . $TAINT;
341     test 65, eval { require $foo } eq '', 'require';
342     test 66, $@ =~ /^Insecure dependency/, $@;
343
344     my $filename = "./taintB$$";        # NB: $filename isn't tainted!
345     END { unlink $filename if defined $filename }
346     $foo = $filename . $TAINT;
347     unlink $filename;   # in any case
348
349     test 67, eval { open FOO, $foo } eq '', 'open for read';
350     test 68, $@ eq '', $@;              # NB: This should be allowed
351     test 69, $! == 2 || ($Is_Dos && $! == 22); # File not found
352
353     test 70, eval { open FOO, "> $foo" } eq '', 'open for write';
354     test 71, $@ =~ /^Insecure dependency/, $@;
355 }
356
357 # Commands to the system can't use tainted data
358 {
359     my $foo = $TAINT;
360
361     if ($^O eq 'amigaos') {
362         for (72..75) { print "ok $_ # Skipped: open('|') is not available\n" }
363     }
364     else {
365         test 72, eval { open FOO, "| $foo" } eq '', 'popen to';
366         test 73, $@ =~ /^Insecure dependency/, $@;
367
368         test 74, eval { open FOO, "$foo |" } eq '', 'popen from';
369         test 75, $@ =~ /^Insecure dependency/, $@;
370     }
371
372     test 76, eval { exec $TAINT } eq '', 'exec';
373     test 77, $@ =~ /^Insecure dependency/, $@;
374
375     test 78, eval { system $TAINT } eq '', 'system';
376     test 79, $@ =~ /^Insecure dependency/, $@;
377
378     $foo = "*";
379     taint_these $foo;
380
381     test 80, eval { `$echo 1$foo` } eq '', 'backticks';
382     test 81, $@ =~ /^Insecure dependency/, $@;
383
384     if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe
385         test 82, join('', eval { glob $foo } ) ne '', 'globbing';
386         test 83, $@ eq '', $@;
387     }
388     else {
389         for (82..83) { print "ok $_ # Skipped: this is not VMS\n"; }
390     }
391 }
392
393 # Operations which affect processes can't use tainted data.
394 {
395     test 84, eval { kill 0, $TAINT } eq '', 'kill';
396     test 85, $@ =~ /^Insecure dependency/, $@;
397
398     if ($Config{d_setpgrp}) {
399         test 86, eval { setpgrp 0, $TAINT } eq '', 'setpgrp';
400         test 87, $@ =~ /^Insecure dependency/, $@;
401     }
402     else {
403         for (86..87) { print "ok $_ # Skipped: setpgrp() is not available\n" }
404     }
405
406     if ($Config{d_setprior}) {
407         test 88, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority';
408         test 89, $@ =~ /^Insecure dependency/, $@;
409     }
410     else {
411         for (88..89) { print "ok $_ # Skipped: setpriority() is not available\n" }
412     }
413 }
414
415 # Some miscellaneous operations can't use tainted data.
416 {
417     if ($Config{d_syscall}) {
418         test 90, eval { syscall $TAINT } eq '', 'syscall';
419         test 91, $@ =~ /^Insecure dependency/, $@;
420     }
421     else {
422         for (90..91) { print "ok $_ # Skipped: syscall() is not available\n" }
423     }
424
425     {
426         my $foo = "x" x 979;
427         taint_these $foo;
428         local *FOO;
429         my $temp = "./taintC$$";
430         END { unlink $temp }
431         test 92, open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
432
433         test 93, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl';
434         test 94, $@ =~ /^Insecure dependency/, $@;
435
436         if ($Config{d_fcntl}) {
437             test 95, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl';
438             test 96, $@ =~ /^Insecure dependency/, $@;
439         }
440         else {
441             for (95..96) { print "ok $_ # Skipped: fcntl() is not available\n" }
442         }
443
444         close FOO;
445     }
446 }
447
448 # Some tests involving references
449 {
450     my $foo = 'abc' . $TAINT;
451     my $fooref = \$foo;
452     test 97, not tainted $fooref;
453     test 98, tainted $$fooref;
454     test 99, tainted $foo;
455 }
456
457 # Some tests involving assignment
458 {
459     my $foo = $TAINT0;
460     my $bar = $foo;
461     test 100, all_tainted $foo, $bar;
462     test 101, tainted($foo = $bar);
463     test 102, tainted($bar = $bar);
464     test 103, tainted($bar += $bar);
465     test 104, tainted($bar -= $bar);
466     test 105, tainted($bar *= $bar);
467     test 106, tainted($bar++);
468     test 107, tainted($bar /= $bar);
469     test 108, tainted($bar += 0);
470     test 109, tainted($bar -= 2);
471     test 110, tainted($bar *= -1);
472     test 111, tainted($bar /= 1);
473     test 112, tainted($bar--);
474     test 113, $bar == 0;
475 }
476
477 # Test assignment and return of lists
478 {
479     my @foo = ("A", "tainted" . $TAINT, "B");
480     test 114, not tainted $foo[0];
481     test 115,     tainted $foo[1];
482     test 116, not tainted $foo[2];
483     my @bar = @foo;
484     test 117, not tainted $bar[0];
485     test 118,     tainted $bar[1];
486     test 119, not tainted $bar[2];
487     my @baz = eval { "A", "tainted" . $TAINT, "B" };
488     test 120, not tainted $baz[0];
489     test 121,     tainted $baz[1];
490     test 122, not tainted $baz[2];
491     my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
492     test 123, not tainted $plugh[0];
493     test 124,     tainted $plugh[1];
494     test 125, not tainted $plugh[2];
495     my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
496     test 126, not tainted ((&$nautilus)[0]);
497     test 127,     tainted ((&$nautilus)[1]);
498     test 128, not tainted ((&$nautilus)[2]);
499     my @xyzzy = &$nautilus;
500     test 129, not tainted $xyzzy[0];
501     test 130,     tainted $xyzzy[1];
502     test 131, not tainted $xyzzy[2];
503     my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
504     test 132, not tainted ((&$red_october)[0]);
505     test 133,     tainted ((&$red_october)[1]);
506     test 134, not tainted ((&$red_october)[2]);
507     my @corge = &$red_october;
508     test 135, not tainted $corge[0];
509     test 136,     tainted $corge[1];
510     test 137, not tainted $corge[2];
511 }
512
513 # Test for system/library calls returning string data of dubious origin.
514 {
515     # No reliable %Config check for getpw*
516     if (eval { setpwent(); getpwent(); 1 }) {
517         setpwent();
518         my @getpwent = getpwent();
519         die "getpwent: $!\n" unless (@getpwent);
520         test 138,(    not tainted $getpwent[0]
521                   and not tainted $getpwent[1]
522                   and not tainted $getpwent[2]
523                   and not tainted $getpwent[3]
524                   and not tainted $getpwent[4]
525                   and not tainted $getpwent[5]
526                   and     tainted $getpwent[6] # gecos
527                   and not tainted $getpwent[7]
528                   and not tainted $getpwent[8]);
529         endpwent();
530     } else {
531         print "ok 138 # Skipped: getpwent() is not available\n";
532     }
533
534     if ($Config{d_readdir}) { # pretty hard to imagine not
535         local(*D);
536         opendir(D, "op") or die "opendir: $!\n";
537         my $readdir = readdir(D);
538         test 139, tainted $readdir;
539         closedir(OP);
540     } else {
541         print "ok 139 # Skipped: readdir() is not available\n";
542     }
543
544     if ($Config{d_readlink} && $Config{d_symlink}) {
545         my $symlink = "sl$$";
546         unlink($symlink);
547         symlink("/something/naughty", $symlink) or die "symlink: $!\n";
548         my $readlink = readlink($symlink);
549         test 140, tainted $readlink;
550         unlink($symlink);
551     } else {
552         print "ok 140 # Skipped: readlink() or symlink() is not available\n";
553     }
554 }
555
556 # test bitwise ops (regression bug)
557 {
558     my $why = "y";
559     my $j = "x" | $why;
560     test 141, not tainted $j;
561     $why = $TAINT."y";
562     $j = "x" | $why;
563     test 142,     tainted $j;
564 }
565
566 # test target of substitution (regression bug)
567 {
568     my $why = $TAINT."y";
569     $why =~ s/y/z/;
570     test 143,     tainted $why;
571
572     my $z = "[z]";
573     $why =~ s/$z/zee/;
574     test 144,     tainted $why;
575
576     $why =~ s/e/'-'.$$/ge;
577     test 145,     tainted $why;
578 }