From patchwork Wed Nov 9 09:07:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuang Wang X-Patchwork-Id: 17420 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp232600wru; Wed, 9 Nov 2022 01:17:51 -0800 (PST) X-Google-Smtp-Source: AMsMyM4Ej8txTYrvxetbBwp1vsOL8C2btMhwXRlTPqpqfQc0sIMpv/8ecSZWiVvvJmIIVUYE0FV3 X-Received: by 2002:a05:6a02:20c:b0:461:74e5:ce9f with SMTP id bh12-20020a056a02020c00b0046174e5ce9fmr52497915pgb.294.1667985471700; Wed, 09 Nov 2022 01:17:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667985471; cv=none; d=google.com; s=arc-20160816; b=ttoHC1/9e7x+BVHcd29jNGgVxLDsPrNpaeKg+refjk2l2QZXGYW9cLzdBomHh4z1lk ItcqtSqCvQKGZZegpezVG0AxU/Od6Hx9BwGJU5BsfHC/bAVDqw1DKB0pn9ZrV26Ji9LA D+HxuRWGOnQwADw/8pNo9BOaiCzu7icaqSBtf2yzMf7uQBdb33+fRG7+LX3weT264bTm LpeJOGqSkP4GH9bDb/dGEitjlx/gKhqBcq6rp7+7Hm7iCMjM2D5/fvaWG+HuCCxT5jM1 3vWseb0hiBfsHU63fXthiW7Qw/RuNToxBUCkd9zq/WbjZ3/UxUp54xBTfaueiXP7Jg9d lI8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:content-transfer-encoding:mime-version :message-id:date:subject:cc:from:dkim-signature; bh=ATZUQ+Nx2kluoQOYVdU2Cj3EMJ55/EmnBRWtaD0eIds=; b=iPUEbtmsUBArql8c1ZgRVOYSysRZxcnBdVU2rqZZOnxkVUZS9BJ9dtF6x/9bjd2LlH UfCChNxtJ72GF1eYzxAV03dbOGN77k2XPODq38tk1ao8WfWDtmMxpEgbPCLZ2FhicKmV 53gZJmZWhhrQKLLVTQz8Z6M3IGnBPoPo/p5gAM2gziMZ3BcwsiEGplQYyQ2KHSp6MK6H mzgKNosV6TvXeWh3WHSFud9g+lzPtglTggcAd3hdm5dO7fChwv0qjrWL8WPsc8gtudn3 vvzobm0gMEUaA3uP8gJuTqOiuHiRc7eBfHE3xkZ5lQvuerUZvqp3kleEB5KSlRM1Ul4E sjUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20210112 header.b=oEA1fT3i; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n18-20020a170902f61200b00178276c2e35si17549048plg.38.2022.11.09.01.17.38; Wed, 09 Nov 2022 01:17:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20210112 header.b=oEA1fT3i; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230385AbiKIJIz (ORCPT + 99 others); Wed, 9 Nov 2022 04:08:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230301AbiKIJIW (ORCPT ); Wed, 9 Nov 2022 04:08:22 -0500 Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A93BF2251F; Wed, 9 Nov 2022 01:07:52 -0800 (PST) Received: by mail-pf1-x430.google.com with SMTP id y203so16131383pfb.4; Wed, 09 Nov 2022 01:07:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=ATZUQ+Nx2kluoQOYVdU2Cj3EMJ55/EmnBRWtaD0eIds=; b=oEA1fT3iQwLXmfxIEDBSRKwG9XhxYfarc819wSUKkHKySyY3x5pnOnWZV7QAQIp93q nd5KE/pUjsfz8Gm8XvgMVVeQgqrKdreF8QfYJ8ebsGgYKr/byczX4OwPGVlOnFThCfd8 z3TeXpeqh2oc1XbSYcbd3nDBlGzeCc16uxOJ97iYQ2ft4HJiTa4J5+NxfjtUVfJKQUCj RSCqtiFxFpuJ4DdPM0KKtqPx8vJc03zqWHMdUUoSeWF8WtS0wi+r93CTfqS58Iw2r2ee yS+MOxgcuSJ5eS6mnr/iKbFw/Mo+h/IXo6OFMVJuobdp30m72opzIvI1YVT2gfo9FY2c z8+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ATZUQ+Nx2kluoQOYVdU2Cj3EMJ55/EmnBRWtaD0eIds=; b=4K+TvIYjk1fVL/9kiQfDO1xZ2I1UPy7FP6nxXf7vblWXiJdzJbNs+dI6LxZsvXo1UT 3tIeI5Lhf3A5j8pXu3qDprO1t5fbTlxOoBsr4ujKeTatKQEIH+uQAfWS+GsYhnqR73O6 FCot7uSGXwP9SRn3DQCxzq65pyzb/l6SdK+zWwQHT8C1+M9ck8VZ+JLFaLXMfirOhP4u CNWvzQV8BIpwPUb/FP2+j5g3aZ2Cwz2q48Akfe2sUpGUOPolAam1ogFhoGCR58u3MQgH x840XqU18bgGunThft4TVZvqHw17mnmnyAdy6/esnSkyqLZNzkHHrZcnUjWWPP9IiYXV CC1w== X-Gm-Message-State: ACrzQf1bAylXs6zJWOgnwPDO7Cg+sTrTNX8h4vO+w3Gz4LXEDOzTUcK9 zQB6PX4uV5T2pS6mQLqQx3w= X-Received: by 2002:a63:8a42:0:b0:46f:5804:8d9e with SMTP id y63-20020a638a42000000b0046f58048d9emr51838333pgd.214.1667984872117; Wed, 09 Nov 2022 01:07:52 -0800 (PST) Received: from localhost.localdomain ([47.242.114.172]) by smtp.gmail.com with ESMTPSA id i9-20020a17090332c900b0017a09ebd1e2sm8578304plr.237.2022.11.09.01.07.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Nov 2022 01:07:51 -0800 (PST) From: Chuang Wang Cc: Chuang Wang , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Gao Feng , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] net: macvlan: fix memory leaks of macvlan_common_newlink Date: Wed, 9 Nov 2022 17:07:34 +0800 Message-Id: <20221109090735.690500-1-nashuiliang@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749009534285160263?= X-GMAIL-MSGID: =?utf-8?q?1749009534285160263?= kmemleak reports memory leaks in macvlan_common_newlink, as follows: ip link add link eth0 name .. type macvlan mode source macaddr add kmemleak reports: unreferenced object 0xffff8880109bb140 (size 64): comm "ip", pid 284, jiffies 4294986150 (age 430.108s) hex dump (first 32 bytes): 00 00 00 00 00 00 00 00 b8 aa 5a 12 80 88 ff ff ..........Z..... 80 1b fa 0d 80 88 ff ff 1e ff ac af c7 c1 6b 6b ..............kk backtrace: [] kmem_cache_alloc_trace+0x1c7/0x300 [] macvlan_hash_add_source+0x45/0xc0 [] macvlan_changelink_sources+0xd7/0x170 [] macvlan_common_newlink+0x38c/0x5a0 [] macvlan_newlink+0xe/0x20 [] __rtnl_newlink+0x7af/0xa50 [] rtnl_newlink+0x48/0x70 ... In the scenario where the macvlan mode is configured as 'source', macvlan_changelink_sources() will be execured to reconfigure list of remote source mac addresses, at the same time, if register_netdevice() return an error, the resource generated by macvlan_changelink_sources() is not cleaned up. Using this patch, in the case of an error, it will execute macvlan_flush_sources() to ensure that the resource is cleaned up. Fixes: aa5fd0fb7748 ("driver: macvlan: Destroy new macvlan port if macvlan_common_newlink failed.") Signed-off-by: Chuang Wang --- drivers/net/macvlan.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/net/macvlan.c b/drivers/net/macvlan.c index c58fea63be7d..28f9f917ff54 100644 --- a/drivers/net/macvlan.c +++ b/drivers/net/macvlan.c @@ -1533,8 +1533,10 @@ int macvlan_common_newlink(struct net *src_net, struct net_device *dev, /* the macvlan port may be freed by macvlan_uninit when fail to register. * so we destroy the macvlan port only when it's valid. */ - if (create && macvlan_port_get_rtnl(lowerdev)) + if (create && macvlan_port_get_rtnl(lowerdev)) { + macvlan_flush_sources(port, vlan); macvlan_port_destroy(port->dev); + } return err; } EXPORT_SYMBOL_GPL(macvlan_common_newlink);