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; | |
17 | ||
0ecd3ba2 MG |
18 | $| = 1; |
19 | ||
9d116dd7 JH |
20 | # We do not want the whole taint.t to fail |
21 | # just because Errno possibly failing. | |
22 | eval { require Errno; import Errno }; | |
23 | ||
c9f931b8 JH |
24 | use vars qw($ipcsysv); # did we manage to load IPC::SysV? |
25 | ||
3eeba6fb CB |
26 | BEGIN { |
27 | if ($^O eq 'VMS' && !defined($Config{d_setenv})) { | |
28 | $ENV{PATH} = $ENV{PATH}; | |
29 | $ENV{TERM} = $ENV{TERM} ne ''? $ENV{TERM} : 'dummy'; | |
30 | } | |
be3174d2 GS |
31 | if ($Config{'extensions'} =~ /\bIPC\/SysV\b/ |
32 | && ($Config{d_shm} || $Config{d_msg})) { | |
c9f931b8 JH |
33 | eval { require IPC::SysV }; |
34 | unless ($@) { | |
35 | $ipcsysv++; | |
36 | IPC::SysV->import(qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU)); | |
37 | } | |
b9d1c439 | 38 | } |
3eeba6fb CB |
39 | } |
40 | ||
1e422769 | 41 | my $Is_VMS = $^O eq 'VMS'; |
68dc0745 | 42 | my $Is_MSWin32 = $^O eq 'MSWin32'; |
2986a63f | 43 | my $Is_NetWare = $^O eq 'NetWare'; |
39e571d4 | 44 | my $Is_Dos = $^O eq 'dos'; |
343d3f38 | 45 | my $Is_Cygwin = $^O eq 'cygwin'; |
68dc0745 | 46 | my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' : |
2986a63f JH |
47 | ($Is_MSWin32 ? '.\perl' : |
48 | ($Is_NetWare ? 'perl' : './perl')); | |
c90c0ff4 | 49 | my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/; |
7bac28a0 | 50 | |
1e422769 | 51 | if ($Is_VMS) { |
7bac28a0 | 52 | my (%old, $x); |
53 | for $x ('DCL$PATH', @MoreEnv) { | |
54 | ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x}; | |
55 | } | |
1e422769 | 56 | eval <<EndOfCleanup; |
57 | END { | |
3eeba6fb | 58 | \$ENV{PATH} = '' if $Config{d_setenv}; |
1e422769 | 59 | warn "# Note: logical name 'PATH' may have been deleted\n"; |
562a7b0c | 60 | \@ENV{keys %old} = values %old; |
1e422769 | 61 | } |
62 | EndOfCleanup | |
63 | } | |
64 | ||
65 | # Sources of taint: | |
66 | # The empty tainted value, for tainting strings | |
67 | my $TAINT = substr($^X, 0, 0); | |
68 | # A tainted zero, useful for tainting numbers | |
69 | my $TAINT0 = 0 + $TAINT; | |
70 | ||
71 | # This taints each argument passed. All must be lvalues. | |
72 | # Side effect: It also stringifies them. :-( | |
73 | sub taint_these (@) { | |
74 | for (@_) { $_ .= $TAINT } | |
75 | } | |
76 | ||
77 | # How to identify taint when you see it | |
78 | sub any_tainted (@) { | |
79 | not eval { join("",@_), kill 0; 1 }; | |
80 | } | |
81 | sub tainted ($) { | |
82 | any_tainted @_; | |
83 | } | |
84 | sub all_tainted (@) { | |
85 | for (@_) { return 0 unless tainted $_ } | |
86 | 1; | |
87 | } | |
88 | ||
89 | sub test ($$;$) { | |
90 | my($serial, $boolean, $diag) = @_; | |
91 | if ($boolean) { | |
92 | print "ok $serial\n"; | |
93 | } else { | |
94 | print "not ok $serial\n"; | |
95 | for (split m/^/m, $diag) { | |
96 | print "# $_"; | |
97 | } | |
9607fc9c | 98 | print "\n" unless |
1e422769 | 99 | $diag eq '' |
100 | or substr($diag, -1) eq "\n"; | |
101 | } | |
102 | } | |
103 | ||
104 | # We need an external program to call. | |
2986a63f | 105 | my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : ($Is_NetWare ? "echo$$" : "./echo$$")); |
1e422769 | 106 | END { unlink $ECHO } |
107 | open PROG, "> $ECHO" or die "Can't create $ECHO: $!"; | |
108 | print PROG 'print "@ARGV\n"', "\n"; | |
109 | close PROG; | |
110 | my $echo = "$Invoke_Perl $ECHO"; | |
111 | ||
8852b6d2 | 112 | print "1..176\n"; |
1e422769 | 113 | |
114 | # First, let's make sure that Perl is checking the dangerous | |
115 | # environment variables. Maybe they aren't set yet, so we'll | |
116 | # taint them ourselves. | |
117 | { | |
118 | $ENV{'DCL$PATH'} = '' if $Is_VMS; | |
119 | ||
7bac28a0 | 120 | $ENV{PATH} = ''; |
c90c0ff4 | 121 | delete @ENV{@MoreEnv}; |
7bac28a0 | 122 | $ENV{TERM} = 'dumb'; |
123 | ||
8852b6d2 | 124 | if ($Is_Cygwin && ! -f 'cygwin1.dll') { |
343d3f38 JH |
125 | system("/usr/bin/cp /usr/bin/cygwin1.dll .") && |
126 | die "$0: failed to cp cygwin1.dll: $!\n"; | |
127 | END { unlink "cygwin1.dll" } # yes, done for all platforms... | |
128 | } | |
129 | ||
7bac28a0 | 130 | test 1, eval { `$echo 1` } eq "1\n"; |
131 | ||
2986a63f | 132 | if ($Is_MSWin32 || $Is_NetWare || $Is_VMS || $Is_Dos) { |
7bac28a0 | 133 | print "# Environment tainting tests skipped\n"; |
c90c0ff4 | 134 | for (2..5) { print "ok $_\n" } |
5aabfad6 | 135 | } |
136 | else { | |
7bac28a0 | 137 | my @vars = ('PATH', @MoreEnv); |
138 | while (my $v = $vars[0]) { | |
139 | local $ENV{$v} = $TAINT; | |
140 | last if eval { `$echo 1` }; | |
141 | last unless $@ =~ /^Insecure \$ENV{$v}/; | |
142 | shift @vars; | |
143 | } | |
144 | test 2, !@vars, "\$$vars[0]"; | |
c90c0ff4 | 145 | |
146 | # tainted $TERM is unsafe only if it contains metachars | |
147 | local $ENV{TERM}; | |
148 | $ENV{TERM} = 'e=mc2'; | |
149 | test 3, eval { `$echo 1` } eq "1\n"; | |
150 | $ENV{TERM} = 'e=mc2' . $TAINT; | |
151 | test 4, eval { `$echo 1` } eq ''; | |
152 | test 5, $@ =~ /^Insecure \$ENV{TERM}/, $@; | |
5aabfad6 | 153 | } |
7bac28a0 | 154 | |
9607fc9c | 155 | my $tmp; |
2986a63f | 156 | if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_NetWare || $Is_Dos) { |
48c036b1 GS |
157 | print "# all directories are writeable\n"; |
158 | } | |
159 | else { | |
9607fc9c | 160 | $tmp = (grep { defined and -d and (stat _)[2] & 2 } |
099f76bb | 161 | qw(sys$scratch /tmp /var/tmp /usr/tmp), |
9607fc9c | 162 | @ENV{qw(TMP TEMP)})[0] |
163 | or print "# can't find world-writeable directory to test PATH\n"; | |
164 | } | |
165 | ||
385588b3 | 166 | if ($tmp) { |
7bac28a0 | 167 | local $ENV{PATH} = $tmp; |
c90c0ff4 | 168 | test 6, eval { `$echo 1` } eq ''; |
169 | test 7, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@; | |
1e422769 | 170 | } |
171 | else { | |
fac76ed7 | 172 | for (6..7) { print "ok $_ # Skipped: all directories are writeable\n" } |
1e422769 | 173 | } |
174 | ||
1e422769 | 175 | if ($Is_VMS) { |
176 | $ENV{'DCL$PATH'} = $TAINT; | |
c90c0ff4 | 177 | test 8, eval { `$echo 1` } eq ''; |
178 | test 9, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@; | |
9607fc9c | 179 | if ($tmp) { |
180 | $ENV{'DCL$PATH'} = $tmp; | |
c90c0ff4 | 181 | test 10, eval { `$echo 1` } eq ''; |
182 | test 11, $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@; | |
9607fc9c | 183 | } |
184 | else { | |
fac76ed7 | 185 | for (10..11) { print "ok $_ # Skipped: can't find world-writeable directory to test DCL\$PATH\n" } |
9607fc9c | 186 | } |
1e422769 | 187 | $ENV{'DCL$PATH'} = ''; |
188 | } | |
189 | else { | |
fac76ed7 | 190 | for (8..11) { print "ok $_ # Skipped: This is not VMS\n"; } |
1e422769 | 191 | } |
192 | } | |
193 | ||
194 | # Let's see that we can taint and untaint as needed. | |
195 | { | |
196 | my $foo = $TAINT; | |
c90c0ff4 | 197 | test 12, tainted $foo; |
9607fc9c | 198 | |
199 | # That was a sanity check. If it failed, stop the insanity! | |
200 | die "Taint checks don't seem to be enabled" unless tainted $foo; | |
1e422769 | 201 | |
202 | $foo = "foo"; | |
c90c0ff4 | 203 | test 13, not tainted $foo; |
1e422769 | 204 | |
205 | taint_these($foo); | |
c90c0ff4 | 206 | test 14, tainted $foo; |
1e422769 | 207 | |
208 | my @list = 1..10; | |
c90c0ff4 | 209 | test 15, not any_tainted @list; |
1e422769 | 210 | taint_these @list[1,3,5,7,9]; |
c90c0ff4 | 211 | test 16, any_tainted @list; |
212 | test 17, all_tainted @list[1,3,5,7,9]; | |
213 | test 18, not any_tainted @list[0,2,4,6,8]; | |
1e422769 | 214 | |
215 | ($foo) = $foo =~ /(.+)/; | |
c90c0ff4 | 216 | test 19, not tainted $foo; |
1e422769 | 217 | |
218 | $foo = $1 if ('bar' . $TAINT) =~ /(.+)/; | |
c90c0ff4 | 219 | test 20, not tainted $foo; |
220 | test 21, $foo eq 'bar'; | |
1e422769 | 221 | |
b3eb6a9b GS |
222 | { |
223 | use re 'taint'; | |
224 | ||
225 | ($foo) = ('bar' . $TAINT) =~ /(.+)/; | |
226 | test 22, tainted $foo; | |
227 | test 23, $foo eq 'bar'; | |
228 | ||
229 | $foo = $1 if ('bar' . $TAINT) =~ /(.+)/; | |
230 | test 24, tainted $foo; | |
231 | test 25, $foo eq 'bar'; | |
232 | } | |
233 | ||
234 | $foo = $1 if 'bar' =~ /(.+)$TAINT/; | |
235 | test 26, tainted $foo; | |
236 | test 27, $foo eq 'bar'; | |
48c036b1 | 237 | |
1e422769 | 238 | my $pi = 4 * atan2(1,1) + $TAINT0; |
b3eb6a9b | 239 | test 28, tainted $pi; |
1e422769 | 240 | |
241 | ($pi) = $pi =~ /(\d+\.\d+)/; | |
b3eb6a9b GS |
242 | test 29, not tainted $pi; |
243 | test 30, sprintf("%.5f", $pi) eq '3.14159'; | |
1e422769 | 244 | } |
245 | ||
246 | # How about command-line arguments? The problem is that we don't | |
247 | # always get some, so we'll run another process with some. | |
248 | { | |
249 | my $arg = "./arg$$"; | |
250 | open PROG, "> $arg" or die "Can't create $arg: $!"; | |
251 | print PROG q{ | |
252 | eval { join('', @ARGV), kill 0 }; | |
253 | exit 0 if $@ =~ /^Insecure dependency/; | |
254 | print "# Oops: \$@ was [$@]\n"; | |
255 | exit 1; | |
256 | }; | |
257 | close PROG; | |
258 | print `$Invoke_Perl "-T" $arg and some suspect arguments`; | |
b3eb6a9b | 259 | test 31, !$?, "Exited with status $?"; |
1e422769 | 260 | unlink $arg; |
261 | } | |
262 | ||
263 | # Reading from a file should be tainted | |
264 | { | |
9607fc9c | 265 | my $file = './TEST'; |
b3eb6a9b | 266 | test 32, open(FILE, $file), "Couldn't open '$file': $!"; |
1e422769 | 267 | |
268 | my $block; | |
269 | sysread(FILE, $block, 100); | |
9607fc9c | 270 | my $line = <FILE>; |
1e422769 | 271 | close FILE; |
b3eb6a9b GS |
272 | test 33, tainted $block; |
273 | test 34, tainted $line; | |
1e422769 | 274 | } |
275 | ||
c90c0ff4 | 276 | # Globs should be forbidden, except under VMS, |
277 | # which doesn't spawn an external program. | |
72b16652 GS |
278 | if (1 # built-in glob |
279 | or $Is_VMS) { | |
b3eb6a9b | 280 | for (35..36) { print "ok $_\n"; } |
c90c0ff4 | 281 | } |
282 | else { | |
7bac28a0 | 283 | my @globs = eval { <*> }; |
b3eb6a9b | 284 | test 35, @globs == 0 && $@ =~ /^Insecure dependency/; |
1e422769 | 285 | |
7bac28a0 | 286 | @globs = eval { glob '*' }; |
b3eb6a9b | 287 | test 36, @globs == 0 && $@ =~ /^Insecure dependency/; |
1e422769 | 288 | } |
289 | ||
290 | # Output of commands should be tainted | |
291 | { | |
292 | my $foo = `$echo abc`; | |
b3eb6a9b | 293 | test 37, tainted $foo; |
1e422769 | 294 | } |
295 | ||
296 | # Certain system variables should be tainted | |
297 | { | |
b3eb6a9b | 298 | test 38, all_tainted $^X, $0; |
1e422769 | 299 | } |
300 | ||
301 | # Results of matching should all be untainted | |
302 | { | |
303 | my $foo = "abcdefghi" . $TAINT; | |
b3eb6a9b | 304 | test 39, tainted $foo; |
1e422769 | 305 | |
306 | $foo =~ /def/; | |
b3eb6a9b | 307 | test 40, not any_tainted $`, $&, $'; |
1e422769 | 308 | |
309 | $foo =~ /(...)(...)(...)/; | |
b3eb6a9b | 310 | test 41, not any_tainted $1, $2, $3, $+; |
1e422769 | 311 | |
312 | my @bar = $foo =~ /(...)(...)(...)/; | |
b3eb6a9b | 313 | test 42, not any_tainted @bar; |
1e422769 | 314 | |
b3eb6a9b GS |
315 | test 43, tainted $foo; # $foo should still be tainted! |
316 | test 44, $foo eq "abcdefghi"; | |
1e422769 | 317 | } |
318 | ||
319 | # Operations which affect files can't use tainted data. | |
320 | { | |
b3eb6a9b GS |
321 | test 45, eval { chmod 0, $TAINT } eq '', 'chmod'; |
322 | test 46, $@ =~ /^Insecure dependency/, $@; | |
1e422769 | 323 | |
9607fc9c | 324 | # There is no feature test in $Config{} for truncate, |
325 | # so we allow for the possibility that it's missing. | |
b3eb6a9b GS |
326 | test 47, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate'; |
327 | test 48, $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@; | |
1e422769 | 328 | |
b3eb6a9b GS |
329 | test 49, eval { rename '', $TAINT } eq '', 'rename'; |
330 | test 50, $@ =~ /^Insecure dependency/, $@; | |
1e422769 | 331 | |
b3eb6a9b GS |
332 | test 51, eval { unlink $TAINT } eq '', 'unlink'; |
333 | test 52, $@ =~ /^Insecure dependency/, $@; | |
9607fc9c | 334 | |
b3eb6a9b GS |
335 | test 53, eval { utime $TAINT } eq '', 'utime'; |
336 | test 54, $@ =~ /^Insecure dependency/, $@; | |
48c036b1 | 337 | |
1e422769 | 338 | if ($Config{d_chown}) { |
b3eb6a9b GS |
339 | test 55, eval { chown -1, -1, $TAINT } eq '', 'chown'; |
340 | test 56, $@ =~ /^Insecure dependency/, $@; | |
1e422769 | 341 | } |
342 | else { | |
b3eb6a9b | 343 | for (55..56) { print "ok $_ # Skipped: chown() is not available\n" } |
1e422769 | 344 | } |
345 | ||
346 | if ($Config{d_link}) { | |
b3eb6a9b GS |
347 | test 57, eval { link $TAINT, '' } eq '', 'link'; |
348 | test 58, $@ =~ /^Insecure dependency/, $@; | |
1e422769 | 349 | } |
350 | else { | |
b3eb6a9b | 351 | for (57..58) { print "ok $_ # Skipped: link() is not available\n" } |
1e422769 | 352 | } |
353 | ||
354 | if ($Config{d_symlink}) { | |
b3eb6a9b GS |
355 | test 59, eval { symlink $TAINT, '' } eq '', 'symlink'; |
356 | test 60, $@ =~ /^Insecure dependency/, $@; | |
1e422769 | 357 | } |
358 | else { | |
b3eb6a9b | 359 | for (59..60) { print "ok $_ # Skipped: symlink() is not available\n" } |
1e422769 | 360 | } |
361 | } | |
362 | ||
363 | # Operations which affect directories can't use tainted data. | |
364 | { | |
b3eb6a9b GS |
365 | test 61, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir'; |
366 | test 62, $@ =~ /^Insecure dependency/, $@; | |
1e422769 | 367 | |
b3eb6a9b GS |
368 | test 63, eval { rmdir $TAINT } eq '', 'rmdir'; |
369 | test 64, $@ =~ /^Insecure dependency/, $@; | |
9607fc9c | 370 | |
b3eb6a9b GS |
371 | test 65, eval { chdir $TAINT } eq '', 'chdir'; |
372 | test 66, $@ =~ /^Insecure dependency/, $@; | |
48c036b1 | 373 | |
1e422769 | 374 | if ($Config{d_chroot}) { |
b3eb6a9b GS |
375 | test 67, eval { chroot $TAINT } eq '', 'chroot'; |
376 | test 68, $@ =~ /^Insecure dependency/, $@; | |
1e422769 | 377 | } |
378 | else { | |
b3eb6a9b | 379 | for (67..68) { print "ok $_ # Skipped: chroot() is not available\n" } |
1e422769 | 380 | } |
381 | } | |
382 | ||
383 | # Some operations using files can't use tainted data. | |
384 | { | |
385 | my $foo = "imaginary library" . $TAINT; | |
b3eb6a9b GS |
386 | test 69, eval { require $foo } eq '', 'require'; |
387 | test 70, $@ =~ /^Insecure dependency/, $@; | |
1e422769 | 388 | |
389 | my $filename = "./taintB$$"; # NB: $filename isn't tainted! | |
390 | END { unlink $filename if defined $filename } | |
391 | $foo = $filename . $TAINT; | |
392 | unlink $filename; # in any case | |
393 | ||
b3eb6a9b GS |
394 | test 71, eval { open FOO, $foo } eq '', 'open for read'; |
395 | test 72, $@ eq '', $@; # NB: This should be allowed | |
9d116dd7 JH |
396 | |
397 | # Try first new style but allow also old style. | |
61ae2fbf JH |
398 | test 73, $!{ENOENT} || |
399 | $! == 2 || # File not found | |
400 | ($Is_Dos && $! == 22) || | |
401 | ($^O eq 'mint' && $! == 33); | |
1e422769 | 402 | |
b3eb6a9b GS |
403 | test 74, eval { open FOO, "> $foo" } eq '', 'open for write'; |
404 | test 75, $@ =~ /^Insecure dependency/, $@; | |
1e422769 | 405 | } |
406 | ||
407 | # Commands to the system can't use tainted data | |
408 | { | |
409 | my $foo = $TAINT; | |
410 | ||
411 | if ($^O eq 'amigaos') { | |
b3eb6a9b | 412 | for (76..79) { print "ok $_ # Skipped: open('|') is not available\n" } |
1e422769 | 413 | } |
414 | else { | |
06eaf0bc | 415 | test 76, eval { open FOO, "| x$foo" } eq '', 'popen to'; |
b3eb6a9b | 416 | test 77, $@ =~ /^Insecure dependency/, $@; |
1e422769 | 417 | |
06eaf0bc | 418 | test 78, eval { open FOO, "x$foo |" } eq '', 'popen from'; |
b3eb6a9b | 419 | test 79, $@ =~ /^Insecure dependency/, $@; |
48c036b1 | 420 | } |
1e422769 | 421 | |
b3eb6a9b GS |
422 | test 80, eval { exec $TAINT } eq '', 'exec'; |
423 | test 81, $@ =~ /^Insecure dependency/, $@; | |
9607fc9c | 424 | |
b3eb6a9b GS |
425 | test 82, eval { system $TAINT } eq '', 'system'; |
426 | test 83, $@ =~ /^Insecure dependency/, $@; | |
48c036b1 | 427 | |
1e422769 | 428 | $foo = "*"; |
429 | taint_these $foo; | |
430 | ||
b3eb6a9b GS |
431 | test 84, eval { `$echo 1$foo` } eq '', 'backticks'; |
432 | test 85, $@ =~ /^Insecure dependency/, $@; | |
1e422769 | 433 | |
434 | if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe | |
b3eb6a9b GS |
435 | test 86, join('', eval { glob $foo } ) ne '', 'globbing'; |
436 | test 87, $@ eq '', $@; | |
1e422769 | 437 | } |
438 | else { | |
b3eb6a9b | 439 | for (86..87) { print "ok $_ # Skipped: this is not VMS\n"; } |
1e422769 | 440 | } |
441 | } | |
442 | ||
443 | # Operations which affect processes can't use tainted data. | |
444 | { | |
b3eb6a9b GS |
445 | test 88, eval { kill 0, $TAINT } eq '', 'kill'; |
446 | test 89, $@ =~ /^Insecure dependency/, $@; | |
1e422769 | 447 | |
448 | if ($Config{d_setpgrp}) { | |
b3eb6a9b GS |
449 | test 90, eval { setpgrp 0, $TAINT } eq '', 'setpgrp'; |
450 | test 91, $@ =~ /^Insecure dependency/, $@; | |
1e422769 | 451 | } |
452 | else { | |
b3eb6a9b | 453 | for (90..91) { print "ok $_ # Skipped: setpgrp() is not available\n" } |
1e422769 | 454 | } |
455 | ||
456 | if ($Config{d_setprior}) { | |
b3eb6a9b GS |
457 | test 92, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority'; |
458 | test 93, $@ =~ /^Insecure dependency/, $@; | |
1e422769 | 459 | } |
460 | else { | |
b3eb6a9b | 461 | for (92..93) { print "ok $_ # Skipped: setpriority() is not available\n" } |
1e422769 | 462 | } |
463 | } | |
464 | ||
465 | # Some miscellaneous operations can't use tainted data. | |
466 | { | |
467 | if ($Config{d_syscall}) { | |
b3eb6a9b GS |
468 | test 94, eval { syscall $TAINT } eq '', 'syscall'; |
469 | test 95, $@ =~ /^Insecure dependency/, $@; | |
1e422769 | 470 | } |
471 | else { | |
b3eb6a9b | 472 | for (94..95) { print "ok $_ # Skipped: syscall() is not available\n" } |
1e422769 | 473 | } |
474 | ||
475 | { | |
476 | my $foo = "x" x 979; | |
477 | taint_these $foo; | |
478 | local *FOO; | |
479 | my $temp = "./taintC$$"; | |
480 | END { unlink $temp } | |
b3eb6a9b | 481 | test 96, open(FOO, "> $temp"), "Couldn't open $temp for write: $!"; |
1e422769 | 482 | |
b3eb6a9b GS |
483 | test 97, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl'; |
484 | test 98, $@ =~ /^Insecure dependency/, $@; | |
1e422769 | 485 | |
486 | if ($Config{d_fcntl}) { | |
b3eb6a9b GS |
487 | test 99, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl'; |
488 | test 100, $@ =~ /^Insecure dependency/, $@; | |
1e422769 | 489 | } |
490 | else { | |
b3eb6a9b | 491 | for (99..100) { print "ok $_ # Skipped: fcntl() is not available\n" } |
1e422769 | 492 | } |
493 | ||
494 | close FOO; | |
495 | } | |
496 | } | |
497 | ||
9607fc9c | 498 | # Some tests involving references |
1e422769 | 499 | { |
500 | my $foo = 'abc' . $TAINT; | |
501 | my $fooref = \$foo; | |
b3eb6a9b GS |
502 | test 101, not tainted $fooref; |
503 | test 102, tainted $$fooref; | |
504 | test 103, tainted $foo; | |
1e422769 | 505 | } |
54310121 | 506 | |
507 | # Some tests involving assignment | |
508 | { | |
509 | my $foo = $TAINT0; | |
510 | my $bar = $foo; | |
b3eb6a9b GS |
511 | test 104, all_tainted $foo, $bar; |
512 | test 105, tainted($foo = $bar); | |
513 | test 106, tainted($bar = $bar); | |
514 | test 107, tainted($bar += $bar); | |
515 | test 108, tainted($bar -= $bar); | |
516 | test 109, tainted($bar *= $bar); | |
517 | test 110, tainted($bar++); | |
518 | test 111, tainted($bar /= $bar); | |
519 | test 112, tainted($bar += 0); | |
520 | test 113, tainted($bar -= 2); | |
521 | test 114, tainted($bar *= -1); | |
522 | test 115, tainted($bar /= 1); | |
523 | test 116, tainted($bar--); | |
524 | test 117, $bar == 0; | |
54310121 | 525 | } |
a1f49e72 CS |
526 | |
527 | # Test assignment and return of lists | |
528 | { | |
529 | my @foo = ("A", "tainted" . $TAINT, "B"); | |
b3eb6a9b GS |
530 | test 118, not tainted $foo[0]; |
531 | test 119, tainted $foo[1]; | |
532 | test 120, not tainted $foo[2]; | |
a1f49e72 | 533 | my @bar = @foo; |
b3eb6a9b GS |
534 | test 121, not tainted $bar[0]; |
535 | test 122, tainted $bar[1]; | |
536 | test 123, not tainted $bar[2]; | |
a1f49e72 | 537 | my @baz = eval { "A", "tainted" . $TAINT, "B" }; |
b3eb6a9b GS |
538 | test 124, not tainted $baz[0]; |
539 | test 125, tainted $baz[1]; | |
540 | test 126, not tainted $baz[2]; | |
a1f49e72 | 541 | my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ]; |
b3eb6a9b GS |
542 | test 127, not tainted $plugh[0]; |
543 | test 128, tainted $plugh[1]; | |
544 | test 129, not tainted $plugh[2]; | |
a1f49e72 | 545 | my $nautilus = sub { "A", "tainted" . $TAINT, "B" }; |
b3eb6a9b GS |
546 | test 130, not tainted ((&$nautilus)[0]); |
547 | test 131, tainted ((&$nautilus)[1]); | |
548 | test 132, not tainted ((&$nautilus)[2]); | |
a1f49e72 | 549 | my @xyzzy = &$nautilus; |
b3eb6a9b GS |
550 | test 133, not tainted $xyzzy[0]; |
551 | test 134, tainted $xyzzy[1]; | |
552 | test 135, not tainted $xyzzy[2]; | |
a1f49e72 | 553 | my $red_october = sub { return "A", "tainted" . $TAINT, "B" }; |
b3eb6a9b GS |
554 | test 136, not tainted ((&$red_october)[0]); |
555 | test 137, tainted ((&$red_october)[1]); | |
556 | test 138, not tainted ((&$red_october)[2]); | |
a1f49e72 | 557 | my @corge = &$red_october; |
b3eb6a9b GS |
558 | test 139, not tainted $corge[0]; |
559 | test 140, tainted $corge[1]; | |
560 | test 141, not tainted $corge[2]; | |
a1f49e72 | 561 | } |
fb73857a | 562 | |
563 | # Test for system/library calls returning string data of dubious origin. | |
564 | { | |
565 | # No reliable %Config check for getpw* | |
566 | if (eval { setpwent(); getpwent(); 1 }) { | |
567 | setpwent(); | |
568 | my @getpwent = getpwent(); | |
569 | die "getpwent: $!\n" unless (@getpwent); | |
b3eb6a9b | 570 | test 142,( not tainted $getpwent[0] |
2959b6e3 | 571 | and tainted $getpwent[1] |
fb73857a | 572 | and not tainted $getpwent[2] |
573 | and not tainted $getpwent[3] | |
574 | and not tainted $getpwent[4] | |
575 | and not tainted $getpwent[5] | |
3972a534 | 576 | and tainted $getpwent[6] # ge?cos |
fb73857a | 577 | and not tainted $getpwent[7] |
3972a534 | 578 | and tainted $getpwent[8]); # shell |
fb73857a | 579 | endpwent(); |
580 | } else { | |
b3eb6a9b | 581 | for (142) { print "ok $_ # Skipped: getpwent() is not available\n" } |
fb73857a | 582 | } |
583 | ||
584 | if ($Config{d_readdir}) { # pretty hard to imagine not | |
585 | local(*D); | |
586 | opendir(D, "op") or die "opendir: $!\n"; | |
587 | my $readdir = readdir(D); | |
b3eb6a9b | 588 | test 143, tainted $readdir; |
fb73857a | 589 | closedir(OP); |
590 | } else { | |
b3eb6a9b | 591 | for (143) { print "ok $_ # Skipped: readdir() is not available\n" } |
fb73857a | 592 | } |
593 | ||
594 | if ($Config{d_readlink} && $Config{d_symlink}) { | |
595 | my $symlink = "sl$$"; | |
596 | unlink($symlink); | |
597 | symlink("/something/naughty", $symlink) or die "symlink: $!\n"; | |
598 | my $readlink = readlink($symlink); | |
b3eb6a9b | 599 | test 144, tainted $readlink; |
fb73857a | 600 | unlink($symlink); |
601 | } else { | |
b3eb6a9b | 602 | for (144) { print "ok $_ # Skipped: readlink() or symlink() is not available\n"; } |
fb73857a | 603 | } |
604 | } | |
605 | ||
606 | # test bitwise ops (regression bug) | |
607 | { | |
608 | my $why = "y"; | |
609 | my $j = "x" | $why; | |
b3eb6a9b | 610 | test 145, not tainted $j; |
fb73857a | 611 | $why = $TAINT."y"; |
612 | $j = "x" | $why; | |
b3eb6a9b | 613 | test 146, tainted $j; |
fb73857a | 614 | } |
615 | ||
48c036b1 GS |
616 | # test target of substitution (regression bug) |
617 | { | |
618 | my $why = $TAINT."y"; | |
619 | $why =~ s/y/z/; | |
b3eb6a9b | 620 | test 147, tainted $why; |
48c036b1 GS |
621 | |
622 | my $z = "[z]"; | |
623 | $why =~ s/$z/zee/; | |
b3eb6a9b | 624 | test 148, tainted $why; |
48c036b1 GS |
625 | |
626 | $why =~ s/e/'-'.$$/ge; | |
b3eb6a9b | 627 | test 149, tainted $why; |
48c036b1 | 628 | } |
d929ce6f JH |
629 | |
630 | # test shmread | |
631 | { | |
c9f931b8 JH |
632 | unless ($ipcsysv) { |
633 | print "ok 150 # skipped: no IPC::SysV\n"; | |
634 | last; | |
635 | } | |
be3174d2 | 636 | if ($Config{'extensions'} =~ /\bIPC\/SysV\b/ && $Config{d_shm}) { |
b9d1c439 | 637 | no strict 'subs'; |
d929ce6f JH |
638 | my $sent = "foobar"; |
639 | my $rcvd; | |
640 | my $size = 2000; | |
c2e66d9e GS |
641 | my $id = shmget(IPC_PRIVATE, $size, S_IRWXU); |
642 | ||
41d6edb2 JH |
643 | if (defined $id) { |
644 | if (shmwrite($id, $sent, 0, 60)) { | |
645 | if (shmread($id, $rcvd, 0, 60)) { | |
d929ce6f JH |
646 | substr($rcvd, index($rcvd, "\0")) = ''; |
647 | } else { | |
648 | warn "# shmread failed: $!\n"; | |
649 | } | |
650 | } else { | |
651 | warn "# shmwrite failed: $!\n"; | |
652 | } | |
c2e66d9e | 653 | shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n"; |
41d6edb2 JH |
654 | } else { |
655 | warn "# shmget failed: $!\n"; | |
d929ce6f JH |
656 | } |
657 | ||
658 | if ($rcvd eq $sent) { | |
659 | test 150, tainted $rcvd; | |
660 | } else { | |
661 | print "ok 150 # Skipped: SysV shared memory operation failed\n"; | |
662 | } | |
663 | } else { | |
41d6edb2 | 664 | print "ok 150 # Skipped: SysV shared memory is not available\n"; |
d929ce6f JH |
665 | } |
666 | } | |
41d6edb2 JH |
667 | |
668 | # test msgrcv | |
669 | { | |
c9f931b8 JH |
670 | unless ($ipcsysv) { |
671 | print "ok 151 # skipped: no IPC::SysV\n"; | |
672 | last; | |
673 | } | |
be3174d2 | 674 | if ($Config{'extensions'} =~ /\bIPC\/SysV\b/ && $Config{d_msg}) { |
b9d1c439 | 675 | no strict 'subs'; |
41d6edb2 JH |
676 | my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU); |
677 | ||
678 | my $sent = "message"; | |
679 | my $type_sent = 1234; | |
680 | my $rcvd; | |
681 | my $type_rcvd; | |
682 | ||
683 | if (defined $id) { | |
684 | if (msgsnd($id, pack("l! a*", $type_sent, $sent), 0)) { | |
685 | if (msgrcv($id, $rcvd, 60, 0, 0)) { | |
686 | ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd); | |
687 | } else { | |
688 | warn "# msgrcv failed\n"; | |
689 | } | |
690 | } else { | |
691 | warn "# msgsnd failed\n"; | |
692 | } | |
c2e66d9e | 693 | msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n"; |
41d6edb2 JH |
694 | } else { |
695 | warn "# msgget failed\n"; | |
696 | } | |
697 | ||
698 | if ($rcvd eq $sent && $type_sent == $type_rcvd) { | |
699 | test 151, tainted $rcvd; | |
700 | } else { | |
701 | print "ok 151 # Skipped: SysV message queue operation failed\n"; | |
702 | } | |
703 | } else { | |
704 | print "ok 151 # Skipped: SysV message queues are not available\n"; | |
705 | } | |
706 | } | |
707 | ||
3887d568 AP |
708 | { |
709 | # bug id 20001004.006 | |
710 | ||
711 | open IN, "./TEST" or warn "$0: cannot read ./TEST: $!" ; | |
712 | local $/; | |
713 | my $a = <IN>; | |
714 | my $b = <IN>; | |
715 | print "not " unless tainted($a) && tainted($b) && !defined($b); | |
716 | print "ok 152\n"; | |
27c9684d | 717 | close IN; |
3887d568 | 718 | } |
27c9684d AP |
719 | |
720 | { | |
721 | # bug id 20001004.007 | |
722 | ||
723 | open IN, "./TEST" or warn "$0: cannot read ./TEST: $!" ; | |
724 | my $a = <IN>; | |
725 | ||
726 | my $c = { a => 42, | |
727 | b => $a }; | |
728 | print "not " unless !tainted($c->{a}) && tainted($c->{b}); | |
729 | print "ok 153\n"; | |
730 | ||
731 | my $d = { a => $a, | |
732 | b => 42 }; | |
733 | print "not " unless tainted($d->{a}) && !tainted($d->{b}); | |
734 | print "ok 154\n"; | |
735 | ||
736 | my $e = { a => 42, | |
737 | b => { c => $a, d => 42 } }; | |
738 | print "not " unless !tainted($e->{a}) && | |
739 | !tainted($e->{b}) && | |
740 | tainted($e->{b}->{c}) && | |
741 | !tainted($e->{b}->{d}); | |
742 | print "ok 155\n"; | |
743 | ||
744 | close IN; | |
745 | } | |
746 | ||
b94c04ac JH |
747 | { |
748 | # bug id 20010519.003 | |
749 | ||
248c32c0 JH |
750 | BEGIN { |
751 | use vars qw($has_fcntl); | |
752 | eval { require Fcntl; import Fcntl; }; | |
753 | unless ($@) { | |
754 | $has_fcntl = 1; | |
755 | } | |
756 | } | |
b94c04ac | 757 | |
248c32c0 JH |
758 | unless ($has_fcntl) { |
759 | for (156..173) { | |
760 | print "ok $_ # Skip: no Fcntl (no dynaloading?)\n"; | |
761 | } | |
762 | } else { | |
763 | my $evil = "foo" . $TAINT; | |
764 | ||
765 | eval { sysopen(my $ro, $evil, &O_RDONLY) }; | |
766 | test 156, $@ !~ /^Insecure dependency/, $@; | |
767 | ||
768 | eval { sysopen(my $wo, $evil, &O_WRONLY) }; | |
769 | test 157, $@ =~ /^Insecure dependency/, $@; | |
770 | ||
771 | eval { sysopen(my $rw, $evil, &O_RDWR) }; | |
772 | test 158, $@ =~ /^Insecure dependency/, $@; | |
773 | ||
774 | eval { sysopen(my $ap, $evil, &O_APPEND) }; | |
775 | test 159, $@ =~ /^Insecure dependency/, $@; | |
776 | ||
777 | eval { sysopen(my $cr, $evil, &O_CREAT) }; | |
778 | test 160, $@ =~ /^Insecure dependency/, $@; | |
779 | ||
780 | eval { sysopen(my $tr, $evil, &O_TRUNC) }; | |
781 | test 161, $@ =~ /^Insecure dependency/, $@; | |
782 | ||
783 | eval { sysopen(my $ro, "foo", &O_RDONLY | $evil) }; | |
784 | test 162, $@ !~ /^Insecure dependency/, $@; | |
785 | ||
786 | eval { sysopen(my $wo, "foo", &O_WRONLY | $evil) }; | |
787 | test 163, $@ =~ /^Insecure dependency/, $@; | |
788 | ||
789 | eval { sysopen(my $rw, "foo", &O_RDWR | $evil) }; | |
790 | test 164, $@ =~ /^Insecure dependency/, $@; | |
791 | ||
792 | eval { sysopen(my $ap, "foo", &O_APPEND | $evil) }; | |
793 | test 165, $@ =~ /^Insecure dependency/, $@; | |
794 | ||
795 | eval { sysopen(my $cr, "foo", &O_CREAT | $evil) }; | |
796 | test 166, $@ =~ /^Insecure dependency/, $@; | |
797 | ||
798 | eval { sysopen(my $tr, "foo", &O_TRUNC | $evil) }; | |
799 | test 167, $@ =~ /^Insecure dependency/, $@; | |
800 | ||
801 | eval { sysopen(my $ro, "foo", &O_RDONLY, $evil) }; | |
802 | test 168, $@ !~ /^Insecure dependency/, $@; | |
803 | ||
804 | eval { sysopen(my $wo, "foo", &O_WRONLY, $evil) }; | |
805 | test 169, $@ =~ /^Insecure dependency/, $@; | |
806 | ||
807 | eval { sysopen(my $rw, "foo", &O_RDWR, $evil) }; | |
808 | test 170, $@ =~ /^Insecure dependency/, $@; | |
809 | ||
810 | eval { sysopen(my $ap, "foo", &O_APPEND, $evil) }; | |
811 | test 171, $@ =~ /^Insecure dependency/, $@; | |
812 | ||
813 | eval { sysopen(my $cr, "foo", &O_CREAT, $evil) }; | |
814 | test 172, $@ =~ /^Insecure dependency/, $@; | |
815 | ||
816 | eval { sysopen(my $tr, "foo", &O_TRUNC, $evil) }; | |
817 | test 173, $@ =~ /^Insecure dependency/, $@; | |
818 | ||
819 | unlink("foo"); # not unlink($evil), because that would fail... | |
820 | } | |
b94c04ac JH |
821 | } |
822 | ||
65811bc3 JH |
823 | { |
824 | # bug 20010526.004 | |
825 | ||
826 | use warnings; | |
827 | ||
828 | $SIG{__WARN__} = sub { print "not " }; | |
829 | ||
830 | sub fmi { | |
831 | my $divnum = shift()/1; | |
832 | sprintf("%1.1f\n", $divnum); | |
833 | } | |
834 | ||
835 | fmi(21 . $TAINT); | |
836 | fmi(37); | |
837 | fmi(248); | |
838 | ||
839 | print "ok 174\n"; | |
840 | } | |
841 | ||
9e1b5a4e A |
842 | |
843 | { | |
844 | # Bug ID 20010730.010 | |
845 | ||
846 | my $i = 0; | |
847 | ||
848 | sub Tie::TIESCALAR { | |
849 | my $class = shift; | |
850 | my $arg = shift; | |
851 | ||
852 | bless \$arg => $class; | |
853 | } | |
854 | ||
855 | sub Tie::FETCH { | |
856 | $i ++; | |
857 | ${$_ [0]} | |
858 | } | |
859 | ||
860 | ||
861 | package main; | |
862 | ||
863 | my $bar = "The Big Bright Green Pleasure Machine"; | |
864 | taint_these $bar; | |
865 | tie my ($foo), Tie => $bar; | |
866 | ||
867 | my $baz = $foo; | |
868 | ||
869 | print $i == 1 ? "ok 175\n" : "not ok 175\n" | |
870 | ||
871 | } | |
872 | ||
8852b6d2 JH |
873 | { |
874 | # Check that all environment variables are tainted. | |
875 | my @untainted; | |
876 | while (my ($k, $v) = each %ENV) { | |
877 | if (!tainted($v) && | |
878 | # These we have untainted explicitly earlier. | |
879 | $k !~ /^(BASH_ENV|CDPATH|ENV|IFS|PATH|TEMP|TERM|TMP)$/) { | |
880 | push @untainted, "# '$k' = '$v'\n"; | |
881 | } | |
882 | } | |
883 | print @untainted == 0 ? "ok 176\n" : "not ok 176\n"; | |
884 | print "# untainted:\n", @untainted if @untainted; | |
885 | } | |
9e1b5a4e A |
886 | |
887 |