mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Geoffrey Thomas <geofft@MIT.EDU>
To: linux-kernel@vger.kernel.org
Cc: Geoffrey Thomas <geofft@MIT.EDU>
Subject: [PATCH] Return ENOEXEC, not ENOENT, if a binary's or script's interpreter doesn't exist.
Date: Thu,  9 Jul 2009 03:48:20 -0400	[thread overview]
Message-ID: <1247125700-12630-1-git-send-email-geofft@mit.edu> (raw)

If you try to execute a binary compiled for ld-linux.so.1 (libc5) on a machine
with only ld-linux.so.2 (libc6), your shell will claim "mybinary: No such file
or directory", even though the binary exists. The ENOENT actually applies to
the ELF intepreter, not to the file itself. The same happens if you have a
nonexistent interpreter in a shell script's shebang line.

Give a more helpful and more expected error, "cannot execute binary file", in
these cases.

Signed-off-by: Geoffrey Thomas <geofft@mit.edu>
Tested-by: Anders Kaseorg <andersk@mit.edu>
---
 fs/binfmt_elf.c       |    5 ++++-
 fs/binfmt_elf_fdpic.c |    2 ++
 fs/binfmt_em86.c      |    8 ++++++--
 fs/binfmt_misc.c      |    5 ++++-
 fs/binfmt_script.c    |    8 ++++++--
 5 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index b7c1603..56954e4 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -685,8 +685,11 @@ static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
 
 			interpreter = open_exec(elf_interpreter);
 			retval = PTR_ERR(interpreter);
-			if (IS_ERR(interpreter))
+			if (IS_ERR(interpreter)) {
+				if (retval == -ENOENT)
+					retval = -ENOEXEC;
 				goto out_free_interp;
+			}
 
 			/*
 			 * If the binary is not readable then enforce
diff --git a/fs/binfmt_elf_fdpic.c b/fs/binfmt_elf_fdpic.c
index 20fbece..604d117 100644
--- a/fs/binfmt_elf_fdpic.c
+++ b/fs/binfmt_elf_fdpic.c
@@ -232,6 +232,8 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm,
 			interpreter = open_exec(interpreter_name);
 			retval = PTR_ERR(interpreter);
 			if (IS_ERR(interpreter)) {
+				if (retval == -ENOENT)
+					retval = -ENOEXEC;
 				interpreter = NULL;
 				goto error;
 			}
diff --git a/fs/binfmt_em86.c b/fs/binfmt_em86.c
index 32fb00b..587e0b9 100644
--- a/fs/binfmt_em86.c
+++ b/fs/binfmt_em86.c
@@ -82,8 +82,12 @@ static int load_em86(struct linux_binprm *bprm,struct pt_regs *regs)
 	 * space, and we don't need to copy it.
 	 */
 	file = open_exec(interp);
-	if (IS_ERR(file))
-		return PTR_ERR(file);
+	if (IS_ERR(file)) {
+		if (PTR_ERR(file) == -ENOENT)
+			return -ENOEXEC;
+		else
+			return PTR_ERR(file);
+	}
 
 	bprm->file = file;
 
diff --git a/fs/binfmt_misc.c b/fs/binfmt_misc.c
index c4e8353..eecf0db 100644
--- a/fs/binfmt_misc.c
+++ b/fs/binfmt_misc.c
@@ -180,8 +180,11 @@ static int load_misc_binary(struct linux_binprm *bprm, struct pt_regs *regs)
 
 	interp_file = open_exec (iname);
 	retval = PTR_ERR (interp_file);
-	if (IS_ERR (interp_file))
+	if (IS_ERR (interp_file)) {
+		if (retval == -ENOENT)
+			retval = -ENOEXEC;
 		goto _error;
+	}
 
 	bprm->file = interp_file;
 	if (fmt->flags & MISC_FMT_CREDENTIALS) {
diff --git a/fs/binfmt_script.c b/fs/binfmt_script.c
index 0834350..738446f 100644
--- a/fs/binfmt_script.c
+++ b/fs/binfmt_script.c
@@ -88,8 +88,12 @@ static int load_script(struct linux_binprm *bprm,struct pt_regs *regs)
 	 * OK, now restart the process with the interpreter's dentry.
 	 */
 	file = open_exec(interp);
-	if (IS_ERR(file))
-		return PTR_ERR(file);
+	if (IS_ERR(file)) {
+		if (PTR_ERR(file) == -ENOENT)
+			return -ENOEXEC;
+		else
+			return PTR_ERR(file);
+	}
 
 	bprm->file = file;
 	retval = prepare_binprm(bprm);
-- 
1.5.6.5


             reply	other threads:[~2009-07-09  7:48 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-09  7:48 Geoffrey Thomas [this message]
2009-07-10  5:18 ` Amerigo Wang
2009-07-10 16:17 ` Pavel Machek
2009-07-30 17:08 Jonathan Reed
2009-07-30 20:12 ` Alan Cox
2009-07-30 20:17 ` Pavel Machek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1247125700-12630-1-git-send-email-geofft@mit.edu \
    --to=geofft@mit.edu \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®