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