open.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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 <linux/compat.h>
  33. #include "internal.h"
  34. int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
  35. struct file *filp)
  36. {
  37. int ret;
  38. struct iattr newattrs;
  39. /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
  40. if (length < 0)
  41. return -EINVAL;
  42. newattrs.ia_size = length;
  43. newattrs.ia_valid = ATTR_SIZE | time_attrs;
  44. if (filp) {
  45. newattrs.ia_file = filp;
  46. newattrs.ia_valid |= ATTR_FILE;
  47. }
  48. /* Remove suid/sgid on truncate too */
  49. ret = should_remove_suid(dentry);
  50. if (ret)
  51. newattrs.ia_valid |= ret | ATTR_FORCE;
  52. mutex_lock(&dentry->d_inode->i_mutex);
  53. ret = notify_change(dentry, &newattrs);
  54. mutex_unlock(&dentry->d_inode->i_mutex);
  55. return ret;
  56. }
  57. long vfs_truncate(struct path *path, loff_t length)
  58. {
  59. struct inode *inode;
  60. long error;
  61. inode = path->dentry->d_inode;
  62. /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
  63. if (S_ISDIR(inode->i_mode))
  64. return -EISDIR;
  65. if (!S_ISREG(inode->i_mode))
  66. return -EINVAL;
  67. error = mnt_want_write(path->mnt);
  68. if (error)
  69. goto out;
  70. error = inode_permission(inode, MAY_WRITE);
  71. if (error)
  72. goto mnt_drop_write_and_out;
  73. error = -EPERM;
  74. if (IS_APPEND(inode))
  75. goto mnt_drop_write_and_out;
  76. error = get_write_access(inode);
  77. if (error)
  78. goto mnt_drop_write_and_out;
  79. /*
  80. * Make sure that there are no leases. get_write_access() protects
  81. * against the truncate racing with a lease-granting setlease().
  82. */
  83. error = break_lease(inode, O_WRONLY);
  84. if (error)
  85. goto put_write_and_out;
  86. error = locks_verify_truncate(inode, NULL, length);
  87. if (!error)
  88. error = security_path_truncate(path);
  89. if (!error)
  90. error = do_truncate(path->dentry, length, 0, NULL);
  91. put_write_and_out:
  92. put_write_access(inode);
  93. mnt_drop_write_and_out:
  94. mnt_drop_write(path->mnt);
  95. out:
  96. return error;
  97. }
  98. EXPORT_SYMBOL_GPL(vfs_truncate);
  99. static long do_sys_truncate(const char __user *pathname, loff_t length)
  100. {
  101. unsigned int lookup_flags = LOOKUP_FOLLOW;
  102. struct path path;
  103. int error;
  104. if (length < 0) /* sorry, but loff_t says... */
  105. return -EINVAL;
  106. retry:
  107. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  108. if (!error) {
  109. error = vfs_truncate(&path, length);
  110. path_put(&path);
  111. }
  112. if (retry_estale(error, lookup_flags)) {
  113. lookup_flags |= LOOKUP_REVAL;
  114. goto retry;
  115. }
  116. return error;
  117. }
  118. SYSCALL_DEFINE2(truncate, const char __user *, path, long, length)
  119. {
  120. return do_sys_truncate(path, length);
  121. }
  122. #ifdef CONFIG_COMPAT
  123. COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length)
  124. {
  125. return do_sys_truncate(path, length);
  126. }
  127. #endif
  128. static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
  129. {
  130. struct inode *inode;
  131. struct dentry *dentry;
  132. struct fd f;
  133. int error;
  134. error = -EINVAL;
  135. if (length < 0)
  136. goto out;
  137. error = -EBADF;
  138. f = fdget(fd);
  139. if (!f.file)
  140. goto out;
  141. /* explicitly opened as large or we are on 64-bit box */
  142. if (f.file->f_flags & O_LARGEFILE)
  143. small = 0;
  144. dentry = f.file->f_path.dentry;
  145. inode = dentry->d_inode;
  146. error = -EINVAL;
  147. if (!S_ISREG(inode->i_mode) || !(f.file->f_mode & FMODE_WRITE))
  148. goto out_putf;
  149. error = -EINVAL;
  150. /* Cannot ftruncate over 2^31 bytes without large file support */
  151. if (small && length > MAX_NON_LFS)
  152. goto out_putf;
  153. error = -EPERM;
  154. if (IS_APPEND(inode))
  155. goto out_putf;
  156. sb_start_write(inode->i_sb);
  157. error = locks_verify_truncate(inode, f.file, length);
  158. if (!error)
  159. error = security_path_truncate(&f.file->f_path);
  160. if (!error)
  161. error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, f.file);
  162. sb_end_write(inode->i_sb);
  163. out_putf:
  164. fdput(f);
  165. out:
  166. return error;
  167. }
  168. SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
  169. {
  170. return do_sys_ftruncate(fd, length, 1);
  171. }
  172. #ifdef CONFIG_COMPAT
  173. COMPAT_SYSCALL_DEFINE2(ftruncate, unsigned int, fd, compat_ulong_t, length)
  174. {
  175. return do_sys_ftruncate(fd, length, 1);
  176. }
  177. #endif
  178. /* LFS versions of truncate are only needed on 32 bit machines */
  179. #if BITS_PER_LONG == 32
  180. SYSCALL_DEFINE2(truncate64, const char __user *, path, loff_t, length)
  181. {
  182. return do_sys_truncate(path, length);
  183. }
  184. SYSCALL_DEFINE2(ftruncate64, unsigned int, fd, loff_t, length)
  185. {
  186. return do_sys_ftruncate(fd, length, 0);
  187. }
  188. #endif /* BITS_PER_LONG == 32 */
  189. int do_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  190. {
  191. struct inode *inode = file_inode(file);
  192. long ret;
  193. if (offset < 0 || len <= 0)
  194. return -EINVAL;
  195. /* Return error if mode is not supported */
  196. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  197. return -EOPNOTSUPP;
  198. /* Punch hole must have keep size set */
  199. if ((mode & FALLOC_FL_PUNCH_HOLE) &&
  200. !(mode & FALLOC_FL_KEEP_SIZE))
  201. return -EOPNOTSUPP;
  202. if (!(file->f_mode & FMODE_WRITE))
  203. return -EBADF;
  204. /* It's not possible punch hole on append only file */
  205. if (mode & FALLOC_FL_PUNCH_HOLE && IS_APPEND(inode))
  206. return -EPERM;
  207. if (IS_IMMUTABLE(inode))
  208. return -EPERM;
  209. /*
  210. * Revalidate the write permissions, in case security policy has
  211. * changed since the files were opened.
  212. */
  213. ret = security_file_permission(file, MAY_WRITE);
  214. if (ret)
  215. return ret;
  216. if (S_ISFIFO(inode->i_mode))
  217. return -ESPIPE;
  218. /*
  219. * Let individual file system decide if it supports preallocation
  220. * for directories or not.
  221. */
  222. if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
  223. return -ENODEV;
  224. /* Check for wrap through zero too */
  225. if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
  226. return -EFBIG;
  227. if (!file->f_op->fallocate)
  228. return -EOPNOTSUPP;
  229. sb_start_write(inode->i_sb);
  230. ret = file->f_op->fallocate(file, mode, offset, len);
  231. sb_end_write(inode->i_sb);
  232. return ret;
  233. }
  234. SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)
  235. {
  236. struct fd f = fdget(fd);
  237. int error = -EBADF;
  238. if (f.file) {
  239. error = do_fallocate(f.file, mode, offset, len);
  240. fdput(f);
  241. }
  242. return error;
  243. }
  244. /*
  245. * access() needs to use the real uid/gid, not the effective uid/gid.
  246. * We do this by temporarily clearing all FS-related capabilities and
  247. * switching the fsuid/fsgid around to the real ones.
  248. */
  249. SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
  250. {
  251. const struct cred *old_cred;
  252. struct cred *override_cred;
  253. struct path path;
  254. struct inode *inode;
  255. int res;
  256. unsigned int lookup_flags = LOOKUP_FOLLOW;
  257. if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
  258. return -EINVAL;
  259. override_cred = prepare_creds();
  260. if (!override_cred)
  261. return -ENOMEM;
  262. override_cred->fsuid = override_cred->uid;
  263. override_cred->fsgid = override_cred->gid;
  264. if (!issecure(SECURE_NO_SETUID_FIXUP)) {
  265. /* Clear the capabilities if we switch to a non-root user */
  266. kuid_t root_uid = make_kuid(override_cred->user_ns, 0);
  267. if (!uid_eq(override_cred->uid, root_uid))
  268. cap_clear(override_cred->cap_effective);
  269. else
  270. override_cred->cap_effective =
  271. override_cred->cap_permitted;
  272. }
  273. old_cred = override_creds(override_cred);
  274. retry:
  275. res = user_path_at(dfd, filename, lookup_flags, &path);
  276. if (res)
  277. goto out;
  278. inode = path.dentry->d_inode;
  279. if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
  280. /*
  281. * MAY_EXEC on regular files is denied if the fs is mounted
  282. * with the "noexec" flag.
  283. */
  284. res = -EACCES;
  285. if (path.mnt->mnt_flags & MNT_NOEXEC)
  286. goto out_path_release;
  287. }
  288. res = inode_permission(inode, mode | MAY_ACCESS);
  289. /* SuS v2 requires we report a read only fs too */
  290. if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
  291. goto out_path_release;
  292. /*
  293. * This is a rare case where using __mnt_is_readonly()
  294. * is OK without a mnt_want/drop_write() pair. Since
  295. * no actual write to the fs is performed here, we do
  296. * not need to telegraph to that to anyone.
  297. *
  298. * By doing this, we accept that this access is
  299. * inherently racy and know that the fs may change
  300. * state before we even see this result.
  301. */
  302. if (__mnt_is_readonly(path.mnt))
  303. res = -EROFS;
  304. out_path_release:
  305. path_put(&path);
  306. if (retry_estale(res, lookup_flags)) {
  307. lookup_flags |= LOOKUP_REVAL;
  308. goto retry;
  309. }
  310. out:
  311. revert_creds(old_cred);
  312. put_cred(override_cred);
  313. return res;
  314. }
  315. SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
  316. {
  317. return sys_faccessat(AT_FDCWD, filename, mode);
  318. }
  319. SYSCALL_DEFINE1(chdir, const char __user *, filename)
  320. {
  321. struct path path;
  322. int error;
  323. unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  324. retry:
  325. error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
  326. if (error)
  327. goto out;
  328. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
  329. if (error)
  330. goto dput_and_out;
  331. set_fs_pwd(current->fs, &path);
  332. dput_and_out:
  333. path_put(&path);
  334. if (retry_estale(error, lookup_flags)) {
  335. lookup_flags |= LOOKUP_REVAL;
  336. goto retry;
  337. }
  338. out:
  339. return error;
  340. }
  341. SYSCALL_DEFINE1(fchdir, unsigned int, fd)
  342. {
  343. struct fd f = fdget_raw(fd);
  344. struct inode *inode;
  345. int error = -EBADF;
  346. error = -EBADF;
  347. if (!f.file)
  348. goto out;
  349. inode = file_inode(f.file);
  350. error = -ENOTDIR;
  351. if (!S_ISDIR(inode->i_mode))
  352. goto out_putf;
  353. error = inode_permission(inode, MAY_EXEC | MAY_CHDIR);
  354. if (!error)
  355. set_fs_pwd(current->fs, &f.file->f_path);
  356. out_putf:
  357. fdput(f);
  358. out:
  359. return error;
  360. }
  361. SYSCALL_DEFINE1(chroot, const char __user *, filename)
  362. {
  363. struct path path;
  364. int error;
  365. unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  366. retry:
  367. error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
  368. if (error)
  369. goto out;
  370. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
  371. if (error)
  372. goto dput_and_out;
  373. error = -EPERM;
  374. if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT))
  375. goto dput_and_out;
  376. error = security_path_chroot(&path);
  377. if (error)
  378. goto dput_and_out;
  379. set_fs_root(current->fs, &path);
  380. error = 0;
  381. dput_and_out:
  382. path_put(&path);
  383. if (retry_estale(error, lookup_flags)) {
  384. lookup_flags |= LOOKUP_REVAL;
  385. goto retry;
  386. }
  387. out:
  388. return error;
  389. }
  390. static int chmod_common(struct path *path, umode_t mode)
  391. {
  392. struct inode *inode = path->dentry->d_inode;
  393. struct iattr newattrs;
  394. int error;
  395. error = mnt_want_write(path->mnt);
  396. if (error)
  397. return error;
  398. mutex_lock(&inode->i_mutex);
  399. error = security_path_chmod(path, mode);
  400. if (error)
  401. goto out_unlock;
  402. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  403. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  404. error = notify_change(path->dentry, &newattrs);
  405. out_unlock:
  406. mutex_unlock(&inode->i_mutex);
  407. mnt_drop_write(path->mnt);
  408. return error;
  409. }
  410. SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)
  411. {
  412. struct fd f = fdget(fd);
  413. int err = -EBADF;
  414. if (f.file) {
  415. audit_inode(NULL, f.file->f_path.dentry, 0);
  416. err = chmod_common(&f.file->f_path, mode);
  417. fdput(f);
  418. }
  419. return err;
  420. }
  421. SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, umode_t, mode)
  422. {
  423. struct path path;
  424. int error;
  425. unsigned int lookup_flags = LOOKUP_FOLLOW;
  426. retry:
  427. error = user_path_at(dfd, filename, lookup_flags, &path);
  428. if (!error) {
  429. error = chmod_common(&path, mode);
  430. path_put(&path);
  431. if (retry_estale(error, lookup_flags)) {
  432. lookup_flags |= LOOKUP_REVAL;
  433. goto retry;
  434. }
  435. }
  436. return error;
  437. }
  438. SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
  439. {
  440. return sys_fchmodat(AT_FDCWD, filename, mode);
  441. }
  442. static int chown_common(struct path *path, uid_t user, gid_t group)
  443. {
  444. struct inode *inode = path->dentry->d_inode;
  445. int error;
  446. struct iattr newattrs;
  447. kuid_t uid;
  448. kgid_t gid;
  449. uid = make_kuid(current_user_ns(), user);
  450. gid = make_kgid(current_user_ns(), group);
  451. newattrs.ia_valid = ATTR_CTIME;
  452. if (user != (uid_t) -1) {
  453. if (!uid_valid(uid))
  454. return -EINVAL;
  455. newattrs.ia_valid |= ATTR_UID;
  456. newattrs.ia_uid = uid;
  457. }
  458. if (group != (gid_t) -1) {
  459. if (!gid_valid(gid))
  460. return -EINVAL;
  461. newattrs.ia_valid |= ATTR_GID;
  462. newattrs.ia_gid = gid;
  463. }
  464. if (!S_ISDIR(inode->i_mode))
  465. newattrs.ia_valid |=
  466. ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
  467. mutex_lock(&inode->i_mutex);
  468. error = security_path_chown(path, uid, gid);
  469. if (!error)
  470. error = notify_change(path->dentry, &newattrs);
  471. mutex_unlock(&inode->i_mutex);
  472. return error;
  473. }
  474. SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
  475. gid_t, group, int, flag)
  476. {
  477. struct path path;
  478. int error = -EINVAL;
  479. int lookup_flags;
  480. if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
  481. goto out;
  482. lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  483. if (flag & AT_EMPTY_PATH)
  484. lookup_flags |= LOOKUP_EMPTY;
  485. retry:
  486. error = user_path_at(dfd, filename, lookup_flags, &path);
  487. if (error)
  488. goto out;
  489. error = mnt_want_write(path.mnt);
  490. if (error)
  491. goto out_release;
  492. error = chown_common(&path, user, group);
  493. mnt_drop_write(path.mnt);
  494. out_release:
  495. path_put(&path);
  496. if (retry_estale(error, lookup_flags)) {
  497. lookup_flags |= LOOKUP_REVAL;
  498. goto retry;
  499. }
  500. out:
  501. return error;
  502. }
  503. SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
  504. {
  505. return sys_fchownat(AT_FDCWD, filename, user, group, 0);
  506. }
  507. SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
  508. {
  509. return sys_fchownat(AT_FDCWD, filename, user, group,
  510. AT_SYMLINK_NOFOLLOW);
  511. }
  512. SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
  513. {
  514. struct fd f = fdget(fd);
  515. int error = -EBADF;
  516. if (!f.file)
  517. goto out;
  518. error = mnt_want_write_file(f.file);
  519. if (error)
  520. goto out_fput;
  521. audit_inode(NULL, f.file->f_path.dentry, 0);
  522. error = chown_common(&f.file->f_path, user, group);
  523. mnt_drop_write_file(f.file);
  524. out_fput:
  525. fdput(f);
  526. out:
  527. return error;
  528. }
  529. /*
  530. * You have to be very careful that these write
  531. * counts get cleaned up in error cases and
  532. * upon __fput(). This should probably never
  533. * be called outside of __dentry_open().
  534. */
  535. static inline int __get_file_write_access(struct inode *inode,
  536. struct vfsmount *mnt)
  537. {
  538. int error;
  539. error = get_write_access(inode);
  540. if (error)
  541. return error;
  542. /*
  543. * Do not take mount writer counts on
  544. * special files since no writes to
  545. * the mount itself will occur.
  546. */
  547. if (!special_file(inode->i_mode)) {
  548. /*
  549. * Balanced in __fput()
  550. */
  551. error = __mnt_want_write(mnt);
  552. if (error)
  553. put_write_access(inode);
  554. }
  555. return error;
  556. }
  557. int open_check_o_direct(struct file *f)
  558. {
  559. /* NB: we're sure to have correct a_ops only after f_op->open */
  560. if (f->f_flags & O_DIRECT) {
  561. if (!f->f_mapping->a_ops ||
  562. ((!f->f_mapping->a_ops->direct_IO) &&
  563. (!f->f_mapping->a_ops->get_xip_mem))) {
  564. return -EINVAL;
  565. }
  566. }
  567. return 0;
  568. }
  569. static int do_dentry_open(struct file *f,
  570. int (*open)(struct inode *, struct file *),
  571. const struct cred *cred)
  572. {
  573. static const struct file_operations empty_fops = {};
  574. struct inode *inode;
  575. int error;
  576. f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
  577. FMODE_PREAD | FMODE_PWRITE;
  578. if (unlikely(f->f_flags & O_PATH))
  579. f->f_mode = FMODE_PATH;
  580. path_get(&f->f_path);
  581. inode = f->f_inode = f->f_path.dentry->d_inode;
  582. if (f->f_mode & FMODE_WRITE) {
  583. error = __get_file_write_access(inode, f->f_path.mnt);
  584. if (error)
  585. goto cleanup_file;
  586. if (!special_file(inode->i_mode))
  587. file_take_write(f);
  588. }
  589. f->f_mapping = inode->i_mapping;
  590. if (unlikely(f->f_mode & FMODE_PATH)) {
  591. f->f_op = &empty_fops;
  592. return 0;
  593. }
  594. f->f_op = fops_get(inode->i_fop);
  595. if (unlikely(WARN_ON(!f->f_op))) {
  596. error = -ENODEV;
  597. goto cleanup_all;
  598. }
  599. error = security_file_open(f, cred);
  600. if (error)
  601. goto cleanup_all;
  602. error = break_lease(inode, f->f_flags);
  603. if (error)
  604. goto cleanup_all;
  605. if (!open)
  606. open = f->f_op->open;
  607. if (open) {
  608. error = open(inode, f);
  609. if (error)
  610. goto cleanup_all;
  611. }
  612. if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  613. i_readcount_inc(inode);
  614. f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  615. file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
  616. return 0;
  617. cleanup_all:
  618. fops_put(f->f_op);
  619. if (f->f_mode & FMODE_WRITE) {
  620. put_write_access(inode);
  621. if (!special_file(inode->i_mode)) {
  622. /*
  623. * We don't consider this a real
  624. * mnt_want/drop_write() pair
  625. * because it all happenend right
  626. * here, so just reset the state.
  627. */
  628. file_reset_write(f);
  629. __mnt_drop_write(f->f_path.mnt);
  630. }
  631. }
  632. cleanup_file:
  633. path_put(&f->f_path);
  634. f->f_path.mnt = NULL;
  635. f->f_path.dentry = NULL;
  636. f->f_inode = NULL;
  637. return error;
  638. }
  639. /**
  640. * finish_open - finish opening a file
  641. * @file: file pointer
  642. * @dentry: pointer to dentry
  643. * @open: open callback
  644. * @opened: state of open
  645. *
  646. * This can be used to finish opening a file passed to i_op->atomic_open().
  647. *
  648. * If the open callback is set to NULL, then the standard f_op->open()
  649. * filesystem callback is substituted.
  650. *
  651. * NB: the dentry reference is _not_ consumed. If, for example, the dentry is
  652. * the return value of d_splice_alias(), then the caller needs to perform dput()
  653. * on it after finish_open().
  654. *
  655. * On successful return @file is a fully instantiated open file. After this, if
  656. * an error occurs in ->atomic_open(), it needs to clean up with fput().
  657. *
  658. * Returns zero on success or -errno if the open failed.
  659. */
  660. int finish_open(struct file *file, struct dentry *dentry,
  661. int (*open)(struct inode *, struct file *),
  662. int *opened)
  663. {
  664. int error;
  665. BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
  666. file->f_path.dentry = dentry;
  667. error = do_dentry_open(file, open, current_cred());
  668. if (!error)
  669. *opened |= FILE_OPENED;
  670. return error;
  671. }
  672. EXPORT_SYMBOL(finish_open);
  673. /**
  674. * finish_no_open - finish ->atomic_open() without opening the file
  675. *
  676. * @file: file pointer
  677. * @dentry: dentry or NULL (as returned from ->lookup())
  678. *
  679. * This can be used to set the result of a successful lookup in ->atomic_open().
  680. *
  681. * NB: unlike finish_open() this function does consume the dentry reference and
  682. * the caller need not dput() it.
  683. *
  684. * Returns "1" which must be the return value of ->atomic_open() after having
  685. * called this function.
  686. */
  687. int finish_no_open(struct file *file, struct dentry *dentry)
  688. {
  689. file->f_path.dentry = dentry;
  690. return 1;
  691. }
  692. EXPORT_SYMBOL(finish_no_open);
  693. struct file *dentry_open(const struct path *path, int flags,
  694. const struct cred *cred)
  695. {
  696. int error;
  697. struct file *f;
  698. validate_creds(cred);
  699. /* We must always pass in a valid mount pointer. */
  700. BUG_ON(!path->mnt);
  701. f = get_empty_filp();
  702. if (!IS_ERR(f)) {
  703. f->f_flags = flags;
  704. f->f_path = *path;
  705. error = do_dentry_open(f, NULL, cred);
  706. if (!error) {
  707. /* from now on we need fput() to dispose of f */
  708. error = open_check_o_direct(f);
  709. if (error) {
  710. fput(f);
  711. f = ERR_PTR(error);
  712. }
  713. } else {
  714. put_filp(f);
  715. f = ERR_PTR(error);
  716. }
  717. }
  718. return f;
  719. }
  720. EXPORT_SYMBOL(dentry_open);
  721. static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
  722. {
  723. int lookup_flags = 0;
  724. int acc_mode;
  725. if (flags & (O_CREAT | __O_TMPFILE))
  726. op->mode = (mode & S_IALLUGO) | S_IFREG;
  727. else
  728. op->mode = 0;
  729. /* Must never be set by userspace */
  730. flags &= ~FMODE_NONOTIFY & ~O_CLOEXEC;
  731. /*
  732. * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
  733. * check for O_DSYNC if the need any syncing at all we enforce it's
  734. * always set instead of having to deal with possibly weird behaviour
  735. * for malicious applications setting only __O_SYNC.
  736. */
  737. if (flags & __O_SYNC)
  738. flags |= O_DSYNC;
  739. if (flags & __O_TMPFILE) {
  740. if ((flags & O_TMPFILE_MASK) != O_TMPFILE)
  741. return -EINVAL;
  742. acc_mode = MAY_OPEN | ACC_MODE(flags);
  743. if (!(acc_mode & MAY_WRITE))
  744. return -EINVAL;
  745. } else if (flags & O_PATH) {
  746. /*
  747. * If we have O_PATH in the open flag. Then we
  748. * cannot have anything other than the below set of flags
  749. */
  750. flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH;
  751. acc_mode = 0;
  752. } else {
  753. acc_mode = MAY_OPEN | ACC_MODE(flags);
  754. }
  755. op->open_flag = flags;
  756. /* O_TRUNC implies we need access checks for write permissions */
  757. if (flags & O_TRUNC)
  758. acc_mode |= MAY_WRITE;
  759. /* Allow the LSM permission hook to distinguish append
  760. access from general write access. */
  761. if (flags & O_APPEND)
  762. acc_mode |= MAY_APPEND;
  763. op->acc_mode = acc_mode;
  764. op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
  765. if (flags & O_CREAT) {
  766. op->intent |= LOOKUP_CREATE;
  767. if (flags & O_EXCL)
  768. op->intent |= LOOKUP_EXCL;
  769. }
  770. if (flags & O_DIRECTORY)
  771. lookup_flags |= LOOKUP_DIRECTORY;
  772. if (!(flags & O_NOFOLLOW))
  773. lookup_flags |= LOOKUP_FOLLOW;
  774. op->lookup_flags = lookup_flags;
  775. return 0;
  776. }
  777. /**
  778. * file_open_name - open file and return file pointer
  779. *
  780. * @name: struct filename containing path to open
  781. * @flags: open flags as per the open(2) second argument
  782. * @mode: mode for the new file if O_CREAT is set, else ignored
  783. *
  784. * This is the helper to open a file from kernelspace if you really
  785. * have to. But in generally you should not do this, so please move
  786. * along, nothing to see here..
  787. */
  788. struct file *file_open_name(struct filename *name, int flags, umode_t mode)
  789. {
  790. struct open_flags op;
  791. int err = build_open_flags(flags, mode, &op);
  792. return err ? ERR_PTR(err) : do_filp_open(AT_FDCWD, name, &op);
  793. }
  794. /**
  795. * filp_open - open file and return file pointer
  796. *
  797. * @filename: path to open
  798. * @flags: open flags as per the open(2) second argument
  799. * @mode: mode for the new file if O_CREAT is set, else ignored
  800. *
  801. * This is the helper to open a file from kernelspace if you really
  802. * have to. But in generally you should not do this, so please move
  803. * along, nothing to see here..
  804. */
  805. struct file *filp_open(const char *filename, int flags, umode_t mode)
  806. {
  807. struct filename name = {.name = filename};
  808. return file_open_name(&name, flags, mode);
  809. }
  810. EXPORT_SYMBOL(filp_open);
  811. struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  812. const char *filename, int flags)
  813. {
  814. struct open_flags op;
  815. int err = build_open_flags(flags, 0, &op);
  816. if (err)
  817. return ERR_PTR(err);
  818. if (flags & O_CREAT)
  819. return ERR_PTR(-EINVAL);
  820. if (!filename && (flags & O_DIRECTORY))
  821. if (!dentry->d_inode->i_op->lookup)
  822. return ERR_PTR(-ENOTDIR);
  823. return do_file_open_root(dentry, mnt, filename, &op);
  824. }
  825. EXPORT_SYMBOL(file_open_root);
  826. long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
  827. {
  828. struct open_flags op;
  829. int fd = build_open_flags(flags, mode, &op);
  830. struct filename *tmp;
  831. if (fd)
  832. return fd;
  833. tmp = getname(filename);
  834. if (IS_ERR(tmp))
  835. return PTR_ERR(tmp);
  836. fd = get_unused_fd_flags(flags);
  837. if (fd >= 0) {
  838. struct file *f = do_filp_open(dfd, tmp, &op);
  839. if (IS_ERR(f)) {
  840. put_unused_fd(fd);
  841. fd = PTR_ERR(f);
  842. } else {
  843. fsnotify_open(f);
  844. fd_install(fd, f);
  845. }
  846. }
  847. putname(tmp);
  848. return fd;
  849. }
  850. SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
  851. {
  852. if (force_o_largefile())
  853. flags |= O_LARGEFILE;
  854. return do_sys_open(AT_FDCWD, filename, flags, mode);
  855. }
  856. SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
  857. umode_t, mode)
  858. {
  859. if (force_o_largefile())
  860. flags |= O_LARGEFILE;
  861. return do_sys_open(dfd, filename, flags, mode);
  862. }
  863. #ifndef __alpha__
  864. /*
  865. * For backward compatibility? Maybe this should be moved
  866. * into arch/i386 instead?
  867. */
  868. SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
  869. {
  870. return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
  871. }
  872. #endif
  873. /*
  874. * "id" is the POSIX thread ID. We use the
  875. * files pointer for this..
  876. */
  877. int filp_close(struct file *filp, fl_owner_t id)
  878. {
  879. int retval = 0;
  880. if (!file_count(filp)) {
  881. printk(KERN_ERR "VFS: Close: file count is 0\n");
  882. return 0;
  883. }
  884. if (filp->f_op->flush)
  885. retval = filp->f_op->flush(filp, id);
  886. if (likely(!(filp->f_mode & FMODE_PATH))) {
  887. dnotify_flush(filp, id);
  888. locks_remove_posix(filp, id);
  889. }
  890. fput(filp);
  891. return retval;
  892. }
  893. EXPORT_SYMBOL(filp_close);
  894. /*
  895. * Careful here! We test whether the file pointer is NULL before
  896. * releasing the fd. This ensures that one clone task can't release
  897. * an fd while another clone is opening it.
  898. */
  899. SYSCALL_DEFINE1(close, unsigned int, fd)
  900. {
  901. int retval = __close_fd(current->files, fd);
  902. /* can't restart close syscall because file table entry was cleared */
  903. if (unlikely(retval == -ERESTARTSYS ||
  904. retval == -ERESTARTNOINTR ||
  905. retval == -ERESTARTNOHAND ||
  906. retval == -ERESTART_RESTARTBLOCK))
  907. retval = -EINTR;
  908. return retval;
  909. }
  910. EXPORT_SYMBOL(sys_close);
  911. /*
  912. * This routine simulates a hangup on the tty, to arrange that users
  913. * are given clean terminals at login time.
  914. */
  915. SYSCALL_DEFINE0(vhangup)
  916. {
  917. if (capable(CAP_SYS_TTY_CONFIG)) {
  918. tty_vhangup_self();
  919. return 0;
  920. }
  921. return -EPERM;
  922. }
  923. /*
  924. * Called when an inode is about to be open.
  925. * We use this to disallow opening large files on 32bit systems if
  926. * the caller didn't specify O_LARGEFILE. On 64bit systems we force
  927. * on this flag in sys_open.
  928. */
  929. int generic_file_open(struct inode * inode, struct file * filp)
  930. {
  931. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  932. return -EOVERFLOW;
  933. return 0;
  934. }
  935. EXPORT_SYMBOL(generic_file_open);
  936. /*
  937. * This is used by subsystems that don't want seekable
  938. * file descriptors. The function is not supposed to ever fail, the only
  939. * reason it returns an 'int' and not 'void' is so that it can be plugged
  940. * directly into file_operations structure.
  941. */
  942. int nonseekable_open(struct inode *inode, struct file *filp)
  943. {
  944. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  945. return 0;
  946. }
  947. EXPORT_SYMBOL(nonseekable_open);