f67515b196b4ae7af5a6007db3f366d25f8d5088
[project/procd.git] / jail / elf.c
1 /*
2 * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14 #define _GNU_SOURCE
15
16 #include <string.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <glob.h>
20 #include <elf.h>
21 #include <linux/limits.h>
22
23 #include <libubox/utils.h>
24
25 #include "elf.h"
26 #include "fs.h"
27 #include "log.h"
28
29 struct avl_tree libraries;
30 static LIST_HEAD(library_paths);
31
32 static void alloc_library_path(const char *path)
33 {
34 struct stat s;
35 if (stat(path, &s))
36 return;
37
38 struct library_path *p;
39 char *_path;
40
41 p = calloc_a(sizeof(*p),
42 &_path, strlen(path) + 1);
43 if (!p)
44 return;
45
46 p->path = strcpy(_path, path);
47
48 list_add_tail(&p->list, &library_paths);
49 DEBUG("adding ld.so path %s\n", path);
50 }
51
52 /*
53 * path = full path
54 * name = soname/avl key
55 */
56 void alloc_library(const char *path, const char *name)
57 {
58 struct library *l;
59 char *_name, *_path;
60
61 l = calloc_a(sizeof(*l),
62 &_path, strlen(path) + 1,
63 &_name, strlen(name) + 1);
64 if (!l)
65 return;
66
67 l->avl.key = l->name = strcpy(_name, name);
68 l->path = strcpy(_path, path);
69
70 avl_insert(&libraries, &l->avl);
71 DEBUG("adding library %s (%s)\n", path, name);
72 }
73
74 int lib_open(char **fullpath, const char *file)
75 {
76 struct library_path *p;
77 char path[PATH_MAX];
78 int fd = -1;
79
80 *fullpath = NULL;
81
82 list_for_each_entry(p, &library_paths, list) {
83 snprintf(path, sizeof(path), "%s/%s", p->path, file);
84 fd = open(path, O_RDONLY|O_CLOEXEC);
85 if (fd >= 0) {
86 *fullpath = strdup(path);
87 break;
88 }
89 }
90
91 return fd;
92 }
93
94 const char* find_lib(const char *file)
95 {
96 struct library *l;
97
98 l = avl_find_element(&libraries, file, l, avl);
99 if (!l)
100 return NULL;
101
102 return l->path;
103 }
104
105 static int elf64_find_section(const char *map, unsigned int type, unsigned int *offset, unsigned int *size, unsigned int *vaddr)
106 {
107 Elf64_Ehdr *e;
108 Elf64_Phdr *ph;
109 int i;
110
111 e = (Elf64_Ehdr *) map;
112 ph = (Elf64_Phdr *) (map + e->e_phoff);
113
114 for (i = 0; i < e->e_phnum; i++) {
115 if (ph[i].p_type == type) {
116 *offset = ph[i].p_offset;
117 if (size)
118 *size = ph[i].p_filesz;
119 if (vaddr)
120 *vaddr = ph[i].p_vaddr;
121 return 0;
122 }
123 }
124
125 return -1;
126 }
127
128 static int elf32_find_section(const char *map, unsigned int type, unsigned int *offset, unsigned int *size, unsigned int *vaddr)
129 {
130 Elf32_Ehdr *e;
131 Elf32_Phdr *ph;
132 int i;
133
134 e = (Elf32_Ehdr *) map;
135 ph = (Elf32_Phdr *) (map + e->e_phoff);
136
137 for (i = 0; i < e->e_phnum; i++) {
138 if (ph[i].p_type == type) {
139 *offset = ph[i].p_offset;
140 if (size)
141 *size = ph[i].p_filesz;
142 if (vaddr)
143 *vaddr = ph[i].p_vaddr;
144 return 0;
145 }
146 }
147
148 return -1;
149 }
150
151 static int elf_find_section(const char *map, unsigned int type, unsigned int *offset, unsigned int *size, unsigned int *vaddr)
152 {
153 int clazz = map[EI_CLASS];
154
155 if (clazz == ELFCLASS32)
156 return elf32_find_section(map, type, offset, size, vaddr);
157 else if (clazz == ELFCLASS64)
158 return elf64_find_section(map, type, offset, size, vaddr);
159
160 ERROR("unknown elf format %d\n", clazz);
161
162 return -1;
163 }
164
165 static int elf32_scan_dynamic(const char *map, int dyn_offset, int dyn_size, int load_offset)
166 {
167 Elf32_Dyn *dynamic = (Elf32_Dyn *) (map + dyn_offset);
168 const char *strtab = NULL;
169
170 while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
171 Elf32_Dyn *curr = dynamic;
172
173 dynamic++;
174 if (curr->d_tag != DT_STRTAB)
175 continue;
176
177 strtab = map + (curr->d_un.d_ptr - load_offset);
178 break;
179 }
180
181 if (!strtab)
182 return -1;
183
184 dynamic = (Elf32_Dyn *) (map + dyn_offset);
185 while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
186 Elf32_Dyn *curr = dynamic;
187
188 dynamic++;
189 if (curr->d_tag != DT_NEEDED)
190 continue;
191
192 if (add_path_and_deps(&strtab[curr->d_un.d_val], 1, -1, 1))
193 return -1;
194 }
195
196 return 0;
197 }
198
199 static int elf64_scan_dynamic(const char *map, int dyn_offset, int dyn_size, int load_offset)
200 {
201 Elf64_Dyn *dynamic = (Elf64_Dyn *) (map + dyn_offset);
202 const char *strtab = NULL;
203
204 while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
205 Elf64_Dyn *curr = dynamic;
206
207 dynamic++;
208 if (curr->d_tag != DT_STRTAB)
209 continue;
210
211 strtab = map + (curr->d_un.d_ptr - load_offset);
212 break;
213 }
214
215 if (!strtab)
216 return -1;
217
218 dynamic = (Elf64_Dyn *) (map + dyn_offset);
219 while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
220 Elf64_Dyn *curr = dynamic;
221
222 dynamic++;
223 if (curr->d_tag != DT_NEEDED)
224 continue;
225
226 if (add_path_and_deps(&strtab[curr->d_un.d_val], 1, -1, 1))
227 return -1;
228 }
229
230 return 0;
231 }
232
233 int elf_load_deps(const char *path, const char *map)
234 {
235 unsigned int dyn_offset, dyn_size;
236 unsigned int load_offset, load_vaddr;
237 unsigned int interp_offset;
238 #if defined(__mips__) && (__mips == 64)
239 static int gcc_mips64_bug_work_around;
240
241 gcc_mips64_bug_work_around = 1;
242 #endif
243 if (elf_find_section(map, PT_LOAD, &load_offset, NULL, &load_vaddr)) {
244 ERROR("failed to load the .load section from %s\n", path);
245 return -1;
246 }
247
248 if (elf_find_section(map, PT_DYNAMIC, &dyn_offset, &dyn_size, NULL)) {
249 ERROR("failed to load the .dynamic section from %s\n", path);
250 return -1;
251 }
252
253 if (elf_find_section(map, PT_INTERP, &interp_offset, NULL, NULL) == 0) {
254 add_path_and_deps(map+interp_offset, 1, -1, 0);
255 }
256
257 int clazz = map[EI_CLASS];
258
259 #if defined(__mips__) && (__mips == 64)
260 if (gcc_mips64_bug_work_around != 1) {
261 ERROR("compiler bug: GCC for MIPS64 should be fixed!\n");
262 return -1;
263 }
264 gcc_mips64_bug_work_around = 0;
265 #endif
266
267 if (clazz == ELFCLASS32)
268 return elf32_scan_dynamic(map, dyn_offset, dyn_size, load_vaddr - load_offset);
269 else if (clazz == ELFCLASS64)
270 return elf64_scan_dynamic(map, dyn_offset, dyn_size, load_vaddr - load_offset);
271
272 ERROR("unknown elf format %d\n", clazz);
273 return -1;
274 }
275
276 static void load_ldso_conf(const char *conf)
277 {
278 FILE* fp = fopen(conf, "r");
279 char line[PATH_MAX];
280
281 if (!fp) {
282 DEBUG("failed to open %s\n", conf);
283 return;
284 }
285
286 while (!feof(fp)) {
287 int len;
288
289 if (!fgets(line, sizeof(line), fp))
290 break;
291 len = strlen(line);
292 if (len < 2)
293 continue;
294 if (*line == '#')
295 continue;
296 if (line[len - 1] == '\n')
297 line[len - 1] = '\0';
298 if (!strncmp(line, "include ", 8)) {
299 char *sep = strstr(line, " ");
300 glob_t gl;
301 int i;
302
303 if (!sep)
304 continue;;
305 while (*sep == ' ')
306 sep++;
307 if (glob(sep, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl)) {
308 ERROR("glob failed on %s\n", sep);
309 continue;
310 }
311 for (i = 0; i < gl.gl_pathc; i++)
312 load_ldso_conf(gl.gl_pathv[i]);
313 globfree(&gl);
314 } else {
315 alloc_library_path(line);
316 }
317 }
318
319 fclose(fp);
320 }
321
322 void init_library_search(void)
323 {
324 avl_init(&libraries, avl_strcmp, false, NULL);
325 alloc_library_path("/lib");
326 alloc_library_path("/lib64");
327 alloc_library_path("/usr/lib");
328 load_ldso_conf("/etc/ld.so.conf");
329 }
330
331 void free_library_search(void)
332 {
333 struct library_path *p, *ptmp;
334 struct library *l, *tmp;
335
336 list_for_each_entry_safe(p, ptmp, &library_paths, list)
337 free(p);
338
339 avl_remove_all_elements(&libraries, l, avl, tmp)
340 free(l);
341 }