// back

Clam AntiVirus Win32-UPX Buffer Overflow Vulnerability

09.19.05

BACKGROUND

Clam Antivirus is a GPL anti-virus toolkit for Unix. It is used in
several anti-virus gateways and available for free on multiple platforms
(Unix, Windows, MacOS X, BeOS, etc.).

More information about Clam AntiVirus is available from:

  http://www.clamav.net/

DESCRIPTION

Remote exploitation of an Input Validation Error in Clam AntiVirus
ClamAV allows attackers to cause a denial of service (DoS) condition.

UPX (Ultimate Executable Packer) is a compressor for small Win32 EXE
files. Remote exploitation of an unexpected condition in the
decompression of UPX files allow attackers to cause a denial of service
condition. Remote code execution is theoretically possible, however
would be non-trivial given the huge size of the buffer (nearly 4GB).

The vulnerability exists due to a signedness issue in the pefromupx()
function [libclamav/upx.c] when dealing with the number of UPX sections.

int pefromupx(...)
{
  char *imports, *sections, *pehdr, *newbuf;
  int sectcnt, upd=1, realstuffsz;
  int foffset=0xd0+0xf8;

  [...]

  if ( ! (sectcnt = pehdr[6]+256*pehdr[7])) {
    [...]
  }
  foffset+=0x28*sectcnt;

  [...]

  if (!(newbuf = (char *) cli_malloc(foffset))) {
    [...]
  }

  [...]

  memcpy(newbuf+0xd0, pehdr,0xf8+0x28*sectcnt);

  [...]
}

The number of UPX sections ('sectcnt') is a signed integer calculated
using signed characters ('pehdr') from the file. By setting pehdr[6]
and/or pehdr[7] to negative values (0x80-0xff), 'sectcnt' can be forced
to be negative.

If 'foofset' is positive for the cli_malloc() to succeed (negative
malloc() is verified by ClamAV) and '0xf8+0x28* sectcnt' is negative,
the process will crash on the memcpy() call due to heap corruption.

This can happen for the following values of pehdr[6]: 0xf5, 0xf6, 0xf7,
0xf8 and 0xf9. pehdr[7] must remain at 0x00.

ANALYSIS

Successful exploitation allows attackers to overflow a buffer allocated
in the heap. Anti-virus detection functionality can be left in a
defunct state depending on the configuration (analysis process
automatically restarted or not). This can be considered as a denial of
service.

Remote execution of arbitrary code is not easy due to the huge value of
the memcpy() size (nearly 4GB), but theoretically possible.

Remote exploitation can be achieved by sending a malicious file in an
e-mail message or during an HTTP session.

DETECTION

This issue was tested to work with ClamAV 0.86. The vulnerable code
was added in Clam AntiVirus 0.81. All versions between .81 and .86
are believed vulnerable.

WORKAROUND

The following patch can be applied to fix the vulnerability:

diff -ruN clamav-0.86.1/libclamav/upx.c
clamav-0.86.1-upx-fix/libclamav/upx.c
--- clamav-0.86.1/libclamav/upx.c       Thu Jun 23 22:03:12 2005
+++ clamav-0.86.1-upx-fix/libclamav/upx.c       Tue Jul  5 15:08:32 2005
@@ -117,7 +117,7 @@
   }

   sections = pehdr+0xf8;
-  if ( ! (sectcnt = pehdr[6]+256*pehdr[7])) {
+  if ( ! (sectcnt = (unsigned char)pehdr[6]+256*(unsigned char)pehdr[7]))
{
     cli_dbgmsg("UPX: No sections? - giving up rebuildn");
     return 0;
   }

Windows PE EXE files analysis can be disabled (--no-pe) but this can
have severe impact on the virus detection functionality.

VENDOR RESPONSE

ClamAV has released version 0.87 to correct this issue.

  http://sourceforge.net/project/showfiles.php?group_id=86638

CVE INFORMATION

The Common Vulnerabilities and Exposures (CVE) project has assigned the
name CAN-2005-2920 to this issue. This is a candidate for inclusion in
the CVE list (http://cve.mitre.org), which standardizes names for
security problems.

DISCLOSURE TIMELINE

09/08/2005   Initial vendor notification
09/19/2005   Initial vendor notification
09/19/2005   Public disclosure

CREDIT

The discoverer of this vulnerability wishes to remain anonymous.

Get paid for vulnerability research
http://www.idefense.com/poi/teams/vcp.jsp

Free tools, research and upcoming events
http://labs.idefense.com

LEGAL NOTICES

Copyright © 2005 Verisign, Inc.

Permission is granted for the redistribution of this alert
electronically. It may not be edited in any way without the express
written consent of iDEFENSE. If you wish to reprint the whole or any
part of this alert in any other medium other than electronically, please
email customerservice@idefense.com for permission.

Disclaimer: The information in the advisory is believed to be accurate
at the time of publishing based on currently available information. Use
of the information constitutes acceptance for use in an AS IS condition.
There are no warranties with regard to this information. Neither the
author nor the publisher accepts any liability for any direct, indirect,
or consequential loss or damage arising from use of, or reliance on,
this information.