exec: cleanup the CONFIG_MODULES logic
search_binary_handler() uses "for (try=0; try<2; try++)" to avoid "goto" but the code looks too complicated and horrible imho. We still need to check "try == 0" before request_module() and add the additional "break" for !CONFIG_MODULES case. Kill this loop and use a simple "bool need_retry" + "goto retry". The code looks much simpler and we do not even need ifdef's, gcc can optimize out the "if (need_retry)" block if !IS_ENABLED(). Signed-off-by: Oleg Nesterov <oleg@redhat.com> Acked-by: Kees Cook <keescook@chromium.org> Cc: Al Viro <viro@ZenIV.linux.org.uk> Cc: Evgeniy Polyakov <zbr@ioremap.net> Cc: Zach Levis <zml@linux.vnet.ibm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
committed by
Linus Torvalds
parent
92eaa565ad
commit
cb7b6b1cbc
72
fs/exec.c
72
fs/exec.c
@@ -1367,13 +1367,15 @@ out:
|
|||||||
}
|
}
|
||||||
EXPORT_SYMBOL(remove_arg_zero);
|
EXPORT_SYMBOL(remove_arg_zero);
|
||||||
|
|
||||||
|
#define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
|
||||||
/*
|
/*
|
||||||
* cycle the list of binary formats handler, until one recognizes the image
|
* cycle the list of binary formats handler, until one recognizes the image
|
||||||
*/
|
*/
|
||||||
int search_binary_handler(struct linux_binprm *bprm)
|
int search_binary_handler(struct linux_binprm *bprm)
|
||||||
{
|
{
|
||||||
int try, retval;
|
bool need_retry = IS_ENABLED(CONFIG_MODULES);
|
||||||
struct linux_binfmt *fmt;
|
struct linux_binfmt *fmt;
|
||||||
|
int retval;
|
||||||
|
|
||||||
/* This allows 4 levels of binfmt rewrites before failing hard. */
|
/* This allows 4 levels of binfmt rewrites before failing hard. */
|
||||||
if (bprm->recursion_depth > 5)
|
if (bprm->recursion_depth > 5)
|
||||||
@@ -1388,47 +1390,39 @@ int search_binary_handler(struct linux_binprm *bprm)
|
|||||||
return retval;
|
return retval;
|
||||||
|
|
||||||
retval = -ENOENT;
|
retval = -ENOENT;
|
||||||
for (try=0; try<2; try++) {
|
retry:
|
||||||
read_lock(&binfmt_lock);
|
read_lock(&binfmt_lock);
|
||||||
list_for_each_entry(fmt, &formats, lh) {
|
list_for_each_entry(fmt, &formats, lh) {
|
||||||
if (!try_module_get(fmt->module))
|
if (!try_module_get(fmt->module))
|
||||||
continue;
|
continue;
|
||||||
read_unlock(&binfmt_lock);
|
|
||||||
bprm->recursion_depth++;
|
|
||||||
retval = fmt->load_binary(bprm);
|
|
||||||
bprm->recursion_depth--;
|
|
||||||
if (retval >= 0) {
|
|
||||||
put_binfmt(fmt);
|
|
||||||
return retval;
|
|
||||||
}
|
|
||||||
read_lock(&binfmt_lock);
|
|
||||||
put_binfmt(fmt);
|
|
||||||
if (retval != -ENOEXEC || bprm->mm == NULL)
|
|
||||||
break;
|
|
||||||
if (!bprm->file) {
|
|
||||||
read_unlock(&binfmt_lock);
|
|
||||||
return retval;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
read_unlock(&binfmt_lock);
|
read_unlock(&binfmt_lock);
|
||||||
#ifdef CONFIG_MODULES
|
bprm->recursion_depth++;
|
||||||
if (retval != -ENOEXEC || bprm->mm == NULL) {
|
retval = fmt->load_binary(bprm);
|
||||||
break;
|
bprm->recursion_depth--;
|
||||||
} else {
|
if (retval >= 0) {
|
||||||
#define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
|
put_binfmt(fmt);
|
||||||
if (printable(bprm->buf[0]) &&
|
return retval;
|
||||||
printable(bprm->buf[1]) &&
|
}
|
||||||
printable(bprm->buf[2]) &&
|
read_lock(&binfmt_lock);
|
||||||
printable(bprm->buf[3]))
|
put_binfmt(fmt);
|
||||||
break; /* -ENOEXEC */
|
if (retval != -ENOEXEC || bprm->mm == NULL)
|
||||||
if (try)
|
break;
|
||||||
break; /* -ENOEXEC */
|
if (!bprm->file) {
|
||||||
request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
|
read_unlock(&binfmt_lock);
|
||||||
|
return retval;
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
read_unlock(&binfmt_lock);
|
||||||
|
|
||||||
|
if (need_retry && retval == -ENOEXEC && bprm->mm) {
|
||||||
|
if (printable(bprm->buf[0]) && printable(bprm->buf[1]) &&
|
||||||
|
printable(bprm->buf[2]) && printable(bprm->buf[3]))
|
||||||
|
return retval;
|
||||||
|
request_module("binfmt-%04x", *(ushort *)(bprm->buf + 2));
|
||||||
|
need_retry = false;
|
||||||
|
goto retry;
|
||||||
|
}
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL(search_binary_handler);
|
EXPORT_SYMBOL(search_binary_handler);
|
||||||
|
Reference in New Issue
Block a user