]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/fcntl.c | |
3 | * | |
4 | * Copyright (C) 1991, 1992 Linus Torvalds | |
5 | */ | |
6 | ||
7 | #include <linux/syscalls.h> | |
8 | #include <linux/init.h> | |
9 | #include <linux/mm.h> | |
29930025 | 10 | #include <linux/sched/task.h> |
1da177e4 LT |
11 | #include <linux/fs.h> |
12 | #include <linux/file.h> | |
9f3acc31 | 13 | #include <linux/fdtable.h> |
16f7e0fe | 14 | #include <linux/capability.h> |
1da177e4 | 15 | #include <linux/dnotify.h> |
1da177e4 LT |
16 | #include <linux/slab.h> |
17 | #include <linux/module.h> | |
35f3d14d | 18 | #include <linux/pipe_fs_i.h> |
1da177e4 LT |
19 | #include <linux/security.h> |
20 | #include <linux/ptrace.h> | |
7ed20e1a | 21 | #include <linux/signal.h> |
ab2af1f5 | 22 | #include <linux/rcupdate.h> |
b488893a | 23 | #include <linux/pid_namespace.h> |
1d151c33 | 24 | #include <linux/user_namespace.h> |
40e041a2 | 25 | #include <linux/shmem_fs.h> |
80f0cce6 | 26 | #include <linux/compat.h> |
1da177e4 LT |
27 | |
28 | #include <asm/poll.h> | |
29 | #include <asm/siginfo.h> | |
7c0f6ba6 | 30 | #include <linux/uaccess.h> |
1da177e4 | 31 | |
76398425 | 32 | #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME) |
1da177e4 | 33 | |
b6450630 | 34 | int setfl(int fd, struct file * filp, unsigned long arg) |
1da177e4 | 35 | { |
496ad9aa | 36 | struct inode * inode = file_inode(filp); |
1da177e4 LT |
37 | int error = 0; |
38 | ||
7d95c8f2 | 39 | /* |
40 | * O_APPEND cannot be cleared if the file is marked as append-only | |
41 | * and the file is open for write. | |
42 | */ | |
43 | if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode)) | |
1da177e4 LT |
44 | return -EPERM; |
45 | ||
46 | /* O_NOATIME can only be set by the owner or superuser */ | |
47 | if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME)) | |
2e149670 | 48 | if (!inode_owner_or_capable(inode)) |
1da177e4 LT |
49 | return -EPERM; |
50 | ||
51 | /* required for strict SunOS emulation */ | |
52 | if (O_NONBLOCK != O_NDELAY) | |
53 | if (arg & O_NDELAY) | |
54 | arg |= O_NONBLOCK; | |
55 | ||
0dbf5f20 | 56 | /* Pipe packetized mode is controlled by O_DIRECT flag */ |
45063097 | 57 | if (!S_ISFIFO(inode->i_mode) && (arg & O_DIRECT)) { |
1da177e4 LT |
58 | if (!filp->f_mapping || !filp->f_mapping->a_ops || |
59 | !filp->f_mapping->a_ops->direct_IO) | |
60 | return -EINVAL; | |
61 | } | |
62 | ||
72c2d531 | 63 | if (filp->f_op->check_flags) |
1da177e4 | 64 | error = filp->f_op->check_flags(arg); |
b6450630 SF |
65 | if (!error && filp->f_op->setfl) |
66 | error = filp->f_op->setfl(filp, arg); | |
1da177e4 LT |
67 | if (error) |
68 | return error; | |
69 | ||
218d11a8 | 70 | /* |
76398425 | 71 | * ->fasync() is responsible for setting the FASYNC bit. |
218d11a8 | 72 | */ |
72c2d531 | 73 | if (((arg ^ filp->f_flags) & FASYNC) && filp->f_op->fasync) { |
76398425 JC |
74 | error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0); |
75 | if (error < 0) | |
76 | goto out; | |
60aa4924 JC |
77 | if (error > 0) |
78 | error = 0; | |
1da177e4 | 79 | } |
db1dd4d3 | 80 | spin_lock(&filp->f_lock); |
1da177e4 | 81 | filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK); |
db1dd4d3 | 82 | spin_unlock(&filp->f_lock); |
76398425 | 83 | |
1da177e4 | 84 | out: |
1da177e4 LT |
85 | return error; |
86 | } | |
b6450630 | 87 | EXPORT_SYMBOL_GPL(setfl); |
1da177e4 | 88 | |
609d7fa9 | 89 | static void f_modown(struct file *filp, struct pid *pid, enum pid_type type, |
2f38d70f | 90 | int force) |
1da177e4 | 91 | { |
80e1e823 | 92 | write_lock_irq(&filp->f_owner.lock); |
1da177e4 | 93 | if (force || !filp->f_owner.pid) { |
609d7fa9 EB |
94 | put_pid(filp->f_owner.pid); |
95 | filp->f_owner.pid = get_pid(pid); | |
96 | filp->f_owner.pid_type = type; | |
2f38d70f ON |
97 | |
98 | if (pid) { | |
99 | const struct cred *cred = current_cred(); | |
100 | filp->f_owner.uid = cred->uid; | |
101 | filp->f_owner.euid = cred->euid; | |
102 | } | |
1da177e4 | 103 | } |
80e1e823 | 104 | write_unlock_irq(&filp->f_owner.lock); |
1da177e4 LT |
105 | } |
106 | ||
e0b93edd | 107 | void __f_setown(struct file *filp, struct pid *pid, enum pid_type type, |
609d7fa9 | 108 | int force) |
1da177e4 | 109 | { |
e0b93edd | 110 | security_file_set_fowner(filp); |
2f38d70f | 111 | f_modown(filp, pid, type, force); |
1da177e4 | 112 | } |
609d7fa9 | 113 | EXPORT_SYMBOL(__f_setown); |
1da177e4 | 114 | |
393cc3f5 | 115 | int f_setown(struct file *filp, unsigned long arg, int force) |
609d7fa9 EB |
116 | { |
117 | enum pid_type type; | |
f7312735 JL |
118 | struct pid *pid = NULL; |
119 | int who = arg, ret = 0; | |
120 | ||
609d7fa9 EB |
121 | type = PIDTYPE_PID; |
122 | if (who < 0) { | |
fc3dc674 JS |
123 | /* avoid overflow below */ |
124 | if (who == INT_MIN) | |
125 | return -EINVAL; | |
126 | ||
609d7fa9 EB |
127 | type = PIDTYPE_PGID; |
128 | who = -who; | |
129 | } | |
f7312735 | 130 | |
609d7fa9 | 131 | rcu_read_lock(); |
f7312735 JL |
132 | if (who) { |
133 | pid = find_vpid(who); | |
134 | if (!pid) | |
135 | ret = -ESRCH; | |
136 | } | |
137 | ||
138 | if (!ret) | |
139 | __f_setown(filp, pid, type, force); | |
609d7fa9 | 140 | rcu_read_unlock(); |
393cc3f5 | 141 | |
f7312735 | 142 | return ret; |
609d7fa9 | 143 | } |
1da177e4 LT |
144 | EXPORT_SYMBOL(f_setown); |
145 | ||
146 | void f_delown(struct file *filp) | |
147 | { | |
2f38d70f | 148 | f_modown(filp, NULL, PIDTYPE_PID, 1); |
609d7fa9 EB |
149 | } |
150 | ||
151 | pid_t f_getown(struct file *filp) | |
152 | { | |
153 | pid_t pid; | |
43fa1adb | 154 | read_lock(&filp->f_owner.lock); |
6c5f3e7b | 155 | pid = pid_vnr(filp->f_owner.pid); |
609d7fa9 EB |
156 | if (filp->f_owner.pid_type == PIDTYPE_PGID) |
157 | pid = -pid; | |
43fa1adb | 158 | read_unlock(&filp->f_owner.lock); |
609d7fa9 | 159 | return pid; |
1da177e4 LT |
160 | } |
161 | ||
ba0a6c9f PZ |
162 | static int f_setown_ex(struct file *filp, unsigned long arg) |
163 | { | |
63784dd0 | 164 | struct f_owner_ex __user *owner_p = (void __user *)arg; |
ba0a6c9f PZ |
165 | struct f_owner_ex owner; |
166 | struct pid *pid; | |
167 | int type; | |
168 | int ret; | |
169 | ||
170 | ret = copy_from_user(&owner, owner_p, sizeof(owner)); | |
171 | if (ret) | |
5b54470d | 172 | return -EFAULT; |
ba0a6c9f PZ |
173 | |
174 | switch (owner.type) { | |
175 | case F_OWNER_TID: | |
176 | type = PIDTYPE_MAX; | |
177 | break; | |
178 | ||
179 | case F_OWNER_PID: | |
180 | type = PIDTYPE_PID; | |
181 | break; | |
182 | ||
978b4053 | 183 | case F_OWNER_PGRP: |
ba0a6c9f PZ |
184 | type = PIDTYPE_PGID; |
185 | break; | |
186 | ||
187 | default: | |
188 | return -EINVAL; | |
189 | } | |
190 | ||
191 | rcu_read_lock(); | |
192 | pid = find_vpid(owner.pid); | |
193 | if (owner.pid && !pid) | |
194 | ret = -ESRCH; | |
195 | else | |
e0b93edd | 196 | __f_setown(filp, pid, type, 1); |
ba0a6c9f PZ |
197 | rcu_read_unlock(); |
198 | ||
199 | return ret; | |
200 | } | |
201 | ||
202 | static int f_getown_ex(struct file *filp, unsigned long arg) | |
203 | { | |
63784dd0 | 204 | struct f_owner_ex __user *owner_p = (void __user *)arg; |
ba0a6c9f PZ |
205 | struct f_owner_ex owner; |
206 | int ret = 0; | |
207 | ||
208 | read_lock(&filp->f_owner.lock); | |
209 | owner.pid = pid_vnr(filp->f_owner.pid); | |
210 | switch (filp->f_owner.pid_type) { | |
211 | case PIDTYPE_MAX: | |
212 | owner.type = F_OWNER_TID; | |
213 | break; | |
214 | ||
215 | case PIDTYPE_PID: | |
216 | owner.type = F_OWNER_PID; | |
217 | break; | |
218 | ||
219 | case PIDTYPE_PGID: | |
978b4053 | 220 | owner.type = F_OWNER_PGRP; |
ba0a6c9f PZ |
221 | break; |
222 | ||
223 | default: | |
224 | WARN_ON(1); | |
225 | ret = -EINVAL; | |
226 | break; | |
227 | } | |
228 | read_unlock(&filp->f_owner.lock); | |
229 | ||
5b54470d | 230 | if (!ret) { |
ba0a6c9f | 231 | ret = copy_to_user(owner_p, &owner, sizeof(owner)); |
5b54470d DC |
232 | if (ret) |
233 | ret = -EFAULT; | |
234 | } | |
ba0a6c9f PZ |
235 | return ret; |
236 | } | |
237 | ||
1d151c33 CG |
238 | #ifdef CONFIG_CHECKPOINT_RESTORE |
239 | static int f_getowner_uids(struct file *filp, unsigned long arg) | |
240 | { | |
241 | struct user_namespace *user_ns = current_user_ns(); | |
63784dd0 | 242 | uid_t __user *dst = (void __user *)arg; |
1d151c33 CG |
243 | uid_t src[2]; |
244 | int err; | |
245 | ||
246 | read_lock(&filp->f_owner.lock); | |
247 | src[0] = from_kuid(user_ns, filp->f_owner.uid); | |
248 | src[1] = from_kuid(user_ns, filp->f_owner.euid); | |
249 | read_unlock(&filp->f_owner.lock); | |
250 | ||
251 | err = put_user(src[0], &dst[0]); | |
252 | err |= put_user(src[1], &dst[1]); | |
253 | ||
254 | return err; | |
255 | } | |
256 | #else | |
257 | static int f_getowner_uids(struct file *filp, unsigned long arg) | |
258 | { | |
259 | return -EINVAL; | |
260 | } | |
261 | #endif | |
262 | ||
c75b1d94 JA |
263 | static bool rw_hint_valid(enum rw_hint hint) |
264 | { | |
265 | switch (hint) { | |
266 | case RWF_WRITE_LIFE_NOT_SET: | |
267 | case RWH_WRITE_LIFE_NONE: | |
268 | case RWH_WRITE_LIFE_SHORT: | |
269 | case RWH_WRITE_LIFE_MEDIUM: | |
270 | case RWH_WRITE_LIFE_LONG: | |
271 | case RWH_WRITE_LIFE_EXTREME: | |
272 | return true; | |
273 | default: | |
274 | return false; | |
275 | } | |
276 | } | |
277 | ||
278 | static long fcntl_rw_hint(struct file *file, unsigned int cmd, | |
279 | unsigned long arg) | |
280 | { | |
281 | struct inode *inode = file_inode(file); | |
282 | u64 *argp = (u64 __user *)arg; | |
283 | enum rw_hint hint; | |
5657cb07 | 284 | u64 h; |
c75b1d94 JA |
285 | |
286 | switch (cmd) { | |
287 | case F_GET_FILE_RW_HINT: | |
5657cb07 JA |
288 | h = file_write_hint(file); |
289 | if (copy_to_user(argp, &h, sizeof(*argp))) | |
c75b1d94 JA |
290 | return -EFAULT; |
291 | return 0; | |
292 | case F_SET_FILE_RW_HINT: | |
5657cb07 | 293 | if (copy_from_user(&h, argp, sizeof(h))) |
c75b1d94 | 294 | return -EFAULT; |
5657cb07 | 295 | hint = (enum rw_hint) h; |
c75b1d94 JA |
296 | if (!rw_hint_valid(hint)) |
297 | return -EINVAL; | |
298 | ||
299 | spin_lock(&file->f_lock); | |
300 | file->f_write_hint = hint; | |
301 | spin_unlock(&file->f_lock); | |
302 | return 0; | |
303 | case F_GET_RW_HINT: | |
5657cb07 JA |
304 | h = inode->i_write_hint; |
305 | if (copy_to_user(argp, &h, sizeof(*argp))) | |
c75b1d94 JA |
306 | return -EFAULT; |
307 | return 0; | |
308 | case F_SET_RW_HINT: | |
5657cb07 | 309 | if (copy_from_user(&h, argp, sizeof(h))) |
c75b1d94 | 310 | return -EFAULT; |
5657cb07 | 311 | hint = (enum rw_hint) h; |
c75b1d94 JA |
312 | if (!rw_hint_valid(hint)) |
313 | return -EINVAL; | |
314 | ||
315 | inode_lock(inode); | |
316 | inode->i_write_hint = hint; | |
317 | inode_unlock(inode); | |
318 | return 0; | |
319 | default: | |
320 | return -EINVAL; | |
321 | } | |
322 | } | |
323 | ||
1da177e4 LT |
324 | static long do_fcntl(int fd, unsigned int cmd, unsigned long arg, |
325 | struct file *filp) | |
326 | { | |
a75d30c7 CH |
327 | void __user *argp = (void __user *)arg; |
328 | struct flock flock; | |
1da177e4 LT |
329 | long err = -EINVAL; |
330 | ||
331 | switch (cmd) { | |
332 | case F_DUPFD: | |
fe17f22d AV |
333 | err = f_dupfd(arg, filp, 0); |
334 | break; | |
22d2b35b | 335 | case F_DUPFD_CLOEXEC: |
12197718 | 336 | err = f_dupfd(arg, filp, O_CLOEXEC); |
1da177e4 LT |
337 | break; |
338 | case F_GETFD: | |
339 | err = get_close_on_exec(fd) ? FD_CLOEXEC : 0; | |
340 | break; | |
341 | case F_SETFD: | |
342 | err = 0; | |
343 | set_close_on_exec(fd, arg & FD_CLOEXEC); | |
344 | break; | |
345 | case F_GETFL: | |
346 | err = filp->f_flags; | |
347 | break; | |
348 | case F_SETFL: | |
349 | err = setfl(fd, filp, arg); | |
350 | break; | |
5d50ffd7 JL |
351 | #if BITS_PER_LONG != 32 |
352 | /* 32-bit arches must use fcntl64() */ | |
0d3f7a2d | 353 | case F_OFD_GETLK: |
5d50ffd7 | 354 | #endif |
1da177e4 | 355 | case F_GETLK: |
a75d30c7 CH |
356 | if (copy_from_user(&flock, argp, sizeof(flock))) |
357 | return -EFAULT; | |
358 | err = fcntl_getlk(filp, cmd, &flock); | |
359 | if (!err && copy_to_user(argp, &flock, sizeof(flock))) | |
360 | return -EFAULT; | |
1da177e4 | 361 | break; |
5d50ffd7 JL |
362 | #if BITS_PER_LONG != 32 |
363 | /* 32-bit arches must use fcntl64() */ | |
0d3f7a2d JL |
364 | case F_OFD_SETLK: |
365 | case F_OFD_SETLKW: | |
5d50ffd7 JL |
366 | #endif |
367 | /* Fallthrough */ | |
1da177e4 LT |
368 | case F_SETLK: |
369 | case F_SETLKW: | |
a75d30c7 CH |
370 | if (copy_from_user(&flock, argp, sizeof(flock))) |
371 | return -EFAULT; | |
372 | err = fcntl_setlk(fd, filp, cmd, &flock); | |
1da177e4 LT |
373 | break; |
374 | case F_GETOWN: | |
375 | /* | |
376 | * XXX If f_owner is a process group, the | |
377 | * negative return value will get converted | |
378 | * into an error. Oops. If we keep the | |
379 | * current syscall conventions, the only way | |
380 | * to fix this will be in libc. | |
381 | */ | |
609d7fa9 | 382 | err = f_getown(filp); |
1da177e4 LT |
383 | force_successful_syscall_return(); |
384 | break; | |
385 | case F_SETOWN: | |
393cc3f5 | 386 | err = f_setown(filp, arg, 1); |
1da177e4 | 387 | break; |
ba0a6c9f PZ |
388 | case F_GETOWN_EX: |
389 | err = f_getown_ex(filp, arg); | |
390 | break; | |
391 | case F_SETOWN_EX: | |
392 | err = f_setown_ex(filp, arg); | |
393 | break; | |
1d151c33 CG |
394 | case F_GETOWNER_UIDS: |
395 | err = f_getowner_uids(filp, arg); | |
396 | break; | |
1da177e4 LT |
397 | case F_GETSIG: |
398 | err = filp->f_owner.signum; | |
399 | break; | |
400 | case F_SETSIG: | |
401 | /* arg == 0 restores default behaviour. */ | |
7ed20e1a | 402 | if (!valid_signal(arg)) { |
1da177e4 LT |
403 | break; |
404 | } | |
405 | err = 0; | |
406 | filp->f_owner.signum = arg; | |
407 | break; | |
408 | case F_GETLEASE: | |
409 | err = fcntl_getlease(filp); | |
410 | break; | |
411 | case F_SETLEASE: | |
412 | err = fcntl_setlease(fd, filp, arg); | |
413 | break; | |
414 | case F_NOTIFY: | |
415 | err = fcntl_dirnotify(fd, filp, arg); | |
416 | break; | |
35f3d14d JA |
417 | case F_SETPIPE_SZ: |
418 | case F_GETPIPE_SZ: | |
419 | err = pipe_fcntl(filp, cmd, arg); | |
420 | break; | |
40e041a2 DH |
421 | case F_ADD_SEALS: |
422 | case F_GET_SEALS: | |
423 | err = shmem_fcntl(filp, cmd, arg); | |
424 | break; | |
c75b1d94 JA |
425 | case F_GET_RW_HINT: |
426 | case F_SET_RW_HINT: | |
427 | case F_GET_FILE_RW_HINT: | |
428 | case F_SET_FILE_RW_HINT: | |
429 | err = fcntl_rw_hint(filp, cmd, arg); | |
430 | break; | |
1da177e4 LT |
431 | default: |
432 | break; | |
433 | } | |
434 | return err; | |
435 | } | |
436 | ||
1abf0c71 AV |
437 | static int check_fcntl_cmd(unsigned cmd) |
438 | { | |
439 | switch (cmd) { | |
440 | case F_DUPFD: | |
441 | case F_DUPFD_CLOEXEC: | |
442 | case F_GETFD: | |
443 | case F_SETFD: | |
444 | case F_GETFL: | |
445 | return 1; | |
446 | } | |
447 | return 0; | |
448 | } | |
449 | ||
a26eab24 | 450 | SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg) |
1da177e4 | 451 | { |
2903ff01 | 452 | struct fd f = fdget_raw(fd); |
1da177e4 LT |
453 | long err = -EBADF; |
454 | ||
2903ff01 | 455 | if (!f.file) |
1da177e4 LT |
456 | goto out; |
457 | ||
2903ff01 | 458 | if (unlikely(f.file->f_mode & FMODE_PATH)) { |
545ec2c7 AV |
459 | if (!check_fcntl_cmd(cmd)) |
460 | goto out1; | |
1abf0c71 AV |
461 | } |
462 | ||
2903ff01 | 463 | err = security_file_fcntl(f.file, cmd, arg); |
545ec2c7 | 464 | if (!err) |
2903ff01 | 465 | err = do_fcntl(fd, cmd, arg, f.file); |
1da177e4 | 466 | |
545ec2c7 | 467 | out1: |
2903ff01 | 468 | fdput(f); |
1da177e4 LT |
469 | out: |
470 | return err; | |
471 | } | |
472 | ||
473 | #if BITS_PER_LONG == 32 | |
a26eab24 HC |
474 | SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd, |
475 | unsigned long, arg) | |
1da177e4 | 476 | { |
a75d30c7 | 477 | void __user *argp = (void __user *)arg; |
2903ff01 | 478 | struct fd f = fdget_raw(fd); |
a75d30c7 | 479 | struct flock64 flock; |
545ec2c7 | 480 | long err = -EBADF; |
1da177e4 | 481 | |
2903ff01 | 482 | if (!f.file) |
1da177e4 LT |
483 | goto out; |
484 | ||
2903ff01 | 485 | if (unlikely(f.file->f_mode & FMODE_PATH)) { |
545ec2c7 AV |
486 | if (!check_fcntl_cmd(cmd)) |
487 | goto out1; | |
1abf0c71 AV |
488 | } |
489 | ||
2903ff01 | 490 | err = security_file_fcntl(f.file, cmd, arg); |
545ec2c7 AV |
491 | if (err) |
492 | goto out1; | |
1da177e4 LT |
493 | |
494 | switch (cmd) { | |
5d50ffd7 | 495 | case F_GETLK64: |
0d3f7a2d | 496 | case F_OFD_GETLK: |
a75d30c7 CH |
497 | err = -EFAULT; |
498 | if (copy_from_user(&flock, argp, sizeof(flock))) | |
499 | break; | |
500 | err = fcntl_getlk64(f.file, cmd, &flock); | |
501 | if (!err && copy_to_user(argp, &flock, sizeof(flock))) | |
502 | err = -EFAULT; | |
5d50ffd7 JL |
503 | break; |
504 | case F_SETLK64: | |
505 | case F_SETLKW64: | |
0d3f7a2d JL |
506 | case F_OFD_SETLK: |
507 | case F_OFD_SETLKW: | |
a75d30c7 CH |
508 | err = -EFAULT; |
509 | if (copy_from_user(&flock, argp, sizeof(flock))) | |
510 | break; | |
511 | err = fcntl_setlk64(fd, f.file, cmd, &flock); | |
5d50ffd7 JL |
512 | break; |
513 | default: | |
514 | err = do_fcntl(fd, cmd, arg, f.file); | |
515 | break; | |
1da177e4 | 516 | } |
545ec2c7 | 517 | out1: |
2903ff01 | 518 | fdput(f); |
1da177e4 LT |
519 | out: |
520 | return err; | |
521 | } | |
522 | #endif | |
523 | ||
80f0cce6 | 524 | #ifdef CONFIG_COMPAT |
8c6657cb | 525 | /* careful - don't use anywhere else */ |
b59eea55 LT |
526 | #define copy_flock_fields(dst, src) \ |
527 | (dst)->l_type = (src)->l_type; \ | |
528 | (dst)->l_whence = (src)->l_whence; \ | |
529 | (dst)->l_start = (src)->l_start; \ | |
530 | (dst)->l_len = (src)->l_len; \ | |
531 | (dst)->l_pid = (src)->l_pid; | |
532 | ||
533 | static int get_compat_flock(struct flock *kfl, const struct compat_flock __user *ufl) | |
80f0cce6 | 534 | { |
8c6657cb AV |
535 | struct compat_flock fl; |
536 | ||
537 | if (copy_from_user(&fl, ufl, sizeof(struct compat_flock))) | |
80f0cce6 | 538 | return -EFAULT; |
b59eea55 | 539 | copy_flock_fields(kfl, &fl); |
80f0cce6 AV |
540 | return 0; |
541 | } | |
542 | ||
b59eea55 | 543 | static int get_compat_flock64(struct flock *kfl, const struct compat_flock64 __user *ufl) |
80f0cce6 | 544 | { |
8c6657cb AV |
545 | struct compat_flock64 fl; |
546 | ||
547 | if (copy_from_user(&fl, ufl, sizeof(struct compat_flock64))) | |
80f0cce6 | 548 | return -EFAULT; |
b59eea55 | 549 | copy_flock_fields(kfl, &fl); |
80f0cce6 AV |
550 | return 0; |
551 | } | |
552 | ||
b59eea55 | 553 | static int put_compat_flock(const struct flock *kfl, struct compat_flock __user *ufl) |
80f0cce6 | 554 | { |
8c6657cb AV |
555 | struct compat_flock fl; |
556 | ||
557 | memset(&fl, 0, sizeof(struct compat_flock)); | |
b59eea55 | 558 | copy_flock_fields(&fl, kfl); |
8c6657cb | 559 | if (copy_to_user(ufl, &fl, sizeof(struct compat_flock))) |
80f0cce6 AV |
560 | return -EFAULT; |
561 | return 0; | |
562 | } | |
80f0cce6 | 563 | |
b59eea55 | 564 | static int put_compat_flock64(const struct flock *kfl, struct compat_flock64 __user *ufl) |
80f0cce6 | 565 | { |
8c6657cb AV |
566 | struct compat_flock64 fl; |
567 | ||
568 | memset(&fl, 0, sizeof(struct compat_flock64)); | |
b59eea55 | 569 | copy_flock_fields(&fl, kfl); |
8c6657cb | 570 | if (copy_to_user(ufl, &fl, sizeof(struct compat_flock64))) |
80f0cce6 AV |
571 | return -EFAULT; |
572 | return 0; | |
573 | } | |
8c6657cb | 574 | #undef copy_flock_fields |
80f0cce6 AV |
575 | |
576 | static unsigned int | |
577 | convert_fcntl_cmd(unsigned int cmd) | |
578 | { | |
579 | switch (cmd) { | |
580 | case F_GETLK64: | |
581 | return F_GETLK; | |
582 | case F_SETLK64: | |
583 | return F_SETLK; | |
584 | case F_SETLKW64: | |
585 | return F_SETLKW; | |
586 | } | |
587 | ||
588 | return cmd; | |
589 | } | |
590 | ||
94073ad7 CH |
591 | /* |
592 | * GETLK was successful and we need to return the data, but it needs to fit in | |
593 | * the compat structure. | |
594 | * l_start shouldn't be too big, unless the original start + end is greater than | |
595 | * COMPAT_OFF_T_MAX, in which case the app was asking for trouble, so we return | |
596 | * -EOVERFLOW in that case. l_len could be too big, in which case we just | |
597 | * truncate it, and only allow the app to see that part of the conflicting lock | |
598 | * that might make sense to it anyway | |
599 | */ | |
600 | static int fixup_compat_flock(struct flock *flock) | |
601 | { | |
602 | if (flock->l_start > COMPAT_OFF_T_MAX) | |
603 | return -EOVERFLOW; | |
604 | if (flock->l_len > COMPAT_OFF_T_MAX) | |
605 | flock->l_len = COMPAT_OFF_T_MAX; | |
606 | return 0; | |
607 | } | |
608 | ||
80f0cce6 AV |
609 | COMPAT_SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd, |
610 | compat_ulong_t, arg) | |
611 | { | |
94073ad7 CH |
612 | struct fd f = fdget_raw(fd); |
613 | struct flock flock; | |
614 | long err = -EBADF; | |
615 | ||
616 | if (!f.file) | |
617 | return err; | |
618 | ||
619 | if (unlikely(f.file->f_mode & FMODE_PATH)) { | |
620 | if (!check_fcntl_cmd(cmd)) | |
621 | goto out_put; | |
622 | } | |
623 | ||
624 | err = security_file_fcntl(f.file, cmd, arg); | |
625 | if (err) | |
626 | goto out_put; | |
80f0cce6 AV |
627 | |
628 | switch (cmd) { | |
629 | case F_GETLK: | |
94073ad7 CH |
630 | err = get_compat_flock(&flock, compat_ptr(arg)); |
631 | if (err) | |
632 | break; | |
633 | err = fcntl_getlk(f.file, convert_fcntl_cmd(cmd), &flock); | |
634 | if (err) | |
635 | break; | |
636 | err = fixup_compat_flock(&flock); | |
637 | if (err) | |
638 | return err; | |
639 | err = put_compat_flock(&flock, compat_ptr(arg)); | |
640 | break; | |
641 | case F_GETLK64: | |
642 | case F_OFD_GETLK: | |
643 | err = get_compat_flock64(&flock, compat_ptr(arg)); | |
644 | if (err) | |
645 | break; | |
646 | err = fcntl_getlk(f.file, convert_fcntl_cmd(cmd), &flock); | |
647 | if (err) | |
648 | break; | |
649 | err = fixup_compat_flock(&flock); | |
650 | if (err) | |
651 | return err; | |
652 | err = put_compat_flock64(&flock, compat_ptr(arg)); | |
653 | break; | |
80f0cce6 AV |
654 | case F_SETLK: |
655 | case F_SETLKW: | |
94073ad7 CH |
656 | err = get_compat_flock(&flock, compat_ptr(arg)); |
657 | if (err) | |
80f0cce6 | 658 | break; |
94073ad7 | 659 | err = fcntl_setlk(fd, f.file, convert_fcntl_cmd(cmd), &flock); |
80f0cce6 | 660 | break; |
80f0cce6 AV |
661 | case F_SETLK64: |
662 | case F_SETLKW64: | |
80f0cce6 AV |
663 | case F_OFD_SETLK: |
664 | case F_OFD_SETLKW: | |
94073ad7 CH |
665 | err = get_compat_flock64(&flock, compat_ptr(arg)); |
666 | if (err) | |
80f0cce6 | 667 | break; |
94073ad7 | 668 | err = fcntl_setlk(fd, f.file, convert_fcntl_cmd(cmd), &flock); |
80f0cce6 | 669 | break; |
80f0cce6 | 670 | default: |
94073ad7 | 671 | err = do_fcntl(fd, cmd, arg, f.file); |
80f0cce6 AV |
672 | break; |
673 | } | |
94073ad7 CH |
674 | out_put: |
675 | fdput(f); | |
676 | return err; | |
80f0cce6 AV |
677 | } |
678 | ||
679 | COMPAT_SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, | |
680 | compat_ulong_t, arg) | |
681 | { | |
682 | switch (cmd) { | |
683 | case F_GETLK64: | |
684 | case F_SETLK64: | |
685 | case F_SETLKW64: | |
686 | case F_OFD_GETLK: | |
687 | case F_OFD_SETLK: | |
688 | case F_OFD_SETLKW: | |
689 | return -EINVAL; | |
690 | } | |
691 | return compat_sys_fcntl64(fd, cmd, arg); | |
692 | } | |
693 | #endif | |
694 | ||
1da177e4 LT |
695 | /* Table to convert sigio signal codes into poll band bitmaps */ |
696 | ||
fa3536cc | 697 | static const long band_table[NSIGPOLL] = { |
1da177e4 LT |
698 | POLLIN | POLLRDNORM, /* POLL_IN */ |
699 | POLLOUT | POLLWRNORM | POLLWRBAND, /* POLL_OUT */ | |
700 | POLLIN | POLLRDNORM | POLLMSG, /* POLL_MSG */ | |
701 | POLLERR, /* POLL_ERR */ | |
702 | POLLPRI | POLLRDBAND, /* POLL_PRI */ | |
703 | POLLHUP | POLLERR /* POLL_HUP */ | |
704 | }; | |
705 | ||
706 | static inline int sigio_perm(struct task_struct *p, | |
707 | struct fown_struct *fown, int sig) | |
708 | { | |
c69e8d9c DH |
709 | const struct cred *cred; |
710 | int ret; | |
711 | ||
712 | rcu_read_lock(); | |
713 | cred = __task_cred(p); | |
8e96e3b7 EB |
714 | ret = ((uid_eq(fown->euid, GLOBAL_ROOT_UID) || |
715 | uid_eq(fown->euid, cred->suid) || uid_eq(fown->euid, cred->uid) || | |
716 | uid_eq(fown->uid, cred->suid) || uid_eq(fown->uid, cred->uid)) && | |
c69e8d9c DH |
717 | !security_file_send_sigiotask(p, fown, sig)); |
718 | rcu_read_unlock(); | |
719 | return ret; | |
1da177e4 LT |
720 | } |
721 | ||
722 | static void send_sigio_to_task(struct task_struct *p, | |
8eeee4e2 | 723 | struct fown_struct *fown, |
ba0a6c9f | 724 | int fd, int reason, int group) |
1da177e4 | 725 | { |
8eeee4e2 ON |
726 | /* |
727 | * F_SETSIG can change ->signum lockless in parallel, make | |
728 | * sure we read it once and use the same value throughout. | |
729 | */ | |
730 | int signum = ACCESS_ONCE(fown->signum); | |
731 | ||
732 | if (!sigio_perm(p, fown, signum)) | |
1da177e4 LT |
733 | return; |
734 | ||
8eeee4e2 | 735 | switch (signum) { |
1da177e4 LT |
736 | siginfo_t si; |
737 | default: | |
738 | /* Queue a rt signal with the appropriate fd as its | |
739 | value. We use SI_SIGIO as the source, not | |
740 | SI_KERNEL, since kernel signals always get | |
741 | delivered even if we can't queue. Failure to | |
742 | queue in this case _should_ be reported; we fall | |
743 | back to SIGIO in that case. --sct */ | |
8eeee4e2 | 744 | si.si_signo = signum; |
1da177e4 LT |
745 | si.si_errno = 0; |
746 | si.si_code = reason; | |
747 | /* Make sure we are called with one of the POLL_* | |
748 | reasons, otherwise we could leak kernel stack into | |
749 | userspace. */ | |
f6298aab | 750 | BUG_ON((reason & __SI_MASK) != __SI_POLL); |
1da177e4 LT |
751 | if (reason - POLL_IN >= NSIGPOLL) |
752 | si.si_band = ~0L; | |
753 | else | |
754 | si.si_band = band_table[reason - POLL_IN]; | |
755 | si.si_fd = fd; | |
ba0a6c9f | 756 | if (!do_send_sig_info(signum, &si, p, group)) |
1da177e4 LT |
757 | break; |
758 | /* fall-through: fall back on the old plain SIGIO signal */ | |
759 | case 0: | |
ba0a6c9f | 760 | do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, group); |
1da177e4 LT |
761 | } |
762 | } | |
763 | ||
764 | void send_sigio(struct fown_struct *fown, int fd, int band) | |
765 | { | |
766 | struct task_struct *p; | |
609d7fa9 EB |
767 | enum pid_type type; |
768 | struct pid *pid; | |
ba0a6c9f | 769 | int group = 1; |
1da177e4 LT |
770 | |
771 | read_lock(&fown->lock); | |
ba0a6c9f | 772 | |
609d7fa9 | 773 | type = fown->pid_type; |
ba0a6c9f PZ |
774 | if (type == PIDTYPE_MAX) { |
775 | group = 0; | |
776 | type = PIDTYPE_PID; | |
777 | } | |
778 | ||
1da177e4 LT |
779 | pid = fown->pid; |
780 | if (!pid) | |
781 | goto out_unlock_fown; | |
782 | ||
783 | read_lock(&tasklist_lock); | |
609d7fa9 | 784 | do_each_pid_task(pid, type, p) { |
ba0a6c9f | 785 | send_sigio_to_task(p, fown, fd, band, group); |
609d7fa9 | 786 | } while_each_pid_task(pid, type, p); |
1da177e4 LT |
787 | read_unlock(&tasklist_lock); |
788 | out_unlock_fown: | |
789 | read_unlock(&fown->lock); | |
790 | } | |
791 | ||
792 | static void send_sigurg_to_task(struct task_struct *p, | |
ba0a6c9f | 793 | struct fown_struct *fown, int group) |
1da177e4 LT |
794 | { |
795 | if (sigio_perm(p, fown, SIGURG)) | |
ba0a6c9f | 796 | do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, group); |
1da177e4 LT |
797 | } |
798 | ||
799 | int send_sigurg(struct fown_struct *fown) | |
800 | { | |
801 | struct task_struct *p; | |
609d7fa9 EB |
802 | enum pid_type type; |
803 | struct pid *pid; | |
ba0a6c9f | 804 | int group = 1; |
609d7fa9 | 805 | int ret = 0; |
1da177e4 LT |
806 | |
807 | read_lock(&fown->lock); | |
ba0a6c9f | 808 | |
609d7fa9 | 809 | type = fown->pid_type; |
ba0a6c9f PZ |
810 | if (type == PIDTYPE_MAX) { |
811 | group = 0; | |
812 | type = PIDTYPE_PID; | |
813 | } | |
814 | ||
1da177e4 LT |
815 | pid = fown->pid; |
816 | if (!pid) | |
817 | goto out_unlock_fown; | |
818 | ||
819 | ret = 1; | |
820 | ||
821 | read_lock(&tasklist_lock); | |
609d7fa9 | 822 | do_each_pid_task(pid, type, p) { |
ba0a6c9f | 823 | send_sigurg_to_task(p, fown, group); |
609d7fa9 | 824 | } while_each_pid_task(pid, type, p); |
1da177e4 LT |
825 | read_unlock(&tasklist_lock); |
826 | out_unlock_fown: | |
827 | read_unlock(&fown->lock); | |
828 | return ret; | |
829 | } | |
830 | ||
989a2979 | 831 | static DEFINE_SPINLOCK(fasync_lock); |
e18b890b | 832 | static struct kmem_cache *fasync_cache __read_mostly; |
1da177e4 | 833 | |
989a2979 ED |
834 | static void fasync_free_rcu(struct rcu_head *head) |
835 | { | |
836 | kmem_cache_free(fasync_cache, | |
837 | container_of(head, struct fasync_struct, fa_rcu)); | |
838 | } | |
839 | ||
1da177e4 | 840 | /* |
53281b6d LT |
841 | * Remove a fasync entry. If successfully removed, return |
842 | * positive and clear the FASYNC flag. If no entry exists, | |
843 | * do nothing and return 0. | |
844 | * | |
845 | * NOTE! It is very important that the FASYNC flag always | |
846 | * match the state "is the filp on a fasync list". | |
847 | * | |
1da177e4 | 848 | */ |
f7347ce4 | 849 | int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp) |
1da177e4 LT |
850 | { |
851 | struct fasync_struct *fa, **fp; | |
1da177e4 LT |
852 | int result = 0; |
853 | ||
53281b6d | 854 | spin_lock(&filp->f_lock); |
989a2979 | 855 | spin_lock(&fasync_lock); |
53281b6d LT |
856 | for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) { |
857 | if (fa->fa_file != filp) | |
858 | continue; | |
989a2979 ED |
859 | |
860 | spin_lock_irq(&fa->fa_lock); | |
861 | fa->fa_file = NULL; | |
862 | spin_unlock_irq(&fa->fa_lock); | |
863 | ||
53281b6d | 864 | *fp = fa->fa_next; |
989a2979 | 865 | call_rcu(&fa->fa_rcu, fasync_free_rcu); |
53281b6d LT |
866 | filp->f_flags &= ~FASYNC; |
867 | result = 1; | |
868 | break; | |
1da177e4 | 869 | } |
989a2979 | 870 | spin_unlock(&fasync_lock); |
53281b6d LT |
871 | spin_unlock(&filp->f_lock); |
872 | return result; | |
873 | } | |
874 | ||
f7347ce4 LT |
875 | struct fasync_struct *fasync_alloc(void) |
876 | { | |
877 | return kmem_cache_alloc(fasync_cache, GFP_KERNEL); | |
878 | } | |
879 | ||
53281b6d | 880 | /* |
f7347ce4 LT |
881 | * NOTE! This can be used only for unused fasync entries: |
882 | * entries that actually got inserted on the fasync list | |
883 | * need to be released by rcu - see fasync_remove_entry. | |
53281b6d | 884 | */ |
f7347ce4 | 885 | void fasync_free(struct fasync_struct *new) |
53281b6d | 886 | { |
f7347ce4 LT |
887 | kmem_cache_free(fasync_cache, new); |
888 | } | |
53281b6d | 889 | |
f7347ce4 LT |
890 | /* |
891 | * Insert a new entry into the fasync list. Return the pointer to the | |
892 | * old one if we didn't use the new one. | |
55f335a8 LT |
893 | * |
894 | * NOTE! It is very important that the FASYNC flag always | |
895 | * match the state "is the filp on a fasync list". | |
f7347ce4 LT |
896 | */ |
897 | struct fasync_struct *fasync_insert_entry(int fd, struct file *filp, struct fasync_struct **fapp, struct fasync_struct *new) | |
898 | { | |
899 | struct fasync_struct *fa, **fp; | |
4a6a4499 | 900 | |
4a6a4499 | 901 | spin_lock(&filp->f_lock); |
989a2979 | 902 | spin_lock(&fasync_lock); |
1da177e4 | 903 | for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) { |
53281b6d LT |
904 | if (fa->fa_file != filp) |
905 | continue; | |
989a2979 ED |
906 | |
907 | spin_lock_irq(&fa->fa_lock); | |
53281b6d | 908 | fa->fa_fd = fd; |
989a2979 | 909 | spin_unlock_irq(&fa->fa_lock); |
53281b6d | 910 | goto out; |
1da177e4 LT |
911 | } |
912 | ||
989a2979 | 913 | spin_lock_init(&new->fa_lock); |
53281b6d LT |
914 | new->magic = FASYNC_MAGIC; |
915 | new->fa_file = filp; | |
916 | new->fa_fd = fd; | |
917 | new->fa_next = *fapp; | |
989a2979 | 918 | rcu_assign_pointer(*fapp, new); |
53281b6d LT |
919 | filp->f_flags |= FASYNC; |
920 | ||
1da177e4 | 921 | out: |
989a2979 | 922 | spin_unlock(&fasync_lock); |
4a6a4499 | 923 | spin_unlock(&filp->f_lock); |
f7347ce4 LT |
924 | return fa; |
925 | } | |
926 | ||
927 | /* | |
928 | * Add a fasync entry. Return negative on error, positive if | |
929 | * added, and zero if did nothing but change an existing one. | |
f7347ce4 LT |
930 | */ |
931 | static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fapp) | |
932 | { | |
933 | struct fasync_struct *new; | |
934 | ||
935 | new = fasync_alloc(); | |
936 | if (!new) | |
937 | return -ENOMEM; | |
938 | ||
939 | /* | |
940 | * fasync_insert_entry() returns the old (update) entry if | |
941 | * it existed. | |
942 | * | |
943 | * So free the (unused) new entry and return 0 to let the | |
944 | * caller know that we didn't add any new fasync entries. | |
945 | */ | |
946 | if (fasync_insert_entry(fd, filp, fapp, new)) { | |
947 | fasync_free(new); | |
948 | return 0; | |
949 | } | |
950 | ||
951 | return 1; | |
1da177e4 LT |
952 | } |
953 | ||
53281b6d LT |
954 | /* |
955 | * fasync_helper() is used by almost all character device drivers | |
956 | * to set up the fasync queue, and for regular files by the file | |
957 | * lease code. It returns negative on error, 0 if it did no changes | |
958 | * and positive if it added/deleted the entry. | |
959 | */ | |
960 | int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp) | |
961 | { | |
962 | if (!on) | |
963 | return fasync_remove_entry(filp, fapp); | |
964 | return fasync_add_entry(fd, filp, fapp); | |
965 | } | |
966 | ||
1da177e4 LT |
967 | EXPORT_SYMBOL(fasync_helper); |
968 | ||
989a2979 ED |
969 | /* |
970 | * rcu_read_lock() is held | |
971 | */ | |
972 | static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band) | |
1da177e4 LT |
973 | { |
974 | while (fa) { | |
989a2979 | 975 | struct fown_struct *fown; |
f4985dc7 AM |
976 | unsigned long flags; |
977 | ||
1da177e4 LT |
978 | if (fa->magic != FASYNC_MAGIC) { |
979 | printk(KERN_ERR "kill_fasync: bad magic number in " | |
980 | "fasync_struct!\n"); | |
981 | return; | |
982 | } | |
f4985dc7 | 983 | spin_lock_irqsave(&fa->fa_lock, flags); |
989a2979 ED |
984 | if (fa->fa_file) { |
985 | fown = &fa->fa_file->f_owner; | |
986 | /* Don't send SIGURG to processes which have not set a | |
987 | queued signum: SIGURG has its own default signalling | |
988 | mechanism. */ | |
989 | if (!(sig == SIGURG && fown->signum == 0)) | |
990 | send_sigio(fown, fa->fa_fd, band); | |
991 | } | |
f4985dc7 | 992 | spin_unlock_irqrestore(&fa->fa_lock, flags); |
989a2979 | 993 | fa = rcu_dereference(fa->fa_next); |
1da177e4 LT |
994 | } |
995 | } | |
996 | ||
1da177e4 LT |
997 | void kill_fasync(struct fasync_struct **fp, int sig, int band) |
998 | { | |
999 | /* First a quick test without locking: usually | |
1000 | * the list is empty. | |
1001 | */ | |
1002 | if (*fp) { | |
989a2979 ED |
1003 | rcu_read_lock(); |
1004 | kill_fasync_rcu(rcu_dereference(*fp), sig, band); | |
1005 | rcu_read_unlock(); | |
1da177e4 LT |
1006 | } |
1007 | } | |
1008 | EXPORT_SYMBOL(kill_fasync); | |
1009 | ||
454eedb8 | 1010 | static int __init fcntl_init(void) |
1da177e4 | 1011 | { |
3ab04d5c JB |
1012 | /* |
1013 | * Please add new bits here to ensure allocation uniqueness. | |
1014 | * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY | |
1015 | * is defined as O_NONBLOCK on some platforms and not on others. | |
1016 | */ | |
80f18379 CH |
1017 | BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ != |
1018 | HWEIGHT32( | |
1019 | (VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) | | |
1020 | __FMODE_EXEC | __FMODE_NONOTIFY)); | |
454eedb8 | 1021 | |
1da177e4 | 1022 | fasync_cache = kmem_cache_create("fasync_cache", |
20c2df83 | 1023 | sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL); |
1da177e4 LT |
1024 | return 0; |
1025 | } | |
1026 | ||
454eedb8 | 1027 | module_init(fcntl_init) |