xref: /freebsd/usr.sbin/crunch/crunchide/exec_elf32.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1997 Christopher G. Demetriou.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Christopher G. Demetriou
17  *	for the NetBSD Project.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 #ifndef lint
35 #if 0
36 __RCSID("$NetBSD: exec_elf32.c,v 1.6 1999/09/20 04:12:16 christos Exp $");
37 #endif
38 #endif
39 __FBSDID("$FreeBSD$");
40 
41 #ifndef ELFSIZE
42 #define ELFSIZE         32
43 #endif
44 
45 #include <sys/types.h>
46 #include <sys/endian.h>
47 #include <sys/stat.h>
48 
49 #include <errno.h>
50 #include <limits.h>
51 #include <stddef.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 
57 #include "extern.h"
58 
59 #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
60     (defined(NLIST_ELF64) && (ELFSIZE == 64))
61 
62 #define	__ELF_WORD_SIZE ELFSIZE
63 #if (ELFSIZE == 32)
64 #include <sys/elf32.h>
65 #define	xewtoh(x)	((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
66 #define	htoxew(x)	((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
67 #define	wewtoh(x)	((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
68 #define	htowew(x)	((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
69 #elif (ELFSIZE == 64)
70 #include <sys/elf64.h>
71 #define	xewtoh(x)	((data == ELFDATA2MSB) ? be64toh(x) : le64toh(x))
72 #define	htoxew(x)	((data == ELFDATA2MSB) ? htobe64(x) : htole64(x))
73 /* elf64 Elf64_Word are 32 bits */
74 #define	wewtoh(x)	((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
75 #define	htowew(x)	((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
76 #endif
77 #include <sys/elf_generic.h>
78 
79 #define CONCAT(x,y)     __CONCAT(x,y)
80 #define ELFNAME(x)      CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
81 #define ELFNAME2(x,y)   CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
82 #define ELFNAMEEND(x)   CONCAT(x,CONCAT(_elf,ELFSIZE))
83 #define ELFDEFNNAME(x)  CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
84 #ifndef ELFCLASS
85 #define ELFCLASS	CONCAT(ELFCLASS,ELFSIZE)
86 #endif
87 
88 #define	xe16toh(x)	((data == ELFDATA2MSB) ? be16toh(x) : le16toh(x))
89 #define	xe32toh(x)	((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
90 #define	htoxe32(x)	((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
91 
92 struct shlayout {
93 	Elf_Shdr *shdr;
94 	void *bufp;
95 };
96 
97 static ssize_t
98 xreadatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
99 {
100 	ssize_t rv;
101 
102 	if (lseek(fd, off, SEEK_SET) != off) {
103 		perror(fn);
104 		return -1;
105 	}
106 	if ((size_t)(rv = read(fd, buf, size)) != size) {
107 		fprintf(stderr, "%s: read error: %s\n", fn,
108 		    rv == -1 ? strerror(errno) : "short read");
109 		return -1;
110 	}
111 	return size;
112 }
113 
114 static ssize_t
115 xwriteatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
116 {
117 	ssize_t rv;
118 
119 	if (lseek(fd, off, SEEK_SET) != off) {
120 		perror(fn);
121 		return -1;
122 	}
123 	if ((size_t)(rv = write(fd, buf, size)) != size) {
124 		fprintf(stderr, "%s: write error: %s\n", fn,
125 		    rv == -1 ? strerror(errno) : "short write");
126 		return -1;
127 	}
128 	return size;
129 }
130 
131 static void *
132 xmalloc(size_t size, const char *fn, const char *use)
133 {
134 	void *rv;
135 
136 	rv = malloc(size);
137 	if (rv == NULL)
138 		fprintf(stderr, "%s: out of memory (allocating for %s)\n",
139 		    fn, use);
140 	return (rv);
141 }
142 
143 static void *
144 xrealloc(void *ptr, size_t size, const char *fn, const char *use)
145 {
146 	void *rv;
147 
148 	rv = realloc(ptr, size);
149 	if (rv == NULL) {
150 		free(ptr);
151 		fprintf(stderr, "%s: out of memory (reallocating for %s)\n",
152 		    fn, use);
153 	}
154 	return (rv);
155 }
156 
157 int
158 ELFNAMEEND(check)(int fd, const char *fn __unused)
159 {
160 	Elf_Ehdr eh;
161 	struct stat sb;
162 	unsigned char data;
163 
164 	/*
165 	 * Check the header to make sure it's an ELF file (of the
166 	 * appropriate size).
167 	 */
168 	if (fstat(fd, &sb) == -1)
169 		return 0;
170 	if (sb.st_size < (off_t)(sizeof eh))
171 		return 0;
172 	if (read(fd, &eh, sizeof eh) != sizeof eh)
173 		return 0;
174 
175 	if (IS_ELF(eh) == 0 || eh.e_ident[EI_CLASS] != ELFCLASS)
176                 return 0;
177 
178 	data = eh.e_ident[EI_DATA];
179 
180 	switch (xe16toh(eh.e_machine)) {
181 	case EM_386: break;
182 	case EM_ALPHA: break;
183 #ifndef EM_AARCH64
184 #define	EM_AARCH64	183
185 #endif
186 	case EM_AARCH64: break;
187 	case EM_ARM: break;
188 	case EM_MIPS: break;
189 	case /* EM_MIPS_RS3_LE */ EM_MIPS_RS4_BE: break;
190 	case EM_PPC: break;
191 	case EM_PPC64: break;
192 #ifndef EM_RISCV
193 #define	EM_RISCV	243
194 #endif
195 	case EM_RISCV: break;
196 	case EM_S390: break;
197 	case EM_SPARCV9: break;
198 	case EM_X86_64: break;
199 /*        ELFDEFNNAME(MACHDEP_ID_CASES) */
200 
201         default:
202                 return 0;
203         }
204 
205 	return 1;
206 }
207 
208 /*
209  * This function 'hides' (some of) ELF executable file's symbols.
210  * It hides them by renaming them to "_$$hide$$ <filename> <symbolname>".
211  * Symbols in the global keep list, or which are marked as being undefined,
212  * are left alone.
213  *
214  * An old version of this code shuffled various tables around, turning
215  * global symbols to be hidden into local symbols.  That lost on the
216  * mips, because CALL16 relocs must reference global symbols, and, if
217  * those symbols were being hidden, they were no longer global.
218  *
219  * The new renaming behaviour doesn't take global symbols out of the
220  * namespace.  However, it's ... unlikely that there will ever be
221  * any collisions in practice because of the new method.
222  */
223 int
224 ELFNAMEEND(hide)(int fd, const char *fn)
225 {
226 	Elf_Ehdr ehdr;
227 	struct shlayout *layoutp = NULL;
228 	Elf_Shdr *shdrp = NULL, *symtabshdr, *strtabshdr, *shstrtabshdr;
229 	Elf_Shdr shdrshdr;
230 	Elf_Sym *symtabp = NULL;
231 	char *shstrtabp = NULL, *strtabp = NULL;
232 	Elf_Size nsyms, ewi;
233 	Elf_Off off;
234 	ssize_t shdrsize;
235 	int rv, i, weird, l, m, r, strtabidx;
236 	size_t nstrtab_size, nstrtab_nextoff, fn_size, size;
237 	char *nstrtabp = NULL;
238 	unsigned char data;
239 	const char *weirdreason = NULL;
240 	void *buf;
241 	Elf_Half shnum;
242 
243 	rv = 0;
244 	if (xreadatoff(fd, &ehdr, 0, sizeof ehdr, fn) != sizeof ehdr)
245 		goto bad;
246 
247 	data = ehdr.e_ident[EI_DATA];
248 	shnum = xe16toh(ehdr.e_shnum);
249 
250 	shdrsize = shnum * xe16toh(ehdr.e_shentsize);
251 	if ((shdrp = xmalloc(shdrsize, fn, "section header table")) == NULL)
252 		goto bad;
253 	if (xreadatoff(fd, shdrp, xewtoh(ehdr.e_shoff), shdrsize, fn) !=
254 	    shdrsize)
255 		goto bad;
256 
257 	symtabshdr = strtabshdr = shstrtabshdr = NULL;
258 	weird = 0;
259 	for (i = 0; i < shnum; i++) {
260 		switch (xe32toh(shdrp[i].sh_type)) {
261 		case SHT_SYMTAB:
262 			if (symtabshdr != NULL) {
263 				weird = 1;
264 				weirdreason = "multiple symbol tables";
265 			}
266 			symtabshdr = &shdrp[i];
267 			strtabshdr = &shdrp[xe32toh(shdrp[i].sh_link)];
268 			break;
269 		case SHT_STRTAB:
270 			if (i == xe16toh(ehdr.e_shstrndx))
271 				shstrtabshdr = &shdrp[i];
272 			break;
273 		}
274 	}
275 	if (symtabshdr == NULL)
276 		goto out;
277 	if (strtabshdr == NULL) {
278 		weird = 1;
279 		weirdreason = "string table does not exist";
280 	}
281 	if (shstrtabshdr == NULL) {
282 		weird = 1;
283 		weirdreason = "section header string table does not exist";
284 	}
285 	if (strtabshdr == shstrtabshdr) {
286 		weird = 1;
287 		weirdreason = "combined strtab and shstrtab not supported";
288 	}
289 	if (weirdreason == NULL)
290 		weirdreason = "unsupported";
291 	if (weird) {
292 		fprintf(stderr, "%s: weird executable (%s)\n", fn, weirdreason);
293 		goto bad;
294 	}
295 
296 	/*
297 	 * sort section layout table by offset
298 	 */
299 	layoutp = xmalloc((shnum + 1) * sizeof(struct shlayout),
300 	    fn, "layout table");
301 	if (layoutp == NULL)
302 		goto bad;
303 
304 	/* add a pseudo entry to represent the section header table */
305 	shdrshdr.sh_offset = ehdr.e_shoff;
306 	shdrshdr.sh_size = htoxew(shdrsize);
307 	shdrshdr.sh_addralign = htoxew(ELFSIZE / 8);
308 	layoutp[shnum].shdr = &shdrshdr;
309 
310 	/* insert and sort normal section headers */
311 	for (i = shnum; i-- != 0;) {
312 		l = i + 1;
313 		r = shnum;
314 		while (l <= r) {
315 			m = ( l + r) / 2;
316 			if (xewtoh(shdrp[i].sh_offset) >
317 			    xewtoh(layoutp[m].shdr->sh_offset))
318 				l = m + 1;
319 			else
320 				r = m - 1;
321 		}
322 
323 		if (r != i) {
324 			memmove(&layoutp[i], &layoutp[i + 1],
325 			    sizeof(struct shlayout) * (r - i));
326 		}
327 
328 		layoutp[r].shdr = &shdrp[i];
329 		layoutp[r].bufp = NULL;
330 	}
331 	++shnum;
332 
333 	/*
334 	 * load up everything we need
335 	 */
336 
337 	/* load section string table for debug use */
338 	if ((size = xewtoh(shstrtabshdr->sh_size)) == 0)
339 		goto bad;
340 	if ((shstrtabp = xmalloc(size, fn, "section string table")) == NULL)
341 		goto bad;
342 	if ((size_t)xreadatoff(fd, shstrtabp, xewtoh(shstrtabshdr->sh_offset),
343 	    size, fn) != size)
344 		goto bad;
345 	if (shstrtabp[size - 1] != '\0')
346 		goto bad;
347 
348 	/* we need symtab, strtab, and everything behind strtab */
349 	strtabidx = INT_MAX;
350 	for (i = 0; i < shnum; i++) {
351 		if (layoutp[i].shdr == &shdrshdr) {
352 			/* not load section header again */
353 			layoutp[i].bufp = shdrp;
354 			continue;
355 		}
356 		if (layoutp[i].shdr == shstrtabshdr) {
357 			/* not load section string table again */
358 			layoutp[i].bufp = shstrtabp;
359 			continue;
360 		}
361 
362 		if (layoutp[i].shdr == strtabshdr)
363 			strtabidx = i;
364 		if (layoutp[i].shdr == symtabshdr || i >= strtabidx) {
365 			off = xewtoh(layoutp[i].shdr->sh_offset);
366 			if ((size = xewtoh(layoutp[i].shdr->sh_size)) == 0)
367 				goto bad;
368 			layoutp[i].bufp = xmalloc(size, fn,
369 			    shstrtabp + xewtoh(layoutp[i].shdr->sh_name));
370 			if (layoutp[i].bufp == NULL)
371 				goto bad;
372 			if ((size_t)xreadatoff(fd, layoutp[i].bufp, off, size, fn) !=
373 			    size)
374 				goto bad;
375 
376 			/* set symbol table and string table */
377 			if (layoutp[i].shdr == symtabshdr) {
378 				symtabp = layoutp[i].bufp;
379 			} else if (layoutp[i].shdr == strtabshdr) {
380 				strtabp = layoutp[i].bufp;
381 				if (strtabp[size - 1] != '\0')
382 					goto bad;
383 			}
384 		}
385 	}
386 
387 	nstrtab_size = 256;
388 	nstrtabp = xmalloc(nstrtab_size, fn, "new string table");
389 	if (nstrtabp == NULL)
390 		goto bad;
391 	nstrtab_nextoff = 0;
392 
393 	fn_size = strlen(fn);
394 
395 	/* Prepare data structures for symbol movement. */
396 	nsyms = xewtoh(symtabshdr->sh_size) / xewtoh(symtabshdr->sh_entsize);
397 
398 	/* move symbols, making them local */
399 	for (ewi = 0; ewi < nsyms; ewi++) {
400 		Elf_Sym *sp = &symtabp[ewi];
401 		const char *symname = strtabp + xe32toh(sp->st_name);
402 		size_t newent_len;
403 		/*
404 		 * make sure there's size for the next entry, even if it's
405 		 * as large as it can be.
406 		 *
407 		 * "_$$hide$$ <filename> <symname><NUL>" ->
408 		 *    9 + 3 + sizes of fn and sym name
409 		 */
410 		while ((nstrtab_size - nstrtab_nextoff) <
411 		    strlen(symname) + fn_size + 12) {
412 			nstrtab_size *= 2;
413 			nstrtabp = xrealloc(nstrtabp, nstrtab_size, fn,
414 			    "new string table");
415 			if (nstrtabp == NULL)
416 				goto bad;
417 		}
418 
419 		sp->st_name = htowew(nstrtab_nextoff);
420 
421 		/* if it's a keeper or is undefined, don't rename it. */
422 		if (in_keep_list(symname) ||
423 		    (xe16toh(sp->st_shndx) == SHN_UNDEF)) {
424 			newent_len = sprintf(nstrtabp + nstrtab_nextoff,
425 			    "%s", symname) + 1;
426 		} else {
427 			newent_len = sprintf(nstrtabp + nstrtab_nextoff,
428 			    "_$$hide$$ %s %s", fn, symname) + 1;
429 		}
430 		nstrtab_nextoff += newent_len;
431 	}
432 	strtabshdr->sh_size = htoxew(nstrtab_nextoff);
433 
434 	/*
435 	 * update section header table in ascending order of offset
436 	 */
437 	for (i = strtabidx + 1; i < shnum; i++) {
438 		Elf_Off soff, align;
439 		soff = xewtoh(layoutp[i - 1].shdr->sh_offset) +
440 		    xewtoh(layoutp[i - 1].shdr->sh_size);
441 		align = xewtoh(layoutp[i].shdr->sh_addralign);
442 		soff = (soff + (align - 1)) & ~(align - 1);
443 		layoutp[i].shdr->sh_offset = htoxew(soff);
444 	}
445 
446 	/*
447 	 * write data to the file in descending order of offset
448 	 */
449 	for (i = shnum; i-- != 0;) {
450 		if (layoutp[i].shdr == strtabshdr) {
451 			/* new string table */
452 			buf = nstrtabp;
453 		} else
454 			buf = layoutp[i].bufp;
455 
456 		if (layoutp[i].shdr == &shdrshdr ||
457 		    layoutp[i].shdr == symtabshdr || i >= strtabidx) {
458 			if (buf == NULL)
459 				goto bad;
460 
461 			/*
462 			 * update the offset of section header table in elf
463 			 * header if needed.
464 			 */
465 			if (layoutp[i].shdr == &shdrshdr &&
466 			    ehdr.e_shoff != shdrshdr.sh_offset) {
467 				ehdr.e_shoff = shdrshdr.sh_offset;
468 				off = offsetof(Elf_Ehdr, e_shoff);
469 				size = sizeof(Elf_Off);
470 				if ((size_t)xwriteatoff(fd, &ehdr.e_shoff, off, size,
471 				    fn) != size)
472 					goto bad;
473 			}
474 
475 			off = xewtoh(layoutp[i].shdr->sh_offset);
476 			size = xewtoh(layoutp[i].shdr->sh_size);
477 			if ((size_t)xwriteatoff(fd, buf, off, size, fn) != size)
478 				goto bad;
479 		}
480 	}
481 
482 out:
483 	if (layoutp != NULL) {
484 		for (i = 0; i < shnum; i++) {
485 			if (layoutp[i].bufp != NULL)
486 				free(layoutp[i].bufp);
487 		}
488 		free(layoutp);
489 	}
490 	free(nstrtabp);
491 	return (rv);
492 
493 bad:
494 	rv = 1;
495 	goto out;
496 }
497 
498 #endif /* include this size of ELF */
499