This file is indexed.

/usr/lib/perl5/Taint/Runtime.pm is in libtaint-runtime-perl 0.3-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package Taint::Runtime;

=head1 NAME

Taint::Runtime - Runtime enable taint checking

=cut

use strict;
use Exporter;
use vars qw(@ISA %EXPORT_TAGS @EXPORT_OK @EXPORT $VERSION $TAINT);
use XSLoader;

@ISA = qw(Exporter);
%EXPORT_TAGS = (
                'all' => [qw(
                             taint_start
                             taint_stop
                             taint_enabled
                             tainted
                             is_tainted
                             taint
                             untaint
                             taint_env
                             taint_deeply
                             $TAINT
                             ) ],
                );
@EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
@EXPORT = qw(taint_start taint_stop);

$VERSION = '0.03';
XSLoader::load('Taint::Runtime', $VERSION);

###----------------------------------------------------------------###

tie $TAINT, __PACKAGE__;

sub TIESCALAR {
  return bless [], __PACKAGE__;
}

sub FETCH {
  _taint_enabled() ? 1 : 0;
}

sub STORE {
  my ($self, $val) = @_;
  $val = 0 if ! $val || $val eq 'disable';
  $val ? _taint_start() : _taint_stop();
}

###----------------------------------------------------------------###

### allow for special enable/disable keywords
sub import {
  my $change;
  for my $i (reverse 1 .. $#_) {
    next if $_[$i] !~ /^(dis|en)able$/;
    my $val = $1 eq 'dis' ? 0 : 1;
    splice @_, $i, 1, ();
    die 'Cannot both enable and disable $TAINT during import' if defined $change && $change != $val;
    $TAINT = $val;
  }
  __PACKAGE__->export_to_level(1, @_);
}

###----------------------------------------------------------------###

sub taint_start { _taint_start(); }

sub taint_stop  { _taint_stop() }

sub taint_enabled { _taint_enabled() }

sub tainted { _tainted() }

sub is_tainted { return if ! defined $_[0]; ! eval { eval '#'.substr($_[0], 0, 0); 1 } }

# slower on tainted and undef
# modified version from standard lib/perl/5.8.5/tainted.pl
sub is_tainted2 { local $^W = 0; local $@; eval { kill 0 * $_[0] }; $@ =~ /^Insecure/ }

sub taint {
  my $str = shift;
  my $ref = ref($str) ? $str : \$str;
  $$ref = '' if ! defined $$ref;
  $$ref .= tainted();
  return ref($str) ? 1 : $str;
}

sub untaint {
  my $str = shift;
  my $ref = ref($str) ? $str : \$str;
  if (! defined $$ref) {
    $$ref = undef;
  } else {
    $$ref = ($$ref =~ /(.*)/s) ? $1 : do { require Carp; Carp::confess("Couldn't find data to untaint") };
  }
  return ref($str) ? 1 : $str;
}

###----------------------------------------------------------------###

sub taint_env {
  taint_deeply(\%ENV);
}

sub taint_deeply {
  my ($ref, $seen) = @_;

  return if ! defined $ref; # can undefined be tainted ?

  if (! ref $ref) {
    taint \$_[0]; # better be modifyable
    return;

  } elsif (UNIVERSAL::isa($ref, 'SCALAR')) {
    taint $ref;
    return;
  }

  ### avoid circular descent
  $seen ||= {};
  return if $seen->{$ref};
  $seen->{$ref} = 1;

  if (UNIVERSAL::isa($ref, 'ARRAY')) {
    taint_deeply($_, $seen) foreach @$ref;

  } elsif (UNIVERSAL::isa($ref, 'HASH')) {
    while (my ($key, $val) = each %$ref) {
      taint_deeply($key);
      taint_deeply($val, $seen);
      $ref->{$key} = $val;
    }
  } else {
    # not really sure if or what to do for GLOBS or CODE refs
  }
}

###----------------------------------------------------------------###

1;

__END__

=head1 SYNOPSIS

  ### sample "enable" usage

  #!/usr/bin/perl -w
  use Taint::Runtime qw(enable taint_env);
  taint_env();
  # having the keyword enable in the import list starts taint


  ### sample $TAINT usage

  #!/usr/bin/perl -w
  use Taint::Runtime qw($TAINT taint_env);
  $TAINT = 1;
  taint_env();

  # taint is now enabled

  if (1) {
    local $TAINT = 0;

    # do something we trust
  }

  # back to an untrustwory area



  ### sample functional usage

  #!/usr/bin/perl -w
  use strict;
  use Taint::Runtime qw(taint_start is_tainted taint_env
                        taint untaint
                        taint_enabled);

  ### other operations here

  taint_start(); # taint should become active
  taint_env(); # %ENV was previously untainted

  print taint_enabled() ? "enabled\n" : "not enabled\n";

  my $var = taint("some string");

  print is_tainted($var) ? "tainted\n" : "not tainted\n";

  $var = untaint($var);
  # OR
  untaint \$var;

  print is_tainted($var) ? "tainted\n" : "not tainted\n";



=head1 DESCRIPTION

First - you probably shouldn't use this module to control taint.
You should probably use the -T switch on the commandline instead.
There are a somewhat limited number of legitimate use cases where
you should use this module instead of the -T switch.  Unless you
have a specific and good reason for not using the -T option, you
should use the -T option.

Taint is a good thing.  However, few people (that I work with or talk
to or discuss items with) use taint even though they should.  The goal of
this module isn't to use taint less, but to actually encourage its use
more.  This module aims to make using taint as painless as possible (This
can be an argument against it - often implementation of security implies
pain - so taking away pain might lessen security - sort of).

In general - the more secure your script needs to be - the earlier
on in your program that tainting should be enabled.  For most setuid scripts,
you should enable taint by using the -T switch.  Without doing so you allow
for a non-root user to override @INC which allows for them to put their
own module in the place of trusted modules.  This is bad.  This is very bad.
Use the -T switch.

There are some common places where this module may be useful, and where
most people don't use it.  One such place is in a web server.  The -T switch
removes PERL5LIB and PERLLIB and '.' from @INC (or remove them before
they can be added).  This makes sense under setuid.  The use of the -T switch
in a CGI environment may cause a bit of a headache.  For new development,
CGI scripts it may be possible to use the -T switch and for mod_perl environments
there is the PerlTaint variable.  Both of these methods will enable taint
and from that point on development should be done with taint.

However, many (possibly most) perl web server implentations add their
own paths to the PERL5LIB.  All CGI's and mod_perl scripts can then have access.
Using the -T switch throws a wrench into the works as suddenly PERL5LIB
disappears (mod_perl can easily have the extra directories added again
using <perl>push @INC, '/our/lib/dir';</perl>).  The company I work for
has 200 plus user visible scripts mixed with some mod_perl.  Currently
none of the scripts use taint.  We would like for them all to, but it
is not feasible to make the change all at once.  Taint::Runtime allows for moving legacy
scripts over one at a time.

Again, if you are using setuid - don't use this script.

If you are not using setuid and have reasons not to use the -T and are
using this module, make sure that taint is enabled before processing
any user data.  Also remember that BECAUSE THE -T SWITCH WAS NOT USED
%ENV IS INITIALLY NOT MARKED AS TAINTED.  Call taint_env() to mark
it as tainted (especially important in CGI scripts which all read from
$ENV{'QUERY_STRING'}).

If you are not using the -T switch, you most likely should use the
following at the very top of your script:

  #!/usr/bin/perl -w

  use strict;
  use Taint::Runtime qw(enable taint_env);
  taint_env();

Though this module allows for you to turn taint off - you probably shouldn't.
This module is more for you to turn taint on - and once it is on it probably
ought to stay on.

=head1 NON-EXPORTABLE XS FUNCTIONS

The following very basic functions provide the base functionality.

=over 4

=item _taint_start()

Sets PL_tainting

=item _taint_stop()

Sets PL_tainting

=item _taint_enabled()

View of PL_tainting

=item _tainted()

Returns a zero length tainted string.

=back

=head1 $TAINT VARIABLE

The variable $TAINT is tied to the current state of taint.
If $TAINT is set to 0 taint mode is off.  When it is set to
1 taint mode is enabled.

  if (1) {
    local $TAINT = 1;

    # taint is enabled
  }

=head1 EXPORT FUNCTIONS

=over 4

=item enable/disable

Not really functions.  If these keywords are in
the import list, taint will be either enabled
or disabled.

=item taint_start

Start taint mode.  $TAINT will equal 1.

=item taint_stop

Stop taint mode.  $TAINT will equal 0.

=item taint_env

Convenience function that taints the keys and values of %ENV.  If
the -T switch was not used - you most likely should call
this as soon as taint mode is enabled.

=item taint

Taints the passed in variable.  Only works on writeable scalar values.
If a scalar ref is passed in - it is modified.  If a scalar is passed in
(non ref) it is copied, modified and returned.  If a value was undefined,
it becomes a zero length defined and tainted string.

  taint(\$var_to_be_tainted);

  my $tainted_copy = taint($some_var);

For a stronger taint, see the Taint module by Dan Sulgalski which is
capable of tainting most types of data.

=item untaint

Untaints the passed in variable.  Only works on writeable scalar values.
If a scalar ref is passed in - it is modified.  If a scalar is passed in
(non ref) it is copied, modified and returned.  If a value was undefined
it becomes an untainted undefined value.

Note:  Just because the variable is untainted, doesn't mean that it
is safe.  You really should use CGI::Ex::Validate, or Data::FormValidator
or any of the Untaint:: modules.  If you are doing your own validation, and
once you have put the user data through very strict checks, then you
can use untaint.

  if ($var_to_be_untainted =~ /^[\w\.\-]{0,100}$/) {
    untaint(\$var_to_be_untainted);
  }

  my $untainted_copy = untaint($some_var);

=item taint_enabled

Boolean - Is taint on.

=item tainted

Returns a zero length tainted string.

=item is_tainted

Boolean - True if the passed value is tainted.

=item taint_deeply

Convenience function that attempts to deply recurse a
structure and mark it as tainted.  Takes a hashref, arrayref,
scalar ref, or scalar and recursively untaints the structure.

For a stronger taint, see the Taint module by Dan Sulgalski which is
capable of tainting most types of data.

=back

=head1 TURNING TAINT ON

(Be sure to call taint_env() after turning taint on the first time)

  #!/usr/bin/perl -T


  use Taint::Runtime qw(enable);
  # this does not create a function called enable - just starts taint

  use Taint::Runtime qw($TAINT);
  $TAINT = 1;


  use Taint::Runtime qw(taint_start);
  taint_start;


=head1 TURNING TAINT OFF

  use Taint::Runtime qw(disable);
  # this does not create a function called disable - just stops taint


  use Taint::Runtime qw($TAINT);
  $TAINT = 0;


  use Taint::Runtime qw(taint_stop);
  taint_stop;


=head1 CREDITS

C code was provided by "hv" on perlmonks.  This module wouldn't
really be possible without insight into the internals that "hv"
provided.  His post with the code was shown in this node on
perlmonks:

  http://perlmonks.org/?node_id=434086

The basic premise in that node was the following code:

  use Inline C => 'void _start_taint() { PL_tainting = 1; }';
  use Inline C => 'SV* _tainted() { PL_tainted = 1; return newSVpvn("", 0); }';

In this module, these two lines have instead been turned into
XS for runtime speed (and so you won't need Inline and Parse::RecDescent).

Note: even though "hv" provided the base code example, that doesn't mean that he
necessarily endorses the idea.  If there are disagreements, quirks, annoyances
or any other negative side effects with this module - blame me - not "hv."

=head1 THANKS

Thanks to Alexey A. Kiritchun for pointing out untaint failure on multiline strings.

=head1 AUTHOR

Paul Seamons (2005)

C stub functions by "hv" on perlmonks.org

=head1 LICENSE

This module may be used and distributed under the same
terms as Perl itself.

=cut