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