rcolebaugh / rpms / bash

Forked from rpms/bash a year ago
Clone

Blame SOURCES/bash-2.05a-interpreter.patch

07a490
diff -up bash-4.2-rc2/config.h.in.interpreter bash-4.2-rc2/config.h.in
07a490
--- bash-4.2-rc2/config.h.in.interpreter	2011-02-09 07:59:21.000000000 +0100
07a490
+++ bash-4.2-rc2/config.h.in	2011-02-09 07:59:21.000000000 +0100
07a490
@@ -706,6 +706,9 @@
07a490
 /* Define if you have the pathconf function. */
07a490
 #undef HAVE_PATHCONF
07a490
 
07a490
+/* Define if you have the pread function. */
07a490
+#undef HAVE_PREAD
07a490
+
07a490
 /* Define if you have the putenv function.  */
07a490
 #undef HAVE_PUTENV
07a490
 
07a490
@@ -898,6 +901,9 @@
07a490
 /* Define if you have the <dlfcn.h> header file.  */
07a490
 #undef HAVE_DLFCN_H
07a490
 
07a490
+/* Define if you have the <elf.h> header file.  */
07a490
+#undef HAVE_ELF_H
07a490
+
07a490
 /* Define if you have the <grp.h> header file.  */
07a490
 #undef HAVE_GRP_H
07a490
 
07a490
diff -up bash-4.2-rc2/configure.in.interpreter bash-4.2-rc2/configure.in
07a490
--- bash-4.2-rc2/configure.in.interpreter	2011-01-16 21:31:12.000000000 +0100
07a490
+++ bash-4.2-rc2/configure.in	2011-02-09 08:02:27.000000000 +0100
07a490
@@ -659,7 +659,7 @@ BASH_HEADER_INTTYPES
07a490
 AC_CHECK_HEADERS(unistd.h stdlib.h stdarg.h varargs.h limits.h string.h \
07a490
 		 memory.h locale.h termcap.h termio.h termios.h dlfcn.h \
07a490
 		 stddef.h stdint.h netdb.h pwd.h grp.h strings.h regex.h \
07a490
-		 syslog.h ulimit.h)
07a490
+		 syslog.h ulimit.h elf.h)
07a490
 AC_CHECK_HEADERS(sys/pte.h sys/stream.h sys/select.h sys/file.h \
07a490
 		 sys/resource.h sys/param.h sys/socket.h sys/stat.h \
07a490
 		 sys/time.h sys/times.h sys/types.h sys/wait.h)
07a490
@@ -723,7 +723,7 @@ dnl checks for system calls
07a490
 AC_CHECK_FUNCS(dup2 eaccess fcntl getdtablesize getgroups gethostname \
07a490
 		getpagesize getpeername getrlimit getrusage gettimeofday \
07a490
 		kill killpg lstat readlink sbrk select setdtablesize \
07a490
-		setitimer tcgetpgrp uname ulimit waitpid)
07a490
+		setitimer tcgetpgrp uname ulimit waitpid pread)
07a490
 AC_REPLACE_FUNCS(rename)
07a490
 
07a490
 dnl checks for c library functions
07a490
diff -up bash-4.2-rc2/execute_cmd.c.interpreter bash-4.2-rc2/execute_cmd.c
07a490
--- bash-4.2-rc2/execute_cmd.c.interpreter	2011-01-20 04:24:47.000000000 +0100
07a490
+++ bash-4.2-rc2/execute_cmd.c	2011-02-09 07:59:21.000000000 +0100
07a490
@@ -41,6 +41,10 @@
07a490
 #  include <unistd.h>
07a490
 #endif
07a490
 
07a490
+#ifdef HAVE_ELF_H
07a490
+# include <elf.h>
07a490
+#endif
07a490
+
07a490
 #include "posixtime.h"
07a490
 
07a490
 #if defined (HAVE_SYS_RESOURCE_H) && !defined (RLIMTYPE)
07a490
@@ -4975,13 +4979,21 @@ shell_execve (command, args, env)
07a490
 	{
07a490
 	  /* The file has the execute bits set, but the kernel refuses to
07a490
 	     run it for some reason.  See why. */
07a490
+#if defined (HAVE_HASH_BANG_EXEC) || defined (HAVE_ELF_H)
07a490
+	int fd = open (command, O_RDONLY);
07a490
+
07a490
+	if (fd >= 0)
07a490
+		sample_len = read (fd, sample, sizeof (sample));
07a490
+	else
07a490
+		sample_len = -1;
07a490
+#endif
07a490
 #if defined (HAVE_HASH_BANG_EXEC)
07a490
-	  READ_SAMPLE_BUF (command, sample, sample_len);
07a490
 	  if (sample_len > 2 && sample[0] == '#' && sample[1] == '!')
07a490
 	    {
07a490
 	      char *interp;
07a490
 	      int ilen;
07a490
 
07a490
+	      close (fd);
07a490
 	      interp = getinterp (sample, sample_len, (int *)NULL);
07a490
 	      ilen = strlen (interp);
07a490
 	      errno = i;
07a490
@@ -4997,6 +5009,136 @@ shell_execve (command, args, env)
07a490
 	      return (EX_NOEXEC);
07a490
 	    }
07a490
 #endif
07a490
+#if defined (HAVE_ELF_H)
07a490
+	  if (i == ENOENT
07a490
+	      && sample_len > EI_NIDENT
07a490
+	      && memcmp (sample, ELFMAG, SELFMAG) == 0)
07a490
+	    {
07a490
+	      off_t offset = -1;
07a490
+
07a490
+	      /* It is an ELF file.  Now determine whether it is dynamically
07a490
+		 linked and if yes, get the offset of the interpreter
07a490
+		 string.  */
07a490
+	      if (sample[EI_CLASS] == ELFCLASS32
07a490
+		  && sample_len > sizeof (Elf32_Ehdr))
07a490
+		{
07a490
+		  Elf32_Ehdr ehdr;
07a490
+		  Elf32_Phdr *phdr;
07a490
+		  int nphdr;
07a490
+
07a490
+		  /* We have to copy the data since the sample buffer
07a490
+		     might not be aligned correctly to be accessed as
07a490
+		     an Elf32_Ehdr struct.  */
07a490
+		  memcpy (&ehdr, sample, sizeof (Elf32_Ehdr));
07a490
+
07a490
+		  nphdr = ehdr.e_phnum;
07a490
+		  phdr = (Elf32_Phdr *) malloc (nphdr * ehdr.e_phentsize);
07a490
+		  if (phdr != NULL)
07a490
+		    {
07a490
+#ifdef HAVE_PREAD
07a490
+		      sample_len = pread (fd, phdr, nphdr * ehdr.e_phentsize,
07a490
+					  ehdr.e_phoff);
07a490
+#else
07a490
+		      if (lseek (fd, ehdr.e_phoff, SEEK_SET) != -1)
07a490
+			sample_len = read (fd, phdr,
07a490
+					   nphdr * ehdr.e_phentsize);
07a490
+		      else
07a490
+			sample_len = -1;
07a490
+#endif
07a490
+		      if (sample_len == nphdr * ehdr.e_phentsize)
07a490
+			while (nphdr-- > 0)
07a490
+			  if (phdr[nphdr].p_type == PT_INTERP)
07a490
+			    {
07a490
+			      offset = phdr[nphdr].p_offset;
07a490
+			      break;
07a490
+			    }
07a490
+		      free (phdr);
07a490
+		    }
07a490
+		}
07a490
+	      else if (sample[EI_CLASS] == ELFCLASS64
07a490
+		       && sample_len > sizeof (Elf64_Ehdr))
07a490
+		{
07a490
+		  Elf64_Ehdr ehdr;
07a490
+		  Elf64_Phdr *phdr;
07a490
+		  int nphdr;
07a490
+
07a490
+		  /* We have to copy the data since the sample buffer
07a490
+		     might not be aligned correctly to be accessed as
07a490
+		     an Elf64_Ehdr struct.  */
07a490
+		  memcpy (&ehdr, sample, sizeof (Elf64_Ehdr));
07a490
+
07a490
+		  nphdr = ehdr.e_phnum;
07a490
+		  phdr = (Elf64_Phdr *) malloc (nphdr * ehdr.e_phentsize);
07a490
+		  if (phdr != NULL)
07a490
+		    {
07a490
+#ifdef HAVE_PREAD
07a490
+		      sample_len = pread (fd, phdr, nphdr * ehdr.e_phentsize,
07a490
+					  ehdr.e_phoff);
07a490
+#else
07a490
+		      if (lseek (fd, ehdr.e_phoff, SEEK_SET) != -1)
07a490
+			sample_len = read (fd, phdr,
07a490
+					   nphdr * ehdr.e_phentsize);
07a490
+		      else
07a490
+			sample_len = -1;
07a490
+#endif
07a490
+		      if (sample_len == nphdr * ehdr.e_phentsize)
07a490
+			while (nphdr-- > 0)
07a490
+			  if (phdr[nphdr].p_type == PT_INTERP)
07a490
+			    {
07a490
+			      offset = phdr[nphdr].p_offset;
07a490
+			      break;
07a490
+			    }
07a490
+		      free (phdr);
07a490
+		    }
07a490
+		}
07a490
+
07a490
+	      if (offset != -1)
07a490
+		{
07a490
+		  size_t maxlen = 0;
07a490
+		  size_t actlen = 0;
07a490
+		  char *interp = NULL;
07a490
+
07a490
+		  do
07a490
+		    {
07a490
+		      if (actlen == maxlen)
07a490
+			{
07a490
+			  char *newinterp = realloc (interp, maxlen += 200);
07a490
+			  if (newinterp == NULL)
07a490
+			    {
07a490
+			      actlen = 0;
07a490
+			      break;
07a490
+			    }
07a490
+			  interp = newinterp;
07a490
+
07a490
+#ifdef HAVE_PREAD
07a490
+			  actlen = pread (fd, interp, maxlen, offset);
07a490
+#else
07a490
+			  if (lseek (fd, offset, SEEK_SET) != -1)
07a490
+			    actlen = read (fd, interp, maxlen);
07a490
+			  else
07a490
+			    actlen = -1;
07a490
+#endif
07a490
+			}
07a490
+		    }
07a490
+		  while (actlen > 0 && memchr (interp, '\0', actlen) == NULL);
07a490
+
07a490
+		  if (actlen > 0)
07a490
+		    {
07a490
+		      close (fd);
07a490
+		      errno = i;
07a490
+		      sys_error ("%s: %s: bad ELF interpreter", command,
07a490
+				 interp);
07a490
+		      free (interp);
07a490
+		      return (EX_NOEXEC);
07a490
+		    }
07a490
+
07a490
+		  free (interp);
07a490
+		}
07a490
+	    }
07a490
+#endif
07a490
+#if defined (HAVE_HASH_BANG_EXEC) || defined (HAVE_ELF_H)
07a490
+	  close (fd);
07a490
+#endif
07a490
 	  errno = i;
07a490
 	  file_error (command);
07a490
 	}