Blame SOURCES/bash-2.05a-interpreter.patch

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