1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#define MARKER ".created_by_calibre_mount_helper"
#define DEV "/dev/"
#define MEDIA "/media/"
#define False 0
#define True 1
int exists(const char *path) {
struct stat file_info;
if (stat(path, &file_info) == 0) return True;
return False;
}
int get_root() {
int res;
res = setreuid(0, 0);
if (res != 0) return False;
if (setregid(0, 0) != 0) return False;
return True;
}
void ensure_root() {
if (!get_root()) {
fprintf(stderr, "Failed to get root.\n");
exit(EXIT_FAILURE);
}
}
void check_mount_point(const char *mp) {
char buffer[PATH_MAX+1];
if (mp == NULL || strlen(mp) < strlen(MEDIA)) {
fprintf(stderr, "Invalid arguments\n");
exit(EXIT_FAILURE);
}
if (exists(mp)) {
if (realpath(mp, buffer) == NULL) {
fprintf(stderr, "Unable to resolve mount path\n");
exit(EXIT_FAILURE);
}
if (strncmp(MEDIA, buffer, strlen(MEDIA)) != 0) {
fprintf(stderr, "Trying to operate on a mount point not under /media is not allowed\n");
exit(EXIT_FAILURE);
}
}
if (strncmp(MEDIA, mp, strlen(MEDIA)) != 0) {
fprintf(stderr, "Trying to operate on a mount point not under /media is not allowed\n");
exit(EXIT_FAILURE);
}
}
int do_mount(const char *dev, const char *mp) {
char options[1000], marker[2000];
#ifdef __NetBSD__
char uids[100], gids[100];
#endif
int errsv;
if (!exists(dev)) {
fprintf(stderr, "Specified device node does not exist\n");
return EXIT_FAILURE;
}
if (!exists(mp)) {
if (mkdir(mp, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) != 0) {
errsv = errno;
fprintf(stderr, "Failed to create mount point with error: %s\n", strerror(errsv));
}
}
/* only mount if mp is under /media */
mp = realpath(mp, NULL);
if (mp == NULL) {
fprintf(stderr, "realpath on mp failed.\n");
exit(EXIT_FAILURE);
}
if (strncmp(MEDIA, mp, strlen(MEDIA)) != 0) {
fprintf(stderr, "mount point is not under /media\n");
exit(EXIT_FAILURE);
}
snprintf(marker, 2000, "%s/%s", mp, MARKER);
if (!exists(marker)) {
int fd = creat(marker, S_IRUSR|S_IWUSR);
if (fd == -1) {
int errsv = errno;
fprintf(stderr, "Failed to create marker with error: %s\n", strerror(errsv));
return EXIT_FAILURE;
}
close(fd);
}
#ifdef __NetBSD__
snprintf(options, 1000, "rw,noexec,nosuid,sync,nodev");
snprintf(uids, 100, "%d", getuid());
snprintf(gids, 100, "%d", getgid());
#else
#ifdef __FreeBSD__
snprintf(options, 1000, "rw,noexec,nosuid,sync,-u=%d,-g=%d",getuid(),getgid());
#else
snprintf(options, 1000, "rw,noexec,nosuid,sync,nodev,quiet,shortname=mixed,uid=%d,gid=%d,umask=077,fmask=0177,dmask=0077,utf8,iocharset=iso8859-1", getuid(), getgid());
#endif
#endif
ensure_root();
#ifdef __NetBSD__
execlp("mount_msdos", "mount_msdos", "-u", uids, "-g", gids, "-o", options, dev, mp, NULL);
#else
#ifdef __FreeBSD__
execlp("mount", "mount", "-t", "msdosfs", "-o", options, dev, mp, NULL);
#else
execlp("mount", "mount", "-t", "auto", "-o", options, dev, mp, NULL);
#endif
#endif
errsv = errno;
fprintf(stderr, "Failed to mount with error: %s\n", strerror(errsv));
return EXIT_FAILURE;
}
int call_eject(const char *dev, const char *mp) {
int ret, pid, errsv, i, status = EXIT_FAILURE;
pid = fork();
if (pid == -1) {
fprintf(stderr, "Failed to fork\n");
exit(EXIT_FAILURE);
}
if (pid == 0) { /* Child process */
ensure_root();
#ifdef __NetBSD__
execlp("eject", "eject", dev, NULL);
#else
#ifdef __FreeBSD__
execlp("umount", "umount", dev, NULL);
#else
execlp("eject", "eject", "-s", dev, NULL);
#endif
#endif
/* execlp failed */
errsv = errno;
fprintf(stderr, "Failed to eject with error: %s\n", strerror(errsv));
exit(EXIT_FAILURE);
} else { /* Parent */
for (i = 0; i < 7; i++) {
sleep(1);
ret = waitpid(pid, &status, WNOHANG);
if (ret == -1) return False;
if (ret > 0) break;
}
return WIFEXITED(status) && WEXITSTATUS(status) == 0;
}
return False;
}
int call_umount(const char *dev, const char *mp) {
int ret, pid, errsv, i, status = EXIT_FAILURE;
pid = fork();
if (pid == -1) {
fprintf(stderr, "Failed to fork\n");
exit(EXIT_FAILURE);
}
if (pid == 0) { /* Child process */
ensure_root();
#ifdef __FreeBSD__
execlp("umount", "umount", mp, NULL);
#else
execlp("umount", "umount", "-l", mp, NULL);
#endif
/* execlp failed */
errsv = errno;
fprintf(stderr, "Failed to umount with error: %s\n", strerror(errsv));
exit(EXIT_FAILURE);
} else { /* Parent */
for (i = 0; i < 7; i++) {
sleep(1);
ret = waitpid(pid, &status, WNOHANG);
if (ret == -1) return False;
if (ret > 0) break;
}
return WIFEXITED(status) && WEXITSTATUS(status) == 0;
}
return False;
}
int cleanup_mount_point(const char *mp) {
char marker[2000];
int urt, rmd, errsv;
snprintf(marker, 2000, "%s/%s", mp, MARKER);
if (exists(marker)) {
urt = unlink(marker);
if (urt == -1) {
errsv = errno;
fprintf(stderr, "Failed to unlink marker: %s\n", strerror(errsv));
return EXIT_FAILURE;
}
}
rmd = rmdir(mp);
if (rmd == -1) {
errsv = errno;
fprintf(stderr, "Failed to remove mount point: %s\n", strerror(errsv));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int do_eject(const char *dev, const char *mp) {
int unmounted = False;
ensure_root();
unmounted = call_eject(dev, mp);
if (!unmounted) call_umount(dev, mp);
if (unmounted) return cleanup_mount_point(mp);
return EXIT_FAILURE;
}
int cleanup(const char *dev, const char *mp) {
ensure_root();
call_umount(dev, mp);
return cleanup_mount_point(mp);
}
void check_dev(const char *dev) {
char buffer[PATH_MAX+1];
struct stat file_info;
if (dev == NULL || strlen(dev) < strlen(DEV)) {
fprintf(stderr, "Invalid arguments\n");
exit(EXIT_FAILURE);
}
if (realpath(dev, buffer) == NULL) {
fprintf(stderr, "Unable to resolve dev path\n");
exit(EXIT_FAILURE);
}
if (strncmp(DEV, buffer, strlen(DEV)) != 0) {
fprintf(stderr, "Trying to operate on a dev node not under /dev\n");
exit(EXIT_FAILURE);
}
if (stat(dev, &file_info) != 0) {
fprintf(stderr, "stat call on dev node failed\n");
exit(EXIT_FAILURE);
}
if (!S_ISBLK(file_info.st_mode)) {
fprintf(stderr, "dev node is not a block device\n");
exit(EXIT_FAILURE);
}
}
int main(int argc, char** argv)
{
char *action, *dev, *mp, *temp;
int status = EXIT_FAILURE;
/*printf("Real UID\t= %d\n", getuid());
printf("Effective UID\t= %d\n", geteuid());
printf("Real GID\t= %d\n", getgid());
printf("Effective GID\t= %d\n", getegid());*/
if (argc != 4) {
fprintf(stderr, "Needs 3 arguments: action, device node and mount point\n");
exit(EXIT_FAILURE);
}
action = argv[1]; dev = argv[2]; mp = argv[3];
/* Ensure that PATH only contains system directories to prevent execution of
arbitrary executables as root */
if (setenv("PATH",
"/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin\0",
1) != 0) {
fprintf(stderr, "Failed to restrict PATH env var, aborting.\n");
exit(EXIT_FAILURE);
}
if (strncmp(action, "mount", 5) == 0) {
dev = realpath(argv[2], NULL);
if (dev == NULL) {
fprintf(stderr, "Failed to resolve device node.\n");
exit(EXIT_FAILURE);
}
temp = realpath(mp, NULL);
if (temp != NULL) mp = temp;
check_dev(dev); check_mount_point(mp);
status = do_mount(dev, mp);
} else if (strncmp(action, "eject", 5) == 0) {
dev = realpath(argv[2], NULL);
if (dev == NULL) {
fprintf(stderr, "Failed to resolve device node.\n");
exit(EXIT_FAILURE);
}
temp = realpath(mp, NULL);
if (temp == NULL) {
fprintf(stderr, "Mount point does not exist\n");
exit(EXIT_FAILURE);
}
mp = temp;
check_dev(dev); check_mount_point(mp);
status = do_eject(dev, mp);
} else if (strncmp(action, "cleanup", 7) == 0) {
temp = realpath(mp, NULL);
if (temp == NULL) {
fprintf(stderr, "Mount point does not exist\n");
exit(EXIT_FAILURE);
}
mp = temp;
check_mount_point(mp);
status = cleanup(dev, mp);
} else {
fprintf(stderr, "Unrecognized action: must be mount, eject or cleanup\n");
}
return status;
}
|