This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 260
/
shim_fcntl.c
276 lines (237 loc) · 7.72 KB
/
shim_fcntl.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
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/* Copyright (C) 2014 Stony Brook University
* Copyright (C) 2021 Intel Corporation
* Paweł Marczewski <[email protected]>
*/
/*
* Implementation of system call "fcntl":
*
* - F_DUPFD, F_DUPFD_CLOEXEC (duplicate a file descriptor)
* - F_GETFD, F_SETFD (file descriptor flags)
* - F_GETFL, F_SETFL (file status flags)
* - F_SETLK, F_SETLKW, F_GETLK (POSIX advisory locks)
* - F_SETOWN (file descriptor owner): dummy implementation
*/
#include <errno.h>
#include <linux/fcntl.h>
#include "pal.h"
#include "pal_error.h"
#include "shim_fs.h"
#include "shim_fs_lock.h"
#include "shim_handle.h"
#include "shim_internal.h"
#include "shim_lock.h"
#include "shim_process.h"
#include "shim_table.h"
#include "shim_thread.h"
#include "shim_utils.h"
#define FCNTL_SETFL_MASK (O_APPEND | O_NONBLOCK)
static int _set_handle_flags(struct shim_handle* hdl, unsigned long arg) {
if (hdl->fs && hdl->fs->fs_ops && hdl->fs->fs_ops->setflags) {
int ret = hdl->fs->fs_ops->setflags(hdl, arg & FCNTL_SETFL_MASK);
if (ret < 0) {
return ret;
}
}
hdl->flags = (hdl->flags & ~FCNTL_SETFL_MASK) | (arg & FCNTL_SETFL_MASK);
return 0;
}
int set_handle_nonblocking(struct shim_handle* hdl, bool on) {
lock(&hdl->lock);
int ret = _set_handle_flags(hdl, on ? hdl->flags | O_NONBLOCK : hdl->flags & ~O_NONBLOCK);
unlock(&hdl->lock);
return ret;
}
/*
* Convert user-mode `struct flock` into our `struct posix_lock`. This mostly means converting the
* position parameters (l_whence, l_start, l_len) to an absolute inclusive range [start .. end]. See
* `man fcntl` for details.
*
* We need to return -EINVAL for underflow (positions before start of file), and -EOVERFLOW for
* positive overflow.
*/
static int flock_to_posix_lock(struct flock* fl, struct shim_handle* hdl, struct posix_lock* pl) {
if (!(fl->l_type == F_RDLCK || fl->l_type == F_WRLCK || fl->l_type == F_UNLCK))
return -EINVAL;
int ret;
struct shim_fs* fs = hdl->fs;
assert(fs && fs->fs_ops);
uint64_t origin;
switch (fl->l_whence) {
case SEEK_SET:
origin = 0;
break;
case SEEK_CUR: {
if (!fs->fs_ops->seek)
return -EINVAL;
off_t pos = fs->fs_ops->seek(hdl, 0, SEEK_CUR);
if (pos < 0)
return pos;
origin = pos;
break;
}
case SEEK_END: {
if (!fs->fs_ops->hstat)
return -EINVAL;
struct stat stat;
ret = fs->fs_ops->hstat(hdl, &stat);
if (ret < 0)
return ret;
assert(stat.st_size >= 0);
origin = stat.st_size;
break;
}
default:
return -EINVAL;
}
if (__builtin_add_overflow(origin, fl->l_start, &origin)) {
return fl->l_start > 0 ? -EOVERFLOW : -EINVAL;
}
uint64_t start, end;
if (fl->l_len > 0) {
/* len > 0: the range is [origin .. origin + len - 1] */
start = origin;
if (__builtin_add_overflow(origin, fl->l_len - 1, &end))
return -EOVERFLOW;
} else if (fl->l_len < 0) {
/* len < 0: the range is [origin + len .. origin - 1] */
if (__builtin_add_overflow(origin, fl->l_len, &start))
return -EINVAL;
if (__builtin_add_overflow(origin, -1, &end))
return -EINVAL;
} else {
/* len == 0: the range is [origin .. EOF] */
start = origin;
end = FS_LOCK_EOF;
}
pl->type = fl->l_type;
pl->start = start;
pl->end = end;
pl->pid = g_process.pid;
return 0;
}
long shim_do_fcntl(int fd, int cmd, unsigned long arg) {
struct shim_handle_map* handle_map = get_thread_handle_map(NULL);
int flags;
int ret;
struct shim_handle* hdl = get_fd_handle(fd, &flags, handle_map);
if (!hdl)
return -EBADF;
switch (cmd) {
/* See `man fcntl` for the expected semantics of these commands. */
/* F_DUPFD (int) */
case F_DUPFD: {
ret = set_new_fd_handle_above_fd(arg, hdl, flags, handle_map);
break;
}
/* F_DUPFD_CLOEXEC (int) */
case F_DUPFD_CLOEXEC: {
flags |= FD_CLOEXEC;
ret = set_new_fd_handle_above_fd(arg, hdl, flags, handle_map);
break;
}
/* F_GETFD (int) */
case F_GETFD:
ret = flags & FD_CLOEXEC;
break;
/* F_SETFD (int) */
case F_SETFD:
lock(&handle_map->lock);
if (HANDLE_ALLOCATED(handle_map->map[fd]))
handle_map->map[fd]->flags = arg & FD_CLOEXEC;
unlock(&handle_map->lock);
ret = 0;
break;
/* F_GETFL (void) */
case F_GETFL:
lock(&hdl->lock);
flags = hdl->flags;
unlock(&hdl->lock);
ret = flags;
break;
/* F_SETFL (int) */
case F_SETFL:
lock(&hdl->lock);
ret = _set_handle_flags(hdl, arg);
unlock(&hdl->lock);
break;
/* F_SETLK, F_SETLKW (struct flock*): see `shim_fs_lock.h` for caveats */
case F_SETLK:
case F_SETLKW: {
struct flock *fl = (struct flock*)arg;
if (!is_user_memory_readable(fl, sizeof(*fl))) {
ret = -EFAULT;
break;
}
if (!hdl->dentry) {
/* TODO: Linux allows locks on pipes etc. Our locks work only for "normal" files
* that have a dentry. */
ret = -EINVAL;
break;
}
if (fl->l_type == F_RDLCK && !(hdl->acc_mode & MAY_READ)) {
ret = -EINVAL;
break;
}
if (fl->l_type == F_WRLCK && !(hdl->acc_mode & MAY_WRITE)) {
ret = -EINVAL;
break;
}
struct posix_lock pl;
ret = flock_to_posix_lock(fl, hdl, &pl);
if (ret < 0)
break;
ret = posix_lock_set(hdl->dentry, &pl, /*wait=*/cmd == F_SETLKW);
break;
}
/* F_GETLK (struct flock*): see `shim_fs_lock.h` for caveats */
case F_GETLK: {
struct flock *fl = (struct flock*)arg;
if (!is_user_memory_readable(fl, sizeof(*fl))
|| !is_user_memory_writable(fl, sizeof(*fl))) {
ret = -EFAULT;
break;
}
if (!hdl->dentry) {
ret = -EINVAL;
break;
}
struct posix_lock pl;
ret = flock_to_posix_lock(fl, hdl, &pl);
if (ret < 0)
break;
if (pl.type == F_UNLCK) {
ret = -EINVAL;
break;
}
struct posix_lock pl2;
ret = posix_lock_get(hdl->dentry, &pl, &pl2);
if (ret < 0)
break;
fl->l_type = pl2.type;
if (pl2.type != F_UNLCK) {
fl->l_whence = SEEK_SET;
fl->l_start = pl2.start;
if (pl2.end == FS_LOCK_EOF) {
/* range until EOF is encoded as len == 0 */
fl->l_len = 0;
} else {
fl->l_len = pl2.end - pl2.start + 1;
}
fl->l_pid = pl2.pid;
}
ret = 0;
break;
}
/* F_SETOWN (int): dummy implementation */
case F_SETOWN:
ret = 0;
/* XXX: DUMMY for now */
break;
default:
ret = -EINVAL;
break;
}
put_handle(hdl);
return ret;
}