fcntl.c 14 KB

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