open.c 25 KB

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