fcntl.c 23 KB

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