summaryrefslogtreecommitdiff
path: root/t/op/taint.t
blob: 32277181f608c40115e34d535e3c1dc2b06aa5db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#!./perl -T
#
# Taint tests by Tom Phoenix <rootbeer@teleport.com>.
#
# I don't claim to know all about tainting. If anyone sees
# tests that I've missed here, please add them. But this is 
# better than having no tests at all, right?
#

BEGIN {
    chdir 't' if -d 't';
    @INC = '../lib' if -d '../lib';
}

use strict;
use Config;

my $Is_VMS = $^O eq 'VMS';
my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' : './perl';
if ($Is_VMS) {
    eval <<EndOfCleanup;
	END {
	    \$ENV{PATH} = '';
	    warn "# Note: logical name 'PATH' may have been deleted\n";
	    \$ENV{IFS} =  "$ENV{IFS}";
	    \$ENV{'DCL\$PATH'} = "$ENV{'DCL$PATH'}";
	}
EndOfCleanup
}

# Sources of taint:
#   The empty tainted value, for tainting strings
my $TAINT = substr($^X, 0, 0);
#   A tainted zero, useful for tainting numbers
my $TAINT0 = 0 + $TAINT;

# This taints each argument passed. All must be lvalues.
# Side effect: It also stringifies them. :-(
sub taint_these (@) {
    for (@_) { $_ .= $TAINT }
}

# How to identify taint when you see it
sub any_tainted (@) {
    not eval { join("",@_), kill 0; 1 };
}
sub tainted ($) {
    any_tainted @_;
}
sub all_tainted (@) {
    for (@_) { return 0 unless tainted $_ }
    1;
}

sub test ($$;$) {
    my($serial, $boolean, $diag) = @_;
    if ($boolean) {
	print "ok $serial\n";
    } else {
	print "not ok $serial\n";
	for (split m/^/m, $diag) {
	    print "# $_";
	}
	print "\n" unless 
	    $diag eq ''
	    or substr($diag, -1) eq "\n";
    }
}

# We need an external program to call.
my $ECHO = "./echo$$";
END { unlink $ECHO }
open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
print PROG 'print "@ARGV\n"', "\n";
close PROG;
my $echo = "$Invoke_Perl $ECHO";

print "1..96\n";

# First, let's make sure that Perl is checking the dangerous
# environment variables. Maybe they aren't set yet, so we'll
# taint them ourselves.
{
    $ENV{'DCL$PATH'} = '' if $Is_VMS;

    $ENV{PATH} = $TAINT;
    $ENV{IFS} = '';
    test 1, eval { `$echo 1` } eq '';
    test 2, $@ =~ /^Insecure \$ENV{PATH}/, $@;

    $ENV{PATH} = '';
    $ENV{IFS} = $TAINT;
    test 3, eval { `$echo 1` } eq '';
    test 4, $@ =~ /^Insecure \$ENV{IFS}/, $@;

    my ($tmp) = grep { (stat)[2] & 2 } '/tmp', '/var/tmp', '/usr/tmp';
    if ($tmp) {
	$ENV{PATH} = $tmp;
	test 5, eval { `$echo 1` } eq '';
	test 6, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
    }
    else {
	print "# can't find writeable directory to test PATH tainting\n";
	for (5..6) { print "ok $_\n" }
    }

    $ENV{PATH} = '';
    $ENV{IFS} = '';
    test 7, eval { `$echo 1` } eq "1\n";
    test 8, $@ eq '', $@;

    if ($Is_VMS) {
	$ENV{'DCL$PATH'} = $TAINT;
	test 9,  eval { `$echo 1` } eq '';
	test 10, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
	$ENV{'DCL$PATH'} = '';
    }
    else {
	print "# This is not VMS\n";
	for (9..10) { print "ok $_\n"; }
    }
}

# Let's see that we can taint and untaint as needed.
{
    my $foo = $TAINT;
    test 11, tainted $foo;

    $foo = "foo";
    test 12, not tainted $foo;

    taint_these($foo);
    test 13, tainted $foo;

    my @list = 1..10;
    test 14, not any_tainted @list;
    taint_these @list[1,3,5,7,9];
    test 15, any_tainted @list;
    test 16, all_tainted @list[1,3,5,7,9];
    test 17, not any_tainted @list[0,2,4,6,8];

    ($foo) = $foo =~ /(.+)/;
    test 18, not tainted $foo;

    $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
    test 19, not tainted $foo;
    test 20, $foo eq 'bar';

    my $pi = 4 * atan2(1,1) + $TAINT0;
    test 21, tainted $pi;

    ($pi) = $pi =~ /(\d+\.\d+)/;
    test 22, not tainted $pi;
    test 23, sprintf("%.5f", $pi) eq '3.14159';
}

# How about command-line arguments? The problem is that we don't
# always get some, so we'll run another process with some.
{
    my $arg = "./arg$$";
    open PROG, "> $arg" or die "Can't create $arg: $!";
    print PROG q{
	eval { join('', @ARGV), kill 0 };
	exit 0 if $@ =~ /^Insecure dependency/;
	print "# Oops: \$@ was [$@]\n";
	exit 1;
    };
    close PROG;
    print `$Invoke_Perl "-T" $arg and some suspect arguments`;
    test 24, !$?, "Exited with status $?";
    unlink $arg;
}

# Reading from a file should be tainted
{
    my $file = './perl' . $Config{exe_ext};
    test 25, open(FILE, $file), "Couldn't open '$file': $!";

    my $block;
    sysread(FILE, $block, 100);
    my $line = <FILE>;		# Should "work"
    close FILE;
    test 26, tainted $block;
    test 27, tainted $line;
}

# Globs should be tainted. 
{
    my @globs = <*>;
    test 28, all_tainted @globs;

    @globs = glob '*';
    test 29, all_tainted @globs;
}

# Output of commands should be tainted
{
    my $foo = `$echo abc`;
    test 30, tainted $foo;
}

# Certain system variables should be tainted
{
    test 31, all_tainted $^X, $0;
}

# Results of matching should all be untainted
{
    my $foo = "abcdefghi" . $TAINT;
    test 32, tainted $foo;

    $foo =~ /def/;
    test 33, not any_tainted $`, $&, $';

    $foo =~ /(...)(...)(...)/;
    test 34, not any_tainted $1, $2, $3, $+;

    my @bar = $foo =~ /(...)(...)(...)/;
    test 35, not any_tainted @bar;

    test 36, tainted $foo;	# $foo should still be tainted!
    test 37, $foo eq "abcdefghi";
}

# Operations which affect files can't use tainted data.
{
    test 38, eval { chmod 0, $TAINT } eq '', 'chmod';
    test 39, $@ =~ /^Insecure dependency/, $@;

    test 40, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate';
    test 41, $@ =~ /^Insecure dependency/, $@;

    test 42, eval { rename '', $TAINT } eq '', 'rename';
    test 43, $@ =~ /^Insecure dependency/, $@;

    test 44, eval { unlink $TAINT } eq '', 'unlink';
    test 45, $@ =~ /^Insecure dependency/, $@;

    test 46, eval { utime $TAINT } eq '', 'utime';
    test 47, $@ =~ /^Insecure dependency/, $@;

    if ($Config{d_chown}) {
	test 48, eval { chown -1, -1, $TAINT } eq '', 'chown';
	test 49, $@ =~ /^Insecure dependency/, $@;
    }
    else {
	print "# chown() is not available\n";
	for (48..49) { print "ok $_\n" }
    }

    if ($Config{d_link}) {
	test 50, eval { link $TAINT, '' } eq '', 'link';
	test 51, $@ =~ /^Insecure dependency/, $@;
    }
    else {
	print "# link() is not available\n";
	for (50..51) { print "ok $_\n" }
    }

    if ($Config{d_symlink}) {
	test 52, eval { symlink $TAINT, '' } eq '', 'symlink';
	test 53, $@ =~ /^Insecure dependency/, $@;
    }
    else {
	print "# symlink() is not available\n";
	for (52..53) { print "ok $_\n" }
    }
}

# Operations which affect directories can't use tainted data.
{
    test 54, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir';
    test 55, $@ =~ /^Insecure dependency/, $@;

    test 56, eval { rmdir $TAINT } eq '', 'rmdir';
    test 57, $@ =~ /^Insecure dependency/, $@;

    test 58, eval { chdir $TAINT } eq '', 'chdir';
    test 59, $@ =~ /^Insecure dependency/, $@;

    if ($Config{d_chroot}) {
	test 60, eval { chroot $TAINT } eq '', 'chroot';
	test 61, $@ =~ /^Insecure dependency/, $@;
    }
    else {
	print "# chroot() is not available\n";
	for (60..61) { print "ok $_\n" }
    }
}

# Some operations using files can't use tainted data.
{
    my $foo = "imaginary library" . $TAINT;
    test 62, eval { require $foo } eq '', 'require';
    test 63, $@ =~ /^Insecure dependency/, $@;

    my $filename = "./taintB$$";	# NB: $filename isn't tainted!
    END { unlink $filename if defined $filename }
    $foo = $filename . $TAINT;
    unlink $filename;	# in any case

    test 64, eval { open FOO, $foo } eq '', 'open for read';
    test 65, $@ eq '', $@;		# NB: This should be allowed
    test 66, $! == 2;			# File not found

    test 67, eval { open FOO, "> $foo" } eq '', 'open for write';
    test 68, $@ =~ /^Insecure dependency/, $@;
}

# Commands to the system can't use tainted data
{
    my $foo = $TAINT;

    if ($^O eq 'amigaos') {
	print "# open(\"|\") is not available\n";
	for (69..72) { print "ok $_\n" }
    }
    else {
	test 69, eval { open FOO, "| $foo" } eq '', 'popen to';
	test 70, $@ =~ /^Insecure dependency/, $@;

	test 71, eval { open FOO, "$foo |" } eq '', 'popen from';
	test 72, $@ =~ /^Insecure dependency/, $@;
    }

    test 73, eval { exec $TAINT } eq '', 'exec';
    test 74, $@ =~ /^Insecure dependency/, $@;

    test 75, eval { system $TAINT } eq '', 'system';
    test 76, $@ =~ /^Insecure dependency/, $@;

    $foo = "*";
    taint_these $foo;

    test 77, eval { `$echo 1$foo` } eq '', 'backticks';
    test 78, $@ =~ /^Insecure dependency/, $@;

    if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe
	test 79, join('', eval { glob $foo } ) ne '', 'globbing';
	test 80, $@ eq '', $@;
    }
    else {
	test 79, join('', eval { glob $foo } ) eq '', 'globbing';
	test 80, $@ =~ /^Insecure dependency/, $@;
    }
}

# Operations which affect processes can't use tainted data.
{
    test 81, eval { kill 0, $TAINT } eq '', 'kill';
    test 82, $@ =~ /^Insecure dependency/, $@;

    if ($Config{d_setpgrp}) {
	test 83, eval { setpgrp 0, $TAINT } eq '', 'setpgrp';
	test 84, $@ =~ /^Insecure dependency/, $@;
    }
    else {
	print "# setpgrp() is not available\n";
	for (83..84) { print "ok $_\n" }
    }

    if ($Config{d_setprior}) {
	test 85, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority';
	test 86, $@ =~ /^Insecure dependency/, $@;
    }
    else {
	print "# setpriority() is not available\n";
	for (85..86) { print "ok $_\n" }
    }
}

# Some miscellaneous operations can't use tainted data.
{
    if ($Config{d_syscall}) {
	test 87, eval { syscall $TAINT } eq '', 'syscall';
	test 88, $@ =~ /^Insecure dependency/, $@;
    }
    else {
	print "# syscall() is not available\n";
	for (87..88) { print "ok $_\n" }
    }

    {
	my $foo = "x" x 979;
	taint_these $foo;
	local *FOO;
	my $temp = "./taintC$$";
	END { unlink $temp }
	test 89, open(FOO, "> $temp"), "Couldn't open $temp for write: $!";

	test 90, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl';
	test 91, $@ =~ /^Insecure dependency/, $@;

	if ($Config{d_fcntl}) {
	    test 92, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl';
	    test 93, $@ =~ /^Insecure dependency/, $@;
	}
	else {
	    print "# fcntl() is not available\n";
	    for (92..93) { print "ok $_\n" }
	}

	close FOO;
    }
}

# Some tests involving references 
{
    my $foo = 'abc' . $TAINT;
    my $fooref = \$foo;
    test 94, not tainted $fooref;
    test 95, tainted $$fooref;
    test 96, tainted $foo;
}