open.c 26 KB

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