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