• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2014 The ChromiumOS Authors
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  */
5 
6 #include <elf.h>
7 #include <endian.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 
13 #include "elfparse.h"
14 #include "util.h"
15 
is_elf_magic(const uint8_t * buf)16 int is_elf_magic (const uint8_t *buf)
17 {
18 	return (buf[EI_MAG0] == ELFMAG0) &&
19 	       (buf[EI_MAG1] == ELFMAG1) &&
20 	       (buf[EI_MAG2] == ELFMAG2) &&
21 	       (buf[EI_MAG3] == ELFMAG3);
22 }
23 
24 #define parseElftemplate(bit)                                                \
25 ElfType parseElf ## bit(FILE *elf_file, uint8_t *pHead, int little_endian)   \
26 {                                                                            \
27 	ElfType                      ret          = ELFSTATIC;               \
28 	Minijail_Elf ## bit ## _Ehdr *pHeader     = NULL;                    \
29 	Minijail_Elf ## bit ## _Phdr pheader;                                \
30 	uint32_t                     i            = 0;                       \
31 	                                                                     \
32 	if (!elf_file || !pHead)                                             \
33 		return ELFERROR;                                             \
34 	                                                                     \
35 	pHeader = (Minijail_Elf ## bit ## _Ehdr *)pHead;                     \
36 	if (little_endian) {                                                 \
37 		pHeader->e_phoff = le ## bit ## toh(pHeader->e_phoff);       \
38 		pHeader->e_phentsize = le16toh(pHeader->e_phentsize);        \
39 		pHeader->e_phnum = le16toh(pHeader->e_phnum);                \
40 	} else {                                                             \
41 		pHeader->e_phoff = be ## bit ## toh(pHeader->e_phoff);       \
42 		pHeader->e_phentsize = be16toh(pHeader->e_phentsize);        \
43 		pHeader->e_phnum = be16toh(pHeader->e_phnum);                \
44 	}                                                                    \
45 	if (pHeader->e_phentsize != sizeof(Minijail_Elf ## bit ## _Phdr))    \
46 		return ELFERROR;                                             \
47 	                                                                     \
48 	if (fseek(elf_file, pHeader->e_phoff, SEEK_SET) != 0)                \
49 		return ELFERROR;                                             \
50 	                                                                     \
51 	for (i = 0; i < pHeader->e_phnum; i++) {                             \
52 		if (fread(&pheader, sizeof(pheader), 1, elf_file) == 1) {    \
53 			if (pheader.p_type == PT_INTERP) {                   \
54 				ret = ELFDYNAMIC;                            \
55 				break;                                       \
56 			}                                                    \
57 		} else {                                                     \
58 			ret = ELFERROR;                                      \
59 			break;                                               \
60 		}                                                            \
61 	}                                                                    \
62 	return ret;                                                          \
63 }
64 parseElftemplate(64)
65 parseElftemplate(32)
66 
67 /* Public function to determine the linkage of an ELF. */
get_elf_linkage(const char * path)68 ElfType get_elf_linkage(const char *path)
69 {
70 	ElfType ret = ELFERROR;
71 	attribute_cleanup_fp FILE *elf_file = NULL;
72 	uint8_t pHeader[HEADERSIZE] = "";
73 
74 	elf_file = fopen(path, "re");
75 	if (elf_file) {
76 		if (fread(pHeader, 1, HEADERSIZE, elf_file) == HEADERSIZE) {
77 			if (is_elf_magic(pHeader)) {
78 				if ((pHeader[EI_DATA] == ELFDATA2LSB) &&
79 				    (pHeader[EI_CLASS] == ELFCLASS64)) {
80 					/* 64-bit little endian. */
81 					ret = parseElf64(elf_file, pHeader, 1);
82 				} else if ((pHeader[EI_DATA] == ELFDATA2MSB) &&
83 					  (pHeader[EI_CLASS] == ELFCLASS64)) {
84 					/* 64-bit big endian. */
85 					ret = parseElf64(elf_file, pHeader, 0);
86 				} else if ((pHeader[EI_DATA] == ELFDATA2LSB) &&
87 					  (pHeader[EI_CLASS] == ELFCLASS32)) {
88 					/* 32-bit little endian. */
89 					ret = parseElf32(elf_file, pHeader, 1);
90 				} else if ((pHeader[EI_DATA] == ELFDATA2MSB) &&
91 					  (pHeader[EI_CLASS] == ELFCLASS32)) {
92 					/* 32-bit big endian. */
93 					ret = parseElf32(elf_file, pHeader, 0);
94 				}
95 			} else {
96 				/*
97 				 * The binary is not an ELF. We assume it's a
98 				 * script. We should parse the #! line and
99 				 * check the interpreter to guard against
100 				 * static interpreters escaping the sandbox.
101 				 * As Minijail is only called from the rootfs
102 				 * it was deemed not necessary to check this.
103 				 * So we will just let execve(2) decide if this
104 				 * is valid.
105 				 */
106 				ret = ELFDYNAMIC;
107 			}
108 		} else {
109 			/*
110 			 * The file is smaller than |HEADERSIZE| bytes.
111 			 * We assume it's a short script. See above for
112 			 * reasoning on scripts.
113 			 */
114 			ret = ELFDYNAMIC;
115 		}
116 	}
117 	return ret;
118 }
119