Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp130328rwb; Wed, 9 Nov 2022 21:25:08 -0800 (PST) X-Google-Smtp-Source: AMsMyM5yyrt0c7m9T3fH6bzbKwvMsVPdes1ORo7VictjGlQDvz3UYZ/z4zpcztc7vq+7mGQDE5Hr X-Received: by 2002:a05:6a00:24cb:b0:56c:7815:bc7d with SMTP id d11-20020a056a0024cb00b0056c7815bc7dmr64175779pfv.44.1668057907840; Wed, 09 Nov 2022 21:25:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668057907; cv=none; d=google.com; s=arc-20160816; b=CDv64ChyfvzBWUUUiiRJtqOoc7mupywyA1ZRPXaQNUZwUaISaMa7/IkNYEOco1kJPL j+BnXs5WOjQrHik9dvLr2jXmvnCaalg5nod3XMrtbTKtVoLs2VkFHhf92UfylsVWVkRH wUzrvl9fupNQ0Powv7c6jOEeAk4pnn/4LH4hKt6xRBxKjxAqrPxehSyiSy88WSiN1K+o 4PM+RRYSk06pW55qzxPlf36qUSfwuSgyI4QNR0YGWm9mF7V2pgoebEWQSTrxAsH2ZGhM NN1iTPIv8ybgUfZboJRki4wV/KTB6hIZIe7qdy3h925Gg9v8LOTTQmXSTfbzQpFvFm3x v+9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=+Wcmz7voPyOA88P4ELUjFdHw2d1whQZrgF5bh9CTi7w=; b=u7PNJ5ZWxHxuJhJpe1NSvgMyEheCtpvjWnXX030FbrU46FKNIo3lXQC4KgfP3qqCOQ XnJ6h4ez65zjyqzxpnCRdD/oDr7iIV9vILPI3y6iNNRqiPTVSWOIx2dcESYoBOFEoTll 94G3wPAtx5o5PCpsTUyGqp8wpdgBoOPDf73ypZM2i+sqXchwr7yKhYK0u+3OqB3aJQIb T3qDoMAKhlyNUNUzU3E3P6ZHp1icCWPoc2FUVBtTqYRjkkcRiUxohV1O7icTdZpbpEuw 2lOBBK5wXelAXehqMHTOtri74Ub9LkllXpuwtE3sPSDKxxLF0D7TmOvyIArErFCcvgFd Hjzg== ARC-Authentication-Results: i=1; mx.google.com; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l125-20020a632583000000b0046fbe60adc5si20325852pgl.510.2022.11.09.21.24.55; Wed, 09 Nov 2022 21:25:07 -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; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230243AbiKJFIM (ORCPT + 92 others); Thu, 10 Nov 2022 00:08:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46456 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229516AbiKJFIH (ORCPT ); Thu, 10 Nov 2022 00:08:07 -0500 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 413A12C123 for ; Wed, 9 Nov 2022 21:08:05 -0800 (PST) Received: from kwepemi500008.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4N78wh3jVHzmV7B; Thu, 10 Nov 2022 13:07:48 +0800 (CST) Received: from huawei.com (10.67.175.83) by kwepemi500008.china.huawei.com (7.221.188.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Thu, 10 Nov 2022 13:08:02 +0800 From: ruanjinjie To: , , , , , , , CC: Subject: [PATCH v2] vdpa_sim: fix possible memory leak in vdpasim_net_init() and vdpasim_blk_init() Date: Thu, 10 Nov 2022 13:04:46 +0800 Message-ID: <20221110050446.3932031-1-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.67.175.83] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemi500008.china.huawei.com (7.221.188.139) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Inject fault while probing module, if device_register() fails in vdpasim_net_init() or vdpasim_blk_init(), but the refcount of kobject is not decreased to 0, the name allocated in dev_set_name() is leaked. Fix this by calling put_device(), so that name can be freed in callback function kobject_cleanup(). (vdpa_sim_net) unreferenced object 0xffff88807eebc370 (size 16): comm "modprobe", pid 3848, jiffies 4362982860 (age 18.153s) hex dump (first 16 bytes): 76 64 70 61 73 69 6d 5f 6e 65 74 00 6b 6b 6b a5 vdpasim_net.kkk. backtrace: [] __kmalloc_node_track_caller+0x4e/0x150 [] kstrdup+0x33/0x60 [] kobject_set_name_vargs+0x41/0x110 [] dev_set_name+0xab/0xe0 [] device_add+0xe3/0x1a80 [] 0xffffffffa0270013 [] do_one_initcall+0x87/0x2e0 [] do_init_module+0x1ab/0x640 [] load_module+0x5d00/0x77f0 [] __do_sys_finit_module+0x110/0x1b0 [] do_syscall_64+0x35/0x80 [] entry_SYSCALL_64_after_hwframe+0x46/0xb0 (vdpa_sim_blk) unreferenced object 0xffff8881070c1250 (size 16): comm "modprobe", pid 6844, jiffies 4364069319 (age 17.572s) hex dump (first 16 bytes): 76 64 70 61 73 69 6d 5f 62 6c 6b 00 6b 6b 6b a5 vdpasim_blk.kkk. backtrace: [] __kmalloc_node_track_caller+0x4e/0x150 [] kstrdup+0x33/0x60 [] kobject_set_name_vargs+0x41/0x110 [] dev_set_name+0xab/0xe0 [] device_add+0xe3/0x1a80 [] 0xffffffffa0220013 [] do_one_initcall+0x87/0x2e0 [] do_init_module+0x1ab/0x640 [] load_module+0x5d00/0x77f0 [] __do_sys_finit_module+0x110/0x1b0 [] do_syscall_64+0x35/0x80 [] entry_SYSCALL_64_after_hwframe+0x46/0xb0 Signed-off-by: ruanjinjie --- v2: - add fault inject message --- drivers/vdpa/vdpa_sim/vdpa_sim_blk.c | 4 +++- drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c b/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c index c6db1a1baf76..f745926237a8 100644 --- a/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c @@ -427,8 +427,10 @@ static int __init vdpasim_blk_init(void) int ret; ret = device_register(&vdpasim_blk_mgmtdev); - if (ret) + if (ret) { + put_device(&vdpasim_blk_mgmtdev); return ret; + } ret = vdpa_mgmtdev_register(&mgmt_dev); if (ret) diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c index c3cb225ea469..11f5a121df24 100644 --- a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c @@ -305,8 +305,10 @@ static int __init vdpasim_net_init(void) int ret; ret = device_register(&vdpasim_net_mgmtdev); - if (ret) + if (ret) { + put_device(&vdpasim_net_mgmtdev); return ret; + } ret = vdpa_mgmtdev_register(&mgmt_dev); if (ret) -- 2.25.1