Linux NFS development
 help / color / mirror / Atom feed
From: Wenchao Hao <haowenchao22@gmail.com>
To: Trond Myklebust <trond.myklebust@hammerspace.com>,
	Anna Schumaker <anna@kernel.org>,
	linux-nfs@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Wenchao Hao <haowenchao2@huawei.com>
Subject: [PATCH] NFS: Fix memleak in nfs_sysfs_move_sb_to_server
Date: Wed, 12 Jul 2023 21:48:53 +0800	[thread overview]
Message-ID: <20230712134853.1044134-1-haowenchao2@huawei.com> (raw)

call kfree() to free memory allocated in kasprintf() to fix
memleak. It's easy to recurrent this memleak, just mount and
remount nfs at NFS client.

The bug is detected by kmemleak with following stack:
unreferenced object 0xffffa31a91fdf2c0 (size 16):
  comm "mount.nfs", pid 960, jiffies 4294724478 (age 111892.067s)
  hex dump (first 16 bytes):
    73 65 72 76 65 72 2d 30 00 f2 fd 91 1a a3 ff ff  server-0........
  backtrace:
    [<00000000de9b21a9>] create_object+0x11/0x17
    [<00000000ed17aa96>] kmemleak_alloc+0x6c/0x7a
    [<000000009f4acc69>] slab_post_alloc_hook.constprop.0+0x81/0x99
    [<00000000d2446455>] __kmem_cache_alloc_node+0x10c/0x13a
    [<00000000eca4a704>] __kmalloc_node_track_caller+0x83/0xb7
    [<0000000076cdb22f>] kvasprintf+0x65/0xc3
    [<00000000f6f2e879>] kasprintf+0x4e/0x69
    [<00000000e49d03be>] nfs_sysfs_move_sb_to_server+0x24/0x5f
    [<0000000039127597>] nfs_kill_super+0x25/0x49
    [<00000000e6ecf809>] deactivate_locked_super+0x35/0x74
    [<000000003b9c3350>] deactivate_super+0x3c/0x44
    [<00000000777c86be>] cleanup_mnt+0x6c/0xe7
    [<000000009f181749>] __cleanup_mnt+0x12/0x18
    [<00000000a709602e>] task_work_run+0x75/0x8c
    [<0000000075da06ba>] exit_to_user_mode_prepare+0x79/0x102
    [<0000000093a53d48>] syscall_exit_to_user_mode+0x1c/0x30

Signed-off-by: Wenchao Hao <haowenchao2@huawei.com>
---
 fs/nfs/sysfs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/fs/nfs/sysfs.c b/fs/nfs/sysfs.c
index acda8f033d30..bf378ecd5d9f 100644
--- a/fs/nfs/sysfs.c
+++ b/fs/nfs/sysfs.c
@@ -345,8 +345,10 @@ void nfs_sysfs_move_sb_to_server(struct nfs_server *server)
 	int ret = -ENOMEM;
 
 	s = kasprintf(GFP_KERNEL, "server-%d", server->s_sysfs_id);
-	if (s)
+	if (s) {
 		ret = kobject_rename(&server->kobj, s);
+		kfree(s);
+	}
 	if (ret < 0)
 		pr_warn("NFS: rename sysfs %s failed (%d)\n",
 					server->kobj.name, ret);
-- 
2.35.3


             reply	other threads:[~2023-07-12 13:49 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-12 13:48 Wenchao Hao [this message]
2023-07-12 13:57 ` [PATCH] NFS: Fix memleak in nfs_sysfs_move_sb_to_server Wenchao Hao
2023-07-12 14:01 ` Ido Schimmel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230712134853.1044134-1-haowenchao2@huawei.com \
    --to=haowenchao22@gmail.com \
    --cc=anna@kernel.org \
    --cc=haowenchao2@huawei.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=trond.myklebust@hammerspace.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox