-
Notifications
You must be signed in to change notification settings - Fork 18
/
libdropbox_fs_fix.c
123 lines (103 loc) · 3.37 KB
/
libdropbox_fs_fix.c
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
/*
* Fix the filesystem detection in the Linux Dropbox client
* Copyright (C) 2018 Marco Leogrande
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <fcntl.h>
#include <libgen.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/magic.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/vfs.h>
// Necessary on older glibc (?). Borrowed from asm-generic/fcntl.h.
#ifndef O_TMPFILE
#define __O_TMPFILE 020000000
#define O_TMPFILE (__O_TMPFILE | O_DIRECTORY)
#define O_TMPFILE_MASK (__O_TMPFILE | O_DIRECTORY | O_CREAT)
#endif
#define CANARY_FILE_PREFIX "/tmp"
char *canary_path = NULL;
#define PROC_FILESYSTEMS "/proc/filesystems"
int (*orig_statfs64)(const char *path, struct statfs64 *buf) = NULL;
int statfs64(const char *path, struct statfs64 *buf) {
if (orig_statfs64 == NULL) {
orig_statfs64 = dlsym(RTLD_NEXT, "statfs64");
if (orig_statfs64 == NULL) {
fprintf(stderr, "dlsym failed: %s\n", dlerror());
return -1;
}
}
int retval = orig_statfs64(path, buf);
if (retval == 0) {
buf->f_type = EXT4_SUPER_MAGIC;
// Assume this is the Dropbox root, learn the canary path prefix.
if (canary_path == NULL) {
char *dup = strdup(path);
char *dname = dirname(dup);
canary_path = malloc(strlen(dname) + strlen(CANARY_FILE_PREFIX) + 1);
if (canary_path) {
strcpy(canary_path, dname);
strcat(canary_path, CANARY_FILE_PREFIX);
#ifdef DEBUG
fprintf(stderr, " LEARNED canary path: %s\n", canary_path);
#endif
}
free(dup);
}
}
return retval;
}
# define OPEN_NEEDS_MODE(flags) \
(((flags) & O_CREAT) != 0 || ((flags) & O_TMPFILE) == O_TMPFILE)
int (*orig_open64)(const char *pathname, int flags, ...)= NULL;
int open64(const char *pathname, int flags, ...) {
if (orig_open64 == NULL) {
orig_open64 = dlsym(RTLD_NEXT, "open64");
if (orig_open64 == NULL) {
fprintf(stderr, "dlsym failed: %s\n", dlerror());
return -1;
}
}
// Reject opens of canary files.
if (canary_path && strncmp(pathname, canary_path, strlen(canary_path)) == 0) {
#ifdef DEBUG
fprintf(stderr, " REJECT canary path: %s\n", pathname);
#endif
return -1;
}
// Reject opens of filesystem pseudofile.
if (strncmp(pathname, PROC_FILESYSTEMS, strlen(PROC_FILESYSTEMS)) == 0) {
#ifdef DEBUG
fprintf(stderr, " REJECT read pseudofile: %s\n", pathname);
#endif
return -1;
}
// Passthru - select target function arity depending on flags. Code
// borrowed from fcntl.h.
if (OPEN_NEEDS_MODE(flags)) {
va_list arg;
va_start (arg, flags);
int mode = va_arg (arg, int);
va_end (arg);
return orig_open64(pathname, flags, mode);
}
return orig_open64(pathname, flags);
}