linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1] binfmt_misc: fix crash when load/unload module
@ 2022-01-24  0:33 Tong Zhang
  2022-01-24 10:40 ` Christian Brauner
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Tong Zhang @ 2022-01-24  0:33 UTC (permalink / raw)
  To: Alexander Viro, Eric Biederman, Kees Cook, Luis Chamberlain,
	Andrew Morton, linux-fsdevel, linux-kernel
  Cc: Tong Zhang

We should unregister the table upon module unload otherwise something
horrible will happen when we load binfmt_misc module again. Also note
that we should keep value returned by register_sysctl_mount_point() and
release it later, otherwise it will leak.

reproduce:
modprobe binfmt_misc
modprobe -r binfmt_misc
modprobe binfmt_misc
modprobe -r binfmt_misc
modprobe binfmt_misc

[   18.032038] Call Trace:
[   18.032108]  <TASK>
[   18.032169]  dump_stack_lvl+0x34/0x44
[   18.032273]  __register_sysctl_table+0x6f4/0x720
[   18.032397]  ? preempt_count_sub+0xf/0xb0
[   18.032508]  ? 0xffffffffc0040000
[   18.032600]  init_misc_binfmt+0x2d/0x1000 [binfmt_misc]
[   18.042520] binfmt_misc: Failed to create fs/binfmt_misc sysctl mount point
modprobe: can't load module binfmt_misc (kernel/fs/binfmt_misc.ko): Cannot allocate memory
[   18.063549] binfmt_misc: Failed to create fs/binfmt_misc sysctl mount point
[   18.204779] BUG: unable to handle page fault for address: fffffbfff8004802

Fixes: 3ba442d5331f ("fs: move binfmt_misc sysctl to its own file")
Signed-off-by: Tong Zhang <ztong0001@gmail.com>
---
 fs/binfmt_misc.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/fs/binfmt_misc.c b/fs/binfmt_misc.c
index ddea6acbddde..614aedb8ab2e 100644
--- a/fs/binfmt_misc.c
+++ b/fs/binfmt_misc.c
@@ -817,12 +817,16 @@ static struct file_system_type bm_fs_type = {
 };
 MODULE_ALIAS_FS("binfmt_misc");
 
+static struct ctl_table_header *binfmt_misc_header;
+
 static int __init init_misc_binfmt(void)
 {
 	int err = register_filesystem(&bm_fs_type);
 	if (!err)
 		insert_binfmt(&misc_format);
-	if (!register_sysctl_mount_point("fs/binfmt_misc")) {
+
+	binfmt_misc_header = register_sysctl_mount_point("fs/binfmt_misc");
+	if (!binfmt_misc_header) {
 		pr_warn("Failed to create fs/binfmt_misc sysctl mount point");
 		return -ENOMEM;
 	}
@@ -831,6 +835,7 @@ static int __init init_misc_binfmt(void)
 
 static void __exit exit_misc_binfmt(void)
 {
+	unregister_sysctl_table(binfmt_misc_header);
 	unregister_binfmt(&misc_format);
 	unregister_filesystem(&bm_fs_type);
 }
-- 
2.25.1


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

end of thread, other threads:[~2022-01-29  0:25 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-01-24  0:33 [PATCH v1] binfmt_misc: fix crash when load/unload module Tong Zhang
2022-01-24 10:40 ` Christian Brauner
2022-01-24 18:18   ` [PATCH v2 0/2] Fix regression on binfmt_misc Tong Zhang
2022-01-24 18:18   ` [PATCH v2 1/2] binfmt_misc: fix crash when load/unload module Tong Zhang
2022-01-25 18:15     ` Luis Chamberlain
2022-01-24 18:18   ` [PATCH v2 2/2] sysctl: fix return type to make compiler happy Tong Zhang
2022-01-24 18:23   ` [PATCH v1] binfmt_misc: fix crash when load/unload module Tong Zhang
2022-01-24 11:40 ` kernel test robot
2022-01-24 23:16   ` Andrew Morton
2022-01-25 18:14     ` Luis Chamberlain
2022-01-26  5:04     ` Murphy Zhou
2022-01-26  5:23       ` Tong Zhang
2022-01-26  6:33         ` Tong Zhang
2022-01-29  0:25           ` Murphy Zhou
2022-01-24 12:14 ` kernel test robot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).