public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
From: Tejun Heo <tj@kernel.org>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Will Rosenberg <whrosenb@asu.edu>,
	Oliver Rosenberg <olrose55@gmail.com>, 杜义恒 <duyiheng@tju.edu.cn>,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: [PATCH] kernfs: fix NULL pointer dereference in __kernfs_new_node()
Date: Sun, 01 Feb 2026 07:05:50 -1000	[thread overview]
Message-ID: <f5fe5674adee792e663a86d680d836c5@kernel.org> (raw)
In-Reply-To: <AOoAIQD0J-9V1NW0JM55A4po.1.1769761572059.Hmail.3019244382@tju.edu.cn>

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1368 bytes --]

Commit 382b1e8f30f7 ("kernfs: fix memory leak of kernfs_iattrs in
__kernfs_new_node") introduced an err_out4 error path which frees iattr
when security_kernfs_init_security() fails. However, iattr is only
allocated by __kernfs_setattr() when the node has non-default uid/gid.
If the node uses default ownership, iattr remains NULL, and
security_kernfs_init_security() failure would cause a NULL pointer
dereference when err_out4 tries to access kn->iattr->xattrs.

Add a NULL check before freeing iattr.

Fixes: 382b1e8f30f7 ("kernfs: fix memory leak of kernfs_iattrs in __kernfs_new_node")
Cc: stable@vger.kernel.org
Reported-by: 杜义恒 <duyiheng@tju.edu.cn>
Signed-off-by: Tejun Heo <tj@kernel.org>
---
 fs/kernfs/dir.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index 5c0efd6b239f..29baeeb97871 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -681,8 +681,10 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
 	return kn;

  err_out4:
-	simple_xattrs_free(&kn->iattr->xattrs, NULL);
-	kmem_cache_free(kernfs_iattrs_cache, kn->iattr);
+	if (kn->iattr) {
+		simple_xattrs_free(&kn->iattr->xattrs, NULL);
+		kmem_cache_free(kernfs_iattrs_cache, kn->iattr);
+	}
  err_out3:
 	spin_lock(&root->kernfs_idr_lock);
 	idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
--
2.47.2

       reply	other threads:[~2026-02-01 17:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <AOoAIQD0J-9V1NW0JM55A4po.1.1769761572059.Hmail.3019244382@tju.edu.cn>
2026-02-01 17:05 ` Tejun Heo [this message]
2026-02-01 17:41   ` [PATCH] kernfs: fix NULL pointer dereference in __kernfs_new_node() Will Rosenberg

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=f5fe5674adee792e663a86d680d836c5@kernel.org \
    --to=tj@kernel.org \
    --cc=duyiheng@tju.edu.cn \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=olrose55@gmail.com \
    --cc=stable@vger.kernel.org \
    --cc=whrosenb@asu.edu \
    /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