open.c 25 KB

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