[v3] vdpa_sim: fix possible memory leak in vdpasim_net_init() and vdpasim_blk_init()
Message ID | 20221110082348.4105476-1-ruanjinjie@huawei.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp21040wru; Thu, 10 Nov 2022 00:35:41 -0800 (PST) X-Google-Smtp-Source: AMsMyM4/ZkKVLrlFcL7/CuwKSkX9D202HFSC1T9KcxDgMPYoqlmkBhi1bGIn45Bhf6l9NKwx8751 X-Received: by 2002:a63:d64:0:b0:460:7bb8:b573 with SMTP id 36-20020a630d64000000b004607bb8b573mr2026963pgn.513.1668069340974; Thu, 10 Nov 2022 00:35:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668069340; cv=none; d=google.com; s=arc-20160816; b=SOVLW01BkrDnXV+NugZ316aJRKUh4RULDgwSBUvnThqXReQOTtZKsDEjW1dvQYV7fl ItamLrCbBXBGkVjsKerEgqloMVCeuuifivAFScyw1e3kKs2nUvCadjvNbAuyPZNi4SiS zNl7KFcZWn5y8g7ce+1/qo1CBM8o7OtWu2uHU/xqaYjY6uJdTIeJ3AUcbu4ytwZnysaa FzlthvwK1NGkbSa6nDw+QVzeymy5kNl6y0Raw9ZgQABfaaqmQNbw61MnKDYinCM1Z+Mu cWAl8grfSgNJBotrjrtwV1aQVNBhzfFLhVwbrS2NefNrhxnPVjtnbyY2Noty1E+qEslE D0Eg== 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=BqFh/dx2Kq9DN8tSWvN8c1FK6tXBlp16nQwOHKUyEOQ=; b=pa4wqMe7bta5T8pI0yMQiags0PDRNBMvKBnfU9vw5UOAPPN7WnhBOe2dREV2GVzMB6 l8HgIHEN6Ihaptw7HIxQBWnPhy4bsuAZR7K6O9sTMdZgQq+At2Axfdr8hXrQ6BJr5iC2 a5Z5Dbh5+DC86a1XDx3aX7DgId96K5ww/SwvhH+y9SIUkj8SrN+6S0hCXnNheDEW8NtZ vyjN4ko9aob/L1p4mDuy4xO1v9v/8rExGmSoPB5wrREYyw2zNUvym7Zc45+s6lh/sfcJ KqAw08t9ujTXltEtdDZNlUDFOe9Kmb7ve6ogk4q5KT+EPXrWFfEfyYgzO/8iLcoXmFXy bZhA== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bd32-20020a056a0027a000b0056368cb7d1asi21635904pfb.104.2022.11.10.00.35.11; Thu, 10 Nov 2022 00:35:40 -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 S232992AbiKJI1J (ORCPT <rfc822;winker.wchi@gmail.com> + 99 others); Thu, 10 Nov 2022 03:27:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232977AbiKJI1I (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 10 Nov 2022 03:27:08 -0500 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 30F301F9F0 for <linux-kernel@vger.kernel.org>; Thu, 10 Nov 2022 00:27:07 -0800 (PST) Received: from kwepemi500008.china.huawei.com (unknown [172.30.72.54]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4N7FL765n9zHvqG; Thu, 10 Nov 2022 16:26:39 +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 16:27:04 +0800 From: ruanjinjie <ruanjinjie@huawei.com> To: <mst@redhat.com>, <jasowang@redhat.com>, <sgarzare@redhat.com>, <eperezma@redhat.com>, <gautam.dawar@xilinx.com>, <elic@nvidia.com>, <virtualization@lists.linux-foundation.org>, <linux-kernel@vger.kernel.org> CC: <ruanjinjie@huawei.com> Subject: [PATCH v3] vdpa_sim: fix possible memory leak in vdpasim_net_init() and vdpasim_blk_init() Date: Thu, 10 Nov 2022 16:23:48 +0800 Message-ID: <20221110082348.4105476-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: dggems705-chm.china.huawei.com (10.3.19.182) 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749097477245860985?= X-GMAIL-MSGID: =?utf-8?q?1749097477245860985?= |
Series |
[v3] vdpa_sim: fix possible memory leak in vdpasim_net_init() and vdpasim_blk_init()
|
|
Commit Message
Jinjie Ruan
Nov. 10, 2022, 8:23 a.m. UTC
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: [<ffffffff8174f19e>] __kmalloc_node_track_caller+0x4e/0x150 [<ffffffff81731d53>] kstrdup+0x33/0x60 [<ffffffff83a5d421>] kobject_set_name_vargs+0x41/0x110 [<ffffffff82d87aab>] dev_set_name+0xab/0xe0 [<ffffffff82d91a23>] device_add+0xe3/0x1a80 [<ffffffffa0270013>] 0xffffffffa0270013 [<ffffffff81001c27>] do_one_initcall+0x87/0x2e0 [<ffffffff813739cb>] do_init_module+0x1ab/0x640 [<ffffffff81379d20>] load_module+0x5d00/0x77f0 [<ffffffff8137bc40>] __do_sys_finit_module+0x110/0x1b0 [<ffffffff83c4d505>] do_syscall_64+0x35/0x80 [<ffffffff83e0006a>] 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: [<ffffffff8174f19e>] __kmalloc_node_track_caller+0x4e/0x150 [<ffffffff81731d53>] kstrdup+0x33/0x60 [<ffffffff83a5d421>] kobject_set_name_vargs+0x41/0x110 [<ffffffff82d87aab>] dev_set_name+0xab/0xe0 [<ffffffff82d91a23>] device_add+0xe3/0x1a80 [<ffffffffa0220013>] 0xffffffffa0220013 [<ffffffff81001c27>] do_one_initcall+0x87/0x2e0 [<ffffffff813739cb>] do_init_module+0x1ab/0x640 [<ffffffff81379d20>] load_module+0x5d00/0x77f0 [<ffffffff8137bc40>] __do_sys_finit_module+0x110/0x1b0 [<ffffffff83c4d505>] do_syscall_64+0x35/0x80 [<ffffffff83e0006a>] entry_SYSCALL_64_after_hwframe+0x46/0xb0 Fixes: 899c4d187f6a ("vdpa_sim_blk: add support for vdpa management tool") Fixes: a3c06ae158dd ("vdpa_sim_net: Add support for user supported devices") Signed-off-by: ruanjinjie <ruanjinjie@huawei.com> Reviewed-by: Stefano Garzarella <sgarzare@redhat.com> --- v3: - add fix tag 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(-)
Comments
On Thu, Nov 10, 2022 at 4:27 PM ruanjinjie <ruanjinjie@huawei.com> wrote: > > 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: > [<ffffffff8174f19e>] __kmalloc_node_track_caller+0x4e/0x150 > [<ffffffff81731d53>] kstrdup+0x33/0x60 > [<ffffffff83a5d421>] kobject_set_name_vargs+0x41/0x110 > [<ffffffff82d87aab>] dev_set_name+0xab/0xe0 > [<ffffffff82d91a23>] device_add+0xe3/0x1a80 > [<ffffffffa0270013>] 0xffffffffa0270013 > [<ffffffff81001c27>] do_one_initcall+0x87/0x2e0 > [<ffffffff813739cb>] do_init_module+0x1ab/0x640 > [<ffffffff81379d20>] load_module+0x5d00/0x77f0 > [<ffffffff8137bc40>] __do_sys_finit_module+0x110/0x1b0 > [<ffffffff83c4d505>] do_syscall_64+0x35/0x80 > [<ffffffff83e0006a>] 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: > [<ffffffff8174f19e>] __kmalloc_node_track_caller+0x4e/0x150 > [<ffffffff81731d53>] kstrdup+0x33/0x60 > [<ffffffff83a5d421>] kobject_set_name_vargs+0x41/0x110 > [<ffffffff82d87aab>] dev_set_name+0xab/0xe0 > [<ffffffff82d91a23>] device_add+0xe3/0x1a80 > [<ffffffffa0220013>] 0xffffffffa0220013 > [<ffffffff81001c27>] do_one_initcall+0x87/0x2e0 > [<ffffffff813739cb>] do_init_module+0x1ab/0x640 > [<ffffffff81379d20>] load_module+0x5d00/0x77f0 > [<ffffffff8137bc40>] __do_sys_finit_module+0x110/0x1b0 > [<ffffffff83c4d505>] do_syscall_64+0x35/0x80 > [<ffffffff83e0006a>] entry_SYSCALL_64_after_hwframe+0x46/0xb0 > > Fixes: 899c4d187f6a ("vdpa_sim_blk: add support for vdpa management tool") > Fixes: a3c06ae158dd ("vdpa_sim_net: Add support for user supported devices") > > Signed-off-by: ruanjinjie <ruanjinjie@huawei.com> > Reviewed-by: Stefano Garzarella <sgarzare@redhat.com> Acked-by: Jason Wang <jasowang@redhat.com> Thanks > --- > v3: > - add fix tag > 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 >
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)