[PATCH V5] netfilter: h323: avoid potential attack

From: Zhouyi Zhou
Date: Tue Feb 02 2016 - 00:40:53 EST


I think hackers chould build a malicious h323 packet to overflow
the pointer p which will panic during the memcpy(addr, p, len)
For example, he may fabricate a very large taddr->ipAddress.ip.

In order to avoid this, I add a valid memory reference check in
get_h2x5_addr functions.

As suggested by Eric, this module is protected by a lock (nf_h323_lock)
so adding a variable h323_buffer_valid_bytes that would contain
the number of valid bytes would not require to change prototypes of
get_h2x5_addr.

Signed-off-by: Zhouyi Zhou <yizhouzhou@xxxxxxxxx>

---
net/netfilter/nf_conntrack_h323_main.c | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)

diff --git a/net/netfilter/nf_conntrack_h323_main.c b/net/netfilter/nf_conntrack_h323_main.c
index 9511af0..21665ec 100644
--- a/net/netfilter/nf_conntrack_h323_main.c
+++ b/net/netfilter/nf_conntrack_h323_main.c
@@ -110,6 +110,25 @@ int (*nat_q931_hook) (struct sk_buff *skb,

static DEFINE_SPINLOCK(nf_h323_lock);
static char *h323_buffer;
+static int h323_buffer_valid_bytes;
+
+static bool h323_buffer_ref_valid(void *p, int len)
+{
+
+ if ((unsigned long)len > h323_buffer_valid_bytes) {
+ return false;
+ }
+
+ if (p + len > (void *)h323_buffer + h323_buffer_valid_bytes) {
+ return false;
+ }
+
+ if (p < (void *)h323_buffer) {
+ return false;
+ }
+
+ return true;
+}

static struct nf_conntrack_helper nf_conntrack_helper_h245;
static struct nf_conntrack_helper nf_conntrack_helper_q931[];
@@ -145,6 +164,7 @@ static int get_tpkt_data(struct sk_buff *skb, unsigned int protoff,

if (*data == NULL) { /* first TPKT */
/* Get first TPKT pointer */
+ h323_buffer_valid_bytes = tcpdatalen;
tpkt = skb_header_pointer(skb, tcpdataoff, tcpdatalen,
h323_buffer);
BUG_ON(tpkt == NULL);
@@ -247,6 +267,9 @@ static int get_h245_addr(struct nf_conn *ct, const unsigned char *data,
return 0;
}

+ if (!h323_buffer_ref_valid((void *)p, len + sizeof(__be16)))
+ return 0;
+
memcpy(addr, p, len);
memset((void *)addr + len, 0, sizeof(*addr) - len);
memcpy(port, p + len, sizeof(__be16));
@@ -669,6 +692,9 @@ int get_h225_addr(struct nf_conn *ct, unsigned char *data,
return 0;
}

+ if (!h323_buffer_ref_valid((void *)p, len + sizeof(__be16)))
+ return 0;
+
memcpy(addr, p, len);
memset((void *)addr + len, 0, sizeof(*addr) - len);
memcpy(port, p + len, sizeof(__be16));
@@ -1248,6 +1274,7 @@ static unsigned char *get_udp_data(struct sk_buff *skb, unsigned int protoff,
if (dataoff >= skb->len)
return NULL;
*datalen = skb->len - dataoff;
+ h323_buffer_valid_bytes = *datalen;
return skb_header_pointer(skb, dataoff, *datalen, h323_buffer);
}

--
1.9.1