-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibpathtrap.c
270 lines (239 loc) · 8.15 KB
/
libpathtrap.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
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
/*
* Copyright (c) 2020 Nutanix Inc. All rights reserved.
*
* Authors: Thanos Makatos <thanos@nutanix.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Nutanix nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*/
/*
* Wrapper for some system calls.
*
* FIXME for each syscall we need to check whether the fd is ours, so we have to
* repeat the same code in every function. To avoid this we can simply use
* something similar to https://github.com/pmem/syscall_intercept. It mentions
* some limitations so we need to check that it works.
*
* FIXME cleanup includes
*/
#define _GNU_SOURCE
#include <unistd.h>
#include <dlfcn.h>
#include <sys/mman.h>
#include <assert.h>
#include <strings.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <search.h>
#include <inttypes.h>
#include <stdlib.h>
#include <sys/param.h>
#include <stdbool.h>
#include "libpathtrap.h"
static void *open_fds = NULL;
static int compar(const void *a, const void *b) {
return ((struct fake_fd*)a)->fd - ((struct fake_fd*)b)->fd;
}
static struct fake_fd** lookup(int fd) {
struct fake_fd fake_fd = {.fd = fd};
return tfind(&fake_fd, &open_fds, compar);
}
static bool should_trap(const char *pathname) {
if (ops.should_trap)
return ops.should_trap(pathname);
return false;
}
int open_fake(const char *pathname, int flags, void *priv) {
int fd;
struct fake_fd *fake_fd;
if (!(fake_fd = malloc(sizeof *fake_fd)))
return -1;
if (asprintf(&fake_fd->pathname, "[%s]", pathname) == -1) {
free(fake_fd);
return -1;
}
if ((fd = ops.open(fake_fd, pathname, flags, priv)) == -1) {
assert(priv);
free(fake_fd);
return fd;
}
fake_fd->fd = fd;
tsearch(fake_fd, &open_fds, &compar);
return fd;
}
int open(const char *pathname, int flags, ...) {
if (!ops.open || !should_trap(pathname)) {
__real_open = dlsym(RTLD_NEXT, "open");
assert(__real_open);
return __real_open(pathname, flags);
}
return open_fake(pathname, flags, NULL);
}
/*
* FIXME open64 is supposed to pass O_LARGEFILE but it seems it gets translated
* to a directory flag.
*/
int open64(const char *pathname, int flags, ...) {
/* FIXME O_LARGEFILE == 0200000? */
return open(pathname, flags);
}
int close(int fd) {
if (ops.close) {
struct fake_fd **fake_fd = lookup(fd);
if (fake_fd) {
int ret = ops.close(*fake_fd);
if (ret == -1)
return ret;
tdelete(fake_fd, &open_fds, compar);
free(*fake_fd);
return ret;
}
}
__real_close = dlsym(RTLD_NEXT, "close");
assert(__real_close);
return __real_close(fd);
}
int __xstat(int __ver, const char *__filename, struct stat *__stat_buf) {
/*
* FIXME for some reason we get a SEGFAULT if we initialize
* __real___xstat in ctor
*/
int (*__real___xstat)(int, const char*, struct stat*) = dlsym(RTLD_NEXT, "__xstat");
if (!ops.__xstat || !should_trap(__filename))
return __real___xstat(__ver, __filename, __stat_buf);
return ops.__xstat(__ver, __filename, __stat_buf);
}
ssize_t _read(int fd, void *buf, size_t count, off_t *offset) {
if (ops.read) {
struct fake_fd **fake_fd = lookup(fd);
if (fake_fd)
return ops.read(*fake_fd, buf, count, offset);
}
if (offset) {
__real_pread = dlsym(RTLD_NEXT, "pread");
assert(__real_pread);
return __real_pread(fd, buf, count, *offset);
}
__real_read = dlsym(RTLD_NEXT, "read");
assert(__real_read);
return __real_read(fd, buf, count);
}
ssize_t read(int fd, void *buf, size_t count) {
return _read(fd, buf, count, NULL);
}
size_t __read_chk(int fd, void * buf, size_t nbytes, size_t buflen __attribute__((unused))) {
return read(fd, buf, nbytes);
}
ssize_t pread(int fd, void *buf, size_t count, off_t offset) {
return _read(fd, buf, count, &offset);
}
ssize_t pread64(int fd, void *buf, size_t count, off_t offset) {
return pread(fd, buf, count, offset);
}
size_t __pread64_chk(int fd, void * buf, size_t nbytes, off64_t offset, size_t buflen __attribute__((unused))) {
return pread64(fd, buf, nbytes, offset);
}
ssize_t __pread_chk(int fd, void * buf, size_t nbytes, off_t offset, size_t buflen __attribute__((unused))) {
return pread(fd, buf, nbytes, offset);
}
ssize_t _write(int fd, const void *buf, size_t count, off_t *offset) {
if (ops.write) {
struct fake_fd **fake_fd = lookup(fd);
if (fake_fd)
return ops.write(*fake_fd, buf, count, offset);
}
if (offset) {
__real_pwrite = dlsym(RTLD_NEXT, "pwrite");
assert(__real_pwrite);
return __real_pwrite(fd, buf, count, *offset);
}
__real_write = dlsym(RTLD_NEXT, "write");
assert(__real_write);
return __real_write(fd, buf, count);
}
ssize_t write(int fd, const void *buf, size_t count) {
return _write(fd, buf, count, NULL);
}
ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset) {
return _write(fd, buf, count, &offset);
}
ssize_t pwrite64(int fd, const void *buf, size_t count, off_t offset) {
return _write(fd, buf, count, &offset);
}
int __xstat64(int __ver, const char *__filename, struct stat64 *__stat_buf) {
if (!ops.__xstat64 || !should_trap(__filename)) {
__real___xstat64 = dlsym(RTLD_NEXT, "__xstat64");
assert(__real___xstat64);
return __real___xstat64(__ver, __filename, __stat_buf);
}
return ops.__xstat64(__ver, __filename, __stat_buf);
}
int __lxstat64 (int __ver, const char *__filename, struct stat64 *__stat_buf) {
if (!ops.__lxstat64 || !should_trap(__filename)) {
__real___lxstat64 = dlsym(RTLD_NEXT, "__lxstat64");
assert(__real___lxstat64);
return __real___lxstat64(__ver, __filename, __stat_buf);
}
return ops.__lxstat64(__ver, __filename, __stat_buf);
}
ssize_t readlink(const char *pathname, char *buf, size_t bufsiz) {
if (!ops.readlink || !should_trap(pathname)) {
__real_readlink = dlsym(RTLD_NEXT, "readlink");
assert(__real_readlink);
return __real_readlink(pathname, buf, bufsiz);
}
return ops.readlink(pathname, buf, bufsiz);
}
int ioctl(int fd, unsigned int cmd, unsigned long args) {
if (ops.ioctl) {
struct fake_fd **fake_fd = lookup(fd);
if (fake_fd)
return ops.ioctl(*fake_fd, cmd, args);
}
__real_ioctl = dlsym(RTLD_NEXT, "ioctl");
assert(__real_ioctl);
return __real_ioctl(fd, cmd, args);
}
char *realpath(const char *path, char *resolved_path) {
if (!ops.realpath || !should_trap(path)) {
__real_realpath = dlsym(RTLD_NEXT, "realpath");
assert(__real_realpath);
return __real_realpath(path, resolved_path);
}
return ops.realpath(path, resolved_path);
}
void* mmap64(void *addr, size_t length, int prot, int flags, int fd, off64_t offset) {
if (ops.mmap64 && fd != -1) {
struct fake_fd **fake_fd = lookup(fd);
if (fake_fd)
return ops.mmap64(addr, length, prot, flags, *fake_fd, offset);
}
__real_mmap64 = dlsym(RTLD_NEXT, "mmap64");
assert(__real_mmap64);
return __real_mmap64(addr, length, prot, flags, fd, offset);
}
void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) {
return mmap64(addr, length, prot, flags, fd, offset);
}