Blame SOURCES/bash-2.05a-interpreter.patch

bb9a7e
diff --git a/config.h.in b/config.h.in
bb9a7e
index ab316d4..11d1d68 100644
bb9a7e
--- a/config.h.in
bb9a7e
+++ b/config.h.in
bb9a7e
@@ -775,6 +775,9 @@
bb9a7e
 /* Define if you have the pselect function.  */
bb9a7e
 #undef HAVE_PSELECT
bb9a7e
 
bb9a7e
+/* Define if you have the pread function. */
bb9a7e
+#undef HAVE_PREAD
bb9a7e
+
bb9a7e
 /* Define if you have the putenv function.  */
bb9a7e
 #undef HAVE_PUTENV
bb9a7e
 
bb9a7e
@@ -981,6 +984,9 @@
bb9a7e
 /* Define if you have the <dlfcn.h> header file.  */
bb9a7e
 #undef HAVE_DLFCN_H
bb9a7e
 
bb9a7e
+/* Define if you have the <elf.h> header file.  */
bb9a7e
+#undef HAVE_ELF_H
bb9a7e
+
bb9a7e
 /* Define if you have the <grp.h> header file.  */
bb9a7e
 #undef HAVE_GRP_H
bb9a7e
 
bb9a7e
diff --git a/configure.ac b/configure.ac
bb9a7e
index 2fe3e7d..f1b7f1b 100644
bb9a7e
--- a/configure.ac
bb9a7e
+++ b/configure.ac
bb9a7e
@@ -827,7 +827,7 @@ dnl checks for system calls
bb9a7e
 AC_CHECK_FUNCS(dup2 eaccess fcntl getdtablesize getentropy getgroups \
bb9a7e
 		gethostname getpagesize getpeername getrandom getrlimit \
bb9a7e
 		getrusage gettimeofday kill killpg lstat pselect readlink \
bb9a7e
-		select setdtablesize setitimer tcgetpgrp uname ulimit waitpid)
bb9a7e
+		select setdtablesize setitimer tcgetpgrp uname ulimit waitpid pread)
bb9a7e
 AC_REPLACE_FUNCS(rename)
bb9a7e
 
bb9a7e
 dnl checks for c library functions
bb9a7e
diff --git a/execute_cmd.c b/execute_cmd.c
bb9a7e
index d2a0dd7..d2555ad 100644
bb9a7e
--- a/execute_cmd.c
bb9a7e
+++ b/execute_cmd.c
bb9a7e
@@ -41,6 +41,10 @@
bb9a7e
 #  include <unistd.h>
bb9a7e
 #endif
bb9a7e
 
bb9a7e
+#ifdef HAVE_ELF_H
bb9a7e
+# include <elf.h>
bb9a7e
+#endif
bb9a7e
+
bb9a7e
 #include "posixtime.h"
bb9a7e
 
bb9a7e
 #if defined (HAVE_SYS_RESOURCE_H) && !defined (RLIMTYPE)
bb9a7e
@@ -5832,6 +5836,14 @@ shell_execve (command, args, env)
bb9a7e
 	{
bb9a7e
 	  /* The file has the execute bits set, but the kernel refuses to
bb9a7e
 	     run it for some reason.  See why. */
bb9a7e
+#if defined (HAVE_HASH_BANG_EXEC) || defined (HAVE_ELF_H)
bb9a7e
+       int fd = open (command, O_RDONLY);
bb9a7e
+
bb9a7e
+       if (fd >= 0)
bb9a7e
+               sample_len = read (fd, sample, sizeof (sample));
bb9a7e
+       else
bb9a7e
+               sample_len = -1;
bb9a7e
+#endif
bb9a7e
 #if defined (HAVE_HASH_BANG_EXEC)
bb9a7e
 	  READ_SAMPLE_BUF (command, sample, sample_len);
bb9a7e
 	  if (sample_len > 0)
bb9a7e
@@ -5841,6 +5853,7 @@ shell_execve (command, args, env)
bb9a7e
 	      char *interp;
bb9a7e
 	      int ilen;
bb9a7e
 
bb9a7e
+          close (fd);
bb9a7e
 	      interp = getinterp (sample, sample_len, (int *)NULL);
bb9a7e
 	      ilen = strlen (interp);
bb9a7e
 	      errno = i;
bb9a7e
@@ -5856,7 +5869,138 @@ shell_execve (command, args, env)
bb9a7e
 	      return (EX_NOEXEC);
bb9a7e
 	    }
bb9a7e
 #endif
bb9a7e
-	  errno = i;
bb9a7e
+#if defined (HAVE_ELF_H)
bb9a7e
+	  if (i == ENOENT
bb9a7e
+	      && sample_len > EI_NIDENT
bb9a7e
+	      && memcmp (sample, ELFMAG, SELFMAG) == 0)
bb9a7e
+	    {
bb9a7e
+	      off_t offset = -1;
bb9a7e
+
bb9a7e
+	      /* It is an ELF file.  Now determine whether it is dynamically
bb9a7e
+		 linked and if yes, get the offset of the interpreter
bb9a7e
+		 string.  */
bb9a7e
+	      if (sample[EI_CLASS] == ELFCLASS32
bb9a7e
+		  && sample_len > sizeof (Elf32_Ehdr))
bb9a7e
+		{
bb9a7e
+		  Elf32_Ehdr ehdr;
bb9a7e
+		  Elf32_Phdr *phdr;
bb9a7e
+		  int nphdr;
bb9a7e
+
bb9a7e
+		  /* We have to copy the data since the sample buffer
bb9a7e
+		     might not be aligned correctly to be accessed as
bb9a7e
+		     an Elf32_Ehdr struct.  */
bb9a7e
+		  memcpy (&ehdr, sample, sizeof (Elf32_Ehdr));
bb9a7e
+
bb9a7e
+		  nphdr = ehdr.e_phnum;
bb9a7e
+		  phdr = (Elf32_Phdr *) malloc (nphdr * ehdr.e_phentsize);
bb9a7e
+		  if (phdr != NULL)
bb9a7e
+		    {
bb9a7e
+#ifdef HAVE_PREAD
bb9a7e
+		      sample_len = pread (fd, phdr, nphdr * ehdr.e_phentsize,
bb9a7e
+					  ehdr.e_phoff);
bb9a7e
+#else
bb9a7e
+		      if (lseek (fd, ehdr.e_phoff, SEEK_SET) != -1)
bb9a7e
+			sample_len = read (fd, phdr,
bb9a7e
+					   nphdr * ehdr.e_phentsize);
bb9a7e
+		      else
bb9a7e
+			sample_len = -1;
bb9a7e
+#endif
bb9a7e
+		      if (sample_len == nphdr * ehdr.e_phentsize)
bb9a7e
+			while (nphdr-- > 0)
bb9a7e
+			  if (phdr[nphdr].p_type == PT_INTERP)
bb9a7e
+			    {
bb9a7e
+			      offset = phdr[nphdr].p_offset;
bb9a7e
+			      break;
bb9a7e
+			    }
bb9a7e
+		      free (phdr);
bb9a7e
+		    }
bb9a7e
+		}
bb9a7e
+	      else if (sample[EI_CLASS] == ELFCLASS64
bb9a7e
+		       && sample_len > sizeof (Elf64_Ehdr))
bb9a7e
+		{
bb9a7e
+		  Elf64_Ehdr ehdr;
bb9a7e
+		  Elf64_Phdr *phdr;
bb9a7e
+		  int nphdr;
bb9a7e
+
bb9a7e
+		  /* We have to copy the data since the sample buffer
bb9a7e
+		     might not be aligned correctly to be accessed as
bb9a7e
+		     an Elf64_Ehdr struct.  */
bb9a7e
+		  memcpy (&ehdr, sample, sizeof (Elf64_Ehdr));
bb9a7e
+
bb9a7e
+		  nphdr = ehdr.e_phnum;
bb9a7e
+		  phdr = (Elf64_Phdr *) malloc (nphdr * ehdr.e_phentsize);
bb9a7e
+		  if (phdr != NULL)
bb9a7e
+		    {
bb9a7e
+#ifdef HAVE_PREAD
bb9a7e
+		      sample_len = pread (fd, phdr, nphdr * ehdr.e_phentsize,
bb9a7e
+					  ehdr.e_phoff);
bb9a7e
+#else
bb9a7e
+		      if (lseek (fd, ehdr.e_phoff, SEEK_SET) != -1)
bb9a7e
+			sample_len = read (fd, phdr,
bb9a7e
+					   nphdr * ehdr.e_phentsize);
bb9a7e
+		      else
bb9a7e
+			sample_len = -1;
bb9a7e
+#endif
bb9a7e
+		      if (sample_len == nphdr * ehdr.e_phentsize)
bb9a7e
+			while (nphdr-- > 0)
bb9a7e
+			  if (phdr[nphdr].p_type == PT_INTERP)
bb9a7e
+			    {
bb9a7e
+			      offset = phdr[nphdr].p_offset;
bb9a7e
+			      break;
bb9a7e
+			    }
bb9a7e
+		      free (phdr);
bb9a7e
+		    }
bb9a7e
+		}
bb9a7e
+
bb9a7e
+	      if (offset != -1)
bb9a7e
+		{
bb9a7e
+		  size_t maxlen = 0;
bb9a7e
+		  size_t actlen = 0;
bb9a7e
+		  char *interp = NULL;
bb9a7e
+
bb9a7e
+		  do
bb9a7e
+		    {
bb9a7e
+		      if (actlen == maxlen)
bb9a7e
+			{
bb9a7e
+			  char *newinterp = realloc (interp, maxlen += 200);
bb9a7e
+			  if (newinterp == NULL)
bb9a7e
+			    {
bb9a7e
+			      actlen = 0;
bb9a7e
+			      break;
bb9a7e
+			    }
bb9a7e
+			  interp = newinterp;
bb9a7e
+
bb9a7e
+#ifdef HAVE_PREAD
bb9a7e
+			  actlen = pread (fd, interp, maxlen, offset);
bb9a7e
+#else
bb9a7e
+			  if (lseek (fd, offset, SEEK_SET) != -1)
bb9a7e
+			    actlen = read (fd, interp, maxlen);
bb9a7e
+			  else
bb9a7e
+			    actlen = -1;
bb9a7e
+#endif
bb9a7e
+			}
bb9a7e
+		    }
bb9a7e
+		  while (actlen > 0 && memchr (interp, '\0', actlen) == NULL);
bb9a7e
+
bb9a7e
+		  if (actlen > 0)
bb9a7e
+		    {
bb9a7e
+		      close (fd);
bb9a7e
+		      errno = i;
bb9a7e
+		      sys_error ("%s: %s: bad ELF interpreter", command,
bb9a7e
+				 interp);
bb9a7e
+		      free (interp);
bb9a7e
+		      return (EX_NOEXEC);
bb9a7e
+		    }
bb9a7e
+
bb9a7e
+		  free (interp);
bb9a7e
+		}
bb9a7e
+	    }
bb9a7e
+#endif
bb9a7e
+#if defined (HAVE_HASH_BANG_EXEC) || defined (HAVE_ELF_H)
bb9a7e
+	  close (fd);
bb9a7e
+#endif
bb9a7e
+
bb9a7e
+      errno = i;
bb9a7e
 	  file_error (command);
bb9a7e
 	}
bb9a7e
       return (last_command_exit_value);