open.c 24 KB

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