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