]>
Commit | Line | Data |
---|---|---|
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> | |
10 | #include <linux/sched/task.h> | |
11 | #include <linux/fs.h> | |
12 | #include <linux/file.h> | |
13 | #include <linux/fdtable.h> | |
14 | #include <linux/capability.h> | |
15 | #include <linux/dnotify.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/module.h> | |
18 | #include <linux/pipe_fs_i.h> | |
19 | #include <linux/security.h> | |
20 | #include <linux/ptrace.h> | |
21 | #include <linux/signal.h> | |
22 | #include <linux/rcupdate.h> | |
23 | #include <linux/pid_namespace.h> | |
24 | #include <linux/user_namespace.h> | |
25 | #include <linux/shmem_fs.h> | |
26 | #include <linux/compat.h> | |
27 | ||
28 | #include <asm/poll.h> | |
29 | #include <asm/siginfo.h> | |
30 | #include <linux/uaccess.h> | |
31 | ||
32 | #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME) | |
33 | ||
34 | int setfl(int fd, struct file * filp, unsigned long arg) | |
35 | { | |
36 | struct inode * inode = file_inode(filp); | |
37 | int error = 0; | |
38 | ||
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)) | |
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)) | |
48 | if (!inode_owner_or_capable(inode)) | |
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 | ||
56 | /* Pipe packetized mode is controlled by O_DIRECT flag */ | |
57 | if (!S_ISFIFO(inode->i_mode) && (arg & O_DIRECT)) { | |
58 | if (!filp->f_mapping || !filp->f_mapping->a_ops || | |
59 | !filp->f_mapping->a_ops->direct_IO) | |
60 | return -EINVAL; | |
61 | } | |
62 | ||
63 | if (filp->f_op->check_flags) | |
64 | error = filp->f_op->check_flags(arg); | |
65 | if (!error && filp->f_op->setfl) | |
66 | error = filp->f_op->setfl(filp, arg); | |
67 | if (error) | |
68 | return error; | |
69 | ||
70 | /* | |
71 | * ->fasync() is responsible for setting the FASYNC bit. | |
72 | */ | |
73 | if (((arg ^ filp->f_flags) & FASYNC) && filp->f_op->fasync) { | |
74 | error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0); | |
75 | if (error < 0) | |
76 | goto out; | |
77 | if (error > 0) | |
78 | error = 0; | |
79 | } | |
80 | spin_lock(&filp->f_lock); | |
81 | filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK); | |
82 | spin_unlock(&filp->f_lock); | |
83 | ||
84 | out: | |
85 | return error; | |
86 | } | |
87 | EXPORT_SYMBOL_GPL(setfl); | |
88 | ||
89 | static void f_modown(struct file *filp, struct pid *pid, enum pid_type type, | |
90 | int force) | |
91 | { | |
92 | write_lock_irq(&filp->f_owner.lock); | |
93 | if (force || !filp->f_owner.pid) { | |
94 | put_pid(filp->f_owner.pid); | |
95 | filp->f_owner.pid = get_pid(pid); | |
96 | filp->f_owner.pid_type = type; | |
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 | } | |
103 | } | |
104 | write_unlock_irq(&filp->f_owner.lock); | |
105 | } | |
106 | ||
107 | void __f_setown(struct file *filp, struct pid *pid, enum pid_type type, | |
108 | int force) | |
109 | { | |
110 | security_file_set_fowner(filp); | |
111 | f_modown(filp, pid, type, force); | |
112 | } | |
113 | EXPORT_SYMBOL(__f_setown); | |
114 | ||
115 | int f_setown(struct file *filp, unsigned long arg, int force) | |
116 | { | |
117 | enum pid_type type; | |
118 | struct pid *pid = NULL; | |
119 | int who = arg, ret = 0; | |
120 | ||
121 | type = PIDTYPE_PID; | |
122 | if (who < 0) { | |
123 | /* avoid overflow below */ | |
124 | if (who == INT_MIN) | |
125 | return -EINVAL; | |
126 | ||
127 | type = PIDTYPE_PGID; | |
128 | who = -who; | |
129 | } | |
130 | ||
131 | rcu_read_lock(); | |
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); | |
140 | rcu_read_unlock(); | |
141 | ||
142 | return ret; | |
143 | } | |
144 | EXPORT_SYMBOL(f_setown); | |
145 | ||
146 | void f_delown(struct file *filp) | |
147 | { | |
148 | f_modown(filp, NULL, PIDTYPE_PID, 1); | |
149 | } | |
150 | ||
151 | pid_t f_getown(struct file *filp) | |
152 | { | |
153 | pid_t pid; | |
154 | read_lock(&filp->f_owner.lock); | |
155 | pid = pid_vnr(filp->f_owner.pid); | |
156 | if (filp->f_owner.pid_type == PIDTYPE_PGID) | |
157 | pid = -pid; | |
158 | read_unlock(&filp->f_owner.lock); | |
159 | return pid; | |
160 | } | |
161 | ||
162 | static int f_setown_ex(struct file *filp, unsigned long arg) | |
163 | { | |
164 | struct f_owner_ex __user *owner_p = (void __user *)arg; | |
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) | |
172 | return -EFAULT; | |
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 | ||
183 | case F_OWNER_PGRP: | |
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 | |
196 | __f_setown(filp, pid, type, 1); | |
197 | rcu_read_unlock(); | |
198 | ||
199 | return ret; | |
200 | } | |
201 | ||
202 | static int f_getown_ex(struct file *filp, unsigned long arg) | |
203 | { | |
204 | struct f_owner_ex __user *owner_p = (void __user *)arg; | |
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: | |
220 | owner.type = F_OWNER_PGRP; | |
221 | break; | |
222 | ||
223 | default: | |
224 | WARN_ON(1); | |
225 | ret = -EINVAL; | |
226 | break; | |
227 | } | |
228 | read_unlock(&filp->f_owner.lock); | |
229 | ||
230 | if (!ret) { | |
231 | ret = copy_to_user(owner_p, &owner, sizeof(owner)); | |
232 | if (ret) | |
233 | ret = -EFAULT; | |
234 | } | |
235 | return ret; | |
236 | } | |
237 | ||
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(); | |
242 | uid_t __user *dst = (void __user *)arg; | |
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 | ||
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; | |
284 | u64 h; | |
285 | ||
286 | switch (cmd) { | |
287 | case F_GET_FILE_RW_HINT: | |
288 | h = file_write_hint(file); | |
289 | if (copy_to_user(argp, &h, sizeof(*argp))) | |
290 | return -EFAULT; | |
291 | return 0; | |
292 | case F_SET_FILE_RW_HINT: | |
293 | if (copy_from_user(&h, argp, sizeof(h))) | |
294 | return -EFAULT; | |
295 | hint = (enum rw_hint) h; | |
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: | |
304 | h = inode->i_write_hint; | |
305 | if (copy_to_user(argp, &h, sizeof(*argp))) | |
306 | return -EFAULT; | |
307 | return 0; | |
308 | case F_SET_RW_HINT: | |
309 | if (copy_from_user(&h, argp, sizeof(h))) | |
310 | return -EFAULT; | |
311 | hint = (enum rw_hint) h; | |
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 | ||
324 | static long do_fcntl(int fd, unsigned int cmd, unsigned long arg, | |
325 | struct file *filp) | |
326 | { | |
327 | void __user *argp = (void __user *)arg; | |
328 | struct flock flock; | |
329 | long err = -EINVAL; | |
330 | ||
331 | switch (cmd) { | |
332 | case F_DUPFD: | |
333 | err = f_dupfd(arg, filp, 0); | |
334 | break; | |
335 | case F_DUPFD_CLOEXEC: | |
336 | err = f_dupfd(arg, filp, O_CLOEXEC); | |
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; | |
351 | #if BITS_PER_LONG != 32 | |
352 | /* 32-bit arches must use fcntl64() */ | |
353 | case F_OFD_GETLK: | |
354 | #endif | |
355 | case F_GETLK: | |
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; | |
361 | break; | |
362 | #if BITS_PER_LONG != 32 | |
363 | /* 32-bit arches must use fcntl64() */ | |
364 | case F_OFD_SETLK: | |
365 | case F_OFD_SETLKW: | |
366 | #endif | |
367 | /* Fallthrough */ | |
368 | case F_SETLK: | |
369 | case F_SETLKW: | |
370 | if (copy_from_user(&flock, argp, sizeof(flock))) | |
371 | return -EFAULT; | |
372 | err = fcntl_setlk(fd, filp, cmd, &flock); | |
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 | */ | |
382 | err = f_getown(filp); | |
383 | force_successful_syscall_return(); | |
384 | break; | |
385 | case F_SETOWN: | |
386 | err = f_setown(filp, arg, 1); | |
387 | break; | |
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; | |
394 | case F_GETOWNER_UIDS: | |
395 | err = f_getowner_uids(filp, arg); | |
396 | break; | |
397 | case F_GETSIG: | |
398 | err = filp->f_owner.signum; | |
399 | break; | |
400 | case F_SETSIG: | |
401 | /* arg == 0 restores default behaviour. */ | |
402 | if (!valid_signal(arg)) { | |
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; | |
417 | case F_SETPIPE_SZ: | |
418 | case F_GETPIPE_SZ: | |
419 | err = pipe_fcntl(filp, cmd, arg); | |
420 | break; | |
421 | case F_ADD_SEALS: | |
422 | case F_GET_SEALS: | |
423 | err = shmem_fcntl(filp, cmd, arg); | |
424 | break; | |
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; | |
431 | default: | |
432 | break; | |
433 | } | |
434 | return err; | |
435 | } | |
436 | ||
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 | ||
450 | SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg) | |
451 | { | |
452 | struct fd f = fdget_raw(fd); | |
453 | long err = -EBADF; | |
454 | ||
455 | if (!f.file) | |
456 | goto out; | |
457 | ||
458 | if (unlikely(f.file->f_mode & FMODE_PATH)) { | |
459 | if (!check_fcntl_cmd(cmd)) | |
460 | goto out1; | |
461 | } | |
462 | ||
463 | err = security_file_fcntl(f.file, cmd, arg); | |
464 | if (!err) | |
465 | err = do_fcntl(fd, cmd, arg, f.file); | |
466 | ||
467 | out1: | |
468 | fdput(f); | |
469 | out: | |
470 | return err; | |
471 | } | |
472 | ||
473 | #if BITS_PER_LONG == 32 | |
474 | SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd, | |
475 | unsigned long, arg) | |
476 | { | |
477 | void __user *argp = (void __user *)arg; | |
478 | struct fd f = fdget_raw(fd); | |
479 | struct flock64 flock; | |
480 | long err = -EBADF; | |
481 | ||
482 | if (!f.file) | |
483 | goto out; | |
484 | ||
485 | if (unlikely(f.file->f_mode & FMODE_PATH)) { | |
486 | if (!check_fcntl_cmd(cmd)) | |
487 | goto out1; | |
488 | } | |
489 | ||
490 | err = security_file_fcntl(f.file, cmd, arg); | |
491 | if (err) | |
492 | goto out1; | |
493 | ||
494 | switch (cmd) { | |
495 | case F_GETLK64: | |
496 | case F_OFD_GETLK: | |
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; | |
503 | break; | |
504 | case F_SETLK64: | |
505 | case F_SETLKW64: | |
506 | case F_OFD_SETLK: | |
507 | case F_OFD_SETLKW: | |
508 | err = -EFAULT; | |
509 | if (copy_from_user(&flock, argp, sizeof(flock))) | |
510 | break; | |
511 | err = fcntl_setlk64(fd, f.file, cmd, &flock); | |
512 | break; | |
513 | default: | |
514 | err = do_fcntl(fd, cmd, arg, f.file); | |
515 | break; | |
516 | } | |
517 | out1: | |
518 | fdput(f); | |
519 | out: | |
520 | return err; | |
521 | } | |
522 | #endif | |
523 | ||
524 | #ifdef CONFIG_COMPAT | |
525 | /* careful - don't use anywhere else */ | |
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) | |
534 | { | |
535 | struct compat_flock fl; | |
536 | ||
537 | if (copy_from_user(&fl, ufl, sizeof(struct compat_flock))) | |
538 | return -EFAULT; | |
539 | copy_flock_fields(kfl, &fl); | |
540 | return 0; | |
541 | } | |
542 | ||
543 | static int get_compat_flock64(struct flock *kfl, const struct compat_flock64 __user *ufl) | |
544 | { | |
545 | struct compat_flock64 fl; | |
546 | ||
547 | if (copy_from_user(&fl, ufl, sizeof(struct compat_flock64))) | |
548 | return -EFAULT; | |
549 | copy_flock_fields(kfl, &fl); | |
550 | return 0; | |
551 | } | |
552 | ||
553 | static int put_compat_flock(const struct flock *kfl, struct compat_flock __user *ufl) | |
554 | { | |
555 | struct compat_flock fl; | |
556 | ||
557 | memset(&fl, 0, sizeof(struct compat_flock)); | |
558 | copy_flock_fields(&fl, kfl); | |
559 | if (copy_to_user(ufl, &fl, sizeof(struct compat_flock))) | |
560 | return -EFAULT; | |
561 | return 0; | |
562 | } | |
563 | ||
564 | static int put_compat_flock64(const struct flock *kfl, struct compat_flock64 __user *ufl) | |
565 | { | |
566 | struct compat_flock64 fl; | |
567 | ||
568 | memset(&fl, 0, sizeof(struct compat_flock64)); | |
569 | copy_flock_fields(&fl, kfl); | |
570 | if (copy_to_user(ufl, &fl, sizeof(struct compat_flock64))) | |
571 | return -EFAULT; | |
572 | return 0; | |
573 | } | |
574 | #undef copy_flock_fields | |
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 | ||
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 | ||
609 | COMPAT_SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd, | |
610 | compat_ulong_t, arg) | |
611 | { | |
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; | |
627 | ||
628 | switch (cmd) { | |
629 | case F_GETLK: | |
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; | |
654 | case F_SETLK: | |
655 | case F_SETLKW: | |
656 | err = get_compat_flock(&flock, compat_ptr(arg)); | |
657 | if (err) | |
658 | break; | |
659 | err = fcntl_setlk(fd, f.file, convert_fcntl_cmd(cmd), &flock); | |
660 | break; | |
661 | case F_SETLK64: | |
662 | case F_SETLKW64: | |
663 | case F_OFD_SETLK: | |
664 | case F_OFD_SETLKW: | |
665 | err = get_compat_flock64(&flock, compat_ptr(arg)); | |
666 | if (err) | |
667 | break; | |
668 | err = fcntl_setlk(fd, f.file, convert_fcntl_cmd(cmd), &flock); | |
669 | break; | |
670 | default: | |
671 | err = do_fcntl(fd, cmd, arg, f.file); | |
672 | break; | |
673 | } | |
674 | out_put: | |
675 | fdput(f); | |
676 | return err; | |
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 | ||
695 | /* Table to convert sigio signal codes into poll band bitmaps */ | |
696 | ||
697 | static const long band_table[NSIGPOLL] = { | |
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 | { | |
709 | const struct cred *cred; | |
710 | int ret; | |
711 | ||
712 | rcu_read_lock(); | |
713 | cred = __task_cred(p); | |
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)) && | |
717 | !security_file_send_sigiotask(p, fown, sig)); | |
718 | rcu_read_unlock(); | |
719 | return ret; | |
720 | } | |
721 | ||
722 | static void send_sigio_to_task(struct task_struct *p, | |
723 | struct fown_struct *fown, | |
724 | int fd, int reason, int group) | |
725 | { | |
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)) | |
733 | return; | |
734 | ||
735 | switch (signum) { | |
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 */ | |
744 | si.si_signo = signum; | |
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. */ | |
750 | BUG_ON((reason & __SI_MASK) != __SI_POLL); | |
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; | |
756 | if (!do_send_sig_info(signum, &si, p, group)) | |
757 | break; | |
758 | /* fall-through: fall back on the old plain SIGIO signal */ | |
759 | case 0: | |
760 | do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, group); | |
761 | } | |
762 | } | |
763 | ||
764 | void send_sigio(struct fown_struct *fown, int fd, int band) | |
765 | { | |
766 | struct task_struct *p; | |
767 | enum pid_type type; | |
768 | struct pid *pid; | |
769 | int group = 1; | |
770 | ||
771 | read_lock(&fown->lock); | |
772 | ||
773 | type = fown->pid_type; | |
774 | if (type == PIDTYPE_MAX) { | |
775 | group = 0; | |
776 | type = PIDTYPE_PID; | |
777 | } | |
778 | ||
779 | pid = fown->pid; | |
780 | if (!pid) | |
781 | goto out_unlock_fown; | |
782 | ||
783 | read_lock(&tasklist_lock); | |
784 | do_each_pid_task(pid, type, p) { | |
785 | send_sigio_to_task(p, fown, fd, band, group); | |
786 | } while_each_pid_task(pid, type, p); | |
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, | |
793 | struct fown_struct *fown, int group) | |
794 | { | |
795 | if (sigio_perm(p, fown, SIGURG)) | |
796 | do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, group); | |
797 | } | |
798 | ||
799 | int send_sigurg(struct fown_struct *fown) | |
800 | { | |
801 | struct task_struct *p; | |
802 | enum pid_type type; | |
803 | struct pid *pid; | |
804 | int group = 1; | |
805 | int ret = 0; | |
806 | ||
807 | read_lock(&fown->lock); | |
808 | ||
809 | type = fown->pid_type; | |
810 | if (type == PIDTYPE_MAX) { | |
811 | group = 0; | |
812 | type = PIDTYPE_PID; | |
813 | } | |
814 | ||
815 | pid = fown->pid; | |
816 | if (!pid) | |
817 | goto out_unlock_fown; | |
818 | ||
819 | ret = 1; | |
820 | ||
821 | read_lock(&tasklist_lock); | |
822 | do_each_pid_task(pid, type, p) { | |
823 | send_sigurg_to_task(p, fown, group); | |
824 | } while_each_pid_task(pid, type, p); | |
825 | read_unlock(&tasklist_lock); | |
826 | out_unlock_fown: | |
827 | read_unlock(&fown->lock); | |
828 | return ret; | |
829 | } | |
830 | ||
831 | static DEFINE_SPINLOCK(fasync_lock); | |
832 | static struct kmem_cache *fasync_cache __read_mostly; | |
833 | ||
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 | ||
840 | /* | |
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 | * | |
848 | */ | |
849 | int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp) | |
850 | { | |
851 | struct fasync_struct *fa, **fp; | |
852 | int result = 0; | |
853 | ||
854 | spin_lock(&filp->f_lock); | |
855 | spin_lock(&fasync_lock); | |
856 | for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) { | |
857 | if (fa->fa_file != filp) | |
858 | continue; | |
859 | ||
860 | spin_lock_irq(&fa->fa_lock); | |
861 | fa->fa_file = NULL; | |
862 | spin_unlock_irq(&fa->fa_lock); | |
863 | ||
864 | *fp = fa->fa_next; | |
865 | call_rcu(&fa->fa_rcu, fasync_free_rcu); | |
866 | filp->f_flags &= ~FASYNC; | |
867 | result = 1; | |
868 | break; | |
869 | } | |
870 | spin_unlock(&fasync_lock); | |
871 | spin_unlock(&filp->f_lock); | |
872 | return result; | |
873 | } | |
874 | ||
875 | struct fasync_struct *fasync_alloc(void) | |
876 | { | |
877 | return kmem_cache_alloc(fasync_cache, GFP_KERNEL); | |
878 | } | |
879 | ||
880 | /* | |
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. | |
884 | */ | |
885 | void fasync_free(struct fasync_struct *new) | |
886 | { | |
887 | kmem_cache_free(fasync_cache, new); | |
888 | } | |
889 | ||
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. | |
893 | * | |
894 | * NOTE! It is very important that the FASYNC flag always | |
895 | * match the state "is the filp on a fasync list". | |
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; | |
900 | ||
901 | spin_lock(&filp->f_lock); | |
902 | spin_lock(&fasync_lock); | |
903 | for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) { | |
904 | if (fa->fa_file != filp) | |
905 | continue; | |
906 | ||
907 | spin_lock_irq(&fa->fa_lock); | |
908 | fa->fa_fd = fd; | |
909 | spin_unlock_irq(&fa->fa_lock); | |
910 | goto out; | |
911 | } | |
912 | ||
913 | spin_lock_init(&new->fa_lock); | |
914 | new->magic = FASYNC_MAGIC; | |
915 | new->fa_file = filp; | |
916 | new->fa_fd = fd; | |
917 | new->fa_next = *fapp; | |
918 | rcu_assign_pointer(*fapp, new); | |
919 | filp->f_flags |= FASYNC; | |
920 | ||
921 | out: | |
922 | spin_unlock(&fasync_lock); | |
923 | spin_unlock(&filp->f_lock); | |
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. | |
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; | |
952 | } | |
953 | ||
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 | ||
967 | EXPORT_SYMBOL(fasync_helper); | |
968 | ||
969 | /* | |
970 | * rcu_read_lock() is held | |
971 | */ | |
972 | static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band) | |
973 | { | |
974 | while (fa) { | |
975 | struct fown_struct *fown; | |
976 | unsigned long flags; | |
977 | ||
978 | if (fa->magic != FASYNC_MAGIC) { | |
979 | printk(KERN_ERR "kill_fasync: bad magic number in " | |
980 | "fasync_struct!\n"); | |
981 | return; | |
982 | } | |
983 | spin_lock_irqsave(&fa->fa_lock, flags); | |
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 | } | |
992 | spin_unlock_irqrestore(&fa->fa_lock, flags); | |
993 | fa = rcu_dereference(fa->fa_next); | |
994 | } | |
995 | } | |
996 | ||
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) { | |
1003 | rcu_read_lock(); | |
1004 | kill_fasync_rcu(rcu_dereference(*fp), sig, band); | |
1005 | rcu_read_unlock(); | |
1006 | } | |
1007 | } | |
1008 | EXPORT_SYMBOL(kill_fasync); | |
1009 | ||
1010 | static int __init fcntl_init(void) | |
1011 | { | |
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 | */ | |
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)); | |
1021 | ||
1022 | fasync_cache = kmem_cache_create("fasync_cache", | |
1023 | sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL); | |
1024 | return 0; | |
1025 | } | |
1026 | ||
1027 | module_init(fcntl_init) |