open.c 25 KB

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