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