fcntl.c 26 KB

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