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