From nobody Wed May 15 03:44:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 148773589885095.53329949051772; Tue, 21 Feb 2017 19:58:18 -0800 (PST) Received: from localhost ([::1]:49711 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cgO4H-0002Jk-LD for importer@patchew.org; Tue, 21 Feb 2017 22:58:17 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43459) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cgNta-0008Of-PW for qemu-devel@nongnu.org; Tue, 21 Feb 2017 22:47:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cgNta-0004nE-0w for qemu-devel@nongnu.org; Tue, 21 Feb 2017 22:47:14 -0500 Received: from [45.249.212.187] (port=2980 helo=dggrg01-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1cgNtZ-0004lv-Lf for qemu-devel@nongnu.org; Tue, 21 Feb 2017 22:47:13 -0500 Received: from 172.30.72.57 (EHLO DGGEMM406-HUB.china.huawei.com) ([172.30.72.57]) by dggrg01-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id AJN09257; Wed, 22 Feb 2017 11:47:10 +0800 (CST) Received: from DGGEML402-HUB.china.huawei.com (10.3.17.38) by DGGEMM406-HUB.china.huawei.com (10.3.20.214) with Microsoft SMTP Server (TLS) id 14.3.301.0; Wed, 22 Feb 2017 11:47:08 +0800 Received: from localhost (10.177.24.212) by DGGEML402-HUB.china.huawei.com (10.3.17.38) with Microsoft SMTP Server id 14.3.301.0; Wed, 22 Feb 2017 11:46:59 +0800 From: zhanghailiang To: , , Date: Wed, 22 Feb 2017 11:46:36 +0800 Message-ID: <1487735198-127300-2-git-send-email-zhang.zhanghailiang@huawei.com> X-Mailer: git-send-email 2.7.2.windows.1 In-Reply-To: <1487735198-127300-1-git-send-email-zhang.zhanghailiang@huawei.com> References: <1487735198-127300-1-git-send-email-zhang.zhanghailiang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.24.212] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020202.58AD09BE.0150, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 6d363e9520d111d60d0b072490abe8c6 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.187 Subject: [Qemu-devel] [PATCH v2 1/3] net/colo: fix memory double free error X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: zhanghailiang , xuquan8@huawei.com, qemu-devel@nongnu.org, pss.wulizhen@huawei.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The 'primary_list' and 'secondary_list' members of struct Connection is not allocated through dynamically g_queue_new(), but we free it by using g_queue_free(), which will lead to a double-free bug. Signed-off-by: zhanghailiang Reviewed-by: Zhang Chen --- net/colo.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/net/colo.c b/net/colo.c index 6a6eacd..8cc166b 100644 --- a/net/colo.c +++ b/net/colo.c @@ -147,9 +147,9 @@ void connection_destroy(void *opaque) Connection *conn =3D opaque; =20 g_queue_foreach(&conn->primary_list, packet_destroy, NULL); - g_queue_free(&conn->primary_list); + g_queue_clear(&conn->primary_list); g_queue_foreach(&conn->secondary_list, packet_destroy, NULL); - g_queue_free(&conn->secondary_list); + g_queue_clear(&conn->secondary_list); g_slice_free(Connection, conn); } =20 --=20 1.8.3.1 From nobody Wed May 15 03:44:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1487736331361430.78111308045425; Tue, 21 Feb 2017 20:05:31 -0800 (PST) Received: from localhost ([::1]:49761 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cgOBF-0000TO-V7 for importer@patchew.org; Tue, 21 Feb 2017 23:05:29 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43475) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cgNtb-0008Pe-Oa for qemu-devel@nongnu.org; Tue, 21 Feb 2017 22:47:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cgNta-0004nR-MF for qemu-devel@nongnu.org; Tue, 21 Feb 2017 22:47:15 -0500 Received: from [45.249.212.187] (port=2981 helo=dggrg01-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1cgNta-0004lx-12 for qemu-devel@nongnu.org; Tue, 21 Feb 2017 22:47:14 -0500 Received: from 172.30.72.57 (EHLO DGGEMM406-HUB.china.huawei.com) ([172.30.72.57]) by dggrg01-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id AJN09258; Wed, 22 Feb 2017 11:47:10 +0800 (CST) Received: from DGGEML402-HUB.china.huawei.com (10.3.17.38) by DGGEMM406-HUB.china.huawei.com (10.3.20.214) with Microsoft SMTP Server (TLS) id 14.3.301.0; Wed, 22 Feb 2017 11:47:09 +0800 Received: from localhost (10.177.24.212) by DGGEML402-HUB.china.huawei.com (10.3.17.38) with Microsoft SMTP Server id 14.3.301.0; Wed, 22 Feb 2017 11:47:00 +0800 From: zhanghailiang To: , , Date: Wed, 22 Feb 2017 11:46:37 +0800 Message-ID: <1487735198-127300-3-git-send-email-zhang.zhanghailiang@huawei.com> X-Mailer: git-send-email 2.7.2.windows.1 In-Reply-To: <1487735198-127300-1-git-send-email-zhang.zhanghailiang@huawei.com> References: <1487735198-127300-1-git-send-email-zhang.zhanghailiang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.24.212] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020202.58AD09BE.01F7, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: a7601548b5b5da5aa233dfae925c3049 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.187 Subject: [Qemu-devel] [PATCH v2 2/3] filter-rewriter: fix memory leak for connection in connection_track_table X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: zhanghailiang , xuquan8@huawei.com, qemu-devel@nongnu.org, pss.wulizhen@huawei.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" After a net connection is closed, we didn't clear its releated resources in connection_track_table, which will lead to memory leak. Let't track the state of net connection, if it is closed, its related resources will be cleared up. Signed-off-by: zhanghailiang --- net/colo.h | 4 +++ net/filter-rewriter.c | 70 +++++++++++++++++++++++++++++++++++++++++++++--= ---- 2 files changed, 67 insertions(+), 7 deletions(-) diff --git a/net/colo.h b/net/colo.h index 7c524f3..cd9027f 100644 --- a/net/colo.h +++ b/net/colo.h @@ -18,6 +18,7 @@ #include "slirp/slirp.h" #include "qemu/jhash.h" #include "qemu/timer.h" +#include "slirp/tcp.h" =20 #define HASHTABLE_MAX_SIZE 16384 =20 @@ -69,6 +70,9 @@ typedef struct Connection { * run once in independent tcp connection */ int syn_flag; + + int tcp_state; /* TCP FSM state */ + tcp_seq fin_ack_seq; /* the seq of 'fin=3D1,ack=3D1' */ } Connection; =20 uint32_t connection_key_hash(const void *opaque); diff --git a/net/filter-rewriter.c b/net/filter-rewriter.c index c4ab91c..7e7ec35 100644 --- a/net/filter-rewriter.c +++ b/net/filter-rewriter.c @@ -60,9 +60,9 @@ static int is_tcp_packet(Packet *pkt) } =20 /* handle tcp packet from primary guest */ -static int handle_primary_tcp_pkt(NetFilterState *nf, +static int handle_primary_tcp_pkt(RewriterState *rf, Connection *conn, - Packet *pkt) + Packet *pkt, ConnectionKey *key) { struct tcphdr *tcp_pkt; =20 @@ -97,15 +97,45 @@ static int handle_primary_tcp_pkt(NetFilterState *nf, tcp_pkt->th_ack =3D htonl(ntohl(tcp_pkt->th_ack) + conn->offset); =20 net_checksum_calculate((uint8_t *)pkt->data, pkt->size); + /* + * Case 1: + * The *server* side of this connect is VM, *client* tries to close + * the connection. + * + * We got 'ack=3D1' packets from client side, it acks 'fin=3D1, ac= k=3D1' + * packet from server side. From this point, we can ensure that th= ere + * will be no packets in the connection, except that, some errors + * happen between the path of 'filter object' and vNIC, if this ra= re + * case really happen, we can still create a new connection, + * So it is safe to remove the connection from connection_track_ta= ble. + * + */ + if ((conn->tcp_state =3D=3D TCPS_LAST_ACK) && + (ntohl(tcp_pkt->th_ack) =3D=3D (conn->fin_ack_seq + 1))) { + fprintf(stderr, "Remove conn " + g_hash_table_remove(rf->connection_track_table, key); + } + } + /* + * Case 2: + * The *server* side of this connect is VM, *server* tries to close + * the connection. + * + * We got 'fin=3D1, ack=3D1' packet from client side, we need to + * record the seq of 'fin=3D1, ack=3D1' packet. + */ + if ((tcp_pkt->th_flags & (TH_ACK | TH_FIN)) =3D=3D (TH_ACK | TH_FIN)) { + conn->fin_ack_seq =3D htonl(tcp_pkt->th_seq); + conn->tcp_state =3D TCPS_LAST_ACK; } =20 return 0; } =20 /* handle tcp packet from secondary guest */ -static int handle_secondary_tcp_pkt(NetFilterState *nf, +static int handle_secondary_tcp_pkt(RewriterState *rf, Connection *conn, - Packet *pkt) + Packet *pkt, ConnectionKey *key) { struct tcphdr *tcp_pkt; =20 @@ -133,8 +163,34 @@ static int handle_secondary_tcp_pkt(NetFilterState *nf, tcp_pkt->th_seq =3D htonl(ntohl(tcp_pkt->th_seq) - conn->offset); =20 net_checksum_calculate((uint8_t *)pkt->data, pkt->size); + /* + * Case 2: + * The *server* side of this connect is VM, *server* tries to close + * the connection. + * + * We got 'ack=3D1' packets from server side, it acks 'fin=3D1, ac= k=3D1' + * packet from client side. Like Case 1, there should be no packets + * in the connection from now know, But the difference here is + * if the packet is lost, We will get the resent 'fin=3D1,ack=3D1'= packet. + * TODO: Fix above case. + */ + if ((conn->tcp_state =3D=3D TCPS_LAST_ACK) && + (ntohl(tcp_pkt->th_ack) =3D=3D (conn->fin_ack_seq + 1))) { + g_hash_table_remove(rf->connection_track_table, key); + } + } + /* + * Case 1: + * The *server* side of this connect is VM, *client* tries to close + * the connection. + * + * We got 'fin=3D1, ack=3D1' packet from server side, we need to + * record the seq of 'fin=3D1, ack=3D1' packet. + */ + if ((tcp_pkt->th_flags & (TH_ACK | TH_FIN)) =3D=3D (TH_ACK | TH_FIN)) { + conn->fin_ack_seq =3D ntohl(tcp_pkt->th_seq); + conn->tcp_state =3D TCPS_LAST_ACK; } - return 0; } =20 @@ -178,7 +234,7 @@ static ssize_t colo_rewriter_receive_iov(NetFilterState= *nf, =20 if (sender =3D=3D nf->netdev) { /* NET_FILTER_DIRECTION_TX */ - if (!handle_primary_tcp_pkt(nf, conn, pkt)) { + if (!handle_primary_tcp_pkt(s, conn, pkt, &key)) { qemu_net_queue_send(s->incoming_queue, sender, 0, (const uint8_t *)pkt->data, pkt->size, NULL); packet_destroy(pkt, NULL); @@ -191,7 +247,7 @@ static ssize_t colo_rewriter_receive_iov(NetFilterState= *nf, } } else { /* NET_FILTER_DIRECTION_RX */ - if (!handle_secondary_tcp_pkt(nf, conn, pkt)) { + if (!handle_secondary_tcp_pkt(s, conn, pkt, &key)) { qemu_net_queue_send(s->incoming_queue, sender, 0, (const uint8_t *)pkt->data, pkt->size, NULL); packet_destroy(pkt, NULL); --=20 1.8.3.1 From nobody Wed May 15 03:44:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1487736147206258.59044224529794; Tue, 21 Feb 2017 20:02:27 -0800 (PST) Received: from localhost ([::1]:49736 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cgO8H-0005gn-Kd for importer@patchew.org; Tue, 21 Feb 2017 23:02:25 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43484) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cgNtc-0008QL-HY for qemu-devel@nongnu.org; Tue, 21 Feb 2017 22:47:17 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cgNtb-0004np-FV for qemu-devel@nongnu.org; Tue, 21 Feb 2017 22:47:16 -0500 Received: from [45.249.212.187] (port=2982 helo=dggrg01-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1cgNta-0004m3-Pj for qemu-devel@nongnu.org; Tue, 21 Feb 2017 22:47:15 -0500 Received: from 172.30.72.57 (EHLO DGGEMM406-HUB.china.huawei.com) ([172.30.72.57]) by dggrg01-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id AJN09259; Wed, 22 Feb 2017 11:47:10 +0800 (CST) Received: from DGGEML402-HUB.china.huawei.com (10.3.17.38) by DGGEMM406-HUB.china.huawei.com (10.3.20.214) with Microsoft SMTP Server (TLS) id 14.3.301.0; Wed, 22 Feb 2017 11:47:09 +0800 Received: from localhost (10.177.24.212) by DGGEML402-HUB.china.huawei.com (10.3.17.38) with Microsoft SMTP Server id 14.3.301.0; Wed, 22 Feb 2017 11:47:01 +0800 From: zhanghailiang To: , , Date: Wed, 22 Feb 2017 11:46:38 +0800 Message-ID: <1487735198-127300-4-git-send-email-zhang.zhanghailiang@huawei.com> X-Mailer: git-send-email 2.7.2.windows.1 In-Reply-To: <1487735198-127300-1-git-send-email-zhang.zhanghailiang@huawei.com> References: <1487735198-127300-1-git-send-email-zhang.zhanghailiang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.24.212] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020206.58AD09BF.003E, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 27af6ae1e5fffbf3998b5251f97d03d1 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.187 Subject: [Qemu-devel] [PATCH v2 3/3] filter-rewriter: skip net_checksum_calculate() while offset = 0 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: zhanghailiang , xuquan8@huawei.com, qemu-devel@nongnu.org, pss.wulizhen@huawei.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" While the offset of packets's sequence for primary side and secondary side is zero, it is unnecessary to call net_checksum_calculate() to recalculate the checksume value of packets. Signed-off-by: zhanghailiang --- net/filter-rewriter.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/net/filter-rewriter.c b/net/filter-rewriter.c index 7e7ec35..c9a6d43 100644 --- a/net/filter-rewriter.c +++ b/net/filter-rewriter.c @@ -93,10 +93,12 @@ static int handle_primary_tcp_pkt(RewriterState *rf, conn->offset -=3D (ntohl(tcp_pkt->th_ack) - 1); conn->syn_flag =3D 0; } - /* handle packets to the secondary from the primary */ - tcp_pkt->th_ack =3D htonl(ntohl(tcp_pkt->th_ack) + conn->offset); + if (conn->offset) { + /* handle packets to the secondary from the primary */ + tcp_pkt->th_ack =3D htonl(ntohl(tcp_pkt->th_ack) + conn->offse= t); =20 - net_checksum_calculate((uint8_t *)pkt->data, pkt->size); + net_checksum_calculate((uint8_t *)pkt->data, pkt->size); + } /* * Case 1: * The *server* side of this connect is VM, *client* tries to close @@ -112,7 +114,6 @@ static int handle_primary_tcp_pkt(RewriterState *rf, */ if ((conn->tcp_state =3D=3D TCPS_LAST_ACK) && (ntohl(tcp_pkt->th_ack) =3D=3D (conn->fin_ack_seq + 1))) { - fprintf(stderr, "Remove conn " g_hash_table_remove(rf->connection_track_table, key); } } @@ -159,10 +160,13 @@ static int handle_secondary_tcp_pkt(RewriterState *rf, } =20 if ((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) =3D=3D TH_ACK) { - /* handle packets to the primary from the secondary*/ - tcp_pkt->th_seq =3D htonl(ntohl(tcp_pkt->th_seq) - conn->offset); + /* Only need to adjust seq while offset is Non-zero */ + if (conn->offset) { + /* handle packets to the primary from the secondary*/ + tcp_pkt->th_seq =3D htonl(ntohl(tcp_pkt->th_seq) - conn->offse= t); =20 - net_checksum_calculate((uint8_t *)pkt->data, pkt->size); + net_checksum_calculate((uint8_t *)pkt->data, pkt->size); + } /* * Case 2: * The *server* side of this connect is VM, *server* tries to close --=20 1.8.3.1