fcntl.c 23 KB

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