nalika / rpms / bash

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