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