Bug 606813

Summary: Archive::Tar->extract_archive() does not return proper exit code
Product: Red Hat Enterprise Linux 6 Reporter: Martin Cermak <mcermak>
Component: perlAssignee: Petr Pisar <ppisar>
Status: CLOSED CURRENTRELEASE QA Contact: Martin Cermak <mcermak>
Severity: medium Docs Contact:
Priority: low    
Version: 6.0CC: ovasik, ppisar, psabata, thoger
Target Milestone: rc   
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: perl-5.10.1-113.el6 Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
: 607687 (view as bug list) Environment:
Last Closed: 2010-11-10 21:19:59 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Attachments:
Description Flags
Possible fix
none
Updated patch
none
Hmm, I do see it. Please, try out my "patched" file /usr/share/perl5/Archive/Tar.pm. none

Description Martin Cermak 2010-06-22 14:30:56 UTC
Description of problem:

   The perl-Archive-Tar's Archive::Tar->extract_archive() returns true 
   even if its operation fails: If I do something like this:

#!/usr/bin/perl
use Archive::Tar;
local $Archive::Tar::DEBUG = 0;
local $Archive::Tar::INSECURE_EXTRACT_MODE  =  0;
exit 1 if (!Archive::Tar->extract_archive('some-unexisting-file.tar', 0)); 

   then the script exits with status 1, which is right, because 
   some-unexisting-file.tar does not exist. But when I do something like 

#!/usr/bin/perl
use Archive::Tar;
local $Archive::Tar::DEBUG = 0;
local $Archive::Tar::INSECURE_EXTRACT_MODE  =  0;
exit 1 if (!Archive::Tar->extract_archive('rep-sym-abspath.tar', 0));

   where 

$ tar tvf rep-sym-abspath.tar
lrwxrwxrwx 0/0		     0 2007-09-12 10:59 dir -> /tmp
-rw------- 0/0		     4 2007-09-12 10:59 dir/bar

   then I got error message "...not allowed under SECURE EXTRACT MODE..."
   which is right (see [1] for more info) but the script ends up with 
   zero exit code. Which is IMHO bad. 

Version-Release number of selected component (if applicable):

   perl-Archive-Tar-1.58-109.el6.x86_64

How reproducible:

   Always

Additional info:

   Additionally I can't find the perl-Archive-Tar component among RHEL6
   perl components in Bugzilla. Thus filing this bug against the perl 
   component.


---------------------------------------------------
[1] https://bugzilla.redhat.com/show_bug.cgi?id=295021#c14

Comment 1 Martin Cermak 2010-06-22 14:33:40 UTC
Created attachment 425955 [details]
reproducer file

Comment 2 Tomas Hoger 2010-06-22 20:24:20 UTC
(In reply to comment #0)
> #!/usr/bin/perl
> use Archive::Tar;
> local $Archive::Tar::DEBUG = 0;
> local $Archive::Tar::INSECURE_EXTRACT_MODE  =  0;
> exit 1 if (!Archive::Tar->extract_archive('rep-sym-abspath.tar', 0));
> 
>    where 
> 
> $ tar tvf rep-sym-abspath.tar
> lrwxrwxrwx 0/0       0 2007-09-12 10:59 dir -> /tmp
> -rw------- 0/0       4 2007-09-12 10:59 dir/bar
> 
>    then I got error message "...not allowed under SECURE EXTRACT MODE..."
>    which is right (see [1] for more info) but the script ends up with 
>    zero exit code. Which is IMHO bad. 

Such behaviour is clearly not consistent with Archive::Tar man page.  However, secure mode extract failure is not the only case where you may get such incorrect return value.  Try (as non-root user) e.g.:

cd /etc/
Archive::Tar->extract_archive('/path/to/harmless.tar',0)

>    Additionally I can't find the perl-Archive-Tar component among RHEL6
>    perl components in Bugzilla. Thus filing this bug against the perl 
>    component.

perl-Archive-Tar RPM is now built from perl SRPM.

Comment 3 Martin Cermak 2010-06-23 05:45:31 UTC
I'm not sure if the argumentation like "This is a bug. But I won't take care of it as there are many others" is the best resolution ;-)

Anyway I understand it is not critical. So I will not consider it as a show stopper for https://bugzilla.redhat.com/show_bug.cgi?id=295021#c35.

Comment 4 Tomas Hoger 2010-06-23 07:37:03 UTC
Created attachment 426187 [details]
Possible fix

Comment 5 Tomas Hoger 2010-06-23 07:46:04 UTC
(In reply to comment #3)
> I'm not sure if the argumentation like "This is a bug. But I won't take care of
> it as there are many others" is the best resolution ;-)

I'm fine with addition of the fix to EL4/EL5 updates too, if we can come up with a good fix.  I've attached patch that should fix extract_archive.  However, I'm not sure whether extract_archive return value as currently defined is all that useful.  What if extraction failure happens in the middle of processing of larger archive (due to insecure path, full disk or permission failure)?  Is it better to return names of files that were extracted already (as you may want to remove them) or just indicate failure?

This fix breaks iter with option extract=>1 (Can't use an undefined value as an ARRAY reference at /path/to/Archive/Tar.pm line 1592).

Comment 6 Martin Cermak 2010-06-23 07:53:37 UTC
> Created an attachment (id=426187) [details]
> Possible fix    

In this case I get right exitcode, but loose the error message: "Entry ... is
attempting to extract to a symlinked directory (...). This is considered a
security vulnerability and not allowed under SECURE EXTRACT MODE at ...". 

=> regression.

Comment 7 Tomas Hoger 2010-06-23 08:05:00 UTC
(In reply to comment #6)
> In this case I get right exitcode, but loose the error message: "Entry ... is
> attempting to extract to a symlinked directory (...). This is considered a
> security vulnerability and not allowed under SECURE EXTRACT MODE at ...". 

What is your test case?  I get error msg printed to stderr and set in $Archive::Tar::error too.

Comment 8 Tomas Hoger 2010-06-23 08:21:30 UTC
Created attachment 426194 [details]
Updated patch

Work-around for the extract=>1 iter() breakage mentioned above.  Returns empty list on the first extract failure.  Function does not have error return value defined, so the same return value as end-of-archive.

Comment 9 RHEL Program Management 2010-06-23 08:23:10 UTC
This request was evaluated by Red Hat Product Management for inclusion in a Red
Hat Enterprise Linux major release.  Product Management has requested further
review of this request by Red Hat Engineering, for potential inclusion in a Red
Hat Enterprise Linux Major release.  This request is not yet committed for
inclusion.

Comment 10 Martin Cermak 2010-06-23 08:35:11 UTC
> What is your test case?  I get error msg printed to stderr and 
> set in $Archive::Tar::error too.

Basically my test case with reproducer files you can find in [1] but I'm testing it on RHEL6 this way:


-------------8<----------------------------------------------------------

a6# cat repro.pl 
#!/usr/bin/perl

use Archive::Tar;

local $Archive::Tar::DEBUG = 0;
local $Archive::Tar::INSECURE_EXTRACT_MODE  =  0; 

#exit 1 if (!Archive::Tar->extract_archive('some-unexisting-file.tar', 0));
exit 1 if (!Archive::Tar->extract_archive('rep-sym-abspath.tar', 0));





a6# ./repro.pl 
Making symbolic link '/root/dir' to '/tmp' failed at ./repro.pl line 9
Entry 'dir/bar' is attempting to extract to a symlinked directory (/root/dir => /tmp). This is considered a security vulnerability and not allowed under SECURE EXTRACT MODE at ./repro.pl line 9

-------------8<----------------------------------------------------------


[1] https://bugzilla.redhat.com/show_bug.cgi?id=295021#c14

Comment 11 Tomas Hoger 2010-06-23 08:54:01 UTC
Sorry, I don't see the regression mentioned in comment #6.

Comment 12 Martin Cermak 2010-06-23 10:24:03 UTC
Created attachment 426222 [details]
Hmm, I do see it. Please, try out my "patched" file /usr/share/perl5/Archive/Tar.pm.

Note, that I'm using 

a6# rpm -q perl
perl-5.10.1-109.el6.x86_64

Comment 13 Tomas Hoger 2010-06-23 11:32:29 UTC
$ ./repro.pl ../rep-sym-abspath.tar 
Entry 'dir/bar' is attempting to extract to a symlinked directory (/path/to/dir => /tmp). This is considered a security vulnerability and not allowed under SECURE EXTRACT MODE at ./repro.pl line 8

Error message is printed as expected.

Comment 14 Martin Cermak 2010-06-23 13:16:17 UTC
Re: comment #12: My fault. Need to remove previously extracted "dir" first as discussed via IRC. There is no regression in fact. Thanks for clarification.

=> Patch looks acceptable.

Comment 15 Tomas Hoger 2010-06-23 14:29:06 UTC
(In reply to comment #14)
> Re: comment #12: My fault. Need to remove previously extracted "dir" first as
> discussed via IRC. There is no regression in fact. Thanks for clarification.

The problem here is that _extract_file fails to extract symlink if another file (regular, symlink or directory) with the same name already exists.  Proposed patch causes extract_archive to stop extracting on the first error.

Martin also filed upstream bug:
  https://rt.cpan.org/Public/Bug/Display.html?id=58636

Comment 27 Martin Cermak 2010-07-15 10:24:30 UTC
=> VERIFIED

Comment 28 releng-rhel@redhat.com 2010-11-10 21:19:59 UTC
Red Hat Enterprise Linux 6.0 is now available and should resolve
the problem described in this bug report. This report is therefore being closed
with a resolution of CURRENTRELEASE. You may reopen this bug report if the
solution does not work for you.