open.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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 (!nsown_capable(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 file * file;
  413. int err = -EBADF;
  414. file = fget(fd);
  415. if (file) {
  416. audit_inode(NULL, file->f_path.dentry, 0);
  417. err = chmod_common(&file->f_path, mode);
  418. fput(file);
  419. }
  420. return err;
  421. }
  422. SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, umode_t, mode)
  423. {
  424. struct path path;
  425. int error;
  426. unsigned int lookup_flags = LOOKUP_FOLLOW;
  427. retry:
  428. error = user_path_at(dfd, filename, lookup_flags, &path);
  429. if (!error) {
  430. error = chmod_common(&path, mode);
  431. path_put(&path);
  432. if (retry_estale(error, lookup_flags)) {
  433. lookup_flags |= LOOKUP_REVAL;
  434. goto retry;
  435. }
  436. }
  437. return error;
  438. }
  439. SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
  440. {
  441. return sys_fchmodat(AT_FDCWD, filename, mode);
  442. }
  443. static int chown_common(struct path *path, uid_t user, gid_t group)
  444. {
  445. struct inode *inode = path->dentry->d_inode;
  446. int error;
  447. struct iattr newattrs;
  448. kuid_t uid;
  449. kgid_t gid;
  450. uid = make_kuid(current_user_ns(), user);
  451. gid = make_kgid(current_user_ns(), group);
  452. newattrs.ia_valid = ATTR_CTIME;
  453. if (user != (uid_t) -1) {
  454. if (!uid_valid(uid))
  455. return -EINVAL;
  456. newattrs.ia_valid |= ATTR_UID;
  457. newattrs.ia_uid = uid;
  458. }
  459. if (group != (gid_t) -1) {
  460. if (!gid_valid(gid))
  461. return -EINVAL;
  462. newattrs.ia_valid |= ATTR_GID;
  463. newattrs.ia_gid = gid;
  464. }
  465. if (!S_ISDIR(inode->i_mode))
  466. newattrs.ia_valid |=
  467. ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
  468. mutex_lock(&inode->i_mutex);
  469. error = security_path_chown(path, uid, gid);
  470. if (!error)
  471. error = notify_change(path->dentry, &newattrs);
  472. mutex_unlock(&inode->i_mutex);
  473. return error;
  474. }
  475. SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
  476. gid_t, group, int, flag)
  477. {
  478. struct path path;
  479. int error = -EINVAL;
  480. int lookup_flags;
  481. if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
  482. goto out;
  483. lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  484. if (flag & AT_EMPTY_PATH)
  485. lookup_flags |= LOOKUP_EMPTY;
  486. retry:
  487. error = user_path_at(dfd, filename, lookup_flags, &path);
  488. if (error)
  489. goto out;
  490. error = mnt_want_write(path.mnt);
  491. if (error)
  492. goto out_release;
  493. error = chown_common(&path, user, group);
  494. mnt_drop_write(path.mnt);
  495. out_release:
  496. path_put(&path);
  497. if (retry_estale(error, lookup_flags)) {
  498. lookup_flags |= LOOKUP_REVAL;
  499. goto retry;
  500. }
  501. out:
  502. return error;
  503. }
  504. SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
  505. {
  506. return sys_fchownat(AT_FDCWD, filename, user, group, 0);
  507. }
  508. SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
  509. {
  510. return sys_fchownat(AT_FDCWD, filename, user, group,
  511. AT_SYMLINK_NOFOLLOW);
  512. }
  513. SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
  514. {
  515. struct fd f = fdget(fd);
  516. int error = -EBADF;
  517. if (!f.file)
  518. goto out;
  519. error = mnt_want_write_file(f.file);
  520. if (error)
  521. goto out_fput;
  522. audit_inode(NULL, f.file->f_path.dentry, 0);
  523. error = chown_common(&f.file->f_path, user, group);
  524. mnt_drop_write_file(f.file);
  525. out_fput:
  526. fdput(f);
  527. out:
  528. return error;
  529. }
  530. /*
  531. * You have to be very careful that these write
  532. * counts get cleaned up in error cases and
  533. * upon __fput(). This should probably never
  534. * be called outside of __dentry_open().
  535. */
  536. static inline int __get_file_write_access(struct inode *inode,
  537. struct vfsmount *mnt)
  538. {
  539. int error;
  540. error = get_write_access(inode);
  541. if (error)
  542. return error;
  543. /*
  544. * Do not take mount writer counts on
  545. * special files since no writes to
  546. * the mount itself will occur.
  547. */
  548. if (!special_file(inode->i_mode)) {
  549. /*
  550. * Balanced in __fput()
  551. */
  552. error = __mnt_want_write(mnt);
  553. if (error)
  554. put_write_access(inode);
  555. }
  556. return error;
  557. }
  558. int open_check_o_direct(struct file *f)
  559. {
  560. /* NB: we're sure to have correct a_ops only after f_op->open */
  561. if (f->f_flags & O_DIRECT) {
  562. if (!f->f_mapping->a_ops ||
  563. ((!f->f_mapping->a_ops->direct_IO) &&
  564. (!f->f_mapping->a_ops->get_xip_mem))) {
  565. return -EINVAL;
  566. }
  567. }
  568. return 0;
  569. }
  570. static int do_dentry_open(struct file *f,
  571. int (*open)(struct inode *, struct file *),
  572. const struct cred *cred)
  573. {
  574. static const struct file_operations empty_fops = {};
  575. struct inode *inode;
  576. int error;
  577. f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
  578. FMODE_PREAD | FMODE_PWRITE;
  579. if (unlikely(f->f_flags & O_PATH))
  580. f->f_mode = FMODE_PATH;
  581. path_get(&f->f_path);
  582. inode = f->f_inode = f->f_path.dentry->d_inode;
  583. if (f->f_mode & FMODE_WRITE) {
  584. error = __get_file_write_access(inode, f->f_path.mnt);
  585. if (error)
  586. goto cleanup_file;
  587. if (!special_file(inode->i_mode))
  588. file_take_write(f);
  589. }
  590. f->f_mapping = inode->i_mapping;
  591. file_sb_list_add(f, inode->i_sb);
  592. if (unlikely(f->f_mode & FMODE_PATH)) {
  593. f->f_op = &empty_fops;
  594. return 0;
  595. }
  596. f->f_op = fops_get(inode->i_fop);
  597. error = security_file_open(f, cred);
  598. if (error)
  599. goto cleanup_all;
  600. error = break_lease(inode, f->f_flags);
  601. if (error)
  602. goto cleanup_all;
  603. if (!open && f->f_op)
  604. open = f->f_op->open;
  605. if (open) {
  606. error = open(inode, f);
  607. if (error)
  608. goto cleanup_all;
  609. }
  610. if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  611. i_readcount_inc(inode);
  612. f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  613. file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
  614. return 0;
  615. cleanup_all:
  616. fops_put(f->f_op);
  617. file_sb_list_del(f);
  618. if (f->f_mode & FMODE_WRITE) {
  619. put_write_access(inode);
  620. if (!special_file(inode->i_mode)) {
  621. /*
  622. * We don't consider this a real
  623. * mnt_want/drop_write() pair
  624. * because it all happenend right
  625. * here, so just reset the state.
  626. */
  627. file_reset_write(f);
  628. __mnt_drop_write(f->f_path.mnt);
  629. }
  630. }
  631. cleanup_file:
  632. path_put(&f->f_path);
  633. f->f_path.mnt = NULL;
  634. f->f_path.dentry = NULL;
  635. f->f_inode = NULL;
  636. return error;
  637. }
  638. /**
  639. * finish_open - finish opening a file
  640. * @od: opaque open data
  641. * @dentry: pointer to dentry
  642. * @open: open callback
  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. int finish_open(struct file *file, struct dentry *dentry,
  650. int (*open)(struct inode *, struct file *),
  651. int *opened)
  652. {
  653. int error;
  654. BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
  655. file->f_path.dentry = dentry;
  656. error = do_dentry_open(file, open, current_cred());
  657. if (!error)
  658. *opened |= FILE_OPENED;
  659. return error;
  660. }
  661. EXPORT_SYMBOL(finish_open);
  662. /**
  663. * finish_no_open - finish ->atomic_open() without opening the file
  664. *
  665. * @od: opaque open data
  666. * @dentry: dentry or NULL (as returned from ->lookup())
  667. *
  668. * This can be used to set the result of a successful lookup in ->atomic_open().
  669. * The filesystem's atomic_open() method shall return NULL after calling this.
  670. */
  671. int finish_no_open(struct file *file, struct dentry *dentry)
  672. {
  673. file->f_path.dentry = dentry;
  674. return 1;
  675. }
  676. EXPORT_SYMBOL(finish_no_open);
  677. struct file *dentry_open(const struct path *path, int flags,
  678. const struct cred *cred)
  679. {
  680. int error;
  681. struct file *f;
  682. validate_creds(cred);
  683. /* We must always pass in a valid mount pointer. */
  684. BUG_ON(!path->mnt);
  685. f = get_empty_filp();
  686. if (!IS_ERR(f)) {
  687. f->f_flags = flags;
  688. f->f_path = *path;
  689. error = do_dentry_open(f, NULL, cred);
  690. if (!error) {
  691. /* from now on we need fput() to dispose of f */
  692. error = open_check_o_direct(f);
  693. if (error) {
  694. fput(f);
  695. f = ERR_PTR(error);
  696. }
  697. } else {
  698. put_filp(f);
  699. f = ERR_PTR(error);
  700. }
  701. }
  702. return f;
  703. }
  704. EXPORT_SYMBOL(dentry_open);
  705. static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
  706. {
  707. int lookup_flags = 0;
  708. int acc_mode;
  709. if (flags & O_CREAT)
  710. op->mode = (mode & S_IALLUGO) | S_IFREG;
  711. else
  712. op->mode = 0;
  713. /* Must never be set by userspace */
  714. flags &= ~FMODE_NONOTIFY & ~O_CLOEXEC;
  715. /*
  716. * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
  717. * check for O_DSYNC if the need any syncing at all we enforce it's
  718. * always set instead of having to deal with possibly weird behaviour
  719. * for malicious applications setting only __O_SYNC.
  720. */
  721. if (flags & __O_SYNC)
  722. flags |= O_DSYNC;
  723. /*
  724. * If we have O_PATH in the open flag. Then we
  725. * cannot have anything other than the below set of flags
  726. */
  727. if (flags & O_PATH) {
  728. flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH;
  729. acc_mode = 0;
  730. } else {
  731. acc_mode = MAY_OPEN | ACC_MODE(flags);
  732. }
  733. op->open_flag = flags;
  734. /* O_TRUNC implies we need access checks for write permissions */
  735. if (flags & O_TRUNC)
  736. acc_mode |= MAY_WRITE;
  737. /* Allow the LSM permission hook to distinguish append
  738. access from general write access. */
  739. if (flags & O_APPEND)
  740. acc_mode |= MAY_APPEND;
  741. op->acc_mode = acc_mode;
  742. op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
  743. if (flags & O_CREAT) {
  744. op->intent |= LOOKUP_CREATE;
  745. if (flags & O_EXCL)
  746. op->intent |= LOOKUP_EXCL;
  747. }
  748. if (flags & O_DIRECTORY)
  749. lookup_flags |= LOOKUP_DIRECTORY;
  750. if (!(flags & O_NOFOLLOW))
  751. lookup_flags |= LOOKUP_FOLLOW;
  752. return lookup_flags;
  753. }
  754. /**
  755. * file_open_name - open file and return file pointer
  756. *
  757. * @name: struct filename containing path to open
  758. * @flags: open flags as per the open(2) second argument
  759. * @mode: mode for the new file if O_CREAT is set, else ignored
  760. *
  761. * This is the helper to open a file from kernelspace if you really
  762. * have to. But in generally you should not do this, so please move
  763. * along, nothing to see here..
  764. */
  765. struct file *file_open_name(struct filename *name, int flags, umode_t mode)
  766. {
  767. struct open_flags op;
  768. int lookup = build_open_flags(flags, mode, &op);
  769. return do_filp_open(AT_FDCWD, name, &op, lookup);
  770. }
  771. /**
  772. * filp_open - open file and return file pointer
  773. *
  774. * @filename: path to open
  775. * @flags: open flags as per the open(2) second argument
  776. * @mode: mode for the new file if O_CREAT is set, else ignored
  777. *
  778. * This is the helper to open a file from kernelspace if you really
  779. * have to. But in generally you should not do this, so please move
  780. * along, nothing to see here..
  781. */
  782. struct file *filp_open(const char *filename, int flags, umode_t mode)
  783. {
  784. struct filename name = {.name = filename};
  785. return file_open_name(&name, flags, mode);
  786. }
  787. EXPORT_SYMBOL(filp_open);
  788. struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  789. const char *filename, int flags)
  790. {
  791. struct open_flags op;
  792. int lookup = build_open_flags(flags, 0, &op);
  793. if (flags & O_CREAT)
  794. return ERR_PTR(-EINVAL);
  795. if (!filename && (flags & O_DIRECTORY))
  796. if (!dentry->d_inode->i_op->lookup)
  797. return ERR_PTR(-ENOTDIR);
  798. return do_file_open_root(dentry, mnt, filename, &op, lookup);
  799. }
  800. EXPORT_SYMBOL(file_open_root);
  801. long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
  802. {
  803. struct open_flags op;
  804. int lookup = build_open_flags(flags, mode, &op);
  805. struct filename *tmp = getname(filename);
  806. int fd = PTR_ERR(tmp);
  807. if (!IS_ERR(tmp)) {
  808. fd = get_unused_fd_flags(flags);
  809. if (fd >= 0) {
  810. struct file *f = do_filp_open(dfd, tmp, &op, lookup);
  811. if (IS_ERR(f)) {
  812. put_unused_fd(fd);
  813. fd = PTR_ERR(f);
  814. } else {
  815. fsnotify_open(f);
  816. fd_install(fd, f);
  817. }
  818. }
  819. putname(tmp);
  820. }
  821. return fd;
  822. }
  823. SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
  824. {
  825. if (force_o_largefile())
  826. flags |= O_LARGEFILE;
  827. return do_sys_open(AT_FDCWD, filename, flags, mode);
  828. }
  829. SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
  830. umode_t, mode)
  831. {
  832. if (force_o_largefile())
  833. flags |= O_LARGEFILE;
  834. return do_sys_open(dfd, filename, flags, mode);
  835. }
  836. #ifndef __alpha__
  837. /*
  838. * For backward compatibility? Maybe this should be moved
  839. * into arch/i386 instead?
  840. */
  841. SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
  842. {
  843. return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
  844. }
  845. #endif
  846. /*
  847. * "id" is the POSIX thread ID. We use the
  848. * files pointer for this..
  849. */
  850. int filp_close(struct file *filp, fl_owner_t id)
  851. {
  852. int retval = 0;
  853. if (!file_count(filp)) {
  854. printk(KERN_ERR "VFS: Close: file count is 0\n");
  855. return 0;
  856. }
  857. if (filp->f_op && filp->f_op->flush)
  858. retval = filp->f_op->flush(filp, id);
  859. if (likely(!(filp->f_mode & FMODE_PATH))) {
  860. dnotify_flush(filp, id);
  861. locks_remove_posix(filp, id);
  862. }
  863. fput(filp);
  864. return retval;
  865. }
  866. EXPORT_SYMBOL(filp_close);
  867. /*
  868. * Careful here! We test whether the file pointer is NULL before
  869. * releasing the fd. This ensures that one clone task can't release
  870. * an fd while another clone is opening it.
  871. */
  872. SYSCALL_DEFINE1(close, unsigned int, fd)
  873. {
  874. int retval = __close_fd(current->files, fd);
  875. /* can't restart close syscall because file table entry was cleared */
  876. if (unlikely(retval == -ERESTARTSYS ||
  877. retval == -ERESTARTNOINTR ||
  878. retval == -ERESTARTNOHAND ||
  879. retval == -ERESTART_RESTARTBLOCK))
  880. retval = -EINTR;
  881. return retval;
  882. }
  883. EXPORT_SYMBOL(sys_close);
  884. /*
  885. * This routine simulates a hangup on the tty, to arrange that users
  886. * are given clean terminals at login time.
  887. */
  888. SYSCALL_DEFINE0(vhangup)
  889. {
  890. if (capable(CAP_SYS_TTY_CONFIG)) {
  891. tty_vhangup_self();
  892. return 0;
  893. }
  894. return -EPERM;
  895. }
  896. /*
  897. * Called when an inode is about to be open.
  898. * We use this to disallow opening large files on 32bit systems if
  899. * the caller didn't specify O_LARGEFILE. On 64bit systems we force
  900. * on this flag in sys_open.
  901. */
  902. int generic_file_open(struct inode * inode, struct file * filp)
  903. {
  904. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  905. return -EOVERFLOW;
  906. return 0;
  907. }
  908. EXPORT_SYMBOL(generic_file_open);
  909. /*
  910. * This is used by subsystems that don't want seekable
  911. * file descriptors. The function is not supposed to ever fail, the only
  912. * reason it returns an 'int' and not 'void' is so that it can be plugged
  913. * directly into file_operations structure.
  914. */
  915. int nonseekable_open(struct inode *inode, struct file *filp)
  916. {
  917. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  918. return 0;
  919. }
  920. EXPORT_SYMBOL(nonseekable_open);