Skip to contentSkip to conversation

exec: more cleanups

PATCH In review Oleg Nesterov 2 participants 9 messages Aug 2, 2013 – Aug 4, 2013

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

cover exec: more cleanups

On top of "[PATCH 0/3] exec: minor cleanups + minor fix" I sent yesterday.

Perhaps too many patches for the poor search_binary_handler(), but I do not know how to document the changes if I join them.

Oleg.

 fs/exec.c |   82 ++++++++++++++++++++++++++----------------------------------
 1 files changed, 36 insertions(+), 46 deletions(-)
#1 lore raw

PATCH 1/5 exec: move allow_write_access/fput to exec_binprm()

When search_binary_handler() succeeds it does allow_write_access() and fput(), then it clears bprm->file to ensure the caller will not do the same.

We can simply move this code to exec_binprm() which is called only once. In fact we could move this to free_bprm() and remove the same code in do_execve_common's error path.

---
 fs/exec.c |    9 +++++----
 1 files changed, 5 insertions(+), 4 deletions(-)
fs/exec.c +5 -4
index ad7d624..ef70320 100644
@@ -1400,10 +1400,6 @@ int search_binary_handler(struct linux_binprm *bprm)
 			bprm->recursion_depth--;
 			if (retval >= 0) {
 				put_binfmt(fmt);
-				allow_write_access(bprm->file);
-				if (bprm->file)
-					fput(bprm->file);
-				bprm->file = NULL;
 				return retval;
 			}
 			read_lock(&binfmt_lock);
@@ -1455,6 +1451,11 @@ static int exec_binprm(struct linux_binprm *bprm)
 		ptrace_event(PTRACE_EVENT_EXEC, old_vpid);
 		current->did_exec = 1;
 		proc_exec_connector(current);
+
+		if (bprm->file) {
+			allow_write_access(bprm->file);
+			fput(bprm->file);
+		}
 	}
 
 	return ret;
Signature · 1 line
1.5.5.1
#2 lore raw

Re: [PATCH 1/5] exec: move allow_write_access/fput to exec_binprm()

Quoted 29 lines · Oleg Nesterov

When search_binary_handler() succeeds it does allow_write_access() and fput(), then it clears bprm->file to ensure the caller will not do the same.

We can simply move this code to exec_binprm() which is called only once. In fact we could move this to free_bprm() and remove the same code in do_execve_common's error path.

---
 fs/exec.c |    9 +++++----
 1 files changed, 5 insertions(+), 4 deletions(-)
fs/exec.c +0 -4
index ad7d624..ef70320 100644
@@ -1400,10 +1400,6 @@ int search_binary_handler(struct linux_binprm *bprm)
                        bprm->recursion_depth--;
                        if (retval >= 0) {
                                put_binfmt(fmt);
-                               allow_write_access(bprm->file);
-                               if (bprm->file)
-                                       fput(bprm->file);
-                               bprm->file = NULL;
                                return retval;
                        }
                        read_lock(&binfmt_lock);

fs/exec.c static int exec_binprm(struct linux_binprm *bprm) · line 1458

Quoted 8 lines
fs/exec.c +5 -0
@@ -1455,6 +1451,11 @@ static int exec_binprm(struct linux_binprm *bprm)
                ptrace_event(PTRACE_EVENT_EXEC, old_vpid);
                current->did_exec = 1;
                proc_exec_connector(current);
+
+               if (bprm->file) {
+                       allow_write_access(bprm->file);
+                       fput(bprm->file);
+               }

Why not keep the bprm->file = NULL assignment? Seems reasonable to keep that just to be avoid use-after-free accidents.

-Kees

Quoted 6 lines
fs/exec.c +0 -1
        }
 
        return ret;
--

1.5.5.1

Signature · 2 lines
Kees Cook
Chrome OS Security
#3 lore raw

Re: [PATCH 1/5] exec: move allow_write_access/fput to exec_binprm()

Quoted 13 lines · Kees Cook

fs/exec.c static int exec_binprm(struct linux_binprm *bprm) · line 1458

Quoted 9 lines · Oleg Nesterov
fs/exec.c +5 -0
@@ -1455,6 +1451,11 @@ static int exec_binprm(struct linux_binprm *bprm)
                ptrace_event(PTRACE_EVENT_EXEC, old_vpid);
                current->did_exec = 1;
                proc_exec_connector(current);
+
+               if (bprm->file) {
+                       allow_write_access(bprm->file);
+                       fput(bprm->file);
+               }

Why not keep the bprm->file = NULL assignment?

Because it is no longer needed.

And now that we have the non-recursive exec_binprm() called right before free_bprm() it is obvious that it won't be used again.

Quoted 2 lines

Seems reasonable to
keep that just to be avoid use-after-free accidents.

OK. I will add it back. With the comment to explain that this is only to catch the possible problems.

I guess it would be better if I resend the whole series to avoid the confusion. I am going to add your acks. It seems that you acked everything except 1/3 in the previous series, perhaps you can ack it too?

Thanks for review!

Oleg.

#4 lore raw

PATCH 2/5 exec: kill ->load_binary != NULL check in search_binary_handler()

In reply to Oleg Nesterov

search_binary_handler() checks ->load_binary != NULL for no reason, this method should be always defined. Turn this check into WARN_ON() and move it into __register_binfmt().

Also, kill the function pointer. The current code looks confusing, as if ->load_binary can go away after read_unlock(&binfmt_lock). But we rely on module_get(fmt->module), this fmt can't be changed or unregistered, otherwise this code is buggy anyway.

---
 fs/exec.c |    7 +++----
 1 files changed, 3 insertions(+), 4 deletions(-)
fs/exec.c +3 -4
index ef70320..9f41e7d 100644
@@ -74,6 +74,8 @@ static DEFINE_RWLOCK(binfmt_lock);
 void __register_binfmt(struct linux_binfmt * fmt, int insert)
 {
 	BUG_ON(!fmt);
+	if (WARN_ON(!fmt->load_binary))
+		return;
 	write_lock(&binfmt_lock);
 	insert ? list_add(&fmt->lh, &formats) :
 		 list_add_tail(&fmt->lh, &formats);
@@ -1389,14 +1391,11 @@ int search_binary_handler(struct linux_binprm *bprm)
 	for (try=0; try<2; try++) {
 		read_lock(&binfmt_lock);
 		list_for_each_entry(fmt, &formats, lh) {
-			int (*fn)(struct linux_binprm *) = fmt->load_binary;
-			if (!fn)
-				continue;
 			if (!try_module_get(fmt->module))
 				continue;
 			read_unlock(&binfmt_lock);
 			bprm->recursion_depth++;
-			retval = fn(bprm);
+			retval = fmt->load_binary(bprm);
 			bprm->recursion_depth--;
 			if (retval >= 0) {
 				put_binfmt(fmt);
Signature · 1 line
1.5.5.1
#5 lore raw

PATCH 3/5 exec: cleanup the CONFIG_MODULES logic

In reply to Oleg Nesterov

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().

---
 fs/exec.c |   68 +++++++++++++++++++++++++++---------------------------------
 1 files changed, 31 insertions(+), 37 deletions(-)
fs/exec.c +31 -37
index 9f41e7d..48344a2 100644
@@ -1367,13 +1367,15 @@ out:
 }
 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
  */
 int search_binary_handler(struct linux_binprm *bprm)
 {
-	int try, retval;
+	bool need_retry = IS_ENABLED(CONFIG_MODULES);
 	struct linux_binfmt *fmt;
+	int retval;
 
 	/* This allows 4 levels of binfmt rewrites before failing hard. */
 	if (bprm->recursion_depth > 5)
@@ -1388,47 +1390,39 @@ int search_binary_handler(struct linux_binprm *bprm)
 		return retval;
 
 	retval = -ENOENT;
-	for (try=0; try<2; try++) {
-		read_lock(&binfmt_lock);
-		list_for_each_entry(fmt, &formats, lh) {
-			if (!try_module_get(fmt->module))
-				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);
+ retry:
+	read_lock(&binfmt_lock);
+	list_for_each_entry(fmt, &formats, lh) {
+		if (!try_module_get(fmt->module))
+			continue;
+		read_unlock(&binfmt_lock);
+		bprm->recursion_depth++;
+		retval = fmt->load_binary(bprm);
+		bprm->recursion_depth--;
+		if (retval >= 0) {
 			put_binfmt(fmt);
-			if (retval != -ENOEXEC || bprm->mm == NULL)
-				break;
-			if (!bprm->file) {
-				read_unlock(&binfmt_lock);
-				return retval;
-			}
+			return retval;
 		}
-		read_unlock(&binfmt_lock);
-#ifdef CONFIG_MODULES
-		if (retval != -ENOEXEC || bprm->mm == NULL) {
+		read_lock(&binfmt_lock);
+		put_binfmt(fmt);
+		if (retval != -ENOEXEC || bprm->mm == NULL)
 			break;
-		} else {
-#define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
-			if (printable(bprm->buf[0]) &&
-			    printable(bprm->buf[1]) &&
-			    printable(bprm->buf[2]) &&
-			    printable(bprm->buf[3]))
-				break; /* -ENOEXEC */
-			if (try)
-				break; /* -ENOEXEC */
-			request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
+		if (!bprm->file) {
+			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;
 }
 EXPORT_SYMBOL(search_binary_handler);
Signature · 1 line
1.5.5.1
#6 lore raw

PATCH 4/5 exec: don't retry if request_module() fails

In reply to Oleg Nesterov

A separate one-liner for better documentation.

It doesn't make sense to retry if request_module() fails to exec /sbin/modprobe, add the addition "request_module() < 0" check.

However, this logic still doesn't look exactly right:

1. It would be better to check "request_module() != 0", the user space modprobe process should report the correct exit code. But I didn't dare to add the user-visible change.

2. The whole ENOEXEC logic looks suboptimal. Suppose that we try to exec a "#!path-to-unsupported-binary" script. In this case request_module() + "retry" will be done twice: first by the "depth == 1" code, and then again by the "depth == 0" caller which doesn't make sense.

3. And note that in the case above bprm->buf was already changed by load_script()->prepare_binprm(), so this looks even more ugly.

---
 fs/exec.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
fs/exec.c +2 -1
index 48344a2..d9fd32c 100644
@@ -1418,7 +1418,8 @@ int search_binary_handler(struct linux_binprm *bprm)
 		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));
+		if (request_module("binfmt-%04x", *(ushort *)(bprm->buf + 2)) < 0)
+			return retval;
 		need_retry = false;
 		goto retry;
 	}
Signature · 1 line
1.5.5.1
#7 lore raw

PATCH 5/5 exec: cleanup the error handling in search_binary_handler()

In reply to Oleg Nesterov

The error hanling and ret-from-loop look confusing and inconsistent.

- "retval >= 0" simply returns

- "!bprm->file" returns too but with read_unlock() because binfmt_lock was already re-acquired

- "retval != -ENOEXEC || bprm->mm == NULL" does "break" and relies on the same check after the main loop

Consolidate these checks into a single if/return statement.

need_retry still checks "retval == -ENOEXEC", but this and -ENOENT before the main loop are not needed. This is only for pathological and impossible list_empty(&formats) case.

It is not clear why do we check "bprm->mm == NULL", probably this should be removed.

---
 fs/exec.c |   11 +++--------
 1 files changed, 3 insertions(+), 8 deletions(-)
fs/exec.c +3 -8
index d9fd32c..7ab2120 100644
@@ -1399,22 +1399,17 @@ int search_binary_handler(struct linux_binprm *bprm)
 		bprm->recursion_depth++;
 		retval = fmt->load_binary(bprm);
 		bprm->recursion_depth--;
-		if (retval >= 0) {
+		if (retval >= 0 || retval != -ENOEXEC ||
+		    bprm->mm == NULL || bprm->file == NULL) {
 			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);
 
-	if (need_retry && retval == -ENOEXEC && bprm->mm) {
+	if (need_retry && retval == -ENOEXEC) {
 		if (printable(bprm->buf[0]) && printable(bprm->buf[1]) &&
 		    printable(bprm->buf[2]) && printable(bprm->buf[3]))
 			return retval;
Signature · 1 line
1.5.5.1
Acked-by on the v1 series
#8 lore raw
In reply to Oleg Nesterov
Quoted 11 lines · Oleg Nesterov

On top of "[PATCH 0/3] exec: minor cleanups + minor fix" I sent yesterday.

Perhaps too many patches for the poor search_binary_handler(), but I do not know how to document the changes if I join them.

Oleg.

 fs/exec.c |   82 ++++++++++++++++++++++++++----------------------------------
 1 files changed, 36 insertions(+), 46 deletions(-)

This all looks really good. Thanks for the cleanups! Besides the one comment on patch 1, consider the series:

Thanks,

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security