Skip to content

Commit e7f1e88

Browse files
lztotorvalds
authored andcommitted
binfmt_misc: fix crash when load/unload module
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. Also, per Christian's comment, to fully restore the old behavior that won't break userspace the check(binfmt_misc_header) should be eliminated. To reproduce: modprobe binfmt_misc modprobe -r binfmt_misc modprobe binfmt_misc modprobe -r binfmt_misc modprobe binfmt_misc resulting in modprobe: can't load module binfmt_misc (kernel/fs/binfmt_misc.ko): Cannot allocate memory and an unhappy kernel: binfmt_misc: Failed to create fs/binfmt_misc sysctl mount point binfmt_misc: Failed to create fs/binfmt_misc sysctl mount point BUG: unable to handle page fault for address: fffffbfff8004802 Call Trace: init_misc_binfmt+0x2d/0x1000 [binfmt_misc] Link: https://lkml.kernel.org/r/20220124181812.1869535-2-ztong0001@gmail.com Fixes: 3ba442d ("fs: move binfmt_misc sysctl to its own file") Signed-off-by: Tong Zhang <ztong0001@gmail.com> Co-developed-by: Christian Brauner<brauner@kernel.org> Acked-by: Luis Chamberlain <mcgrof@kernel.org> Cc: Eric Biederman <ebiederm@xmission.com> Cc: Kees Cook <keescook@chromium.org> Cc: Iurii Zaikin <yzaikin@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 6cb9174 commit e7f1e88

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

fs/binfmt_misc.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -817,20 +817,20 @@ static struct file_system_type bm_fs_type = {
817817
};
818818
MODULE_ALIAS_FS("binfmt_misc");
819819

820+
static struct ctl_table_header *binfmt_misc_header;
821+
820822
static int __init init_misc_binfmt(void)
821823
{
822824
int err = register_filesystem(&bm_fs_type);
823825
if (!err)
824826
insert_binfmt(&misc_format);
825-
if (!register_sysctl_mount_point("fs/binfmt_misc")) {
826-
pr_warn("Failed to create fs/binfmt_misc sysctl mount point");
827-
return -ENOMEM;
828-
}
827+
binfmt_misc_header = register_sysctl_mount_point("fs/binfmt_misc");
829828
return 0;
830829
}
831830

832831
static void __exit exit_misc_binfmt(void)
833832
{
833+
unregister_sysctl_table(binfmt_misc_header);
834834
unregister_binfmt(&misc_format);
835835
unregister_filesystem(&bm_fs_type);
836836
}

0 commit comments

Comments
 (0)