fcntl.c 13 KB

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