in reply to Please explain this tainting behaviour

You do have a syntactical error in the script. One of the lines should read as follows:

eval { kill 0 * $_[0] };

Note the use of square brackets instead of parenthesis.

Also, you asked about why only $_[0] is evaluated. The sub tainted() is apparently intended to take a single scalar as its argument. That scalar is held (loosely speaking) as the first element of the special variable @_. You can read up on that in perlvar and perlsub.


Dave

Replies are listed 'Best First'.
Re^2: Please explain this tainting behaviour
by Anonymous Monk on Aug 05, 2004 at 19:14 UTC
    Thanks for the replies everyone, makes sense. The bracket problem I meant to mention - when I previewed the post the brackets weren't coming out, so i put parentheses in.

    To get some more clarification - do I need to be running -T for this to work? I was hoping I could just pass all my parameters through it and get a "this data is OK" or "not OK".

      Yes, you'll have to use -T to enable taint checking. It cannot do your work for you, though. It is merely prevents you from accidentally using unfiltered user input to perform dangerous operations. The onus for defining what data is well-formed and safe to accept and what's not, though, is still on you.

      The only way to get untainted data from a tainted variable is to perform a pattern match, and capture some or all of the data. The captured data is then untainted. F.ex, if you have an input value that must only consist of digits, you could untaint it like so:

      unless( $some_user_input =~ /^(\d+)$/ ) { die "You did not pass only digits for some_input\n"; # or you produce an error page here or send the user back if it's +a CGI, f.ex } my $untainted_user_input = $1;

      Now you can perform dangerous operations using $untainted_user_input.

      Of course, nothing stops you from using /(.*)/s as the test pattern, therefor accepting any input at all and thus defeating the point of taint checks.

      Ovid's excellent CGI course has an enlightening chapter on how to untaint data sensibly, treating taint checking as an ally that will help you avoid getting exploited.

      There are modules on CPAN that will help you with common untainting tasks — look for the various Untaint modules.

      Makeshifts last the longest.