From patchwork Thu Jun 6 11:17:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xin Long X-Patchwork-Id: 10979321 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B7C5713AD for ; Thu, 6 Jun 2019 11:17:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A592828869 for ; Thu, 6 Jun 2019 11:17:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 99710288BA; Thu, 6 Jun 2019 11:17:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from bastion.fedoraproject.org (bastion01.fedoraproject.org [209.132.181.2]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 27B6928869 for ; Thu, 6 Jun 2019 11:17:53 +0000 (UTC) Received: from mailman01.phx2.fedoraproject.org (mailman01.phx2.fedoraproject.org [10.5.126.36]) by bastion01.phx2.fedoraproject.org (Postfix) with ESMTP id 44D0961E17AB; Thu, 6 Jun 2019 11:17:53 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 bastion01.phx2.fedoraproject.org 44D0961E17AB Received: from mailman01.phx2.fedoraproject.org (localhost [IPv6:::1]) by mailman01.phx2.fedoraproject.org (Postfix) with ESMTP id 35C3558096452; Thu, 6 Jun 2019 11:17:53 +0000 (UTC) Received: by mailman01.phx2.fedoraproject.org (Postfix, from userid 991) id 8422058096451; Thu, 6 Jun 2019 11:17:50 +0000 (UTC) Received: from smtp-mm-ib01.fedoraproject.org (smtp-mm-ib01.vpn.fedoraproject.org [192.168.1.83]) by mailman01.phx2.fedoraproject.org (Postfix) with ESMTP id 10DD258096444 for ; Thu, 6 Jun 2019 11:17:50 +0000 (UTC) Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK)) by smtp-mm-ib01.fedoraproject.org (Postfix) with ESMTPS id 9A476605D2E7 for ; Thu, 6 Jun 2019 11:17:49 +0000 (UTC) Received: by mail-pg1-f196.google.com with SMTP id v11so1179449pgl.5 for ; Thu, 06 Jun 2019 04:17:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=BrghANHyuvE+KpkDr3Ktw0yS8AmMNW1/BiluJxUk38w=; b=O2fez6R3720fDHILdactN6TFQpftBegjYBIpNZ6RymvIpMaC/I5P7nPozGltYqFIsP OOFKcqSzVMCybQT6xSvJuP/Ih8MFXfsk7fnp3CZBh+9e1dXljhw5B5Y7FuGT6DXNoq2H J41dbDZ9gmA3iBmcy0q98nZT9Rbj7WeawKOn1THuKgBUex9fzuGrMq1b4ywz16iPtdmh 7MDOUQVTYPDXGtRvwhhrsPfC9oQzJJrSoRIOeaAlDOWGkGng+v0ynAI+1Y0nOqr3y0vb 9D+aXR50PtYFe6vyj7qJ1ss0UXS3CyYAlZKo2xMeRT515id5Ps6eABjAn/Ient5kCEel 32aA== X-Gm-Message-State: APjAAAUENjz4SJnPWyUx/yg8z9twHGN4os49+n2oieIMUqtgiNsQahXn GK/tujheLgEpcPvtg1B6SaK6Twvx X-Google-Smtp-Source: APXvYqz9r3d8HyIR6qyzTpi1YD+3WxotXjT9FD4qEiH9sqXRQO6GJIGvDatz6+vNOm/VQEP+2AACVQ== X-Received: by 2002:a63:d658:: with SMTP id d24mr2912339pgj.191.1559819868089; Thu, 06 Jun 2019 04:17:48 -0700 (PDT) Received: from localhost ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id v4sm2617422pff.45.2019.06.06.04.17.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 06 Jun 2019 04:17:47 -0700 (PDT) From: Xin Long To: libteam@lists.fedorahosted.org Subject: [PATCH] teamd: fix a json object memleak in get_port_obj() Date: Thu, 6 Jun 2019 19:17:39 +0800 Message-Id: <1577c20a41fd09549b8dc3ee933d48bdc668553d.1559819859.git.lucien.xin@gmail.com> X-Mailer: git-send-email 2.1.0 Message-ID-Hash: 6XWQ5GXGZNZUUVI7465S7SGMERW74KJN X-Message-ID-Hash: 6XWQ5GXGZNZUUVI7465S7SGMERW74KJN X-MailFrom: lucien.xin@gmail.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header CC: Jiri Pirko X-Mailman-Version: 3.1.1 Precedence: list List-Id: Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP The issue can be noticed simply by doing: # ip link add aeth0 type dummy # teamd -d -t ateam0 -c '{"link_watch" : {"name" : "ethtool"}}' # teamdctl ateam0 port add aeth0 # while true; do # teamdctl ateam0 config dump actual > /dev/null # date "+%Y-%m-%dT%H:%M:%S $(ps -auwwx | grep teamd |grep -v grep)" # done After 30 mins: USER PID %CPU %MEM VSZ ... COMMAND 2019-06-06T06:25:55 root 26574 0.0 0.0 66572 ... teamd ... ... 2019-06-06T06:55:55 root 26574 1.2 0.6 89012 ... teamd ... MEM used by team grew from 0.0% to 0.6%, VSZ from 66572 to 89012. get_port_obj() calld in teamd_config_actual_dump() should not have used json_object_set(), which can json_incref the child object causing later json_decref(parent object) not to be able to free it. So change to use json_object_set_new(), a function for setting newly created objects, and it won't json_incref the child object that will be freed when its parent object gets freed later. Fixes: c6eba4866e18 ("teamd: add possibility to add/change port config") Reported-by: Michael Colombo Signed-off-by: Xin Long --- teamd/teamd_config.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/teamd/teamd_config.c b/teamd/teamd_config.c index 69b25de..34fef1f 100644 --- a/teamd/teamd_config.c +++ b/teamd/teamd_config.c @@ -84,7 +84,7 @@ static int get_port_obj(json_t **pport_obj, json_t *config_json, ports_obj = json_object(); if (!ports_obj) return -ENOMEM; - err = json_object_set(config_json, "ports", ports_obj); + err = json_object_set_new(config_json, "ports", ports_obj); if (err) { json_decref(ports_obj); return -ENOMEM; @@ -95,7 +95,7 @@ static int get_port_obj(json_t **pport_obj, json_t *config_json, port_obj = json_object(); if (!port_obj) return -ENOMEM; - err = json_object_set(ports_obj, port_name, port_obj); + err = json_object_set_new(ports_obj, port_name, port_obj); if (err) { json_decref(port_obj); return -ENOMEM;