ujail: DT_STRTAB uses d_ptr in d_un union (not d_val)
[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
104 l = avl_find_element(&libraries, file, l, avl);
105 if (!l)
106 return NULL;
107
108 snprintf(path, sizeof(path), "%s/%s", l->path, file);
109
110 return path;
111 }
112
113 static int elf64_find_section(const 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(const 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(const 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(const char *map, int dyn_offset, int dyn_size, int load_offset)
174 {
175 Elf32_Dyn *dynamic = (Elf32_Dyn *) (map + dyn_offset);
176 const 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_ptr - 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(const char *map, int dyn_offset, int dyn_size, int load_offset)
208 {
209 Elf64_Dyn *dynamic = (Elf64_Dyn *) (map + dyn_offset);
210 const 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_ptr - 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(const 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 *elf1 = strdup(library);
288 char *elf2 = strdup(library);
289
290 alloc_library(dirname(elf1), basename(elf2));
291 free(elf1);
292 free(elf2);
293 }
294 clazz = map[EI_CLASS];
295
296 if (clazz == ELFCLASS32)
297 ret = elf32_scan_dynamic(map, dyn_offset, dyn_size, load_vaddr - load_offset);
298 else if (clazz == ELFCLASS64)
299 ret = elf64_scan_dynamic(map, dyn_offset, dyn_size, load_vaddr - load_offset);
300
301 err_out:
302 if (map)
303 munmap(map, s.st_size);
304 close(fd);
305
306 return ret;
307 }
308
309 static void load_ldso_conf(const char *conf)
310 {
311 FILE* fp = fopen(conf, "r");
312 char line[PATH_MAX];
313
314 if (!fp) {
315 DEBUG("failed to open %s\n", conf);
316 return;
317 }
318
319 while (!feof(fp)) {
320 int len;
321
322 if (!fgets(line, sizeof(line), fp))
323 break;
324 len = strlen(line);
325 if (len < 2)
326 continue;
327 if (*line == '#')
328 continue;
329 if (line[len - 1] == '\n')
330 line[len - 1] = '\0';
331 if (!strncmp(line, "include ", 8)) {
332 char *sep = strstr(line, " ");
333 glob_t gl;
334 int i;
335
336 if (!sep)
337 continue;;
338 while (*sep == ' ')
339 sep++;
340 if (glob(sep, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl)) {
341 ERROR("glob failed on %s\n", sep);
342 continue;
343 }
344 for (i = 0; i < gl.gl_pathc; i++)
345 load_ldso_conf(gl.gl_pathv[i]);
346 globfree(&gl);
347 } else {
348 alloc_library_path(line);
349 }
350 }
351
352 fclose(fp);
353 }
354
355 void init_library_search(void)
356 {
357 avl_init(&libraries, avl_strcmp, false, NULL);
358 alloc_library_path("/lib");
359 alloc_library_path("/lib64");
360 alloc_library_path("/usr/lib");
361 load_ldso_conf("/etc/ld.so.conf");
362 }