summaryrefslogtreecommitdiffstats
path: root/bash-2.05a-interpreter.patch
blob: b9f0da2fb44bc6320cec93075ed168c0c531c07a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
diff -up bash-4.2-rc2/config.h.in.interpreter bash-4.2-rc2/config.h.in
--- bash-4.2-rc2/config.h.in.interpreter	2011-02-09 07:59:21.000000000 +0100
+++ bash-4.2-rc2/config.h.in	2011-02-09 07:59:21.000000000 +0100
@@ -706,6 +706,9 @@
 /* Define if you have the pathconf function. */
 #undef HAVE_PATHCONF
 
+/* Define if you have the pread function. */
+#undef HAVE_PREAD
+
 /* Define if you have the putenv function.  */
 #undef HAVE_PUTENV
 
@@ -898,6 +901,9 @@
 /* Define if you have the <dlfcn.h> header file.  */
 #undef HAVE_DLFCN_H
 
+/* Define if you have the <elf.h> header file.  */
+#undef HAVE_ELF_H
+
 /* Define if you have the <grp.h> header file.  */
 #undef HAVE_GRP_H
 
diff -up bash-4.2-rc2/configure.in.interpreter bash-4.2-rc2/configure.in
--- bash-4.2-rc2/configure.in.interpreter	2011-01-16 21:31:12.000000000 +0100
+++ bash-4.2-rc2/configure.ac	2011-02-09 08:02:27.000000000 +0100
@@ -659,7 +659,7 @@ BASH_HEADER_INTTYPES
 AC_CHECK_HEADERS(unistd.h stdlib.h stdarg.h varargs.h limits.h string.h \
 		 memory.h locale.h termcap.h termio.h termios.h dlfcn.h \
 		 stdbool.h stddef.h stdint.h netdb.h pwd.h grp.h strings.h \
-		 regex.h syslog.h ulimit.h)
+		 regex.h syslog.h ulimit.h elf.h)
 AC_CHECK_HEADERS(sys/pte.h sys/stream.h sys/select.h sys/file.h \
 		 sys/resource.h sys/param.h sys/socket.h sys/stat.h \
 		 sys/time.h sys/times.h sys/types.h sys/wait.h)
@@ -723,7 +723,7 @@ dnl checks for system calls
 AC_CHECK_FUNCS(dup2 eaccess fcntl getdtablesize getgroups gethostname \
 		getpagesize getpeername getrlimit getrusage gettimeofday \
 		kill killpg lstat readlink sbrk select setdtablesize \
-		setitimer tcgetpgrp uname ulimit waitpid)
+		setitimer tcgetpgrp uname ulimit waitpid pread)
 AC_REPLACE_FUNCS(rename)
 
 dnl checks for c library functions
diff -up bash-4.2-rc2/execute_cmd.c.interpreter bash-4.2-rc2/execute_cmd.c
--- bash-4.2-rc2/execute_cmd.c.interpreter	2011-01-20 04:24:47.000000000 +0100
+++ bash-4.2-rc2/execute_cmd.c	2011-02-09 07:59:21.000000000 +0100
@@ -41,6 +41,10 @@
 #  include <unistd.h>
 #endif
 
+#ifdef HAVE_ELF_H
+# include <elf.h>
+#endif
+
 #include "posixtime.h"
 
 #if defined (HAVE_SYS_RESOURCE_H) && !defined (RLIMTYPE)
@@ -4975,14 +4979,22 @@ shell_execve (command, args, env)
 	{
 	  /* The file has the execute bits set, but the kernel refuses to
 	     run it for some reason.  See why. */
+#if defined (HAVE_HASH_BANG_EXEC) || defined (HAVE_ELF_H)
+	int fd = open (command, O_RDONLY);
+
+	if (fd >= 0)
+		sample_len = read (fd, sample, sizeof (sample));
+	else
+		sample_len = -1;
+#endif
 #if defined (HAVE_HASH_BANG_EXEC)
-	  READ_SAMPLE_BUF (command, sample, sample_len);
 	  sample[sample_len - 1] = '\0';
 	  if (sample_len > 2 && sample[0] == '#' && sample[1] == '!')
 	    {
 	      char *interp;
 	      int ilen;
 
+	      close (fd);
 	      interp = getinterp (sample, sample_len, (int *)NULL);
 	      ilen = strlen (interp);
 	      errno = i;
@@ -4997,6 +5009,136 @@ shell_execve (command, args, env)
 	      return (EX_NOEXEC);
 	    }
 #endif
+#if defined (HAVE_ELF_H)
+	  if (i == ENOENT
+	      && sample_len > EI_NIDENT
+	      && memcmp (sample, ELFMAG, SELFMAG) == 0)
+	    {
+	      off_t offset = -1;
+
+	      /* It is an ELF file.  Now determine whether it is dynamically
+		 linked and if yes, get the offset of the interpreter
+		 string.  */
+	      if (sample[EI_CLASS] == ELFCLASS32
+		  && sample_len > sizeof (Elf32_Ehdr))
+		{
+		  Elf32_Ehdr ehdr;
+		  Elf32_Phdr *phdr;
+		  int nphdr;
+
+		  /* We have to copy the data since the sample buffer
+		     might not be aligned correctly to be accessed as
+		     an Elf32_Ehdr struct.  */
+		  memcpy (&ehdr, sample, sizeof (Elf32_Ehdr));
+
+		  nphdr = ehdr.e_phnum;
+		  phdr = (Elf32_Phdr *) malloc (nphdr * ehdr.e_phentsize);
+		  if (phdr != NULL)
+		    {
+#ifdef HAVE_PREAD
+		      sample_len = pread (fd, phdr, nphdr * ehdr.e_phentsize,
+					  ehdr.e_phoff);
+#else
+		      if (lseek (fd, ehdr.e_phoff, SEEK_SET) != -1)
+			sample_len = read (fd, phdr,
+					   nphdr * ehdr.e_phentsize);
+		      else
+			sample_len = -1;
+#endif
+		      if (sample_len == nphdr * ehdr.e_phentsize)
+			while (nphdr-- > 0)
+			  if (phdr[nphdr].p_type == PT_INTERP)
+			    {
+			      offset = phdr[nphdr].p_offset;
+			      break;
+			    }
+		      free (phdr);
+		    }
+		}
+	      else if (sample[EI_CLASS] == ELFCLASS64
+		       && sample_len > sizeof (Elf64_Ehdr))
+		{
+		  Elf64_Ehdr ehdr;
+		  Elf64_Phdr *phdr;
+		  int nphdr;
+
+		  /* We have to copy the data since the sample buffer
+		     might not be aligned correctly to be accessed as
+		     an Elf64_Ehdr struct.  */
+		  memcpy (&ehdr, sample, sizeof (Elf64_Ehdr));
+
+		  nphdr = ehdr.e_phnum;
+		  phdr = (Elf64_Phdr *) malloc (nphdr * ehdr.e_phentsize);
+		  if (phdr != NULL)
+		    {
+#ifdef HAVE_PREAD
+		      sample_len = pread (fd, phdr, nphdr * ehdr.e_phentsize,
+					  ehdr.e_phoff);
+#else
+		      if (lseek (fd, ehdr.e_phoff, SEEK_SET) != -1)
+			sample_len = read (fd, phdr,
+					   nphdr * ehdr.e_phentsize);
+		      else
+			sample_len = -1;
+#endif
+		      if (sample_len == nphdr * ehdr.e_phentsize)
+			while (nphdr-- > 0)
+			  if (phdr[nphdr].p_type == PT_INTERP)
+			    {
+			      offset = phdr[nphdr].p_offset;
+			      break;
+			    }
+		      free (phdr);
+		    }
+		}
+
+	      if (offset != -1)
+		{
+		  size_t maxlen = 0;
+		  size_t actlen = 0;
+		  char *interp = NULL;
+
+		  do
+		    {
+		      if (actlen == maxlen)
+			{
+			  char *newinterp = realloc (interp, maxlen += 200);
+			  if (newinterp == NULL)
+			    {
+			      actlen = 0;
+			      break;
+			    }
+			  interp = newinterp;
+
+#ifdef HAVE_PREAD
+			  actlen = pread (fd, interp, maxlen, offset);
+#else
+			  if (lseek (fd, offset, SEEK_SET) != -1)
+			    actlen = read (fd, interp, maxlen);
+			  else
+			    actlen = -1;
+#endif
+			}
+		    }
+		  while (actlen > 0 && memchr (interp, '\0', actlen) == NULL);
+
+		  if (actlen > 0)
+		    {
+		      close (fd);
+		      errno = i;
+		      sys_error ("%s: %s: bad ELF interpreter", command,
+				 interp);
+		      free (interp);
+		      return (EX_NOEXEC);
+		    }
+
+		  free (interp);
+		}
+	    }
+#endif
+#if defined (HAVE_HASH_BANG_EXEC) || defined (HAVE_ELF_H)
+	  close (fd);
+#endif
 	  errno = i;
 	  file_error (command);
 	}