Bug 63201 (CVE-2004-1013)

Summary: VUL-0: CVE-2004-1013: cyrus-imapd multiple remote vulnerabilites
Product: [Novell Products] SUSE Security Incidents Reporter: Marcus Meissner <meissner>
Component: IncidentsAssignee: Security Team bot <security-team>
Status: RESOLVED FIXED QA Contact: Security Team bot <security-team>
Severity: Critical    
Priority: P3 - Medium CC: security-team
Version: unspecified   
Target Milestone: ---   
Hardware: All   
OS: Linux   
Whiteboard: CVE-2004-1013: CVSS v2 Base Score: 10.0 (AV:N/AC:L/Au:N/C:C/I:C/A:C)
Found By: --- Services Priority:
Business Priority: Blocker: ---
Marketing QA Status: --- IT Deployment: ---
Attachments: imapd-bugdemo.tgz
my patch proposal for 2.2.8
New patch, obsoleting old one
the patch upstream is using
patchinfo-file box
patchinfo for maintained
New patch fixing same bugs which shoed also up in proxyd.c

Description Marcus Meissner 2004-11-12 21:37:49 UTC
From: Stefan Esser <sesser@php.net>                                              
To: vendor-sec@lst.de                                                            
Subject: [vendor-sec] Cyrus IMAP Server - Multiple Vulnerabilities               
                                                                                 
[-- Attachment #1 [details] --]                                                            
[-- Type: text/plain, Encoding: 7bit, Size: 6.0K --]                             
                                                                                 
Hello Vendor Sec,                                                                
                                                                                 
attached is my mail to the Cyrus IMAP guys. They meanwhile replied and           
said that their plan is to release Cyrus IMAP 2.2.9 with the fixes               
already next week. They want to give me more details about their planned         
release soon.                                                                    
                                                                                 
(So the 24th is no longer the target date)                                       
                                                                                 
Stefan Esser                                                                     
                                                                                 
--- from here my original mail to cyrus imap guys ---                            
                                                                                 
                                                                                 
Cyrus IMAP Server - multiple remote vulnerabilities                              
---------------------------------------------------                              
                                                                                 
An Audit of the Cyrus IMAP Server from my side revealed 3 remotely               
exploitable vulnerabilities. While all 3 should be patched within                
2.2.8 the one in the middle is not exploitable after 2.2.6 because               
of another fix in the same area.                                                 
                                                                                 
Please handle this information carefully and contact me back as                  
soon as you have verified the problems. After you have verified their            
existance I would like to do a coordinated disclosure with vendor-sec            
of this bug in about 2 weeks. My prefered date is                                
                                                                                 
24th November 2004 : 15:00 CET                                                   
                                                                                 
Please tell me if there are any problems with the date, etc...                   
                                                                                 
Greetings,                                                                       
Stefan Esser                                                                     
                                                                                 
                                                                                 
and here come the bugs...                                                        
                                                                                 
                                                                                 
[01 - Cyrus IMAP Server - IMAPMAGICPLUS preauthentification overflow]            
                                                                                 
Affected Versions:                                                               
   2.2.4 - 2.2.8                                                                 
                                                                                 
Desc:                                                                            
   When the option imapmagicplus is activated LOGIN and some other               
   commands do not properly check the length of the supplied username            
   before copying it into a stack buffer. This results in an easy to             
   exploit stack overflow.                                                       
                                                                                 
Code:                                                                            
                                                                                 
   imap/imapd.c - line 290                                                       
   -----------------------                                                       
                                                                                 
   static int imapd_canon_user(sasl_conn_t *conn, void *context,                 
                   const char *user, unsigned ulen,                              
                   unsigned flags, const char *user_realm,                       
                   ....)                                                         
   {                                                                             
       char userbuf[MAX_MAILBOX_NAME+1], *p;                                     
       size_t n;                                                                 
       int r;                                                                    
                                                                                 
       if (!ulen) ulen = strlen(user);                                           
                                                                                 
       if (config_getswitch(IMAPOPT_IMAPMAGICPLUS)) {                            
       /* make a working copy of the auth[z]id */                                
                                                                                 
       memcpy(userbuf, user, ulen);        <---- copy without check              
                                                                                 
       userbuf[ulen] = '\0';                                                     
       user = userbuf;                                                           
                                                                                 
                                                                                 
   imap/imapd.c - line 348                                                       
   -----------------------                                                       
                                                                                 
   static int imapd_proxy_policy(sasl_conn_t *conn,                              
                 void *context,                                                  
                 const char *requested_user, unsigned rlen,                      
                 const char *auth_identity, unsigned alen,                       
                 const char *def_realm,                                          
                 unsigned urlen,                                                 
                 struct propctx *propctx)                                        
   {                                                                             
       if (config_getswitch(IMAPOPT_IMAPMAGICPLUS)) {                            
       char userbuf[MAX_MAILBOX_NAME+1], *p;                                     
       size_t n;                                                                 
                                                                                 
       /* make a working copy of the authzid */                                  
       if (!rlen) rlen = strlen(requested_user);                                 
                                                                                 
       memcpy(userbuf, requested_user, rlen);  <-- copy without check            
                                                                                 
    userbuf[rlen] = '\0';                                                        
                                                                                 
                                                                                 
                                                                                 
[02 - Cyrus IMAP Server - PARTIAL out of bounds memory corruption]               
                                                                                 
Affected Versions:                                                               
   <= 2.2.6                                                                      
   (because unexploitable in 2.2.7 + 2.2.8)                                      
                                                                                 
Desc:                                                                            
   In imapd versions prior to 2.2.7 the cmd_partial function did                 
   overwrite the closing '[' bracket of a body/body.peek entity.                 
   Due to a bug in the parser it is possible that this overwrite                 
   takes place outside of the allocated buffer. This is f.e. the                 
   case when the parser hits a "body[p" in this case the pointer                 
   is moved 10 forward which could lead to bypassing the end of 
   the allocated buffer and could therefore result in remote                     
   code execution through a one byte heap control structure                      
   memory corruption                                                             
                                                                                 
Code:                                                                            
                                                                                 
   imap/imapd.c - line 3157  (the wrong one bye write is fixed in 3172)          
   --------------------------------------------------------------------          
                                                                                 
   void cmd_partial(const char *tag, const char *msgno, char *data,              
            const char *start, const char *count)                                
   {                                                                             
       ...                                                                       
       else if (!strncmp(data, "body[", 5) ||                                    
            !strncmp(data, "body.peek[", 10)) {                                  
       p = section = data + 5;                                                   
       if (*p == 'p') {                                                          
           p = section += 5;  <--- here we could leave the buffer                
       }                                                                         
                                                                                 
                                                                                 
                                                                                 
[03 - Cyrus IMAP Server - FETCH out of bounds memory corruption]                 
                                                                                 
Affected Versions:                                                               
   <= 2.2.8                                                                      
                                                                                 
Desc:                                                                            
   Similiar to the PARTIAL out of bounds memory corruption the parser            
   of the FETCH command may leave the allocated buffer when it hits a            
   "BODY[P", "BINARY[S" or a "BINARY[P". The execution of the                    
   PARSE_PARTIAL macro after such an incident can lead to the same               
   one byte memory corruption on the heap that could lead to remote              
   code execution                                                                
                                                                                 
Code:                                                                            
                                                                                 
   imap/imapd.c - lines 2774, 2777, 2817                                         
   -------------------------------------                                         
                                                                                 
   void cmd_fetch(char *tag, char *sequence, int usinguid)                       
   {                                                                             
       char *cmd = usinguid ? "UID Fetch" : "Fetch";                             
       static struct buf fetchatt, fieldname;                                    
       ...                                                                       
      case 'B':                                                                  
           if (!strncmp(fetchatt.s, "BINARY[", 7) ||                             
           !strncmp(fetchatt.s, "BINARY.PEEK[", 12) ||                           
           !strncmp(fetchatt.s, "BINARY.SIZE[", 12)) {                           
           int binsize = 0;                                                      
                                                                                 
           p = section = fetchatt.s + 7;                                         
           if (*p == 'P') {                                                      
               p = section += 5;   <--- possible break here                      
           }                                                                     
           else if (*p == 'S') {                                                 
               p = section += 5;   <--- another break here                       
               binsize = 1;                                                      
           }                      
             ...                                                                          
            else if (!strncmp(fetchatt.s, "BODY[", 5) ||                         
                 !strncmp(fetchatt.s, "BODY.PEEK[", 10)) {                       
            p = section = fetchatt.s + 5;                                        
            if (*p == 'P') {                                                     
                p = section += 5;   <--- and again here                          
            }                                                                    
                                                                                 
--                                                                               
--------------------------------------------------------------------------       
 Stefan Esser                                        s.esser@e-matters.de        
 e-matters Security                         http://security.e-matters.de/        
                                                                                 
 GPG-Key                gpg --keyserver pgp.mit.edu --recv-key 0x15ABDA78        
 Key fingerprint       7806 58C8 CFA8 CE4A 1C2C  57DD 4AE1 795E 15AB DA78        
--------------------------------------------------------------------------       
 Did I help you? Consider a gift:            http://wishlist.suspekt.org/        
--------------------------------------------------------------------------
Comment 1 Marcus Meissner 2004-11-12 21:37:50 UTC
<!-- SBZ_reproduce  -->
see attachment
Comment 2 Marcus Meissner 2004-11-12 21:38:28 UTC
Here are some CVE names.  Please can you let the Cyrus folks know, with          
issues that are this serious it's worth trying to get upstream to use the        
CVE names too.                                                                   
                                                                                 
Because of the different affected versions this needs three names                
                                                                                 
> [01 - Cyrus IMAP Server - IMAPMAGICPLUS preauthentification overflow]          
                                                                                 
CAN-2004-1011                                                                    
                                                                                 
> [02 - Cyrus IMAP Server - PARTIAL out of bounds memory corruption]             
                                                                                 
CAN-2004-1012                                                                    
                                                                                 
> [03 - Cyrus IMAP Server - FETCH out of bounds memory corruption]               
                                                                                 
CAN-2004-1013                                                                    
Comment 3 Marcus Meissner 2004-11-12 21:39:01 UTC
CRD origiunally 24.11.2004, but most likely will be released next week by 
cyrus-imapd team. 
 
No patches yet, inquiring... 
Comment 4 Marcus Meissner 2004-11-12 21:44:43 UTC
Created attachment 26056 [details]
imapd-bugdemo.tgz
Comment 5 Carsten Hoeger 2004-11-15 17:51:05 UTC
regarding comment 2, what is CVE names?
Comment 6 Sebastian Krahmer 2004-11-15 18:53:13 UTC
Created attachment 26089 [details]
my patch proposal for 2.2.8

Please comment
Comment 7 Sebastian Krahmer 2004-11-15 20:47:13 UTC
Created attachment 26093 [details]
New patch, obsoleting old one

...
Comment 8 Carsten Hoeger 2004-11-15 23:08:50 UTC
from readme in imapd-bugdemo.tgz:

 - cmd_fetch.output   - this is the return value on a test server
                        clearly the OK line after the 2nd BAD line
                        is wrong. Without the bug OK line would be
                        like the 1st BAD line

where this is the content of cmd_fetch.output:

* OK XXXXX Cyrus IMAP4 v2.1.16-IPv6-Debian-2.1.16-10 server ready
0 OK Anonymous access granted
* FLAGS (\Answered \Flagged \Draft \Deleted \Seen)
* OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen \*)]  
* 0 EXISTS
* 0 RECENT
* OK [UIDVALIDITY 1099606002]  
* OK [UIDNEXT 1]  
0 OK [READ-WRITE] Completed
0 BAD Invalid body section
0 BAD Invalid Fetch attribute BODY.P11111111111111]
0 NO No matching messages
* BYE LOGOUT received
0 OK Completed


So what OK line after the 2nd BAD line might be talked about???
Or is that a typo and the "NO" line after the 2nd BAD line is meant?
Comment 9 Carsten Hoeger 2004-11-19 17:09:26 UTC
packages submittet
Comment 10 Sebastian Krahmer 2004-11-19 18:33:27 UTC
Created attachment 26228 [details]
the patch upstream is using

This exactly the patch we sent to the maintainmers, except they
handle oversized packets by a 'return' rather thsn truncating.
Could you please go ahead with the build and apply the patches to the other
versions too?
Comment 11 Sebastian Krahmer 2004-11-19 20:13:13 UTC
Created attachment 26239 [details]
patchinfo-file box

...
Comment 12 Sebastian Krahmer 2004-11-19 20:13:33 UTC
Created attachment 26240 [details]
patchinfo for maintained

...
Comment 13 Sebastian Krahmer 2004-11-22 18:12:14 UTC
Ok, so everything should be in place. Could you please tell 
suse-dist? I thought SWAMP would do this automatically but anyway.
Comment 14 Marcus Meissner 2004-11-23 20:04:18 UTC
is public now. 
Comment 15 Sebastian Krahmer 2004-11-23 22:30:32 UTC
Created attachment 26347 [details]
New patch fixing same bugs which shoed also up in proxyd.c

...
Comment 16 Sebastian Krahmer 2004-11-23 22:35:52 UTC
Could you please submit new sources? We got reports via vendor-sec
that the same bug was contained in proxyd.c. I attached new fix,
and also sent it to vendor-sec to see what they say. Patchinfos
are the same except you need to add a newline or a space somewhere
for the md5 sum.
Should I reject the patchinfo?
Comment 17 Carsten Hoeger 2004-11-25 18:29:01 UTC
Markus just told me about this additional patch (I didn't see it, as I was not
in CC)
Comment 18 Carsten Hoeger 2004-11-25 19:06:16 UTC
Okay, submitted 9.2 and SLES9 versions:

/work/src/done/9.2/cyrus-imapd
/work/src/done/SLES9/cyrus-imapd

the older versions do not have support for IMAPMAGICPLUS and/or do not have
imap/global.c

Please also not forget to add this documentation to the SLES9 version of the
cyrus-imapd update:

-----------------------------------------------------------------------------
Fixes for the sieve vacation functionality. Existing sieve scripts must be
recompiled when installing this update, e.g. using the tool masssievec. These
commands can be used to do that:

find /var/lib/sieve -name "*.bc" | xargs rm -v
su - cyrus -c '/usr/share/doc/packages/cyrus-imapd/tools/masssievec \
/usr/lib/cyrus/bin/sievec'

It is recommended to make a backup of /var/lib/sieve, first.
Comment 19 Sebastian Krahmer 2004-11-29 17:27:55 UTC
Ok, so everything should be in place, yet I dont see it on patch-status.
Could you please inform suse-dist? (I am submitting patchinfo files right now).
For the non-security docs, this is something the documenters need to know.
Comment 20 Sebastian Krahmer 2004-11-29 17:32:34 UTC
Patchinfos submitted. Ah, I see the SLES cyrus on patchstatus, but I think you
need to re-submit the 8.1, 8.2 and so on since it has been rejected because
of the missing fix for the 9.2.
Patchinfo is there, you can also use the same fix. Sorry for the mess.

Comment 21 Carsten Hoeger 2004-11-29 17:40:49 UTC
resubmit the package without changes to /work/src/done???
Comment 22 Marcus Meissner 2004-11-29 17:41:35 UTC
please leave everything as-is, all is fine , patchinfos are complete and in QA 
already... 
Comment 23 Thomas Biege 2004-12-02 03:58:04 UTC
packages approved. 
advisory will be released tomorrow. 
Comment 24 Thomas Biege 2009-10-13 19:59:22 UTC
CVE-2004-1013: CVSS v2 Base Score: 10.0 (AV:N/AC:L/Au:N/C:C/I:C/A:C)