open.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. /*
  2. * linux/fs/open.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/string.h>
  7. #include <linux/mm.h>
  8. #include <linux/file.h>
  9. #include <linux/fdtable.h>
  10. #include <linux/fsnotify.h>
  11. #include <linux/module.h>
  12. #include <linux/tty.h>
  13. #include <linux/namei.h>
  14. #include <linux/backing-dev.h>
  15. #include <linux/capability.h>
  16. #include <linux/securebits.h>
  17. #include <linux/security.h>
  18. #include <linux/mount.h>
  19. #include <linux/fcntl.h>
  20. #include <linux/slab.h>
  21. #include <asm/uaccess.h>
  22. #include <linux/fs.h>
  23. #include <linux/personality.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/rcupdate.h>
  27. #include <linux/audit.h>
  28. #include <linux/falloc.h>
  29. #include <linux/fs_struct.h>
  30. #include <linux/ima.h>
  31. #include <linux/dnotify.h>
  32. #include "internal.h"
  33. int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
  34. struct file *filp)
  35. {
  36. int ret;
  37. struct iattr newattrs;
  38. /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
  39. if (length < 0)
  40. return -EINVAL;
  41. newattrs.ia_size = length;
  42. newattrs.ia_valid = ATTR_SIZE | time_attrs;
  43. if (filp) {
  44. newattrs.ia_file = filp;
  45. newattrs.ia_valid |= ATTR_FILE;
  46. }
  47. /* Remove suid/sgid on truncate too */
  48. ret = should_remove_suid(dentry);
  49. if (ret)
  50. newattrs.ia_valid |= ret | ATTR_FORCE;
  51. mutex_lock(&dentry->d_inode->i_mutex);
  52. ret = notify_change(dentry, &newattrs);
  53. mutex_unlock(&dentry->d_inode->i_mutex);
  54. return ret;
  55. }
  56. static long do_sys_truncate(const char __user *pathname, loff_t length)
  57. {
  58. struct path path;
  59. struct inode *inode;
  60. int error;
  61. error = -EINVAL;
  62. if (length < 0) /* sorry, but loff_t says... */
  63. goto out;
  64. error = user_path(pathname, &path);
  65. if (error)
  66. goto out;
  67. inode = path.dentry->d_inode;
  68. /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
  69. error = -EISDIR;
  70. if (S_ISDIR(inode->i_mode))
  71. goto dput_and_out;
  72. error = -EINVAL;
  73. if (!S_ISREG(inode->i_mode))
  74. goto dput_and_out;
  75. error = mnt_want_write(path.mnt);
  76. if (error)
  77. goto dput_and_out;
  78. error = inode_permission(inode, MAY_WRITE);
  79. if (error)
  80. goto mnt_drop_write_and_out;
  81. error = -EPERM;
  82. if (IS_APPEND(inode))
  83. goto mnt_drop_write_and_out;
  84. error = get_write_access(inode);
  85. if (error)
  86. goto mnt_drop_write_and_out;
  87. /*
  88. * Make sure that there are no leases. get_write_access() protects
  89. * against the truncate racing with a lease-granting setlease().
  90. */
  91. error = break_lease(inode, O_WRONLY);
  92. if (error)
  93. goto put_write_and_out;
  94. error = locks_verify_truncate(inode, NULL, length);
  95. if (!error)
  96. error = security_path_truncate(&path);
  97. if (!error)
  98. error = do_truncate(path.dentry, length, 0, NULL);
  99. put_write_and_out:
  100. put_write_access(inode);
  101. mnt_drop_write_and_out:
  102. mnt_drop_write(path.mnt);
  103. dput_and_out:
  104. path_put(&path);
  105. out:
  106. return error;
  107. }
  108. SYSCALL_DEFINE2(truncate, const char __user *, path, long, length)
  109. {
  110. return do_sys_truncate(path, length);
  111. }
  112. static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
  113. {
  114. struct inode * inode;
  115. struct dentry *dentry;
  116. struct file * file;
  117. int error;
  118. error = -EINVAL;
  119. if (length < 0)
  120. goto out;
  121. error = -EBADF;
  122. file = fget(fd);
  123. if (!file)
  124. goto out;
  125. /* explicitly opened as large or we are on 64-bit box */
  126. if (file->f_flags & O_LARGEFILE)
  127. small = 0;
  128. dentry = file->f_path.dentry;
  129. inode = dentry->d_inode;
  130. error = -EINVAL;
  131. if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
  132. goto out_putf;
  133. error = -EINVAL;
  134. /* Cannot ftruncate over 2^31 bytes without large file support */
  135. if (small && length > MAX_NON_LFS)
  136. goto out_putf;
  137. error = -EPERM;
  138. if (IS_APPEND(inode))
  139. goto out_putf;
  140. error = locks_verify_truncate(inode, file, length);
  141. if (!error)
  142. error = security_path_truncate(&file->f_path);
  143. if (!error)
  144. error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, file);
  145. out_putf:
  146. fput(file);
  147. out:
  148. return error;
  149. }
  150. SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
  151. {
  152. long ret = do_sys_ftruncate(fd, length, 1);
  153. /* avoid REGPARM breakage on x86: */
  154. asmlinkage_protect(2, ret, fd, length);
  155. return ret;
  156. }
  157. /* LFS versions of truncate are only needed on 32 bit machines */
  158. #if BITS_PER_LONG == 32
  159. SYSCALL_DEFINE(truncate64)(const char __user * path, loff_t length)
  160. {
  161. return do_sys_truncate(path, length);
  162. }
  163. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  164. asmlinkage long SyS_truncate64(long path, loff_t length)
  165. {
  166. return SYSC_truncate64((const char __user *) path, length);
  167. }
  168. SYSCALL_ALIAS(sys_truncate64, SyS_truncate64);
  169. #endif
  170. SYSCALL_DEFINE(ftruncate64)(unsigned int fd, loff_t length)
  171. {
  172. long ret = do_sys_ftruncate(fd, length, 0);
  173. /* avoid REGPARM breakage on x86: */
  174. asmlinkage_protect(2, ret, fd, length);
  175. return ret;
  176. }
  177. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  178. asmlinkage long SyS_ftruncate64(long fd, loff_t length)
  179. {
  180. return SYSC_ftruncate64((unsigned int) fd, length);
  181. }
  182. SYSCALL_ALIAS(sys_ftruncate64, SyS_ftruncate64);
  183. #endif
  184. #endif /* BITS_PER_LONG == 32 */
  185. int do_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  186. {
  187. struct inode *inode = file->f_path.dentry->d_inode;
  188. long ret;
  189. if (offset < 0 || len <= 0)
  190. return -EINVAL;
  191. /* Return error if mode is not supported */
  192. if (mode && !(mode & FALLOC_FL_KEEP_SIZE))
  193. return -EOPNOTSUPP;
  194. if (!(file->f_mode & FMODE_WRITE))
  195. return -EBADF;
  196. /*
  197. * Revalidate the write permissions, in case security policy has
  198. * changed since the files were opened.
  199. */
  200. ret = security_file_permission(file, MAY_WRITE);
  201. if (ret)
  202. return ret;
  203. if (S_ISFIFO(inode->i_mode))
  204. return -ESPIPE;
  205. /*
  206. * Let individual file system decide if it supports preallocation
  207. * for directories or not.
  208. */
  209. if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
  210. return -ENODEV;
  211. /* Check for wrap through zero too */
  212. if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
  213. return -EFBIG;
  214. if (!inode->i_op->fallocate)
  215. return -EOPNOTSUPP;
  216. return inode->i_op->fallocate(inode, mode, offset, len);
  217. }
  218. SYSCALL_DEFINE(fallocate)(int fd, int mode, loff_t offset, loff_t len)
  219. {
  220. struct file *file;
  221. int error = -EBADF;
  222. file = fget(fd);
  223. if (file) {
  224. error = do_fallocate(file, mode, offset, len);
  225. fput(file);
  226. }
  227. return error;
  228. }
  229. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  230. asmlinkage long SyS_fallocate(long fd, long mode, loff_t offset, loff_t len)
  231. {
  232. return SYSC_fallocate((int)fd, (int)mode, offset, len);
  233. }
  234. SYSCALL_ALIAS(sys_fallocate, SyS_fallocate);
  235. #endif
  236. /*
  237. * access() needs to use the real uid/gid, not the effective uid/gid.
  238. * We do this by temporarily clearing all FS-related capabilities and
  239. * switching the fsuid/fsgid around to the real ones.
  240. */
  241. SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
  242. {
  243. const struct cred *old_cred;
  244. struct cred *override_cred;
  245. struct path path;
  246. struct inode *inode;
  247. int res;
  248. if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
  249. return -EINVAL;
  250. override_cred = prepare_creds();
  251. if (!override_cred)
  252. return -ENOMEM;
  253. override_cred->fsuid = override_cred->uid;
  254. override_cred->fsgid = override_cred->gid;
  255. if (!issecure(SECURE_NO_SETUID_FIXUP)) {
  256. /* Clear the capabilities if we switch to a non-root user */
  257. if (override_cred->uid)
  258. cap_clear(override_cred->cap_effective);
  259. else
  260. override_cred->cap_effective =
  261. override_cred->cap_permitted;
  262. }
  263. old_cred = override_creds(override_cred);
  264. res = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
  265. if (res)
  266. goto out;
  267. inode = path.dentry->d_inode;
  268. if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
  269. /*
  270. * MAY_EXEC on regular files is denied if the fs is mounted
  271. * with the "noexec" flag.
  272. */
  273. res = -EACCES;
  274. if (path.mnt->mnt_flags & MNT_NOEXEC)
  275. goto out_path_release;
  276. }
  277. res = inode_permission(inode, mode | MAY_ACCESS);
  278. /* SuS v2 requires we report a read only fs too */
  279. if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
  280. goto out_path_release;
  281. /*
  282. * This is a rare case where using __mnt_is_readonly()
  283. * is OK without a mnt_want/drop_write() pair. Since
  284. * no actual write to the fs is performed here, we do
  285. * not need to telegraph to that to anyone.
  286. *
  287. * By doing this, we accept that this access is
  288. * inherently racy and know that the fs may change
  289. * state before we even see this result.
  290. */
  291. if (__mnt_is_readonly(path.mnt))
  292. res = -EROFS;
  293. out_path_release:
  294. path_put(&path);
  295. out:
  296. revert_creds(old_cred);
  297. put_cred(override_cred);
  298. return res;
  299. }
  300. SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
  301. {
  302. return sys_faccessat(AT_FDCWD, filename, mode);
  303. }
  304. SYSCALL_DEFINE1(chdir, const char __user *, filename)
  305. {
  306. struct path path;
  307. int error;
  308. error = user_path_dir(filename, &path);
  309. if (error)
  310. goto out;
  311. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
  312. if (error)
  313. goto dput_and_out;
  314. set_fs_pwd(current->fs, &path);
  315. dput_and_out:
  316. path_put(&path);
  317. out:
  318. return error;
  319. }
  320. SYSCALL_DEFINE1(fchdir, unsigned int, fd)
  321. {
  322. struct file *file;
  323. struct inode *inode;
  324. int error;
  325. error = -EBADF;
  326. file = fget(fd);
  327. if (!file)
  328. goto out;
  329. inode = file->f_path.dentry->d_inode;
  330. error = -ENOTDIR;
  331. if (!S_ISDIR(inode->i_mode))
  332. goto out_putf;
  333. error = inode_permission(inode, MAY_EXEC | MAY_CHDIR);
  334. if (!error)
  335. set_fs_pwd(current->fs, &file->f_path);
  336. out_putf:
  337. fput(file);
  338. out:
  339. return error;
  340. }
  341. SYSCALL_DEFINE1(chroot, const char __user *, filename)
  342. {
  343. struct path path;
  344. int error;
  345. error = user_path_dir(filename, &path);
  346. if (error)
  347. goto out;
  348. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
  349. if (error)
  350. goto dput_and_out;
  351. error = -EPERM;
  352. if (!capable(CAP_SYS_CHROOT))
  353. goto dput_and_out;
  354. error = security_path_chroot(&path);
  355. if (error)
  356. goto dput_and_out;
  357. set_fs_root(current->fs, &path);
  358. error = 0;
  359. dput_and_out:
  360. path_put(&path);
  361. out:
  362. return error;
  363. }
  364. SYSCALL_DEFINE2(fchmod, unsigned int, fd, mode_t, mode)
  365. {
  366. struct inode * inode;
  367. struct dentry * dentry;
  368. struct file * file;
  369. int err = -EBADF;
  370. struct iattr newattrs;
  371. file = fget(fd);
  372. if (!file)
  373. goto out;
  374. dentry = file->f_path.dentry;
  375. inode = dentry->d_inode;
  376. audit_inode(NULL, dentry);
  377. err = mnt_want_write_file(file);
  378. if (err)
  379. goto out_putf;
  380. mutex_lock(&inode->i_mutex);
  381. err = security_path_chmod(dentry, file->f_vfsmnt, mode);
  382. if (err)
  383. goto out_unlock;
  384. if (mode == (mode_t) -1)
  385. mode = inode->i_mode;
  386. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  387. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  388. err = notify_change(dentry, &newattrs);
  389. out_unlock:
  390. mutex_unlock(&inode->i_mutex);
  391. mnt_drop_write(file->f_path.mnt);
  392. out_putf:
  393. fput(file);
  394. out:
  395. return err;
  396. }
  397. SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, mode_t, mode)
  398. {
  399. struct path path;
  400. struct inode *inode;
  401. int error;
  402. struct iattr newattrs;
  403. error = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
  404. if (error)
  405. goto out;
  406. inode = path.dentry->d_inode;
  407. error = mnt_want_write(path.mnt);
  408. if (error)
  409. goto dput_and_out;
  410. mutex_lock(&inode->i_mutex);
  411. error = security_path_chmod(path.dentry, path.mnt, mode);
  412. if (error)
  413. goto out_unlock;
  414. if (mode == (mode_t) -1)
  415. mode = inode->i_mode;
  416. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  417. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  418. error = notify_change(path.dentry, &newattrs);
  419. out_unlock:
  420. mutex_unlock(&inode->i_mutex);
  421. mnt_drop_write(path.mnt);
  422. dput_and_out:
  423. path_put(&path);
  424. out:
  425. return error;
  426. }
  427. SYSCALL_DEFINE2(chmod, const char __user *, filename, mode_t, mode)
  428. {
  429. return sys_fchmodat(AT_FDCWD, filename, mode);
  430. }
  431. static int chown_common(struct path *path, uid_t user, gid_t group)
  432. {
  433. struct inode *inode = path->dentry->d_inode;
  434. int error;
  435. struct iattr newattrs;
  436. newattrs.ia_valid = ATTR_CTIME;
  437. if (user != (uid_t) -1) {
  438. newattrs.ia_valid |= ATTR_UID;
  439. newattrs.ia_uid = user;
  440. }
  441. if (group != (gid_t) -1) {
  442. newattrs.ia_valid |= ATTR_GID;
  443. newattrs.ia_gid = group;
  444. }
  445. if (!S_ISDIR(inode->i_mode))
  446. newattrs.ia_valid |=
  447. ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
  448. mutex_lock(&inode->i_mutex);
  449. error = security_path_chown(path, user, group);
  450. if (!error)
  451. error = notify_change(path->dentry, &newattrs);
  452. mutex_unlock(&inode->i_mutex);
  453. return error;
  454. }
  455. SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
  456. {
  457. struct path path;
  458. int error;
  459. error = user_path(filename, &path);
  460. if (error)
  461. goto out;
  462. error = mnt_want_write(path.mnt);
  463. if (error)
  464. goto out_release;
  465. error = chown_common(&path, user, group);
  466. mnt_drop_write(path.mnt);
  467. out_release:
  468. path_put(&path);
  469. out:
  470. return error;
  471. }
  472. SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
  473. gid_t, group, int, flag)
  474. {
  475. struct path path;
  476. int error = -EINVAL;
  477. int follow;
  478. if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
  479. goto out;
  480. follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  481. error = user_path_at(dfd, filename, follow, &path);
  482. if (error)
  483. goto out;
  484. error = mnt_want_write(path.mnt);
  485. if (error)
  486. goto out_release;
  487. error = chown_common(&path, user, group);
  488. mnt_drop_write(path.mnt);
  489. out_release:
  490. path_put(&path);
  491. out:
  492. return error;
  493. }
  494. SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
  495. {
  496. struct path path;
  497. int error;
  498. error = user_lpath(filename, &path);
  499. if (error)
  500. goto out;
  501. error = mnt_want_write(path.mnt);
  502. if (error)
  503. goto out_release;
  504. error = chown_common(&path, user, group);
  505. mnt_drop_write(path.mnt);
  506. out_release:
  507. path_put(&path);
  508. out:
  509. return error;
  510. }
  511. SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
  512. {
  513. struct file * file;
  514. int error = -EBADF;
  515. struct dentry * dentry;
  516. file = fget(fd);
  517. if (!file)
  518. goto out;
  519. error = mnt_want_write_file(file);
  520. if (error)
  521. goto out_fput;
  522. dentry = file->f_path.dentry;
  523. audit_inode(NULL, dentry);
  524. error = chown_common(&file->f_path, user, group);
  525. mnt_drop_write(file->f_path.mnt);
  526. out_fput:
  527. fput(file);
  528. out:
  529. return error;
  530. }
  531. /*
  532. * You have to be very careful that these write
  533. * counts get cleaned up in error cases and
  534. * upon __fput(). This should probably never
  535. * be called outside of __dentry_open().
  536. */
  537. static inline int __get_file_write_access(struct inode *inode,
  538. struct vfsmount *mnt)
  539. {
  540. int error;
  541. error = get_write_access(inode);
  542. if (error)
  543. return error;
  544. /*
  545. * Do not take mount writer counts on
  546. * special files since no writes to
  547. * the mount itself will occur.
  548. */
  549. if (!special_file(inode->i_mode)) {
  550. /*
  551. * Balanced in __fput()
  552. */
  553. error = mnt_want_write(mnt);
  554. if (error)
  555. put_write_access(inode);
  556. }
  557. return error;
  558. }
  559. static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
  560. struct file *f,
  561. int (*open)(struct inode *, struct file *),
  562. const struct cred *cred)
  563. {
  564. struct inode *inode;
  565. int error;
  566. f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
  567. FMODE_PREAD | FMODE_PWRITE;
  568. inode = dentry->d_inode;
  569. if (f->f_mode & FMODE_WRITE) {
  570. error = __get_file_write_access(inode, mnt);
  571. if (error)
  572. goto cleanup_file;
  573. if (!special_file(inode->i_mode))
  574. file_take_write(f);
  575. }
  576. f->f_mapping = inode->i_mapping;
  577. f->f_path.dentry = dentry;
  578. f->f_path.mnt = mnt;
  579. f->f_pos = 0;
  580. f->f_op = fops_get(inode->i_fop);
  581. file_sb_list_add(f, inode->i_sb);
  582. error = security_dentry_open(f, cred);
  583. if (error)
  584. goto cleanup_all;
  585. if (!open && f->f_op)
  586. open = f->f_op->open;
  587. if (open) {
  588. error = open(inode, f);
  589. if (error)
  590. goto cleanup_all;
  591. }
  592. ima_counts_get(f);
  593. f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  594. file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
  595. /* NB: we're sure to have correct a_ops only after f_op->open */
  596. if (f->f_flags & O_DIRECT) {
  597. if (!f->f_mapping->a_ops ||
  598. ((!f->f_mapping->a_ops->direct_IO) &&
  599. (!f->f_mapping->a_ops->get_xip_mem))) {
  600. fput(f);
  601. f = ERR_PTR(-EINVAL);
  602. }
  603. }
  604. return f;
  605. cleanup_all:
  606. fops_put(f->f_op);
  607. if (f->f_mode & FMODE_WRITE) {
  608. put_write_access(inode);
  609. if (!special_file(inode->i_mode)) {
  610. /*
  611. * We don't consider this a real
  612. * mnt_want/drop_write() pair
  613. * because it all happenend right
  614. * here, so just reset the state.
  615. */
  616. file_reset_write(f);
  617. mnt_drop_write(mnt);
  618. }
  619. }
  620. file_sb_list_del(f);
  621. f->f_path.dentry = NULL;
  622. f->f_path.mnt = NULL;
  623. cleanup_file:
  624. put_filp(f);
  625. dput(dentry);
  626. mntput(mnt);
  627. return ERR_PTR(error);
  628. }
  629. /**
  630. * lookup_instantiate_filp - instantiates the open intent filp
  631. * @nd: pointer to nameidata
  632. * @dentry: pointer to dentry
  633. * @open: open callback
  634. *
  635. * Helper for filesystems that want to use lookup open intents and pass back
  636. * a fully instantiated struct file to the caller.
  637. * This function is meant to be called from within a filesystem's
  638. * lookup method.
  639. * Beware of calling it for non-regular files! Those ->open methods might block
  640. * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
  641. * leading to a deadlock, as nobody can open that fifo anymore, because
  642. * another process to open fifo will block on locked parent when doing lookup).
  643. * Note that in case of error, nd->intent.open.file is destroyed, but the
  644. * path information remains valid.
  645. * If the open callback is set to NULL, then the standard f_op->open()
  646. * filesystem callback is substituted.
  647. */
  648. struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
  649. int (*open)(struct inode *, struct file *))
  650. {
  651. const struct cred *cred = current_cred();
  652. if (IS_ERR(nd->intent.open.file))
  653. goto out;
  654. if (IS_ERR(dentry))
  655. goto out_err;
  656. nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->path.mnt),
  657. nd->intent.open.file,
  658. open, cred);
  659. out:
  660. return nd->intent.open.file;
  661. out_err:
  662. release_open_intent(nd);
  663. nd->intent.open.file = (struct file *)dentry;
  664. goto out;
  665. }
  666. EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
  667. /**
  668. * nameidata_to_filp - convert a nameidata to an open filp.
  669. * @nd: pointer to nameidata
  670. * @flags: open flags
  671. *
  672. * Note that this function destroys the original nameidata
  673. */
  674. struct file *nameidata_to_filp(struct nameidata *nd)
  675. {
  676. const struct cred *cred = current_cred();
  677. struct file *filp;
  678. /* Pick up the filp from the open intent */
  679. filp = nd->intent.open.file;
  680. /* Has the filesystem initialised the file for us? */
  681. if (filp->f_path.dentry == NULL)
  682. filp = __dentry_open(nd->path.dentry, nd->path.mnt, filp,
  683. NULL, cred);
  684. else
  685. path_put(&nd->path);
  686. return filp;
  687. }
  688. /*
  689. * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
  690. * error.
  691. */
  692. struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags,
  693. const struct cred *cred)
  694. {
  695. int error;
  696. struct file *f;
  697. validate_creds(cred);
  698. /*
  699. * We must always pass in a valid mount pointer. Historically
  700. * callers got away with not passing it, but we must enforce this at
  701. * the earliest possible point now to avoid strange problems deep in the
  702. * filesystem stack.
  703. */
  704. if (!mnt) {
  705. printk(KERN_WARNING "%s called with NULL vfsmount\n", __func__);
  706. dump_stack();
  707. return ERR_PTR(-EINVAL);
  708. }
  709. error = -ENFILE;
  710. f = get_empty_filp();
  711. if (f == NULL) {
  712. dput(dentry);
  713. mntput(mnt);
  714. return ERR_PTR(error);
  715. }
  716. f->f_flags = flags;
  717. return __dentry_open(dentry, mnt, f, NULL, cred);
  718. }
  719. EXPORT_SYMBOL(dentry_open);
  720. static void __put_unused_fd(struct files_struct *files, unsigned int fd)
  721. {
  722. struct fdtable *fdt = files_fdtable(files);
  723. __FD_CLR(fd, fdt->open_fds);
  724. if (fd < files->next_fd)
  725. files->next_fd = fd;
  726. }
  727. void put_unused_fd(unsigned int fd)
  728. {
  729. struct files_struct *files = current->files;
  730. spin_lock(&files->file_lock);
  731. __put_unused_fd(files, fd);
  732. spin_unlock(&files->file_lock);
  733. }
  734. EXPORT_SYMBOL(put_unused_fd);
  735. /*
  736. * Install a file pointer in the fd array.
  737. *
  738. * The VFS is full of places where we drop the files lock between
  739. * setting the open_fds bitmap and installing the file in the file
  740. * array. At any such point, we are vulnerable to a dup2() race
  741. * installing a file in the array before us. We need to detect this and
  742. * fput() the struct file we are about to overwrite in this case.
  743. *
  744. * It should never happen - if we allow dup2() do it, _really_ bad things
  745. * will follow.
  746. */
  747. void fd_install(unsigned int fd, struct file *file)
  748. {
  749. struct files_struct *files = current->files;
  750. struct fdtable *fdt;
  751. spin_lock(&files->file_lock);
  752. fdt = files_fdtable(files);
  753. BUG_ON(fdt->fd[fd] != NULL);
  754. rcu_assign_pointer(fdt->fd[fd], file);
  755. spin_unlock(&files->file_lock);
  756. }
  757. EXPORT_SYMBOL(fd_install);
  758. long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
  759. {
  760. char *tmp = getname(filename);
  761. int fd = PTR_ERR(tmp);
  762. if (!IS_ERR(tmp)) {
  763. fd = get_unused_fd_flags(flags);
  764. if (fd >= 0) {
  765. struct file *f = do_filp_open(dfd, tmp, flags, mode, 0);
  766. if (IS_ERR(f)) {
  767. put_unused_fd(fd);
  768. fd = PTR_ERR(f);
  769. } else {
  770. fsnotify_open(f);
  771. fd_install(fd, f);
  772. }
  773. }
  774. putname(tmp);
  775. }
  776. return fd;
  777. }
  778. SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, int, mode)
  779. {
  780. long ret;
  781. if (force_o_largefile())
  782. flags |= O_LARGEFILE;
  783. ret = do_sys_open(AT_FDCWD, filename, flags, mode);
  784. /* avoid REGPARM breakage on x86: */
  785. asmlinkage_protect(3, ret, filename, flags, mode);
  786. return ret;
  787. }
  788. SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
  789. int, mode)
  790. {
  791. long ret;
  792. if (force_o_largefile())
  793. flags |= O_LARGEFILE;
  794. ret = do_sys_open(dfd, filename, flags, mode);
  795. /* avoid REGPARM breakage on x86: */
  796. asmlinkage_protect(4, ret, dfd, filename, flags, mode);
  797. return ret;
  798. }
  799. #ifndef __alpha__
  800. /*
  801. * For backward compatibility? Maybe this should be moved
  802. * into arch/i386 instead?
  803. */
  804. SYSCALL_DEFINE2(creat, const char __user *, pathname, int, mode)
  805. {
  806. return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
  807. }
  808. #endif
  809. /*
  810. * "id" is the POSIX thread ID. We use the
  811. * files pointer for this..
  812. */
  813. int filp_close(struct file *filp, fl_owner_t id)
  814. {
  815. int retval = 0;
  816. if (!file_count(filp)) {
  817. printk(KERN_ERR "VFS: Close: file count is 0\n");
  818. return 0;
  819. }
  820. if (filp->f_op && filp->f_op->flush)
  821. retval = filp->f_op->flush(filp, id);
  822. dnotify_flush(filp, id);
  823. locks_remove_posix(filp, id);
  824. fput(filp);
  825. return retval;
  826. }
  827. EXPORT_SYMBOL(filp_close);
  828. /*
  829. * Careful here! We test whether the file pointer is NULL before
  830. * releasing the fd. This ensures that one clone task can't release
  831. * an fd while another clone is opening it.
  832. */
  833. SYSCALL_DEFINE1(close, unsigned int, fd)
  834. {
  835. struct file * filp;
  836. struct files_struct *files = current->files;
  837. struct fdtable *fdt;
  838. int retval;
  839. spin_lock(&files->file_lock);
  840. fdt = files_fdtable(files);
  841. if (fd >= fdt->max_fds)
  842. goto out_unlock;
  843. filp = fdt->fd[fd];
  844. if (!filp)
  845. goto out_unlock;
  846. rcu_assign_pointer(fdt->fd[fd], NULL);
  847. FD_CLR(fd, fdt->close_on_exec);
  848. __put_unused_fd(files, fd);
  849. spin_unlock(&files->file_lock);
  850. retval = filp_close(filp, files);
  851. /* can't restart close syscall because file table entry was cleared */
  852. if (unlikely(retval == -ERESTARTSYS ||
  853. retval == -ERESTARTNOINTR ||
  854. retval == -ERESTARTNOHAND ||
  855. retval == -ERESTART_RESTARTBLOCK))
  856. retval = -EINTR;
  857. return retval;
  858. out_unlock:
  859. spin_unlock(&files->file_lock);
  860. return -EBADF;
  861. }
  862. EXPORT_SYMBOL(sys_close);
  863. /*
  864. * This routine simulates a hangup on the tty, to arrange that users
  865. * are given clean terminals at login time.
  866. */
  867. SYSCALL_DEFINE0(vhangup)
  868. {
  869. if (capable(CAP_SYS_TTY_CONFIG)) {
  870. tty_vhangup_self();
  871. return 0;
  872. }
  873. return -EPERM;
  874. }
  875. /*
  876. * Called when an inode is about to be open.
  877. * We use this to disallow opening large files on 32bit systems if
  878. * the caller didn't specify O_LARGEFILE. On 64bit systems we force
  879. * on this flag in sys_open.
  880. */
  881. int generic_file_open(struct inode * inode, struct file * filp)
  882. {
  883. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  884. return -EOVERFLOW;
  885. return 0;
  886. }
  887. EXPORT_SYMBOL(generic_file_open);
  888. /*
  889. * This is used by subsystems that don't want seekable
  890. * file descriptors. The function is not supposed to ever fail, the only
  891. * reason it returns an 'int' and not 'void' is so that it can be plugged
  892. * directly into file_operations structure.
  893. */
  894. int nonseekable_open(struct inode *inode, struct file *filp)
  895. {
  896. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  897. return 0;
  898. }
  899. EXPORT_SYMBOL(nonseekable_open);