open.c 26 KB

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