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