Skip to contentSkip to conversation

get_addr_generic: fix server crash in freeaddrinfo on musl libc

PATCH Superseded Petr Štetiar 4 participants 10 messages Feb 9, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 get_addr_generic: fix server crash in freeaddrinfo on musl libc

Server can crash on systems using musl libc when client with comma in commonName tries to connect:

ifconfig_pool_read(), in='VPN Client, abc,192.168.1.2,'
RESOLVE: Cannot parse IP address: abc: (Name does not resolve)

as this leads to NULL pointer dereference in freeaddrinfo():

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7fbf81a in freeaddrinfo (p=0x0) at src/network/freeaddrinfo.c:10
(gdb) bt
#0 0x00007ffff7fbf81a in freeaddrinfo (p=0x0) at src/network/freeaddrinfo.c:10
#1 0x00000000004389ec in get_addr_generic ([email protected]...=2, [email protected]...=4, [email protected]...=0x7ffff7ee2988 " abc", [email protected]...=0x7fffffffcb7c, [email protected]...=0x0,

     [email protected]...=0, signal_received=0x0, msglevel=64) at openvpn-2.5.7/src/openvpn/socket.c:186
 #2  0x0000000000438a2d in getaddr ([email protected]...=4, [email protected]...=0x7ffff7ee2988 " abc", [email protected]...=0, [email protected]...=0x7fffffffcba7,
     [email protected]...=0x0) at openvpn-2.5.7/src/openvpn/socket.c:202

#3 0x0000000000430ae5 in ifconfig_pool_read (persist=0x7ffff7ee4510, pool=0x7ffff7edd450) at openvpn-2.5.7/src/openvpn/pool.c:661

So fix it by checking if struct addrinfo* pointer is valid before passing it down to freeaddrinfo().

References: https://github.com/openwrt/openwrt/issues/11890

---
 src/openvpn/socket.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
src/openvpn/socket.c +4 -1
index a883ac4a156c..d304554cefda 100644
@@ -172,7 +172,10 @@ get_addr_generic(sa_family_t af, unsigned int flags, const char *hostname,
         *sep = '/';
     }
 out:
-    freeaddrinfo(ai);
+    if (ai)
+    {
+        freeaddrinfo(ai);
+    }
     free(var_host);
 
     return ret;
#1 lore raw

Am 09.02.23 um 16:36 schrieb Petr Štetiar:

Quoted 13 lines

Server can crash on systems using musl libc when client with comma in commonName tries to connect:

ifconfig_pool_read(), in='VPN Client, abc,192.168.1.2,'
RESOLVE: Cannot parse IP address: abc: (Name does not resolve)

as this leads to NULL pointer dereference in freeaddrinfo():

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7fbf81a in freeaddrinfo (p=0x0) at src/network/freeaddrinfo.c:10
(gdb) bt
#0 0x00007ffff7fbf81a in freeaddrinfo (p=0x0) at src/network/freeaddrinfo.c:10
#1 0x00000000004389ec in get_addr_generic ([email protected]...=2, [email protected]...=4, [email protected]...=0x7ffff7ee2988 " abc", [email protected]...=0x7fffffffcb7c, [email protected]...=0x0,

Quoted 7 lines
      [email protected]...=0, signal_received=0x0, msglevel=64) at openvpn-2.5.7/src/openvpn/socket.c:186
  #2  0x0000000000438a2d in getaddr ([email protected]...=4, [email protected]...=0x7ffff7ee2988 " abc", [email protected]...=0, [email protected]...=0x7fffffffcba7,
      [email protected]...=0x0) at openvpn-2.5.7/src/openvpn/socket.c:202

#3 0x0000000000430ae5 in ifconfig_pool_read (persist=0x7ffff7ee4510, pool=0x7ffff7edd450) at openvpn-2.5.7/src/openvpn/pool.c:661

So fix it by checking if struct addrinfo* pointer is valid before passing it down to freeaddrinfo().

I am bit surprised that freeadrinfo does not follow the usual semantics of calling freesomething on a nullptr is a noop.

If this is really the case would should add similar code (with comments that freeaddrinfo is special here) also in gc_freeaddrinfo_callback and the other places that call it without checking for ai to be valid.

Arne

#2 lore raw

Am 09.02.23 um 16:53 schrieb Arne Schwabe:

Quoted 19 lines

Am 09.02.23 um 16:36 schrieb Petr Štetiar:

Quoted 18 lines

Server can crash on systems using musl libc when client with comma in commonName tries to connect:

  ifconfig_pool_read(), in='VPN Client, abc,192.168.1.2,'
  RESOLVE: Cannot parse IP address:  abc: (Name does not resolve)

as this leads to NULL pointer dereference in freeaddrinfo():

  Program received signal SIGSEGV, Segmentation fault.
  0x00007ffff7fbf81a in freeaddrinfo (p=0x0) at src/network/freeaddrinfo.c:10
  (gdb) bt
  #0  0x00007ffff7fbf81a in freeaddrinfo (p=0x0) at src/network/freeaddrinfo.c:10
  #1  0x00000000004389ec in get_addr_generic ([email protected]...=2, [email protected]...=4, [email protected]...=0x7ffff7ee2988 " abc", [email protected]...=0x7fffffffcb7c, [email protected]...=0x0, [email protected]...=0, signal_received=0x0, msglevel=64) at openvpn-2.5.7/src/openvpn/socket.c:186
  #2  0x0000000000438a2d in getaddr ([email protected]...=4, [email protected]...=0x7ffff7ee2988 " abc", [email protected]...=0, [email protected]...=0x7fffffffcba7, [email protected]...=0x0) at openvpn-2.5.7/src/openvpn/socket.c:202
  #3  0x0000000000430ae5 in ifconfig_pool_read (persist=0x7ffff7ee4510, pool=0x7ffff7edd450) at openvpn-2.5.7/src/openvpn/pool.c:661

Quoted 7 lines
Quoted 2 lines

So fix it by checking if struct addrinfo* pointer is valid before passing it down to freeaddrinfo().

I am bit surprised that freeadrinfo does not follow the usual semantics of calling freesomething on a nullptr is a noop.

If this is really the case would should add similar code (with comments that freeaddrinfo is special here) also in gc_freeaddrinfo_callback and the other places that call it without checking for ai to be valid.

So FreeBSD has:

* - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it
is
* invalid. Current code accepts NULL to be compatible with other
OSes.

and android bionic has a similar comment (bionic is also BSD libc based):

* - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is
* invalid.
* current code - SEGV on freeaddrinfo(NULL)

but then actually does nothing on ai == NULL:

#if defined(__BIONIC__)
	if (ai == NULL) return;
#else
	_DIAGASSERT(ai != NULL);

#endif

so it seems that apart from some implementations like musl this assumption that freeaddrinfo is a noop is kind of true. I would still open a report against musl to fix this as this seem to be better to be aligned with the rest of the world.

Arne

#3 lore raw

Arne Schwabe <[email protected]...> [2023-02-09 17:03:31]:

Quoted 60 lines

Am 09.02.23 um 16:53 schrieb Arne Schwabe:

Quoted 34 lines

Am 09.02.23 um 16:36 schrieb Petr Štetiar:

Quoted 30 lines

Server can crash on systems using musl libc when client with comma in commonName tries to connect:

  ifconfig_pool_read(), in='VPN Client, abc,192.168.1.2,'
  RESOLVE: Cannot parse IP address:  abc: (Name does not resolve)

as this leads to NULL pointer dereference in freeaddrinfo():

  Program received signal SIGSEGV, Segmentation fault. 0x00007ffff7fbf81a in freeaddrinfo (p=0x0) at src/network/freeaddrinfo.c:10
  (gdb) bt
  #0  0x00007ffff7fbf81a in freeaddrinfo (p=0x0) at src/network/freeaddrinfo.c:10
  #1  0x00000000004389ec in get_addr_generic ([email protected]...=2, [email protected]...=4, [email protected]...=0x7ffff7ee2988 " abc", [email protected]...=0x7fffffffcb7c, [email protected]...=0x0, [email protected]...=0, signal_received=0x0, msglevel=64) at openvpn-2.5.7/src/openvpn/socket.c:186
  #2  0x0000000000438a2d in getaddr ([email protected]...=4, [email protected]...=0x7ffff7ee2988 " abc", [email protected]...=0, [email protected]...=0x7fffffffcba7, [email protected]...=0x0) at openvpn-2.5.7/src/openvpn/socket.c:202
  #3  0x0000000000430ae5 in ifconfig_pool_read (persist=0x7ffff7ee4510, pool=0x7ffff7edd450) at openvpn-2.5.7/src/openvpn/pool.c:661

Quoted 2 lines

So fix it by checking if struct addrinfo* pointer is valid before passing it down to freeaddrinfo().

Quoted 7 lines

I am bit surprised that freeadrinfo does not follow the usual semantics of calling freesomething on a nullptr is a noop.

If this is really the case would should add similar code (with comments that freeaddrinfo is special here) also in gc_freeaddrinfo_callback and the other places that call it without checking for ai to be valid.

So FreeBSD has:

* - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is
* invalid. Current code accepts NULL to be compatible with other OSes.

and android bionic has a similar comment (bionic is also BSD libc based):

* - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is
* invalid.
* current code - SEGV on freeaddrinfo(NULL)

but then actually does nothing on ai == NULL:

#if defined(__BIONIC__)
	if (ai == NULL) return;
#else
	_DIAGASSERT(ai != NULL);
Quoted 6 lines

#endif

so it seems that apart from some implementations like musl this assumption that freeaddrinfo is a noop is kind of true. I would still open a report against musl to fix this as this seem to be better to be aligned with the rest of the world.

https://www.openwall.com/lists/musl/2019/03/26/18

#4 lore raw
In reply to Petr Štetiar

Hi,

Quoted 5 lines · Petr ??tetiar
src/openvpn/socket.c +0 -0
index a883ac4a156c..d304554cefda 100644

src/openvpn/socket.c get_addr_generic(sa_family_t af, unsigned int flags, const char *hostname, · line 179

Quoted 9 lines
src/openvpn/socket.c +4 -1
@@ -172,7 +172,10 @@ get_addr_generic(sa_family_t af, unsigned int flags, const char *hostname,
         *sep = '/';
     }
 out:
-    freeaddrinfo(ai);
+    if (ai)
+    {
+        freeaddrinfo(ai);
+    }
     free(var_host);

The patch itself is fine, but as Arne says, maybe we should then add this to all places where freeaddrinfo() is called and could be NULL - so we're at least consistent in our interpretation of "should we pass NULL?".

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#5 lore raw

PATCH v2 1/1 Fix server crash in freeaddrinfo on musl libc

In reply to Petr Štetiar

Server can crash on systems using musl libc when client with comma in commonName tries to connect:

ifconfig_pool_read(), in='VPN Client, abc,192.168.1.2,'
RESOLVE: Cannot parse IP address: abc: (Name does not resolve)

as this leads to NULL pointer dereference in freeaddrinfo():

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7fbf81a in freeaddrinfo (p=0x0) at src/network/freeaddrinfo.c:10
(gdb) bt
#0 0x00007ffff7fbf81a in freeaddrinfo (p=0x0) at src/network/freeaddrinfo.c:10
#1 0x00000000004389ec in get_addr_generic ([email protected]...=2, [email protected]...=4, [email protected]...=0x7ffff7ee2988 " abc", [email protected]...=0x7fffffffcb7c, [email protected]...=0x0,

     [email protected]...=0, signal_received=0x0, msglevel=64) at openvpn-2.5.7/src/openvpn/socket.c:186
 #2  0x0000000000438a2d in getaddr ([email protected]...=4, [email protected]...=0x7ffff7ee2988 " abc", [email protected]...=0, [email protected]...=0x7fffffffcba7,
     [email protected]...=0x0) at openvpn-2.5.7/src/openvpn/socket.c:202

#3 0x0000000000430ae5 in ifconfig_pool_read (persist=0x7ffff7ee4510, pool=0x7ffff7edd450) at openvpn-2.5.7/src/openvpn/pool.c:661

So fix it by checking if struct addrinfo* pointer is valid before passing it down to freeaddrinfo() and while at it add there a warning comment as well.

References: https://github.com/openwrt/openwrt/issues/11890

---
 src/openvpn/buffer.h | 6 +++++-
 src/openvpn/dns.c    | 7 ++++++-
 src/openvpn/ps.c     | 7 ++++++-
 src/openvpn/socket.c | 8 +++++++-
 4 files changed, 24 insertions(+), 4 deletions(-)

Changes in v2:

* handle freeaddrinfo in all places and add warning comment (Arne)

src/openvpn/buffer.h +5 -1
index 2461a20703aa..0f10bf00399e 100644
@@ -214,7 +214,11 @@ bool buf_init_debug(struct buffer *buf, int offset, const char *file, int line);
 static inline void
 gc_freeaddrinfo_callback(void *addr)
 {
-    freeaddrinfo((struct addrinfo *) addr);
+    /* WARNING: musl libc needs valid pointer */
+    if (addr)
+    {
+        freeaddrinfo((struct addrinfo *) addr);
+    }
 }
 
 /** Return an empty struct buffer */
src/openvpn/dns.c +6 -1
index 9f2a7d5ecaf8..05b87e983272 100644
@@ -130,7 +130,12 @@ dns_server_addr_parse(struct dns_server *server, const char *addr)
         server->port6 = port;
     }
 
-    freeaddrinfo(ai);
+    /* WARNING: musl libc needs valid pointer */
+    if (ai)
+    {
+        freeaddrinfo(ai);
+    }
+
     return true;
 }
 
src/openvpn/ps.c +6 -1
index 3609630af779..59f535bf2494 100644
@@ -841,7 +841,12 @@ port_share_open(const char *host,
                                  host, port,  0, NULL, AF_INET, &ai);
     ASSERT(status==0);
     hostaddr = *((struct sockaddr_in *) ai->ai_addr);
-    freeaddrinfo(ai);
+
+    /* WARNING: musl libc needs valid pointer */
+    if (ai)
+    {
+        freeaddrinfo(ai);
+    }
 
     /*
      * Make a socket for foreground and background processes
src/openvpn/socket.c +7 -1
index a883ac4a156c..9f6442698ed1 100644
@@ -172,7 +172,11 @@ get_addr_generic(sa_family_t af, unsigned int flags, const char *hostname,
         *sep = '/';
     }
 out:
-    freeaddrinfo(ai);
+    /* WARNING: musl libc needs valid pointer */
+    if (ai)
+    {
+        freeaddrinfo(ai);
+    }
     free(var_host);
 
     return ret;
@@ -1347,10 +1351,12 @@ socket_listen_accept(socket_descriptor_t sd,
                 {
                     msg(M_ERR, "TCP: close socket failed (new_sd)");
                 }
+                /* WARNING: musl libc needs valid pointer */
                 freeaddrinfo(ai);
             }
             else
             {
+                /* WARNING: musl libc needs valid pointer */
                 if (ai)
                 {
                     freeaddrinfo(ai);
#6 lore raw
In reply to Petr Štetiar
Quoted 5 lines · Petr Štetiar

Server can crash on systems using musl libc when client with comma in commonName tries to connect:

ifconfig_pool_read(), in='VPN Client, abc,192.168.1.2,'
RESOLVE: Cannot parse IP address: abc: (Name does not resolve)

I also would like to empathize what exactly leads to the crash. It clearly tried to parse a part of client certificate's Common Name as an IP address. As I understand it, this IP address was supposed to be used to restore client's previous IP address. This is another bug, different from trying to "free" NULL pointer.

Mykhailo

#7 lore raw

Hi,

Quoted 4 lines · Mykhailo Mishchenko
Quoted 3 lines · Petr ??tetiar

Server can crash on systems using musl libc when client with comma in commonName tries to connect:

Quoted 8 lines
Quoted 2 lines

ifconfig_pool_read(), in='VPN Client, abc,192.168.1.2,'
RESOLVE: Cannot parse IP address: abc: (Name does not resolve)

I also would like to empathize what exactly leads to the crash. It clearly tried to parse a part of client certificate's Common Name as an IP address. As I understand it, this IP address was supposed to be used to restore client's previous IP address. This is another bug, different from trying to "free" NULL pointer.

CNs with commas in them are a long-standing issue, because OpenVPN doesn't know how to deal with them. So, don't do that.

(There is an old Trac ticket about it, but nobody went out and fixed the code yet - which is tricky, as you can't just change the format of ifconfig-pool-persist without breaking existing setups)

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#8 lore raw
Quoted 2 lines · Gert Doering

CNs with commas in them are a long-standing issue, because OpenVPN doesn't know how to deal with them. So, don't do that.

Ugh... Sometimes it is just not realistic to reissue certificates. :( It would be great to at least have it mentioned in BUGS section of the man page.

Quoted 3 lines · Gert Doering

(There is an old Trac ticket about it, but nobody went out and fixed the code yet - which is tricky, as you can't just change the format of ifconfig-pool-persist without breaking existing setups)

I would escape the comma with backslash, for example. It would only break those setups, which are already broken because they have a comma in a CN of a client certificate. But maybe there are reasons why this is also problematic, and I just don't know about them.

Mykhailo

#9 lore raw

Hi,

Quoted 9 lines · Mykhailo Mishchenko
Quoted 3 lines · Gert Doering

(There is an old Trac ticket about it, but nobody went out and fixed the code yet - which is tricky, as you can't just change the format of ifconfig-pool-persist without breaking existing setups)

I would escape the comma with backslash, for example. It would only break those setups, which are already broken because they have a comma in a CN of a client certificate. But maybe there are reasons why this is also problematic, and I just don't know about them.

status files, management console output & associated consumers, lots of things inside OpenVPN are separated by commas.

Yes, this can all be fixed, but for all the bits that are "for other consumers" (status files, management) those entities would know how to deal with escaped commas.

ifconfig-pool-persist might actually be a fairly easy one to solve, by going backwards from the end of the line - so not taking "field 2 + 3" but "last, and second-last", so it doesn't matter if there are commas in the first section. OTOH we'd need to deal with old-style files that have no IPv6...

Mmmh.

"Someone needs to code it, and test all variants"

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...