Skip to contentSkip to conversation

socks.c: fix alen for DOMAIN type addresses

PATCH Superseded Gert Doering 2 participants 7 messages Sep 9, 2020 – Sep 14, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 socks.c: fix alen for DOMAIN type addresses

When a SOCKS5 server sends back a reply, it encodes an "address", which can be IPv4 (4 bytes), IPv6 (16 bytes) or "a domain name", which has a lenght (1 byte) and "a string of length <length>" - so when copying bytes, we need to hande "length +1" bytes.

Our code totally doesn't use this variant of addresses, but since this has been pointed out by "tpw_rules" in Trac, fix it, so if/when someone works on this again, the foundation is correct.

Reported-By: tpw_rules in Trac
Trac: #848

---
 src/openvpn/socks.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
src/openvpn/socks.c +4 -1
index 57f0cee2..aff62746 100644
@@ -381,7 +381,10 @@ recv_socks_reply(socket_descriptor_t sd,
                     break;
 
                 case '\x03':    /* DOMAINNAME */
-                    alen = (unsigned char) c;
+                    /* RFC 1928, section 5: 1 byte length, <n> bytes name,
+                     * so the total "address length" is (length+1)
+                     */
+                    alen = (unsigned char) c +1;
                     break;
 
                 case '\x04':    /* IP V6 */
Signature · 1 line
2.26.2
#1 lore raw

PATCH v2 1/1 socks.c: fix alen for DOMAIN type addresses

When a SOCKS5 server sends back a reply, it encodes an "address", which can be IPv4 (4 bytes), IPv6 (16 bytes) or "a domain name", which has a lenght (1 byte) and "a string of length <length>" - so when copying bytes, we need to hande "length +1" bytes.

Our code totally doesn't use this variant of addresses, but since this has been pointed out by "tpw_rules" in Trac, fix it, so if/when someone works on this again, the foundation is correct.

v2: increase buf[] len to be large enough to actually copy a domain name

Reported-By: tpw_rules in Trac
Trac: #848

---
 src/openvpn/socks.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
src/openvpn/socks.c +5 -2
index 57f0cee2..f1bdf4d4 100644
@@ -312,7 +312,7 @@ recv_socks_reply(socket_descriptor_t sd,
     char atyp = '\0';
     int alen = 0;
     int len = 0;
-    char buf[22];
+    char buf[270];		/* 4 + alen(max 256) + 2 */
     const int timeout_sec = 5;
 
     if (addr != NULL)
@@ -381,7 +381,10 @@ recv_socks_reply(socket_descriptor_t sd,
                     break;
 
                 case '\x03':    /* DOMAINNAME */
-                    alen = (unsigned char) c;
+                    /* RFC 1928, section 5: 1 byte length, <n> bytes name,
+                     * so the total "address length" is (length+1)
+                     */
+                    alen = (unsigned char) c +1;
                     break;
 
                 case '\x04':    /* IP V6 */
Signature · 1 line
2.26.2
#2 lore raw

PATCH v3 1/1 socks.c: fix alen for DOMAIN type addresses, bump up buffer sizes

When a SOCKS5 server sends back a reply, it encodes an "address", which can be IPv4 (4 bytes), IPv6 (16 bytes) or "a domain name", which has a lenght (1 byte) and "a string of length <length>" - so when copying bytes, we need to hande "length +1" bytes.

Our code totally doesn't use this variant of addresses on reception, but since this has been pointed out by "tpw_rules" in Trac, fix it, so if/when someone works on this again, the foundation is correct.

While at it, increase buffer size used for sending to handle domain names longer than 122 characters (length was already checked, so a longer name would not overflow but just "not work").

v2: increase buf[] len in recv_socks_reply() from 22 to 270 so it
is large enough to actually copy a domain name

v3: increase buf[] len in establish_socks_proxy_passthru() from 128 to
270, to handle long domain names in queries

Reported-By: tpw_rules in Trac
Trac: #848

---
 src/openvpn/socks.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
src/openvpn/socks.c +6 -3
index 57f0cee2..d43d84a8 100644
@@ -312,7 +312,7 @@ recv_socks_reply(socket_descriptor_t sd,
     char atyp = '\0';
     int alen = 0;
     int len = 0;
-    char buf[22];
+    char buf[270];		/* 4 + alen(max 256) + 2 */
     const int timeout_sec = 5;
 
     if (addr != NULL)
@@ -381,7 +381,10 @@ recv_socks_reply(socket_descriptor_t sd,
                     break;
 
                 case '\x03':    /* DOMAINNAME */
-                    alen = (unsigned char) c;
+                    /* RFC 1928, section 5: 1 byte length, <n> bytes name,
+                     * so the total "address length" is (length+1)
+                     */
+                    alen = (unsigned char) c +1;
                     break;
 
                 case '\x04':    /* IP V6 */
@@ -451,7 +454,7 @@ establish_socks_proxy_passthru(struct socks_proxy_info *p,
                                const char *servname,    /* openvpn server port */
                                volatile int *signal_received)
 {
-    char buf[128];
+    char buf[270];
     size_t len;
 
     if (!socks_handshake(p, sd, signal_received))
Signature · 1 line
2.26.2
#3 lore raw

Re: [Openvpn-devel] [PATCH v3] socks.c: fix alen for DOMAIN type addresses, bump up buffer sizes

Hi,

Quoted 14 lines · Gert Doering
src/openvpn/socks.c +1 -1
index 57f0cee2..d43d84a8 100644
@@ -312,7 +312,7 @@ recv_socks_reply(socket_descriptor_t sd,
     char atyp = '\0';
     int alen = 0;
     int len = 0;
-    char buf[22];
+    char buf[270];		/* 4 + alen(max 256) + 2 */
     const int timeout_sec = 5;
 
     if (addr != NULL)

src/openvpn/socks.c recv_socks_reply(socket_descriptor_t sd, · line 387

Quoted 8 lines
src/openvpn/socks.c +4 -1
@@ -381,7 +381,10 @@ recv_socks_reply(socket_descriptor_t sd,
                     break;
 
                 case '\x03':    /* DOMAINNAME */
-                    alen = (unsigned char) c;
+                    /* RFC 1928, section 5: 1 byte length, <n> bytes name,
+                     * so the total "address length" is (length+1)
+                     */
+                    alen = (unsigned char) c +1;

since you are touching this line...how about making it straight?
1) why casting to unsigned char? "alen" is int.
2) please add a space after the '+' operator.

Regards,

Signature · 1 line
Antonio Quartulli
#4 lore raw

Re: [Openvpn-devel] [PATCH v3] socks.c: fix alen for DOMAIN type addresses, bump up buffer sizes

In reply to Gert Doering

Hi,

Quoted 16 lines · Gert Doering

When a SOCKS5 server sends back a reply, it encodes an "address", which can be IPv4 (4 bytes), IPv6 (16 bytes) or "a domain name", which has a lenght (1 byte) and "a string of length <length>" - so when copying bytes, we need to hande "length +1" bytes.

Our code totally doesn't use this variant of addresses on reception, but since this has been pointed out by "tpw_rules" in Trac, fix it, so if/when someone works on this again, the foundation is correct.

While at it, increase buffer size used for sending to handle domain names longer than 122 characters (length was already checked, so a longer name would not overflow but just "not work").

v2: increase buf[] len in recv_socks_reply() from 22 to 270 so it
is large enough to actually copy a domain name

Quoted 7 lines

v3: increase buf[] len in establish_socks_proxy_passthru() from 128 to
270, to handle long domain names in queries

Reported-By: tpw_rules in Trac
Trac: #848

After a quick discussion on IRC I am fine with this patch, assuming the whitespace is added after the '+' operator.

Further refactoring of this code will be carried on in later patches.

Regards,

Signature · 1 line
Antonio Quartulli
#5 lore raw

Re: [Openvpn-devel] [PATCH v3] socks.c: fix alen for DOMAIN type addresses, bump up buffer sizes

Hi,

Quoted 25 lines · Antonio Quartulli

Hi,

Quoted 22 lines · Gert Doering

When a SOCKS5 server sends back a reply, it encodes an "address", which can be IPv4 (4 bytes), IPv6 (16 bytes) or "a domain name", which has a lenght (1 byte) and "a string of length <length>" - so when copying bytes, we need to hande "length +1" bytes.

Our code totally doesn't use this variant of addresses on reception, but since this has been pointed out by "tpw_rules" in Trac, fix it, so if/when someone works on this again, the foundation is correct.

While at it, increase buffer size used for sending to handle domain names longer than 122 characters (length was already checked, so a longer name would not overflow but just "not work").

v2: increase buf[] len in recv_socks_reply() from 22 to 270 so it
is large enough to actually copy a domain name

v3: increase buf[] len in establish_socks_proxy_passthru() from 128 to
270, to handle long domain names in queries

Reported-By: tpw_rules in Trac
Trac: #848

Quoted 6 lines
Quoted 1 line

After a quick discussion on IRC I am fine with this patch, assuming the whitespace is added after the '+' operator.

Further refactoring of this code will be carried on in later patches.

forgot to add:

Signature · 1 line
Antonio Quartulli
#6 lore raw

[Openvpn-devel] [PATCH applied] Re: socks.c: fix alen for DOMAIN type addresses, bump up buffer sizes

In reply to Gert Doering

Thanks for the review.

Patch has been applied to many branches (bugfix)...

commit eebeaa02367d247fc2549df3edf8e598c58c3572 (master) commit c7f0d7b95bff05b0a5ddab15318cd53fcc91d60a (release/2.5) commit 64a76533b676ad441ca20bab0c8b2e387bd56ebe (release/2.4)

Author: Gert Doering
Date:   Wed Sep 9 14:22:23 2020 +0200
     socks.c: fix alen for DOMAIN type addresses, bump up buffer sizes

     Signed-off-by: Gert Doering <[email protected]...>
     Acked-by: Antonio Quartulli <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg20928.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering