forked from ish-app/ish
-
Notifications
You must be signed in to change notification settings - Fork 12
/
fd.c
395 lines (353 loc) · 10.7 KB
/
fd.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include <stdlib.h>
#include <string.h>
#include "debug.h"
#include "kernel/calls.h"
#include "kernel/resource.h"
#include "kernel/fs.h"
#include "fs/poll.h"
#include "fs/fd.h"
#include "fs/inode.h"
struct fd *fd_create(const struct fd_ops *ops) {
struct fd *fd = malloc(sizeof(struct fd));
if (fd == NULL)
return NULL;
*fd = (struct fd) {};
fd->ops = ops;
fd->refcount = 1;
fd->flags = 0;
fd->mount = NULL;
fd->offset = 0;
fd->stat.ctime = (dword_t)time(NULL);
list_init(&fd->poll_fds);
lock_init(&fd->poll_lock, "fd_create_poll\0");
lock_init(&fd->lock, "fd_create\0");
cond_init(&fd->cond);
return fd;
}
struct fd *fd_retain(struct fd *fd) {
fd->refcount++;
return fd;
}
int fd_close(struct fd *fd) {
int err = 0;
if (--fd->refcount == 0) {
poll_cleanup_fd(fd);
if (fd->ops->close)
err = fd->ops->close(fd);
// see comment in close in kernel/fs.h
if (fd->mount && fd->mount->fs->close && fd->mount->fs->close != fd->ops->close) {
int new_err = fd->mount->fs->close(fd);
if (new_err < 0)
err = new_err;
}
if (fd->inode)
inode_release(fd->inode);
if (fd->mount)
mount_release(fd->mount);
free(fd);
}
return err;
}
static int fdtable_resize(struct fdtable *table, unsigned size);
struct fdtable *fdtable_new(int size) {
struct fdtable *fdt = malloc(sizeof(struct fdtable));
if (fdt == NULL)
return ERR_PTR(_ENOMEM);
fdt->refcount = 1;
fdt->size = 0;
fdt->files = NULL;
fdt->cloexec = NULL;
lock_init(&fdt->lock, "fdtable_new\0");
int err = fdtable_resize(fdt, size);
if (err < 0) {
free(fdt);
return ERR_PTR(err);
}
return fdt;
}
static int fdtable_close(struct fdtable *table, fd_t f);
// FIXME this looks like it has the classic refcount UAF
void fdtable_release(struct fdtable *table) {
// mkefee
lock(&table->lock, 0);
if (--table->refcount == 0) {
for (fd_t f = 0; (unsigned) f < table->size; f++) {
fdtable_close(table, f);
}
free(table->files);
free(table->cloexec);
unlock(&table->lock);
free(table);
} else {
unlock(&table->lock);
}
}
static int fdtable_resize(struct fdtable *table, unsigned size) {
// currently the only legitimate use of this is to expand the table
assert(size > table->size);
struct fd **files = malloc(sizeof(struct fd *) * size);
if (files == NULL)
return _ENOMEM;
memset(files, 0, sizeof(struct fd *) * size);
if (table->files)
memcpy(files, table->files, sizeof(struct fd *) * table->size);
bits_t *cloexec = malloc(BITS_SIZE(size));
if (cloexec == NULL) {
free(files);
return _ENOMEM;
}
memset(cloexec, 0, BITS_SIZE(size));
if (table->cloexec)
memcpy(cloexec, table->cloexec, BITS_SIZE(table->size));
free(table->files);
table->files = files;
free(table->cloexec);
table->cloexec = cloexec;
table->size = size;
return 0;
}
struct fdtable *fdtable_copy(struct fdtable *table) {
lock(&table->lock, 0);
int size = table->size;
struct fdtable *new_table = fdtable_new(size);
if (IS_ERR(new_table)) {
unlock(&table->lock);
return new_table;
}
memcpy(new_table->files, table->files, sizeof(struct fd *) * size);
for (fd_t f = 0; f < size; f++)
if (new_table->files[f])
new_table->files[f]->refcount++;
memcpy(new_table->cloexec, table->cloexec, BITS_SIZE(size));
unlock(&table->lock);
return new_table;
}
static int fdtable_expand(struct fdtable *table, fd_t max) {
unsigned size = max + 1;
if (size > rlimit(RLIMIT_NOFILE_))
return _EMFILE;
if (table->size >= size)
return 0;
return fdtable_resize(table, max + 1);
}
struct fd *fdtable_get(struct fdtable *table, fd_t f) {
if (f < 0 || (unsigned) f >= current->files->size)
return NULL;
return table->files[f];
}
struct fd *f_get(fd_t f) {
lock(¤t->files->lock, 0);
struct fd *fd = fdtable_get(current->files, f);
unlock(¤t->files->lock);
return fd;
}
static fd_t f_install_start(struct fd *fd, fd_t start) {
assert(start >= 0);
struct fdtable *table = current->files;
unsigned size = rlimit(RLIMIT_NOFILE_);
if (size > table->size)
size = table->size;
fd_t f;
for (f = start; (unsigned) f < size; f++)
if (table->files[f] == NULL)
break;
if ((unsigned) f >= size) {
int err = fdtable_expand(table, f);
if (err < 0)
f = err;
}
if (f >= 0) {
table->files[f] = fd;
bit_clear(f, table->cloexec);
} else {
fd_close(fd);
}
return f;
}
fd_t f_install(struct fd *fd, int flags) {
lock(¤t->files->lock, 0);
fd_t f = f_install_start(fd, 0);
if (f >= 0) {
if (flags & O_CLOEXEC_)
bit_set(f, current->files->cloexec);
if (flags & O_NONBLOCK_)
fd_setflags(fd, O_NONBLOCK_);
}
unlock(¤t->files->lock);
return f;
}
static int fdtable_close(struct fdtable *table, fd_t f) {
struct fd *fd = fdtable_get(table, f);
if (fd == NULL)
return _EBADF;
if (fd->inode != NULL) // temporary hack for files like sockets that right now don't have inodes but will eventually
file_lock_remove_owned_by(fd, table);
int err = fd_close(fd);
table->files[f] = NULL;
bit_clear(f, table->cloexec);
return err;
}
int f_close(fd_t f) {
lock(¤t->files->lock, 0);
int err = fdtable_close(current->files, f);
unlock(¤t->files->lock);
return err;
}
dword_t sys_close(fd_t f) {
STRACE("close(%d)", f);
return f_close(f);
}
void fdtable_do_cloexec(struct fdtable *table) {
lock(&table->lock, 0);
for (fd_t f = 0; (unsigned) f < table->size; f++)
if (bit_test(f, table->cloexec))
fdtable_close(table, f);
unlock(&table->lock);
}
#define F_DUPFD_ 0
#define F_GETFD_ 1
#define F_SETFD_ 2
#define F_GETFL_ 3
#define F_SETFL_ 4
#define F_GETLK_ 5
#define F_SETLK_ 6
#define F_SETLKW_ 7
#define F_GETLK64_ 12
#define F_SETLK64_ 13
#define F_SETLKW64_ 14
#define F_DUPFD_CLOEXEC_ 1030
dword_t sys_dup(fd_t f) {
STRACE("dup(%d)", f);
struct fd *fd = f_get(f);
if (fd == NULL)
return _EBADF;
fd->refcount++;
return f_install(fd, 0);
}
dword_t sys_dup3(fd_t f, fd_t new_f, int_t flags) {
STRACE("dup3(%d, %d, %d)", f, new_f, flags);
struct fdtable *table = current->files;
struct fd *fd = f_get(f);
if (fd == NULL)
return _EBADF;
int err = fdtable_expand(table, new_f);
if (err < 0)
return err;
fd_retain(fd);
f_close(new_f);
table->files[new_f] = fd;
if (flags & O_CLOEXEC_)
bit_set(new_f, table->cloexec);
return new_f;
}
dword_t sys_dup2(fd_t f, fd_t new_f) {
return sys_dup3(f, new_f, 0);
}
int fd_getflags(struct fd *fd) {
if (fd->ops->getflags)
return fd->ops->getflags(fd);
return fd->flags;
}
#define FD_ALLOWED_FLAGS (O_APPEND_ | O_NONBLOCK_)
int fd_setflags(struct fd *fd, int flags) {
if (fd->ops->setflags)
return fd->ops->setflags(fd, flags);
fd->flags = (fd->flags & ~FD_ALLOWED_FLAGS) | (flags & FD_ALLOWED_FLAGS);
return 0;
}
dword_t sys_fcntl(fd_t f, dword_t cmd, dword_t arg) {
struct fdtable *table = current->files;
struct fd *fd = f_get(f);
if (fd == NULL)
return _EBADF;
struct flock32_ flock32;
struct flock_ flock;
fd_t new_f;
int err;
switch (cmd) {
case F_DUPFD_:
STRACE("fcntl(%d, F_DUPFD, %d)", f, arg);
fd->refcount++;
return f_install_start(fd, arg);
case F_DUPFD_CLOEXEC_:
STRACE("fcntl(%d, F_DUPFD_CLOEXEC, %d)", f, arg);
fd->refcount++;
new_f = f_install_start(fd, arg);
bit_set(new_f, table->cloexec);
return new_f;
case F_GETFD_:
STRACE("fcntl(%d, F_GETFD)", f);
return bit_test(f, table->cloexec);
case F_SETFD_:
STRACE("fcntl(%d, F_SETFD, 0x%x)", f, arg);
if (arg & 1)
bit_set(f, table->cloexec);
else
bit_clear(f, table->cloexec);
return 0;
case F_GETFL_:
STRACE("fcntl(%d, F_GETFL)", f);
return fd_getflags(fd);
case F_SETFL_:
STRACE("fcntl(%d, F_SETFL, %#x)", f, arg);
return fd_setflags(fd, arg);
case F_GETLK_:
STRACE("fcntl(%d, F_GETLK, %#x)", f, arg);
if (user_read(arg, &flock32, sizeof(flock32)))
return _EFAULT;
flock.type = flock32.type;
flock.whence = flock32.whence;
flock.start = flock32.start;
flock.len = flock32.len;
flock.pid = flock32.pid;
err = fcntl_getlk(fd, &flock);
if (err >= 0) {
flock32.type = flock.type;
flock32.whence = flock.whence;
flock32.start = flock.start;
flock32.len = flock.len;
flock32.pid = flock.pid;
if (user_write(arg, &flock32, sizeof(flock32)))
return _EFAULT;
}
return err;
case F_GETLK64_:
STRACE("fcntl(%d, F_GETLK64, %#x)", f, arg);
if (user_read(arg, &flock, sizeof(flock)))
return _EFAULT;
err = fcntl_getlk(fd, &flock);
if (err >= 0)
if (user_write(arg, &flock, sizeof(flock)))
return _EFAULT;
return err;
case F_SETLK_:
case F_SETLKW_:
STRACE("fcntl(%d, F_SETLK%*s, %#x)", f, cmd == F_SETLKW_, "W", arg);
if (user_read(arg, &flock32, sizeof(flock32)))
return _EFAULT;
flock.type = flock32.type;
flock.whence = flock32.whence;
flock.start = flock32.start;
flock.len = flock32.len;
flock.pid = flock32.pid;
return fcntl_setlk(fd, &flock, cmd == F_SETLKW64_);
case F_SETLK64_:
case F_SETLKW64_:
STRACE("fcntl(%d, F_SETLK%*s64, %#x)", f, cmd == F_SETLKW_, "W", arg);
if (user_read(arg, &flock, sizeof(flock)))
return _EFAULT;
return fcntl_setlk(fd, &flock, cmd == F_SETLKW_);
default:
STRACE("fcntl(%d, %d)", f, cmd);
return _EINVAL;
}
}
dword_t sys_fcntl32(fd_t fd, dword_t cmd, dword_t arg) {
switch (cmd) {
case F_GETLK64_:
case F_SETLK64_:
case F_SETLKW64_:
return _EINVAL;
}
return sys_fcntl(fd, cmd, arg);
}