diff options
author | 2017-05-20 02:10:35 +0530 | |
---|---|---|
committer | 2017-05-20 02:10:35 +0530 | |
commit | 3916c7f46236e4d477b8ba680ed7cc456ca5c1f9 (patch) | |
tree | f6f03c235e9bdad059da208de4bfc309f5d26e9d /src/floader/loader.c | |
parent | bringing back private-dev in chromium profile - /dev/video handled by private... (diff) | |
download | firejail-3916c7f46236e4d477b8ba680ed7cc456ca5c1f9.tar.gz firejail-3916c7f46236e4d477b8ba680ed7cc456ca5c1f9.tar.zst firejail-3916c7f46236e4d477b8ba680ed7cc456ca5c1f9.zip |
added floader files
Diffstat (limited to 'src/floader/loader.c')
-rw-r--r-- | src/floader/loader.c | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/src/floader/loader.c b/src/floader/loader.c new file mode 100644 index 000000000..0970794e9 --- /dev/null +++ b/src/floader/loader.c | |||
@@ -0,0 +1,161 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2017 Madura A. (madura.x86@gmail.com) | ||
3 | * | ||
4 | */ | ||
5 | #include <sys/types.h> | ||
6 | #include <sys/stat.h> | ||
7 | #include <sys/mman.h> | ||
8 | #include <fcntl.h> | ||
9 | #include <unistd.h> | ||
10 | |||
11 | #include <string.h> | ||
12 | #include <stdio.h> | ||
13 | #include <stdlib.h> | ||
14 | #include <ctype.h> | ||
15 | |||
16 | #define MAX_MATCHES 32 | ||
17 | #define MAX_ARGS 1024 | ||
18 | #define MAX_ARGS_LEN 4096 | ||
19 | static void loader_main() __attribute__((constructor)); | ||
20 | |||
21 | char cmdline[MAX_ARGS_LEN]; | ||
22 | char *args[MAX_ARGS]; | ||
23 | char loader[] = "firejail"; | ||
24 | char confFile[256]; | ||
25 | char *names[MAX_MATCHES]; | ||
26 | |||
27 | #ifdef DEBUG | ||
28 | #define DBG printf | ||
29 | #else | ||
30 | #define DBG | ||
31 | #endif | ||
32 | void remove_trailing_spaces(char *str) | ||
33 | { | ||
34 | while (!isspace(*str)) | ||
35 | { | ||
36 | str++; | ||
37 | } | ||
38 | |||
39 | while (*str != '\0') | ||
40 | { | ||
41 | *str = '\0'; | ||
42 | str++; | ||
43 | } | ||
44 | } | ||
45 | |||
46 | void read_cmdline() | ||
47 | { | ||
48 | int fd = open("/proc/self/cmdline", O_RDONLY); | ||
49 | ssize_t ret = 0, total = 0; | ||
50 | char* wcmdbuf = cmdline; | ||
51 | while ((ret = read(fd, wcmdbuf, 1)) != 0) | ||
52 | { | ||
53 | wcmdbuf++; | ||
54 | total += ret; | ||
55 | if (total > MAX_ARGS_LEN) | ||
56 | { | ||
57 | printf("Not enough memory\n"); | ||
58 | close(fd); | ||
59 | return ; | ||
60 | } | ||
61 | } | ||
62 | close(fd); | ||
63 | } | ||
64 | |||
65 | void make_args() | ||
66 | { | ||
67 | int cI = 0, argI=0; | ||
68 | char* argstart = &cmdline[0]; | ||
69 | for (;cI<MAX_ARGS_LEN;cI++) | ||
70 | { | ||
71 | if (cmdline[cI] == '\0') | ||
72 | { | ||
73 | args[argI]= argstart; | ||
74 | argstart = &cmdline[cI+1]; | ||
75 | argI++; | ||
76 | if (*argstart == '\0') | ||
77 | { | ||
78 | break; | ||
79 | } | ||
80 | } | ||
81 | } | ||
82 | args[argI] = argstart; | ||
83 | argI++; | ||
84 | args[argI] = NULL; | ||
85 | } | ||
86 | |||
87 | void loader_main() | ||
88 | { | ||
89 | snprintf(confFile, 255, "%s/.loader.conf", getenv("HOME")); | ||
90 | |||
91 | struct stat confFileStat; | ||
92 | |||
93 | stat(confFile, &confFileStat); | ||
94 | |||
95 | int confFd = open(confFile, O_RDONLY); | ||
96 | |||
97 | if (confFd == -1) | ||
98 | { | ||
99 | close(confFd); | ||
100 | return; | ||
101 | } | ||
102 | char* conf = (char*) malloc(confFileStat.st_size); | ||
103 | if (conf == NULL) | ||
104 | { | ||
105 | close(confFd); | ||
106 | return; | ||
107 | } | ||
108 | ssize_t ret = read(confFd, conf, confFileStat.st_size); | ||
109 | if (ret == -1) | ||
110 | { | ||
111 | close(confFd); | ||
112 | return; | ||
113 | } | ||
114 | |||
115 | close(confFd); | ||
116 | size_t fI = 0; | ||
117 | int matchId = 0; | ||
118 | names[matchId] = conf; | ||
119 | matchId++; | ||
120 | for (;fI < confFileStat.st_size-1;fI++) | ||
121 | { | ||
122 | if (conf[fI] == ',') | ||
123 | { | ||
124 | names[matchId] = &conf[fI+1]; | ||
125 | conf[fI] = '\0'; | ||
126 | |||
127 | matchId++; | ||
128 | } | ||
129 | } | ||
130 | |||
131 | remove_trailing_spaces(names[matchId-1]); | ||
132 | |||
133 | read_cmdline(); | ||
134 | |||
135 | make_args(); | ||
136 | |||
137 | #ifdef DEBUG | ||
138 | int xarg=0; | ||
139 | while (args[xarg] != NULL) | ||
140 | { | ||
141 | DBG(".%s\n", args[xarg]); | ||
142 | xarg++; | ||
143 | } | ||
144 | #endif | ||
145 | |||
146 | int x; | ||
147 | |||
148 | for (x = 0;x<matchId;x++) | ||
149 | { | ||
150 | DBG("%s\n",names[x]); | ||
151 | if (strstr(args[0], names[x]) != NULL) | ||
152 | { | ||
153 | DBG("highjack!\n"); | ||
154 | |||
155 | free(conf); | ||
156 | |||
157 | execvp(loader, args ); | ||
158 | } | ||
159 | } | ||
160 | |||
161 | } | ||