add initial version of ujail and utrace
[project/procd.git] / jail / preload.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/types.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <string.h>
20 #include <dlfcn.h>
21 #include <syslog.h>
22
23 #include "seccomp.h"
24 #include "../preload.h"
25
26 static main_t __main__;
27
28 static int __preload_main__(int argc, char **argv, char **envp)
29 {
30         uid_t uid = getuid();
31         char *env_file = getenv("SECCOMP_FILE");
32
33         if (uid) {
34                 INFO("preload-seccomp: %s: not root, cannot install seccomp filter\n", *argv);
35                 return -1;
36         }
37
38         if (install_syscall_filter(*argv, env_file))
39                 return -1;
40
41         unsetenv("LD_PRELOAD");
42         unsetenv("SECCOMP_FILE");
43
44         return (*__main__)(argc, argv, envp);
45 }
46
47 int __libc_start_main(main_t main,
48                         int argc,
49                         char **argv,
50                         ElfW(auxv_t) *auxvec,
51                         __typeof (main) init,
52                         void (*fini) (void),
53                         void (*rtld_fini) (void),
54                         void *stack_end)
55 {
56         start_main_t __start_main__;
57
58         __start_main__ = dlsym(RTLD_NEXT, "__libc_start_main");
59         if (!__start_main__)
60                 INFO("failed to find __libc_start_main %s\n", dlerror());
61
62         __main__ = main;
63
64         return (*__start_main__)(__preload_main__, argc, argv, auxvec,
65                 init, fini, rtld_fini, stack_end);
66 }
67
68 void __uClibc_main(main_t main,
69                         int argc,
70                         char **argv,
71                         void (*app_init)(void),
72                         void (*app_fini)(void),
73                         void (*rtld_fini)(void),
74                         void *stack_end attribute_unused)
75 {
76         uClibc_main __start_main__;
77
78         __start_main__ = dlsym(RTLD_NEXT, "__uClibc_main");
79         if (!__start_main__)
80                 INFO("failed to find __uClibc_main %s\n", dlerror());
81
82         __main__ = main;
83
84         return (*__start_main__)(__preload_main__, argc, argv,
85                 app_init, app_fini, rtld_fini, stack_end);
86 }