open.c 25 KB

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