open.c 25 KB

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