open.c 24 KB

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