open.c 24 KB

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