open.c 26 KB

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