3fb13f
diff --git a/config.h.in b/config.h.in
3fb13f
index a5ad9e7..62a6b32 100644
3fb13f
--- a/config.h.in
3fb13f
+++ b/config.h.in
3fb13f
@@ -748,6 +748,9 @@
3fb13f
 /* Define if you have the pselect function.  */
3fb13f
 #undef HAVE_PSELECT
3fb13f
 
3fb13f
+/* Define if you have the pread function. */
3fb13f
+#undef HAVE_PREAD
3fb13f
+
3fb13f
 /* Define if you have the putenv function.  */
3fb13f
 #undef HAVE_PUTENV
3fb13f
 
3fb13f
@@ -946,6 +949,9 @@
3fb13f
 /* Define if you have the <dlfcn.h> header file.  */
3fb13f
 #undef HAVE_DLFCN_H
3fb13f
 
3fb13f
+/* Define if you have the <elf.h> header file.  */
3fb13f
+#undef HAVE_ELF_H
3fb13f
+
3fb13f
 /* Define if you have the <grp.h> header file.  */
3fb13f
 #undef HAVE_GRP_H
3fb13f
 
3fb13f
diff --git a/configure.ac b/configure.ac
3fb13f
index ce4e9b6..eda95d6 100644
3fb13f
--- a/configure.ac
3fb13f
+++ b/configure.ac
3fb13f
@@ -700,7 +700,7 @@ BASH_HEADER_INTTYPES
3fb13f
 AC_CHECK_HEADERS(unistd.h stdlib.h stdarg.h varargs.h limits.h string.h \
3fb13f
 		 memory.h locale.h termcap.h termio.h termios.h dlfcn.h \
3fb13f
 		 stdbool.h stddef.h stdint.h netdb.h pwd.h grp.h strings.h \
3fb13f
-		 regex.h syslog.h ulimit.h)
3fb13f
+		 regex.h syslog.h ulimit.h elf.h)
3fb13f
 AC_CHECK_HEADERS(sys/pte.h sys/stream.h sys/select.h sys/file.h sys/ioctl.h \
3fb13f
 		 sys/param.h sys/socket.h sys/stat.h \
3fb13f
 		 sys/time.h sys/times.h sys/types.h sys/wait.h)
3fb13f
@@ -771,7 +771,7 @@ dnl checks for system calls
3fb13f
 AC_CHECK_FUNCS(dup2 eaccess fcntl getdtablesize getgroups gethostname \
3fb13f
 		getpagesize getpeername getrlimit getrusage gettimeofday \
3fb13f
 		kill killpg lstat pselect readlink sbrk select setdtablesize \
3fb13f
-		setitimer tcgetpgrp uname ulimit waitpid)
3fb13f
+		setitimer tcgetpgrp uname ulimit waitpid pread)
3fb13f
 AC_REPLACE_FUNCS(rename)
3fb13f
 
3fb13f
 dnl checks for c library functions
3fb13f
diff --git a/execute_cmd.c b/execute_cmd.c
3fb13f
index 2a3df6d..b5cd405 100644
3fb13f
--- a/execute_cmd.c
3fb13f
+++ b/execute_cmd.c
3fb13f
@@ -41,6 +41,10 @@
3fb13f
 #  include <unistd.h>
3fb13f
 #endif
3fb13f
 
3fb13f
+#ifdef HAVE_ELF_H
3fb13f
+# include <elf.h>
3fb13f
+#endif
3fb13f
+
3fb13f
 #include "posixtime.h"
3fb13f
 
3fb13f
 #if defined (HAVE_SYS_RESOURCE_H) && !defined (RLIMTYPE)
3fb13f
@@ -5486,6 +5490,14 @@ shell_execve (command, args, env)
3fb13f
 	{
3fb13f
 	  /* The file has the execute bits set, but the kernel refuses to
3fb13f
 	     run it for some reason.  See why. */
3fb13f
+#if defined (HAVE_HASH_BANG_EXEC) || defined (HAVE_ELF_H)
3fb13f
+       int fd = open (command, O_RDONLY);
3fb13f
+
3fb13f
+       if (fd >= 0)
3fb13f
+               sample_len = read (fd, sample, sizeof (sample));
3fb13f
+       else
3fb13f
+               sample_len = -1;
3fb13f
+#endif
3fb13f
 #if defined (HAVE_HASH_BANG_EXEC)
3fb13f
 	  READ_SAMPLE_BUF (command, sample, sample_len);
3fb13f
 	  if (sample_len > 0)
3fb13f
@@ -5495,6 +5507,7 @@ shell_execve (command, args, env)
3fb13f
 	      char *interp;
3fb13f
 	      int ilen;
3fb13f
 
3fb13f
+              close (fd);
3fb13f
 	      interp = getinterp (sample, sample_len, (int *)NULL);
3fb13f
 	      ilen = strlen (interp);
3fb13f
 	      errno = i;
3fb13f
@@ -5510,6 +5523,136 @@ shell_execve (command, args, env)
3fb13f
 	      return (EX_NOEXEC);
3fb13f
 	    }
3fb13f
 #endif
3fb13f
+#if defined (HAVE_ELF_H)
3fb13f
+	  if (i == ENOENT
3fb13f
+	      && sample_len > EI_NIDENT
3fb13f
+	      && memcmp (sample, ELFMAG, SELFMAG) == 0)
3fb13f
+	    {
3fb13f
+	      off_t offset = -1;
3fb13f
+
3fb13f
+	      /* It is an ELF file.  Now determine whether it is dynamically
3fb13f
+		 linked and if yes, get the offset of the interpreter
3fb13f
+		 string.  */
3fb13f
+	      if (sample[EI_CLASS] == ELFCLASS32
3fb13f
+		  && sample_len > sizeof (Elf32_Ehdr))
3fb13f
+		{
3fb13f
+		  Elf32_Ehdr ehdr;
3fb13f
+		  Elf32_Phdr *phdr;
3fb13f
+		  int nphdr;
3fb13f
+
3fb13f
+		  /* We have to copy the data since the sample buffer
3fb13f
+		     might not be aligned correctly to be accessed as
3fb13f
+		     an Elf32_Ehdr struct.  */
3fb13f
+		  memcpy (&ehdr, sample, sizeof (Elf32_Ehdr));
3fb13f
+
3fb13f
+		  nphdr = ehdr.e_phnum;
3fb13f
+		  phdr = (Elf32_Phdr *) malloc (nphdr * ehdr.e_phentsize);
3fb13f
+		  if (phdr != NULL)
3fb13f
+		    {
3fb13f
+#ifdef HAVE_PREAD
3fb13f
+		      sample_len = pread (fd, phdr, nphdr * ehdr.e_phentsize,
3fb13f
+					  ehdr.e_phoff);
3fb13f
+#else
3fb13f
+		      if (lseek (fd, ehdr.e_phoff, SEEK_SET) != -1)
3fb13f
+			sample_len = read (fd, phdr,
3fb13f
+					   nphdr * ehdr.e_phentsize);
3fb13f
+		      else
3fb13f
+			sample_len = -1;
3fb13f
+#endif
3fb13f
+		      if (sample_len == nphdr * ehdr.e_phentsize)
3fb13f
+			while (nphdr-- > 0)
3fb13f
+			  if (phdr[nphdr].p_type == PT_INTERP)
3fb13f
+			    {
3fb13f
+			      offset = phdr[nphdr].p_offset;
3fb13f
+			      break;
3fb13f
+			    }
3fb13f
+		      free (phdr);
3fb13f
+		    }
3fb13f
+		}
3fb13f
+	      else if (sample[EI_CLASS] == ELFCLASS64
3fb13f
+		       && sample_len > sizeof (Elf64_Ehdr))
3fb13f
+		{
3fb13f
+		  Elf64_Ehdr ehdr;
3fb13f
+		  Elf64_Phdr *phdr;
3fb13f
+		  int nphdr;
3fb13f
+
3fb13f
+		  /* We have to copy the data since the sample buffer
3fb13f
+		     might not be aligned correctly to be accessed as
3fb13f
+		     an Elf64_Ehdr struct.  */
3fb13f
+		  memcpy (&ehdr, sample, sizeof (Elf64_Ehdr));
3fb13f
+
3fb13f
+		  nphdr = ehdr.e_phnum;
3fb13f
+		  phdr = (Elf64_Phdr *) malloc (nphdr * ehdr.e_phentsize);
3fb13f
+		  if (phdr != NULL)
3fb13f
+		    {
3fb13f
+#ifdef HAVE_PREAD
3fb13f
+		      sample_len = pread (fd, phdr, nphdr * ehdr.e_phentsize,
3fb13f
+					  ehdr.e_phoff);
3fb13f
+#else
3fb13f
+		      if (lseek (fd, ehdr.e_phoff, SEEK_SET) != -1)
3fb13f
+			sample_len = read (fd, phdr,
3fb13f
+					   nphdr * ehdr.e_phentsize);
3fb13f
+		      else
3fb13f
+			sample_len = -1;
3fb13f
+#endif
3fb13f
+		      if (sample_len == nphdr * ehdr.e_phentsize)
3fb13f
+			while (nphdr-- > 0)
3fb13f
+			  if (phdr[nphdr].p_type == PT_INTERP)
3fb13f
+			    {
3fb13f
+			      offset = phdr[nphdr].p_offset;
3fb13f
+			      break;
3fb13f
+			    }
3fb13f
+		      free (phdr);
3fb13f
+		    }
3fb13f
+		}
3fb13f
+
3fb13f
+	      if (offset != -1)
3fb13f
+		{
3fb13f
+		  size_t maxlen = 0;
3fb13f
+		  size_t actlen = 0;
3fb13f
+		  char *interp = NULL;
3fb13f
+
3fb13f
+		  do
3fb13f
+		    {
3fb13f
+		      if (actlen == maxlen)
3fb13f
+			{
3fb13f
+			  char *newinterp = realloc (interp, maxlen += 200);
3fb13f
+			  if (newinterp == NULL)
3fb13f
+			    {
3fb13f
+			      actlen = 0;
3fb13f
+			      break;
3fb13f
+			    }
3fb13f
+			  interp = newinterp;
3fb13f
+
3fb13f
+#ifdef HAVE_PREAD
3fb13f
+			  actlen = pread (fd, interp, maxlen, offset);
3fb13f
+#else
3fb13f
+			  if (lseek (fd, offset, SEEK_SET) != -1)
3fb13f
+			    actlen = read (fd, interp, maxlen);
3fb13f
+			  else
3fb13f
+			    actlen = -1;
3fb13f
+#endif
3fb13f
+			}
3fb13f
+		    }
3fb13f
+		  while (actlen > 0 && memchr (interp, '\0', actlen) == NULL);
3fb13f
+
3fb13f
+		  if (actlen > 0)
3fb13f
+		    {
3fb13f
+		      close (fd);
3fb13f
+		      errno = i;
3fb13f
+		      sys_error ("%s: %s: bad ELF interpreter", command,
3fb13f
+				 interp);
3fb13f
+		      free (interp);
3fb13f
+		      return (EX_NOEXEC);
3fb13f
+		    }
3fb13f
+
3fb13f
+		  free (interp);
3fb13f
+		}
3fb13f
+	    }
3fb13f
+#endif
3fb13f
+#if defined (HAVE_HASH_BANG_EXEC) || defined (HAVE_ELF_H)
3fb13f
+	  close (fd);
3fb13f
+#endif
3fb13f
 	  errno = i;
3fb13f
 	  file_error (command);
3fb13f
 	}
3fb13f
-- 
3fb13f
2.9.3
3fb13f