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