Skip to content

Commit d740269

Browse files
keestorvalds
authored andcommitted
exec: use -ELOOP for max recursion depth
To avoid an explosion of request_module calls on a chain of abusive scripts, fail maximum recursion with -ELOOP instead of -ENOEXEC. As soon as maximum recursion depth is hit, the error will fail all the way back up the chain, aborting immediately. This also has the side-effect of stopping the user's shell from attempting to reexecute the top-level file as a shell script. As seen in the dash source: if (cmd != path_bshell && errno == ENOEXEC) { *argv-- = cmd; *argv = cmd = path_bshell; goto repeat; } The above logic was designed for running scripts automatically that lacked the "#!" header, not to re-try failed recursion. On a legitimate -ENOEXEC, things continue to behave as the shell expects. Additionally, when tracking recursion, the binfmt handlers should not be involved. The recursion being tracked is the depth of calls through search_binary_handler(), so that function should be exclusively responsible for tracking the depth. Signed-off-by: Kees Cook <[email protected]> Cc: halfdog <[email protected]> Cc: P J P <[email protected]> Cc: Alexander Viro <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 8d23802 commit d740269

File tree

5 files changed

+6
-17
lines changed

5 files changed

+6
-17
lines changed

fs/binfmt_em86.c

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ static int load_em86(struct linux_binprm *bprm)
4242
return -ENOEXEC;
4343
}
4444

45-
bprm->recursion_depth++; /* Well, the bang-shell is implicit... */
4645
allow_write_access(bprm->file);
4746
fput(bprm->file);
4847
bprm->file = NULL;

fs/binfmt_misc.c

-6
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,6 @@ static int load_misc_binary(struct linux_binprm *bprm)
117117
if (!enabled)
118118
goto _ret;
119119

120-
retval = -ENOEXEC;
121-
if (bprm->recursion_depth > BINPRM_MAX_RECURSION)
122-
goto _ret;
123-
124120
/* to keep locking time low, we copy the interpreter string */
125121
read_lock(&entries_lock);
126122
fmt = check_file(bprm);
@@ -197,8 +193,6 @@ static int load_misc_binary(struct linux_binprm *bprm)
197193
if (retval < 0)
198194
goto _error;
199195

200-
bprm->recursion_depth++;
201-
202196
retval = search_binary_handler(bprm);
203197
if (retval < 0)
204198
goto _error;

fs/binfmt_script.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@ static int load_script(struct linux_binprm *bprm)
2222
char interp[BINPRM_BUF_SIZE];
2323
int retval;
2424

25-
if ((bprm->buf[0] != '#') || (bprm->buf[1] != '!') ||
26-
(bprm->recursion_depth > BINPRM_MAX_RECURSION))
25+
if ((bprm->buf[0] != '#') || (bprm->buf[1] != '!'))
2726
return -ENOEXEC;
2827
/*
2928
* This section does the #! interpretation.
3029
* Sorta complicated, but hopefully it will work. -TYT
3130
*/
3231

33-
bprm->recursion_depth++;
3432
allow_write_access(bprm->file);
3533
fput(bprm->file);
3634
bprm->file = NULL;

fs/exec.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,10 @@ int search_binary_handler(struct linux_binprm *bprm)
13561356
struct linux_binfmt *fmt;
13571357
pid_t old_pid, old_vpid;
13581358

1359+
/* This allows 4 levels of binfmt rewrites before failing hard. */
1360+
if (depth > 5)
1361+
return -ELOOP;
1362+
13591363
retval = security_bprm_check(bprm);
13601364
if (retval)
13611365
return retval;
@@ -1380,12 +1384,8 @@ int search_binary_handler(struct linux_binprm *bprm)
13801384
if (!try_module_get(fmt->module))
13811385
continue;
13821386
read_unlock(&binfmt_lock);
1387+
bprm->recursion_depth = depth + 1;
13831388
retval = fn(bprm);
1384-
/*
1385-
* Restore the depth counter to its starting value
1386-
* in this call, so we don't have to rely on every
1387-
* load_binary function to restore it on return.
1388-
*/
13891389
bprm->recursion_depth = depth;
13901390
if (retval >= 0) {
13911391
if (depth == 0) {

include/linux/binfmts.h

-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ struct linux_binprm {
5454
#define BINPRM_FLAGS_EXECFD_BIT 1
5555
#define BINPRM_FLAGS_EXECFD (1 << BINPRM_FLAGS_EXECFD_BIT)
5656

57-
#define BINPRM_MAX_RECURSION 4
58-
5957
/* Function parameter for binfmt->coredump */
6058
struct coredump_params {
6159
siginfo_t *siginfo;

0 commit comments

Comments
 (0)