fcntl.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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. /* Make sure we are called with one of the POLL_*
  662. reasons, otherwise we could leak kernel stack into
  663. userspace. */
  664. BUG_ON((reason & __SI_MASK) != __SI_POLL);
  665. if (reason - POLL_IN >= NSIGPOLL)
  666. si.si_band = ~0L;
  667. else
  668. si.si_band = band_table[reason - POLL_IN];
  669. si.si_fd = fd;
  670. if (!do_send_sig_info(signum, &si, p, group))
  671. break;
  672. /* fall-through: fall back on the old plain SIGIO signal */
  673. case 0:
  674. do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, group);
  675. }
  676. }
  677. void send_sigio(struct fown_struct *fown, int fd, int band)
  678. {
  679. struct task_struct *p;
  680. enum pid_type type;
  681. struct pid *pid;
  682. int group = 1;
  683. read_lock(&fown->lock);
  684. type = fown->pid_type;
  685. if (type == PIDTYPE_MAX) {
  686. group = 0;
  687. type = PIDTYPE_PID;
  688. }
  689. pid = fown->pid;
  690. if (!pid)
  691. goto out_unlock_fown;
  692. read_lock(&tasklist_lock);
  693. do_each_pid_task(pid, type, p) {
  694. send_sigio_to_task(p, fown, fd, band, group);
  695. } while_each_pid_task(pid, type, p);
  696. read_unlock(&tasklist_lock);
  697. out_unlock_fown:
  698. read_unlock(&fown->lock);
  699. }
  700. static void send_sigurg_to_task(struct task_struct *p,
  701. struct fown_struct *fown, int group)
  702. {
  703. if (sigio_perm(p, fown, SIGURG))
  704. do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, group);
  705. }
  706. int send_sigurg(struct fown_struct *fown)
  707. {
  708. struct task_struct *p;
  709. enum pid_type type;
  710. struct pid *pid;
  711. int group = 1;
  712. int ret = 0;
  713. read_lock(&fown->lock);
  714. type = fown->pid_type;
  715. if (type == PIDTYPE_MAX) {
  716. group = 0;
  717. type = PIDTYPE_PID;
  718. }
  719. pid = fown->pid;
  720. if (!pid)
  721. goto out_unlock_fown;
  722. ret = 1;
  723. read_lock(&tasklist_lock);
  724. do_each_pid_task(pid, type, p) {
  725. send_sigurg_to_task(p, fown, group);
  726. } while_each_pid_task(pid, type, p);
  727. read_unlock(&tasklist_lock);
  728. out_unlock_fown:
  729. read_unlock(&fown->lock);
  730. return ret;
  731. }
  732. static DEFINE_SPINLOCK(fasync_lock);
  733. static struct kmem_cache *fasync_cache __read_mostly;
  734. static void fasync_free_rcu(struct rcu_head *head)
  735. {
  736. kmem_cache_free(fasync_cache,
  737. container_of(head, struct fasync_struct, fa_rcu));
  738. }
  739. /*
  740. * Remove a fasync entry. If successfully removed, return
  741. * positive and clear the FASYNC flag. If no entry exists,
  742. * do nothing and return 0.
  743. *
  744. * NOTE! It is very important that the FASYNC flag always
  745. * match the state "is the filp on a fasync list".
  746. *
  747. */
  748. int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp)
  749. {
  750. struct fasync_struct *fa, **fp;
  751. int result = 0;
  752. spin_lock(&filp->f_lock);
  753. spin_lock(&fasync_lock);
  754. for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
  755. if (fa->fa_file != filp)
  756. continue;
  757. spin_lock_irq(&fa->fa_lock);
  758. fa->fa_file = NULL;
  759. spin_unlock_irq(&fa->fa_lock);
  760. *fp = fa->fa_next;
  761. call_rcu(&fa->fa_rcu, fasync_free_rcu);
  762. filp->f_flags &= ~FASYNC;
  763. result = 1;
  764. break;
  765. }
  766. spin_unlock(&fasync_lock);
  767. spin_unlock(&filp->f_lock);
  768. return result;
  769. }
  770. struct fasync_struct *fasync_alloc(void)
  771. {
  772. return kmem_cache_alloc(fasync_cache, GFP_KERNEL);
  773. }
  774. /*
  775. * NOTE! This can be used only for unused fasync entries:
  776. * entries that actually got inserted on the fasync list
  777. * need to be released by rcu - see fasync_remove_entry.
  778. */
  779. void fasync_free(struct fasync_struct *new)
  780. {
  781. kmem_cache_free(fasync_cache, new);
  782. }
  783. /*
  784. * Insert a new entry into the fasync list. Return the pointer to the
  785. * old one if we didn't use the new one.
  786. *
  787. * NOTE! It is very important that the FASYNC flag always
  788. * match the state "is the filp on a fasync list".
  789. */
  790. struct fasync_struct *fasync_insert_entry(int fd, struct file *filp, struct fasync_struct **fapp, struct fasync_struct *new)
  791. {
  792. struct fasync_struct *fa, **fp;
  793. spin_lock(&filp->f_lock);
  794. spin_lock(&fasync_lock);
  795. for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
  796. if (fa->fa_file != filp)
  797. continue;
  798. spin_lock_irq(&fa->fa_lock);
  799. fa->fa_fd = fd;
  800. spin_unlock_irq(&fa->fa_lock);
  801. goto out;
  802. }
  803. spin_lock_init(&new->fa_lock);
  804. new->magic = FASYNC_MAGIC;
  805. new->fa_file = filp;
  806. new->fa_fd = fd;
  807. new->fa_next = *fapp;
  808. rcu_assign_pointer(*fapp, new);
  809. filp->f_flags |= FASYNC;
  810. out:
  811. spin_unlock(&fasync_lock);
  812. spin_unlock(&filp->f_lock);
  813. return fa;
  814. }
  815. /*
  816. * Add a fasync entry. Return negative on error, positive if
  817. * added, and zero if did nothing but change an existing one.
  818. */
  819. static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fapp)
  820. {
  821. struct fasync_struct *new;
  822. new = fasync_alloc();
  823. if (!new)
  824. return -ENOMEM;
  825. /*
  826. * fasync_insert_entry() returns the old (update) entry if
  827. * it existed.
  828. *
  829. * So free the (unused) new entry and return 0 to let the
  830. * caller know that we didn't add any new fasync entries.
  831. */
  832. if (fasync_insert_entry(fd, filp, fapp, new)) {
  833. fasync_free(new);
  834. return 0;
  835. }
  836. return 1;
  837. }
  838. /*
  839. * fasync_helper() is used by almost all character device drivers
  840. * to set up the fasync queue, and for regular files by the file
  841. * lease code. It returns negative on error, 0 if it did no changes
  842. * and positive if it added/deleted the entry.
  843. */
  844. int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
  845. {
  846. if (!on)
  847. return fasync_remove_entry(filp, fapp);
  848. return fasync_add_entry(fd, filp, fapp);
  849. }
  850. EXPORT_SYMBOL(fasync_helper);
  851. /*
  852. * rcu_read_lock() is held
  853. */
  854. static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band)
  855. {
  856. while (fa) {
  857. struct fown_struct *fown;
  858. unsigned long flags;
  859. if (fa->magic != FASYNC_MAGIC) {
  860. printk(KERN_ERR "kill_fasync: bad magic number in "
  861. "fasync_struct!\n");
  862. return;
  863. }
  864. spin_lock_irqsave(&fa->fa_lock, flags);
  865. if (fa->fa_file) {
  866. fown = &fa->fa_file->f_owner;
  867. /* Don't send SIGURG to processes which have not set a
  868. queued signum: SIGURG has its own default signalling
  869. mechanism. */
  870. if (!(sig == SIGURG && fown->signum == 0))
  871. send_sigio(fown, fa->fa_fd, band);
  872. }
  873. spin_unlock_irqrestore(&fa->fa_lock, flags);
  874. fa = rcu_dereference(fa->fa_next);
  875. }
  876. }
  877. void kill_fasync(struct fasync_struct **fp, int sig, int band)
  878. {
  879. /* First a quick test without locking: usually
  880. * the list is empty.
  881. */
  882. if (*fp) {
  883. rcu_read_lock();
  884. kill_fasync_rcu(rcu_dereference(*fp), sig, band);
  885. rcu_read_unlock();
  886. }
  887. }
  888. EXPORT_SYMBOL(kill_fasync);
  889. static int __init fcntl_init(void)
  890. {
  891. /*
  892. * Please add new bits here to ensure allocation uniqueness.
  893. * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
  894. * is defined as O_NONBLOCK on some platforms and not on others.
  895. */
  896. BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=
  897. HWEIGHT32(
  898. (VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
  899. __FMODE_EXEC | __FMODE_NONOTIFY));
  900. fasync_cache = kmem_cache_create("fasync_cache",
  901. sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
  902. return 0;
  903. }
  904. module_init(fcntl_init)