open.c 25 KB

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