Skip to contentSkip to conversation

exec: fix set_binfmt() vs sys_delete_module() race

PATCH In review Oleg Nesterov 2 participants 4 messages Jul 24, 2009 – Jul 29, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 exec: fix set_binfmt() vs sys_delete_module() race

sys_delete_module() can set MODULE_STATE_GOING after search_binary_handler() does try_module_get(). In this case set_binfmt()->try_module_get() fails but since none of the callers check the returned error, the task will run with the wrong old ->binfmt.

The proper fix should change all ->load_binary() methods, but we can rely on fact that the caller must hold a reference to binfmt->module and use __module_get() which never fails.

include/linux/binfmts.h renamed from include/linux/binfmts.h~SET_BINFMT +1 -1
@@ -119,7 +119,7 @@ extern int bprm_mm_init(struct linux_bin
 extern int copy_strings_kernel(int argc,char ** argv,struct linux_binprm *bprm);
 extern void install_exec_creds(struct linux_binprm *bprm);
 extern void do_coredump(long signr, int exit_code, struct pt_regs *regs);
-extern int set_binfmt(struct linux_binfmt *new);
+extern void set_binfmt(struct linux_binfmt *new);
 extern void free_bprm(struct linux_binprm *);
 
 #endif /* __KERNEL__ */
fs/exec.c renamed from fs/exec.c~SET_BINFMT +5 -9
@@ -1377,18 +1377,14 @@ out_ret:
 	return retval;
 }
 
-int set_binfmt(struct linux_binfmt *new)
+void set_binfmt(struct linux_binfmt *new)
 {
-	struct linux_binfmt *old = current->binfmt;
+	if (current->binfmt)
+		module_put(current->binfmt->module);
 
-	if (new) {
-		if (!try_module_get(new->module))
-			return -1;
-	}
 	current->binfmt = new;
-	if (old)
-		module_put(old->module);
-	return 0;
+	if (new)
+		__module_get(new->module);
 }
 
 EXPORT_SYMBOL(set_binfmt);
#1 lore raw
Quoted 9 lines · Oleg Nesterov

sys_delete_module() can set MODULE_STATE_GOING after search_binary_handler() does try_module_get(). In this case set_binfmt()->try_module_get() fails but since none of the callers check the returned error, the task will run with the wrong old ->binfmt.

The proper fix should change all ->load_binary() methods, but we can rely on fact that the caller must hold a reference to binfmt->module and use __module_get() which never fails.

Sounds reasonable.

Would like to put the last words as comments into code below?

Quoted 26 lines
include/linux/binfmts.h renamed from include/linux/binfmts.h~SET_BINFMT +1 -1
@@ -119,7 +119,7 @@ extern int bprm_mm_init(struct linux_bin
 extern int copy_strings_kernel(int argc,char ** argv,struct linux_binprm *bprm);
 extern void install_exec_creds(struct linux_binprm *bprm);
 extern void do_coredump(long signr, int exit_code, struct pt_regs *regs);
-extern int set_binfmt(struct linux_binfmt *new);
+extern void set_binfmt(struct linux_binfmt *new);
 extern void free_bprm(struct linux_binprm *);
 
 #endif /* __KERNEL__ */
fs/exec.c renamed from fs/exec.c~SET_BINFMT +3 -2
@@ -1377,18 +1377,14 @@ out_ret:
 	return retval;
 }
 
-int set_binfmt(struct linux_binfmt *new)
+void set_binfmt(struct linux_binfmt *new)
 {
-	struct linux_binfmt *old = current->binfmt;
+	if (current->binfmt)
+		module_put(current->binfmt->module);
 

fs/exec.c out_ret: · line 1387

Quoted 10 lines
fs/exec.c renamed from fs/exec.c~SET_BINFMT +2 -7
-	if (new) {
-		if (!try_module_get(new->module))
-			return -1;
-	}
 	current->binfmt = new;
-	if (old)
-		module_put(old->module);
-	return 0;
+	if (new)
+		__module_get(new->module);

I prefer to put the 'current->binfmt = new;' line as the last statement within this function, since this is more readable for me.

Quoted 3 lines
fs/exec.c renamed from fs/exec.c~SET_BINFMT +0 -0
 }
 
 EXPORT_SYMBOL(set_binfmt);
#2 lore raw
Quoted 7 lines · Amerigo Wang
Quoted 5 lines · Oleg Nesterov

sys_delete_module() can set MODULE_STATE_GOING after search_binary_handler() does try_module_get(). In this case set_binfmt()->try_module_get() fails but since none of the callers check the returned error, the task will run with the wrong old ->binfmt.

Quoted 8 lines
Quoted 4 lines

The proper fix should change all ->load_binary() methods, but we can rely on fact that the caller must hold a reference to binfmt->module and use __module_get() which never fails.

Sounds reasonable.

Would like to put the last words as comments into code below?

Yes, thanks.

Rusty pointed out this too, and I already sent the updated patch. But due to my mistake (I forgot to CC lkml) this was discussed off-list.

Quoted 7 lines
Quoted 7 lines
fs/exec.c renamed from fs/exec.c~SET_BINFMT +1 -1
-int set_binfmt(struct linux_binfmt *new)
+void set_binfmt(struct linux_binfmt *new)

{

fs/exec.c renamed from fs/exec.c~SET_BINFMT +2 -1
-	struct linux_binfmt *old = current->binfmt;
+	if (current->binfmt)
+		module_put(current->binfmt->module);
Quoted 14 lines

fs/exec.c out_ret: · line 1387

Quoted 10 lines
fs/exec.c renamed from fs/exec.c~SET_BINFMT +0 -4
-	if (new) {
-		if (!try_module_get(new->module))
-			return -1;
-	}
	current->binfmt = new;
fs/exec.c renamed from fs/exec.c~SET_BINFMT +2 -3
-	if (old)
-		module_put(old->module);
-	return 0;
+	if (new)
+		__module_get(new->module);

I prefer to put the 'current->binfmt = new;' line as the last statement within this function, since this is more readable for me.

Perhaps... but this is purely cosmetic, and the patch is already in -mm. Unless you have a strong feeleing, I'd prefer to not send yet another update.

Oleg.

#3 lore raw
Quoted 15 lines · Oleg Nesterov
Quoted 14 lines · Amerigo Wang
Quoted 9 lines · Oleg Nesterov

sys_delete_module() can set MODULE_STATE_GOING after search_binary_handler() does try_module_get(). In this case set_binfmt()->try_module_get() fails but since none of the callers check the returned error, the task will run with the wrong old ->binfmt.

The proper fix should change all ->load_binary() methods, but we can rely on fact that the caller must hold a reference to binfmt->module and use __module_get() which never fails.

Sounds reasonable.

Quoted 7 lines
Quoted 1 line

Would like to put the last words as comments into code below?

Yes, thanks.

Rusty pointed out this too, and I already sent the updated patch. But due to my mistake (I forgot to CC lkml) this was discussed off-list.

I see...

Quoted 23 lines
Quoted 8 lines
Quoted 8 lines
fs/exec.c renamed from fs/exec.c~SET_BINFMT +1 -1
-int set_binfmt(struct linux_binfmt *new)
+void set_binfmt(struct linux_binfmt *new)

{

fs/exec.c renamed from fs/exec.c~SET_BINFMT +2 -1
-	struct linux_binfmt *old = current->binfmt;
+	if (current->binfmt)
+		module_put(current->binfmt->module);
Quoted 14 lines

fs/exec.c out_ret: · line 1387

Quoted 10 lines
fs/exec.c renamed from fs/exec.c~SET_BINFMT +0 -4
-	if (new) {
-		if (!try_module_get(new->module))
-			return -1;
-	}
	current->binfmt = new;
fs/exec.c renamed from fs/exec.c~SET_BINFMT +2 -3
-	if (old)
-		module_put(old->module);
-	return 0;
+	if (new)
+		__module_get(new->module);

I prefer to put the 'current->binfmt = new;' line as the last statement within this function, since this is more readable for me.

Quoted 4 lines

Perhaps... but this is purely cosmetic, and the patch is already in -mm. Unless you have a strong feeleing, I'd prefer to not send yet another update.

No problem, it's just my personal taste. :-D

Thanks!