nalika / rpms / bash

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