jail: cleanup include
[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 #include <sys/mman.h>
16
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <libgen.h>
24 #include <glob.h>
25 #include <elf.h>
26
27 #include <libubox/utils.h>
28
29 #include "elf.h"
30
31 struct avl_tree libraries;
32 static LIST_HEAD(library_paths);
33
34 void alloc_library_path(const char *path)
35 {
36 struct library_path *p;
37 char *_path;
38
39 p = calloc_a(sizeof(*p),
40 &_path, strlen(path) + 1);
41 if (!p)
42 return;
43
44 p->path = strcpy(_path, path);
45
46 list_add_tail(&p->list, &library_paths);
47 DEBUG("adding ld.so path %s\n", path);
48 }
49
50 static void alloc_library(const char *path, const char *name)
51 {
52 struct library *l;
53 char *_name, *_path;
54
55 l = calloc_a(sizeof(*l),
56 &_path, strlen(path) + 1,
57 &_name, strlen(name) + 1);
58 if (!l)
59 return;
60
61 l->avl.key = l->name = strcpy(_name, name);
62 l->path = strcpy(_path, path);
63
64 avl_insert(&libraries, &l->avl);
65 DEBUG("adding library %s/%s\n", path, name);
66 }
67
68 static int elf_open(char **dir, char *file)
69 {
70 struct library_path *p;
71 char path[256];
72 int fd = -1;
73
74 *dir = NULL;
75
76 list_for_each_entry(p, &library_paths, list) {
77 if (strlen(p->path))
78 snprintf(path, sizeof(path), "%s/%s", p->path, file);
79 else
80 strncpy(path, file, sizeof(path));
81 fd = open(path, O_RDONLY);
82 if (fd >= 0) {
83 *dir = p->path;
84 break;
85 }
86 }
87
88 if (fd == -1)
89 fd = open(file, O_RDONLY);
90
91 return fd;
92 }
93
94 char* find_lib(char *file)
95 {
96 struct library *l;
97 static char path[256];
98 const char *p;
99
100 l = avl_find_element(&libraries, file, l, avl);
101 if (!l)
102 return NULL;
103
104 p = l->path;
105 if (strstr(p, "local"))
106 p = "/lib";
107
108 snprintf(path, sizeof(path), "%s/%s", p, file);
109
110 return path;
111 }
112
113 static int elf64_find_section(char *map, unsigned int type, unsigned int *offset, unsigned int *size, unsigned int *vaddr)
114 {
115 Elf64_Ehdr *e;
116 Elf64_Phdr *ph;
117 int i;
118
119 e = (Elf64_Ehdr *) map;
120 ph = (Elf64_Phdr *) (map + e->e_phoff);
121
122 for (i = 0; i < e->e_phnum; i++) {
123 if (ph[i].p_type == type) {
124 *offset = ph[i].p_offset;
125 if (size)
126 *size = ph[i].p_filesz;
127 if (vaddr)
128 *vaddr = ph[i].p_vaddr;
129 return 0;
130 }
131 }
132
133 return -1;
134 }
135
136 static int elf32_find_section(char *map, unsigned int type, unsigned int *offset, unsigned int *size, unsigned int *vaddr)
137 {
138 Elf32_Ehdr *e;
139 Elf32_Phdr *ph;
140 int i;
141
142 e = (Elf32_Ehdr *) map;
143 ph = (Elf32_Phdr *) (map + e->e_phoff);
144
145 for (i = 0; i < e->e_phnum; i++) {
146 if (ph[i].p_type == type) {
147 *offset = ph[i].p_offset;
148 if (size)
149 *size = ph[i].p_filesz;
150 if (vaddr)
151 *vaddr = ph[i].p_vaddr;
152 return 0;
153 }
154 }
155
156 return -1;
157 }
158
159 static int elf_find_section(char *map, unsigned int type, unsigned int *offset, unsigned int *size, unsigned int *vaddr)
160 {
161 int clazz = map[EI_CLASS];
162
163 if (clazz == ELFCLASS32)
164 return elf32_find_section(map, type, offset, size, vaddr);
165 else if (clazz == ELFCLASS64)
166 return elf64_find_section(map, type, offset, size, vaddr);
167
168 ERROR("unknown elf format %d\n", clazz);
169
170 return -1;
171 }
172
173 static int elf32_scan_dynamic(char *map, int dyn_offset, int dyn_size, int load_offset)
174 {
175 Elf32_Dyn *dynamic = (Elf32_Dyn *) (map + dyn_offset);
176 char *strtab = NULL;
177
178 while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
179 Elf32_Dyn *curr = dynamic;
180
181 dynamic++;
182 if (curr->d_tag != DT_STRTAB)
183 continue;
184
185 strtab = map + (curr->d_un.d_val - load_offset);
186 break;
187 }
188
189 if (!strtab)
190 return -1;
191
192 dynamic = (Elf32_Dyn *) (map + dyn_offset);
193 while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
194 Elf32_Dyn *curr = dynamic;
195
196 dynamic++;
197 if (curr->d_tag != DT_NEEDED)
198 continue;
199
200 if (elf_load_deps(&strtab[curr->d_un.d_val]))
201 return -1;
202 }
203
204 return 0;
205 }
206
207 static int elf64_scan_dynamic(char *map, int dyn_offset, int dyn_size, int load_offset)
208 {
209 Elf64_Dyn *dynamic = (Elf64_Dyn *) (map + dyn_offset);
210 char *strtab = NULL;
211
212 while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
213 Elf64_Dyn *curr = dynamic;
214
215 dynamic++;
216 if (curr->d_tag != DT_STRTAB)
217 continue;
218
219 strtab = map + (curr->d_un.d_val - load_offset);
220 break;
221 }
222
223 if (!strtab)
224 return -1;
225
226 dynamic = (Elf64_Dyn *) (map + dyn_offset);
227 while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
228 Elf64_Dyn *curr = dynamic;
229
230 dynamic++;
231 if (curr->d_tag != DT_NEEDED)
232 continue;
233
234 if (elf_load_deps(&strtab[curr->d_un.d_val]))
235 return -1;
236 }
237
238 return 0;
239 }
240
241 int elf_load_deps(char *library)
242 {
243 unsigned int dyn_offset, dyn_size;
244 unsigned int load_offset, load_vaddr;
245 struct stat s;
246 char *map = NULL, *dir = NULL;
247 int clazz, fd, ret = -1;
248
249 if (avl_find(&libraries, library))
250 return 0;
251
252 fd = elf_open(&dir, library);
253
254 if (fd < 0) {
255 ERROR("failed to open %s\n", library);
256 return -1;
257 }
258
259 if (fstat(fd, &s) == -1) {
260 ERROR("failed to stat %s\n", library);
261 ret = -1;
262 goto err_out;
263 }
264
265 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
266 if (map == MAP_FAILED) {
267 ERROR("failed to mmap %s\n", library);
268 ret = -1;
269 goto err_out;
270 }
271
272 if (elf_find_section(map, PT_LOAD, &load_offset, NULL, &load_vaddr)) {
273 ERROR("failed to load the .load section from %s\n", library);
274 ret = -1;
275 goto err_out;
276 }
277
278 if (elf_find_section(map, PT_DYNAMIC, &dyn_offset, &dyn_size, NULL)) {
279 ERROR("failed to load the .dynamic section from %s\n", library);
280 ret = -1;
281 goto err_out;
282 }
283
284 if (dir) {
285 alloc_library(dir, library);
286 } else {
287 char *elf = strdup(library);
288
289 alloc_library(dirname(elf), basename(library));
290 free(elf);
291 }
292 clazz = map[EI_CLASS];
293
294 if (clazz == ELFCLASS32)
295 ret = elf32_scan_dynamic(map, dyn_offset, dyn_size, load_vaddr - load_offset);
296 else if (clazz == ELFCLASS64)
297 ret = elf64_scan_dynamic(map, dyn_offset, dyn_size, load_vaddr - load_offset);
298
299 err_out:
300 if (map)
301 munmap(map, s.st_size);
302 close(fd);
303
304 return ret;
305 }
306
307 void load_ldso_conf(const char *conf)
308 {
309 FILE* fp = fopen(conf, "r");
310 char line[256];
311
312 if (!fp) {
313 DEBUG("failed to open %s\n", conf);
314 return;
315 }
316
317 while (!feof(fp)) {
318 int len;
319
320 if (!fgets(line, 256, fp))
321 break;
322 len = strlen(line);
323 if (len < 2)
324 continue;
325 if (*line == '#')
326 continue;
327 if (line[len - 1] == '\n')
328 line[len - 1] = '\0';
329 if (!strncmp(line, "include ", 8)) {
330 char *sep = strstr(line, " ");
331 glob_t gl;
332 int i;
333
334 if (!sep)
335 continue;;
336 while (*sep == ' ')
337 sep++;
338 if (glob(sep, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl)) {
339 ERROR("glob failed on %s\n", sep);
340 continue;
341 }
342 for (i = 0; i < gl.gl_pathc; i++)
343 load_ldso_conf(gl.gl_pathv[i]);
344 globfree(&gl);
345 } else {
346 struct stat s;
347
348 if (stat(line, &s))
349 continue;
350 alloc_library_path(line);
351 }
352 }
353
354 fclose(fp);
355 }