summaryrefslogtreecommitdiffstats
path: root/tcsh-6.18.02-elf-interpreter.patch
blob: b1ee11742d811c58e690b02262a34e2ee0b6dfa9 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
From df1e58586f48f124079fe81ba1361e12cef2eb1c Mon Sep 17 00:00:00 2001
From: Fridolin Pokorny <fpokorny@redhat.com>
Date: Fri, 25 Jul 2014 08:52:25 +0200
Subject: [PATCH 7/8] Added tcsh-6.18.01-elf-interpreter.patch to report
 missing ELF interpreter

Resolves: #711066

---
 config.h.in  |   6 +++
 configure.ac |   5 ++-
 sh.err.c     |   4 +-
 sh.exec.c    | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 152 insertions(+), 3 deletions(-)

diff --git a/config.h.in b/config.h.in
index 15c916a..0f3a64d 100644
--- a/config.h.in
+++ b/config.h.in
@@ -36,6 +36,9 @@
 /* Define to 1 if you have the `dup2' function. */
 #undef HAVE_DUP2
 
+/* Define to 1 if you have the <elf.h> header file. */
+#undef HAVE_ELF_H
+
 /* Define to 1 if you have the <features.h> header file. */
 #undef HAVE_FEATURES_H
 
@@ -96,6 +99,9 @@
 /* Define to 1 if you have the <paths.h> header file. */
 #undef HAVE_PATHS_H
 
+/* Define to 1 if you have the `pread' function. */
+#undef HAVE_PREAD
+
 /* Define to 1 if you have the `sbrk' function. */
 #undef HAVE_SBRK
 
diff --git a/configure.ac b/configure.ac
index c6f9f1a..2d2868f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -316,7 +316,7 @@ AC_SEARCH_LIBS(catgets, catgets)
 AM_ICONV
 
 dnl Checks for header files
-AC_CHECK_HEADERS([auth.h crypt.h features.h inttypes.h paths.h] dnl
+AC_CHECK_HEADERS([auth.h crypt.h elf.h features.h inttypes.h paths.h] dnl
 		 [shadow.h stdint.h utmp.h utmpx.h])
 AC_CHECK_HEADERS([wchar.h],
 	[AC_CHECK_SIZEOF([wchar_t], [], [dnl
@@ -398,7 +398,8 @@ AC_CHECK_FUNC([setlocale], [have_setlocale=yes], [have_setlocale=no])
 AC_CHECK_FUNC([catgets], [have_catgets=yes], [have_catgets=no])
 AC_CHECK_FUNCS([dup2 getauthid getcwd gethostname getpwent] dnl
 	[getutent getutxent mallinfo memmove memset mkstemp nice] dnl
-	[nl_langinfo sbrk setpgid setpriority strerror strstr sysconf wcwidth])
+	[nl_langinfo pread sbrk setpgid setpriority strerror strstr] dnl
+	[sysconf wcwidth])
 AC_FUNC_GETPGRP
 AC_FUNC_MBRTOWC
 if test "x${cross_compiling}" != xyes ; then
diff --git a/sh.err.c b/sh.err.c
index e157d6a..d39111b 100644
--- a/sh.err.c
+++ b/sh.err.c
@@ -189,7 +189,8 @@ char   *seterr = NULL;	/* Holds last error if there was one */
 #define ERR_INVALID	133
 #define ERR_BADCOLORVAR	134
 #define ERR_EOF		135
-#define NO_ERRORS	136
+#define ERR_ELFINTERP	136
+#define NO_ERRORS	137
 
 static const char *elst[NO_ERRORS] INIT_ZERO_STRUCT;
 
@@ -367,6 +368,7 @@ errinit(void)
     elst[ERR_BADJOB] = CSAVS(1, 136, "No such job (badjob)");
     elst[ERR_BADCOLORVAR] = CSAVS(1, 137, "Unknown colorls variable `%c%c'");
     elst[ERR_EOF] = CSAVS(1, 138, "Unexpected end of file");
+    elst[ERR_ELFINTERP] = CSAVS(1, 139, "No such ELF interpreter");
 }
 
 /* Cleanup data. */
diff --git a/sh.exec.c b/sh.exec.c
index 14f9445..13a3cdf 100644
--- a/sh.exec.c
+++ b/sh.exec.c
@@ -40,6 +40,10 @@ RCSID("$tcsh: sh.exec.c,v 3.80 2014/07/11 14:57:55 christos Exp $")
 #include <nt.const.h>
 #endif /*WINNT_NATIVE*/
 
+#ifdef HAVE_ELF_H
+#include <elf.h>
+#endif /*HAVE_ELF_H*/
+
 /*
  * C shell
  */
@@ -509,6 +513,142 @@ texec(Char *sf, Char **st)
     case 0:			/* execv fails and returns 0! */
 #endif /* _IBMR2 */
     case ENOENT:
+#ifdef HAVE_ELF_H
+	/*
+	 * If dynamically linked ELF binary is not executed and exists,
+	 * the real reason ENOENT is that ELF interpreter is missing.
+	 *
+	 * Written by Ulrich Drepper for bash
+	 * adopted by Fridolin Pokorny <fpokorny@redhat.com>
+	 */
+	if ((fd = xopen(f, O_RDONLY|O_LARGEFILE)) != -1) {
+	    int nread;
+	    char *sample;
+	    int offset = -1;
+	    int sample_size;
+
+	    /* Inspect 32 and 64 ELF */
+	    if (sizeof(Elf64_Ehdr) > sizeof(Elf32_Ehdr))
+		sample_size = sizeof(Elf64_Ehdr);
+	    else
+		sample_size = sizeof(Elf32_Ehdr);
+
+	    sample = xmalloc(sample_size);
+
+	    if (sample != 0 &&
+		    (nread = xread(fd, sample, sample_size)) == sample_size) {
+		if (memcmp(sample, ELFMAG, SELFMAG) == 0) {
+		    if (sample[EI_CLASS] == ELFCLASS32 &&
+			    sample_size >= 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 = xmalloc(nphdr * ehdr.e_phentsize);
+			if(phdr != NULL) {
+#ifdef HAVE_PREAD
+			    nread = pread(fd, phdr, nphdr * ehdr.e_phentsize,
+					  ehdr.e_phoff);
+#else /* !HAVE_PREAD */
+			    if (lseek(fd, ehdr.e_phoff, SEEK_SET) != -1)
+				nread = read(fd, phdr,
+					   nphdr * ehdr.e_phentsize);
+			    else
+				nread = -1;
+#endif /* HAVE_PREAD */
+			    if (nread == nphdr * ehdr.e_phentsize) {
+				while (nphdr-- > 0) {
+				    if (phdr[nphdr].p_type == PT_INTERP) {
+					offset = phdr[nphdr].p_offset;
+					break;
+				    }
+				}
+			    }
+			    xfree(phdr);
+			}
+		    } else if (sample[EI_CLASS] == ELFCLASS64 &&
+			    sample_size >= 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 = xmalloc(nphdr * ehdr.e_phentsize);
+			if (phdr != NULL) {
+#ifdef HAVE_PREAD
+			    nread = pread (fd, phdr, nphdr * ehdr.e_phentsize,
+					  ehdr.e_phoff);
+#else /* !HAVE_PREAD */
+			    if (lseek(fd, ehdr.e_phoff, SEEK_SET) != -1)
+				nread = read (fd, phdr,
+					   nphdr * ehdr.e_phentsize);
+			    else
+				nread = -1;
+#endif /* HAVE_PREAD */
+			    if (nread == nphdr * ehdr.e_phentsize) {
+				while (nphdr-- > 0) {
+				  if (phdr[nphdr].p_type == PT_INTERP) {
+				      offset = phdr[nphdr].p_offset;
+				      break;
+				    }
+				}
+			    }
+			    xfree (phdr);
+			}
+		    }
+
+		    if (offset != -1) {
+			size_t maxlen = 0;
+			size_t actlen = 0;
+			char *interp = NULL;
+
+			do {
+			    if (actlen == maxlen) {
+				char *newinterp = xrealloc(interp, maxlen += 200);
+				if (newinterp == NULL) {
+				    actlen = 0;
+				    break;
+				}
+				interp = newinterp;
+#ifdef HAVE_PREAD
+				actlen = pread (fd, interp, maxlen, offset);
+#else /* !HAVE_PREAD */
+				if (lseek (fd, offset, SEEK_SET) != -1)
+				    actlen = read (fd, interp, maxlen);
+				else
+				    actlen = -1;
+#endif /* HAVE_PREAD */
+			    }
+			} while (actlen > 0 &&
+				memchr (interp, '\0', actlen) == NULL);
+
+			if (actlen > 0) {
+			    xclose (fd);
+			    xfree (interp);
+			    setname(f);
+			    stderror(ERR_NAME | ERR_ELFINTERP);
+			}
+		    }
+		}
+	    }
+	    xfree(sample);
+	}
+#endif /* HAVE_ELF_H */
 	break;
 
     default:
-- 
1.9.3