public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] kernfs: fix memory leak of kernfs_iattrs in __kernfs_new_node
@ 2025-11-24 16:02 Will Rosenberg
  2025-11-24 16:15 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 9+ messages in thread
From: Will Rosenberg @ 2025-11-24 16:02 UTC (permalink / raw)
  Cc: Will Rosenberg, Oliver Rosenberg, Greg Kroah-Hartman, Tejun Heo,
	linux-kernel

There exists a memory leak of kernfs_iattrs contained as an element
of kernfs_node allocated in __kernfs_new_node(). __kernfs_setattr()
allocates kernfs_iattrs as a sub-object, and the LSM security check
incorrectly errors out and does not free the kernfs_iattrs sub-object.

Make an additional error out case that properly frees kernfs_iattrs if
security_kernfs_init_security() fails.

Fixes: f72dae20891d7bcc43e9263ab206960b6ae5209f ("Merge tag 'selinux-pr-20190507' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux")
Co-developed-by: Oliver Rosenberg <olrose55@gmail.com>
Signed-off-by: Oliver Rosenberg <olrose55@gmail.com>
Signed-off-by: Will Rosenberg <whrosenb@asu.edu>
---
 fs/kernfs/dir.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index a670ba3e565e..5a40bfee7055 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -675,11 +675,13 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
 	if (parent) {
 		ret = security_kernfs_init_security(parent, kn);
 		if (ret)
-			goto err_out3;
+			goto err_out4;
 	}
 
 	return kn;
 
+ err_out4:
+	kfree(kn->iattr);
  err_out3:
 	spin_lock(&root->kernfs_idr_lock);
 	idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));

base-commit: dcb6fa37fd7bc9c3d2b066329b0d27dedf8becaa
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2025-12-07 22:38 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-24 16:02 [PATCH v2] kernfs: fix memory leak of kernfs_iattrs in __kernfs_new_node Will Rosenberg
2025-11-24 16:15 ` Greg Kroah-Hartman
2025-11-24 18:37   ` [PATCH v3] " Will Rosenberg
2025-11-24 20:41     ` Paul Moore
2025-11-25 15:11       ` Will Rosenberg
2025-11-25 15:13       ` [PATCH v4] " Will Rosenberg
2025-12-07 21:45         ` Paul Moore
2025-12-07 22:28           ` Will Rosenberg
2025-12-07 22:38             ` Paul Moore

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox