fcntl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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/fs.h>
  10. #include <linux/file.h>
  11. #include <linux/fdtable.h>
  12. #include <linux/capability.h>
  13. #include <linux/dnotify.h>
  14. #include <linux/slab.h>
  15. #include <linux/module.h>
  16. #include <linux/security.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/signal.h>
  19. #include <linux/rcupdate.h>
  20. #include <linux/pid_namespace.h>
  21. #include <linux/smp_lock.h>
  22. #include <asm/poll.h>
  23. #include <asm/siginfo.h>
  24. #include <asm/uaccess.h>
  25. void set_close_on_exec(unsigned int fd, int flag)
  26. {
  27. struct files_struct *files = current->files;
  28. struct fdtable *fdt;
  29. spin_lock(&files->file_lock);
  30. fdt = files_fdtable(files);
  31. if (flag)
  32. FD_SET(fd, fdt->close_on_exec);
  33. else
  34. FD_CLR(fd, fdt->close_on_exec);
  35. spin_unlock(&files->file_lock);
  36. }
  37. static int get_close_on_exec(unsigned int fd)
  38. {
  39. struct files_struct *files = current->files;
  40. struct fdtable *fdt;
  41. int res;
  42. rcu_read_lock();
  43. fdt = files_fdtable(files);
  44. res = FD_ISSET(fd, fdt->close_on_exec);
  45. rcu_read_unlock();
  46. return res;
  47. }
  48. asmlinkage long sys_dup3(unsigned int oldfd, unsigned int newfd, int flags)
  49. {
  50. int err = -EBADF;
  51. struct file * file, *tofree;
  52. struct files_struct * files = current->files;
  53. struct fdtable *fdt;
  54. if ((flags & ~O_CLOEXEC) != 0)
  55. return -EINVAL;
  56. if (unlikely(oldfd == newfd))
  57. return -EINVAL;
  58. spin_lock(&files->file_lock);
  59. err = expand_files(files, newfd);
  60. file = fcheck(oldfd);
  61. if (unlikely(!file))
  62. goto Ebadf;
  63. if (unlikely(err < 0)) {
  64. if (err == -EMFILE)
  65. goto Ebadf;
  66. goto out_unlock;
  67. }
  68. /*
  69. * We need to detect attempts to do dup2() over allocated but still
  70. * not finished descriptor. NB: OpenBSD avoids that at the price of
  71. * extra work in their equivalent of fget() - they insert struct
  72. * file immediately after grabbing descriptor, mark it larval if
  73. * more work (e.g. actual opening) is needed and make sure that
  74. * fget() treats larval files as absent. Potentially interesting,
  75. * but while extra work in fget() is trivial, locking implications
  76. * and amount of surgery on open()-related paths in VFS are not.
  77. * FreeBSD fails with -EBADF in the same situation, NetBSD "solution"
  78. * deadlocks in rather amusing ways, AFAICS. All of that is out of
  79. * scope of POSIX or SUS, since neither considers shared descriptor
  80. * tables and this condition does not arise without those.
  81. */
  82. err = -EBUSY;
  83. fdt = files_fdtable(files);
  84. tofree = fdt->fd[newfd];
  85. if (!tofree && FD_ISSET(newfd, fdt->open_fds))
  86. goto out_unlock;
  87. get_file(file);
  88. rcu_assign_pointer(fdt->fd[newfd], file);
  89. FD_SET(newfd, fdt->open_fds);
  90. if (flags & O_CLOEXEC)
  91. FD_SET(newfd, fdt->close_on_exec);
  92. else
  93. FD_CLR(newfd, fdt->close_on_exec);
  94. spin_unlock(&files->file_lock);
  95. if (tofree)
  96. filp_close(tofree, files);
  97. return newfd;
  98. Ebadf:
  99. err = -EBADF;
  100. out_unlock:
  101. spin_unlock(&files->file_lock);
  102. return err;
  103. }
  104. asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd)
  105. {
  106. if (unlikely(newfd == oldfd)) { /* corner case */
  107. struct files_struct *files = current->files;
  108. rcu_read_lock();
  109. if (!fcheck_files(files, oldfd))
  110. oldfd = -EBADF;
  111. rcu_read_unlock();
  112. return oldfd;
  113. }
  114. return sys_dup3(oldfd, newfd, 0);
  115. }
  116. asmlinkage long sys_dup(unsigned int fildes)
  117. {
  118. int ret = -EBADF;
  119. struct file *file = fget(fildes);
  120. if (file) {
  121. ret = get_unused_fd();
  122. if (ret >= 0)
  123. fd_install(ret, file);
  124. else
  125. fput(file);
  126. }
  127. return ret;
  128. }
  129. #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | FASYNC | O_DIRECT | O_NOATIME)
  130. static int setfl(int fd, struct file * filp, unsigned long arg)
  131. {
  132. struct inode * inode = filp->f_path.dentry->d_inode;
  133. int error = 0;
  134. /*
  135. * O_APPEND cannot be cleared if the file is marked as append-only
  136. * and the file is open for write.
  137. */
  138. if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode))
  139. return -EPERM;
  140. /* O_NOATIME can only be set by the owner or superuser */
  141. if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
  142. if (!is_owner_or_cap(inode))
  143. return -EPERM;
  144. /* required for strict SunOS emulation */
  145. if (O_NONBLOCK != O_NDELAY)
  146. if (arg & O_NDELAY)
  147. arg |= O_NONBLOCK;
  148. if (arg & O_DIRECT) {
  149. if (!filp->f_mapping || !filp->f_mapping->a_ops ||
  150. !filp->f_mapping->a_ops->direct_IO)
  151. return -EINVAL;
  152. }
  153. if (filp->f_op && filp->f_op->check_flags)
  154. error = filp->f_op->check_flags(arg);
  155. if (error)
  156. return error;
  157. /*
  158. * We still need a lock here for now to keep multiple FASYNC calls
  159. * from racing with each other.
  160. */
  161. lock_kernel();
  162. if ((arg ^ filp->f_flags) & FASYNC) {
  163. if (filp->f_op && filp->f_op->fasync) {
  164. error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
  165. if (error < 0)
  166. goto out;
  167. }
  168. }
  169. filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
  170. out:
  171. unlock_kernel();
  172. return error;
  173. }
  174. static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
  175. uid_t uid, uid_t euid, int force)
  176. {
  177. write_lock_irq(&filp->f_owner.lock);
  178. if (force || !filp->f_owner.pid) {
  179. put_pid(filp->f_owner.pid);
  180. filp->f_owner.pid = get_pid(pid);
  181. filp->f_owner.pid_type = type;
  182. filp->f_owner.uid = uid;
  183. filp->f_owner.euid = euid;
  184. }
  185. write_unlock_irq(&filp->f_owner.lock);
  186. }
  187. int __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
  188. int force)
  189. {
  190. int err;
  191. err = security_file_set_fowner(filp);
  192. if (err)
  193. return err;
  194. f_modown(filp, pid, type, current->uid, current->euid, force);
  195. return 0;
  196. }
  197. EXPORT_SYMBOL(__f_setown);
  198. int f_setown(struct file *filp, unsigned long arg, int force)
  199. {
  200. enum pid_type type;
  201. struct pid *pid;
  202. int who = arg;
  203. int result;
  204. type = PIDTYPE_PID;
  205. if (who < 0) {
  206. type = PIDTYPE_PGID;
  207. who = -who;
  208. }
  209. rcu_read_lock();
  210. pid = find_vpid(who);
  211. result = __f_setown(filp, pid, type, force);
  212. rcu_read_unlock();
  213. return result;
  214. }
  215. EXPORT_SYMBOL(f_setown);
  216. void f_delown(struct file *filp)
  217. {
  218. f_modown(filp, NULL, PIDTYPE_PID, 0, 0, 1);
  219. }
  220. pid_t f_getown(struct file *filp)
  221. {
  222. pid_t pid;
  223. read_lock(&filp->f_owner.lock);
  224. pid = pid_vnr(filp->f_owner.pid);
  225. if (filp->f_owner.pid_type == PIDTYPE_PGID)
  226. pid = -pid;
  227. read_unlock(&filp->f_owner.lock);
  228. return pid;
  229. }
  230. static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
  231. struct file *filp)
  232. {
  233. long err = -EINVAL;
  234. switch (cmd) {
  235. case F_DUPFD:
  236. case F_DUPFD_CLOEXEC:
  237. if (arg >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
  238. break;
  239. err = alloc_fd(arg, cmd == F_DUPFD_CLOEXEC ? O_CLOEXEC : 0);
  240. if (err >= 0) {
  241. get_file(filp);
  242. fd_install(err, filp);
  243. }
  244. break;
  245. case F_GETFD:
  246. err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
  247. break;
  248. case F_SETFD:
  249. err = 0;
  250. set_close_on_exec(fd, arg & FD_CLOEXEC);
  251. break;
  252. case F_GETFL:
  253. err = filp->f_flags;
  254. break;
  255. case F_SETFL:
  256. err = setfl(fd, filp, arg);
  257. break;
  258. case F_GETLK:
  259. err = fcntl_getlk(filp, (struct flock __user *) arg);
  260. break;
  261. case F_SETLK:
  262. case F_SETLKW:
  263. err = fcntl_setlk(fd, filp, cmd, (struct flock __user *) arg);
  264. break;
  265. case F_GETOWN:
  266. /*
  267. * XXX If f_owner is a process group, the
  268. * negative return value will get converted
  269. * into an error. Oops. If we keep the
  270. * current syscall conventions, the only way
  271. * to fix this will be in libc.
  272. */
  273. err = f_getown(filp);
  274. force_successful_syscall_return();
  275. break;
  276. case F_SETOWN:
  277. err = f_setown(filp, arg, 1);
  278. break;
  279. case F_GETSIG:
  280. err = filp->f_owner.signum;
  281. break;
  282. case F_SETSIG:
  283. /* arg == 0 restores default behaviour. */
  284. if (!valid_signal(arg)) {
  285. break;
  286. }
  287. err = 0;
  288. filp->f_owner.signum = arg;
  289. break;
  290. case F_GETLEASE:
  291. err = fcntl_getlease(filp);
  292. break;
  293. case F_SETLEASE:
  294. err = fcntl_setlease(fd, filp, arg);
  295. break;
  296. case F_NOTIFY:
  297. err = fcntl_dirnotify(fd, filp, arg);
  298. break;
  299. default:
  300. break;
  301. }
  302. return err;
  303. }
  304. asmlinkage long sys_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg)
  305. {
  306. struct file *filp;
  307. long err = -EBADF;
  308. filp = fget(fd);
  309. if (!filp)
  310. goto out;
  311. err = security_file_fcntl(filp, cmd, arg);
  312. if (err) {
  313. fput(filp);
  314. return err;
  315. }
  316. err = do_fcntl(fd, cmd, arg, filp);
  317. fput(filp);
  318. out:
  319. return err;
  320. }
  321. #if BITS_PER_LONG == 32
  322. asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
  323. {
  324. struct file * filp;
  325. long err;
  326. err = -EBADF;
  327. filp = fget(fd);
  328. if (!filp)
  329. goto out;
  330. err = security_file_fcntl(filp, cmd, arg);
  331. if (err) {
  332. fput(filp);
  333. return err;
  334. }
  335. err = -EBADF;
  336. switch (cmd) {
  337. case F_GETLK64:
  338. err = fcntl_getlk64(filp, (struct flock64 __user *) arg);
  339. break;
  340. case F_SETLK64:
  341. case F_SETLKW64:
  342. err = fcntl_setlk64(fd, filp, cmd,
  343. (struct flock64 __user *) arg);
  344. break;
  345. default:
  346. err = do_fcntl(fd, cmd, arg, filp);
  347. break;
  348. }
  349. fput(filp);
  350. out:
  351. return err;
  352. }
  353. #endif
  354. /* Table to convert sigio signal codes into poll band bitmaps */
  355. static const long band_table[NSIGPOLL] = {
  356. POLLIN | POLLRDNORM, /* POLL_IN */
  357. POLLOUT | POLLWRNORM | POLLWRBAND, /* POLL_OUT */
  358. POLLIN | POLLRDNORM | POLLMSG, /* POLL_MSG */
  359. POLLERR, /* POLL_ERR */
  360. POLLPRI | POLLRDBAND, /* POLL_PRI */
  361. POLLHUP | POLLERR /* POLL_HUP */
  362. };
  363. static inline int sigio_perm(struct task_struct *p,
  364. struct fown_struct *fown, int sig)
  365. {
  366. return (((fown->euid == 0) ||
  367. (fown->euid == p->suid) || (fown->euid == p->uid) ||
  368. (fown->uid == p->suid) || (fown->uid == p->uid)) &&
  369. !security_file_send_sigiotask(p, fown, sig));
  370. }
  371. static void send_sigio_to_task(struct task_struct *p,
  372. struct fown_struct *fown,
  373. int fd,
  374. int reason)
  375. {
  376. if (!sigio_perm(p, fown, fown->signum))
  377. return;
  378. switch (fown->signum) {
  379. siginfo_t si;
  380. default:
  381. /* Queue a rt signal with the appropriate fd as its
  382. value. We use SI_SIGIO as the source, not
  383. SI_KERNEL, since kernel signals always get
  384. delivered even if we can't queue. Failure to
  385. queue in this case _should_ be reported; we fall
  386. back to SIGIO in that case. --sct */
  387. si.si_signo = fown->signum;
  388. si.si_errno = 0;
  389. si.si_code = reason;
  390. /* Make sure we are called with one of the POLL_*
  391. reasons, otherwise we could leak kernel stack into
  392. userspace. */
  393. BUG_ON((reason & __SI_MASK) != __SI_POLL);
  394. if (reason - POLL_IN >= NSIGPOLL)
  395. si.si_band = ~0L;
  396. else
  397. si.si_band = band_table[reason - POLL_IN];
  398. si.si_fd = fd;
  399. if (!group_send_sig_info(fown->signum, &si, p))
  400. break;
  401. /* fall-through: fall back on the old plain SIGIO signal */
  402. case 0:
  403. group_send_sig_info(SIGIO, SEND_SIG_PRIV, p);
  404. }
  405. }
  406. void send_sigio(struct fown_struct *fown, int fd, int band)
  407. {
  408. struct task_struct *p;
  409. enum pid_type type;
  410. struct pid *pid;
  411. read_lock(&fown->lock);
  412. type = fown->pid_type;
  413. pid = fown->pid;
  414. if (!pid)
  415. goto out_unlock_fown;
  416. read_lock(&tasklist_lock);
  417. do_each_pid_task(pid, type, p) {
  418. send_sigio_to_task(p, fown, fd, band);
  419. } while_each_pid_task(pid, type, p);
  420. read_unlock(&tasklist_lock);
  421. out_unlock_fown:
  422. read_unlock(&fown->lock);
  423. }
  424. static void send_sigurg_to_task(struct task_struct *p,
  425. struct fown_struct *fown)
  426. {
  427. if (sigio_perm(p, fown, SIGURG))
  428. group_send_sig_info(SIGURG, SEND_SIG_PRIV, p);
  429. }
  430. int send_sigurg(struct fown_struct *fown)
  431. {
  432. struct task_struct *p;
  433. enum pid_type type;
  434. struct pid *pid;
  435. int ret = 0;
  436. read_lock(&fown->lock);
  437. type = fown->pid_type;
  438. pid = fown->pid;
  439. if (!pid)
  440. goto out_unlock_fown;
  441. ret = 1;
  442. read_lock(&tasklist_lock);
  443. do_each_pid_task(pid, type, p) {
  444. send_sigurg_to_task(p, fown);
  445. } while_each_pid_task(pid, type, p);
  446. read_unlock(&tasklist_lock);
  447. out_unlock_fown:
  448. read_unlock(&fown->lock);
  449. return ret;
  450. }
  451. static DEFINE_RWLOCK(fasync_lock);
  452. static struct kmem_cache *fasync_cache __read_mostly;
  453. /*
  454. * fasync_helper() is used by some character device drivers (mainly mice)
  455. * to set up the fasync queue. It returns negative on error, 0 if it did
  456. * no changes and positive if it added/deleted the entry.
  457. */
  458. int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
  459. {
  460. struct fasync_struct *fa, **fp;
  461. struct fasync_struct *new = NULL;
  462. int result = 0;
  463. if (on) {
  464. new = kmem_cache_alloc(fasync_cache, GFP_KERNEL);
  465. if (!new)
  466. return -ENOMEM;
  467. }
  468. write_lock_irq(&fasync_lock);
  469. for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
  470. if (fa->fa_file == filp) {
  471. if(on) {
  472. fa->fa_fd = fd;
  473. kmem_cache_free(fasync_cache, new);
  474. } else {
  475. *fp = fa->fa_next;
  476. kmem_cache_free(fasync_cache, fa);
  477. result = 1;
  478. }
  479. goto out;
  480. }
  481. }
  482. if (on) {
  483. new->magic = FASYNC_MAGIC;
  484. new->fa_file = filp;
  485. new->fa_fd = fd;
  486. new->fa_next = *fapp;
  487. *fapp = new;
  488. result = 1;
  489. }
  490. out:
  491. write_unlock_irq(&fasync_lock);
  492. return result;
  493. }
  494. EXPORT_SYMBOL(fasync_helper);
  495. void __kill_fasync(struct fasync_struct *fa, int sig, int band)
  496. {
  497. while (fa) {
  498. struct fown_struct * fown;
  499. if (fa->magic != FASYNC_MAGIC) {
  500. printk(KERN_ERR "kill_fasync: bad magic number in "
  501. "fasync_struct!\n");
  502. return;
  503. }
  504. fown = &fa->fa_file->f_owner;
  505. /* Don't send SIGURG to processes which have not set a
  506. queued signum: SIGURG has its own default signalling
  507. mechanism. */
  508. if (!(sig == SIGURG && fown->signum == 0))
  509. send_sigio(fown, fa->fa_fd, band);
  510. fa = fa->fa_next;
  511. }
  512. }
  513. EXPORT_SYMBOL(__kill_fasync);
  514. void kill_fasync(struct fasync_struct **fp, int sig, int band)
  515. {
  516. /* First a quick test without locking: usually
  517. * the list is empty.
  518. */
  519. if (*fp) {
  520. read_lock(&fasync_lock);
  521. /* reread *fp after obtaining the lock */
  522. __kill_fasync(*fp, sig, band);
  523. read_unlock(&fasync_lock);
  524. }
  525. }
  526. EXPORT_SYMBOL(kill_fasync);
  527. static int __init fasync_init(void)
  528. {
  529. fasync_cache = kmem_cache_create("fasync_cache",
  530. sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
  531. return 0;
  532. }
  533. module_init(fasync_init)