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'; |
20822f61 |
12 | @INC = '../lib'; |
1e422769 |
13 | } |
14 | |
15 | use strict; |
16 | use Config; |
dc459aad |
17 | use File::Spec::Functions; |
1e422769 |
18 | |
09f04786 |
19 | BEGIN { require './test.pl'; } |
3c4fb04a |
20 | plan tests => 301; |
7c36658b |
21 | |
0ecd3ba2 |
22 | $| = 1; |
23 | |
c9f931b8 |
24 | use vars qw($ipcsysv); # did we manage to load IPC::SysV? |
25 | |
ff504b36 |
26 | my ($old_env_path, $old_env_dcl_path, $old_env_term); |
3eeba6fb |
27 | BEGIN { |
ff504b36 |
28 | $old_env_path = $ENV{'PATH'}; |
29 | $old_env_dcl_path = $ENV{'DCL$PATH'}; |
30 | $old_env_term = $ENV{'TERM'}; |
3eeba6fb |
31 | if ($^O eq 'VMS' && !defined($Config{d_setenv})) { |
32 | $ENV{PATH} = $ENV{PATH}; |
33 | $ENV{TERM} = $ENV{TERM} ne ''? $ENV{TERM} : 'dummy'; |
34 | } |
be3174d2 |
35 | if ($Config{'extensions'} =~ /\bIPC\/SysV\b/ |
36 | && ($Config{d_shm} || $Config{d_msg})) { |
c9f931b8 |
37 | eval { require IPC::SysV }; |
38 | unless ($@) { |
39 | $ipcsysv++; |
ddc3217d |
40 | IPC::SysV->import(qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU IPC_NOWAIT)); |
c9f931b8 |
41 | } |
b9d1c439 |
42 | } |
3eeba6fb |
43 | } |
44 | |
09f04786 |
45 | my $Is_MacOS = $^O eq 'MacOS'; |
46 | my $Is_VMS = $^O eq 'VMS'; |
47 | my $Is_MSWin32 = $^O eq 'MSWin32'; |
48 | my $Is_NetWare = $^O eq 'NetWare'; |
49 | my $Is_Dos = $^O eq 'dos'; |
50 | my $Is_Cygwin = $^O eq 'cygwin'; |
8c2c7bd1 |
51 | my $Is_OpenBSD = $^O eq 'openbsd'; |
08322f8f |
52 | my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.exe' : |
09f04786 |
53 | $Is_MSWin32 ? '.\perl' : |
54 | $Is_MacOS ? ':perl' : |
55 | $Is_NetWare ? 'perl' : |
56 | './perl' ; |
c90c0ff4 |
57 | my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/; |
7bac28a0 |
58 | |
1e422769 |
59 | if ($Is_VMS) { |
7bac28a0 |
60 | my (%old, $x); |
61 | for $x ('DCL$PATH', @MoreEnv) { |
62 | ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x}; |
63 | } |
ff504b36 |
64 | # VMS note: PATH and TERM are automatically created by the C |
65 | # library in VMS on reference to the their keys in %ENV. |
66 | # There is currently no way to determine if they did not exist |
67 | # before this test was run. |
1e422769 |
68 | eval <<EndOfCleanup; |
69 | END { |
ff504b36 |
70 | \$ENV{PATH} = \$old_env_path; |
71 | warn "# Note: logical name 'PATH' may have been created\n"; |
72 | \$ENV{'TERM'} = \$old_env_term; |
73 | warn "# Note: logical name 'TERM' may have been created\n"; |
562a7b0c |
74 | \@ENV{keys %old} = values %old; |
ff504b36 |
75 | if (defined \$old_env_dcl_path) { |
76 | \$ENV{'DCL\$PATH'} = \$old_env_dcl_path; |
77 | } else { |
78 | delete \$ENV{'DCL\$PATH'}; |
79 | } |
1e422769 |
80 | } |
81 | EndOfCleanup |
82 | } |
83 | |
84 | # Sources of taint: |
85 | # The empty tainted value, for tainting strings |
86 | my $TAINT = substr($^X, 0, 0); |
87 | # A tainted zero, useful for tainting numbers |
09f04786 |
88 | my $TAINT0; |
89 | { |
90 | no warnings; |
91 | $TAINT0 = 0 + $TAINT; |
92 | } |
1e422769 |
93 | |
94 | # This taints each argument passed. All must be lvalues. |
95 | # Side effect: It also stringifies them. :-( |
96 | sub taint_these (@) { |
97 | for (@_) { $_ .= $TAINT } |
98 | } |
99 | |
100 | # How to identify taint when you see it |
101 | sub any_tainted (@) { |
102 | not eval { join("",@_), kill 0; 1 }; |
103 | } |
104 | sub tainted ($) { |
105 | any_tainted @_; |
106 | } |
107 | sub all_tainted (@) { |
108 | for (@_) { return 0 unless tainted $_ } |
109 | 1; |
110 | } |
111 | |
09f04786 |
112 | |
113 | sub test ($;$) { |
114 | my($ok, $diag) = @_; |
115 | |
116 | my $curr_test = curr_test(); |
117 | |
118 | if ($ok) { |
119 | print "ok $curr_test\n"; |
1e422769 |
120 | } else { |
09f04786 |
121 | print "not ok $curr_test\n"; |
122 | printf "# Failed test at line %d\n", (caller)[2]; |
1e422769 |
123 | for (split m/^/m, $diag) { |
124 | print "# $_"; |
125 | } |
9607fc9c |
126 | print "\n" unless |
1e422769 |
127 | $diag eq '' |
128 | or substr($diag, -1) eq "\n"; |
129 | } |
09f04786 |
130 | |
131 | next_test(); |
132 | |
133 | return $ok; |
1e422769 |
134 | } |
135 | |
136 | # We need an external program to call. |
dc459aad |
137 | my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : $Is_MacOS ? ":echo$$" : ($Is_NetWare ? "echo$$" : "./echo$$")); |
1e422769 |
138 | END { unlink $ECHO } |
139 | open PROG, "> $ECHO" or die "Can't create $ECHO: $!"; |
140 | print PROG 'print "@ARGV\n"', "\n"; |
141 | close PROG; |
142 | my $echo = "$Invoke_Perl $ECHO"; |
143 | |
dc459aad |
144 | my $TEST = catfile(curdir(), 'TEST'); |
145 | |
1e422769 |
146 | # First, let's make sure that Perl is checking the dangerous |
147 | # environment variables. Maybe they aren't set yet, so we'll |
148 | # taint them ourselves. |
149 | { |
150 | $ENV{'DCL$PATH'} = '' if $Is_VMS; |
151 | |
f68313a1 |
152 | if ($Is_MSWin32 && $Config{ccname} =~ /bcc32/ && ! -f 'cc3250mt.dll') { |
153 | my $bcc_dir; |
154 | foreach my $dir (split /$Config{path_sep}/, $ENV{PATH}) { |
155 | if (-f "$dir/cc3250mt.dll") { |
156 | $bcc_dir = $dir and last; |
157 | } |
158 | } |
159 | if (defined $bcc_dir) { |
160 | require File::Copy; |
161 | File::Copy::copy("$bcc_dir/cc3250mt.dll", '.') or |
162 | die "$0: failed to copy cc3250mt.dll: $!\n"; |
163 | eval q{ |
164 | END { unlink "cc3250mt.dll" } |
165 | }; |
166 | } |
167 | } |
e59c8b07 |
168 | $ENV{PATH} = ($Is_Cygwin) ? '/usr/bin' : ''; |
c90c0ff4 |
169 | delete @ENV{@MoreEnv}; |
7bac28a0 |
170 | $ENV{TERM} = 'dumb'; |
171 | |
09f04786 |
172 | test eval { `$echo 1` } eq "1\n"; |
173 | |
174 | SKIP: { |
175 | skip "Environment tainting tests skipped", 4 |
176 | if $Is_MSWin32 || $Is_NetWare || $Is_VMS || $Is_Dos || $Is_MacOS; |
7bac28a0 |
177 | |
7bac28a0 |
178 | my @vars = ('PATH', @MoreEnv); |
179 | while (my $v = $vars[0]) { |
180 | local $ENV{$v} = $TAINT; |
181 | last if eval { `$echo 1` }; |
182 | last unless $@ =~ /^Insecure \$ENV{$v}/; |
183 | shift @vars; |
184 | } |
09f04786 |
185 | test !@vars, "@vars"; |
c90c0ff4 |
186 | |
187 | # tainted $TERM is unsafe only if it contains metachars |
188 | local $ENV{TERM}; |
189 | $ENV{TERM} = 'e=mc2'; |
09f04786 |
190 | test eval { `$echo 1` } eq "1\n"; |
c90c0ff4 |
191 | $ENV{TERM} = 'e=mc2' . $TAINT; |
09f04786 |
192 | test !eval { `$echo 1` }; |
193 | test $@ =~ /^Insecure \$ENV{TERM}/, $@; |
5aabfad6 |
194 | } |
7bac28a0 |
195 | |
9607fc9c |
196 | my $tmp; |
2986a63f |
197 | if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_NetWare || $Is_Dos) { |
48c036b1 |
198 | print "# all directories are writeable\n"; |
199 | } |
200 | else { |
9607fc9c |
201 | $tmp = (grep { defined and -d and (stat _)[2] & 2 } |
099f76bb |
202 | qw(sys$scratch /tmp /var/tmp /usr/tmp), |
9607fc9c |
203 | @ENV{qw(TMP TEMP)})[0] |
204 | or print "# can't find world-writeable directory to test PATH\n"; |
205 | } |
206 | |
09f04786 |
207 | SKIP: { |
208 | skip "all directories are writeable", 2 unless $tmp; |
209 | |
7bac28a0 |
210 | local $ENV{PATH} = $tmp; |
09f04786 |
211 | test !eval { `$echo 1` }; |
212 | test $@ =~ /^Insecure directory in \$ENV{PATH}/, $@; |
1e422769 |
213 | } |
214 | |
09f04786 |
215 | SKIP: { |
216 | skip "This is not VMS", 4 unless $Is_VMS; |
217 | |
1e422769 |
218 | $ENV{'DCL$PATH'} = $TAINT; |
09f04786 |
219 | test eval { `$echo 1` } eq ''; |
220 | test $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@; |
221 | SKIP: { |
222 | skip q[can't find world-writeable directory to test DCL$PATH], 2 |
25fb98c0 |
223 | unless $tmp; |
09f04786 |
224 | |
9607fc9c |
225 | $ENV{'DCL$PATH'} = $tmp; |
09f04786 |
226 | test eval { `$echo 1` } eq ''; |
227 | test $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@; |
9607fc9c |
228 | } |
1e422769 |
229 | $ENV{'DCL$PATH'} = ''; |
230 | } |
1e422769 |
231 | } |
232 | |
233 | # Let's see that we can taint and untaint as needed. |
234 | { |
235 | my $foo = $TAINT; |
09f04786 |
236 | test tainted $foo; |
9607fc9c |
237 | |
238 | # That was a sanity check. If it failed, stop the insanity! |
239 | die "Taint checks don't seem to be enabled" unless tainted $foo; |
1e422769 |
240 | |
241 | $foo = "foo"; |
09f04786 |
242 | test not tainted $foo; |
1e422769 |
243 | |
244 | taint_these($foo); |
09f04786 |
245 | test tainted $foo; |
1e422769 |
246 | |
247 | my @list = 1..10; |
09f04786 |
248 | test not any_tainted @list; |
1e422769 |
249 | taint_these @list[1,3,5,7,9]; |
09f04786 |
250 | test any_tainted @list; |
251 | test all_tainted @list[1,3,5,7,9]; |
252 | test not any_tainted @list[0,2,4,6,8]; |
1e422769 |
253 | |
254 | ($foo) = $foo =~ /(.+)/; |
09f04786 |
255 | test not tainted $foo; |
1e422769 |
256 | |
257 | $foo = $1 if ('bar' . $TAINT) =~ /(.+)/; |
09f04786 |
258 | test not tainted $foo; |
259 | test $foo eq 'bar'; |
1e422769 |
260 | |
b3eb6a9b |
261 | { |
262 | use re 'taint'; |
263 | |
264 | ($foo) = ('bar' . $TAINT) =~ /(.+)/; |
09f04786 |
265 | test tainted $foo; |
266 | test $foo eq 'bar'; |
b3eb6a9b |
267 | |
268 | $foo = $1 if ('bar' . $TAINT) =~ /(.+)/; |
09f04786 |
269 | test tainted $foo; |
270 | test $foo eq 'bar'; |
b3eb6a9b |
271 | } |
272 | |
273 | $foo = $1 if 'bar' =~ /(.+)$TAINT/; |
09f04786 |
274 | test tainted $foo; |
275 | test $foo eq 'bar'; |
48c036b1 |
276 | |
1e422769 |
277 | my $pi = 4 * atan2(1,1) + $TAINT0; |
09f04786 |
278 | test tainted $pi; |
1e422769 |
279 | |
280 | ($pi) = $pi =~ /(\d+\.\d+)/; |
09f04786 |
281 | test not tainted $pi; |
282 | test sprintf("%.5f", $pi) eq '3.14159'; |
1e422769 |
283 | } |
284 | |
285 | # How about command-line arguments? The problem is that we don't |
286 | # always get some, so we'll run another process with some. |
dc459aad |
287 | SKIP: { |
1ab9acc5 |
288 | my $arg = tempfile(); |
1e422769 |
289 | open PROG, "> $arg" or die "Can't create $arg: $!"; |
290 | print PROG q{ |
291 | eval { join('', @ARGV), kill 0 }; |
292 | exit 0 if $@ =~ /^Insecure dependency/; |
293 | print "# Oops: \$@ was [$@]\n"; |
294 | exit 1; |
295 | }; |
296 | close PROG; |
297 | print `$Invoke_Perl "-T" $arg and some suspect arguments`; |
09f04786 |
298 | test !$?, "Exited with status $?"; |
1e422769 |
299 | unlink $arg; |
300 | } |
301 | |
302 | # Reading from a file should be tainted |
303 | { |
09f04786 |
304 | test open(FILE, $TEST), "Couldn't open '$TEST': $!"; |
1e422769 |
305 | |
306 | my $block; |
307 | sysread(FILE, $block, 100); |
9607fc9c |
308 | my $line = <FILE>; |
1e422769 |
309 | close FILE; |
09f04786 |
310 | test tainted $block; |
311 | test tainted $line; |
1e422769 |
312 | } |
313 | |
c90c0ff4 |
314 | # Globs should be forbidden, except under VMS, |
315 | # which doesn't spawn an external program. |
09f04786 |
316 | SKIP: { |
317 | skip "globs should be forbidden", 2 if 1 or $Is_VMS; |
318 | |
7bac28a0 |
319 | my @globs = eval { <*> }; |
09f04786 |
320 | test @globs == 0 && $@ =~ /^Insecure dependency/; |
1e422769 |
321 | |
7bac28a0 |
322 | @globs = eval { glob '*' }; |
09f04786 |
323 | test @globs == 0 && $@ =~ /^Insecure dependency/; |
1e422769 |
324 | } |
325 | |
326 | # Output of commands should be tainted |
327 | { |
328 | my $foo = `$echo abc`; |
09f04786 |
329 | test tainted $foo; |
1e422769 |
330 | } |
331 | |
332 | # Certain system variables should be tainted |
333 | { |
09f04786 |
334 | test all_tainted $^X, $0; |
1e422769 |
335 | } |
336 | |
337 | # Results of matching should all be untainted |
338 | { |
339 | my $foo = "abcdefghi" . $TAINT; |
09f04786 |
340 | test tainted $foo; |
1e422769 |
341 | |
342 | $foo =~ /def/; |
09f04786 |
343 | test not any_tainted $`, $&, $'; |
1e422769 |
344 | |
345 | $foo =~ /(...)(...)(...)/; |
09f04786 |
346 | test not any_tainted $1, $2, $3, $+; |
1e422769 |
347 | |
348 | my @bar = $foo =~ /(...)(...)(...)/; |
09f04786 |
349 | test not any_tainted @bar; |
1e422769 |
350 | |
09f04786 |
351 | test tainted $foo; # $foo should still be tainted! |
352 | test $foo eq "abcdefghi"; |
1e422769 |
353 | } |
354 | |
355 | # Operations which affect files can't use tainted data. |
356 | { |
09f04786 |
357 | test !eval { chmod 0, $TAINT }, 'chmod'; |
358 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
359 | |
9607fc9c |
360 | # There is no feature test in $Config{} for truncate, |
361 | # so we allow for the possibility that it's missing. |
09f04786 |
362 | test !eval { truncate 'NoSuChFiLe', $TAINT0 }, 'truncate'; |
363 | test $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@; |
1e422769 |
364 | |
09f04786 |
365 | test !eval { rename '', $TAINT }, 'rename'; |
366 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
367 | |
09f04786 |
368 | test !eval { unlink $TAINT }, 'unlink'; |
369 | test $@ =~ /^Insecure dependency/, $@; |
9607fc9c |
370 | |
09f04786 |
371 | test !eval { utime $TAINT }, 'utime'; |
372 | test $@ =~ /^Insecure dependency/, $@; |
48c036b1 |
373 | |
09f04786 |
374 | SKIP: { |
375 | skip "chown() is not available", 2 unless $Config{d_chown}; |
1e422769 |
376 | |
09f04786 |
377 | test !eval { chown -1, -1, $TAINT }, 'chown'; |
378 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
379 | } |
380 | |
09f04786 |
381 | SKIP: { |
382 | skip "link() is not available", 2 unless $Config{d_link}; |
383 | |
384 | test !eval { link $TAINT, '' }, 'link'; |
385 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
386 | } |
09f04786 |
387 | |
388 | SKIP: { |
389 | skip "symlink() is not available", 2 unless $Config{d_symlink}; |
390 | |
391 | test !eval { symlink $TAINT, '' }, 'symlink'; |
392 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
393 | } |
394 | } |
395 | |
396 | # Operations which affect directories can't use tainted data. |
397 | { |
09f04786 |
398 | test !eval { mkdir "foo".$TAINT, 0755.$TAINT0 }, 'mkdir'; |
399 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
400 | |
09f04786 |
401 | test !eval { rmdir $TAINT }, 'rmdir'; |
402 | test $@ =~ /^Insecure dependency/, $@; |
9607fc9c |
403 | |
09f04786 |
404 | test !eval { chdir "foo".$TAINT }, 'chdir'; |
405 | test $@ =~ /^Insecure dependency/, $@; |
48c036b1 |
406 | |
09f04786 |
407 | SKIP: { |
408 | skip "chroot() is not available", 2 unless $Config{d_chroot}; |
409 | |
410 | test !eval { chroot $TAINT }, 'chroot'; |
411 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
412 | } |
413 | } |
414 | |
415 | # Some operations using files can't use tainted data. |
416 | { |
417 | my $foo = "imaginary library" . $TAINT; |
09f04786 |
418 | test !eval { require $foo }, 'require'; |
419 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
420 | |
1c25d394 |
421 | my $filename = tempfile(); # NB: $filename isn't tainted! |
1e422769 |
422 | $foo = $filename . $TAINT; |
423 | unlink $filename; # in any case |
424 | |
09f04786 |
425 | test !eval { open FOO, $foo }, 'open for read'; |
426 | test $@ eq '', $@; # NB: This should be allowed |
9d116dd7 |
427 | |
428 | # Try first new style but allow also old style. |
327ccce1 |
429 | # We do not want the whole taint.t to fail |
430 | # just because Errno possibly failing. |
09f04786 |
431 | test eval('$!{ENOENT}') || |
61ae2fbf |
432 | $! == 2 || # File not found |
cd86ed9d |
433 | ($Is_Dos && $! == 22); |
1e422769 |
434 | |
09f04786 |
435 | test !eval { open FOO, "> $foo" }, 'open for write'; |
436 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
437 | } |
438 | |
439 | # Commands to the system can't use tainted data |
440 | { |
441 | my $foo = $TAINT; |
442 | |
09f04786 |
443 | SKIP: { |
444 | skip "open('|') is not available", 4 if $^O eq 'amigaos'; |
445 | |
446 | test !eval { open FOO, "| x$foo" }, 'popen to'; |
447 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
448 | |
09f04786 |
449 | test !eval { open FOO, "x$foo |" }, 'popen from'; |
450 | test $@ =~ /^Insecure dependency/, $@; |
48c036b1 |
451 | } |
1e422769 |
452 | |
09f04786 |
453 | test !eval { exec $TAINT }, 'exec'; |
454 | test $@ =~ /^Insecure dependency/, $@; |
9607fc9c |
455 | |
09f04786 |
456 | test !eval { system $TAINT }, 'system'; |
457 | test $@ =~ /^Insecure dependency/, $@; |
48c036b1 |
458 | |
1e422769 |
459 | $foo = "*"; |
460 | taint_these $foo; |
461 | |
09f04786 |
462 | test !eval { `$echo 1$foo` }, 'backticks'; |
463 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
464 | |
09f04786 |
465 | SKIP: { |
466 | # wildcard expansion doesn't invoke shell on VMS, so is safe |
467 | skip "This is not VMS", 2 unless $Is_VMS; |
468 | |
469 | test join('', eval { glob $foo } ) ne '', 'globbing'; |
470 | test $@ eq '', $@; |
1e422769 |
471 | } |
472 | } |
473 | |
474 | # Operations which affect processes can't use tainted data. |
475 | { |
09f04786 |
476 | test !eval { kill 0, $TAINT }, 'kill'; |
477 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
478 | |
09f04786 |
479 | SKIP: { |
480 | skip "setpgrp() is not available", 2 unless $Config{d_setpgrp}; |
1e422769 |
481 | |
09f04786 |
482 | test !eval { setpgrp 0, $TAINT0 }, 'setpgrp'; |
483 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
484 | } |
09f04786 |
485 | |
486 | SKIP: { |
487 | skip "setpriority() is not available", 2 unless $Config{d_setprior}; |
488 | |
489 | test !eval { setpriority 0, $TAINT0, $TAINT0 }, 'setpriority'; |
490 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
491 | } |
492 | } |
493 | |
494 | # Some miscellaneous operations can't use tainted data. |
495 | { |
09f04786 |
496 | SKIP: { |
497 | skip "syscall() is not available", 2 unless $Config{d_syscall}; |
498 | |
499 | test !eval { syscall $TAINT }, 'syscall'; |
500 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
501 | } |
502 | |
503 | { |
504 | my $foo = "x" x 979; |
505 | taint_these $foo; |
506 | local *FOO; |
1c25d394 |
507 | my $temp = tempfile(); |
09f04786 |
508 | test open(FOO, "> $temp"), "Couldn't open $temp for write: $!"; |
1e422769 |
509 | |
09f04786 |
510 | test !eval { ioctl FOO, $TAINT0, $foo }, 'ioctl'; |
511 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
512 | |
09f04786 |
513 | SKIP: { |
514 | skip "fcntl() is not available", 2 unless $Config{d_fcntl}; |
515 | |
516 | test !eval { fcntl FOO, $TAINT0, $foo }, 'fcntl'; |
517 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
518 | } |
519 | |
520 | close FOO; |
521 | } |
522 | } |
523 | |
9607fc9c |
524 | # Some tests involving references |
1e422769 |
525 | { |
526 | my $foo = 'abc' . $TAINT; |
527 | my $fooref = \$foo; |
09f04786 |
528 | test not tainted $fooref; |
529 | test tainted $$fooref; |
530 | test tainted $foo; |
1e422769 |
531 | } |
54310121 |
532 | |
533 | # Some tests involving assignment |
534 | { |
535 | my $foo = $TAINT0; |
536 | my $bar = $foo; |
09f04786 |
537 | test all_tainted $foo, $bar; |
538 | test tainted($foo = $bar); |
539 | test tainted($bar = $bar); |
540 | test tainted($bar += $bar); |
541 | test tainted($bar -= $bar); |
542 | test tainted($bar *= $bar); |
543 | test tainted($bar++); |
544 | test tainted($bar /= $bar); |
545 | test tainted($bar += 0); |
546 | test tainted($bar -= 2); |
547 | test tainted($bar *= -1); |
548 | test tainted($bar /= 1); |
549 | test tainted($bar--); |
550 | test $bar == 0; |
54310121 |
551 | } |
a1f49e72 |
552 | |
553 | # Test assignment and return of lists |
554 | { |
555 | my @foo = ("A", "tainted" . $TAINT, "B"); |
09f04786 |
556 | test not tainted $foo[0]; |
557 | test tainted $foo[1]; |
558 | test not tainted $foo[2]; |
a1f49e72 |
559 | my @bar = @foo; |
09f04786 |
560 | test not tainted $bar[0]; |
561 | test tainted $bar[1]; |
562 | test not tainted $bar[2]; |
a1f49e72 |
563 | my @baz = eval { "A", "tainted" . $TAINT, "B" }; |
09f04786 |
564 | test not tainted $baz[0]; |
565 | test tainted $baz[1]; |
566 | test not tainted $baz[2]; |
a1f49e72 |
567 | my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ]; |
09f04786 |
568 | test not tainted $plugh[0]; |
569 | test tainted $plugh[1]; |
570 | test not tainted $plugh[2]; |
a1f49e72 |
571 | my $nautilus = sub { "A", "tainted" . $TAINT, "B" }; |
09f04786 |
572 | test not tainted ((&$nautilus)[0]); |
573 | test tainted ((&$nautilus)[1]); |
574 | test not tainted ((&$nautilus)[2]); |
a1f49e72 |
575 | my @xyzzy = &$nautilus; |
09f04786 |
576 | test not tainted $xyzzy[0]; |
577 | test tainted $xyzzy[1]; |
578 | test not tainted $xyzzy[2]; |
a1f49e72 |
579 | my $red_october = sub { return "A", "tainted" . $TAINT, "B" }; |
09f04786 |
580 | test not tainted ((&$red_october)[0]); |
581 | test tainted ((&$red_october)[1]); |
582 | test not tainted ((&$red_october)[2]); |
a1f49e72 |
583 | my @corge = &$red_october; |
09f04786 |
584 | test not tainted $corge[0]; |
585 | test tainted $corge[1]; |
586 | test not tainted $corge[2]; |
a1f49e72 |
587 | } |
fb73857a |
588 | |
589 | # Test for system/library calls returning string data of dubious origin. |
590 | { |
591 | # No reliable %Config check for getpw* |
09f04786 |
592 | SKIP: { |
593 | skip "getpwent() is not available", 1 unless |
594 | eval { setpwent(); getpwent() }; |
595 | |
fb73857a |
596 | setpwent(); |
597 | my @getpwent = getpwent(); |
598 | die "getpwent: $!\n" unless (@getpwent); |
09f04786 |
599 | test ( not tainted $getpwent[0] |
2959b6e3 |
600 | and tainted $getpwent[1] |
fb73857a |
601 | and not tainted $getpwent[2] |
602 | and not tainted $getpwent[3] |
603 | and not tainted $getpwent[4] |
604 | and not tainted $getpwent[5] |
3972a534 |
605 | and tainted $getpwent[6] # ge?cos |
fb73857a |
606 | and not tainted $getpwent[7] |
3972a534 |
607 | and tainted $getpwent[8]); # shell |
fb73857a |
608 | endpwent(); |
fb73857a |
609 | } |
610 | |
09f04786 |
611 | SKIP: { |
612 | # pretty hard to imagine not |
613 | skip "readdir() is not available", 1 unless $Config{d_readdir}; |
614 | |
fb73857a |
615 | local(*D); |
616 | opendir(D, "op") or die "opendir: $!\n"; |
617 | my $readdir = readdir(D); |
09f04786 |
618 | test tainted $readdir; |
619 | closedir(D); |
fb73857a |
620 | } |
621 | |
09f04786 |
622 | SKIP: { |
623 | skip "readlink() or symlink() is not available" unless |
624 | $Config{d_readlink} && $Config{d_symlink}; |
625 | |
fb73857a |
626 | my $symlink = "sl$$"; |
627 | unlink($symlink); |
dc459aad |
628 | my $sl = "/something/naughty"; |
629 | # it has to be a real path on Mac OS |
630 | $sl = MacPerl::MakePath((MacPerl::Volumes())[0]) if $Is_MacOS; |
631 | symlink($sl, $symlink) or die "symlink: $!\n"; |
fb73857a |
632 | my $readlink = readlink($symlink); |
09f04786 |
633 | test tainted $readlink; |
fb73857a |
634 | unlink($symlink); |
fb73857a |
635 | } |
636 | } |
637 | |
638 | # test bitwise ops (regression bug) |
639 | { |
640 | my $why = "y"; |
641 | my $j = "x" | $why; |
09f04786 |
642 | test not tainted $j; |
fb73857a |
643 | $why = $TAINT."y"; |
644 | $j = "x" | $why; |
09f04786 |
645 | test tainted $j; |
fb73857a |
646 | } |
647 | |
48c036b1 |
648 | # test target of substitution (regression bug) |
649 | { |
650 | my $why = $TAINT."y"; |
651 | $why =~ s/y/z/; |
09f04786 |
652 | test tainted $why; |
48c036b1 |
653 | |
654 | my $z = "[z]"; |
655 | $why =~ s/$z/zee/; |
09f04786 |
656 | test tainted $why; |
48c036b1 |
657 | |
658 | $why =~ s/e/'-'.$$/ge; |
09f04786 |
659 | test tainted $why; |
48c036b1 |
660 | } |
d929ce6f |
661 | |
09f04786 |
662 | |
663 | SKIP: { |
664 | skip "no IPC::SysV", 2 unless $ipcsysv; |
665 | |
666 | # test shmread |
667 | SKIP: { |
668 | skip "shm*() not available", 1 unless $Config{d_shm}; |
669 | |
670 | no strict 'subs'; |
671 | my $sent = "foobar"; |
672 | my $rcvd; |
673 | my $size = 2000; |
674 | my $id = shmget(IPC_PRIVATE, $size, S_IRWXU); |
675 | |
676 | if (defined $id) { |
677 | if (shmwrite($id, $sent, 0, 60)) { |
678 | if (shmread($id, $rcvd, 0, 60)) { |
679 | substr($rcvd, index($rcvd, "\0")) = ''; |
680 | } else { |
681 | warn "# shmread failed: $!\n"; |
682 | } |
683 | } else { |
684 | warn "# shmwrite failed: $!\n"; |
685 | } |
686 | shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n"; |
687 | } else { |
688 | warn "# shmget failed: $!\n"; |
689 | } |
690 | |
691 | skip "SysV shared memory operation failed", 1 unless |
692 | $rcvd eq $sent; |
693 | |
694 | test tainted $rcvd; |
c9f931b8 |
695 | } |
c2e66d9e |
696 | |
d929ce6f |
697 | |
09f04786 |
698 | # test msgrcv |
699 | SKIP: { |
700 | skip "msg*() not available", 1 unless $Config{d_msg}; |
41d6edb2 |
701 | |
b9d1c439 |
702 | no strict 'subs'; |
41d6edb2 |
703 | my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU); |
704 | |
705 | my $sent = "message"; |
706 | my $type_sent = 1234; |
707 | my $rcvd; |
708 | my $type_rcvd; |
709 | |
710 | if (defined $id) { |
ddc3217d |
711 | if (msgsnd($id, pack("l! a*", $type_sent, $sent), IPC_NOWAIT)) { |
712 | if (msgrcv($id, $rcvd, 60, 0, IPC_NOWAIT)) { |
41d6edb2 |
713 | ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd); |
714 | } else { |
ddc3217d |
715 | warn "# msgrcv failed: $!\n"; |
41d6edb2 |
716 | } |
717 | } else { |
ddc3217d |
718 | warn "# msgsnd failed: $!\n"; |
41d6edb2 |
719 | } |
c2e66d9e |
720 | msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n"; |
41d6edb2 |
721 | } else { |
722 | warn "# msgget failed\n"; |
723 | } |
724 | |
09f04786 |
725 | SKIP: { |
726 | skip "SysV message queue operation failed", 1 |
727 | unless $rcvd eq $sent && $type_sent == $type_rcvd; |
728 | |
729 | test tainted $rcvd; |
41d6edb2 |
730 | } |
41d6edb2 |
731 | } |
732 | } |
733 | |
3887d568 |
734 | { |
735 | # bug id 20001004.006 |
736 | |
dc459aad |
737 | open IN, $TEST or warn "$0: cannot read $TEST: $!" ; |
3887d568 |
738 | local $/; |
739 | my $a = <IN>; |
740 | my $b = <IN>; |
09f04786 |
741 | |
742 | ok tainted($a) && tainted($b) && !defined($b); |
743 | |
27c9684d |
744 | close IN; |
3887d568 |
745 | } |
27c9684d |
746 | |
747 | { |
748 | # bug id 20001004.007 |
749 | |
dc459aad |
750 | open IN, $TEST or warn "$0: cannot read $TEST: $!" ; |
27c9684d |
751 | my $a = <IN>; |
752 | |
753 | my $c = { a => 42, |
754 | b => $a }; |
09f04786 |
755 | |
756 | ok !tainted($c->{a}) && tainted($c->{b}); |
757 | |
27c9684d |
758 | |
759 | my $d = { a => $a, |
760 | b => 42 }; |
09f04786 |
761 | ok tainted($d->{a}) && !tainted($d->{b}); |
762 | |
27c9684d |
763 | |
764 | my $e = { a => 42, |
765 | b => { c => $a, d => 42 } }; |
09f04786 |
766 | ok !tainted($e->{a}) && |
767 | !tainted($e->{b}) && |
768 | tainted($e->{b}->{c}) && |
769 | !tainted($e->{b}->{d}); |
27c9684d |
770 | |
771 | close IN; |
772 | } |
773 | |
b94c04ac |
774 | { |
775 | # bug id 20010519.003 |
776 | |
248c32c0 |
777 | BEGIN { |
778 | use vars qw($has_fcntl); |
779 | eval { require Fcntl; import Fcntl; }; |
780 | unless ($@) { |
781 | $has_fcntl = 1; |
782 | } |
783 | } |
b94c04ac |
784 | |
09f04786 |
785 | SKIP: { |
786 | skip "no Fcntl", 18 unless $has_fcntl; |
787 | |
248c32c0 |
788 | my $evil = "foo" . $TAINT; |
789 | |
790 | eval { sysopen(my $ro, $evil, &O_RDONLY) }; |
09f04786 |
791 | test $@ !~ /^Insecure dependency/, $@; |
248c32c0 |
792 | |
793 | eval { sysopen(my $wo, $evil, &O_WRONLY) }; |
09f04786 |
794 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
795 | |
796 | eval { sysopen(my $rw, $evil, &O_RDWR) }; |
09f04786 |
797 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
798 | |
799 | eval { sysopen(my $ap, $evil, &O_APPEND) }; |
09f04786 |
800 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
801 | |
802 | eval { sysopen(my $cr, $evil, &O_CREAT) }; |
09f04786 |
803 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
804 | |
805 | eval { sysopen(my $tr, $evil, &O_TRUNC) }; |
09f04786 |
806 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
807 | |
09f04786 |
808 | eval { sysopen(my $ro, "foo", &O_RDONLY | $TAINT0) }; |
809 | test $@ !~ /^Insecure dependency/, $@; |
248c32c0 |
810 | |
09f04786 |
811 | eval { sysopen(my $wo, "foo", &O_WRONLY | $TAINT0) }; |
812 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
813 | |
09f04786 |
814 | eval { sysopen(my $rw, "foo", &O_RDWR | $TAINT0) }; |
815 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
816 | |
09f04786 |
817 | eval { sysopen(my $ap, "foo", &O_APPEND | $TAINT0) }; |
818 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
819 | |
09f04786 |
820 | eval { sysopen(my $cr, "foo", &O_CREAT | $TAINT0) }; |
821 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
822 | |
09f04786 |
823 | eval { sysopen(my $tr, "foo", &O_TRUNC | $TAINT0) }; |
824 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
825 | |
09f04786 |
826 | eval { sysopen(my $ro, "foo", &O_RDONLY, $TAINT0) }; |
827 | test $@ !~ /^Insecure dependency/, $@; |
248c32c0 |
828 | |
09f04786 |
829 | eval { sysopen(my $wo, "foo", &O_WRONLY, $TAINT0) }; |
830 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
831 | |
09f04786 |
832 | eval { sysopen(my $rw, "foo", &O_RDWR, $TAINT0) }; |
833 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
834 | |
09f04786 |
835 | eval { sysopen(my $ap, "foo", &O_APPEND, $TAINT0) }; |
836 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
837 | |
09f04786 |
838 | eval { sysopen(my $cr, "foo", &O_CREAT, $TAINT0) }; |
839 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
840 | |
09f04786 |
841 | eval { sysopen(my $tr, "foo", &O_TRUNC, $TAINT0) }; |
842 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
843 | |
844 | unlink("foo"); # not unlink($evil), because that would fail... |
845 | } |
b94c04ac |
846 | } |
847 | |
65811bc3 |
848 | { |
849 | # bug 20010526.004 |
850 | |
851 | use warnings; |
852 | |
09f04786 |
853 | my $saw_warning = 0; |
854 | local $SIG{__WARN__} = sub { $saw_warning = 1 }; |
65811bc3 |
855 | |
856 | sub fmi { |
857 | my $divnum = shift()/1; |
858 | sprintf("%1.1f\n", $divnum); |
859 | } |
860 | |
861 | fmi(21 . $TAINT); |
862 | fmi(37); |
863 | fmi(248); |
864 | |
09f04786 |
865 | test !$saw_warning; |
65811bc3 |
866 | } |
867 | |
9e1b5a4e |
868 | |
869 | { |
870 | # Bug ID 20010730.010 |
871 | |
872 | my $i = 0; |
873 | |
874 | sub Tie::TIESCALAR { |
875 | my $class = shift; |
876 | my $arg = shift; |
877 | |
878 | bless \$arg => $class; |
879 | } |
880 | |
881 | sub Tie::FETCH { |
882 | $i ++; |
883 | ${$_ [0]} |
884 | } |
885 | |
886 | |
887 | package main; |
888 | |
889 | my $bar = "The Big Bright Green Pleasure Machine"; |
890 | taint_these $bar; |
891 | tie my ($foo), Tie => $bar; |
892 | |
893 | my $baz = $foo; |
894 | |
09f04786 |
895 | ok $i == 1; |
9e1b5a4e |
896 | } |
897 | |
8852b6d2 |
898 | { |
899 | # Check that all environment variables are tainted. |
900 | my @untainted; |
901 | while (my ($k, $v) = each %ENV) { |
902 | if (!tainted($v) && |
eb25aaf6 |
903 | # These we have explicitly untainted or set earlier. |
904 | $k !~ /^(BASH_ENV|CDPATH|ENV|IFS|PATH|PERL_CORE|TEMP|TERM|TMP)$/) { |
8852b6d2 |
905 | push @untainted, "# '$k' = '$v'\n"; |
906 | } |
907 | } |
09f04786 |
908 | test @untainted == 0, "untainted:\n @untainted"; |
8852b6d2 |
909 | } |
9e1b5a4e |
910 | |
911 | |
9aa05f58 |
912 | ok( ${^TAINT} == 1, '$^TAINT is on' ); |
7c36658b |
913 | |
914 | eval { ${^TAINT} = 0 }; |
915 | ok( ${^TAINT}, '$^TAINT is not assignable' ); |
916 | ok( $@ =~ /^Modification of a read-only value attempted/, |
c212f17f |
917 | 'Assigning to ${^TAINT} fails' ); |
7c36658b |
918 | |
e08e52cf |
919 | { |
920 | # bug 20011111.105 |
921 | |
922 | my $re1 = qr/x$TAINT/; |
09f04786 |
923 | test tainted $re1; |
e08e52cf |
924 | |
925 | my $re2 = qr/^$re1\z/; |
09f04786 |
926 | test tainted $re2; |
e08e52cf |
927 | |
928 | my $re3 = "$re2"; |
09f04786 |
929 | test tainted $re3; |
e08e52cf |
930 | } |
52a55424 |
931 | |
09f04786 |
932 | SKIP: { |
933 | skip "system {} has different semantics on Win32", 1 if $Is_MSWin32; |
934 | |
52a55424 |
935 | # bug 20010221.005 |
936 | local $ENV{PATH} .= $TAINT; |
937 | eval { system { "echo" } "/arg0", "arg1" }; |
09f04786 |
938 | test $@ =~ /^Insecure \$ENV/; |
52a55424 |
939 | } |
09f04786 |
940 | |
941 | TODO: { |
942 | todo_skip 'tainted %ENV warning occludes tainted arguments warning', 22 |
943 | if $Is_VMS; |
944 | |
945 | # bug 20020208.005 plus some single arg exec/system extras |
06bd1802 |
946 | my $err = qr/^Insecure dependency/ ; |
09f04786 |
947 | test !eval { exec $TAINT, $TAINT }, 'exec'; |
948 | test $@ =~ $err, $@; |
949 | test !eval { exec $TAINT $TAINT }, 'exec'; |
950 | test $@ =~ $err, $@; |
951 | test !eval { exec $TAINT $TAINT, $TAINT }, 'exec'; |
952 | test $@ =~ $err, $@; |
953 | test !eval { exec $TAINT 'notaint' }, 'exec'; |
954 | test $@ =~ $err, $@; |
955 | test !eval { exec {'notaint'} $TAINT }, 'exec'; |
956 | test $@ =~ $err, $@; |
957 | |
958 | test !eval { system $TAINT, $TAINT }, 'system'; |
959 | test $@ =~ $err, $@; |
960 | test !eval { system $TAINT $TAINT }, 'system'; |
961 | test $@ =~ $err, $@; |
962 | test !eval { system $TAINT $TAINT, $TAINT }, 'system'; |
963 | test $@ =~ $err, $@; |
964 | test !eval { system $TAINT 'notaint' }, 'system'; |
965 | test $@ =~ $err, $@; |
966 | test !eval { system {'notaint'} $TAINT }, 'system'; |
967 | test $@ =~ $err, $@; |
968 | |
969 | eval { |
970 | no warnings; |
971 | system("lskdfj does not exist","with","args"); |
972 | }; |
973 | test !$@; |
974 | |
975 | SKIP: { |
976 | skip "no exec() on MacOS Classic" if $Is_MacOS; |
977 | |
978 | eval { |
979 | no warnings; |
980 | exec("lskdfj does not exist","with","args"); |
981 | }; |
982 | test !$@; |
63c6dcc1 |
983 | } |
6c8794e1 |
984 | |
985 | # If you add tests here update also the above skip block for VMS. |
bbd7eb8a |
986 | } |
a8c7c11a |
987 | |
988 | { |
989 | # [ID 20020704.001] taint propagation failure |
990 | use re 'taint'; |
991 | $TAINT =~ /(.*)/; |
09f04786 |
992 | test tainted(my $foo = $1); |
a8c7c11a |
993 | } |
7b756e0a |
994 | |
995 | { |
c038024b |
996 | # [perl #24291] this used to dump core |
997 | our %nonmagicalenv = ( PATH => "util" ); |
7b756e0a |
998 | local *ENV = \%nonmagicalenv; |
999 | eval { system("lskdfj"); }; |
09f04786 |
1000 | test $@ =~ /^%ENV is aliased to another variable while running with -T switch/; |
c038024b |
1001 | local *ENV = *nonmagicalenv; |
7b756e0a |
1002 | eval { system("lskdfj"); }; |
09f04786 |
1003 | test $@ =~ /^%ENV is aliased to %nonmagicalenv while running with -T switch/; |
7b756e0a |
1004 | } |
0b4182de |
1005 | { |
1006 | # [perl #24248] |
1007 | $TAINT =~ /(.*)/; |
09f04786 |
1008 | test !tainted($1); |
0b4182de |
1009 | my $notaint = $1; |
09f04786 |
1010 | test !tainted($notaint); |
0b4182de |
1011 | |
1012 | my $l; |
1013 | $notaint =~ /($notaint)/; |
1014 | $l = $1; |
09f04786 |
1015 | test !tainted($1); |
1016 | test !tainted($l); |
0b4182de |
1017 | $notaint =~ /($TAINT)/; |
1018 | $l = $1; |
09f04786 |
1019 | test tainted($1); |
1020 | test tainted($l); |
0b4182de |
1021 | |
1022 | $TAINT =~ /($notaint)/; |
1023 | $l = $1; |
09f04786 |
1024 | test !tainted($1); |
1025 | test !tainted($l); |
0b4182de |
1026 | $TAINT =~ /($TAINT)/; |
1027 | $l = $1; |
09f04786 |
1028 | test tainted($1); |
1029 | test tainted($l); |
0b4182de |
1030 | |
1031 | my $r; |
1032 | ($r = $TAINT) =~ /($notaint)/; |
09f04786 |
1033 | test !tainted($1); |
0b4182de |
1034 | ($r = $TAINT) =~ /($TAINT)/; |
09f04786 |
1035 | test tainted($1); |
3511154c |
1036 | |
1037 | # [perl #24674] |
1038 | # accessing $^O shoudn't taint it as a side-effect; |
1039 | # assigning tainted data to it is now an error |
1040 | |
09f04786 |
1041 | test !tainted($^O); |
3511154c |
1042 | if (!$^X) { } elsif ($^O eq 'bar') { } |
09f04786 |
1043 | test !tainted($^O); |
3511154c |
1044 | eval '$^O = $^X'; |
09f04786 |
1045 | test $@ =~ /Insecure dependency in/; |
0b4182de |
1046 | } |
23634c10 |
1047 | |
1048 | EFFECTIVELY_CONSTANTS: { |
1049 | my $tainted_number = 12 + $TAINT0; |
09f04786 |
1050 | test tainted( $tainted_number ); |
23634c10 |
1051 | |
1052 | # Even though it's always 0, it's still tainted |
1053 | my $tainted_product = $tainted_number * 0; |
09f04786 |
1054 | test tainted( $tainted_product ); |
1055 | test $tainted_product == 0; |
23634c10 |
1056 | } |
1057 | |
1058 | TERNARY_CONDITIONALS: { |
1059 | my $tainted_true = $TAINT . "blah blah blah"; |
1060 | my $tainted_false = $TAINT0; |
09f04786 |
1061 | test tainted( $tainted_true ); |
1062 | test tainted( $tainted_false ); |
23634c10 |
1063 | |
1064 | my $result = $tainted_true ? "True" : "False"; |
09f04786 |
1065 | test $result eq "True"; |
1066 | test !tainted( $result ); |
23634c10 |
1067 | |
1068 | $result = $tainted_false ? "True" : "False"; |
09f04786 |
1069 | test $result eq "False"; |
1070 | test !tainted( $result ); |
23634c10 |
1071 | |
1072 | my $untainted_whatever = "The Fabulous Johnny Cash"; |
1073 | my $tainted_whatever = "Soft Cell" . $TAINT; |
1074 | |
1075 | $result = $tainted_true ? $tainted_whatever : $untainted_whatever; |
09f04786 |
1076 | test $result eq "Soft Cell"; |
1077 | test tainted( $result ); |
23634c10 |
1078 | |
1079 | $result = $tainted_false ? $tainted_whatever : $untainted_whatever; |
09f04786 |
1080 | test $result eq "The Fabulous Johnny Cash"; |
1081 | test !tainted( $result ); |
23634c10 |
1082 | } |
65814f21 |
1083 | |
1084 | { |
1085 | # rt.perl.org 5900 $1 remains tainted if... |
1086 | # 1) The regular expression contains a scalar variable AND |
1087 | # 2) The regular expression appears in an elsif clause |
1088 | |
1089 | my $foo = "abcdefghi" . $TAINT; |
1090 | |
1091 | my $valid_chars = 'a-z'; |
1092 | if ( $foo eq '' ) { |
1093 | } |
1094 | elsif ( $foo =~ /([$valid_chars]+)/o ) { |
1095 | test not tainted $1; |
1096 | } |
1097 | |
1098 | if ( $foo eq '' ) { |
1099 | } |
1100 | elsif ( my @bar = $foo =~ /([$valid_chars]+)/o ) { |
1101 | test not any_tainted @bar; |
1102 | } |
1103 | } |
0a9c116b |
1104 | |
1105 | # at scope exit, a restored localised value should have its old |
1106 | # taint status, not the taint status of the current statement |
1107 | |
1108 | { |
1109 | our $x99 = $^X; |
1110 | test tainted $x99; |
1111 | |
1112 | $x99 = ''; |
1113 | test not tainted $x99; |
1114 | |
1115 | my $c = do { local $x99; $^X }; |
1116 | test not tainted $x99; |
1117 | } |
1118 | { |
1119 | our $x99 = $^X; |
1120 | test tainted $x99; |
1121 | |
1122 | my $c = do { local $x99; '' }; |
1123 | test tainted $x99; |
1124 | } |
1125 | |
27cc343c |
1126 | # an mg_get of a tainted value during localization shouldn't taint the |
1127 | # statement |
1128 | |
1129 | { |
1130 | eval { local $0, eval '1' }; |
1131 | test $@ eq ''; |
1132 | } |
e26a4975 |
1133 | |
1134 | # [perl #8262] //g loops infinitely on tainted data |
1135 | |
1136 | { |
1137 | my @a; |
c7dd0d64 |
1138 | local $::TODO = 1; |
e26a4975 |
1139 | $a[0] = $^X; |
1140 | my $i = 0; |
1141 | while($a[0]=~ m/(.)/g ) { |
1142 | last if $i++ > 10000; |
1143 | } |
86f12da2 |
1144 | cmp_ok $i, '<', 10000, "infinite m//g"; |
e26a4975 |
1145 | } |
0aa395f8 |
1146 | |
1147 | SKIP: |
1148 | { |
1149 | my $got_dualvar; |
1150 | eval 'use Scalar::Util "dualvar"; $got_dualvar++'; |
1151 | skip "No Scalar::Util::dualvar" unless $got_dualvar; |
1152 | my $a = Scalar::Util::dualvar(3, $^X); |
1153 | my $b = $a + 5; |
1154 | is ($b, 8, "Arithmetic on tainted dualvars works"); |
1155 | } |
f27977c3 |
1156 | |
1157 | # opening '|-' should not trigger $ENV{PATH} check |
1158 | |
1159 | { |
1160 | SKIP: { |
1161 | skip "fork() is not available", 3 unless $Config{'d_fork'}; |
b4fda7a3 |
1162 | skip "opening |- is not stable on threaded OpenBSD with taint", 3 |
1163 | if $Config{useithreads} && $Is_OpenBSD; |
f27977c3 |
1164 | |
1165 | $ENV{'PATH'} = $TAINT; |
1166 | local $SIG{'PIPE'} = 'IGNORE'; |
1167 | eval { |
1168 | my $pid = open my $pipe, '|-'; |
1169 | if (!defined $pid) { |
1170 | die "open failed: $!"; |
1171 | } |
1172 | if (!$pid) { |
1173 | kill 'KILL', $$; # child suicide |
1174 | } |
1175 | close $pipe; |
1176 | }; |
1177 | test $@ !~ /Insecure \$ENV/, 'fork triggers %ENV check'; |
1178 | test $@ eq '', 'pipe/fork/open/close failed'; |
1179 | eval { |
1180 | open my $pipe, "|$Invoke_Perl -e 1"; |
1181 | close $pipe; |
1182 | }; |
1183 | test $@ =~ /Insecure \$ENV/, 'popen neglects %ENV check'; |
1184 | } |
1185 | } |
5d121f7f |
1186 | |
1187 | { |
1188 | package AUTOLOAD_TAINT; |
1189 | sub AUTOLOAD { |
1190 | our $AUTOLOAD; |
1191 | return if $AUTOLOAD =~ /DESTROY/; |
1192 | if ($AUTOLOAD =~ /untainted/) { |
1193 | main::ok(!main::tainted($AUTOLOAD), '$AUTOLOAD can be untainted'); |
1194 | } else { |
1195 | main::ok(main::tainted($AUTOLOAD), '$AUTOLOAD can be tainted'); |
1196 | } |
1197 | } |
1198 | |
1199 | package main; |
1200 | my $o = bless [], 'AUTOLOAD_TAINT'; |
1201 | $o->$TAINT; |
1202 | $o->untainted; |
1203 | } |
1204 | |
20ee07fb |
1205 | { |
1206 | # tests for tainted format in s?printf |
1207 | eval { printf($TAINT . "# %s\n", "foo") }; |
1208 | like($@, qr/^Insecure dependency in printf/, q/printf doesn't like tainted formats/); |
1209 | eval { printf("# %s\n", $TAINT . "foo") }; |
1210 | ok(!$@, q/printf accepts other tainted args/); |
1211 | eval { sprintf($TAINT . "# %s\n", "foo") }; |
1212 | like($@, qr/^Insecure dependency in sprintf/, q/sprintf doesn't like tainted formats/); |
1213 | eval { sprintf("# %s\n", $TAINT . "foo") }; |
1214 | ok(!$@, q/sprintf accepts other tainted args/); |
1215 | } |
085bde85 |
1216 | |
1217 | { |
1218 | # 40708 |
1219 | my $n = 7e9; |
1220 | 8e9 - $n; |
1221 | |
1222 | my $val = $n; |
1223 | is ($val, '7000000000', 'Assignment to untainted variable'); |
1224 | $val = $TAINT; |
1225 | $val = $n; |
1226 | is ($val, '7000000000', 'Assignment to tainted variable'); |
1227 | } |
5e500fc8 |
1228 | |
1229 | { |
5e500fc8 |
1230 | my $val = 0; |
1231 | my $tainted = '1' . $TAINT; |
1232 | eval '$val = eval $tainted;'; |
1233 | is ($val, 0, "eval doesn't like tainted strings"); |
1234 | like ($@, qr/^Insecure dependency in eval/); |
1235 | |
abb7fb96 |
1236 | # Rather nice code to get a tainted undef by from Rick Delaney |
1237 | open FH, "test.pl" or die $!; |
af2d3def |
1238 | seek FH, 0, 2 or die $!; |
1239 | $tainted = <FH>; |
5e500fc8 |
1240 | |
1241 | eval 'eval $tainted'; |
1242 | like ($@, qr/^Insecure dependency in eval/); |
1243 | } |
beeaa6fd |
1244 | |
d3706118 |
1245 | foreach my $ord (78, 163, 256) { |
1246 | # 47195 |
1247 | my $line = 'A1' . $TAINT . chr $ord; |
1248 | chop $line; |
1249 | is($line, 'A1'); |
1250 | $line =~ /(A\S*)/; |
d3706118 |
1251 | ok(!tainted($1), "\\S match with chr $ord"); |
1252 | } |
1253 | |
ec93b65f |
1254 | { |
1255 | # 59998 |
1256 | sub cr { my $x = crypt($_[0], $_[1]); $x } |
1257 | sub co { my $x = ~$_[0]; $x } |
1258 | my ($a, $b); |
1259 | $a = cr('hello', 'foo' . $TAINT); |
1260 | $b = cr('hello', 'foo'); |
1261 | ok(tainted($a), "tainted crypt"); |
1262 | ok(!tainted($b), "untainted crypt"); |
1263 | $a = co('foo' . $TAINT); |
1264 | $b = co('foo'); |
1265 | ok(tainted($a), "tainted complement"); |
1266 | ok(!tainted($b), "untainted complement"); |
1267 | } |
1268 | |
302c0c93 |
1269 | { |
1270 | my @data = qw(bonk zam zlonk qunckkk); |
1271 | # Clearly some sort of usenet bang-path |
1272 | my $string = $TAINT . join "!", @data; |
1273 | |
1274 | ok(tainted($string), "tainted data"); |
1275 | |
1276 | my @got = split /!|,/, $string; |
1277 | |
1278 | # each @got would be useful here, but I want the test for earlier perls |
1279 | for my $i (0 .. $#data) { |
1280 | ok(tainted($got[$i]), "tainted result $i"); |
1281 | is($got[$i], $data[$i], "correct content $i"); |
1282 | } |
1283 | |
1284 | ok(tainted($string), "still tainted data"); |
1285 | |
1286 | my @got = split /[!,]/, $string; |
1287 | |
1288 | # each @got would be useful here, but I want the test for earlier perls |
1289 | for my $i (0 .. $#data) { |
1290 | ok(tainted($got[$i]), "tainted result $i"); |
1291 | is($got[$i], $data[$i], "correct content $i"); |
1292 | } |
1293 | |
1294 | ok(tainted($string), "still tainted data"); |
1295 | |
1296 | my @got = split /!/, $string; |
1297 | |
1298 | # each @got would be useful here, but I want the test for earlier perls |
1299 | for my $i (0 .. $#data) { |
1300 | ok(tainted($got[$i]), "tainted result $i"); |
1301 | is($got[$i], $data[$i], "correct content $i"); |
1302 | } |
1303 | } |
1304 | |
3c4fb04a |
1305 | # Bug RT #52552 - broken by change at git commit id f337b08 |
1306 | { |
1307 | my $x = $TAINT. q{print "Hello world\n"}; |
1308 | my $y = pack "a*", $x; |
1309 | ok(tainted($y), "pack a* preserves tainting"); |
1310 | |
1311 | my $z = pack "A*", q{print "Hello world\n"}.$TAINT; |
1312 | ok(tainted($z), "pack A* preserves tainting"); |
1313 | |
1314 | my $zz = pack "a*a*", q{print "Hello world\n"}, $TAINT; |
1315 | ok(tainted($zz), "pack a*a* preserves tainting"); |
1316 | } |
1317 | |
beeaa6fd |
1318 | # This may bomb out with the alarm signal so keep it last |
1319 | SKIP: { |
1320 | skip "No alarm()" unless $Config{d_alarm}; |
1321 | # Test from RT #41831] |
1322 | # [PATCH] Bug & fix: hang when using study + taint mode (perl 5.6.1, 5.8.x) |
1323 | |
1324 | my $DATA = <<'END' . $TAINT; |
1325 | line1 is here |
1326 | line2 is here |
1327 | line3 is here |
1328 | line4 is here |
1329 | |
1330 | END |
1331 | |
1332 | #study $DATA; |
1333 | |
1334 | ## don't set $SIG{ALRM}, since we'd never get to a user-level handler as |
1335 | ## perl is stuck in a regexp infinite loop! |
1336 | |
1337 | alarm(10); |
1338 | |
1339 | if ($DATA =~ /^line2.*line4/m) { |
1340 | fail("Should not be a match") |
1341 | } else { |
1342 | pass("Match on tainted multiline data should fail promptly"); |
1343 | } |
1344 | |
1345 | alarm(0); |
1346 | } |
1347 | __END__ |
1348 | # Keep the previous test last |