nalika / rpms / bash

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