fcntl.c 23 KB

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